added GPL header to all files and added INSTALL AUTHORS COPYING files.
[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* getChanByName(const char *name) { //case insensitive
96     int chanListIndex = get_chanlist_entry(name[1]);
97     if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
98         return NULL;
99     struct ChanNode *chan;
100     for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
101         if(!stricmp(name, chan->name))
102             return chan;
103     }
104     return NULL;
105 }
106
107 struct ChanNode* addChannel(const char *name) {
108     int chanListIndex = get_chanlist_entry(name[1]);
109     if(chanListIndex == -1 || !is_valid_chan(name))
110         return NULL;
111     struct ChanNode *chan = malloc(sizeof(*chan));
112     if (!chan)
113     {
114         perror("malloc() failed");
115         return NULL;
116     }
117     strcpy(chan->name, name);
118     chan->user = NULL;
119     chan->bans = NULL;
120     chan->usercount = 0;
121     chan->chanbot = NULL;
122     chan->topic[0] = 0;
123     chan->flags = 0;
124     /* mode lists */
125     chan->modes = createModeNode(chan);
126     chan->trigger = NULL;
127     
128     chan->next = chanList[chanListIndex];
129     chanList[chanListIndex] = chan;
130     return chan;
131 }
132
133 int getChannelCount() {
134     int i, count = 0;
135     struct ChanNode *chan;
136     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
137         for(chan = chanList[i]; chan; chan = chan->next) {
138             count++;
139         }
140     }
141     return count;
142 }
143
144 int getChanUserCount() {
145     int i, count = 0;
146     struct ChanNode *chan;
147     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
148         for(chan = chanList[i]; chan; chan = chan->next) {
149             count += chan->usercount;
150         }
151     }
152     return count;
153 }
154
155 int getChanBanCount() {
156     int i, count = 0;
157     struct ChanNode *chan;
158     struct BanNode *ban;
159     for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
160         for(chan = chanList[i]; chan; chan = chan->next) {
161             for(ban = chan->bans; ban; ban = ban->next)
162                 count ++;
163         }
164     }
165     return count;
166 }
167
168 void delChannel(struct ChanNode* chan, int freeChan) {
169     int chanListIndex = get_chanlist_entry(chan->name[1]);
170     if(chanListIndex == -1) return;
171     struct ChanNode *cchan, *last_chan = NULL;
172     for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
173         if(cchan == chan) {
174             if(last_chan)
175                 last_chan->next = chan->next;
176             else
177                 chanList[chanListIndex] = chan->next;
178             break;
179         } else
180             last_chan = cchan;
181     }
182     if(chan->user) {
183         //free all chanusers
184         struct ChanUser *chanuser, *next;
185         for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
186             next = getChannelUsers(chan, chanuser);
187             removeChanUserFromLists(chanuser, 0, 1, 1);
188         }
189     }
190     if(freeChan)
191         freeChanNode(chan);
192     else
193         chan->next = NULL;
194 }
195
196 void freeChanNode(struct ChanNode* chan) {
197     if(chan->trigger) {
198         struct trigger_cache *trigger, *next_trigger;
199         for(trigger = chan->trigger; trigger; trigger = next_trigger) {
200             next_trigger = trigger->next;
201             free(trigger->trigger);
202             free(trigger);
203         }
204     }
205     freeModeNode(chan->modes);
206     if(chan->bans)
207         removeChannelBans(chan);
208     free(chan);
209 }
210
211 void checkChannelVisibility(struct ChanNode* chan) {
212     struct ChanUser *chanuser, *next;
213     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
214         if(chanuser->user->flags & USERFLAG_ISBOT) {
215             chan->chanbot = chanuser->user;
216             return;
217         }
218     }
219     //free the channel...
220     for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
221         next = getChannelUsers(chan, chanuser);
222         //remove the channel from the user's channel-list
223         removeChanUserFromLists(chanuser, 0, 1, 0);
224         if(!chanuser->user->channel) {
225             //free the user (no more channels)
226             delUser(chanuser->user, 1);
227         }
228         free(chanuser);
229     }
230     chan->user = NULL;
231     delChannel(chan, 1);
232 }