1 /* ChanNode.c - NeonServ v5.2
2 * Copyright (C) 2011 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"
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 //kamikaze free all channels and chanusers
44 struct ChanNode *chan, *next;
45 struct ChanUser *chanuser, *next_chanuser;
46 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
47 for(chan = chanList[i]; chan; chan = next) {
49 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next_chanuser) {
50 next_chanuser = getChannelUsers(chan, chanuser);
51 freeChanUser(chanuser);
59 int is_valid_chan(const char *name) {
63 for (ii=1; name[ii]; ++ii) {
64 if ((name[ii] > 0) && (name[ii] <= 32))
68 if (name[ii] == '\xa0')
74 static int get_chanlist_entry(int name) {
75 if((name > 0 && name <= 32) || name == ',' || name == '\xa0') return -1; //invalid name
76 if(tolower(name) >= 97 && tolower(name) <= 122) {
77 return (tolower(name) - 97);
79 if(tolower(name) >= 48 && tolower(name) <= 57) {
80 return (tolower(name) - 48 + 26);
83 if(name == '{') return 36;
84 if(name == '|') return 37;
85 if(name == '}') return 38;
86 if(name == '~') return 39;
87 if(name == '[') return 40;
88 if(name == '\\') return 41;
89 if(name == ']') return 42;
90 if(name == '^') return 43;
91 if(name == '_') return 44;
92 if(name == '`') return 45;
96 struct ChanNode* getAllChans(struct ChanNode *last) {
97 if(last == NULL || last->next == NULL) {
102 cindex = get_chanlist_entry(last->name[1]) + 1;
103 while(chanList[cindex] == NULL && cindex < CHANNEL_LIST_SIZE)
105 if(cindex >= CHANNEL_LIST_SIZE) return NULL;
106 return chanList[cindex];
111 struct ChanNode* getChanByName(const char *name) { //case insensitive
112 int chanListIndex = get_chanlist_entry(name[1]);
113 if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
115 struct ChanNode *chan;
116 for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
117 if(!stricmp(name, chan->name))
123 struct ChanNode* addChannel(const char *name) {
124 int chanListIndex = get_chanlist_entry(name[1]);
125 if(chanListIndex == -1 || !is_valid_chan(name))
127 struct ChanNode *chan = malloc(sizeof(*chan));
130 perror("malloc() failed");
133 strcpy(chan->name, name);
136 chan->spam_settings = NULL;
138 chan->chanbot = NULL;
142 chan->modes = createModeNode(chan);
143 chan->trigger = NULL;
145 chan->next = chanList[chanListIndex];
146 chanList[chanListIndex] = chan;
150 int getChannelCount() {
152 struct ChanNode *chan;
153 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
154 for(chan = chanList[i]; chan; chan = chan->next) {
161 int getChanUserCount() {
163 struct ChanNode *chan;
164 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
165 for(chan = chanList[i]; chan; chan = chan->next) {
166 count += chan->usercount;
172 int getChanBanCount() {
174 struct ChanNode *chan;
176 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
177 for(chan = chanList[i]; chan; chan = chan->next) {
178 for(ban = chan->bans; ban; ban = ban->next)
185 void delChannel(struct ChanNode* chan, int freeChan) {
186 int chanListIndex = get_chanlist_entry(chan->name[1]);
187 if(chanListIndex == -1) return;
188 struct ChanNode *cchan, *last_chan = NULL;
189 for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
192 last_chan->next = chan->next;
194 chanList[chanListIndex] = chan->next;
201 struct ChanUser *chanuser, *next;
202 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
203 next = getChannelUsers(chan, chanuser);
204 removeChanUserFromLists(chanuser, 0, 1, 1);
213 void freeChanNode(struct ChanNode* chan) {
215 struct trigger_cache *trigger, *next_trigger;
216 for(trigger = chan->trigger; trigger; trigger = next_trigger) {
217 next_trigger = trigger->next;
218 free(trigger->trigger);
222 freeModeNode(chan->modes);
224 removeChannelBans(chan);
225 if(chan->spam_settings)
226 freeNeonSpamSettings(chan->spam_settings);
230 void checkChannelVisibility(struct ChanNode* chan) {
231 struct ChanUser *chanuser, *next;
232 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
233 if(chanuser->user->flags & USERFLAG_ISBOT) {
234 chan->chanbot = chanuser->user;
238 //free the channel...
239 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
240 next = getChannelUsers(chan, chanuser);
241 //remove the channel from the user's channel-list
242 removeChanUserFromLists(chanuser, 0, 1, 0);
243 if(!chanuser->user->channel) {
244 //free the user (no more channels)
245 delUser(chanuser->user, 1);
247 freeChanUser(chanuser);