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