removed unused code in bots.c
[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
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->botcount = 0;
142     chan->topic[0] = 0;
143     chan->flags = 0;
144     /* mode lists */
145     chan->modes = createModeNode(chan);
146     chan->trigger = NULL;
147     
148     SYNCHRONIZE(cache_sync);
149     chan->next = chanList[chanListIndex];
150     chanList[chanListIndex] = chan;
151     DESYNCHRONIZE(cache_sync);
152     return chan;
153 }
154
155 int getChannelCount() {
156     int i, count = 0;
157     struct ChanNode *chan;
158     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
159         for(chan = chanList[i]; chan; chan = chan->next) {
160             count++;
161         }
162     }
163     return count;
164 }
165
166 int getChanUserCount() {
167     int i, count = 0;
168     struct ChanNode *chan;
169     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
170         for(chan = chanList[i]; chan; chan = chan->next) {
171             count += chan->usercount;
172         }
173     }
174     return count;
175 }
176
177 int getChanBanCount() {
178     int i, count = 0;
179     struct ChanNode *chan;
180     struct BanNode *ban;
181     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
182         for(chan = chanList[i]; chan; chan = chan->next) {
183             for(ban = chan->bans; ban; ban = ban->next)
184                 count ++;
185         }
186     }
187     return count;
188 }
189
190 void delChannel(struct ChanNode* chan, int freeChan) {
191     int chanListIndex = get_chanlist_entry(chan->name[1]);
192     if(chanListIndex == -1) return;
193     SYNCHRONIZE(cache_sync);
194     struct ChanNode *cchan, *last_chan = NULL;
195     for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
196         if(cchan == chan) {
197             if(last_chan)
198                 last_chan->next = chan->next;
199             else
200                 chanList[chanListIndex] = chan->next;
201             break;
202         } else
203             last_chan = cchan;
204     }
205     if(chan->user) {
206         //free all chanusers
207         struct ChanUser *chanuser, *next;
208         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
209             next = getChannelUsers(chan, chanuser);
210             removeChanUserFromLists(chanuser, 0, 1, 1);
211         }
212     }
213     if(freeChan)
214         freeChanNode(chan);
215     else
216         chan->next = NULL;
217     DESYNCHRONIZE(cache_sync);
218 }
219
220 void freeChanNode(struct ChanNode* chan) {
221     event_freechan(chan);
222     if(chan->trigger) {
223         struct trigger_cache *trigger, *next_trigger;
224         for(trigger = chan->trigger; trigger; trigger = next_trigger) {
225             next_trigger = trigger->next;
226             free(trigger->trigger);
227             free(trigger);
228         }
229     }
230     freeModeNode(chan->modes);
231     if(chan->bans)
232         removeChannelBans(chan);
233     free(chan);
234 }
235
236 int checkChannelVisibility(struct ChanNode* chan) {
237     struct ChanUser *chanuser, *next;
238     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
239         if(chanuser->user->flags & USERFLAG_ISBOT)
240             return 1;
241     }
242     //free the channel...
243     SYNCHRONIZE(cache_sync);
244     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
245         next = getChannelUsers(chan, chanuser);
246         //remove the channel from the user's channel-list
247         removeChanUserFromLists(chanuser, 0, 1, 0);
248         if(!chanuser->user->channel) {
249             //free the user (no more channels)
250             delUser(chanuser->user, 1);
251         }
252         freeChanUser(chanuser);
253     }
254     chan->user = NULL;
255     delChannel(chan, 1);
256     DESYNCHRONIZE(cache_sync);
257     return 0;
258 }