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