fixed some missing includes
[NeonServV5.git] / src / ChanNode.c
1 /* ChanNode.c - NeonServ v5.6
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 "IRCEvents.h"
24 #include "tools.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     SYNCHRONIZE(cache_sync);
44     //kamikaze free all channels and chanusers
45     int i;
46     struct ChanNode *chan, *next;
47     struct ChanUser *chanuser, *next_chanuser;
48     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
49         for(chan = chanList[i]; chan; chan = next) {
50             next = chan->next;
51             for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next_chanuser) {
52                 next_chanuser = getChannelUsers(chan, chanuser);
53                 freeChanUser(chanuser);
54             }
55             freeChanNode(chan);
56         }
57     }
58     free(chanList);
59     DESYNCHRONIZE(cache_sync);
60 }
61
62 int is_valid_chan(const char *name) {
63     unsigned int ii;
64     if (*name !='#')
65         return 0;
66     for (ii=1; name[ii]; ++ii) {
67         if ((name[ii] > 0) && (name[ii] <= 32))
68             return 0;
69         if (name[ii] == ',')
70             return 0;
71         if (name[ii] == '\xa0')
72             return 0;
73     }
74     return 1;
75 }
76
77 static int get_chanlist_entry(int name) {
78     if((name > 0 && name <= 32) || name == ',' || name == '\xa0') return -1; //invalid name
79     if(tolower(name) >= 97 && tolower(name) <= 122) {
80         return (tolower(name) - 97);
81     }
82     if(tolower(name) >= 48 && tolower(name) <= 57) {
83         return (tolower(name) - 48 + 26);
84     }
85     /* {|}~[\]^_` */
86     if(name == '{') return 36;
87     if(name == '|') return 37;
88     if(name == '}') return 38;
89     if(name == '~') return 39;
90     if(name == '[') return 40;
91     if(name == '\\') return 41;
92     if(name == ']') return 42;
93     if(name == '^') return 43;
94     if(name == '_') return 44;
95     if(name == '`') return 45;
96     return 46;
97 }
98
99 struct ChanNode* getAllChans(struct ChanNode *last) {
100     if(last == NULL || last->next == NULL) {
101         int cindex;
102         if(last == NULL)
103             cindex = 0;
104         else
105             cindex = get_chanlist_entry(last->name[1]) + 1;
106         while(chanList[cindex] == NULL && cindex < CHANNEL_LIST_SIZE)
107             cindex++;
108         if(cindex >= CHANNEL_LIST_SIZE) return NULL;
109         return chanList[cindex];
110     } else {
111         return last->next;
112     }
113 }
114
115 struct ChanNode* getChanByName(const char *name) { //case insensitive
116     int chanListIndex = get_chanlist_entry(name[1]);
117     if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
118         return NULL;
119     struct ChanNode *chan;
120     for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
121         if(!stricmp(name, chan->name))
122             return chan;
123     }
124     return NULL;
125 }
126
127 struct ChanNode* addChannel(const char *name) {
128     int chanListIndex = get_chanlist_entry(name[1]);
129     if(chanListIndex == -1 || !is_valid_chan(name))
130         return NULL;
131     struct ChanNode *chan = malloc(sizeof(*chan));
132     if (!chan)
133     {
134         perror("malloc() failed");
135         return NULL;
136     }
137     strcpy(chan->name, name);
138     chan->user = NULL;
139     chan->bans = NULL;
140     chan->spam_settings = NULL;
141     chan->usercount = 0;
142     chan->botcount = 0;
143     chan->topic[0] = 0;
144     chan->flags = 0;
145     /* mode lists */
146     chan->modes = createModeNode(chan);
147     chan->trigger = NULL;
148     
149     SYNCHRONIZE(cache_sync);
150     chan->next = chanList[chanListIndex];
151     chanList[chanListIndex] = chan;
152     DESYNCHRONIZE(cache_sync);
153     return chan;
154 }
155
156 int getChannelCount() {
157     int i, count = 0;
158     struct ChanNode *chan;
159     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
160         for(chan = chanList[i]; chan; chan = chan->next) {
161             count++;
162         }
163     }
164     return count;
165 }
166
167 int getChanUserCount() {
168     int i, count = 0;
169     struct ChanNode *chan;
170     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
171         for(chan = chanList[i]; chan; chan = chan->next) {
172             count += chan->usercount;
173         }
174     }
175     return count;
176 }
177
178 int getChanBanCount() {
179     int i, count = 0;
180     struct ChanNode *chan;
181     struct BanNode *ban;
182     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
183         for(chan = chanList[i]; chan; chan = chan->next) {
184             for(ban = chan->bans; ban; ban = ban->next)
185                 count ++;
186         }
187     }
188     return count;
189 }
190
191 void delChannel(struct ChanNode* chan, int freeChan) {
192     int chanListIndex = get_chanlist_entry(chan->name[1]);
193     if(chanListIndex == -1) return;
194     SYNCHRONIZE(cache_sync);
195     struct ChanNode *cchan, *last_chan = NULL;
196     for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
197         if(cchan == chan) {
198             if(last_chan)
199                 last_chan->next = chan->next;
200             else
201                 chanList[chanListIndex] = chan->next;
202             break;
203         } else
204             last_chan = cchan;
205     }
206     if(chan->user) {
207         //free all chanusers
208         struct ChanUser *chanuser, *next;
209         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
210             next = getChannelUsers(chan, chanuser);
211             removeChanUserFromLists(chanuser, 0, 1, 1);
212         }
213     }
214     if(freeChan)
215         freeChanNode(chan);
216     else
217         chan->next = NULL;
218     DESYNCHRONIZE(cache_sync);
219 }
220
221 void freeChanNode(struct ChanNode* chan) {
222     event_freechan(chan);
223     if(chan->trigger) {
224         struct trigger_cache *trigger, *next_trigger;
225         for(trigger = chan->trigger; trigger; trigger = next_trigger) {
226             next_trigger = trigger->next;
227             free(trigger->trigger);
228             free(trigger);
229         }
230     }
231     freeModeNode(chan->modes);
232     if(chan->bans)
233         removeChannelBans(chan);
234     free(chan);
235 }
236
237 int checkChannelVisibility(struct ChanNode* chan) {
238     struct ChanUser *chanuser, *next;
239     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
240         if(chanuser->user->flags & USERFLAG_ISBOT)
241             return 1;
242     }
243     //free the channel...
244     SYNCHRONIZE(cache_sync);
245     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
246         next = getChannelUsers(chan, chanuser);
247         //remove the channel from the user's channel-list
248         removeChanUserFromLists(chanuser, 0, 1, 0);
249         if(!chanuser->user->channel) {
250             //free the user (no more channels)
251             delUser(chanuser->user, 1);
252         }
253         freeChanUser(chanuser);
254     }
255     chan->user = NULL;
256     delChannel(chan, 1);
257     DESYNCHRONIZE(cache_sync);
258     return 0;
259 }