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