9641df31f0d1083192502d36ad31d4952f0bc260
[NeonServV5.git] / src / ChanNode.c
1 /* ChanNode.c - NeonServ v5.3
2  * Copyright (C) 2011-2012  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 #include "IRCEvents.h"
25
26 static struct ChanNode **chanList;
27
28 void init_ChanNode() {
29     /*
30      len pos chars 
31      26  0   a-z
32      10  26  0-9
33      10  36  {|}~[\]^_`
34      1   46  *everything else*
35      ---------------------------
36      = 47
37     */
38     #define CHANNEL_LIST_SIZE 47
39     chanList = calloc(CHANNEL_LIST_SIZE, sizeof(*chanList));
40 }
41
42 void free_ChanNode() {
43     //kamikaze free all channels and chanusers
44     int i;
45     struct ChanNode *chan, *next;
46     struct ChanUser *chanuser, *next_chanuser;
47     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
48         for(chan = chanList[i]; chan; chan = next) {
49             next = chan->next;
50             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next_chanuser) {
51                 next_chanuser = getChannelUsers(chan, chanuser);
52                 freeChanUser(chanuser);
53             }
54             freeChanNode(chan);
55         }
56     }
57     free(chanList);
58 }
59
60 int is_valid_chan(const char *name) {
61     unsigned int ii;
62     if (*name !='#')
63         return 0;
64     for (ii=1; name[ii]; ++ii) {
65         if ((name[ii] > 0) && (name[ii] <= 32))
66             return 0;
67         if (name[ii] == ',')
68             return 0;
69         if (name[ii] == '\xa0')
70             return 0;
71     }
72     return 1;
73 }
74
75 static int get_chanlist_entry(int name) {
76     if((name > 0 && name <= 32) || name == ',' || name == '\xa0') return -1; //invalid name
77     if(tolower(name) >= 97 && tolower(name) <= 122) {
78         return (tolower(name) - 97);
79     }
80     if(tolower(name) >= 48 && tolower(name) <= 57) {
81         return (tolower(name) - 48 + 26);
82     }
83     /* {|}~[\]^_` */
84     if(name == '{') return 36;
85     if(name == '|') return 37;
86     if(name == '}') return 38;
87     if(name == '~') return 39;
88     if(name == '[') return 40;
89     if(name == '\\') return 41;
90     if(name == ']') return 42;
91     if(name == '^') return 43;
92     if(name == '_') return 44;
93     if(name == '`') return 45;
94     return 46;
95 }
96
97 struct ChanNode* getAllChans(struct ChanNode *last) {
98     if(last == NULL || last->next == NULL) {
99         int cindex;
100         if(last == NULL)
101             cindex = 0;
102         else
103             cindex = get_chanlist_entry(last->name[1]) + 1;
104         while(chanList[cindex] == NULL && cindex < CHANNEL_LIST_SIZE)
105             cindex++;
106         if(cindex >= CHANNEL_LIST_SIZE) return NULL;
107         return chanList[cindex];
108     } else
109         return last->next;
110 }
111
112 struct ChanNode* getChanByName(const char *name) { //case insensitive
113     int chanListIndex = get_chanlist_entry(name[1]);
114     if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
115         return NULL;
116     struct ChanNode *chan;
117     for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
118         if(!stricmp(name, chan->name))
119             return chan;
120     }
121     return NULL;
122 }
123
124 struct ChanNode* addChannel(const char *name) {
125     int chanListIndex = get_chanlist_entry(name[1]);
126     if(chanListIndex == -1 || !is_valid_chan(name))
127         return NULL;
128     struct ChanNode *chan = malloc(sizeof(*chan));
129     if (!chan)
130     {
131         perror("malloc() failed");
132         return NULL;
133     }
134     strcpy(chan->name, name);
135     chan->user = NULL;
136     chan->bans = NULL;
137     chan->spam_settings = NULL;
138     chan->usercount = 0;
139     chan->chanbot = NULL;
140     chan->topic[0] = 0;
141     chan->flags = 0;
142     /* mode lists */
143     chan->modes = createModeNode(chan);
144     chan->trigger = NULL;
145     
146     chan->next = chanList[chanListIndex];
147     chanList[chanListIndex] = chan;
148     return chan;
149 }
150
151 int getChannelCount() {
152     int i, count = 0;
153     struct ChanNode *chan;
154     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
155         for(chan = chanList[i]; chan; chan = chan->next) {
156             count++;
157         }
158     }
159     return count;
160 }
161
162 int getChanUserCount() {
163     int i, count = 0;
164     struct ChanNode *chan;
165     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
166         for(chan = chanList[i]; chan; chan = chan->next) {
167             count += chan->usercount;
168         }
169     }
170     return count;
171 }
172
173 int getChanBanCount() {
174     int i, count = 0;
175     struct ChanNode *chan;
176     struct BanNode *ban;
177     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
178         for(chan = chanList[i]; chan; chan = chan->next) {
179             for(ban = chan->bans; ban; ban = ban->next)
180                 count ++;
181         }
182     }
183     return count;
184 }
185
186 void delChannel(struct ChanNode* chan, int freeChan) {
187     int chanListIndex = get_chanlist_entry(chan->name[1]);
188     if(chanListIndex == -1) return;
189     struct ChanNode *cchan, *last_chan = NULL;
190     for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
191         if(cchan == chan) {
192             if(last_chan)
193                 last_chan->next = chan->next;
194             else
195                 chanList[chanListIndex] = chan->next;
196             break;
197         } else
198             last_chan = cchan;
199     }
200     if(chan->user) {
201         //free all chanusers
202         struct ChanUser *chanuser, *next;
203         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
204             next = getChannelUsers(chan, chanuser);
205             removeChanUserFromLists(chanuser, 0, 1, 1);
206         }
207     }
208     if(freeChan)
209         freeChanNode(chan);
210     else
211         chan->next = NULL;
212 }
213
214 void freeChanNode(struct ChanNode* chan) {
215     event_freechan(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 int 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 1;
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         freeChanUser(chanuser);
250     }
251     chan->user = NULL;
252     delChannel(chan, 1);
253     return 0;
254 }