/[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.55 - (show annotations) (download) (as text)
Wed Jan 29 07:42:50 2003 UTC (17 years ago) by wcc
Branch: MAIN
Changes since 1.54: +27 -3 lines
File MIME type: text/x-chdr
* Synch 1.6: save_ignores, save_ignores2, killtransfer-segv,
             bugfixes (whatever applied), header-fixes, hello.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23