/[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.62 - (show annotations) (download) (as text)
Sun May 11 02:40:28 2003 UTC (16 years, 4 months ago) by stdarg
Branch: MAIN
CVS Tags: HEAD
Changes since 1.61: +1 -1 lines
File MIME type: text/x-chdr
FILE REMOVED
* Remove some stray files

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23