/[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.52 - (show annotations) (download) (as text)
Fri Sep 20 21:41:49 2002 UTC (17 years, 4 months ago) by stdarg
Branch: MAIN
Changes since 1.51: +3 -6 lines
File MIME type: text/x-chdr
* Mo-Ize removed the botnet code, thanks

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23