userrec.c 19 KB

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