userrec.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  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 "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,
  27. dcc_total;
  28. extern char userfile[], ver[], 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 = NULL;
  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. static char buf[512] = "";
  60. char *p = NULL;
  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 = NULL, *ret = NULL;
  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 = NULL;
  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 = NULL, *v = NULL;
  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 = NULL;
  157. for (i = 0; i < dcc_total; i++)
  158. dcc[i].user = NULL;
  159. conf.bot->u = NULL;
  160. clear_chanlist();
  161. lastuser = NULL;
  162. while (global_ign)
  163. delignore(global_ign->igmask);
  164. clear_masks(global_bans);
  165. clear_masks(global_exempts);
  166. clear_masks(global_invites);
  167. global_exempts = global_invites = global_bans = NULL;
  168. for (cst = chanset; cst; cst = cst->next) {
  169. clear_masks(cst->bans);
  170. clear_masks(cst->exempts);
  171. clear_masks(cst->invites);
  172. cst->bans = cst->exempts = cst->invites = NULL;
  173. }
  174. }
  175. /* Remember to set your userlist to NULL after calling this */
  176. }
  177. /* Find CLOSEST host match
  178. * (if "*!*@*" and "*!*@*clemson.edu" both match, use the latter!)
  179. *
  180. * Checks the chanlist first, to possibly avoid needless search.
  181. */
  182. struct userrec *get_user_by_host(char *host)
  183. {
  184. struct userrec *u = NULL, *ret = NULL;
  185. struct list_type *q = NULL;
  186. int cnt, i;
  187. char host2[UHOSTLEN] = "";
  188. if (host == NULL)
  189. return NULL;
  190. rmspace(host);
  191. if (!host[0])
  192. return NULL;
  193. ret = check_chanlist(host);
  194. cnt = 0;
  195. if (ret != NULL) {
  196. cache_hit++;
  197. return ret;
  198. }
  199. cache_miss++;
  200. strncpyz(host2, host, sizeof host2);
  201. host = fixfrom(host);
  202. for (u = userlist; u; u = u->next) {
  203. q = get_user(&USERENTRY_HOSTS, u);
  204. for (; q; q = q->next) {
  205. i = wild_match(q->extra, host);
  206. if (i > cnt) {
  207. ret = u;
  208. cnt = i;
  209. }
  210. }
  211. }
  212. if (ret != NULL) {
  213. lastuser = ret;
  214. set_chanlist(host2, ret);
  215. }
  216. return ret;
  217. }
  218. /* use fixfrom() or dont? (drummer)
  219. */
  220. struct userrec *get_user_by_equal_host(char *host)
  221. {
  222. struct userrec *u = NULL;
  223. struct list_type *q = NULL;
  224. for (u = userlist; u; u = u->next)
  225. for (q = get_user(&USERENTRY_HOSTS, u); q; q = q->next)
  226. if (!rfc_casecmp(q->extra, host))
  227. return u;
  228. return NULL;
  229. }
  230. /* Try: pass_match_by_host("-",host)
  231. * will return 1 if no password is set for that host
  232. */
  233. int u_pass_match(struct userrec *u, char *in)
  234. {
  235. char *cmp = NULL, new[32] = "", pass[16] = "";
  236. if (!u)
  237. return 0;
  238. egg_snprintf(pass, sizeof pass, "%s", in);
  239. cmp = get_user(&USERENTRY_PASS, u);
  240. if (!cmp && (!pass[0] || (pass[0] == '-')))
  241. return 1;
  242. if (!cmp || !pass || !pass[0] || (pass[0] == '-'))
  243. return 0;
  244. if (u->flags & USER_BOT) {
  245. if (!strcmp(cmp, pass))
  246. return 1;
  247. } else {
  248. if (strlen(pass) > 15)
  249. pass[15] = 0;
  250. encrypt_pass(pass, new);
  251. if (!strcmp(cmp, new))
  252. return 1;
  253. }
  254. return 0;
  255. }
  256. int write_user(struct userrec *u, FILE * f, int idx)
  257. {
  258. char s[181] = "";
  259. struct chanuserrec *ch = NULL;
  260. struct chanset_t *cst = NULL;
  261. struct user_entry *ue = NULL;
  262. struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
  263. fr.global = u->flags;
  264. fr.udef_global = u->flags_udef;
  265. build_flags(s, &fr, NULL);
  266. if (lfprintf(f, "%-10s - %-24s\n", u->handle, s) == EOF)
  267. return 0;
  268. for (ch = u->chanrec; ch; ch = ch->next) {
  269. cst = findchan_by_dname(ch->channel);
  270. if (cst && ((idx < 0) || channel_shared(cst))) {
  271. if (idx >= 0) {
  272. fr.match = (FR_CHAN | FR_BOT);
  273. get_user_flagrec(dcc[idx].user, &fr, ch->channel);
  274. } else
  275. fr.chan = BOT_SHARE;
  276. if ((fr.chan & BOT_SHARE) || (1)) {
  277. fr.match = FR_CHAN;
  278. fr.chan = ch->flags;
  279. fr.udef_chan = ch->flags_udef;
  280. build_flags(s, &fr, NULL);
  281. if (lfprintf(f, "! %-20s %lu %-10s %s\n", ch->channel, ch->laston, s, 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 = NULL;
  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 = 1;
  340. struct userrec *last = NULL, *p = NULL, *c = NULL, *n = NULL;
  341. while ((userlist != last) && (again)) {
  342. p = NULL;
  343. c = userlist;
  344. n = c->next;
  345. again = 0;
  346. while (n != last) {
  347. if (sort_compare(c, n)) {
  348. again = 1;
  349. c->next = n->next;
  350. n->next = c;
  351. if (p == NULL)
  352. userlist = n;
  353. else
  354. p->next = n;
  355. }
  356. p = c;
  357. c = n;
  358. n = n->next;
  359. }
  360. last = c;
  361. }
  362. }
  363. /* Rewrite the entire user file. Call USERFILE hook as well, probably
  364. * causing the channel file to be rewritten as well.
  365. */
  366. int write_userfile(int idx)
  367. {
  368. FILE *f = NULL;
  369. char *new_userfile = NULL, s1[81] = "", backup[DIRMAX] = "";
  370. time_t tt;
  371. struct userrec *u = NULL;
  372. int ok;
  373. if (userlist == NULL)
  374. return 1; /* No point in saving userfile */
  375. new_userfile = malloc(strlen(userfile) + 5);
  376. sprintf(new_userfile, "%s~new", userfile);
  377. f = fopen(new_userfile, "w");
  378. chmod(new_userfile, 0600);
  379. if (f == NULL) {
  380. putlog(LOG_MISC, "*", USERF_ERRWRITE);
  381. free(new_userfile);
  382. return 2;
  383. }
  384. // putlog(LOG_MISC, "*", USERF_WRITING);
  385. if (sort_users)
  386. sort_userlist();
  387. tt = now;
  388. strcpy(s1, ctime(&tt));
  389. lfprintf(f, "#4v: %s -- %s -- written %s", ver, conf.bot->nick, s1);
  390. ok = 1;
  391. fclose(f);
  392. call_hook(HOOK_USERFILE);
  393. f = fopen(new_userfile, "a");
  394. putlog(LOG_DEBUG, "@", "Writing user entries.");
  395. for (u = userlist; u && ok; u = u->next)
  396. ok = write_user(u, f, idx);
  397. if (!ok || fflush(f)) {
  398. putlog(LOG_MISC, "*", "%s (%s)", USERF_ERRWRITE, strerror(ferror(f)));
  399. fclose(f);
  400. free(new_userfile);
  401. return 3;
  402. }
  403. fclose(f);
  404. putlog(LOG_DEBUG, "@", "Done writing userfile.");
  405. egg_snprintf(backup, sizeof backup, "%s%s~", tempdir, userfile);
  406. copyfile(userfile, backup);
  407. movefile(new_userfile, userfile);
  408. free(new_userfile);
  409. return 0;
  410. }
  411. int change_handle(struct userrec *u, char *newh)
  412. {
  413. int i;
  414. char s[HANDLEN + 1] = "";
  415. if (!u)
  416. return 0;
  417. /* Nothing that will confuse the userfile */
  418. if (!newh[1] && strchr(BADHANDCHARS, newh[0]))
  419. return 0;
  420. check_bind_nkch(u->handle, newh);
  421. /* Yes, even send bot nick changes now: */
  422. if (!noshare && !(u->flags & USER_UNSHARED))
  423. shareout(NULL, "h %s %s\n", u->handle, newh);
  424. strncpyz(s, u->handle, sizeof s);
  425. strncpyz(u->handle, newh, sizeof u->handle);
  426. for (i = 0; i < dcc_total; i++)
  427. if (dcc[i].type != &DCC_BOT && !egg_strcasecmp(dcc[i].nick, s)) {
  428. strncpyz(dcc[i].nick, newh, sizeof dcc[i].nick);
  429. if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
  430. chanout_but(-1, dcc[i].u.chat->channel,
  431. "*** Handle change: %s -> %s\n", s, newh);
  432. if (dcc[i].u.chat->channel < GLOBAL_CHANS)
  433. botnet_send_nkch(i, s);
  434. }
  435. }
  436. return 1;
  437. }
  438. struct userrec *adduser(struct userrec *bu, char *handle, char *host, char *pass, int flags)
  439. {
  440. struct userrec *u = NULL, *x = NULL;
  441. int oldshare = noshare;
  442. noshare = 1;
  443. u = (struct userrec *) calloc(1, sizeof(struct userrec));
  444. /* u->next=bu; bu=u; */
  445. strncpyz(u->handle, handle, sizeof u->handle);
  446. u->next = NULL;
  447. u->chanrec = NULL;
  448. u->entries = NULL;
  449. if (flags != USER_DEFAULT) { /* drummer */
  450. u->flags = flags;
  451. u->flags_udef = 0;
  452. } else {
  453. u->flags = default_flags;
  454. u->flags_udef = default_uflags;
  455. }
  456. set_user(&USERENTRY_PASS, u, pass);
  457. /* Strip out commas -- they're illegal */
  458. if (host && host[0]) {
  459. char *p = NULL;
  460. /* About this fixfrom():
  461. * We should use this fixfrom before every call of adduser()
  462. * but its much easier to use here... (drummer)
  463. * Only use it if we have a host :) (dw)
  464. */
  465. host = fixfrom(host);
  466. p = strchr(host, ',');
  467. while (p != NULL) {
  468. *p = '?';
  469. p = strchr(host, ',');
  470. }
  471. set_user(&USERENTRY_HOSTS, u, host);
  472. } else
  473. set_user(&USERENTRY_HOSTS, u, "none");
  474. if (bu == userlist)
  475. clear_chanlist();
  476. noshare = oldshare;
  477. if ((!noshare) && (handle[0] != '*') && (!(flags & USER_UNSHARED)) && (bu == userlist)) {
  478. struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
  479. char x[100] = "";
  480. fr.global = u->flags;
  481. fr.udef_global = u->flags_udef;
  482. build_flags(x, &fr, 0);
  483. shareout(NULL, "n %s %s %s %s\n", handle, host && host[0] ? host : "none", pass, x);
  484. }
  485. if (bu == NULL)
  486. bu = u;
  487. else {
  488. if ((bu == userlist) && (lastuser != NULL))
  489. x = lastuser;
  490. else
  491. x = bu;
  492. while (x->next != NULL)
  493. x = x->next;
  494. x->next = u;
  495. if (bu == userlist)
  496. lastuser = u;
  497. }
  498. return bu;
  499. }
  500. void freeuser(struct userrec *u)
  501. {
  502. struct user_entry *ue = NULL, *ut = NULL;
  503. struct chanuserrec *ch = NULL, *z = NULL;
  504. if (u == NULL)
  505. return;
  506. ch = u->chanrec;
  507. while (ch) {
  508. z = ch;
  509. ch = ch->next;
  510. if (z->info != NULL)
  511. free(z->info);
  512. free(z);
  513. }
  514. u->chanrec = NULL;
  515. for (ue = u->entries; ue; ue = ut) {
  516. ut = ue->next;
  517. if (ue->name) {
  518. struct list_type *lt, *ltt;
  519. for (lt = ue->u.list; lt; lt = ltt) {
  520. ltt = lt->next;
  521. free(lt->extra);
  522. free(lt);
  523. }
  524. free(ue->name);
  525. free(ue);
  526. } else {
  527. ue->type->kill(ue);
  528. }
  529. }
  530. free(u);
  531. }
  532. int deluser(char *handle)
  533. {
  534. struct userrec *u = userlist, *prev = NULL;
  535. int fnd = 0;
  536. while ((u != NULL) && (!fnd)) {
  537. if (!egg_strcasecmp(u->handle, handle))
  538. fnd = 1;
  539. else {
  540. prev = u;
  541. u = u->next;
  542. }
  543. }
  544. if (!fnd)
  545. return 0;
  546. if (prev == NULL)
  547. userlist = u->next;
  548. else
  549. prev->next = u->next;
  550. if (!noshare && (handle[0] != '*') && !(u->flags & USER_UNSHARED))
  551. shareout(NULL, "k %s\n", handle);
  552. for (fnd = 0; fnd < dcc_total; fnd++)
  553. if (dcc[fnd].user == u)
  554. dcc[fnd].user = 0; /* Clear any dcc users for this entry,
  555. * null is safe-ish */
  556. clear_chanlist();
  557. freeuser(u);
  558. lastuser = NULL;
  559. return 1;
  560. }
  561. int delhost_by_handle(char *handle, char *host)
  562. {
  563. struct userrec *u = NULL;
  564. struct list_type *q = NULL, *qnext = NULL, *qprev = NULL;
  565. struct user_entry *e = NULL;
  566. int i = 0;
  567. u = get_user_by_handle(userlist, handle);
  568. if (!u)
  569. return 0;
  570. q = get_user(&USERENTRY_HOSTS, u);
  571. qprev = q;
  572. if (q) {
  573. if (!rfc_casecmp(q->extra, host)) {
  574. e = find_user_entry(&USERENTRY_HOSTS, u);
  575. e->u.extra = q->next;
  576. free(q->extra);
  577. free(q);
  578. i++;
  579. qprev = NULL;
  580. q = e->u.extra;
  581. } else
  582. q = q->next;
  583. while (q) {
  584. qnext = q->next;
  585. if (!rfc_casecmp(q->extra, host)) {
  586. if (qprev)
  587. qprev->next = q->next;
  588. else if (e) {
  589. e->u.extra = q->next;
  590. qprev = NULL;
  591. }
  592. free(q->extra);
  593. free(q);
  594. i++;
  595. } else
  596. qprev = q;
  597. q = qnext;
  598. }
  599. }
  600. if (!qprev)
  601. set_user(&USERENTRY_HOSTS, u, "none");
  602. if (!noshare && i && !(u->flags & USER_UNSHARED))
  603. shareout(NULL, "-h %s %s\n", handle, host);
  604. clear_chanlist();
  605. return i;
  606. }
  607. void addhost_by_handle(char *handle, char *host)
  608. {
  609. struct userrec *u = get_user_by_handle(userlist, handle);
  610. set_user(&USERENTRY_HOSTS, u, host);
  611. /* u will be cached, so really no overhead, even tho this looks dumb: */
  612. if ((!noshare) && !(u->flags & USER_UNSHARED)) {
  613. if (u->flags & USER_BOT)
  614. shareout(NULL, "+bh %s %s\n", handle, host);
  615. else
  616. shareout(NULL, "+h %s %s\n", handle, host);
  617. }
  618. clear_chanlist();
  619. }
  620. void touch_laston(struct userrec *u, char *where, time_t timeval)
  621. {
  622. if (!u)
  623. return;
  624. if (timeval > 1) {
  625. struct laston_info *li = (struct laston_info *) get_user(&USERENTRY_LASTON, u);
  626. if (!li)
  627. li = calloc(1, sizeof(struct laston_info));
  628. else if (li->lastonplace)
  629. free(li->lastonplace);
  630. li->laston = timeval;
  631. if (where) {
  632. li->lastonplace = strdup(where);
  633. } else
  634. li->lastonplace = NULL;
  635. set_user(&USERENTRY_LASTON, u, li);
  636. } else if (timeval == 1)
  637. set_user(&USERENTRY_LASTON, u, 0);
  638. }
  639. /* Go through all channel records and try to find a matching
  640. * nick. Will return the user's user record if that is known
  641. * to the bot. (Fabian)
  642. *
  643. * Warning: This is unreliable by concept!
  644. */
  645. struct userrec *get_user_by_nick(char *nick)
  646. {
  647. struct chanset_t *chan = NULL;
  648. memberlist *m = NULL;
  649. for (chan = chanset; chan; chan = chan->next) {
  650. for (m = chan->channel.member; m && m->nick[0] ;m = m->next) {
  651. if (!rfc_casecmp(nick, m->nick)) {
  652. char word[512] = "";
  653. egg_snprintf(word, sizeof word, "%s!%s", m->nick, m->userhost);
  654. /* No need to check the return value ourself */
  655. return get_user_by_host(word);;
  656. }
  657. }
  658. }
  659. /* Sorry, no matches */
  660. return NULL;
  661. }
  662. void user_del_chan(char *dname)
  663. {
  664. struct chanuserrec *ch = NULL, *och = NULL;
  665. struct userrec *u = NULL;
  666. for (u = userlist; u; u = u->next) {
  667. ch = u->chanrec;
  668. och = NULL;
  669. while (ch) {
  670. if (!rfc_casecmp(dname, ch->channel)) {
  671. if (och)
  672. och->next = ch->next;
  673. else
  674. u->chanrec = ch->next;
  675. if (ch->info)
  676. free(ch->info);
  677. free(ch);
  678. break;
  679. }
  680. och = ch;
  681. ch = ch->next;
  682. }
  683. }
  684. }