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 void init_ChanNode() {
34 1 46 *everything else*
35 ---------------------------
38 #define CHANNEL_LIST_SIZE 47
39 chanList = calloc(CHANNEL_LIST_SIZE, sizeof(*chanList));
42 void free_ChanNode() {
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);
60 int is_valid_chan(const char *name) {
64 for (ii=1; name[ii]; ++ii) {
65 if ((name[ii] > 0) && (name[ii] <= 32))
69 if (name[ii] == '\xa0')
75 static int get_chanlist_entry(int name) {
76 if((name > 0 && name <= 32) || name == ',' || name == '\xa0') return -1; //invalid name
77 if(tolower(name) >= 97 && tolower(name) <= 122) {
78 return (tolower(name) - 97);
80 if(tolower(name) >= 48 && tolower(name) <= 57) {
81 return (tolower(name) - 48 + 26);
84 if(name == '{') return 36;
85 if(name == '|') return 37;
86 if(name == '}') return 38;
87 if(name == '~') return 39;
88 if(name == '[') return 40;
89 if(name == '\\') return 41;
90 if(name == ']') return 42;
91 if(name == '^') return 43;
92 if(name == '_') return 44;
93 if(name == '`') return 45;
97 struct ChanNode* getAllChans(struct ChanNode *last) {
98 if(last == NULL || last->next == NULL) {
103 cindex = get_chanlist_entry(last->name[1]) + 1;
104 while(chanList[cindex] == NULL && cindex < CHANNEL_LIST_SIZE)
106 if(cindex >= CHANNEL_LIST_SIZE) return NULL;
107 return chanList[cindex];
112 struct ChanNode* getChanByName(const char *name) { //case insensitive
113 int chanListIndex = get_chanlist_entry(name[1]);
114 if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
116 struct ChanNode *chan;
117 for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
118 if(!stricmp(name, chan->name))
124 struct ChanNode* addChannel(const char *name) {
125 int chanListIndex = get_chanlist_entry(name[1]);
126 if(chanListIndex == -1 || !is_valid_chan(name))
128 struct ChanNode *chan = malloc(sizeof(*chan));
131 perror("malloc() failed");
134 strcpy(chan->name, name);
137 chan->spam_settings = NULL;
139 chan->chanbot = NULL;
143 chan->modes = createModeNode(chan);
144 chan->trigger = NULL;
146 chan->next = chanList[chanListIndex];
147 chanList[chanListIndex] = chan;
151 int getChannelCount() {
153 struct ChanNode *chan;
154 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
155 for(chan = chanList[i]; chan; chan = chan->next) {
162 int getChanUserCount() {
164 struct ChanNode *chan;
165 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
166 for(chan = chanList[i]; chan; chan = chan->next) {
167 count += chan->usercount;
173 int getChanBanCount() {
175 struct ChanNode *chan;
177 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
178 for(chan = chanList[i]; chan; chan = chan->next) {
179 for(ban = chan->bans; ban; ban = ban->next)
186 void delChannel(struct ChanNode* chan, int freeChan) {
187 int chanListIndex = get_chanlist_entry(chan->name[1]);
188 if(chanListIndex == -1) return;
189 struct ChanNode *cchan, *last_chan = NULL;
190 for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
193 last_chan->next = chan->next;
195 chanList[chanListIndex] = chan->next;
202 struct ChanUser *chanuser, *next;
203 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
204 next = getChannelUsers(chan, chanuser);
205 removeChanUserFromLists(chanuser, 0, 1, 1);
214 void freeChanNode(struct ChanNode* chan) {
215 event_freechan(chan);
217 struct trigger_cache *trigger, *next_trigger;
218 for(trigger = chan->trigger; trigger; trigger = next_trigger) {
219 next_trigger = trigger->next;
220 free(trigger->trigger);
224 freeModeNode(chan->modes);
226 removeChannelBans(chan);
227 if(chan->spam_settings)
228 freeNeonSpamSettings(chan->spam_settings);
232 int checkChannelVisibility(struct ChanNode* chan) {
233 struct ChanUser *chanuser, *next;
234 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
235 if(chanuser->user->flags & USERFLAG_ISBOT) {
236 chan->chanbot = chanuser->user;
240 //free the channel...
241 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
242 next = getChannelUsers(chan, chanuser);
243 //remove the channel from the user's channel-list
244 removeChanUserFromLists(chanuser, 0, 1, 0);
245 if(!chanuser->user->channel) {
246 //free the user (no more channels)
247 delUser(chanuser->user, 1);
249 freeChanUser(chanuser);