1 /* ChanNode.c - NeonServ v5.6
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"
27 static struct ChanNode **chanList;
29 void init_ChanNode() {
35 1 46 *everything else*
36 ---------------------------
39 #define CHANNEL_LIST_SIZE 47
40 chanList = calloc(CHANNEL_LIST_SIZE, sizeof(*chanList));
43 void free_ChanNode() {
44 SYNCHRONIZE(cache_sync);
45 //kamikaze free all channels and chanusers
47 struct ChanNode *chan, *next;
48 struct ChanUser *chanuser, *next_chanuser;
49 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
50 for(chan = chanList[i]; chan; chan = next) {
52 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next_chanuser) {
53 next_chanuser = getChannelUsers(chan, chanuser);
54 freeChanUser(chanuser);
60 DESYNCHRONIZE(cache_sync);
63 int is_valid_chan(const char *name) {
67 for (ii=1; name[ii]; ++ii) {
68 if ((name[ii] > 0) && (name[ii] <= 32))
72 if (name[ii] == '\xa0')
78 static int get_chanlist_entry(int name) {
79 if((name > 0 && name <= 32) || name == ',' || name == '\xa0') return -1; //invalid name
80 if(tolower(name) >= 97 && tolower(name) <= 122) {
81 return (tolower(name) - 97);
83 if(tolower(name) >= 48 && tolower(name) <= 57) {
84 return (tolower(name) - 48 + 26);
87 if(name == '{') return 36;
88 if(name == '|') return 37;
89 if(name == '}') return 38;
90 if(name == '~') return 39;
91 if(name == '[') return 40;
92 if(name == '\\') return 41;
93 if(name == ']') return 42;
94 if(name == '^') return 43;
95 if(name == '_') return 44;
96 if(name == '`') return 45;
100 struct ChanNode* getAllChans(struct ChanNode *last) {
101 if(last == NULL || last->next == NULL) {
106 cindex = get_chanlist_entry(last->name[1]) + 1;
107 while(chanList[cindex] == NULL && cindex < CHANNEL_LIST_SIZE)
109 if(cindex >= CHANNEL_LIST_SIZE) return NULL;
110 return chanList[cindex];
116 struct ChanNode* getChanByName(const char *name) { //case insensitive
117 int chanListIndex = get_chanlist_entry(name[1]);
118 if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
120 struct ChanNode *chan;
121 for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
122 if(!stricmp(name, chan->name))
128 struct ChanNode* addChannel(const char *name) {
129 int chanListIndex = get_chanlist_entry(name[1]);
130 if(chanListIndex == -1 || !is_valid_chan(name))
132 struct ChanNode *chan = malloc(sizeof(*chan));
135 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
138 strcpy(chan->name, name);
141 chan->spam_settings = NULL;
147 chan->modes = createModeNode(chan);
148 chan->trigger = NULL;
150 SYNCHRONIZE(cache_sync);
151 chan->next = chanList[chanListIndex];
152 chanList[chanListIndex] = chan;
153 DESYNCHRONIZE(cache_sync);
157 int getChannelCount() {
159 struct ChanNode *chan;
160 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
161 for(chan = chanList[i]; chan; chan = chan->next) {
168 int getChanUserCount() {
170 struct ChanNode *chan;
171 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
172 for(chan = chanList[i]; chan; chan = chan->next) {
173 count += chan->usercount;
179 int getChanBanCount() {
181 struct ChanNode *chan;
183 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
184 for(chan = chanList[i]; chan; chan = chan->next) {
185 for(ban = chan->bans; ban; ban = ban->next)
192 void delChannel(struct ChanNode* chan, int freeChan) {
193 int chanListIndex = get_chanlist_entry(chan->name[1]);
194 if(chanListIndex == -1) return;
195 SYNCHRONIZE(cache_sync);
196 struct ChanNode *cchan, *last_chan = NULL;
197 for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
200 last_chan->next = chan->next;
202 chanList[chanListIndex] = chan->next;
209 struct ChanUser *chanuser, *next;
210 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
211 next = getChannelUsers(chan, chanuser);
212 removeChanUserFromLists(chanuser, 0, 1, 1);
219 DESYNCHRONIZE(cache_sync);
222 void freeChanNode(struct ChanNode* chan) {
223 event_freechan(chan);
225 struct trigger_cache *trigger, *next_trigger;
226 for(trigger = chan->trigger; trigger; trigger = next_trigger) {
227 next_trigger = trigger->next;
228 free(trigger->trigger);
232 freeModeNode(chan->modes);
234 removeChannelBans(chan);
238 int checkChannelVisibility(struct ChanNode* chan) {
239 struct ChanUser *chanuser, *next;
240 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
241 if(chanuser->user->flags & USERFLAG_ISBOT)
244 //free the channel...
245 SYNCHRONIZE(cache_sync);
246 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
247 next = getChannelUsers(chan, chanuser);
248 //remove the channel from the user's channel-list
249 removeChanUserFromLists(chanuser, 0, 1, 0);
250 if(!chanuser->user->channel) {
251 //free the user (no more channels)
252 delUser(chanuser->user, 1);
254 freeChanUser(chanuser);
258 DESYNCHRONIZE(cache_sync);