1 /* ChanNode.c - NeonServ v5.0
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* getChanByName(const char *name) { //case insensitive
96 int chanListIndex = get_chanlist_entry(name[1]);
97 if(chanListIndex == -1 || chanList[chanListIndex] == NULL)
99 struct ChanNode *chan;
100 for(chan = chanList[chanListIndex]; chan; chan = chan->next) {
101 if(!stricmp(name, chan->name))
107 struct ChanNode* addChannel(const char *name) {
108 int chanListIndex = get_chanlist_entry(name[1]);
109 if(chanListIndex == -1 || !is_valid_chan(name))
111 struct ChanNode *chan = malloc(sizeof(*chan));
114 perror("malloc() failed");
117 strcpy(chan->name, name);
121 chan->chanbot = NULL;
125 chan->modes = createModeNode(chan);
126 chan->trigger = NULL;
128 chan->next = chanList[chanListIndex];
129 chanList[chanListIndex] = chan;
133 int getChannelCount() {
135 struct ChanNode *chan;
136 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
137 for(chan = chanList[i]; chan; chan = chan->next) {
144 int getChanUserCount() {
146 struct ChanNode *chan;
147 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
148 for(chan = chanList[i]; chan; chan = chan->next) {
149 count += chan->usercount;
155 int getChanBanCount() {
157 struct ChanNode *chan;
159 for(i = 0; i < CHANNEL_LIST_SIZE; i++) {
160 for(chan = chanList[i]; chan; chan = chan->next) {
161 for(ban = chan->bans; ban; ban = ban->next)
168 void delChannel(struct ChanNode* chan, int freeChan) {
169 int chanListIndex = get_chanlist_entry(chan->name[1]);
170 if(chanListIndex == -1) return;
171 struct ChanNode *cchan, *last_chan = NULL;
172 for(cchan = chanList[chanListIndex]; cchan; cchan = cchan->next) {
175 last_chan->next = chan->next;
177 chanList[chanListIndex] = chan->next;
184 struct ChanUser *chanuser, *next;
185 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
186 next = getChannelUsers(chan, chanuser);
187 removeChanUserFromLists(chanuser, 0, 1, 1);
196 void freeChanNode(struct ChanNode* chan) {
198 struct trigger_cache *trigger, *next_trigger;
199 for(trigger = chan->trigger; trigger; trigger = next_trigger) {
200 next_trigger = trigger->next;
201 free(trigger->trigger);
205 freeModeNode(chan->modes);
207 removeChannelBans(chan);
211 void checkChannelVisibility(struct ChanNode* chan) {
212 struct ChanUser *chanuser, *next;
213 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = getChannelUsers(chan, chanuser)) {
214 if(chanuser->user->flags & USERFLAG_ISBOT) {
215 chan->chanbot = chanuser->user;
219 //free the channel...
220 for(chanuser = getChannelUsers(chan, NULL); chanuser; chanuser = next) {
221 next = getChannelUsers(chan, chanuser);
222 //remove the channel from the user's channel-list
223 removeChanUserFromLists(chanuser, 0, 1, 0);
224 if(!chanuser->user->channel) {
225 //free the user (no more channels)
226 delUser(chanuser->user, 1);