userrec.c 17 KB

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