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