userrec.c 19 KB

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