userrec.c 19 KB

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