userrec.cc 20 KB

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