userrec.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * userrec.c -- handles:
  22. * add_q() del_q() str2flags() flags2str() str2chflags() chflags2str()
  23. * a bunch of functions to find and change user records
  24. * change and check user (and channel-specific) flags
  25. *
  26. */
  27. #include <sys/stat.h>
  28. #include "common.h"
  29. #include "userrec.h"
  30. #include "misc.h"
  31. #include "misc_file.h"
  32. #include "rfc1459.h"
  33. #include "dcc.h"
  34. #include "botnet.h"
  35. #include "src/mod/share.mod/share.h"
  36. #include "src/mod/channels.mod/channels.h"
  37. #include "src/mod/irc.mod/irc.h"
  38. #include "main.h"
  39. #include "users.h"
  40. #include "chan.h"
  41. #include "match.h"
  42. #include "dccutil.h"
  43. #include "tandem.h"
  44. #include "chanprog.h"
  45. #include "crypt.h"
  46. #include "core_binds.h"
  47. #include "socket.h"
  48. #include "net.h"
  49. bool noshare = 1; /* don't send out to sharebots */
  50. struct userrec *userlist = NULL; /* user records are stored here */
  51. struct userrec *lastuser = NULL; /* last accessed user record */
  52. maskrec *global_bans = NULL,
  53. *global_exempts = NULL,
  54. *global_invites = NULL;
  55. struct igrec *global_ign = NULL;
  56. int cache_hit = 0,
  57. cache_miss = 0; /* temporary cache accounting */
  58. int userfile_perm = 0600; /* Userfile permissions,
  59. default rw------- */
  60. static bool sort_users = 1; /* sort the userlist when saving */
  61. int count_users(struct userrec *bu)
  62. {
  63. int tot = 0;
  64. for (struct userrec *u = bu; u; u = u->next)
  65. tot++;
  66. return tot;
  67. }
  68. static struct userrec *check_dcclist_hand(char *handle)
  69. {
  70. for (int i = 0; i < dcc_total; i++)
  71. if (dcc[i].type && !egg_strcasecmp(dcc[i].nick, handle))
  72. return dcc[i].user;
  73. return NULL;
  74. }
  75. struct userrec *host_conflicts(char *host)
  76. {
  77. struct userrec *u = NULL;
  78. struct list_type *q = NULL;
  79. for (u = userlist; u; u = u->next) {
  80. for (q = (struct list_type *) get_user(&USERENTRY_HOSTS, u); q; q = q->next) {
  81. if (wild_match(host, q->extra) || wild_match(q->extra, host))
  82. return u;
  83. }
  84. }
  85. return NULL;
  86. }
  87. struct userrec *get_user_by_handle(struct userrec *bu, char *handle)
  88. {
  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. struct userrec *ret = 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 (struct userrec *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 = 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. static void freeuser(struct userrec *);
  148. void clear_userlist(struct userrec *bu)
  149. {
  150. struct userrec *v = NULL;
  151. for (struct userrec *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 (int i = 0; i < dcc_total; i++)
  158. if (dcc[i].type)
  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. if (host == NULL)
  186. return NULL;
  187. rmspace(host);
  188. if (!host[0])
  189. return NULL;
  190. struct userrec *ret = check_chanlist(host);
  191. if (ret != NULL) {
  192. cache_hit++;
  193. return ret;
  194. }
  195. struct userrec *u = NULL;
  196. struct list_type *q = NULL;
  197. int cnt = 0, i;
  198. char host2[UHOSTLEN] = "", *p = NULL;
  199. bool do_cidr = 0;
  200. cache_miss++;
  201. strlcpy(host2, host, sizeof host2);
  202. /* do CIDR matching if given host is an ip */
  203. if ((p = strchr(host2, '@')))
  204. if (is_dotted_ip(++p))
  205. do_cidr = 1;
  206. for (u = userlist; u; u = u->next) {
  207. q = (struct list_type *) get_user(&USERENTRY_HOSTS, u);
  208. for (; q; q = q->next) {
  209. if (do_cidr) {
  210. i = match_cidr(q->extra, host);
  211. if (i > cnt) {
  212. ret = u;
  213. cnt = i;
  214. }
  215. }
  216. i = wild_match(q->extra, host);
  217. if (i > cnt) {
  218. ret = u;
  219. cnt = i;
  220. }
  221. }
  222. }
  223. if (ret != NULL) {
  224. lastuser = ret;
  225. set_chanlist(host2, ret);
  226. }
  227. return ret;
  228. }
  229. bool user_has_host(const char *handle, struct userrec *u, char *host)
  230. {
  231. if (host == NULL)
  232. return 0;
  233. rmspace(host);
  234. if (!host[0])
  235. return 0;
  236. if (!u && handle)
  237. get_user_by_handle(userlist, (char *) handle);
  238. if (!u)
  239. return 0;
  240. struct list_type *q = NULL;
  241. for (q = (struct list_type *) get_user(&USERENTRY_HOSTS, u); q; q = q->next)
  242. if (!egg_strcasecmp(q->extra, host))
  243. return 1;
  244. return 0;
  245. }
  246. void convert_password(struct userrec *u)
  247. {
  248. char *oldpass = (char *) get_user(&USERENTRY_TMPPASS, u);
  249. if (oldpass && oldpass[0]) {
  250. char *pass = NULL;
  251. /* need to convert into new password format and remove old */
  252. /* --------- this changes to reflect how to decrypt old password --------- */
  253. pass = decrypt_string(u->handle, oldpass);
  254. /* ----------------------------------------------------------------------- */
  255. if (strlen(pass) > MAXPASSLEN)
  256. pass[MAXPASSLEN] = 0;
  257. set_user(&USERENTRY_PASS, u, pass);
  258. free(pass);
  259. /* clear old record */
  260. noshare = 1;
  261. set_user(&USERENTRY_TMPPASS, u, NULL);
  262. noshare = 0;
  263. }
  264. }
  265. /* Try: pass_match_by_host("-",host)
  266. * will return 1 if no password is set for that host
  267. */
  268. int u_pass_match(struct userrec *u, char *in)
  269. {
  270. if (!u)
  271. return 0;
  272. convert_password(u);
  273. char *cmp = (char *) get_user(&USERENTRY_PASS, u), pass[MAXPASSLEN + 1] = "";
  274. simple_snprintf(pass, sizeof pass, "%s", in);
  275. if (!cmp && (!pass[0] || (pass[0] == '-')))
  276. return 1;
  277. if (!cmp || !pass[0] || (pass[0] == '-'))
  278. return 0;
  279. if (u->bot) {
  280. if (!strcmp(cmp, pass))
  281. return 1;
  282. } else {
  283. char *newpass = NULL;
  284. if (strlen(pass) > MAXPASSLEN)
  285. pass[MAXPASSLEN] = 0;
  286. newpass = encrypt_pass(u, pass);
  287. if (!strcmp(cmp, newpass)) {
  288. free(newpass);
  289. return 1;
  290. }
  291. free(newpass);
  292. }
  293. return 0;
  294. }
  295. bool write_user(struct userrec *u, FILE * f, int idx)
  296. {
  297. char s[181] = "";
  298. struct flag_record fr = {FR_GLOBAL, u->flags, 0, 0 };
  299. build_flags(s, &fr, NULL);
  300. if (lfprintf(f, "%s%-10s - %-24s\n", u->bot ? "-" : "", u->handle, s) == EOF)
  301. return 0;
  302. struct chanset_t *cst = NULL;
  303. for (struct chanuserrec *ch = u->chanrec; ch; ch = ch->next) {
  304. cst = findchan_by_dname(ch->channel);
  305. if (cst) {
  306. if (idx >= 0) {
  307. fr.match = FR_CHAN;
  308. get_user_flagrec(dcc[idx].user, &fr, ch->channel);
  309. }
  310. fr.match = FR_CHAN;
  311. fr.chan = ch->flags;
  312. build_flags(s, &fr, NULL);
  313. if (lfprintf(f, "! %-20s %li %-10s %s\n", ch->channel, (long) ch->laston, s, ch->info ? ch->info : "") == EOF)
  314. return 0;
  315. }
  316. }
  317. for (struct user_entry *ue = u->entries; ue; ue = ue->next) {
  318. #ifdef no
  319. if (ue->name) {
  320. struct list_type *lt = NULL;
  321. for (lt = ue->u.list; lt; lt = lt->next)
  322. if (lfprintf(f, "--%s %s\n", ue->name, lt->extra) == EOF)
  323. return 0;
  324. } else
  325. #endif
  326. if (ue->type)
  327. if (conf.bot->hub && !ue->type->write_userfile(f, u, ue, idx))
  328. return 0;
  329. }
  330. return 1;
  331. }
  332. static int sort_compare(struct userrec *a, struct userrec *b)
  333. {
  334. /* Order by flags, then alphabetically
  335. * first bots: +h / +a / +l / other bots
  336. * then users: +n / +m / +o / other users
  337. * return true if (a > b)
  338. */
  339. if (a->bot && b->bot) {
  340. if (bot_hublevel(a) > bot_hublevel(b))
  341. return 1;
  342. else if (bot_hublevel(a) < bot_hublevel(b))
  343. return 0;
  344. else if (bot_hublevel(a) == bot_hublevel(b) && bot_hublevel(a) == 999)
  345. return 0;
  346. } else {
  347. if (a->bot && !b->bot)
  348. return 1;
  349. if (!a->bot && b->bot)
  350. return 0;
  351. if (~a->flags & b->flags & USER_ADMIN)
  352. return 1;
  353. if (a->flags & ~b->flags & USER_ADMIN)
  354. return 0;
  355. if (~a->flags & b->flags & USER_OWNER)
  356. return 1;
  357. if (a->flags & ~b->flags & USER_OWNER)
  358. return 0;
  359. if (~a->flags & b->flags & USER_MASTER)
  360. return 1;
  361. if (a->flags & ~b->flags & USER_MASTER)
  362. return 0;
  363. if (~a->flags & b->flags & USER_OP)
  364. return 1;
  365. if (a->flags & ~b->flags & USER_OP)
  366. return 0;
  367. }
  368. return (egg_strcasecmp(a->handle, b->handle) > 0);
  369. }
  370. static void sort_userlist()
  371. {
  372. int again = 1;
  373. struct userrec *last = NULL, *p = NULL, *c = NULL, *n = NULL;
  374. while ((userlist != last) && (again)) {
  375. p = NULL;
  376. c = userlist;
  377. n = c->next;
  378. again = 0;
  379. while (n != last) {
  380. if (sort_compare(c, n)) {
  381. again = 1;
  382. c->next = n->next;
  383. n->next = c;
  384. if (p == NULL)
  385. userlist = n;
  386. else
  387. p->next = n;
  388. }
  389. p = c;
  390. c = n;
  391. n = n->next;
  392. }
  393. last = c;
  394. }
  395. }
  396. /* Rewrite the entire user file. Call USERFILE hook as well, probably
  397. * causing the channel file to be rewritten as well.
  398. */
  399. int write_userfile(int idx)
  400. {
  401. if (userlist == NULL || !conf.bot->hub)
  402. return 1; /* No point in saving userfile */
  403. FILE *f = NULL;
  404. char *new_userfile = NULL;
  405. size_t siz = strlen(userfile) + 4 + 1;
  406. new_userfile = (char *) my_calloc(1, siz);
  407. simple_snprintf(new_userfile, siz, "%s~new", userfile);
  408. f = fopen(new_userfile, "w");
  409. fixmod(new_userfile);
  410. if (f == NULL) {
  411. putlog(LOG_MISC, "*", "ERROR writing user file.");
  412. free(new_userfile);
  413. return 2;
  414. }
  415. char s1[81] = "", backup[DIRMAX] = "";
  416. bool ok = 1;
  417. if (idx >= 0)
  418. dprintf(idx, "Saving userfile...\n");
  419. if (sort_users)
  420. sort_userlist();
  421. time_t tt = now;
  422. strcpy(s1, ctime(&tt));
  423. lfprintf(f, "#4v: %s -- %s -- written %s", ver, conf.bot->nick, s1);
  424. fclose(f);
  425. /* FIXME: REMOVE AFTER 1.2.14 */
  426. bool old = 0;
  427. tand_t* bot = idx != -1 ? findbot(dcc[idx].nick) : NULL;
  428. if (bot && bot->buildts < 1175102242) /* flood-* hacks */
  429. old = 1;
  430. channels_writeuserfile(old);
  431. f = fopen(new_userfile, "a");
  432. if (f == NULL) {
  433. putlog(LOG_MISC, "*", "ERROR writing user file.");
  434. free(new_userfile);
  435. return 2;
  436. }
  437. putlog(LOG_DEBUG, "@", "Writing user entries.");
  438. for (struct userrec *u = userlist; u && ok; u = u->next)
  439. ok = write_user(u, f, -1);
  440. if (!ok || fflush(f)) {
  441. putlog(LOG_MISC, "*", "ERROR writing user file. (%s)", strerror(ferror(f)));
  442. fclose(f);
  443. free(new_userfile);
  444. return 3;
  445. }
  446. fclose(f);
  447. putlog(LOG_DEBUG, "@", "Done writing userfile.");
  448. simple_snprintf(backup, sizeof backup, "%s/%s~", conf.datadir, userfile);
  449. copyfile(userfile, backup);
  450. movefile(new_userfile, userfile);
  451. free(new_userfile);
  452. return 0;
  453. }
  454. int change_handle(struct userrec *u, char *newh)
  455. {
  456. if (!u)
  457. return 0;
  458. /* Nothing that will confuse the userfile */
  459. if (!newh[1] && strchr(BADHANDCHARS, newh[0]))
  460. return 0;
  461. /* Yes, even send bot nick changes now: */
  462. if (!noshare)
  463. shareout("h %s %s\n", u->handle, newh);
  464. char s[HANDLEN + 1] = "", *pass = NULL;
  465. pass = decrypt_pass(u);
  466. strlcpy(s, u->handle, sizeof s);
  467. strlcpy(u->handle, newh, sizeof u->handle);
  468. set_user(&USERENTRY_PASS, u, pass);
  469. OPENSSL_cleanse(pass, strlen(pass));
  470. free(pass);
  471. for (int i = 0; i < dcc_total; i++)
  472. if (dcc[i].type && dcc[i].type != &DCC_BOT && !egg_strcasecmp(dcc[i].nick, s)) {
  473. strlcpy(dcc[i].nick, newh, sizeof dcc[i].nick);
  474. if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
  475. chanout_but(-1, dcc[i].u.chat->channel, "*** Handle change: %s -> %s\n", s, newh);
  476. if (dcc[i].u.chat->channel < GLOBAL_CHANS)
  477. botnet_send_nkch(i, s);
  478. }
  479. }
  480. return 1;
  481. }
  482. struct userrec *adduser(struct userrec *bu, char *handle, char *host, char *pass, flag_t flags, int bot)
  483. {
  484. struct userrec *u = NULL, *x = NULL;
  485. int oldshare = noshare;
  486. noshare = 1;
  487. u = (struct userrec *) my_calloc(1, sizeof(struct userrec));
  488. u->bot = bot;
  489. /* u->next=bu; bu=u; */
  490. strlcpy(u->handle, handle, sizeof u->handle);
  491. u->next = NULL;
  492. u->chanrec = NULL;
  493. u->entries = NULL;
  494. if (flags != USER_DEFAULT) { /* drummer */
  495. u->flags = flags;
  496. } else {
  497. u->flags = default_flags;
  498. }
  499. set_user(&USERENTRY_PASS, u, pass);
  500. /* Strip out commas -- they're illegal */
  501. if (host && host[0]) {
  502. char *p = strchr(host, ',');
  503. while (p != NULL) {
  504. *p = '?';
  505. p = strchr(host, ',');
  506. }
  507. set_user(&USERENTRY_HOSTS, u, host);
  508. } else
  509. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  510. if (bu == userlist)
  511. clear_chanlist();
  512. noshare = oldshare;
  513. if ((!noshare) && (handle[0] != '*') && (bu == userlist)) {
  514. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  515. char xx[100] = "";
  516. fr.global = u->flags;
  517. build_flags(xx, &fr, 0);
  518. shareout("n %s%s %s %s %s\n", bot ? "-" : "", handle, host && host[0] ? host : "none", pass, xx);
  519. }
  520. if (bu == NULL)
  521. bu = u;
  522. else {
  523. if ((bu == userlist) && (lastuser != NULL))
  524. x = lastuser;
  525. else
  526. x = bu;
  527. while (x->next != NULL)
  528. x = x->next;
  529. x->next = u;
  530. if (bu == userlist)
  531. lastuser = u;
  532. }
  533. return bu;
  534. }
  535. static void freeuser(struct userrec *u)
  536. {
  537. if (u == NULL)
  538. return;
  539. struct user_entry *ue = NULL, *ut = NULL;
  540. struct chanuserrec *ch = u->chanrec, *z = NULL;
  541. while (ch) {
  542. z = ch;
  543. ch = ch->next;
  544. if (z->info != NULL)
  545. free(z->info);
  546. free(z);
  547. }
  548. u->chanrec = NULL;
  549. for (ue = u->entries; ue; ue = ut) {
  550. ut = ue->next;
  551. if (ue->name) {
  552. struct list_type *lt, *ltt;
  553. for (lt = ue->u.list; lt; lt = ltt) {
  554. ltt = lt->next;
  555. free(lt->extra);
  556. free(lt);
  557. }
  558. free(ue->name);
  559. free(ue);
  560. } else {
  561. ue->type->kill(ue);
  562. }
  563. }
  564. free(u);
  565. }
  566. int deluser(char *handle)
  567. {
  568. struct userrec *u = userlist, *prev = NULL;
  569. int fnd = 0;
  570. while ((u != NULL) && (!fnd)) {
  571. if (!egg_strcasecmp(u->handle, handle))
  572. fnd = 1;
  573. else {
  574. prev = u;
  575. u = u->next;
  576. }
  577. }
  578. if (!fnd)
  579. return 0;
  580. if (prev == NULL)
  581. userlist = u->next;
  582. else
  583. prev->next = u->next;
  584. if (!noshare && (handle[0] != '*'))
  585. shareout("k %s\n", handle);
  586. for (fnd = 0; fnd < dcc_total; fnd++) {
  587. if (dcc[fnd].type && dcc[fnd].user == u && dcc[fnd].simul == -1) {
  588. if (u->bot) {
  589. int i = nextbot(handle);
  590. if (i != -1 && !egg_strcasecmp(dcc[i].nick, handle))
  591. botunlink(-1, handle, "Bot removed.");
  592. else { /* This will probably never be called -- but just in case */
  593. /* Kill link in attempt/progress */
  594. if (dcc[fnd].sock != -1)
  595. killsock(dcc[fnd].sock);
  596. lostdcc(fnd);
  597. }
  598. } else if (!u->bot) {
  599. dprintf(fnd, "-+- POOF! -+-\n");
  600. dprintf(fnd, "You are no longer a user on this botnet.\n");
  601. do_boot(fnd, conf.bot->nick, "User removed.");
  602. }
  603. }
  604. }
  605. clear_chanlist();
  606. freeuser(u);
  607. lastuser = NULL;
  608. return 1;
  609. }
  610. int delhost_by_handle(char *handle, char *host)
  611. {
  612. struct userrec *u = get_user_by_handle(userlist, handle);
  613. if (!u)
  614. return 0;
  615. struct list_type *q = (struct list_type *) get_user(&USERENTRY_HOSTS, u), *qnext = NULL, *qprev = q;
  616. struct user_entry *e = NULL;
  617. int i = 0;
  618. if (q) {
  619. if (!rfc_casecmp(q->extra, host)) {
  620. e = find_user_entry(&USERENTRY_HOSTS, u);
  621. e->u.extra = q->next;
  622. free(q->extra);
  623. free(q);
  624. i++;
  625. qprev = NULL;
  626. q = (struct list_type *) e->u.extra;
  627. } else
  628. q = q->next;
  629. while (q) {
  630. qnext = q->next;
  631. if (!rfc_casecmp(q->extra, host)) {
  632. if (qprev)
  633. qprev->next = q->next;
  634. else if (e) {
  635. e->u.extra = q->next;
  636. qprev = NULL;
  637. }
  638. free(q->extra);
  639. free(q);
  640. i++;
  641. } else
  642. qprev = q;
  643. q = qnext;
  644. }
  645. }
  646. if (!qprev)
  647. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  648. if (!noshare && i)
  649. shareout("-h %s %s\n", handle, host);
  650. clear_chanlist();
  651. return i;
  652. }
  653. void addhost_by_handle(char *handle, char *host)
  654. {
  655. struct userrec *u = get_user_by_handle(userlist, handle);
  656. set_user(&USERENTRY_HOSTS, u, host);
  657. /* u will be cached, so really no overhead, even tho this looks dumb: */
  658. if (!noshare) {
  659. if (u->bot)
  660. shareout("+bh %s %s\n", handle, host);
  661. else
  662. shareout("+h %s %s\n", handle, host);
  663. }
  664. clear_chanlist();
  665. }
  666. void touch_laston(struct userrec *u, char *where, time_t timeval)
  667. {
  668. if (!u)
  669. return;
  670. if (timeval > 1) {
  671. struct laston_info *li = (struct laston_info *) get_user(&USERENTRY_LASTON, u);
  672. if (!li)
  673. li = (struct laston_info *) my_calloc(1, sizeof(struct laston_info));
  674. else if (li->lastonplace)
  675. free(li->lastonplace);
  676. li->laston = timeval;
  677. if (where) {
  678. li->lastonplace = strdup(where);
  679. } else
  680. li->lastonplace = NULL;
  681. set_user(&USERENTRY_LASTON, u, li);
  682. } else if (timeval == 1)
  683. set_user(&USERENTRY_LASTON, u, 0);
  684. }
  685. void user_del_chan(char *dname)
  686. {
  687. struct chanuserrec *ch = NULL, *och = NULL;
  688. for (struct userrec *u = userlist; u; u = u->next) {
  689. ch = u->chanrec;
  690. och = NULL;
  691. while (ch) {
  692. if (!rfc_casecmp(dname, ch->channel)) {
  693. if (och)
  694. och->next = ch->next;
  695. else
  696. u->chanrec = ch->next;
  697. if (ch->info)
  698. free(ch->info);
  699. free(ch);
  700. break;
  701. }
  702. och = ch;
  703. ch = ch->next;
  704. }
  705. }
  706. }
  707. bool clearhosts(struct userrec *u) {
  708. if (get_user(&USERENTRY_HOSTS, u)) {
  709. while (struct list_type *q = (struct list_type *) get_user(&USERENTRY_HOSTS, u)) {
  710. char *host = strdup(q->extra);
  711. delhost_by_handle(u->handle, host);
  712. check_this_user(u->handle, 1, host);
  713. free(host);
  714. }
  715. return 1;
  716. }
  717. return 0;
  718. }