1
0

userrec.cc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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 "auth.h"
  30. #include "userrec.h"
  31. #include "misc.h"
  32. #include "misc_file.h"
  33. #include "rfc1459.h"
  34. #include "dcc.h"
  35. #include "botnet.h"
  36. #include "src/mod/share.mod/share.h"
  37. #include "src/mod/channels.mod/channels.h"
  38. #include "src/mod/irc.mod/irc.h"
  39. #include "main.h"
  40. #include "users.h"
  41. #include "chan.h"
  42. #include "match.h"
  43. #include "dccutil.h"
  44. #include "tandem.h"
  45. #include "chanprog.h"
  46. #include "crypt.h"
  47. #include "core_binds.h"
  48. #include "socket.h"
  49. #include "net.h"
  50. #include "EncryptedStream.h"
  51. #include <bdlib/src/AtomicFile.h>
  52. #include <bdlib/src/String.h>
  53. int noshare = 1; /* don't send out to sharebots */
  54. struct userrec *userlist = NULL; /* user records are stored here */
  55. struct userrec *lastuser = NULL; /* last accessed user record */
  56. maskrec *global_bans = NULL,
  57. *global_exempts = NULL,
  58. *global_invites = NULL;
  59. struct igrec *global_ign = NULL;
  60. int cache_hit = 0,
  61. cache_miss = 0; /* temporary cache accounting */
  62. int userfile_perm = 0600; /* Userfile permissions,
  63. default rw------- */
  64. static bool sort_users = 1; /* sort the userlist when saving */
  65. int
  66. count_users(const struct userrec *bu)
  67. {
  68. int tot = 0;
  69. for (const struct userrec *u = bu; u; u = u->next)
  70. tot++;
  71. return tot;
  72. }
  73. static struct userrec *
  74. __attribute__((pure))
  75. check_dcclist_hand(const char *handle)
  76. {
  77. for (int i = 0; i < dcc_total; i++)
  78. if (dcc[i].type && !strcasecmp(dcc[i].nick, handle))
  79. return dcc[i].user;
  80. return NULL;
  81. }
  82. struct userrec *host_conflicts(const char *host)
  83. {
  84. struct userrec *u = NULL;
  85. struct list_type *q = NULL;
  86. for (u = userlist; u; u = u->next) {
  87. for (q = (struct list_type *) get_user(&USERENTRY_HOSTS, u); q; q = q->next) {
  88. // Ignore -telnet!*@* hosts for this check, as this is for irc.
  89. if (!strncmp(q->extra, "-telnet!", 8))
  90. continue;
  91. if (wild_match(host, q->extra) || wild_match(q->extra, host))
  92. return u;
  93. }
  94. }
  95. return NULL;
  96. }
  97. struct userrec *get_user_by_handle(struct userrec *bu, const char *handle)
  98. {
  99. if (!handle)
  100. return NULL;
  101. if (!handle[0] || (handle[0] == '*'))
  102. return NULL;
  103. struct userrec *ret = NULL;
  104. if (bu == userlist) {
  105. if (lastuser && !strcasecmp(lastuser->handle, handle)) {
  106. cache_hit++;
  107. return lastuser;
  108. }
  109. ret = check_dcclist_hand(handle);
  110. if (ret) {
  111. cache_hit++;
  112. return ret;
  113. }
  114. ret = check_chanlist_hand(handle);
  115. if (ret) {
  116. cache_hit++;
  117. return ret;
  118. }
  119. cache_miss++;
  120. }
  121. for (struct userrec *u = bu; u; u = u->next)
  122. if (!strcasecmp(u->handle, handle)) {
  123. if (bu == userlist)
  124. lastuser = u;
  125. return u;
  126. }
  127. return NULL;
  128. }
  129. /* Fix capitalization, etc
  130. */
  131. void correct_handle(char *handle)
  132. {
  133. struct userrec *u = get_user_by_handle(userlist, handle);
  134. if (u == NULL || handle == u->handle)
  135. return;
  136. strlcpy(handle, u->handle, HANDLEN + 1);
  137. }
  138. /* This will be usefull in a lot of places, much more code re-use so we
  139. * endup with a smaller executable bot. <cybah>
  140. */
  141. void clear_masks(maskrec *m)
  142. {
  143. maskrec *temp = NULL;
  144. for (; m; m = temp) {
  145. temp = m->next;
  146. if (m->mask)
  147. free(m->mask);
  148. if (m->user)
  149. free(m->user);
  150. if (m->desc)
  151. free(m->desc);
  152. free(m);
  153. }
  154. }
  155. static void freeuser(struct userrec *);
  156. void clear_cached_users()
  157. {
  158. for (int i = 0; i < dcc_total; i++) {
  159. if (dcc[i].type) {
  160. dcc[i].user = NULL;
  161. }
  162. }
  163. conf.bot->u = NULL;
  164. for (conf_bot *bot = conf.bots; bot; bot = bot->next) {
  165. bot->u = NULL;
  166. }
  167. for (tand_t* bot = tandbot; bot; bot = bot->next) {
  168. bot->u = NULL;
  169. }
  170. if (!conf.bot->hub) {
  171. clear_chanlist(); /* Remove all user references from the
  172. * channel lists. */
  173. }
  174. }
  175. void cache_users()
  176. {
  177. for (int i = 0; i < dcc_total; i++) {
  178. if (dcc[i].type) {
  179. dcc[i].user = get_user_by_handle(userlist, dcc[i].nick);
  180. }
  181. }
  182. conf.bot->u = get_user_by_handle(userlist, conf.bot->nick);
  183. for (conf_bot *bot = conf.bots; bot; bot = bot->next) {
  184. bot->u = get_user_by_handle(userlist, bot->nick);
  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, int idx, bool old) {
  463. idx = -1;
  464. time_t tt = now;
  465. char s1[81] = "";
  466. strcpy(s1, ctime(&tt));
  467. stream << bd::String::printf("#4v: %s -- %s -- written %s", ver, conf.bot->nick, s1);
  468. channels_writeuserfile(stream, old);
  469. for (const struct userrec *u = bu; u; u = u->next)
  470. write_user(u, stream, idx);
  471. }
  472. /* Rewrite the entire user file. Call USERFILE hook as well, probably
  473. * causing the channel file to be rewritten as well.
  474. */
  475. int real_write_userfile(int idx)
  476. {
  477. if (!userlist) {
  478. return 1;
  479. }
  480. conf_update_hubs(userlist);
  481. if (!conf.bot->hub)
  482. return 1; /* No point in saving userfile */
  483. if (idx >= 0)
  484. dprintf(idx, "Saving userfile...\n");
  485. if (sort_users)
  486. sort_userlist();
  487. putlog(LOG_DEBUG, "@", "Writing user entries.");
  488. bd::AtomicFile *new_userfile = new bd::AtomicFile;
  489. if (!new_userfile->open(userfile)) {
  490. return 3;
  491. }
  492. const char salt1[] = SALT1;
  493. EncryptedStream stream(salt1);
  494. stream_writeuserfile(stream, userlist, -1);
  495. if (stream.writeFile(new_userfile->fd())) {
  496. putlog(LOG_MISC, "*", "ERROR writing user file. (%s)", strerror(errno));
  497. delete new_userfile;
  498. return 3;
  499. }
  500. char backup[DIRMAX] = "";
  501. simple_snprintf(backup, sizeof backup, "%s/%s~", conf.datadir, userfile);
  502. copyfile(userfile, backup);
  503. if (!new_userfile->commit()) {
  504. return 3;
  505. }
  506. putlog(LOG_DEBUG, "@", "Done writing userfile.");
  507. delete new_userfile;
  508. return 0;
  509. }
  510. int write_userfile(int idx) {
  511. if (idx >= 0) {
  512. return real_write_userfile(idx);
  513. }
  514. if (!do_write_userfile) {
  515. do_write_userfile = 3;
  516. } else if (do_write_userfile == 15) {
  517. do_write_userfile = 0;
  518. return real_write_userfile(idx);
  519. } else {
  520. ++do_write_userfile;
  521. }
  522. return 0;
  523. }
  524. int change_handle(struct userrec *u, char *newh)
  525. {
  526. if (!u)
  527. return 0;
  528. /* Nothing that will confuse the userfile */
  529. if (!newh[1] && strchr(BADHANDCHARS, newh[0]))
  530. return 0;
  531. /* Yes, even send bot nick changes now: */
  532. if (!noshare)
  533. shareout("h %s %s\n", u->handle, newh);
  534. char s[HANDLEN + 1] = "";
  535. strlcpy(s, u->handle, sizeof s);
  536. strlcpy(u->handle, newh, sizeof u->handle);
  537. for (int i = 0; i < dcc_total; i++)
  538. if (dcc[i].type && dcc[i].type != &DCC_BOT && !strcasecmp(dcc[i].nick, s)) {
  539. strlcpy(dcc[i].nick, newh, sizeof dcc[i].nick);
  540. if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
  541. chanout_but(-1, dcc[i].u.chat->channel, "*** Handle change: %s -> %s\n", s, newh);
  542. if (dcc[i].u.chat->channel < GLOBAL_CHANS)
  543. botnet_send_nkch(i, s);
  544. }
  545. break;
  546. }
  547. return 1;
  548. }
  549. struct userrec *adduser(struct userrec *bu, const char *handle, char *host, char *pass, flag_t flags, int bot)
  550. {
  551. struct userrec *u = NULL, *x = NULL;
  552. int oldshare = noshare;
  553. noshare = 1;
  554. u = (struct userrec *) calloc(1, sizeof(struct userrec));
  555. u->bot = bot;
  556. /* u->next=bu; bu=u; */
  557. strlcpy(u->handle, handle, sizeof u->handle);
  558. u->next = NULL;
  559. u->chanrec = NULL;
  560. u->entries = NULL;
  561. if (flags != USER_DEFAULT) { /* drummer */
  562. u->flags = flags;
  563. } else {
  564. u->flags = default_flags;
  565. }
  566. set_user(&USERENTRY_PASS, u, pass);
  567. /* Strip out commas -- they're illegal */
  568. if (host && host[0]) {
  569. char *p = strchr(host, ',');
  570. while (p != NULL) {
  571. *p = '?';
  572. p = strchr(host, ',');
  573. }
  574. set_user(&USERENTRY_HOSTS, u, host);
  575. } else
  576. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  577. if (bu == userlist)
  578. clear_chanlist();
  579. noshare = oldshare;
  580. if ((!noshare) && (handle[0] != '*') && (bu == userlist)) {
  581. struct flag_record fr = {FR_GLOBAL, 0, 0, 0 };
  582. char xx[100] = "";
  583. fr.global = u->flags;
  584. build_flags(xx, &fr, 0);
  585. shareout("n %s%s %s %s %s\n", bot ? "-" : "", u->handle, host && host[0] ? host : "none", pass, xx);
  586. }
  587. if (bu == NULL)
  588. bu = u;
  589. else {
  590. if ((bu == userlist) && (lastuser != NULL))
  591. x = lastuser;
  592. else
  593. x = bu;
  594. while (x->next != NULL)
  595. x = x->next;
  596. x->next = u;
  597. if (bu == userlist)
  598. lastuser = u;
  599. }
  600. return bu;
  601. }
  602. static void freeuser(struct userrec *u)
  603. {
  604. if (u == NULL)
  605. return;
  606. struct user_entry *ue = NULL, *ut = NULL;
  607. struct chanuserrec *ch = u->chanrec, *z = NULL;
  608. while (ch) {
  609. z = ch;
  610. ch = ch->next;
  611. if (z->info != NULL)
  612. free(z->info);
  613. free(z);
  614. }
  615. u->chanrec = NULL;
  616. for (ue = u->entries; ue; ue = ut) {
  617. ut = ue->next;
  618. if (ue->name) {
  619. struct list_type *lt, *ltt;
  620. for (lt = ue->u.list; lt; lt = ltt) {
  621. ltt = lt->next;
  622. free(lt->extra);
  623. free(lt);
  624. }
  625. free(ue->name);
  626. free(ue);
  627. } else {
  628. ue->type->kill(ue);
  629. }
  630. }
  631. free(u);
  632. }
  633. int deluser(char *handle)
  634. {
  635. struct userrec *u = userlist, *prev = NULL;
  636. int fnd = 0;
  637. while ((u != NULL) && (!fnd)) {
  638. if (!strcasecmp(u->handle, handle))
  639. fnd = 1;
  640. else {
  641. prev = u;
  642. u = u->next;
  643. }
  644. }
  645. if (!fnd)
  646. return 0;
  647. if (prev == NULL)
  648. userlist = u->next;
  649. else
  650. prev->next = u->next;
  651. if (!noshare && (handle[0] != '*'))
  652. shareout("k %s\n", handle);
  653. for (fnd = 0; fnd < dcc_total; fnd++) {
  654. if (dcc[fnd].type && dcc[fnd].user == u && dcc[fnd].simul == -1) {
  655. if (u->bot) {
  656. int i = nextbot(handle);
  657. if (i != -1 && !strcasecmp(dcc[i].nick, handle))
  658. botunlink(-1, handle, "Bot removed.");
  659. else { /* This will probably never be called -- but just in case */
  660. /* Kill link in attempt/progress */
  661. if (dcc[fnd].sock != -1)
  662. killsock(dcc[fnd].sock);
  663. lostdcc(fnd);
  664. }
  665. } else if (!u->bot) {
  666. dprintf(fnd, "-+- POOF! -+-\n");
  667. dprintf(fnd, "You are no longer a user on this botnet.\n");
  668. do_boot(fnd, conf.bot->nick, "User removed.");
  669. }
  670. }
  671. }
  672. clear_chanlist();
  673. freeuser(u);
  674. lastuser = NULL;
  675. return 1;
  676. }
  677. int delhost_by_handle(char *handle, char *host)
  678. {
  679. struct userrec *u = get_user_by_handle(userlist, handle);
  680. if (!u)
  681. return 0;
  682. struct list_type *q = (struct list_type *) get_user(&USERENTRY_HOSTS, u), *qnext = NULL, *qprev = q;
  683. struct user_entry *e = NULL;
  684. int i = 0;
  685. if (q) {
  686. if (!rfc_casecmp(q->extra, host)) {
  687. e = find_user_entry(&USERENTRY_HOSTS, u);
  688. e->u.extra = q->next;
  689. free(q->extra);
  690. free(q);
  691. i++;
  692. qprev = NULL;
  693. q = (struct list_type *) e->u.extra;
  694. } else
  695. q = q->next;
  696. while (q) {
  697. qnext = q->next;
  698. if (!rfc_casecmp(q->extra, host)) {
  699. if (qprev)
  700. qprev->next = q->next;
  701. else if (e) {
  702. e->u.extra = q->next;
  703. qprev = NULL;
  704. }
  705. free(q->extra);
  706. free(q);
  707. i++;
  708. } else
  709. qprev = q;
  710. q = qnext;
  711. }
  712. }
  713. if (!qprev)
  714. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  715. if (!noshare && i)
  716. shareout("-h %s %s\n", handle, host);
  717. clear_chanlist();
  718. return i;
  719. }
  720. void addhost_by_handle(char *handle, char *host)
  721. {
  722. struct userrec *u = get_user_by_handle(userlist, handle);
  723. set_user(&USERENTRY_HOSTS, u, host);
  724. /* u will be cached, so really no overhead, even tho this looks dumb: */
  725. if (!noshare) {
  726. if (u->bot)
  727. shareout("+bh %s %s\n", handle, host);
  728. else
  729. shareout("+h %s %s\n", handle, host);
  730. }
  731. clear_chanlist();
  732. }
  733. void touch_laston(struct userrec *u, char *where, time_t timeval)
  734. {
  735. if (!u)
  736. return;
  737. if (timeval > 1) {
  738. struct laston_info *li = (struct laston_info *) get_user(&USERENTRY_LASTON, u);
  739. if (!li)
  740. li = (struct laston_info *) calloc(1, sizeof(struct laston_info));
  741. else if (li->lastonplace)
  742. free(li->lastonplace);
  743. li->laston = timeval;
  744. if (where) {
  745. li->lastonplace = strdup(where);
  746. } else
  747. li->lastonplace = NULL;
  748. set_user(&USERENTRY_LASTON, u, li);
  749. } else if (timeval == 1)
  750. set_user(&USERENTRY_LASTON, u, 0);
  751. }
  752. void user_del_chan(char *dname)
  753. {
  754. struct chanuserrec *ch = NULL, *och = NULL;
  755. for (struct userrec *u = userlist; u; u = u->next) {
  756. ch = u->chanrec;
  757. och = NULL;
  758. while (ch) {
  759. if (!rfc_casecmp(dname, ch->channel)) {
  760. if (och)
  761. och->next = ch->next;
  762. else
  763. u->chanrec = ch->next;
  764. if (ch->info)
  765. free(ch->info);
  766. free(ch);
  767. break;
  768. }
  769. och = ch;
  770. ch = ch->next;
  771. }
  772. }
  773. }
  774. /* vim: set sts=2 sw=2 ts=8 et: */