/[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.53 - (show annotations) (download) (as text)
Thu Oct 10 04:41:59 2002 UTC (17 years, 3 months ago) by stdarg
Branch: MAIN
Changes since 1.52: +1 -3 lines
File MIME type: text/x-chdr
* Moved bind tables to libeggdrop

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23