userrec.c 19 KB

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