1 /* ChanNode.c - NeonServ v5.1
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/>.
24 static struct ChanNode **chanList;
26 void init_ChanNode() {
32 1 46 *everything else*
33 ---------------------------
36 #define CHANNEL_LIST_SIZE 47
37 chanList = calloc(CHANNEL_LIST_SIZE, sizeof(*chanList));
40 void free_ChanNode() {
41 //kamikaze free all channels and chanusers
43 struct ChanNode *chan, *next;
44 struct ChanUser *chanuser, *next_chanuser;
45 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
46 for(chan = chanList[i]; chan; chan = next) {
48 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next_chanuser) {
49 next_chanuser = getChannelUsers(chan, chanuser);
58 int is_valid_chan(const char *name) {
62 for (ii=1; name[ii]; ++ii) {
63 if ((name[ii] > 0) && (name[ii] <= 32))
67 if (name[ii] == '\xa0')
73 static int get_chanlist_entry(int name) {
74 if((name > 0 && name <= 32) || name == ',' || name == '\xa0') return -1; //invalid name
75 if(tolower(name) >= 97 && tolower(name) <= 122) {
76 return (tolower(name) - 97);
78 if(tolower(name) >= 48 && tolower(name) <= 57) {
79 return (tolower(name) - 48 + 26);
82 if(name == '{') return 36;
83 if(name == '|') return 37;
84 if(name == '}') return 38;
85 if(name == '~') return 39;
86 if(name == '[') return 40;
87 if(name == '\\') return 41;
88 if(name == ']') return 42;
89 if(name == '^') return 43;
90 if(name == '_') return 44;
91 if(name == '`') return 45;
95 struct ChanNode* getAllChans(struct ChanNode *last) {
96 if(last == NULL || last->next == NULL) {
101 cindex = get_chanlist_entry(last->name[1]) + 1;
102 while(chanList[cindex] == NULL && cindex < CHANNEL_LIST_SIZE)
104 if(cindex >= CHANNEL_LIST_SIZE) return NULL;
105 return chanList[cindex];
110 struct ChanNode* getChanByName(const char *name) { //case insensitive
111 int chanListIndex = get_chanlist_entry(name[1]);
112 if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
114 struct ChanNode *chan;
115 for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
116 if(!stricmp(name, chan->name))
122 struct ChanNode* addChannel(const char *name) {
123 int chanListIndex = get_chanlist_entry(name[1]);
124 if(chanListIndex == -1 || !is_valid_chan(name))
126 struct ChanNode *chan = malloc(sizeof(*chan));
129 perror("malloc() failed");
132 strcpy(chan->name, name);
136 chan->chanbot = NULL;
140 chan->modes = createModeNode(chan);
141 chan->trigger = NULL;
143 chan->next = chanList[chanListIndex];
144 chanList[chanListIndex] = chan;
148 int getChannelCount() {
150 struct ChanNode *chan;
151 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
152 for(chan = chanList[i]; chan; chan = chan->next) {
159 int getChanUserCount() {
161 struct ChanNode *chan;
162 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
163 for(chan = chanList[i]; chan; chan = chan->next) {
164 count += chan->usercount;
170 int getChanBanCount() {
172 struct ChanNode *chan;
174 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
175 for(chan = chanList[i]; chan; chan = chan->next) {
176 for(ban = chan->bans; ban; ban = ban->next)
183 void delChannel(struct ChanNode* chan, int freeChan) {
184 int chanListIndex = get_chanlist_entry(chan->name[1]);
185 if(chanListIndex == -1) return;
186 struct ChanNode *cchan, *last_chan = NULL;
187 for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
190 last_chan->next = chan->next;
192 chanList[chanListIndex] = chan->next;
199 struct ChanUser *chanuser, *next;
200 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
201 next = getChannelUsers(chan, chanuser);
202 removeChanUserFromLists(chanuser, 0, 1, 1);
211 void freeChanNode(struct ChanNode* chan) {
213 struct trigger_cache *trigger, *next_trigger;
214 for(trigger = chan->trigger; trigger; trigger = next_trigger) {
215 next_trigger = trigger->next;
216 free(trigger->trigger);
220 freeModeNode(chan->modes);
222 removeChannelBans(chan);
226 void checkChannelVisibility(struct ChanNode* chan) {
227 struct ChanUser *chanuser, *next;
228 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
229 if(chanuser->user->flags & USERFLAG_ISBOT) {
230 chan->chanbot = chanuser->user;
234 //free the channel...
235 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
236 next = getChannelUsers(chan, chanuser);
237 //remove the channel from the user's channel-list
238 removeChanUserFromLists(chanuser, 0, 1, 0);
239 if(!chanuser->user->channel) {
240 //free the user (no more channels)
241 delUser(chanuser->user, 1);