1 /* ChanNode.c - NeonServ v5.1
2 * Copyright (C) 2011 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "bot_NeonSpam.h"
25 static struct ChanNode **chanList;
27 void init_ChanNode() {
33 1 46 *everything else*
34 ---------------------------
37 #define CHANNEL_LIST_SIZE 47
38 chanList = calloc(CHANNEL_LIST_SIZE, sizeof(*chanList));
41 void free_ChanNode() {
42 //kamikaze free all channels and chanusers
44 struct ChanNode *chan, *next;
45 struct ChanUser *chanuser, *next_chanuser;
46 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
47 for(chan = chanList[i]; chan; chan = next) {
49 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next_chanuser) {
50 next_chanuser = getChannelUsers(chan, chanuser);
51 if(chanuser->spamnode)
52 free(chanuser->spamnode);
61 int is_valid_chan(const char *name) {
65 for (ii=1; name[ii]; ++ii) {
66 if ((name[ii] > 0) && (name[ii] <= 32))
70 if (name[ii] == '\xa0')
76 static int get_chanlist_entry(int name) {
77 if((name > 0 && name <= 32) || name == ',' || name == '\xa0') return -1; //invalid name
78 if(tolower(name) >= 97 && tolower(name) <= 122) {
79 return (tolower(name) - 97);
81 if(tolower(name) >= 48 && tolower(name) <= 57) {
82 return (tolower(name) - 48 + 26);
85 if(name == '{') return 36;
86 if(name == '|') return 37;
87 if(name == '}') return 38;
88 if(name == '~') return 39;
89 if(name == '[') return 40;
90 if(name == '\\') return 41;
91 if(name == ']') return 42;
92 if(name == '^') return 43;
93 if(name == '_') return 44;
94 if(name == '`') return 45;
98 struct ChanNode* getAllChans(struct ChanNode *last) {
99 if(last == NULL || last->next == NULL) {
104 cindex = get_chanlist_entry(last->name[1]) + 1;
105 while(chanList[cindex] == NULL && cindex < CHANNEL_LIST_SIZE)
107 if(cindex >= CHANNEL_LIST_SIZE) return NULL;
108 return chanList[cindex];
113 struct ChanNode* getChanByName(const char *name) { //case insensitive
114 int chanListIndex = get_chanlist_entry(name[1]);
115 if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
117 struct ChanNode *chan;
118 for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
119 if(!stricmp(name, chan->name))
125 struct ChanNode* addChannel(const char *name) {
126 int chanListIndex = get_chanlist_entry(name[1]);
127 if(chanListIndex == -1 || !is_valid_chan(name))
129 struct ChanNode *chan = malloc(sizeof(*chan));
132 perror("malloc() failed");
135 strcpy(chan->name, name);
138 chan->spam_settings = NULL;
140 chan->chanbot = NULL;
144 chan->modes = createModeNode(chan);
145 chan->trigger = NULL;
147 chan->next = chanList[chanListIndex];
148 chanList[chanListIndex] = chan;
152 int getChannelCount() {
154 struct ChanNode *chan;
155 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
156 for(chan = chanList[i]; chan; chan = chan->next) {
163 int getChanUserCount() {
165 struct ChanNode *chan;
166 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
167 for(chan = chanList[i]; chan; chan = chan->next) {
168 count += chan->usercount;
174 int getChanBanCount() {
176 struct ChanNode *chan;
178 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
179 for(chan = chanList[i]; chan; chan = chan->next) {
180 for(ban = chan->bans; ban; ban = ban->next)
187 void delChannel(struct ChanNode* chan, int freeChan) {
188 int chanListIndex = get_chanlist_entry(chan->name[1]);
189 if(chanListIndex == -1) return;
190 struct ChanNode *cchan, *last_chan = NULL;
191 for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
194 last_chan->next = chan->next;
196 chanList[chanListIndex] = chan->next;
203 struct ChanUser *chanuser, *next;
204 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
205 next = getChannelUsers(chan, chanuser);
206 removeChanUserFromLists(chanuser, 0, 1, 1);
215 void freeChanNode(struct ChanNode* chan) {
217 struct trigger_cache *trigger, *next_trigger;
218 for(trigger = chan->trigger; trigger; trigger = next_trigger) {
219 next_trigger = trigger->next;
220 free(trigger->trigger);
224 freeModeNode(chan->modes);
226 removeChannelBans(chan);
227 if(chan->spam_settings)
228 freeNeonSpamSettings(chan->spam_settings);
232 void checkChannelVisibility(struct ChanNode* chan) {
233 struct ChanUser *chanuser, *next;
234 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
235 if(chanuser->user->flags & USERFLAG_ISBOT) {
236 chan->chanbot = chanuser->user;
240 //free the channel...
241 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
242 next = getChannelUsers(chan, chanuser);
243 //remove the channel from the user's channel-list
244 removeChanUserFromLists(chanuser, 0, 1, 0);
245 if(!chanuser->user->channel) {
246 //free the user (no more channels)
247 delUser(chanuser->user, 1);
249 if(chanuser->spamnode)
250 free(chanuser->spamnode);