userrec.cc 19 KB

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