/[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.37 - (show annotations) (download) (as text)
Mon Oct 15 18:47:11 2001 UTC (18 years, 3 months ago) by poptix
Branch: MAIN
Changes since 1.36: +3 -3 lines
File MIME type: text/x-chdr
* Oops, forgot to fix it here too, weren't supposed to free xk,xk->data in adduser

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23