/[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.57 - (show annotations) (download) (as text)
Thu Jan 30 07:47:17 2003 UTC (17 years ago) by wcc
Branch: MAIN
Changes since 1.56: +3 -3 lines
File MIME type: text/x-chdr
* Fixed a few compiler errors/warnings (found by Eule).

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23