userrec.c 17 KB

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