1 /* ChanNode.c - NeonServ v5.5
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
23 #include "IRCEvents.h"
25 static struct ChanNode **chanList;
27 void init_ChanNode() {
33 1 46 *everything else*
34 ---------------------------
37 #define CHANNEL_LIST_SIZE 47
38 chanList = calloc(CHANNEL_LIST_SIZE, sizeof(*chanList));
41 void free_ChanNode() {
42 SYNCHRONIZE(cache_sync);
43 //kamikaze free all channels and chanusers
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) {
50 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next_chanuser) {
51 next_chanuser = getChannelUsers(chan, chanuser);
52 freeChanUser(chanuser);
58 DESYNCHRONIZE(cache_sync);
61 int is_valid_chan(const char *name) {
65 for (ii=1; name[ii]; ++ii) {
66 if ((name[ii] > 0) && (name[ii] <= 32))
70 if (name[ii] == '\xa0')
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);
81 if(tolower(name) >= 48 && tolower(name) <= 57) {
82 return (tolower(name) - 48 + 26);
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;
98 struct ChanNode* getAllChans(struct ChanNode *last) {
99 if(last == NULL || last->next == NULL) {
104 cindex = get_chanlist_entry(last->name[1]) + 1;
105 while(chanList[cindex] == NULL && cindex < CHANNEL_LIST_SIZE)
107 if(cindex >= CHANNEL_LIST_SIZE) return NULL;
108 return chanList[cindex];
114 struct ChanNode* getChanByName(const char *name) { //case insensitive
115 int chanListIndex = get_chanlist_entry(name[1]);
116 if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
118 struct ChanNode *chan;
119 for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
120 if(!stricmp(name, chan->name))
126 struct ChanNode* addChannel(const char *name) {
127 int chanListIndex = get_chanlist_entry(name[1]);
128 if(chanListIndex == -1 || !is_valid_chan(name))
130 struct ChanNode *chan = malloc(sizeof(*chan));
133 perror("malloc() failed");
136 strcpy(chan->name, name);
139 chan->spam_settings = NULL;
144 chan->modes = createModeNode(chan);
145 chan->trigger = NULL;
147 SYNCHRONIZE(cache_sync);
148 chan->next = chanList[chanListIndex];
149 chanList[chanListIndex] = chan;
150 DESYNCHRONIZE(cache_sync);
154 int getChannelCount() {
156 struct ChanNode *chan;
157 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
158 for(chan = chanList[i]; chan; chan = chan->next) {
165 int getChanUserCount() {
167 struct ChanNode *chan;
168 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
169 for(chan = chanList[i]; chan; chan = chan->next) {
170 count += chan->usercount;
176 int getChanBanCount() {
178 struct ChanNode *chan;
180 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
181 for(chan = chanList[i]; chan; chan = chan->next) {
182 for(ban = chan->bans; ban; ban = ban->next)
189 void delChannel(struct ChanNode* chan, int freeChan) {
190 int chanListIndex = get_chanlist_entry(chan->name[1]);
191 if(chanListIndex == -1) return;
192 SYNCHRONIZE(cache_sync);
193 struct ChanNode *cchan, *last_chan = NULL;
194 for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
197 last_chan->next = chan->next;
199 chanList[chanListIndex] = chan->next;
206 struct ChanUser *chanuser, *next;
207 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
208 next = getChannelUsers(chan, chanuser);
209 removeChanUserFromLists(chanuser, 0, 1, 1);
216 DESYNCHRONIZE(cache_sync);
219 void freeChanNode(struct ChanNode* chan) {
220 event_freechan(chan);
222 struct trigger_cache *trigger, *next_trigger;
223 for(trigger = chan->trigger; trigger; trigger = next_trigger) {
224 next_trigger = trigger->next;
225 free(trigger->trigger);
229 freeModeNode(chan->modes);
231 removeChannelBans(chan);
235 int checkChannelVisibility(struct ChanNode* chan) {
236 struct ChanUser *chanuser, *next;
237 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
238 if(chanuser->user->flags & USERFLAG_ISBOT)
241 //free the channel...
242 SYNCHRONIZE(cache_sync);
243 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
244 next = getChannelUsers(chan, chanuser);
245 //remove the channel from the user's channel-list
246 removeChanUserFromLists(chanuser, 0, 1, 0);
247 if(!chanuser->user->channel) {
248 //free the user (no more channels)
249 delUser(chanuser->user, 1);
251 freeChanUser(chanuser);
255 DESYNCHRONIZE(cache_sync);