userrec.c 17 KB

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