/[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.50 - (show annotations) (download) (as text)
Mon Jun 17 16:39:38 2002 UTC (17 years, 7 months ago) by guppy
Branch: MAIN
Changes since 1.49: +1 -75 lines
File MIME type: text/x-chdr
removed sort_users since any decent userfile backend should do this on the fly -- i.e.. sql or our new user api that is being worked on

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23