/[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.32 - (show annotations) (download) (as text)
Mon Jul 16 05:14:33 2001 UTC (18 years, 6 months ago) by guppy
Branch: MAIN
CVS Tags: eggdrop1_6_6
Changes since 1.31: +2 -2 lines
File MIME type: text/x-chdr
fixed a tiny buffer overflow in cmd_whois ..

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23