/[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.60 - (show annotations) (download) (as text)
Mon Feb 17 10:22:30 2003 UTC (16 years, 11 months ago) by stdarg
Branch: MAIN
Changes since 1.59: +1 -2 lines
File MIME type: text/x-chdr
* Moved irccmp to libeggdrop
* Initial support for 005 numeric

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23