]> jfr.im git - irc/evilnet/x3.git/blame - src/hash.c
python is still crashing, but iv done some debug work and documentation that i dont...
[irc/evilnet/x3.git] / src / hash.c
CommitLineData
d76ed9a9 1/* hash.c - IRC network state database
2 * Copyright 2000-2004 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
d76ed9a9 5 *
d0f04f71 6 * x3 is free software; you can redistribute it and/or modify
d76ed9a9 7 * it under the terms of the GNU General Public License as published by
348683aa 8 * the Free Software Foundation; either version 3 of the License, or
d76ed9a9 9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21#include "conf.h"
22#include "global.h"
23#include "hash.h"
24#include "log.h"
25
58528547 26#if defined(HAVE_LIBGEOIP)&&defined(HAVE_GEOIP_H)&&defined(HAVE_GEOIPCITY_H)
21f6caee 27#include <GeoIP.h>
21f6caee 28#include <GeoIPCity.h>
58528547 29
30GeoIP * gi = NULL;
31GeoIP * cgi = NULL;
21f6caee 32#endif
33
d76ed9a9 34struct server *self;
35dict_t channels;
36dict_t clients;
37dict_t servers;
38unsigned int max_clients, invis_clients;
39time_t max_clients_time;
40struct userList curr_opers;
41
30874d66 42static void hash_cleanup(void *extra);
d76ed9a9 43
44void init_structs(void)
45{
46 channels = dict_new();
47 clients = dict_new();
48 servers = dict_new();
49 userList_init(&curr_opers);
30874d66 50 reg_exit_func(hash_cleanup, NULL);
d76ed9a9 51}
52
1136f709 53int userList_contains(struct userList *list, struct userNode *user)
54{
55 unsigned int ii;
56
57 for (ii = 0; ii < list->used; ++ii) {
58 if (user == list->list[ii]) {
59 return 1;
60 }
61 }
62 return 0;
63}
64
d76ed9a9 65server_link_func_t *slf_list;
9d59f196 66void **slf_list_extra;
d76ed9a9 67unsigned int slf_size = 0, slf_used = 0;
68
69void
9d59f196 70reg_server_link_func(server_link_func_t handler, void *extra)
d76ed9a9 71{
72 if (slf_used == slf_size) {
9d59f196 73 if (slf_size) {
74 slf_size <<= 1;
75 slf_list = realloc(slf_list, slf_size*sizeof(server_link_func_t));
76 slf_list_extra = realloc(slf_list_extra, slf_size*sizeof(void*));
77 } else {
78 slf_size = 8;
79 slf_list = malloc(slf_size*sizeof(server_link_func_t));
80 slf_list_extra = malloc(slf_size*sizeof(void*));
81 }
82 }
83 slf_list[slf_used] = handler;
84 slf_list_extra[slf_used++] = extra;
85}
86
87void
88call_server_link_funcs(struct server *server)
89{
90 unsigned int i;
91
92 for (i = 0; i < slf_used; ++i)
93 {
94 slf_list[i](server, slf_list_extra[i]);
d76ed9a9 95 }
d76ed9a9 96}
97
98struct server*
99GetServerH(const char *name)
100{
101 return dict_find(servers, name, NULL);
102}
103
c8b4b87b
MB
104sasl_input_func_t *sif_list;
105void **sif_list_extra;
106unsigned int sif_size = 0, sif_used = 0;
107
108void
109reg_sasl_input_func(sasl_input_func_t handler, void *extra)
110{
111 if (sif_used == sif_size) {
112 if (sif_size) {
113 sif_size <<= 1;
114 sif_list = realloc(sif_list, sif_size*sizeof(new_user_func_t));
115 sif_list_extra = realloc(sif_list_extra, sif_size*sizeof(void*));
116 } else {
117 sif_size = 8;
118 sif_list = malloc(sif_size*sizeof(new_user_func_t));
119 sif_list_extra = malloc(sif_size*sizeof(void*));
120 }
121 }
122 sif_list[sif_used] = handler;
123 sif_list_extra[sif_used++] = extra;
124}
125
126void
127call_sasl_input_func(struct server* source ,const char *identifier, const char *subcmd, const char *data, const char *ext)
128{
129 unsigned int i;
130
131 for (i = 0; i < sif_used; ++i)
132 {
133 sif_list[i](source, identifier, subcmd, data, ext, sif_list_extra[i]);
134 }
135}
136
137void
138unreg_sasl_input_func(sasl_input_func_t handler, void *extra)
139{
140 unsigned int i;
141 for (i=0; i<sif_used; i++) {
142 if (sif_list[i] == handler && sif_list_extra[i] == extra) break;
143 }
144 if (i == sif_used) return;
145 memmove(sif_list+i, sif_list+i+1, (sif_used-i-1)*sizeof(sif_list[0]));
146 memmove(sif_list_extra+i, sif_list_extra+i+1, (sif_used-i-1)*sizeof(sif_list_extra[0]));
147 sif_used--;
148}
149
d76ed9a9 150new_user_func_t *nuf_list;
f0fb2e2d 151void **nuf_list_extra;
d76ed9a9 152unsigned int nuf_size = 0, nuf_used = 0;
153
154void
f0fb2e2d 155reg_new_user_func(new_user_func_t handler, void *extra)
d76ed9a9 156{
157 if (nuf_used == nuf_size) {
f0fb2e2d 158 if (nuf_size) {
159 nuf_size <<= 1;
160 nuf_list = realloc(nuf_list, nuf_size*sizeof(new_user_func_t));
161 nuf_list_extra = realloc(nuf_list_extra, nuf_size*sizeof(void*));
162 } else {
163 nuf_size = 8;
164 nuf_list = malloc(nuf_size*sizeof(new_user_func_t));
165 nuf_list_extra = malloc(nuf_size*sizeof(void*));
166 }
167 }
168 nuf_list[nuf_used] = handler;
169 nuf_list_extra[nuf_used++] = extra;
170}
171
172void
173call_new_user_funcs(struct userNode* user)
174{
175 unsigned int i;
176
63189c10 177 for (i = 0; i < nuf_used && !(user->dead); ++i)
f0fb2e2d 178 {
179 nuf_list[i](user, nuf_list_extra[i]);
d76ed9a9 180 }
d76ed9a9 181}
182
183static nick_change_func_t *ncf2_list;
63189c10 184static void **ncf2_list_extra;
d76ed9a9 185static unsigned int ncf2_size = 0, ncf2_used = 0;
186
187void
63189c10 188reg_nick_change_func(nick_change_func_t handler, void *extra)
d76ed9a9 189{
190 if (ncf2_used == ncf2_size) {
191 if (ncf2_size) {
192 ncf2_size <<= 1;
193 ncf2_list = realloc(ncf2_list, ncf2_size*sizeof(nick_change_func_t));
63189c10 194 ncf2_list_extra = realloc(ncf2_list_extra, ncf2_size*sizeof(void*));
d76ed9a9 195 } else {
196 ncf2_size = 8;
197 ncf2_list = malloc(ncf2_size*sizeof(nick_change_func_t));
63189c10 198 ncf2_list_extra = malloc(ncf2_size*sizeof(void*));
d76ed9a9 199 }
200 }
63189c10 201 ncf2_list[ncf2_used] = handler;
63c0b8ad 202 ncf2_list_extra[ncf2_used++] = extra;
d76ed9a9 203}
204
205
206del_user_func_t *duf_list;
a6bcc929 207void **duf_list_extra;
d76ed9a9 208unsigned int duf_size = 0, duf_used = 0;
209
210void
a6bcc929 211reg_del_user_func(del_user_func_t handler, void *extra)
d76ed9a9 212{
213 if (duf_used == duf_size) {
a6bcc929 214 if (duf_size) {
215 duf_size <<= 1;
216 duf_list = realloc(duf_list, duf_size*sizeof(del_user_func_t));
217 duf_list_extra = realloc(duf_list_extra, duf_size*sizeof(void*));
218 } else {
219 duf_size = 8;
220 duf_list = malloc(duf_size*sizeof(del_user_func_t));
221 duf_list_extra = malloc(duf_size*sizeof(void*));
222 }
223 }
224 duf_list[duf_used] = handler;
225 duf_list_extra[duf_used++] = extra;
226}
227
228void
229call_del_user_funcs(struct userNode *user, struct userNode *killer, const char *why)
230{
231 unsigned int i;
232
233 for (i = 0; i < duf_used; ++i)
234 {
235 duf_list[i](user, killer, why, duf_list_extra[i]);
d76ed9a9 236 }
d76ed9a9 237}
238
239void
a6bcc929 240unreg_del_user_func(del_user_func_t handler, void *extra)
d76ed9a9 241{
242 unsigned int i;
243 for (i=0; i<duf_used; i++) {
a6bcc929 244 if (duf_list[i] == handler && duf_list_extra[i] == extra) break;
d76ed9a9 245 }
246 if (i == duf_used) return;
247 memmove(duf_list+i, duf_list+i+1, (duf_used-i-1)*sizeof(duf_list[0]));
a6bcc929 248 memmove(duf_list_extra+i, duf_list_extra+i+1, (duf_used-i-1)*sizeof(duf_list_extra[0]));
d76ed9a9 249 duf_used--;
250}
251
252/* reintroduces a user after it has been killed. */
253void
254ReintroduceUser(struct userNode *user)
255{
256 struct mod_chanmode change;
257 unsigned int n;
258
259 irc_user(user);
260 mod_chanmode_init(&change);
261 change.argc = 1;
262 for (n = 0; n < user->channels.used; n++) {
263 struct modeNode *mn = user->channels.list[n];
264 irc_join(user, mn->channel);
265 if (mn->modes) {
266 change.args[0].mode = mn->modes;
a32da4c7 267 change.args[0].u.member = mn;
d76ed9a9 268 mod_chanmode_announce(user, mn->channel, &change);
269 }
270 }
271}
272
273void
274NickChange(struct userNode* user, const char *new_nick, int no_announce)
275{
276 char *old_nick;
277 unsigned int nn;
278
279 /* don't do anything if there's no change */
280 old_nick = user->nick;
281 if (!strncmp(new_nick, old_nick, NICKLEN))
282 return;
283
284 /* remove old entry from clients dictionary */
285 dict_remove(clients, old_nick);
286#if !defined(WITH_PROTOCOL_P10)
287 /* Remove from uplink's clients dict */
288 dict_remove(user->uplink->users, old_nick);
289#endif
290 /* and reinsert */
291 user->nick = strdup(new_nick);
292 dict_insert(clients, user->nick, user);
293#if !defined(WITH_PROTOCOL_P10)
294 dict_insert(user->uplink->users, user->nick, user);
295#endif
296
297 /* Make callbacks for nick changes. Do this with new nick in
298 * place because that is slightly more useful.
299 */
1136f709 300 for (nn=0; (nn<ncf2_used) && !user->dead; nn++)
63189c10 301 ncf2_list[nn](user, old_nick, ncf2_list_extra[nn]);
d76ed9a9 302 user->timestamp = now;
303 if (IsLocal(user) && !no_announce)
304 irc_nick(user, old_nick);
305 free(old_nick);
306}
307
b404335b 308void
cf8bedff 309SVSNickChange(struct userNode* user, const char *new_nick)
b404335b 310{
311 char *old_nick;
312 unsigned int nn;
313
314 /* don't do anything if there's no change */
315 old_nick = user->nick;
316 if (!strncmp(new_nick, old_nick, NICKLEN))
317 return;
318
319 /* remove old entry from clients dictionary */
320 dict_remove(clients, old_nick);
321#if !defined(WITH_PROTOCOL_P10)
322 /* Remove from uplink's clients dict */
323 dict_remove(user->uplink->users, old_nick);
324#endif
325 /* and reinsert */
326 user->nick = strdup(new_nick);
327 dict_insert(clients, user->nick, user);
328#if !defined(WITH_PROTOCOL_P10)
329 dict_insert(user->uplink->users, user->nick, user);
330#endif
331
332 /* Make callbacks for nick changes. Do this with new nick in
333 * place because that is slightly more useful.
334 */
1136f709 335 for (nn=0; (nn<ncf2_used) && !user->dead; nn++)
63189c10 336 ncf2_list[nn](user, old_nick, ncf2_list_extra[nn]);
b404335b 337 user->timestamp = now;
338
339 free(old_nick);
340}
341
d76ed9a9 342struct userNode *
343GetUserH(const char *nick)
344{
345 return dict_find(clients, nick, NULL);
346}
347
348static account_func_t account_func;
349
350void
351reg_account_func(account_func_t handler)
352{
353 if (account_func) {
354 log_module(MAIN_LOG, LOG_WARNING, "Reregistering ACCOUNT handler.");
355 }
356 account_func = handler;
357}
358
359void
360call_account_func(struct userNode *user, const char *stamp)
361{
362 /* We've received an account stamp for a user; notify
363 NickServ, which registers the sole account_func
f1d4a7db 364 right now. TODO: This is a bug. This needs to register
365 a proper list not just kill with each call!! -Rubin
d76ed9a9 366
367 P10 Protocol violation if (user->modes & FLAGS_STAMPED) here.
368 */
369 if (account_func)
370 account_func(user, stamp);
371
372#ifdef WITH_PROTOCOL_P10
373 /* Mark the user so we don't stamp it again. */
374 user->modes |= FLAGS_STAMPED;
375#endif
376}
377
378void
b21e2cfe 379StampUser(struct userNode *user, const char *stamp, time_t timestamp)
d76ed9a9 380{
381#ifdef WITH_PROTOCOL_P10
382 /* The P10 protocol says we can't stamp users who already
383 have a stamp. */
384 if (IsStamped(user))
385 return;
386#endif
387
b21e2cfe 388 irc_account(user, stamp, timestamp);
d76ed9a9 389 user->modes |= FLAGS_STAMPED;
390}
391
392void
393assign_fakehost(struct userNode *user, const char *host, int announce)
394{
395 safestrncpy(user->fakehost, host, sizeof(user->fakehost));
396 if (announce)
397 irc_fakehost(user, host);
398}
399
21f6caee 400void
401set_geoip_info(struct userNode *user)
402{
7a278540 403 if(IsLocal(user))
404 return;
21f6caee 405/* Need the libs and the headers if this is going to compile properly */
406#if defined(HAVE_LIBGEOIP)&&defined(HAVE_GEOIP_H)&&defined(HAVE_GEOIPCITY_H)
21f6caee 407 GeoIPRecord * gir;
408 const char *geoip_data_file = NULL;
409 const char *geoip_city_file = NULL;
410
411 geoip_data_file = conf_get_data("services/opserv/geoip_data_file", RECDB_QSTRING);
412 geoip_city_file = conf_get_data("services/opserv/geoip_city_data_file", RECDB_QSTRING);
413
7a278540 414 if ((!geoip_data_file && !geoip_city_file))
21f6caee 415 return; /* Admin doesnt want to use geoip functions */
416
58528547 417 if (geoip_data_file && !gi)
418 gi = GeoIP_open(geoip_data_file, GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE);
21f6caee 419
58528547 420 if (geoip_city_file && !cgi)
421 cgi = GeoIP_open(geoip_city_file, GEOIP_MEMORY_CACHE | GEOIP_CHECK_CACHE);
21f6caee 422
423 if (cgi) {
424 gir = GeoIP_record_by_name(cgi, user->hostname);
425 if (gir) {
426 user->country_name = strdup(gir->country_name ? gir->country_name : "");
427 user->country_code = strdup(gir->country_code ? gir->country_code : "");
428 user->city = strdup(gir->city ? gir->city : "");
429 user->region = strdup(gir->region ? gir->region : "");
430 user->postal_code = strdup(gir->postal_code ? gir->postal_code : "");
431
432 user->latitude = gir->latitude ? gir->latitude : 0;
433 user->longitude = gir->longitude ? gir->longitude : 0;
434 user->dma_code = gir->dma_code ? gir->dma_code : 0;
435 user->area_code = gir->area_code ? gir->area_code : 0;
436
437 GeoIPRecord_delete(gir);
438 }
439
21f6caee 440 return;
441 } else if (gi) {
442 const char *country = GeoIP_country_name_by_name(gi, user->hostname);
443 user->country_name = strdup(country ? country : "");
21f6caee 444 return;
445 }
446
447 return;
448#endif
449}
450
d76ed9a9 451static new_channel_func_t *ncf_list;
157ce145 452static void **ncf_list_extra;
d76ed9a9 453static unsigned int ncf_size = 0, ncf_used = 0;
454
455void
157ce145 456reg_new_channel_func(new_channel_func_t handler, void *extra)
d76ed9a9 457{
458 if (ncf_used == ncf_size) {
459 if (ncf_size) {
460 ncf_size <<= 1;
461 ncf_list = realloc(ncf_list, ncf_size*sizeof(ncf_list[0]));
157ce145 462 ncf_list_extra = realloc(ncf_list_extra, ncf_size*sizeof(void*));
d76ed9a9 463 } else {
464 ncf_size = 8;
465 ncf_list = malloc(ncf_size*sizeof(ncf_list[0]));
157ce145 466 ncf_list_extra = malloc(ncf_size*sizeof(void*));
d76ed9a9 467 }
468 }
157ce145 469 ncf_list[ncf_used] = handler;
470 ncf_list_extra[ncf_used++] = extra;
d76ed9a9 471}
472
473static join_func_t *jf_list;
fb38e2bc 474static void **jf_list_extra;
d76ed9a9 475static unsigned int jf_size = 0, jf_used = 0;
476
477void
fb38e2bc 478reg_join_func(join_func_t handler, void *extra)
d76ed9a9 479{
480 if (jf_used == jf_size) {
481 if (jf_size) {
482 jf_size <<= 1;
483 jf_list = realloc(jf_list, jf_size*sizeof(join_func_t));
fb38e2bc 484 jf_list_extra = realloc(jf_list_extra, jf_size*sizeof(void*));
d76ed9a9 485 } else {
486 jf_size = 8;
487 jf_list = malloc(jf_size*sizeof(join_func_t));
fb38e2bc 488 jf_list_extra = malloc(jf_size*sizeof(void*));
d76ed9a9 489 }
490 }
fb38e2bc 491 jf_list[jf_used] = handler;
492 jf_list_extra[jf_used++] = extra;
d76ed9a9 493}
494
495int rel_age;
496
497static void
498wipeout_channel(struct chanNode *cNode, time_t new_time, char **modes, unsigned int modec) {
499 unsigned int orig_limit;
500 chan_mode_t orig_modes;
501 char orig_key[KEYLEN+1];
2f61d1d7 502 char orig_apass[KEYLEN+1];
503 char orig_upass[KEYLEN+1];
d76ed9a9 504 unsigned int nn, argc;
505
506 /* nuke old topic */
507 cNode->topic[0] = '\0';
508 cNode->topic_nick[0] = '\0';
509 cNode->topic_time = 0;
510
511 /* remember the old modes, and update them with the new */
512 orig_modes = cNode->modes;
513 orig_limit = cNode->limit;
514 strcpy(orig_key, cNode->key);
2f61d1d7 515 strcpy(orig_upass, cNode->upass);
516 strcpy(orig_apass, cNode->apass);
d76ed9a9 517 cNode->modes = 0;
518 mod_chanmode(NULL, cNode, modes, modec, 0);
519 cNode->timestamp = new_time;
520
521 /* remove our old ban list, replace it with the new one */
522 for (nn=0; nn<cNode->banlist.used; nn++)
523 free(cNode->banlist.list[nn]);
524 cNode->banlist.used = 0;
525
2aef5f4b 526 /* remove our old exe,[t list, replace it with the new one */
527 for (nn=0; nn<cNode->exemptlist.used; nn++)
528 free(cNode->exemptlist.list[nn]);
529 cNode->exemptlist.used = 0;
530
d76ed9a9 531 /* deop anybody in the channel now, but count services to reop */
532 for (nn=argc=0; nn<cNode->members.used; nn++) {
533 struct modeNode *mn = cNode->members.list[nn];
534 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user))
535 argc++;
536 }
55342ce8 537
d76ed9a9 538 if (argc) {
539 struct mod_chanmode *change;
540
541 change = mod_chanmode_alloc(argc);
542 change->modes_clear = 0;
543 change->modes_set = orig_modes;
544 change->new_limit = orig_limit;
545 strcpy(change->new_key, orig_key);
2f61d1d7 546 strcpy(change->new_upass, orig_upass);
547 strcpy(change->new_apass, orig_apass);
d76ed9a9 548 for (nn = argc = 0; nn < cNode->members.used; ++nn) {
549 struct modeNode *mn = cNode->members.list[nn];
550 if ((mn->modes & MODE_CHANOP) && IsService(mn->user) && IsLocal(mn->user)) {
551 change->args[argc].mode = MODE_CHANOP;
a32da4c7 552 change->args[argc].u.member = mn;
d76ed9a9 553 argc++;
554 }
555 }
556 assert(argc == change->argc);
a32da4c7 557 change->args[0].u.member->modes &= ~MODE_CHANOP;
558 mod_chanmode_announce(change->args[0].u.member->user, cNode, change);
d76ed9a9 559 mod_chanmode_free(change);
560 }
561}
562
563struct chanNode *
2aef5f4b 564AddChannel(const char *name, time_t time_, const char *modes, char *banlist, char *exemptlist)
d76ed9a9 565{
566 struct chanNode *cNode;
567 char new_modes[MAXLEN], *argv[MAXNUMPARAMS];
568 unsigned int nn;
569
570 if (!IsChannelName(name)) {
571 log_module(MAIN_LOG, LOG_ERROR, "Somebody asked to add channel '%s', which isn't a channel name!", name);
572 return NULL;
573 }
574 if (!modes)
575 modes = "";
576
577 safestrncpy(new_modes, modes, sizeof(new_modes));
578 nn = split_line(new_modes, 0, ArrayLength(argv), argv);
579 if (!(cNode = GetChannel(name))) {
580 cNode = calloc(1, sizeof(*cNode) + strlen(name));
581 strcpy(cNode->name, name);
582 banList_init(&cNode->banlist);
2aef5f4b 583 exemptList_init(&cNode->exemptlist);
d76ed9a9 584 modeList_init(&cNode->members);
2f61d1d7 585 mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER);
d76ed9a9 586 dict_insert(channels, cNode->name, cNode);
587 cNode->timestamp = time_;
588 rel_age = 1;
589 } else if (cNode->timestamp > time_) {
590 wipeout_channel(cNode, time_, argv, nn);
591 rel_age = 1;
592 } else if (cNode->timestamp == time_) {
2f61d1d7 593 mod_chanmode(NULL, cNode, argv, nn, MCP_FROM_SERVER);
d76ed9a9 594 rel_age = 0;
595 } else {
596 rel_age = -1;
597 }
598
599 /* rel_age is the relative ages of our channel data versus what is
600 * in a BURST command. 1 means ours is younger, 0 means both are
601 * the same age, -1 means ours is older. */
602
603 /* if it's a new or updated channel, make callbacks */
604 if (rel_age > 0)
605 for (nn=0; nn<ncf_used; nn++)
157ce145 606 ncf_list[nn](cNode, ncf_list_extra[nn]);
d76ed9a9 607
608 /* go through list of bans and add each one */
609 if (banlist && (rel_age >= 0)) {
610 for (nn=0; banlist[nn];) {
611 char *ban = banlist + nn;
612 struct banNode *bn;
613 while (banlist[nn] != ' ' && banlist[nn])
614 nn++;
615 while (banlist[nn] == ' ')
616 banlist[nn++] = 0;
617 bn = calloc(1, sizeof(*bn));
618 safestrncpy(bn->ban, ban, sizeof(bn->ban));
619 safestrncpy(bn->who, "<unknown>", sizeof(bn->who));
620 bn->set = now;
621 banList_append(&cNode->banlist, bn);
622 }
623 }
624
2aef5f4b 625 /* go through list of exempts and add each one */
626 if (exemptlist && (rel_age >= 0)) {
627 for (nn=0; exemptlist[nn];) {
628 char *exempt = exemptlist + nn;
629 struct exemptNode *en;
630 while (exemptlist[nn] != ' ' && exemptlist[nn])
631 nn++;
632 while (exemptlist[nn] == ' ')
633 exemptlist[nn++] = 0;
634 en = calloc(1, sizeof(*en));
635 safestrncpy(en->exempt, exempt, sizeof(en->exempt));
636 safestrncpy(en->who, "<unknown>", sizeof(en->who));
637 en->set = now;
638 exemptList_append(&cNode->exemptlist, en);
639 }
640 }
641
d76ed9a9 642 return cNode;
643}
644
645static del_channel_func_t *dcf_list;
1898d8ea 646static void **dcf_list_extra;
d76ed9a9 647static unsigned int dcf_size = 0, dcf_used = 0;
648
649void
1898d8ea 650reg_del_channel_func(del_channel_func_t handler, void *extra)
d76ed9a9 651{
652 if (dcf_used == dcf_size) {
653 if (dcf_size) {
654 dcf_size <<= 1;
655 dcf_list = realloc(dcf_list, dcf_size*sizeof(dcf_list[0]));
1898d8ea 656 dcf_list_extra = realloc(dcf_list_extra, dcf_size*sizeof(void*));
d76ed9a9 657 } else {
658 dcf_size = 8;
659 dcf_list = malloc(dcf_size*sizeof(dcf_list[0]));
1898d8ea 660 dcf_list_extra = malloc(dcf_size*sizeof(dcf_list_extra[0]));
d76ed9a9 661 }
662 }
1898d8ea 663 dcf_list[dcf_used] = handler;
664 dcf_list_extra[dcf_used++] = extra;
d76ed9a9 665}
666
667static void
668DelChannel(struct chanNode *channel)
669{
670 unsigned int n;
671
1136f709 672 verify(channel);
d76ed9a9 673 dict_remove(channels, channel->name);
674
675 if (channel->members.used || channel->locks) {
676 log_module(MAIN_LOG, LOG_ERROR, "Warning: deleting channel %s with %d users and %d locks remaining.", channel->name, channel->members.used, channel->locks);
677 }
678
679 /* go through all channel members and delete them from the channel */
680 for (n=channel->members.used; n>0; )
1136f709 681 DelChannelUser(channel->members.list[--n]->user, channel, NULL, 1);
d76ed9a9 682
683 /* delete all channel bans */
684 for (n=channel->banlist.used; n>0; )
685 free(channel->banlist.list[--n]);
686 channel->banlist.used = 0;
687
2aef5f4b 688 /* delete all channel exempts */
689 for (n=channel->exemptlist.used; n>0; )
690 free(channel->exemptlist.list[--n]);
691 channel->exemptlist.used = 0;
692
d76ed9a9 693 for (n=0; n<dcf_used; n++)
1898d8ea 694 dcf_list[n](channel, dcf_list_extra[n]);
d76ed9a9 695
696 modeList_clean(&channel->members);
697 banList_clean(&channel->banlist);
2aef5f4b 698 exemptList_clean(&channel->exemptlist);
d76ed9a9 699 free(channel);
700}
701
702struct modeNode *
703AddChannelUser(struct userNode *user, struct chanNode* channel)
704{
705 struct modeNode *mNode;
706 unsigned int n;
707
708 mNode = GetUserMode(channel, user);
709 if (mNode)
710 return mNode;
711
712 mNode = malloc(sizeof(*mNode));
713
714 /* set up modeNode */
715 mNode->channel = channel;
716 mNode->user = user;
717 mNode->modes = 0;
2f61d1d7 718 mNode->oplevel = -1;
d76ed9a9 719 mNode->idle_since = now;
720
721 /* Add modeNode to channel and to user.
722 * We have to do this before calling join funcs in case the
723 * modeNode is manipulated (e.g. chanserv ops the user).
724 */
725 modeList_append(&channel->members, mNode);
726 modeList_append(&user->channels, mNode);
727
0d16e639 728 if (channel->members.used == 1
2f61d1d7 729 && !(channel->modes & MODE_REGISTERED)
51e05af8 730 && !(channel->modes & MODE_APASS)) {
d76ed9a9 731 mNode->modes |= MODE_CHANOP;
51e05af8 732 log_module(MAIN_LOG, LOG_DEBUG, "setting op");
733 }
d76ed9a9 734
1136f709 735 if (IsLocal(user)) {
736 irc_join(user, channel);
737 }
738
51e05af8 739 for (n=0; (n<jf_used) && !user->dead; n++) {
d76ed9a9 740 /* Callbacks return true if they kick or kill the user,
741 * and we can continue without removing mNode. */
fb38e2bc 742 if (jf_list[n](mNode, jf_list_extra[n]))
d76ed9a9 743 return NULL;
744 }
745
d76ed9a9 746 return mNode;
747}
748
749static part_func_t *pf_list;
63637aea 750static void **pf_list_extra;
d76ed9a9 751static unsigned int pf_size = 0, pf_used = 0;
752
753void
63637aea 754reg_part_func(part_func_t handler, void *extra)
d76ed9a9 755{
756 if (pf_used == pf_size) {
757 if (pf_size) {
758 pf_size <<= 1;
759 pf_list = realloc(pf_list, pf_size*sizeof(part_func_t));
63637aea 760 pf_list_extra = realloc(pf_list_extra, pf_size*sizeof(void*));
d76ed9a9 761 } else {
762 pf_size = 8;
763 pf_list = malloc(pf_size*sizeof(part_func_t));
63637aea 764 pf_list_extra = malloc(pf_size*sizeof(void*));
d76ed9a9 765 }
766 }
63637aea 767 pf_list[pf_used] = handler;
768 pf_list_extra[pf_used++] = extra;
d76ed9a9 769}
770
771void
63637aea 772unreg_part_func(part_func_t handler, void *extra)
d76ed9a9 773{
774 unsigned int i;
775 for (i=0; i<pf_used; i++)
63637aea 776 if (pf_list[i] == handler && pf_list_extra[i] == extra)
d76ed9a9 777 break;
778 if (i == pf_used)
779 return;
780 memmove(pf_list+i, pf_list+i+1, (pf_used-i-1)*sizeof(pf_list[0]));
63637aea 781 memmove(pf_list_extra+i, pf_list_extra+i+1, (pf_used-i-1)*sizeof(pf_list_extra[0]));
d76ed9a9 782 pf_used--;
783}
784
785void
786LockChannel(struct chanNode* channel)
787{
788 channel->locks++;
789}
790
791void
792UnlockChannel(struct chanNode *channel)
793{
794 assert(channel->locks > 0);
795 if (!--channel->locks && !channel->members.used)
796 DelChannel(channel);
797}
798
799void
800DelChannelUser(struct userNode* user, struct chanNode* channel, const char *reason, int deleting)
801{
802 struct modeNode* mNode;
803 unsigned int n;
804
1136f709 805 if (IsLocal(user) && reason)
d76ed9a9 806 irc_part(user, channel, reason);
807
808 mNode = GetUserMode(channel, user);
809
810 /* Sometimes we get a PART when the user has been KICKed.
811 * In this case, we get no usermode, and should not try to free it.
812 */
813 if (!mNode)
814 return;
815
816 /* remove modeNode from channel and user */
817 modeList_remove(&channel->members, mNode);
818 modeList_remove(&user->channels, mNode);
819
820 /* make callbacks */
821 for (n=0; n<pf_used; n++)
63637aea 822 pf_list[n](mNode, reason, pf_list_extra[n]);
d76ed9a9 823
824 /* free memory */
825 free(mNode);
826
2f61d1d7 827 /* A single check for APASS only should be enough here */
828 if (!deleting && !channel->members.used && !channel->locks
829 && !(channel->modes & MODE_REGISTERED) && !(channel->modes & MODE_APASS))
d76ed9a9 830 DelChannel(channel);
831}
832
b34a7505 833static kick_func_t *kf_list;
91667658 834static void **kf_list_extra;
b34a7505 835static unsigned int kf_size = 0, kf_used = 0;
836
d76ed9a9 837void
838KickChannelUser(struct userNode* target, struct chanNode* channel, struct userNode *kicker, const char *why)
839{
b34a7505 840 unsigned int n;
841
d76ed9a9 842 if (!target || !channel || IsService(target) || !GetUserMode(channel, target))
843 return;
b34a7505 844
845 /* This may break things, but lets see.. -Rubin */
846 for (n=0; n<kf_used; n++)
91667658 847 kf_list[n](kicker, target, channel, kf_list_extra[n]);
b34a7505 848
d76ed9a9 849 /* don't remove them from the channel, since the server will send a PART */
850 irc_kick(kicker, target, channel, why);
851
852 if (IsLocal(target))
853 {
854 /* NULL reason because we don't want a PART message to be
855 sent by DelChannelUser. */
856 DelChannelUser(target, channel, NULL, 0);
857 }
858}
859
d76ed9a9 860void
91667658 861reg_kick_func(kick_func_t handler, void *extra)
d76ed9a9 862{
863 if (kf_used == kf_size) {
864 if (kf_size) {
865 kf_size <<= 1;
866 kf_list = realloc(kf_list, kf_size*sizeof(kick_func_t));
91667658 867 kf_list_extra = realloc(kf_list_extra, kf_size*sizeof(void*));
d76ed9a9 868 } else {
869 kf_size = 8;
870 kf_list = malloc(kf_size*sizeof(kick_func_t));
91667658 871 kf_list_extra = malloc(kf_size*sizeof(void*));
d76ed9a9 872 }
873 }
91667658 874 kf_list[kf_used] = handler;
875 kf_list_extra[kf_used++] = extra;
d76ed9a9 876}
877
878void
879ChannelUserKicked(struct userNode* kicker, struct userNode* victim, struct chanNode* channel)
880{
881 unsigned int n;
882 struct modeNode *mn;
883
b2fd1d25 884 if (!victim || !channel || !GetUserMode(channel, victim))
d76ed9a9 885 return;
886
887 /* Update the kicker's idle time (kicker may be null if it was a server) */
888 if (kicker && (mn = GetUserMode(channel, kicker)))
889 mn->idle_since = now;
890
891 for (n=0; n<kf_used; n++)
91667658 892 kf_list[n](kicker, victim, channel, kf_list_extra[n]);
d76ed9a9 893
894 DelChannelUser(victim, channel, 0, 0);
895
896 if (IsLocal(victim))
897 irc_part(victim, channel, NULL);
898}
899
900int ChannelBanExists(struct chanNode *channel, const char *ban)
901{
902 unsigned int n;
903
904 for (n = 0; n < channel->banlist.used; n++)
905 if (match_ircglobs(channel->banlist.list[n]->ban, ban))
906 return 1;
907 return 0;
908}
909
2aef5f4b 910int ChannelExemptExists(struct chanNode *channel, const char *exempt)
911{
912 unsigned int n;
913
914 for (n = 0; n < channel->exemptlist.used; n++)
915 if (match_ircglobs(channel->exemptlist.list[n]->exempt, exempt))
916 return 1;
917 return 0;
918}
919
d76ed9a9 920static topic_func_t *tf_list;
5a2c7cf6 921static void **tf_list_extra;
d76ed9a9 922static unsigned int tf_size = 0, tf_used = 0;
923
924void
5a2c7cf6 925reg_topic_func(topic_func_t handler, void *extra)
d76ed9a9 926{
927 if (tf_used == tf_size) {
928 if (tf_size) {
929 tf_size <<= 1;
930 tf_list = realloc(tf_list, tf_size*sizeof(topic_func_t));
5a2c7cf6 931 tf_list_extra = realloc(tf_list_extra, tf_size*sizeof(void*));
d76ed9a9 932 } else {
933 tf_size = 8;
934 tf_list = malloc(tf_size*sizeof(topic_func_t));
5a2c7cf6 935 tf_list_extra = malloc(tf_size*sizeof(void*));
d76ed9a9 936 }
937 }
5a2c7cf6 938 tf_list[tf_used] = handler;
939 tf_list_extra[tf_used++] = extra;
d76ed9a9 940}
941
942void
7fda2b52 943SetChannelTopic(struct chanNode *channel, struct userNode *service, struct userNode *user, const char *topic, int announce)
d76ed9a9 944{
945 unsigned int n;
946 struct modeNode *mn;
947 char old_topic[TOPICLEN+1];
948
949 safestrncpy(old_topic, channel->topic, sizeof(old_topic));
950 safestrncpy(channel->topic, topic, sizeof(channel->topic));
951 channel->topic_time = now;
952
953 if (user) {
954 safestrncpy(channel->topic_nick, user->nick, sizeof(channel->topic_nick));
955
956 /* Update the setter's idle time */
957 if ((mn = GetUserMode(channel, user)))
958 mn->idle_since = now;
959 }
960
961 if (announce) {
962 /* We don't really care if a local user messes with the topic,
963 * so don't call the tf_list functions. */
7fda2b52 964 irc_topic(service, user, channel, topic);
d76ed9a9 965 } else {
966 for (n=0; n<tf_used; n++)
1136f709 967 /* A topic change handler can return non-zero to indicate
968 * that it has reverted the topic change, and that further
969 * hooks should not be called.
970 */
5a2c7cf6 971 if (tf_list[n](user, channel, old_topic, tf_list_extra[n]))
d76ed9a9 972 break;
973 }
974}
975
976struct chanNode *
977GetChannel(const char *name)
978{
979 return dict_find(channels, name, NULL);
980}
981
982struct modeNode *
983GetUserMode(struct chanNode *channel, struct userNode *user)
984{
985 unsigned int n;
986 struct modeNode *mn = NULL;
ec1a68c8 987
988 verify(channel);
989 verify(channel->members.list);
990 verify(user);
991 verify(user->channels.list);
d76ed9a9 992 if (channel->members.used < user->channels.used) {
993 for (n=0; n<channel->members.used; n++) {
ec1a68c8 994 verify(channel->members.list[n]);
d76ed9a9 995 if (user == channel->members.list[n]->user) {
996 mn = channel->members.list[n];
997 break;
998 }
999 }
1000 } else {
1001 for (n=0; n<user->channels.used; n++) {
ec1a68c8 1002 verify(user->channels.list[n]);
d76ed9a9 1003 if (channel == user->channels.list[n]->channel) {
1004 mn = user->channels.list[n];
1005 break;
1006 }
1007 }
1008 }
1009 return mn;
1010}
1011
ac3bdc8d 1012struct userNode *IsInChannel(struct chanNode *channel, struct userNode *user)
1013{
1014 unsigned int n;
ac3bdc8d 1015
1016 verify(channel);
1017 verify(channel->members.list);
1018 verify(user);
1019 verify(user->channels.list);
1020 if (channel->members.used < user->channels.used) {
1021 for (n=0; n<channel->members.used; n++) {
1022 verify(channel->members.list[n]);
1023 if (user == channel->members.list[n]->user) {
1024 return(user);
1025 }
1026 }
1027 } else {
1028 for (n=0; n<user->channels.used; n++) {
1029 verify(user->channels.list[n]);
1030 if (channel == user->channels.list[n]->channel) {
1031 return(user);
1032 }
1033 }
1034 }
1035 return NULL;
1036}
1037
d76ed9a9 1038DEFINE_LIST(userList, struct userNode*)
1039DEFINE_LIST(modeList, struct modeNode*)
1040DEFINE_LIST(banList, struct banNode*)
2aef5f4b 1041DEFINE_LIST(exemptList, struct exemptNode*)
d76ed9a9 1042DEFINE_LIST(channelList, struct chanNode*)
1043DEFINE_LIST(serverList, struct server*)
1044
1045static void
30874d66 1046hash_cleanup(UNUSED_ARG(void *extra))
d76ed9a9 1047{
1048 dict_iterator_t it, next;
1049
1050 DelServer(self, 0, NULL);
1051 for (it = dict_first(channels); it; it = next) {
1052 next = iter_next(it);
1053 DelChannel(iter_data(it));
1054 }
1055 dict_delete(channels);
1056 dict_delete(clients);
1057 dict_delete(servers);
1058 userList_clean(&curr_opers);
1059
1060 free(slf_list);
63189c10 1061 free(slf_list_extra);
d76ed9a9 1062 free(nuf_list);
63189c10 1063 free(nuf_list_extra);
d76ed9a9 1064 free(ncf2_list);
63189c10 1065 free(ncf2_list_extra);
d76ed9a9 1066 free(duf_list);
63189c10 1067 free(duf_list_extra);
d76ed9a9 1068 free(ncf_list);
157ce145 1069 free(ncf_list_extra);
d76ed9a9 1070 free(jf_list);
fb38e2bc 1071 free(jf_list_extra);
d76ed9a9 1072 free(dcf_list);
1898d8ea 1073 free(dcf_list_extra);
d76ed9a9 1074 free(pf_list);
63637aea 1075 free(pf_list_extra);
d76ed9a9 1076 free(kf_list);
91667658 1077 free(kf_list_extra);
d76ed9a9 1078 free(tf_list);
5a2c7cf6 1079 free(tf_list_extra);
d76ed9a9 1080}