userrec.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. /*
  2. * userrec.c -- handles:
  3. * add_q() del_q() str2flags() flags2str() str2chflags() chflags2str()
  4. * a bunch of functions to find and change user records
  5. * change and check user (and channel-specific) flags
  6. *
  7. */
  8. #include <sys/stat.h>
  9. #include "main.h"
  10. #include "users.h"
  11. #include "chan.h"
  12. #include "modules.h"
  13. #include "tandem.h"
  14. extern struct dcc_t *dcc;
  15. extern struct chanset_t *chanset;
  16. extern int default_flags, default_uflags, quiet_save,
  17. dcc_total;
  18. extern char userfile[], ver[], botnetnick[], tempdir[];
  19. extern time_t now;
  20. int noshare = 1; /* don't send out to sharebots */
  21. int sort_users = 1; /* sort the userlist when saving */
  22. struct userrec *userlist = NULL; /* user records are stored here */
  23. struct userrec *lastuser = NULL; /* last accessed user record */
  24. maskrec *global_bans = NULL,
  25. *global_exempts = NULL,
  26. *global_invites = NULL;
  27. struct igrec *global_ign = NULL;
  28. int cache_hit = 0,
  29. cache_miss = 0; /* temporary cache accounting */
  30. int strict_host = 0;
  31. int userfile_perm = 0600; /* Userfile permissions,
  32. default rw------- */
  33. #ifdef S_DCCPASS
  34. extern struct cmd_pass *cmdpass;
  35. #endif /* S_DCCPASS */
  36. void *_user_malloc(int size, const char *file, int line)
  37. {
  38. #ifdef DEBUG_MEM
  39. char x[1024];
  40. const char *p;
  41. p = strrchr(file, '/');
  42. simple_sprintf(x, "_userrec.c:%s", p ? p + 1 : file);
  43. return n_malloc(size, x, line);
  44. #else /* !DEBUG_MEM */
  45. return nmalloc(size);
  46. #endif /* DEBUG_MEM */
  47. }
  48. void *_user_realloc(void *ptr, int size, const char *file, int line)
  49. {
  50. #ifdef DEBUG_MEM
  51. char x[1024];
  52. const char *p;
  53. p = strrchr(file, '/');
  54. simple_sprintf(x, "_userrec.c:%s", p ? p + 1 : file);
  55. return n_realloc(ptr, size, x, line);
  56. #else /* !DEBUG_MEM */
  57. return nrealloc(ptr, size);
  58. #endif /* DEBUG_MEM */
  59. }
  60. inline int expmem_mask(struct maskrec *m)
  61. {
  62. int result = 0;
  63. while (m) {
  64. result += sizeof(struct maskrec);
  65. result += strlen(m->mask) + 1;
  66. if (m->user)
  67. result += strlen(m->user) + 1;
  68. if (m->desc)
  69. result += strlen(m->desc) + 1;
  70. m = m->next;
  71. }
  72. return result;
  73. }
  74. /* Memory we should be using
  75. */
  76. int expmem_users()
  77. {
  78. int tot;
  79. struct userrec *u;
  80. struct chanuserrec *ch;
  81. struct chanset_t *chan;
  82. struct user_entry *ue;
  83. struct igrec *i;
  84. tot = 0;
  85. for (u = userlist; u; u = u->next) {
  86. for (ch = u->chanrec; ch; ch = ch->next) {
  87. tot += sizeof(struct chanuserrec);
  88. if (ch->info != NULL)
  89. tot += strlen(ch->info) + 1;
  90. }
  91. tot += sizeof(struct userrec);
  92. for (ue = u->entries; ue; ue = ue->next) {
  93. tot += sizeof(struct user_entry);
  94. if (ue->name) {
  95. tot += strlen(ue->name) + 1;
  96. tot += list_type_expmem(ue->u.list);
  97. } else
  98. tot += ue->type->expmem(ue);
  99. }
  100. }
  101. /* Account for each channel's masks */
  102. for (chan = chanset; chan; chan = chan->next) {
  103. /* Account for each channel's ban-list user */
  104. tot += expmem_mask(chan->bans);
  105. /* Account for each channel's exempt-list user */
  106. tot += expmem_mask(chan->exempts);
  107. /* Account for each channel's invite-list user */
  108. tot += expmem_mask(chan->invites);
  109. }
  110. tot += expmem_mask(global_bans);
  111. tot += expmem_mask(global_exempts);
  112. tot += expmem_mask(global_invites);
  113. for (i = global_ign; i; i = i->next) {
  114. tot += sizeof(struct igrec);
  115. tot += strlen(i->igmask) + 1;
  116. if (i->user)
  117. tot += strlen(i->user) + 1;
  118. if (i->msg)
  119. tot += strlen(i->msg) + 1;
  120. }
  121. return tot;
  122. }
  123. int count_users(struct userrec *bu)
  124. {
  125. int tot = 0;
  126. struct userrec *u;
  127. for (u = bu; u; u = u->next)
  128. tot++;
  129. return tot;
  130. }
  131. /* Convert "nick!~user@host", "nick!+user@host" and "nick!-user@host"
  132. * to "nick!user@host" if necessary. (drummer)
  133. */
  134. char *fixfrom(char *s)
  135. {
  136. char *p;
  137. static char buf[512];
  138. if (s == NULL)
  139. return NULL;
  140. strncpyz(buf, s, sizeof buf);
  141. if (strict_host)
  142. return buf;
  143. if ((p = strchr(buf, '!')))
  144. p++;
  145. else
  146. p = s; /* Sometimes we get passed just a
  147. * user@host here... */
  148. /* These are ludicrous. */
  149. if (strchr("~+-^=", *p) && (p[1] != '@')) /* added check for @ - drummer */
  150. // strcpy(p, p + 1);
  151. sprintf(p, "%s", p + 1);
  152. /* Bug was: n!~@host -> n!@host now: n!~@host */
  153. return buf;
  154. }
  155. struct userrec *check_dcclist_hand(char *handle)
  156. {
  157. int i;
  158. for (i = 0; i < dcc_total; i++)
  159. if (!egg_strcasecmp(dcc[i].nick, handle))
  160. return dcc[i].user;
  161. return NULL;
  162. }
  163. struct userrec *get_user_by_handle(struct userrec *bu, char *handle)
  164. {
  165. struct userrec *u, *ret;
  166. if (!handle)
  167. return NULL;
  168. /* FIXME: This should be done outside of this function. */
  169. rmspace(handle);
  170. if (!handle[0] || (handle[0] == '*'))
  171. return NULL;
  172. if (bu == userlist) {
  173. if (lastuser && !egg_strcasecmp(lastuser->handle, handle)) {
  174. cache_hit++;
  175. return lastuser;
  176. }
  177. ret = check_dcclist_hand(handle);
  178. if (ret) {
  179. cache_hit++;
  180. return ret;
  181. }
  182. ret = check_chanlist_hand(handle);
  183. if (ret) {
  184. cache_hit++;
  185. return ret;
  186. }
  187. cache_miss++;
  188. }
  189. for (u = bu; u; u = u->next)
  190. if (!egg_strcasecmp(u->handle, handle)) {
  191. if (bu == userlist)
  192. lastuser = u;
  193. return u;
  194. }
  195. return NULL;
  196. }
  197. /* Fix capitalization, etc
  198. */
  199. void correct_handle(char *handle)
  200. {
  201. struct userrec *u;
  202. u = get_user_by_handle(userlist, handle);
  203. if (u == NULL || handle == u->handle)
  204. return;
  205. strcpy(handle, u->handle);
  206. }
  207. /* This will be usefull in a lot of places, much more code re-use so we
  208. * endup with a smaller executable bot. <cybah>
  209. */
  210. void clear_masks(maskrec *m)
  211. {
  212. maskrec *temp = NULL;
  213. for (; m; m = temp) {
  214. temp = m->next;
  215. if (m->mask)
  216. nfree(m->mask);
  217. if (m->user)
  218. nfree(m->user);
  219. if (m->desc)
  220. nfree(m->desc);
  221. nfree(m);
  222. }
  223. }
  224. void clear_userlist(struct userrec *bu)
  225. {
  226. struct userrec *u, *v;
  227. int i;
  228. for (u = bu; u; u = v) {
  229. v = u->next;
  230. freeuser(u);
  231. }
  232. if (userlist == bu) {
  233. struct chanset_t *cst;
  234. for (i = 0; i < dcc_total; i++)
  235. dcc[i].user = NULL;
  236. clear_chanlist();
  237. lastuser = NULL;
  238. while (global_ign)
  239. delignore(global_ign->igmask);
  240. clear_masks(global_bans);
  241. clear_masks(global_exempts);
  242. clear_masks(global_invites);
  243. global_exempts = global_invites = global_bans = NULL;
  244. for (cst = chanset; cst; cst = cst->next) {
  245. clear_masks(cst->bans);
  246. clear_masks(cst->exempts);
  247. clear_masks(cst->invites);
  248. cst->bans = cst->exempts = cst->invites = NULL;
  249. }
  250. }
  251. /* Remember to set your userlist to NULL after calling this */
  252. }
  253. /* Find CLOSEST host match
  254. * (if "*!*@*" and "*!*@*clemson.edu" both match, use the latter!)
  255. *
  256. * Checks the chanlist first, to possibly avoid needless search.
  257. */
  258. struct userrec *get_user_by_host(char *host)
  259. {
  260. struct userrec *u, *ret;
  261. struct list_type *q;
  262. int cnt, i;
  263. char host2[UHOSTLEN];
  264. if (host == NULL)
  265. return NULL;
  266. rmspace(host);
  267. if (!host[0])
  268. return NULL;
  269. ret = check_chanlist(host);
  270. cnt = 0;
  271. if (ret != NULL) {
  272. cache_hit++;
  273. return ret;
  274. }
  275. cache_miss++;
  276. strncpyz(host2, host, sizeof host2);
  277. host = fixfrom(host);
  278. for (u = userlist; u; u = u->next) {
  279. q = get_user(&USERENTRY_HOSTS, u);
  280. for (; q; q = q->next) {
  281. i = wild_match(q->extra, host);
  282. if (i > cnt) {
  283. ret = u;
  284. cnt = i;
  285. }
  286. }
  287. }
  288. if (ret != NULL) {
  289. lastuser = ret;
  290. set_chanlist(host2, ret);
  291. }
  292. return ret;
  293. }
  294. /* use fixfrom() or dont? (drummer)
  295. */
  296. struct userrec *get_user_by_equal_host(char *host)
  297. {
  298. struct userrec *u;
  299. struct list_type *q;
  300. for (u = userlist; u; u = u->next)
  301. for (q = get_user(&USERENTRY_HOSTS, u); q; q = q->next)
  302. if (!rfc_casecmp(q->extra, host))
  303. return u;
  304. return NULL;
  305. }
  306. /* Try: pass_match_by_host("-",host)
  307. * will return 1 if no password is set for that host
  308. */
  309. int u_pass_match(struct userrec *u, char *in)
  310. {
  311. char *cmp, new[32], pass[16];
  312. if (!u)
  313. return 0;
  314. egg_snprintf(pass, sizeof pass, "%s", in);
  315. cmp = get_user(&USERENTRY_PASS, u);
  316. if (!cmp && (!pass[0] || (pass[0] == '-')))
  317. return 1;
  318. if (!cmp || !pass || !pass[0] || (pass[0] == '-'))
  319. return 0;
  320. if (u->flags & USER_BOT) {
  321. if (!strcmp(cmp, pass))
  322. return 1;
  323. } else {
  324. if (strlen(pass) > 15)
  325. pass[15] = 0;
  326. encrypt_pass(pass, new);
  327. if (!strcmp(cmp, new))
  328. return 1;
  329. }
  330. return 0;
  331. }
  332. int write_user(struct userrec *u, FILE * f, int idx)
  333. {
  334. char s[181];
  335. struct chanuserrec *ch;
  336. struct chanset_t *cst;
  337. struct user_entry *ue;
  338. struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
  339. fr.global = u->flags;
  340. fr.udef_global = u->flags_udef;
  341. build_flags(s, &fr, NULL);
  342. if (lfprintf(f, "%-10s - %-24s\n", u->handle, s) == EOF)
  343. return 0;
  344. for (ch = u->chanrec; ch; ch = ch->next) {
  345. cst = findchan_by_dname(ch->channel);
  346. if (cst && ((idx < 0) || channel_shared(cst))) {
  347. if (idx >= 0) {
  348. fr.match = (FR_CHAN | FR_BOT);
  349. get_user_flagrec(dcc[idx].user, &fr, ch->channel);
  350. } else
  351. fr.chan = BOT_SHARE;
  352. if ((fr.chan & BOT_SHARE) || (1)) {
  353. fr.match = FR_CHAN;
  354. fr.chan = ch->flags;
  355. fr.udef_chan = ch->flags_udef;
  356. build_flags(s, &fr, NULL);
  357. if (lfprintf(f, "! %-20s %lu %-10s %s\n", ch->channel, ch->laston, s,
  358. ch->info ? ch->info : "") == EOF)
  359. return 0;
  360. }
  361. }
  362. }
  363. for (ue = u->entries; ue; ue = ue->next) {
  364. if (ue->name) {
  365. struct list_type *lt;
  366. for (lt = ue->u.list; lt; lt = lt->next)
  367. if (lfprintf(f, "--%s %s\n", ue->name, lt->extra) == EOF)
  368. return 0;
  369. } else {
  370. if (!ue->type->write_userfile(f, u, ue))
  371. return 0;
  372. }
  373. }
  374. return 1;
  375. }
  376. int sort_compare(struct userrec *a, struct userrec *b)
  377. {
  378. /* Order by flags, then alphabetically
  379. * first bots: +h / +a / +l / other bots
  380. * then users: +n / +m / +o / other users
  381. * return true if (a > b)
  382. */
  383. if (a->flags & b->flags & USER_BOT) {
  384. if (bot_hublevel(a) > bot_hublevel(b))
  385. return 1;
  386. else if (bot_hublevel(a) < bot_hublevel(b))
  387. return 0;
  388. else if (bot_hublevel(a) == bot_hublevel(b) && bot_hublevel(a) == 999)
  389. return 0;
  390. } else {
  391. if (~a->flags & b->flags & USER_BOT)
  392. return 1;
  393. if (a->flags & ~b->flags & USER_BOT)
  394. return 0;
  395. if (~a->flags & b->flags & USER_ADMIN)
  396. return 1;
  397. if (a->flags & ~b->flags & USER_ADMIN)
  398. return 0;
  399. if (~a->flags & b->flags & USER_OWNER)
  400. return 1;
  401. if (a->flags & ~b->flags & USER_OWNER)
  402. return 0;
  403. if (~a->flags & b->flags & USER_MASTER)
  404. return 1;
  405. if (a->flags & ~b->flags & USER_MASTER)
  406. return 0;
  407. if (~a->flags & b->flags & USER_OP)
  408. return 1;
  409. if (a->flags & ~b->flags & USER_OP)
  410. return 0;
  411. }
  412. return (egg_strcasecmp(a->handle, b->handle) > 0);
  413. }
  414. void sort_userlist()
  415. {
  416. int again;
  417. struct userrec *last, *p, *c, *n;
  418. again = 1;
  419. last = NULL;
  420. while ((userlist != last) && (again)) {
  421. p = NULL;
  422. c = userlist;
  423. n = c->next;
  424. again = 0;
  425. while (n != last) {
  426. if (sort_compare(c, n)) {
  427. again = 1;
  428. c->next = n->next;
  429. n->next = c;
  430. if (p == NULL)
  431. userlist = n;
  432. else
  433. p->next = n;
  434. }
  435. p = c;
  436. c = n;
  437. n = n->next;
  438. }
  439. last = c;
  440. }
  441. }
  442. /* Rewrite the entire user file. Call USERFILE hook as well, probably
  443. * causing the channel file to be rewritten as well.
  444. */
  445. int write_userfile(int idx)
  446. {
  447. FILE *f;
  448. char *new_userfile;
  449. char s1[81], backup[DIRMAX];
  450. time_t tt;
  451. struct userrec *u;
  452. int ok;
  453. if (userlist == NULL)
  454. return 1; /* No point in saving userfile */
  455. new_userfile = nmalloc(strlen(userfile) + 5);
  456. sprintf(new_userfile, "%s~new", userfile);
  457. f = fopen(new_userfile, "w");
  458. chmod(new_userfile, 0600);
  459. if (f == NULL) {
  460. putlog(LOG_MISC, "*", USERF_ERRWRITE);
  461. nfree(new_userfile);
  462. return 2;
  463. }
  464. if (!quiet_save)
  465. putlog(LOG_MISC, "*", USERF_WRITING);
  466. if (sort_users)
  467. sort_userlist();
  468. tt = now;
  469. strcpy(s1, ctime(&tt));
  470. lfprintf(f, "#4v: %s -- %s -- written %s", ver, botnetnick, s1);
  471. ok = 1;
  472. fclose(f);
  473. call_hook(HOOK_USERFILE);
  474. f = fopen(new_userfile, "a");
  475. putlog(LOG_DEBUG, "@", "Writing user entries.");
  476. for (u = userlist; u && ok; u = u->next)
  477. ok = write_user(u, f, idx);
  478. if (!ok || fflush(f)) {
  479. putlog(LOG_MISC, "*", "%s (%s)", USERF_ERRWRITE, strerror(ferror(f)));
  480. fclose(f);
  481. nfree(new_userfile);
  482. return 3;
  483. }
  484. fclose(f);
  485. putlog(LOG_DEBUG, "@", "Done writing userfile.");
  486. egg_snprintf(backup, sizeof backup, "%s%s~", tempdir, userfile);
  487. copyfile(userfile, backup);
  488. movefile(new_userfile, userfile);
  489. nfree(new_userfile);
  490. return 0;
  491. }
  492. int change_handle(struct userrec *u, char *newh)
  493. {
  494. int i;
  495. char s[HANDLEN + 1];
  496. if (!u)
  497. return 0;
  498. /* Nothing that will confuse the userfile */
  499. if (!newh[1] && strchr(BADHANDCHARS, newh[0]))
  500. return 0;
  501. check_tcl_nkch(u->handle, newh);
  502. /* Yes, even send bot nick changes now: */
  503. if (!noshare && !(u->flags & USER_UNSHARED))
  504. shareout(NULL, "h %s %s\n", u->handle, newh);
  505. strncpyz(s, u->handle, sizeof s);
  506. strncpyz(u->handle, newh, sizeof u->handle);
  507. for (i = 0; i < dcc_total; i++)
  508. if (dcc[i].type != &DCC_BOT && !egg_strcasecmp(dcc[i].nick, s)) {
  509. strncpyz(dcc[i].nick, newh, sizeof dcc[i].nick);
  510. if (dcc[i].type == &DCC_CHAT && dcc[i].u.chat->channel >= 0) {
  511. chanout_but(-1, dcc[i].u.chat->channel,
  512. "*** Handle change: %s -> %s\n", s, newh);
  513. if (dcc[i].u.chat->channel < GLOBAL_CHANS)
  514. botnet_send_nkch(i, s);
  515. }
  516. }
  517. return 1;
  518. }
  519. struct userrec *adduser(struct userrec *bu, char *handle, char *host,
  520. char *pass, int flags)
  521. {
  522. struct userrec *u, *x;
  523. int oldshare = noshare;
  524. noshare = 1;
  525. u = (struct userrec *) nmalloc(sizeof(struct userrec));
  526. /* u->next=bu; bu=u; */
  527. strncpyz(u->handle, handle, sizeof u->handle);
  528. u->next = NULL;
  529. u->chanrec = NULL;
  530. u->entries = NULL;
  531. if (flags != USER_DEFAULT) { /* drummer */
  532. u->flags = flags;
  533. u->flags_udef = 0;
  534. } else {
  535. u->flags = default_flags;
  536. u->flags_udef = default_uflags;
  537. }
  538. set_user(&USERENTRY_PASS, u, pass);
  539. /* Strip out commas -- they're illegal */
  540. if (host && host[0]) {
  541. char *p;
  542. /* About this fixfrom():
  543. * We should use this fixfrom before every call of adduser()
  544. * but its much easier to use here... (drummer)
  545. * Only use it if we have a host :) (dw)
  546. */
  547. host = fixfrom(host);
  548. p = strchr(host, ',');
  549. while (p != NULL) {
  550. *p = '?';
  551. p = strchr(host, ',');
  552. }
  553. set_user(&USERENTRY_HOSTS, u, host);
  554. } else
  555. set_user(&USERENTRY_HOSTS, u, "none");
  556. if (bu == userlist)
  557. clear_chanlist();
  558. noshare = oldshare;
  559. if ((!noshare) && (handle[0] != '*') && (!(flags & USER_UNSHARED)) &&
  560. (bu == userlist)) {
  561. struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
  562. char x[100];
  563. fr.global = u->flags;
  564. fr.udef_global = u->flags_udef;
  565. build_flags(x, &fr, 0);
  566. shareout(NULL, "n %s %s %s %s\n", handle, host && host[0] ? host : "none",
  567. pass, x);
  568. }
  569. if (bu == NULL)
  570. bu = u;
  571. else {
  572. if ((bu == userlist) && (lastuser != NULL))
  573. x = lastuser;
  574. else
  575. x = bu;
  576. while (x->next != NULL)
  577. x = x->next;
  578. x->next = u;
  579. if (bu == userlist)
  580. lastuser = u;
  581. }
  582. return bu;
  583. }
  584. void freeuser(struct userrec *u)
  585. {
  586. struct user_entry *ue, *ut;
  587. struct chanuserrec *ch, *z;
  588. Context;
  589. if (u == NULL)
  590. return;
  591. ch = u->chanrec;
  592. while (ch) {
  593. z = ch;
  594. ch = ch->next;
  595. if (z->info != NULL)
  596. nfree(z->info);
  597. nfree(z);
  598. }
  599. u->chanrec = NULL;
  600. for (ue = u->entries; ue; ue = ut) {
  601. ut = ue->next;
  602. if (ue->name) {
  603. struct list_type *lt, *ltt;
  604. for (lt = ue->u.list; lt; lt = ltt) {
  605. ltt = lt->next;
  606. nfree(lt->extra);
  607. nfree(lt);
  608. }
  609. nfree(ue->name);
  610. nfree(ue);
  611. } else {
  612. Context;
  613. ue->type->kill(ue);
  614. Context;
  615. }
  616. }
  617. nfree(u);
  618. }
  619. int deluser(char *handle)
  620. {
  621. struct userrec *u = userlist, *prev = NULL;
  622. int fnd = 0;
  623. while ((u != NULL) && (!fnd)) {
  624. if (!egg_strcasecmp(u->handle, handle))
  625. fnd = 1;
  626. else {
  627. prev = u;
  628. u = u->next;
  629. }
  630. }
  631. if (!fnd)
  632. return 0;
  633. if (prev == NULL)
  634. userlist = u->next;
  635. else
  636. prev->next = u->next;
  637. if (!noshare && (handle[0] != '*') && !(u->flags & USER_UNSHARED))
  638. shareout(NULL, "k %s\n", handle);
  639. for (fnd = 0; fnd < dcc_total; fnd++)
  640. if (dcc[fnd].user == u)
  641. dcc[fnd].user = 0; /* Clear any dcc users for this entry,
  642. * null is safe-ish */
  643. clear_chanlist();
  644. freeuser(u);
  645. lastuser = NULL;
  646. return 1;
  647. }
  648. int delhost_by_handle(char *handle, char *host)
  649. {
  650. struct userrec *u;
  651. struct list_type *q, *qnext, *qprev;
  652. struct user_entry *e = NULL;
  653. int i = 0;
  654. u = get_user_by_handle(userlist, handle);
  655. if (!u)
  656. return 0;
  657. q = get_user(&USERENTRY_HOSTS, u);
  658. qprev = q;
  659. if (q) {
  660. if (!rfc_casecmp(q->extra, host)) {
  661. e = find_user_entry(&USERENTRY_HOSTS, u);
  662. e->u.extra = q->next;
  663. nfree(q->extra);
  664. nfree(q);
  665. i++;
  666. qprev = NULL;
  667. q = e->u.extra;
  668. } else
  669. q = q->next;
  670. while (q) {
  671. qnext = q->next;
  672. if (!rfc_casecmp(q->extra, host)) {
  673. if (qprev)
  674. qprev->next = q->next;
  675. else if (e) {
  676. e->u.extra = q->next;
  677. qprev = NULL;
  678. }
  679. nfree(q->extra);
  680. nfree(q);
  681. i++;
  682. } else
  683. qprev = q;
  684. q = qnext;
  685. }
  686. }
  687. if (!qprev)
  688. set_user(&USERENTRY_HOSTS, u, "none");
  689. if (!noshare && i && !(u->flags & USER_UNSHARED))
  690. shareout(NULL, "-h %s %s\n", handle, host);
  691. clear_chanlist();
  692. return i;
  693. }
  694. void addhost_by_handle(char *handle, char *host)
  695. {
  696. struct userrec *u = get_user_by_handle(userlist, handle);
  697. set_user(&USERENTRY_HOSTS, u, host);
  698. /* u will be cached, so really no overhead, even tho this looks dumb: */
  699. if ((!noshare) && !(u->flags & USER_UNSHARED)) {
  700. if (u->flags & USER_BOT)
  701. shareout(NULL, "+bh %s %s\n", handle, host);
  702. else
  703. shareout(NULL, "+h %s %s\n", handle, host);
  704. }
  705. clear_chanlist();
  706. }
  707. void touch_laston(struct userrec *u, char *where, time_t timeval)
  708. {
  709. if (!u)
  710. return;
  711. if (timeval > 1) {
  712. struct laston_info *li =
  713. (struct laston_info *) get_user(&USERENTRY_LASTON, u);
  714. if (!li)
  715. li = nmalloc(sizeof(struct laston_info));
  716. else if (li->lastonplace)
  717. nfree(li->lastonplace);
  718. li->laston = timeval;
  719. if (where) {
  720. li->lastonplace = nmalloc(strlen(where) + 1);
  721. strcpy(li->lastonplace, where);
  722. } else
  723. li->lastonplace = NULL;
  724. set_user(&USERENTRY_LASTON, u, li);
  725. } else if (timeval == 1)
  726. set_user(&USERENTRY_LASTON, u, 0);
  727. }
  728. /* Go through all channel records and try to find a matching
  729. * nick. Will return the user's user record if that is known
  730. * to the bot. (Fabian)
  731. *
  732. * Warning: This is unreliable by concept!
  733. */
  734. struct userrec *get_user_by_nick(char *nick)
  735. {
  736. struct chanset_t *chan;
  737. memberlist *m;
  738. for (chan = chanset; chan; chan = chan->next) {
  739. for (m = chan->channel.member; m && m->nick[0] ;m = m->next) {
  740. if (!rfc_casecmp(nick, m->nick)) {
  741. char word[512];
  742. egg_snprintf(word, sizeof word, "%s!%s", m->nick, m->userhost);
  743. /* No need to check the return value ourself */
  744. return get_user_by_host(word);;
  745. }
  746. }
  747. }
  748. /* Sorry, no matches */
  749. return NULL;
  750. }
  751. void user_del_chan(char *dname)
  752. {
  753. struct chanuserrec *ch, *och;
  754. struct userrec *u;
  755. for (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. nfree(ch->info);
  766. nfree(ch);
  767. break;
  768. }
  769. och = ch;
  770. ch = ch->next;
  771. }
  772. }
  773. }