rewrote IRC cache parser to be (hopefully) more stable
[NeonServV5.git] / src / ChanNode.c
1 /* ChanNode.c - NeonServ v5.4
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
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     SYNCHRONIZE(cache_sync);
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     DESYNCHRONIZE(cache_sync);
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
114 struct ChanNode* getChanByName(const char *name) { //case insensitive
115     int chanListIndex = get_chanlist_entry(name[1]);
116     if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
117         return NULL;
118     struct ChanNode *chan;
119     for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
120         if(!stricmp(name, chan->name))
121             return chan;
122     }
123     return NULL;
124 }
125
126 struct ChanNode* addChannel(const char *name) {
127     int chanListIndex = get_chanlist_entry(name[1]);
128     if(chanListIndex == -1 || !is_valid_chan(name))
129         return NULL;
130     struct ChanNode *chan = malloc(sizeof(*chan));
131     if (!chan)
132     {
133         perror("malloc() failed");
134         return NULL;
135     }
136     strcpy(chan->name, name);
137     chan->user = NULL;
138     chan->bans = NULL;
139     chan->spam_settings = NULL;
140     chan->usercount = 0;
141     chan->topic[0] = 0;
142     chan->flags = 0;
143     /* mode lists */
144     chan->modes = createModeNode(chan);
145     chan->trigger = NULL;
146     
147     SYNCHRONIZE(cache_sync);
148     chan->next = chanList[chanListIndex];
149     chanList[chanListIndex] = chan;
150     DESYNCHRONIZE(cache_sync);
151     return chan;
152 }
153
154 int getChannelCount() {
155     int i, count = 0;
156     struct ChanNode *chan;
157     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
158         for(chan = chanList[i]; chan; chan = chan->next) {
159             count++;
160         }
161     }
162     return count;
163 }
164
165 int getChanUserCount() {
166     int i, count = 0;
167     struct ChanNode *chan;
168     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
169         for(chan = chanList[i]; chan; chan = chan->next) {
170             count += chan->usercount;
171         }
172     }
173     return count;
174 }
175
176 int getChanBanCount() {
177     int i, count = 0;
178     struct ChanNode *chan;
179     struct BanNode *ban;
180     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
181         for(chan = chanList[i]; chan; chan = chan->next) {
182             for(ban = chan->bans; ban; ban = ban->next)
183                 count ++;
184         }
185     }
186     return count;
187 }
188
189 void delChannel(struct ChanNode* chan, int freeChan) {
190     int chanListIndex = get_chanlist_entry(chan->name[1]);
191     if(chanListIndex == -1) return;
192     SYNCHRONIZE(cache_sync);
193     struct ChanNode *cchan, *last_chan = NULL;
194     for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
195         if(cchan == chan) {
196             if(last_chan)
197                 last_chan->next = chan->next;
198             else
199                 chanList[chanListIndex] = chan->next;
200             break;
201         } else
202             last_chan = cchan;
203     }
204     if(chan->user) {
205         //free all chanusers
206         struct ChanUser *chanuser, *next;
207         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
208             next = getChannelUsers(chan, chanuser);
209             removeChanUserFromLists(chanuser, 0, 1, 1);
210         }
211     }
212     if(freeChan)
213         freeChanNode(chan);
214     else
215         chan->next = NULL;
216     DESYNCHRONIZE(cache_sync);
217 }
218
219 void freeChanNode(struct ChanNode* chan) {
220     event_freechan(chan);
221     if(chan->trigger) {
222         struct trigger_cache *trigger, *next_trigger;
223         for(trigger = chan->trigger; trigger; trigger = next_trigger) {
224             next_trigger = trigger->next;
225             free(trigger->trigger);
226             free(trigger);
227         }
228     }
229     freeModeNode(chan->modes);
230     if(chan->bans)
231         removeChannelBans(chan);
232     free(chan);
233 }
234
235 int checkChannelVisibility(struct ChanNode* chan) {
236     struct ChanUser *chanuser, *next;
237     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
238         if(chanuser->user->flags & USERFLAG_ISBOT)
239             return 1;
240     }
241     //free the channel...
242     SYNCHRONIZE(cache_sync);
243     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
244         next = getChannelUsers(chan, chanuser);
245         //remove the channel from the user's channel-list
246         removeChanUserFromLists(chanuser, 0, 1, 0);
247         if(!chanuser->user->channel) {
248             //free the user (no more channels)
249             delUser(chanuser->user, 1);
250         }
251         freeChanUser(chanuser);
252     }
253     chan->user = NULL;
254     delChannel(chan, 1);
255     DESYNCHRONIZE(cache_sync);
256     return 0;
257 }