added joinflood scanner
[NeonServV5.git] / src / ChanNode.c
1 /* ChanNode.c - NeonServ v5.1
2  * Copyright (C) 2011  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17 #include "ChanNode.h"
18 #include "ChanUser.h"
19 #include "UserNode.h"
20 #include "BanNode.h"
21 #include "modcmd.h"
22 #include "ModeNode.h"
23 #include "bot_NeonSpam.h"
24
25 static struct ChanNode **chanList;
26
27 void init_ChanNode() {
28     /*
29      len pos chars 
30      26  0   a-z
31      10  26  0-9
32      10  36  {|}~[\]^_`
33      1   46  *everything else*
34      ---------------------------
35      = 47
36     */
37     #define CHANNEL_LIST_SIZE 47
38     chanList = calloc(CHANNEL_LIST_SIZE, sizeof(*chanList));
39 }
40
41 void free_ChanNode() {
42     //kamikaze free all channels and chanusers
43     int i;
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) {
48             next = 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);
53                 free(chanuser);
54             }
55             freeChanNode(chan);
56         }
57     }
58     free(chanList);
59 }
60
61 int is_valid_chan(const char *name) {
62     unsigned int ii;
63     if (*name !='#')
64         return 0;
65     for (ii=1; name[ii]; ++ii) {
66         if ((name[ii] > 0) && (name[ii] <= 32))
67             return 0;
68         if (name[ii] == ',')
69             return 0;
70         if (name[ii] == '\xa0')
71             return 0;
72     }
73     return 1;
74 }
75
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);
80     }
81     if(tolower(name) >= 48 && tolower(name) <= 57) {
82         return (tolower(name) - 48 + 26);
83     }
84     /* {|}~[\]^_` */
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;
95     return 46;
96 }
97
98 struct ChanNode* getAllChans(struct ChanNode *last) {
99     if(last == NULL || last->next == NULL) {
100         int cindex;
101         if(last == NULL)
102             cindex = 0;
103         else
104             cindex = get_chanlist_entry(last->name[1]) + 1;
105         while(chanList[cindex] == NULL && cindex < CHANNEL_LIST_SIZE)
106             cindex++;
107         if(cindex >= CHANNEL_LIST_SIZE) return NULL;
108         return chanList[cindex];
109     } else
110         return last->next;
111 }
112
113 struct ChanNode* getChanByName(const char *name) { //case insensitive
114     int chanListIndex = get_chanlist_entry(name[1]);
115     if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
116         return NULL;
117     struct ChanNode *chan;
118     for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
119         if(!stricmp(name, chan->name))
120             return chan;
121     }
122     return NULL;
123 }
124
125 struct ChanNode* addChannel(const char *name) {
126     int chanListIndex = get_chanlist_entry(name[1]);
127     if(chanListIndex == -1 || !is_valid_chan(name))
128         return NULL;
129     struct ChanNode *chan = malloc(sizeof(*chan));
130     if (!chan)
131     {
132         perror("malloc() failed");
133         return NULL;
134     }
135     strcpy(chan->name, name);
136     chan->user = NULL;
137     chan->bans = NULL;
138     chan->spam_settings = NULL;
139     chan->usercount = 0;
140     chan->chanbot = NULL;
141     chan->topic[0] = 0;
142     chan->flags = 0;
143     /* mode lists */
144     chan->modes = createModeNode(chan);
145     chan->trigger = NULL;
146     
147     chan->next = chanList[chanListIndex];
148     chanList[chanListIndex] = chan;
149     return chan;
150 }
151
152 int getChannelCount() {
153     int i, count = 0;
154     struct ChanNode *chan;
155     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
156         for(chan = chanList[i]; chan; chan = chan->next) {
157             count++;
158         }
159     }
160     return count;
161 }
162
163 int getChanUserCount() {
164     int i, count = 0;
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;
169         }
170     }
171     return count;
172 }
173
174 int getChanBanCount() {
175     int i, count = 0;
176     struct ChanNode *chan;
177     struct BanNode *ban;
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)
181                 count ++;
182         }
183     }
184     return count;
185 }
186
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) {
192         if(cchan == chan) {
193             if(last_chan)
194                 last_chan->next = chan->next;
195             else
196                 chanList[chanListIndex] = chan->next;
197             break;
198         } else
199             last_chan = cchan;
200     }
201     if(chan->user) {
202         //free all chanusers
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);
207         }
208     }
209     if(freeChan)
210         freeChanNode(chan);
211     else
212         chan->next = NULL;
213 }
214
215 void freeChanNode(struct ChanNode* chan) {
216     if(chan->trigger) {
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);
221             free(trigger);
222         }
223     }
224     freeModeNode(chan->modes);
225     if(chan->bans)
226         removeChannelBans(chan);
227     if(chan->spam_settings)
228         freeNeonSpamSettings(chan->spam_settings);
229     free(chan);
230 }
231
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;
237             return;
238         }
239     }
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);
248         }
249         if(chanuser->spamnode)
250             free(chanuser->spamnode);
251         free(chanuser);
252     }
253     chan->user = NULL;
254     delChannel(chan, 1);
255 }