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