userrec.cc 20 KB

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