/[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.23 - (show annotations) (download) (as text)
Sun Aug 27 18:52:26 2000 UTC (19 years, 5 months ago) by fabian
Branch: MAIN
Changes since 1.22: +13 -8 lines
File MIME type: text/x-chdr
<fabian> userfile_disappear 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.22 2000/08/07 10:09:53 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. Also write the channel file at the same time
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 }
590
591 int change_handle(struct userrec *u, char *newh)
592 {
593 int i;
594 char s[HANDLEN + 1];
595
596 if (!u)
597 return 0;
598 /* Nothing that will confuse the userfile */
599 if (newh[1] == 0 && strchr(BADHANDCHARS, newh[0]))
600 return 0;
601 check_tcl_nkch(u->handle, newh);
602 /* Yes, even send bot nick changes now: */
603 if (!noshare && !(u->flags & USER_UNSHARED))
604 shareout(NULL, "h %s %s\n", u->handle, newh);
605 strncpy(s, u->handle, HANDLEN);
606 s[HANDLEN] = 0;
607 strncpy(u->handle, newh, HANDLEN);
608 u->handle[HANDLEN] = 0;
609 for (i = 0; i < dcc_total; i++)
610 if (!egg_strcasecmp(dcc[i].nick, s) && dcc[i].type != &DCC_BOT) {
611 strncpy(dcc[i].nick, newh, HANDLEN);
612 dcc[i].nick[HANDLEN] = 0;
613 if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
614 chanout_but(-1, dcc[i].u.chat->channel,
615 "*** Handle change: %s -> %s\n", s, newh);
616 if (dcc[i].u.chat->channel < 100000)
617 botnet_send_nkch(i, s);
618 }
619 }
620 return 1;
621 }
622
623 extern int noxtra;
624
625 struct userrec *adduser(struct userrec *bu, char *handle, char *host,
626 char *pass, int flags)
627 {
628 struct userrec *u, *x;
629 struct xtra_key *xk;
630 int oldshare = noshare;
631
632 noshare = 1;
633 u = (struct userrec *) nmalloc(sizeof(struct userrec));
634
635 /* u->next=bu; bu=u; */
636 strncpy(u->handle, handle, HANDLEN);
637 u->handle[HANDLEN] = 0;
638 u->next = NULL;
639 u->chanrec = NULL;
640 u->entries = NULL;
641 if (flags != USER_DEFAULT) { /* drummer */
642 u->flags = flags;
643 u->flags_udef = 0;
644 } else {
645 u->flags = default_flags;
646 u->flags_udef = default_uflags;
647 }
648 set_user(&USERENTRY_PASS, u, pass);
649 if (!noxtra) {
650 xk = nmalloc(sizeof(struct xtra_key));
651
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