1 /* ChanNode.c - NeonServ v5.3
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 "bot_NeonSpam.h"
24 #include "IRCEvents.h"
26 static struct ChanNode **chanList;
28 pthread_mutex_t cache_sync;
31 void init_ChanNode() {
32 THREAD_MUTEX_INIT(cache_sync);
38 1 46 *everything else*
39 ---------------------------
42 #define CHANNEL_LIST_SIZE 47
43 chanList = calloc(CHANNEL_LIST_SIZE, sizeof(*chanList));
46 void free_ChanNode() {
47 SYNCHRONIZE(cache_sync);
48 //kamikaze free all channels and chanusers
50 struct ChanNode *chan, *next;
51 struct ChanUser *chanuser, *next_chanuser;
52 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
53 for(chan = chanList[i]; chan; chan = next) {
55 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next_chanuser) {
56 next_chanuser = getChannelUsers(chan, chanuser);
57 freeChanUser(chanuser);
63 DESYNCHRONIZE(cache_sync);
66 int is_valid_chan(const char *name) {
70 for (ii=1; name[ii]; ++ii) {
71 if ((name[ii] > 0) && (name[ii] <= 32))
75 if (name[ii] == '\xa0')
81 static int get_chanlist_entry(int name) {
82 if((name > 0 && name <= 32) || name == ',' || name == '\xa0') return -1; //invalid name
83 if(tolower(name) >= 97 && tolower(name) <= 122) {
84 return (tolower(name) - 97);
86 if(tolower(name) >= 48 && tolower(name) <= 57) {
87 return (tolower(name) - 48 + 26);
90 if(name == '{') return 36;
91 if(name == '|') return 37;
92 if(name == '}') return 38;
93 if(name == '~') return 39;
94 if(name == '[') return 40;
95 if(name == '\\') return 41;
96 if(name == ']') return 42;
97 if(name == '^') return 43;
98 if(name == '_') return 44;
99 if(name == '`') return 45;
103 struct ChanNode* getAllChans(struct ChanNode *last) {
104 if(last == NULL || last->next == NULL) {
109 cindex = get_chanlist_entry(last->name[1]) + 1;
110 while(chanList[cindex] == NULL && cindex < CHANNEL_LIST_SIZE)
112 if(cindex >= CHANNEL_LIST_SIZE) return NULL;
113 return chanList[cindex];
119 struct ChanNode* getChanByName(const char *name) { //case insensitive
120 int chanListIndex = get_chanlist_entry(name[1]);
121 if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
123 struct ChanNode *chan;
124 for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
125 if(!stricmp(name, chan->name))
131 struct ChanNode* addChannel(const char *name) {
132 int chanListIndex = get_chanlist_entry(name[1]);
133 if(chanListIndex == -1 || !is_valid_chan(name))
135 struct ChanNode *chan = malloc(sizeof(*chan));
138 perror("malloc() failed");
141 strcpy(chan->name, name);
144 chan->spam_settings = NULL;
146 chan->chanbot = NULL;
150 chan->modes = createModeNode(chan);
151 chan->trigger = NULL;
153 SYNCHRONIZE(cache_sync);
154 chan->next = chanList[chanListIndex];
155 chanList[chanListIndex] = chan;
156 DESYNCHRONIZE(cache_sync);
160 int getChannelCount() {
162 struct ChanNode *chan;
163 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
164 for(chan = chanList[i]; chan; chan = chan->next) {
171 int getChanUserCount() {
173 struct ChanNode *chan;
174 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
175 for(chan = chanList[i]; chan; chan = chan->next) {
176 count += chan->usercount;
182 int getChanBanCount() {
184 struct ChanNode *chan;
186 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
187 for(chan = chanList[i]; chan; chan = chan->next) {
188 for(ban = chan->bans; ban; ban = ban->next)
195 void delChannel(struct ChanNode* chan, int freeChan) {
196 int chanListIndex = get_chanlist_entry(chan->name[1]);
197 if(chanListIndex == -1) return;
198 SYNCHRONIZE(cache_sync);
199 struct ChanNode *cchan, *last_chan = NULL;
200 for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
203 last_chan->next = chan->next;
205 chanList[chanListIndex] = chan->next;
212 struct ChanUser *chanuser, *next;
213 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
214 next = getChannelUsers(chan, chanuser);
215 removeChanUserFromLists(chanuser, 0, 1, 1);
222 DESYNCHRONIZE(cache_sync);
225 void freeChanNode(struct ChanNode* chan) {
226 event_freechan(chan);
228 struct trigger_cache *trigger, *next_trigger;
229 for(trigger = chan->trigger; trigger; trigger = next_trigger) {
230 next_trigger = trigger->next;
231 free(trigger->trigger);
235 freeModeNode(chan->modes);
237 removeChannelBans(chan);
238 if(chan->spam_settings)
239 freeNeonSpamSettings(chan->spam_settings);
243 int checkChannelVisibility(struct ChanNode* chan) {
244 struct ChanUser *chanuser, *next;
245 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
246 if(chanuser->user->flags & USERFLAG_ISBOT) {
247 chan->chanbot = chanuser->user;
251 //free the channel...
252 SYNCHRONIZE(cache_sync);
253 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
254 next = getChannelUsers(chan, chanuser);
255 //remove the channel from the user's channel-list
256 removeChanUserFromLists(chanuser, 0, 1, 0);
257 if(!chanuser->user->channel) {
258 //free the user (no more channels)
259 delUser(chanuser->user, 1);
261 freeChanUser(chanuser);
265 DESYNCHRONIZE(cache_sync);