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