userrec.c 17 KB

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