/[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.44 - (show annotations) (download) (as text)
Wed Feb 13 22:57:37 2002 UTC (17 years, 11 months ago) by ite
Branch: MAIN
Changes since 1.43: +2 -1 lines
File MIME type: text/x-chdr
HQ user's nick to be defined only once.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23