userrec.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 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. #include "EncryptedStream.h"
  50. #include <bdlib/src/String.h>
  51. bool noshare = 1; /* don't send out to sharebots */
  52. struct userrec *userlist = NULL; /* user records are stored here */
  53. struct userrec *lastuser = NULL; /* last accessed user record */
  54. maskrec *global_bans = NULL,
  55. *global_exempts = NULL,
  56. *global_invites = NULL;
  57. struct igrec *global_ign = NULL;
  58. int cache_hit = 0,
  59. cache_miss = 0; /* temporary cache accounting */
  60. int userfile_perm = 0600; /* Userfile permissions,
  61. default rw------- */
  62. static bool sort_users = 1; /* sort the userlist when saving */
  63. int count_users(struct userrec *bu)
  64. {
  65. int tot = 0;
  66. for (struct userrec *u = bu; u; u = u->next)
  67. tot++;
  68. return tot;
  69. }
  70. static struct userrec *check_dcclist_hand(char *handle)
  71. {
  72. for (int i = 0; i < dcc_total; i++)
  73. if (dcc[i].type && !strcasecmp(dcc[i].nick, handle))
  74. return dcc[i].user;
  75. return NULL;
  76. }
  77. struct userrec *host_conflicts(char *host)
  78. {
  79. struct userrec *u = NULL;
  80. struct list_type *q = NULL;
  81. for (u = userlist; u; u = u->next) {
  82. for (q = (struct list_type *) get_user(&USERENTRY_HOSTS, u); q; q = q->next) {
  83. // Ignore -telnet!*@* hosts for this check, as this is for irc.
  84. if (!strncmp(q->extra, "-telnet!", 8))
  85. continue;
  86. if (wild_match(host, q->extra) || wild_match(q->extra, host))
  87. return u;
  88. }
  89. }
  90. return NULL;
  91. }
  92. struct userrec *get_user_by_handle(struct userrec *bu, char *handle)
  93. {
  94. if (!handle)
  95. return NULL;
  96. /* FIXME: This should be done outside of this function. */
  97. rmspace(handle);
  98. if (!handle[0] || (handle[0] == '*'))
  99. return NULL;
  100. struct userrec *ret = NULL;
  101. if (bu == userlist) {
  102. if (lastuser && !strcasecmp(lastuser->handle, handle)) {
  103. cache_hit++;
  104. return lastuser;
  105. }
  106. ret = check_dcclist_hand(handle);
  107. if (ret) {
  108. cache_hit++;
  109. return ret;
  110. }
  111. ret = check_chanlist_hand(handle);
  112. if (ret) {
  113. cache_hit++;
  114. return ret;
  115. }
  116. cache_miss++;
  117. }
  118. for (struct userrec *u = bu; u; u = u->next)
  119. if (!strcasecmp(u->handle, handle)) {
  120. if (bu == userlist)
  121. lastuser = u;
  122. return u;
  123. }
  124. return NULL;
  125. }
  126. /* Fix capitalization, etc
  127. */
  128. void correct_handle(char *handle)
  129. {
  130. struct userrec *u = get_user_by_handle(userlist, handle);
  131. if (u == NULL || handle == u->handle)
  132. return;
  133. strlcpy(handle, u->handle, HANDLEN + 1);
  134. }
  135. /* This will be usefull in a lot of places, much more code re-use so we
  136. * endup with a smaller executable bot. <cybah>
  137. */
  138. void clear_masks(maskrec *m)
  139. {
  140. maskrec *temp = NULL;
  141. for (; m; m = temp) {
  142. temp = m->next;
  143. if (m->mask)
  144. free(m->mask);
  145. if (m->user)
  146. free(m->user);
  147. if (m->desc)
  148. free(m->desc);
  149. free(m);
  150. }
  151. }
  152. static void freeuser(struct userrec *);
  153. void clear_userlist(struct userrec *bu)
  154. {
  155. struct userrec *v = NULL;
  156. for (struct userrec *u = bu; u; u = v) {
  157. v = u->next;
  158. freeuser(u);
  159. }
  160. if (userlist == bu) {
  161. struct chanset_t *cst = NULL;
  162. for (int i = 0; i < dcc_total; i++)
  163. if (dcc[i].type)
  164. dcc[i].user = NULL;
  165. conf.bot->u = NULL;
  166. clear_chanlist();
  167. lastuser = NULL;
  168. while (global_ign)
  169. delignore(global_ign->igmask);
  170. clear_masks(global_bans);
  171. clear_masks(global_exempts);
  172. clear_masks(global_invites);
  173. global_exempts = global_invites = global_bans = NULL;
  174. for (cst = chanset; cst; cst = cst->next) {
  175. clear_masks(cst->bans);
  176. clear_masks(cst->exempts);
  177. clear_masks(cst->invites);
  178. cst->bans = cst->exempts = cst->invites = NULL;
  179. }
  180. }
  181. /* Remember to set your userlist to NULL after calling this */
  182. }
  183. /* Find CLOSEST host match
  184. * (if "*!*@*" and "*!*@*clemson.edu" both match, use the latter!)
  185. *
  186. * Checks the chanlist first, to possibly avoid needless search.
  187. */
  188. struct userrec *get_user_by_host(char *host)
  189. {
  190. if (host == NULL)
  191. return NULL;
  192. rmspace(host);
  193. if (!host[0])
  194. return NULL;
  195. struct userrec *ret = check_chanlist(host);
  196. if (ret != NULL) {
  197. cache_hit++;
  198. return ret;
  199. }
  200. struct userrec *u = NULL;
  201. struct list_type *q = NULL;
  202. int cnt = 0, i;
  203. char host2[UHOSTLEN] = "", *p = NULL;
  204. bool do_cidr = 0;
  205. cache_miss++;
  206. strlcpy(host2, host, sizeof host2);
  207. /* do CIDR matching if given host is an ip */
  208. if ((p = strchr(host2, '@')))
  209. if (is_dotted_ip(++p))
  210. do_cidr = 1;
  211. for (u = userlist; u; u = u->next) {
  212. q = (struct list_type *) get_user(&USERENTRY_HOSTS, u);
  213. for (; q; q = q->next) {
  214. if (do_cidr) {
  215. i = match_cidr(q->extra, host);
  216. if (i > cnt) {
  217. ret = u;
  218. cnt = i;
  219. }
  220. }
  221. i = wild_match(q->extra, host);
  222. if (i > cnt) {
  223. ret = u;
  224. cnt = i;
  225. }
  226. }
  227. }
  228. if (ret != NULL) {
  229. lastuser = ret;
  230. set_chanlist(host2, ret);
  231. }
  232. return ret;
  233. }
  234. bool user_has_host(const char *handle, struct userrec *u, char *host)
  235. {
  236. if (host == NULL)
  237. return 0;
  238. rmspace(host);
  239. if (!host[0])
  240. return 0;
  241. if (!u && handle)
  242. u = get_user_by_handle(userlist, (char *) handle);
  243. if (!u)
  244. return 0;
  245. struct list_type *q = NULL;
  246. for (q = (struct list_type *) get_user(&USERENTRY_HOSTS, u); q; q = q->next)
  247. if (!strcasecmp(q->extra, host))
  248. return 1;
  249. return 0;
  250. }
  251. bool user_has_matching_host(const char *handle, struct userrec *u, char *host)
  252. {
  253. if (host == NULL) {
  254. return false;
  255. }
  256. rmspace(host);
  257. if (!host[0]) {
  258. return false;
  259. }
  260. if (!u && handle) {
  261. u = get_user_by_handle(userlist, (char *) handle);
  262. }
  263. if (!u) {
  264. return false;
  265. }
  266. /* do CIDR matching if given host is an ip */
  267. char *p = NULL;
  268. bool do_cidr = 0;
  269. struct list_type *q = NULL;
  270. do_cidr = ((p = strchr(host, '@')) && is_dotted_ip(++p));
  271. for (q = (struct list_type *) get_user(&USERENTRY_HOSTS, u); q; q = q->next) {
  272. if (do_cidr && match_cidr(q->extra, host)) {
  273. return true;
  274. }
  275. if (wild_match(q->extra, host)) {
  276. return true;
  277. }
  278. }
  279. return false;
  280. }
  281. void convert_password(struct userrec *u)
  282. {
  283. char *oldpass = (char *) get_user(&USERENTRY_PASS1, u);
  284. if (oldpass && oldpass[0]) {
  285. char *pass = NULL, *passp = NULL;
  286. /* need to convert into new password format and remove old */
  287. /* --------- this changes to reflect how to decrypt old password --------- */
  288. passp = pass = decrypt_string(u->handle, &oldpass[1]);
  289. /* Advance pass over the salt */
  290. pass += 17;
  291. /* ----------------------------------------------------------------------- */
  292. set_user(&USERENTRY_PASS, u, pass);
  293. OPENSSL_cleanse(passp, strlen(passp));
  294. free(passp);
  295. /* clear old record */
  296. noshare = 1;
  297. set_user(&USERENTRY_PASS1, u, NULL);
  298. noshare = 0;
  299. }
  300. }
  301. /* Try: pass_match_by_host("-",host)
  302. * will return 1 if no password is set for that host
  303. */
  304. int u_pass_match(struct userrec *u, const char *in)
  305. {
  306. if (!u)
  307. return 0;
  308. convert_password(u);
  309. const char *cmp = (const char *) get_user(&USERENTRY_PASS, u);
  310. if (!cmp && (!in[0] || (in[0] == '-')))
  311. return 1;
  312. if (!cmp || !in[0] || (in[0] == '-'))
  313. return 0;
  314. if (u->bot) {
  315. if (!strcmp(cmp, in))
  316. return 1;
  317. } else {
  318. char *pass = strdup(in), *pass_p = pass;
  319. /* Pass the salted pass in so the same salt can be used */
  320. int n = salted_sha1cmp(cmp, pass);
  321. OPENSSL_cleanse(pass, sizeof(pass));
  322. free(pass_p);
  323. if (!n)
  324. return 1;
  325. }
  326. return 0;
  327. }
  328. static void write_user(const struct userrec *u, bd::Stream& stream, int idx)
  329. {
  330. char s[181] = "";
  331. struct flag_record fr = {FR_GLOBAL, u->flags, 0, 0 };
  332. build_flags(s, &fr, NULL);
  333. stream << bd::String::printf("%s%s - %s\n", u->bot ? "-" : "", u->handle, s);
  334. struct chanset_t *cst = NULL;
  335. for (struct chanuserrec *ch = u->chanrec; ch; ch = ch->next) {
  336. cst = findchan_by_dname(ch->channel);
  337. if (cst) {
  338. if (idx >= 0) {
  339. fr.match = FR_CHAN;
  340. get_user_flagrec(dcc[idx].user, &fr, ch->channel);
  341. }
  342. fr.match = FR_CHAN;
  343. fr.chan = ch->flags;
  344. build_flags(s, &fr, NULL);
  345. stream << bd::String::printf("! %s %li %s %s\n", ch->channel, (long) ch->laston, s, ch->info ? ch->info : "");
  346. }
  347. }
  348. for (struct user_entry *ue = u->entries; ue; ue = ue->next) {
  349. #ifdef no
  350. if (ue->name) {
  351. struct list_type *lt = NULL;
  352. for (lt = ue->u.list; lt; lt = lt->next)
  353. if (lfprintf(f, "--%s %s\n", ue->name, lt->extra) == EOF)
  354. return 0;
  355. } else
  356. #endif
  357. if (ue->type)
  358. if (conf.bot->hub || conf.bot->localhub)
  359. ue->type->write_userfile(stream, u, ue, idx);
  360. }
  361. }
  362. static int sort_compare(struct userrec *a, struct userrec *b)
  363. {
  364. /* Order by flags, then alphabetically
  365. * first bots: +h / +a / +l / other bots
  366. * then users: +n / +m / +o / other users
  367. * return true if (a > b)
  368. */
  369. if (a->bot && b->bot) {
  370. if (bot_hublevel(a) > bot_hublevel(b))
  371. return 1;
  372. else if (bot_hublevel(a) < bot_hublevel(b))
  373. return 0;
  374. else if (bot_hublevel(a) == bot_hublevel(b) && bot_hublevel(a) == 999)
  375. return 0;
  376. } else {
  377. if (a->bot && !b->bot)
  378. return 1;
  379. if (!a->bot && b->bot)
  380. return 0;
  381. if (~a->flags & b->flags & USER_ADMIN)
  382. return 1;
  383. if (a->flags & ~b->flags & USER_ADMIN)
  384. return 0;
  385. if (~a->flags & b->flags & USER_OWNER)
  386. return 1;
  387. if (a->flags & ~b->flags & USER_OWNER)
  388. return 0;
  389. if (~a->flags & b->flags & USER_MASTER)
  390. return 1;
  391. if (a->flags & ~b->flags & USER_MASTER)
  392. return 0;
  393. if (~a->flags & b->flags & USER_OP)
  394. return 1;
  395. if (a->flags & ~b->flags & USER_OP)
  396. return 0;
  397. }
  398. return (strcasecmp(a->handle, b->handle) > 0);
  399. }
  400. static void sort_userlist()
  401. {
  402. int again = 1;
  403. struct userrec *last = NULL, *p = NULL, *c = NULL, *n = NULL;
  404. while ((userlist != last) && (again)) {
  405. p = NULL;
  406. c = userlist;
  407. n = c->next;
  408. again = 0;
  409. while (n != last) {
  410. if (sort_compare(c, n)) {
  411. again = 1;
  412. c->next = n->next;
  413. n->next = c;
  414. if (p == NULL)
  415. userlist = n;
  416. else
  417. p->next = n;
  418. }
  419. p = c;
  420. c = n;
  421. n = n->next;
  422. }
  423. last = c;
  424. }
  425. }
  426. void stream_writeuserfile(bd::Stream& stream, const struct userrec *bu, bool old) {
  427. time_t tt = now;
  428. char s1[81] = "";
  429. strcpy(s1, ctime(&tt));
  430. stream << bd::String::printf("#4v: %s -- %s -- written %s", ver, conf.bot->nick, s1);
  431. channels_writeuserfile(stream, old);
  432. for (const struct userrec *u = bu; u; u = u->next)
  433. write_user(u, stream, -1);
  434. }
  435. /* Rewrite the entire user file. Call USERFILE hook as well, probably
  436. * causing the channel file to be rewritten as well.
  437. */
  438. int write_userfile(int idx)
  439. {
  440. if (userlist == NULL || !conf.bot->hub)
  441. return 1; /* No point in saving userfile */
  442. if (idx >= 0)
  443. dprintf(idx, "Saving userfile...\n");
  444. if (sort_users)
  445. sort_userlist();
  446. putlog(LOG_DEBUG, "@", "Writing user entries.");
  447. Tempfile *new_userfile = new Tempfile("userfile");
  448. const char salt1[] = SALT1;
  449. EncryptedStream stream(salt1);
  450. stream_writeuserfile(stream, userlist);
  451. if (stream.writeFile(new_userfile->fd)) {
  452. putlog(LOG_MISC, "*", "ERROR writing user file. (%s)", strerror(errno));
  453. delete new_userfile;
  454. return 3;
  455. }
  456. new_userfile->my_close();
  457. putlog(LOG_DEBUG, "@", "Done writing userfile.");
  458. char backup[DIRMAX] = "";
  459. simple_snprintf(backup, sizeof backup, "%s/%s~", conf.datadir, userfile);
  460. copyfile(userfile, backup);
  461. movefile(new_userfile->file, userfile);
  462. delete new_userfile;
  463. return 0;
  464. }
  465. int change_handle(struct userrec *u, char *newh)
  466. {
  467. if (!u)
  468. return 0;
  469. /* Nothing that will confuse the userfile */
  470. if (!newh[1] && strchr(BADHANDCHARS, newh[0]))
  471. return 0;
  472. /* Yes, even send bot nick changes now: */
  473. if (!noshare)
  474. shareout("h %s %s\n", u->handle, newh);
  475. char s[HANDLEN + 1] = "";
  476. strlcpy(s, u->handle, sizeof s);
  477. strlcpy(u->handle, newh, sizeof u->handle);
  478. for (int i = 0; i < dcc_total; i++)
  479. if (dcc[i].type && dcc[i].type != &DCC_BOT && !strcasecmp(dcc[i].nick, s)) {
  480. strlcpy(dcc[i].nick, newh, sizeof dcc[i].nick);
  481. if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
  482. chanout_but(-1, dcc[i].u.chat->channel, "*** Handle change: %s -> %s\n", s, newh);
  483. if (dcc[i].u.chat->channel < GLOBAL_CHANS)
  484. botnet_send_nkch(i, s);
  485. }
  486. break;
  487. }
  488. return 1;
  489. }
  490. struct userrec *adduser(struct userrec *bu, char *handle, char *host, char *pass, flag_t flags, int bot)
  491. {
  492. struct userrec *u = NULL, *x = NULL;
  493. int oldshare = noshare;
  494. noshare = 1;
  495. u = (struct userrec *) my_calloc(1, sizeof(struct userrec));
  496. u->bot = bot;
  497. /* u->next=bu; bu=u; */
  498. strlcpy(u->handle, handle, sizeof u->handle);
  499. u->next = NULL;
  500. u->chanrec = NULL;
  501. u->entries = NULL;
  502. if (flags != USER_DEFAULT) { /* drummer */
  503. u->flags = flags;
  504. } else {
  505. u->flags = default_flags;
  506. }
  507. set_user(&USERENTRY_PASS, u, pass);
  508. /* Strip out commas -- they're illegal */
  509. if (host && host[0]) {
  510. char *p = strchr(host, ',');
  511. while (p != NULL) {
  512. *p = '?';
  513. p = strchr(host, ',');
  514. }
  515. set_user(&USERENTRY_HOSTS, u, host);
  516. } else
  517. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  518. if (bu == userlist)
  519. clear_chanlist();
  520. noshare = oldshare;
  521. if ((!noshare) && (handle[0] != '*') && (bu == userlist)) {
  522. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  523. char xx[100] = "";
  524. fr.global = u->flags;
  525. build_flags(xx, &fr, 0);
  526. shareout("n %s%s %s %s %s\n", bot ? "-" : "", u->handle, host && host[0] ? host : "none", pass, xx);
  527. }
  528. if (bu == NULL)
  529. bu = u;
  530. else {
  531. if ((bu == userlist) && (lastuser != NULL))
  532. x = lastuser;
  533. else
  534. x = bu;
  535. while (x->next != NULL)
  536. x = x->next;
  537. x->next = u;
  538. if (bu == userlist)
  539. lastuser = u;
  540. }
  541. return bu;
  542. }
  543. static void freeuser(struct userrec *u)
  544. {
  545. if (u == NULL)
  546. return;
  547. struct user_entry *ue = NULL, *ut = NULL;
  548. struct chanuserrec *ch = u->chanrec, *z = NULL;
  549. while (ch) {
  550. z = ch;
  551. ch = ch->next;
  552. if (z->info != NULL)
  553. free(z->info);
  554. free(z);
  555. }
  556. u->chanrec = NULL;
  557. for (ue = u->entries; ue; ue = ut) {
  558. ut = ue->next;
  559. if (ue->name) {
  560. struct list_type *lt, *ltt;
  561. for (lt = ue->u.list; lt; lt = ltt) {
  562. ltt = lt->next;
  563. free(lt->extra);
  564. free(lt);
  565. }
  566. free(ue->name);
  567. free(ue);
  568. } else {
  569. ue->type->kill(ue);
  570. }
  571. }
  572. free(u);
  573. }
  574. int deluser(char *handle)
  575. {
  576. struct userrec *u = userlist, *prev = NULL;
  577. int fnd = 0;
  578. while ((u != NULL) && (!fnd)) {
  579. if (!strcasecmp(u->handle, handle))
  580. fnd = 1;
  581. else {
  582. prev = u;
  583. u = u->next;
  584. }
  585. }
  586. if (!fnd)
  587. return 0;
  588. if (prev == NULL)
  589. userlist = u->next;
  590. else
  591. prev->next = u->next;
  592. if (!noshare && (handle[0] != '*'))
  593. shareout("k %s\n", handle);
  594. for (fnd = 0; fnd < dcc_total; fnd++) {
  595. if (dcc[fnd].type && dcc[fnd].user == u && dcc[fnd].simul == -1) {
  596. if (u->bot) {
  597. int i = nextbot(handle);
  598. if (i != -1 && !strcasecmp(dcc[i].nick, handle))
  599. botunlink(-1, handle, "Bot removed.");
  600. else { /* This will probably never be called -- but just in case */
  601. /* Kill link in attempt/progress */
  602. if (dcc[fnd].sock != -1)
  603. killsock(dcc[fnd].sock);
  604. lostdcc(fnd);
  605. }
  606. } else if (!u->bot) {
  607. dprintf(fnd, "-+- POOF! -+-\n");
  608. dprintf(fnd, "You are no longer a user on this botnet.\n");
  609. do_boot(fnd, conf.bot->nick, "User removed.");
  610. }
  611. }
  612. }
  613. clear_chanlist();
  614. freeuser(u);
  615. lastuser = NULL;
  616. return 1;
  617. }
  618. int delhost_by_handle(char *handle, char *host)
  619. {
  620. struct userrec *u = get_user_by_handle(userlist, handle);
  621. if (!u)
  622. return 0;
  623. struct list_type *q = (struct list_type *) get_user(&USERENTRY_HOSTS, u), *qnext = NULL, *qprev = q;
  624. struct user_entry *e = NULL;
  625. int i = 0;
  626. if (q) {
  627. if (!rfc_casecmp(q->extra, host)) {
  628. e = find_user_entry(&USERENTRY_HOSTS, u);
  629. e->u.extra = q->next;
  630. free(q->extra);
  631. free(q);
  632. i++;
  633. qprev = NULL;
  634. q = (struct list_type *) e->u.extra;
  635. } else
  636. q = q->next;
  637. while (q) {
  638. qnext = q->next;
  639. if (!rfc_casecmp(q->extra, host)) {
  640. if (qprev)
  641. qprev->next = q->next;
  642. else if (e) {
  643. e->u.extra = q->next;
  644. qprev = NULL;
  645. }
  646. free(q->extra);
  647. free(q);
  648. i++;
  649. } else
  650. qprev = q;
  651. q = qnext;
  652. }
  653. }
  654. if (!qprev)
  655. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  656. if (!noshare && i)
  657. shareout("-h %s %s\n", handle, host);
  658. clear_chanlist();
  659. return i;
  660. }
  661. void addhost_by_handle(char *handle, char *host)
  662. {
  663. struct userrec *u = get_user_by_handle(userlist, handle);
  664. set_user(&USERENTRY_HOSTS, u, host);
  665. /* u will be cached, so really no overhead, even tho this looks dumb: */
  666. if (!noshare) {
  667. if (u->bot)
  668. shareout("+bh %s %s\n", handle, host);
  669. else
  670. shareout("+h %s %s\n", handle, host);
  671. }
  672. clear_chanlist();
  673. }
  674. void touch_laston(struct userrec *u, char *where, time_t timeval)
  675. {
  676. if (!u)
  677. return;
  678. if (timeval > 1) {
  679. struct laston_info *li = (struct laston_info *) get_user(&USERENTRY_LASTON, u);
  680. if (!li)
  681. li = (struct laston_info *) my_calloc(1, sizeof(struct laston_info));
  682. else if (li->lastonplace)
  683. free(li->lastonplace);
  684. li->laston = timeval;
  685. if (where) {
  686. li->lastonplace = strdup(where);
  687. } else
  688. li->lastonplace = NULL;
  689. set_user(&USERENTRY_LASTON, u, li);
  690. } else if (timeval == 1)
  691. set_user(&USERENTRY_LASTON, u, 0);
  692. }
  693. void user_del_chan(char *dname)
  694. {
  695. struct chanuserrec *ch = NULL, *och = NULL;
  696. for (struct userrec *u = userlist; u; u = u->next) {
  697. ch = u->chanrec;
  698. och = NULL;
  699. while (ch) {
  700. if (!rfc_casecmp(dname, ch->channel)) {
  701. if (och)
  702. och->next = ch->next;
  703. else
  704. u->chanrec = ch->next;
  705. if (ch->info)
  706. free(ch->info);
  707. free(ch);
  708. break;
  709. }
  710. och = ch;
  711. ch = ch->next;
  712. }
  713. }
  714. }