/[cvs]/eggdrop1.9/src/userrec.c
ViewVC logotype

Contents of /eggdrop1.9/src/userrec.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.27 - (show annotations) (download) (as text)
Fri Oct 27 19:32:41 2000 UTC (19 years, 4 months ago) by fabian
Branch: MAIN
Changes since 1.26: +14 -15 lines
File MIME type: text/x-chdr
fabian: applied tcl_hash_overhaul patch. (minor change: added comments to end of check_tcl_bind().)

1 /*
2 * userrec.c -- handles:
3 * add_q() del_q() str2flags() flags2str() str2chflags() chflags2str()
4 * a bunch of functions to find and change user records
5 * change and check user (and channel-specific) flags
6 *
7 * $Id: userrec.c,v 1.26 2000/10/19 16:33:11 fabian Exp $
8 */
9 /*
10 * Copyright (C) 1997 Robey Pointer
11 * Copyright (C) 1999, 2000 Eggheads
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27
28 #include <sys/stat.h>
29 #include "main.h"
30 #include "users.h"
31 #include "chan.h"
32 #include "modules.h"
33 #include "tandem.h"
34
35 extern struct dcc_t *dcc;
36 extern struct chanset_t *chanset;
37 extern int default_flags, default_uflags, quiet_save,
38 dcc_total, share_greet;
39 extern char userfile[], ver[], botnetnick[];
40 extern time_t now;
41
42 int noshare = 1; /* don't send out to sharebots */
43 int sort_users = 0; /* sort the userlist when saving */
44 struct userrec *userlist = NULL; /* user records are stored here */
45 struct userrec *lastuser = NULL; /* last accessed user record */
46 maskrec *global_bans = NULL,
47 *global_exempts = NULL,
48 *global_invites = NULL;
49 struct igrec *global_ign = NULL;
50 int cache_hit = 0,
51 cache_miss = 0; /* temporary cache accounting */
52 int strict_host = 1;
53 int userfile_perm = 0600; /* Userfile permissions,
54 default rw------- */
55
56
57 void *_user_malloc(int size, const char *file, int line)
58 {
59 #ifdef DEBUG_MEM
60 char x[1024];
61 const char *p;
62
63 p = strrchr(file, '/');
64 simple_sprintf(x, "userrec.c:%s", p ? p + 1 : file);
65 return n_malloc(size, x, line);
66 #else
67 return nmalloc(size);
68 #endif
69 }
70
71 void *_user_realloc(void *ptr, int size, const char *file, int line)
72 {
73 #ifdef DEBUG_MEM
74 char x[1024];
75 const char *p;
76
77 p = strrchr(file, '/');
78 simple_sprintf(x, "userrec.c:%s", p ? p + 1 : file);
79 return n_realloc(ptr, size, x, line);
80 #else
81 return nrealloc(ptr, size);
82 #endif
83 }
84
85 inline int expmem_mask(struct maskrec *m)
86 {
87 int result = 0;
88
89 while (m) {
90 result += sizeof(struct maskrec);
91 result += strlen(m->mask) + 1;
92 if (m->user)
93 result += strlen(m->user) + 1;
94 if (m->desc)
95 result += strlen(m->desc) + 1;
96
97 m = m->next;
98 }
99
100 return result;
101 }
102
103 /* Memory we should be using
104 */
105 int expmem_users()
106 {
107 int tot;
108 struct userrec *u;
109 struct chanuserrec *ch;
110 struct chanset_t *chan;
111 struct user_entry *ue;
112 struct igrec *i;
113
114 Context;
115 tot = 0;
116 u = userlist;
117 while (u != NULL) {
118 ch = u->chanrec;
119 while (ch) {
120 tot += sizeof(struct chanuserrec);
121
122 if (ch->info != NULL)
123 tot += strlen(ch->info) + 1;
124 ch = ch->next;
125 }
126 tot += sizeof(struct userrec);
127
128 for (ue = u->entries; ue; ue = ue->next) {
129 tot += sizeof(struct user_entry);
130
131 if (ue->name) {
132 tot += strlen(ue->name) + 1;
133 tot += list_type_expmem(ue->u.list);
134 } else {
135 tot += ue->type->expmem(ue);
136 }
137 }
138 u = u->next;
139 }
140 /* Account for each channel's masks */
141 for (chan = chanset; chan; chan = chan->next) {
142
143 /* Account for each channel's ban-list user */
144 tot += expmem_mask(chan->bans);
145
146 /* Account for each channel's exempt-list user */
147 tot += expmem_mask(chan->exempts);
148
149 /* Account for each channel's invite-list user */
150 tot += expmem_mask(chan->invites);
151 }
152
153 tot += expmem_mask(global_bans);
154 tot += expmem_mask(global_exempts);
155 tot += expmem_mask(global_invites);
156
157 for (i = global_ign; i; i = i->next) {
158 tot += sizeof(struct igrec);
159
160 tot += strlen(i->igmask) + 1;
161 if (i->user)
162 tot += strlen(i->user) + 1;
163 if (i->msg)
164 tot += strlen(i->msg) + 1;
165 }
166 return tot;
167 }
168
169 int count_users(struct userrec *bu)
170 {
171 int tot = 0;
172 struct userrec *u = bu;
173
174 while (u != NULL) {
175 tot++;
176 u = u->next;
177 }
178 return tot;
179 }
180
181 /* Convert "nick!~user@host", "nick!+user@host" and "nick!-user@host"
182 * to "nick!user@host" if necessary. (drummer)
183 */
184 static char *fixfrom(char *s)
185 {
186 char *p;
187 static char buf[512];
188
189 if (s == NULL)
190 return NULL;
191 strncpy(buf, s, 511);
192 buf[511] = 0;
193 if (strict_host)
194 return buf;
195 if ((p = strchr(buf, '!')))
196 p++;
197 else
198 p = s; /* Sometimes we get passed just a
199 * user@host here... */
200 /* These are ludicrous. */
201 if (strchr("~+-^=", *p) && (p[1] != '@')) /* added check for @ - drummer */
202 strcpy(p, p + 1);
203 /* Bug was: n!~@host -> n!@host now: n!~@host */
204 return buf;
205 }
206
207 struct userrec *check_dcclist_hand(char *handle)
208 {
209 int i;
210
211 for (i = 0; i < dcc_total; i++)
212 if (!egg_strcasecmp(dcc[i].nick, handle))
213 return dcc[i].user;
214 return NULL;
215 }
216
217 struct userrec *get_user_by_handle(struct userrec *bu, char *handle)
218 {
219 struct userrec *u = bu, *ret;
220
221 if (!handle)
222 return NULL;
223 /* FIXME: This should be done outside of this function. */
224 rmspace(handle);
225 if (!handle[0] || (handle[0] == '*'))
226 return NULL;
227 if (bu == userlist) {
228 if (lastuser && !egg_strcasecmp(lastuser->handle, handle)) {
229 cache_hit++;
230 return lastuser;
231 }
232 ret = check_dcclist_hand(handle);
233 if (ret) {
234 cache_hit++;
235 return ret;
236 }
237 ret = check_chanlist_hand(handle);
238 if (ret) {
239 cache_hit++;
240 return ret;
241 }
242 cache_miss++;
243 }
244 while (u) {
245 if (!egg_strcasecmp(u->handle, handle)) {
246 if (bu == userlist)
247 lastuser = u;
248 return u;
249 }
250 u = u->next;
251 }
252 return NULL;
253 }
254
255 /* Fix capitalization, etc
256 */
257 void correct_handle(char *handle)
258 {
259 struct userrec *u;
260
261 u = get_user_by_handle(userlist, handle);
262 if (u == NULL)
263 return;
264 strcpy(handle, u->handle);
265 }
266
267 /* This will be usefull in a lot of places, much more code re-use so we
268 * endup with a smaller executable bot. <cybah>
269 */
270 void clear_masks(maskrec *m)
271 {
272 maskrec *temp = NULL;
273
274 while (m) {
275 temp = m->next;
276
277 if (m->mask)
278 nfree(m->mask);
279 if (m->user)
280 nfree(m->user);
281 if (m->desc)
282 nfree(m->desc);
283
284 nfree(m);
285 m = temp;
286 }
287 }
288
289 void clear_userlist(struct userrec *bu)
290 {
291 struct userrec *u = bu, *v;
292 int i;
293
294 Context;
295 while (u != NULL) {
296 v = u->next;
297 freeuser(u);
298 u = v;
299 }
300 if (userlist == bu) {
301 struct chanset_t *cst;
302
303 for (i = 0; i < dcc_total; i++)
304 dcc[i].user = NULL;
305 clear_chanlist();
306 lastuser = NULL;
307
308 while (global_ign)
309 delignore(global_ign->igmask);
310
311 clear_masks(global_bans);
312 clear_masks(global_exempts);
313 clear_masks(global_invites);
314 global_exempts = global_invites = global_bans = NULL;
315
316 for (cst = chanset; cst; cst = cst->next) {
317 clear_masks(cst->bans);
318 clear_masks(cst->exempts);
319 clear_masks(cst->invites);
320
321 cst->bans = cst->exempts = cst->invites = NULL;
322 }
323 }
324 /* Remember to set your userlist to NULL after calling this */
325 Context;
326 }
327
328 /* Find CLOSEST host match
329 * (if "*!*@*" and "*!*@*clemson.edu" both match, use the latter!)
330 *
331 * Checks the chanlist first, to possibly avoid needless search.
332 */
333 struct userrec *get_user_by_host(char *host)
334 {
335 struct userrec *u, *ret;
336 struct list_type *q;
337 int cnt, i;
338 char host2[UHOSTLEN];
339
340 if (host == NULL)
341 return NULL;
342 rmspace(host);
343 if (!host[0])
344 return NULL;
345 ret = check_chanlist(host);
346 cnt = 0;
347 if (ret != NULL) {
348 cache_hit++;
349 return ret;
350 }
351 cache_miss++;
352 strcpy(host2, host);
353 host = fixfrom(host);
354 for (u = userlist; u; u = u->next) {
355 q = get_user(&USERENTRY_HOSTS, u);
356 for (; q; q = q->next) {
357 i = wild_match(q->extra, host);
358 if (i > cnt) {
359 ret = u;
360 cnt = i;
361 }
362 }
363 }
364 if (ret != NULL) {
365 lastuser = ret;
366 set_chanlist(host2, ret);
367 }
368 return ret;
369 }
370
371 /* use fixfrom() or dont? (drummer)
372 */
373 struct userrec *get_user_by_equal_host(char *host)
374 {
375 struct userrec *u = userlist;
376 struct list_type *q;
377
378 while (u != NULL) {
379 q = get_user(&USERENTRY_HOSTS, u);
380 while (q != NULL) {
381 if (!rfc_casecmp(q->extra, host))
382 return u;
383 q = q->next;
384 }
385 u = u->next;
386 }
387 return NULL;
388 }
389
390 /* Try: pass_match_by_host("-",host)
391 * will return 1 if no password is set for that host
392 */
393 int u_pass_match(struct userrec *u, char *pass)
394 {
395 char *cmp, new[32];
396
397 if (!u)
398 return 0;
399 cmp = get_user(&USERENTRY_PASS, u);
400 if (!cmp && (!pass[0] || (pass[0] == '-')))
401 return 1;
402 if (!cmp || !pass || !pass[0] || (pass[0] == '-'))
403 return 0;
404 if (u->flags & USER_BOT) {
405 if (!strcmp(cmp, pass))
406 return 1;
407 } else {
408 if (strlen(pass) > 15)
409 pass[15] = 0;
410 encrypt_pass(pass, new);
411 if (!strcmp(cmp, new))
412 return 1;
413 }
414 return 0;
415 }
416
417 int write_user(struct userrec *u, FILE * f, int idx)
418 {
419 char s[181];
420 struct chanuserrec *ch;
421 struct chanset_t *cst;
422 struct user_entry *ue;
423 struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
424
425 fr.global = u->flags;
426 fr.udef_global = u->flags_udef;
427 build_flags(s, &fr, NULL);
428 if (fprintf(f, "%-10s - %-24s\n", u->handle, s) == EOF)
429 return 0;
430 for (ch = u->chanrec; ch; ch = ch->next) {
431 cst = findchan_by_dname(ch->channel);
432 if (cst && ((idx < 0) || channel_shared(cst))) {
433 if (idx >= 0) {
434 fr.match = (FR_CHAN | FR_BOT);
435 get_user_flagrec(dcc[idx].user, &fr, ch->channel);
436 } else
437 fr.chan = BOT_SHARE;
438 if ((fr.chan & BOT_SHARE) || (fr.bot & BOT_GLOBAL)) {
439 fr.match = FR_CHAN;
440 fr.chan = ch->flags;
441 fr.udef_chan = ch->flags_udef;
442 build_flags(s, &fr, NULL);
443 if (fprintf(f, "! %-20s %lu %-10s %s\n", ch->channel, ch->laston, s,
444 (((idx < 0) || share_greet) && ch->info) ? ch->info
445 : "") == EOF)
446 return 0;
447 }
448 }
449 }
450 for (ue = u->entries; ue; ue = ue->next) {
451 if (ue->name) {
452 struct list_type *lt;
453
454 for (lt = ue->u.list; lt; lt = lt->next)
455 if (fprintf(f, "--%s %s\n", ue->name, lt->extra) == EOF)
456 return 0;
457 } else {
458 if (!ue->type->write_userfile(f, u, ue))
459 return 0;
460 }
461 }
462 return 1;
463 }
464
465 int sort_compare(struct userrec *a, struct userrec *b)
466 {
467 /* Order by flags, then alphabetically
468 * first bots: +h / +a / +l / other bots
469 * then users: +n / +m / +o / other users
470 * return true if (a > b)
471 */
472 if (a->flags & b->flags & USER_BOT) {
473 if (~bot_flags(a) & bot_flags(b) & BOT_HUB)
474 return 1;
475 if (bot_flags(a) & ~bot_flags(b) & BOT_HUB)
476 return 0;
477 if (~bot_flags(a) & bot_flags(b) & BOT_ALT)
478 return 1;
479 if (bot_flags(a) & ~bot_flags(b) & BOT_ALT)
480 return 0;
481 if (~bot_flags(a) & bot_flags(b) & BOT_LEAF)
482 return 1;
483 if (bot_flags(a) & ~bot_flags(b) & BOT_LEAF)
484 return 0;
485 } else {
486 if (~a->flags & b->flags & USER_BOT)
487 return 1;
488 if (a->flags & ~b->flags & USER_BOT)
489 return 0;
490 if (~a->flags & b->flags & USER_OWNER)
491 return 1;
492 if (a->flags & ~b->flags & USER_OWNER)
493 return 0;
494 if (~a->flags & b->flags & USER_MASTER)
495 return 1;
496 if (a->flags & ~b->flags & USER_MASTER)
497 return 0;
498 if (~a->flags & b->flags & USER_OP)
499 return 1;
500 if (a->flags & ~b->flags & USER_OP)
501 return 0;
502 }
503 return (egg_strcasecmp(a->handle, b->handle) > 0);
504 }
505
506 void sort_userlist()
507 {
508 int again;
509 struct userrec *last, *p, *c, *n;
510
511 again = 1;
512 last = NULL;
513 while ((userlist != last) && (again)) {
514 p = NULL;
515 c = userlist;
516 n = c->next;
517 again = 0;
518 while (n != last) {
519 if (sort_compare(c, n)) {
520 again = 1;
521 c->next = n->next;
522 n->next = c;
523 if (p == NULL)
524 userlist = n;
525 else
526 p->next = n;
527 }
528 p = c;
529 c = n;
530 n = n->next;
531 }
532 last = c;
533 }
534 }
535
536 /* Rewrite the entire user file. Call USERFILE hook as well, probably
537 * causing the channel file to be rewritten as well.
538 */
539 void write_userfile(int idx)
540 {
541 FILE *f;
542 char *new_userfile;
543 char s1[81];
544 time_t tt;
545 struct userrec *u;
546 int ok;
547
548 Context;
549 if (userlist == NULL)
550 return; /* No point in saving userfile */
551
552 new_userfile = nmalloc(strlen(userfile) + 5);
553 sprintf(new_userfile, "%s~new", userfile);
554
555 f = fopen(new_userfile, "w");
556 chmod(new_userfile, userfile_perm);
557 if (f == NULL) {
558 putlog(LOG_MISC, "*", USERF_ERRWRITE);
559 nfree(new_userfile);
560 return;
561 }
562 if (!quiet_save)
563 putlog(LOG_MISC, "*", USERF_WRITING);
564 if (sort_users)
565 sort_userlist();
566 tt = now;
567 strcpy(s1, ctime(&tt));
568 fprintf(f, "#4v: %s -- %s -- written %s", ver, botnetnick, s1);
569 Context;
570 ok = 1;
571 u = userlist;
572 while (u != NULL && ok) {
573 ok = write_user(u, f, idx);
574 u = u->next;
575 }
576 Context;
577 if (!ok || fflush(f)) {
578 putlog(LOG_MISC, "*", "%s (%s)", USERF_ERRWRITE, strerror(ferror(f)));
579 fclose(f);
580 nfree(new_userfile);
581 return;
582 }
583 fclose(f);
584 Context;
585 call_hook(HOOK_USERFILE);
586 Context;
587 movefile(new_userfile, userfile);
588 nfree(new_userfile);
589 Context;
590 }
591
592 int change_handle(struct userrec *u, char *newh)
593 {
594 int i;
595 char s[HANDLEN + 1];
596
597 if (!u)
598 return 0;
599 /* Nothing that will confuse the userfile */
600 if (newh[1] == 0 && strchr(BADHANDCHARS, newh[0]))
601 return 0;
602 check_tcl_nkch(u->handle, newh);
603 /* Yes, even send bot nick changes now: */
604 if (!noshare && !(u->flags & USER_UNSHARED))
605 shareout(NULL, "h %s %s\n", u->handle, newh);
606 strncpy(s, u->handle, HANDLEN);
607 s[HANDLEN] = 0;
608 strncpy(u->handle, newh, HANDLEN);
609 u->handle[HANDLEN] = 0;
610 for (i = 0; i < dcc_total; i++)
611 if (!egg_strcasecmp(dcc[i].nick, s) && dcc[i].type != &DCC_BOT) {
612 strncpy(dcc[i].nick, newh, HANDLEN);
613 dcc[i].nick[HANDLEN] = 0;
614 if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
615 chanout_but(-1, dcc[i].u.chat->channel,
616 "*** Handle change: %s -> %s\n", s, newh);
617 if (dcc[i].u.chat->channel < 100000)
618 botnet_send_nkch(i, s);
619 }
620 }
621 return 1;
622 }
623
624 extern int noxtra;
625
626 struct userrec *adduser(struct userrec *bu, char *handle, char *host,
627 char *pass, int flags)
628 {
629 struct userrec *u, *x;
630 struct xtra_key *xk;
631 int oldshare = noshare;
632
633 noshare = 1;
634 u = (struct userrec *) nmalloc(sizeof(struct userrec));
635
636 /* u->next=bu; bu=u; */
637 strncpy(u->handle, handle, HANDLEN);
638 u->handle[HANDLEN] = 0;
639 u->next = NULL;
640 u->chanrec = NULL;
641 u->entries = NULL;
642 if (flags != USER_DEFAULT) { /* drummer */
643 u->flags = flags;
644 u->flags_udef = 0;
645 } else {
646 u->flags = default_flags;
647 u->flags_udef = default_uflags;
648 }
649 set_user(&USERENTRY_PASS, u, pass);
650 if (!noxtra) {
651 xk = nmalloc(sizeof(struct xtra_key));
652 xk->key = nmalloc(8);
653 strcpy(xk->key, "created");
654 xk->data = nmalloc(10);
655 sprintf(xk->data, "%09lu", now);
656 set_user(&USERENTRY_XTRA, u, xk);
657 }
658 /* Strip out commas -- they're illegal */
659 if (host && host[0]) {
660 char *p;
661
662 /* About this fixfrom():
663 * We should use this fixfrom before every call of adduser()
664 * but its much easier to use here... (drummer)
665 * Only use it if we have a host :) (dw)
666 */
667 host = fixfrom(host);
668 p = strchr(host, ',');
669
670 while (p != NULL) {
671 *p = '?';
672 p = strchr(host, ',');
673 }
674 set_user(&USERENTRY_HOSTS, u, host);
675 } else
676 set_user(&USERENTRY_HOSTS, u, "none");
677 if (bu == userlist)
678 clear_chanlist();
679 noshare = oldshare;
680 if ((!noshare) && (handle[0] != '*') && (!(flags & USER_UNSHARED)) &&
681 (bu == userlist)) {
682 struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
683 char x[100];
684
685 fr.global = u->flags;
686 fr.udef_global = u->flags_udef;
687 build_flags(x, &fr, 0);
688 shareout(NULL, "n %s %s %s %s\n", handle, host && host[0] ? host : "none",
689 pass, x);
690 }
691 if (bu == NULL)
692 bu = u;
693 else {
694 if ((bu == userlist) && (lastuser != NULL))
695 x = lastuser;
696 else
697 x = bu;
698 while (x->next != NULL)
699 x = x->next;
700 x->next = u;
701 if (bu == userlist)
702 lastuser = u;
703 }
704 return bu;
705 }
706
707 void freeuser(struct userrec *u)
708 {
709 struct user_entry *ue, *ut;
710 struct chanuserrec *ch, *z;
711
712 if (u == NULL)
713 return;
714 ch = u->chanrec;
715 while (ch) {
716 z = ch;
717 ch = ch->next;
718 if (z->info != NULL)
719 nfree(z->info);
720 nfree(z);
721 }
722 u->chanrec = NULL;
723 for (ue = u->entries; ue; ue = ut) {
724 ut = ue->next;
725 if (ue->name) {
726 struct list_type *lt, *ltt;
727
728 for (lt = ue->u.list; lt; lt = ltt) {
729 ltt = lt->next;
730 nfree(lt->extra);
731 nfree(lt);
732 }
733 nfree(ue->name);
734 nfree(ue);
735 } else {
736 ue->type->kill(ue);
737 }
738 }
739 nfree(u);
740 }
741
742 int deluser(char *handle)
743 {
744 struct userrec *u = userlist, *prev = NULL;
745 int fnd = 0;
746
747 while ((u != NULL) && (!fnd)) {
748 if (!egg_strcasecmp(u->handle, handle))
749 fnd = 1;
750 else {
751 prev = u;
752 u = u->next;
753 }
754 }
755 if (!fnd)
756 return 0;
757 if (prev == NULL)
758 userlist = u->next;
759 else
760 prev->next = u->next;
761 if (!noshare && (handle[0] != '*') && !(u->flags & USER_UNSHARED))
762 shareout(NULL, "k %s\n", handle);
763 for (fnd = 0; fnd < dcc_total; fnd++)
764 if (dcc[fnd].user == u)
765 dcc[fnd].user = 0; /* Clear any dcc users for this entry,
766 * null is safe-ish */
767 clear_chanlist();
768 freeuser(u);
769 lastuser = NULL;
770 return 1;
771 }
772
773 int delhost_by_handle(char *handle, char *host)
774 {
775 struct userrec *u;
776 struct list_type *q, *qnext, *qprev;
777 struct user_entry *e = NULL;
778 int i = 0;
779
780 Context;
781 u = get_user_by_handle(userlist, handle);
782 if (!u)
783 return 0;
784 q = get_user(&USERENTRY_HOSTS, u);
785 qprev = q;
786 if (q) {
787 if (!rfc_casecmp(q->extra, host)) {
788 e = find_user_entry(&USERENTRY_HOSTS, u);
789 e->u.extra = q->next;
790 nfree(q->extra);
791 nfree(q);
792 i++;
793 qprev = NULL;
794 q = e->u.extra;
795 } else
796 q = q->next;
797 while (q) {
798 qnext = q->next;
799 if (!rfc_casecmp(q->extra, host)) {
800 if (qprev)
801 qprev->next = q->next;
802 else if (e) {
803 e->u.extra = q->next;
804 qprev = NULL;
805 }
806 nfree(q->extra);
807 nfree(q);
808 i++;
809 } else
810 qprev = q;
811 q = qnext;
812 }
813 }
814 if (!qprev)
815 set_user(&USERENTRY_HOSTS, u, "none");
816 if (!noshare && i && !(u->flags & USER_UNSHARED))
817 shareout(NULL, "-h %s %s\n", handle, host);
818 clear_chanlist();
819 return i;
820 }
821
822 void addhost_by_handle(char *handle, char *host)
823 {
824 struct userrec *u = get_user_by_handle(userlist, handle);
825
826 set_user(&USERENTRY_HOSTS, u, host);
827 /* u will be cached, so really no overhead, even tho this looks dumb: */
828 if ((!noshare) && !(u->flags & USER_UNSHARED)) {
829 if (u->flags & USER_BOT)
830 shareout(NULL, "+bh %s %s\n", handle, host);
831 else
832 shareout(NULL, "+h %s %s\n", handle, host);
833 }
834 clear_chanlist();
835 }
836
837 void touch_laston(struct userrec *u, char *where, time_t timeval)
838 {
839 if (!u)
840 return;
841 if (timeval > 1) {
842 struct laston_info *li =
843 (struct laston_info *) get_user(&USERENTRY_LASTON, u);
844
845 if (!li)
846 li = nmalloc(sizeof(struct laston_info));
847
848 else if (li->lastonplace)
849 nfree(li->lastonplace);
850 li->laston = timeval;
851 if (where) {
852 li->lastonplace = nmalloc(strlen(where) + 1);
853 strcpy(li->lastonplace, where);
854 } else
855 li->lastonplace = NULL;
856 set_user(&USERENTRY_LASTON, u, li);
857 } else if (timeval == 1) {
858 set_user(&USERENTRY_LASTON, u, 0);
859 }
860 }
861
862 /* Go through all channel records and try to find a matching
863 * nick. Will return the user's user record if that is known
864 * to the bot. (Fabian)
865 *
866 * Warning: This is unreliable by concept!
867 */
868 struct userrec *get_user_by_nick(char *nick)
869 {
870 struct chanset_t *chan = chanset;
871 memberlist *m;
872
873 Context;
874 while (chan) {
875 m = chan->channel.member;
876 while (m && m->nick[0]) {
877 if (!rfc_casecmp(nick, m->nick)) {
878 char word[512];
879
880 sprintf(word, "%s!%s", m->nick, m->userhost);
881 /* No need to check the return value ourself */
882 return get_user_by_host(word);;
883 }
884 m = m->next;
885 }
886 chan = chan->next;
887 }
888 /* Sorry, no matches */
889 return NULL;
890 }
891
892 void user_del_chan(char *dname)
893 {
894 struct chanuserrec *ch, *och;
895 struct userrec *u;
896
897 for (u = userlist; u; u = u->next) {
898 ch = u->chanrec;
899 och = NULL;
900 while (ch) {
901 if (!rfc_casecmp(dname, ch->channel)) {
902 if (och)
903 och->next = ch->next;
904 else
905 u->chanrec = ch->next;
906
907 if (ch->info)
908 nfree(ch->info);
909 nfree(ch);
910 break;
911 }
912 och = ch;
913 ch = ch->next;
914 }
915 }
916 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23