/[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.25 - (show annotations) (download) (as text)
Sun Oct 1 19:11:43 2000 UTC (19 years, 4 months ago) by fabian
Branch: MAIN
Changes since 1.24: +5 -3 lines
File MIME type: text/x-chdr
fabian: applied notes_lang_fix patch

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23