/[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.58 - (show annotations) (download) (as text)
Mon Feb 3 11:41:35 2003 UTC (16 years, 11 months ago) by wcc
Branch: MAIN
Changes since 1.57: +5 -6 lines
File MIME type: text/x-chdr
* More botnet removal.

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.57 2003/01/30 07:47: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, dcc_total, share_greet;
49 extern char userfile[], ver[], myname[];
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, i->user ? i->user :
355 myname, i->added, i->msg ? i->msg : "") == EOF) {
356 if (mask)
357 free(mask);
358 return 0;
359 }
360 free(mask);
361 }
362 return 1;
363 }
364
365 /* Rewrite the entire user file. Call USERFILE hook as well, probably
366 * causing the channel file to be rewritten as well.
367 */
368 void write_userfile(int idx)
369 {
370 FILE *f;
371 char *new_userfile;
372 char s1[81];
373 time_t tt;
374 struct userrec *u;
375 int ok;
376
377 if (userlist == NULL)
378 return; /* No point in saving userfile */
379
380 new_userfile = malloc(strlen(userfile) + 5);
381 sprintf(new_userfile, "%s~new", userfile);
382
383 f = fopen(new_userfile, "w");
384 chmod(new_userfile, userfile_perm);
385 if (f == NULL) {
386 putlog(LOG_MISC, "*", _("ERROR writing user file."));
387 free(new_userfile);
388 return;
389 }
390 tt = now;
391 strcpy(s1, ctime(&tt));
392 fprintf(f, "#4v: %s -- %s -- written %s", ver, myname, s1);
393 ok = 1;
394 for (u = userlist; u && ok; u = u->next)
395 if (!write_user(u, f, idx))
396 ok = 0;
397 if (!ok || !write_ignores(f, -1) || fflush(f)) {
398 putlog(LOG_MISC, "*", "%s (%s)", _("ERROR writing user file."), strerror(ferror(f)));
399 fclose(f);
400 free(new_userfile);
401 return;
402 }
403 fclose(f);
404 call_hook(HOOK_USERFILE);
405 movefile(new_userfile, userfile);
406 free(new_userfile);
407 }
408
409 int change_handle(struct userrec *u, char *newh)
410 {
411 int i;
412 char s[HANDLEN + 1];
413
414 if (!u)
415 return 0;
416 /* Nothing that will confuse the userfile */
417 if (!newh[1] && strchr(BADHANDCHARS, newh[0]))
418 return 0;
419 /* Yes, even send bot nick changes now: */
420 if (!noshare && !(u->flags & USER_UNSHARED))
421 shareout(NULL, "h %s %s\n", u->handle, newh);
422 strlcpy(s, u->handle, sizeof s);
423 strlcpy(u->handle, newh, sizeof u->handle);
424 for (i = 0; i < dcc_total; i++)
425 if (!strcasecmp(dcc[i].nick, s)) {
426 strlcpy(dcc[i].nick, newh, sizeof dcc[i].nick);
427 if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
428 chanout_but(-1, dcc[i].u.chat->channel,
429 "*** Handle change: %s -> %s\n", s, newh);
430 }
431 }
432 return 1;
433 }
434
435 extern int noxtra;
436
437 struct userrec *adduser(struct userrec *bu, char *handle, char *host,
438 char *pass, int flags)
439 {
440 struct userrec *u, *x;
441 struct xtra_key *xk;
442 int oldshare = noshare;
443
444 noshare = 1;
445 u = (struct userrec *) malloc(sizeof(struct userrec));
446
447 /* u->next=bu; bu=u; */
448 strlcpy(u->handle, handle, sizeof u->handle);
449 u->next = NULL;
450 u->chanrec = NULL;
451 u->entries = NULL;
452 if (flags != USER_DEFAULT) { /* drummer */
453 u->flags = flags;
454 u->flags_udef = 0;
455 } else {
456 u->flags = default_flags;
457 u->flags_udef = default_uflags;
458 }
459 set_user(&USERENTRY_PASS, u, pass);
460 if (!noxtra) {
461 char *now2;
462 xk = malloc(sizeof(struct xtra_key));
463 xk->key = strdup("created");
464 now2 = malloc(15);
465 sprintf(now2, "%lu", now);
466 xk->data = malloc(strlen(now2) +1);
467 sprintf(xk->data, "%lu", now);
468 set_user(&USERENTRY_XTRA, u, xk);
469 free(now2);
470 }
471 /* Strip out commas -- they're illegal */
472 if (host && host[0]) {
473 char *p;
474
475 /* About this fixfrom():
476 * We should use this fixfrom before every call of adduser()
477 * but its much easier to use here... (drummer)
478 * Only use it if we have a host :) (dw)
479 */
480 host = fixfrom(host);
481 p = strchr(host, ',');
482
483 while (p != NULL) {
484 *p = '?';
485 p = strchr(host, ',');
486 }
487 set_user(&USERENTRY_HOSTS, u, host);
488 } else
489 set_user(&USERENTRY_HOSTS, u, "none");
490 if (bu == userlist)
491 clear_chanlist();
492 noshare = oldshare;
493 if ((!noshare) && (handle[0] != '*') && (!(flags & USER_UNSHARED)) &&
494 (bu == userlist)) {
495 struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
496 char x[100];
497
498 fr.global = u->flags;
499 fr.udef_global = u->flags_udef;
500 build_flags(x, &fr, 0);
501 shareout(NULL, "n %s %s %s %s\n", handle, host && host[0] ? host : "none",
502 pass, x);
503 }
504 if (bu == NULL)
505 bu = u;
506 else {
507 if ((bu == userlist) && (lastuser != NULL))
508 x = lastuser;
509 else
510 x = bu;
511 while (x->next != NULL)
512 x = x->next;
513 x->next = u;
514 if (bu == userlist)
515 lastuser = u;
516 }
517 return bu;
518 }
519
520 void freeuser(struct userrec *u)
521 {
522 struct user_entry *ue, *ut;
523 struct chanuserrec *ch, *z;
524
525 if (u == NULL)
526 return;
527 ch = u->chanrec;
528 while (ch) {
529 z = ch;
530 ch = ch->next;
531 if (z->info != NULL)
532 free(z->info);
533 free(z);
534 }
535 u->chanrec = NULL;
536 for (ue = u->entries; ue; ue = ut) {
537 ut = ue->next;
538 if (ue->name) {
539 struct list_type *lt, *ltt;
540
541 for (lt = ue->u.list; lt; lt = ltt) {
542 ltt = lt->next;
543 free(lt->extra);
544 free(lt);
545 }
546 free(ue->name);
547 free(ue);
548 } else {
549 ue->type->kill(ue);
550 }
551 }
552 free(u);
553 }
554
555 int deluser(char *handle)
556 {
557 struct userrec *u = userlist, *prev = NULL;
558 int fnd = 0;
559
560 while ((u != NULL) && (!fnd)) {
561 if (!strcasecmp(u->handle, handle))
562 fnd = 1;
563 else {
564 prev = u;
565 u = u->next;
566 }
567 }
568 if (!fnd)
569 return 0;
570 if (prev == NULL)
571 userlist = u->next;
572 else
573 prev->next = u->next;
574 if (!noshare && (handle[0] != '*') && !(u->flags & USER_UNSHARED))
575 shareout(NULL, "k %s\n", handle);
576 for (fnd = 0; fnd < dcc_total; fnd++)
577 if (dcc[fnd].type && dcc[fnd].user == u)
578 dcc[fnd].user = 0; /* Clear any dcc users for this entry,
579 * null is safe-ish */
580 clear_chanlist();
581 freeuser(u);
582 lastuser = NULL;
583 return 1;
584 }
585
586 int delhost_by_handle(char *handle, char *host)
587 {
588 struct userrec *u;
589 struct list_type *q, *qnext, *qprev;
590 struct user_entry *e = NULL;
591 int i = 0;
592
593 u = get_user_by_handle(userlist, handle);
594 if (!u)
595 return 0;
596 q = get_user(&USERENTRY_HOSTS, u);
597 qprev = q;
598 if (q) {
599 if (!irccmp(q->extra, host)) {
600 e = find_user_entry(&USERENTRY_HOSTS, u);
601 e->u.extra = q->next;
602 free(q->extra);
603 free(q);
604 i++;
605 qprev = NULL;
606 q = e->u.extra;
607 } else
608 q = q->next;
609 while (q) {
610 qnext = q->next;
611 if (!irccmp(q->extra, host)) {
612 if (qprev)
613 qprev->next = q->next;
614 else if (e) {
615 e->u.extra = q->next;
616 qprev = NULL;
617 }
618 free(q->extra);
619 free(q);
620 i++;
621 } else
622 qprev = q;
623 q = qnext;
624 }
625 }
626 if (!qprev)
627 set_user(&USERENTRY_HOSTS, u, "none");
628 if (!noshare && i && !(u->flags & USER_UNSHARED))
629 shareout(NULL, "-h %s %s\n", handle, host);
630 clear_chanlist();
631 return i;
632 }
633
634 void addhost_by_handle(char *handle, char *host)
635 {
636 struct userrec *u = get_user_by_handle(userlist, handle);
637
638 set_user(&USERENTRY_HOSTS, u, host);
639 /* u will be cached, so really no overhead, even tho this looks dumb: */
640 if ((!noshare) && !(u->flags & USER_UNSHARED)) {
641 if (u->flags & USER_BOT)
642 shareout(NULL, "+bh %s %s\n", handle, host);
643 else
644 shareout(NULL, "+h %s %s\n", handle, host);
645 }
646 clear_chanlist();
647 }
648
649 void touch_laston(struct userrec *u, char *where, time_t timeval)
650 {
651 if (!u)
652 return;
653 if (timeval > 1) {
654 struct laston_info *li =
655 (struct laston_info *) get_user(&USERENTRY_LASTON, u);
656
657 if (!li)
658 li = malloc(sizeof(struct laston_info));
659
660 else if (li->lastonplace)
661 free(li->lastonplace);
662 li->laston = timeval;
663 if (where)
664 li->lastonplace = strdup(where);
665 else
666 li->lastonplace = NULL;
667 set_user(&USERENTRY_LASTON, u, li);
668 } else if (timeval == 1) {
669 set_user(&USERENTRY_LASTON, u, 0);
670 }
671 }
672
673 /* Go through all channel records and try to find a matching
674 * nick. Will return the user's user record if that is known
675 * to the bot. (Fabian)
676 *
677 * Warning: This is unreliable by concept!
678 */
679 struct userrec *get_user_by_nick(char *nick)
680 {
681 struct chanset_t *chan;
682 memberlist *m;
683
684 for (chan = chanset; chan; chan = chan->next) {
685 for (m = chan->channel.member; m && m->nick[0] ;m = m->next) {
686 if (!irccmp(nick, m->nick)) {
687 char word[512];
688
689 snprintf(word, sizeof word, "%s!%s", m->nick, m->userhost);
690 /* No need to check the return value ourself */
691 return get_user_by_host(word);;
692 }
693 }
694 }
695 /* Sorry, no matches */
696 return NULL;
697 }
698
699 void user_del_chan(char *dname)
700 {
701 struct chanuserrec *ch, *och;
702 struct userrec *u;
703
704 for (u = userlist; u; u = u->next) {
705 ch = u->chanrec;
706 och = NULL;
707 while (ch) {
708 if (!irccmp(dname, ch->channel)) {
709 if (och)
710 och->next = ch->next;
711 else
712 u->chanrec = ch->next;
713
714 if (ch->info)
715 free(ch->info);
716 free(ch);
717 break;
718 }
719 och = ch;
720 ch = ch->next;
721 }
722 }
723 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23