userrec.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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, share_greet;
  18. extern char userfile[], ver[], botnetnick[];
  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
  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
  45. return nmalloc(size);
  46. #endif
  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
  57. return nrealloc(ptr, size);
  58. #endif
  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. (((idx < 0) || share_greet) && ch->info) ? ch->info
  358. : "") == 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_flags(a) & bot_flags(b) & BOT_HUB)
  385. return 1;
  386. if (bot_flags(a) & ~bot_flags(b) & BOT_HUB)
  387. return 0;
  388. /* if (~bot_flags(a) & bot_flags(b) & BOT_ALT)
  389. return 1;
  390. if (bot_flags(a) & ~bot_flags(b) & BOT_ALT)
  391. return 0;*/
  392. if (~bot_flags(a) & bot_flags(b) & BOT_LEAF)
  393. return 1;
  394. if (bot_flags(a) & ~bot_flags(b) & BOT_LEAF)
  395. return 0;
  396. } else {
  397. if (~a->flags & b->flags & USER_BOT)
  398. return 1;
  399. if (a->flags & ~b->flags & USER_BOT)
  400. return 0;
  401. if (~a->flags & b->flags & USER_OWNER)
  402. return 1;
  403. if (a->flags & ~b->flags & USER_OWNER)
  404. return 0;
  405. if (~a->flags & b->flags & USER_MASTER)
  406. return 1;
  407. if (a->flags & ~b->flags & USER_MASTER)
  408. return 0;
  409. if (~a->flags & b->flags & USER_OP)
  410. return 1;
  411. if (a->flags & ~b->flags & USER_OP)
  412. return 0;
  413. }
  414. return (egg_strcasecmp(a->handle, b->handle) > 0);
  415. }
  416. void sort_userlist()
  417. {
  418. int again;
  419. struct userrec *last, *p, *c, *n;
  420. again = 1;
  421. last = NULL;
  422. while ((userlist != last) && (again)) {
  423. p = NULL;
  424. c = userlist;
  425. n = c->next;
  426. again = 0;
  427. while (n != last) {
  428. if (sort_compare(c, n)) {
  429. again = 1;
  430. c->next = n->next;
  431. n->next = c;
  432. if (p == NULL)
  433. userlist = n;
  434. else
  435. p->next = n;
  436. }
  437. p = c;
  438. c = n;
  439. n = n->next;
  440. }
  441. last = c;
  442. }
  443. }
  444. /* Rewrite the entire user file. Call USERFILE hook as well, probably
  445. * causing the channel file to be rewritten as well.
  446. */
  447. void write_userfile(int idx)
  448. {
  449. FILE *f;
  450. char *new_userfile;
  451. char s1[81];
  452. time_t tt;
  453. struct userrec *u;
  454. int ok;
  455. if (userlist == NULL)
  456. return; /* No point in saving userfile */
  457. new_userfile = nmalloc(strlen(userfile) + 5);
  458. sprintf(new_userfile, "%s~new", userfile);
  459. f = fopen(new_userfile, "w");
  460. chmod(new_userfile, 0600);
  461. if (f == NULL) {
  462. putlog(LOG_MISC, "*", USERF_ERRWRITE);
  463. nfree(new_userfile);
  464. return;
  465. }
  466. if (!quiet_save)
  467. putlog(LOG_MISC, "*", USERF_WRITING);
  468. if (sort_users)
  469. sort_userlist();
  470. tt = now;
  471. strcpy(s1, ctime(&tt));
  472. lfprintf(f, "#4v: %s -- %s -- written %s", ver, botnetnick, s1);
  473. ok = 1;
  474. fclose(f);
  475. call_hook(HOOK_USERFILE);
  476. f = fopen(new_userfile, "a");
  477. putlog(LOG_DEBUG, "@", "Writing user entries.");
  478. for (u = userlist; u && ok; u = u->next)
  479. ok = write_user(u, f, idx);
  480. if (!ok || fflush(f)) {
  481. putlog(LOG_MISC, "*", "%s (%s)", USERF_ERRWRITE, strerror(ferror(f)));
  482. fclose(f);
  483. nfree(new_userfile);
  484. return;
  485. }
  486. lfprintf(f, "#DONT DELETE THIS LINE.");
  487. fclose(f);
  488. putlog(LOG_DEBUG, "@", "Done writing userfile.");
  489. movefile(new_userfile, userfile);
  490. nfree(new_userfile);
  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. }
  774. struct cfg_entry
  775. CFG_BADCOOKIE,
  776. CFG_MANUALOP,
  777. #ifdef G_MEAN
  778. CFG_MEANDEOP,
  779. CFG_MEANKICK,
  780. CFG_MEANBAN,
  781. #endif
  782. CFG_MDOP;
  783. int deflag_dontshare=0;
  784. char deflag_tmp[20];
  785. #define DEFL_IGNORE 0
  786. #define DEFL_DEOP 1
  787. #define DEFL_KICK 2
  788. #define DEFL_DELETE 3
  789. void deflag_describe(struct cfg_entry *cfgent, int idx)
  790. {
  791. if (cfgent == &CFG_BADCOOKIE)
  792. dprintf(idx, STR("bad-cookie decides what happens to a bot if it does an illegal op (no/incorrect op cookie)\n"));
  793. else if (cfgent==&CFG_MANUALOP)
  794. dprintf(idx, STR("manop decides what happens to a user doing a manual op in a -manop channel\n"));
  795. #ifdef G_MEAN
  796. else if (cfgent==&CFG_MEANDEOP)
  797. dprintf(idx, STR("mean-deop decides what happens to a user deopping a bot in a +mean channel\n"));
  798. else if (cfgent==&CFG_MEANKICK)
  799. dprintf(idx, STR("mean-kick decides what happens to a user kicking a bot in a +mean channel\n"));
  800. else if (cfgent==&CFG_MEANBAN)
  801. dprintf(idx, STR("mean-ban decides what happens to a user banning a bot in a +mean channel\n"));
  802. #endif
  803. else if (cfgent==&CFG_MDOP)
  804. dprintf(idx, STR("mdop decides what happens to a user doing a mass deop\n"));
  805. dprintf(idx, STR("Valid settings are: ignore (No flag changes), deop (give -fmnop+d), kick (give -fmnop+dk) or delete (remove from userlist)\n"));
  806. }
  807. void deflag_changed(struct cfg_entry * entry, char * oldval, int * valid) {
  808. char * p = (char *) entry->gdata;
  809. if (!p)
  810. return;
  811. if (strcmp(p, STR("ignore")) && strcmp(p, STR("deop")) && strcmp(p, STR("kick")) && strcmp(p, STR("delete")))
  812. *valid=0;
  813. }
  814. struct cfg_entry CFG_BADCOOKIE = {
  815. "bad-cookie", CFGF_GLOBAL, NULL, NULL,
  816. deflag_changed,
  817. NULL,
  818. deflag_describe
  819. };
  820. struct cfg_entry CFG_MANUALOP = {
  821. "manop", CFGF_GLOBAL, NULL, NULL,
  822. deflag_changed,
  823. NULL,
  824. deflag_describe
  825. };
  826. #ifdef G_MEAN
  827. struct cfg_entry CFG_MEANDEOP = {
  828. "mean-deop", CFGF_GLOBAL, NULL, NULL,
  829. deflag_changed,
  830. NULL,
  831. deflag_describe
  832. };
  833. struct cfg_entry CFG_MEANKICK = {
  834. "mean-kick", CFGF_GLOBAL, NULL, NULL,
  835. deflag_changed,
  836. NULL,
  837. deflag_describe
  838. };
  839. struct cfg_entry CFG_MEANBAN = {
  840. "mean-ban", CFGF_GLOBAL, NULL, NULL,
  841. deflag_changed,
  842. NULL,
  843. deflag_describe
  844. };
  845. #endif
  846. struct cfg_entry CFG_MDOP = {
  847. "mdop", CFGF_GLOBAL, NULL, NULL,
  848. deflag_changed,
  849. NULL,
  850. deflag_describe
  851. };
  852. void deflag_user(struct userrec *u, int why, char *msg, struct chanset_t *chan)
  853. {
  854. char tmp[256], tmp2[1024];
  855. struct cfg_entry * ent = NULL;
  856. struct flag_record fr = {FR_GLOBAL, FR_CHAN, 0, 0, 0};
  857. if (!u)
  858. return;
  859. switch (why) {
  860. case DEFLAG_BADCOOKIE:
  861. strcpy(tmp, STR("Bad op cookie"));
  862. ent=&CFG_BADCOOKIE;
  863. break;
  864. case DEFLAG_MANUALOP:
  865. strcpy(tmp, STR("Manual op in -manop channel"));
  866. ent=&CFG_MANUALOP;
  867. break;
  868. #ifdef G_MEAN
  869. case DEFLAG_MEAN_DEOP:
  870. strcpy(tmp, STR("Deopped bot in +mean channel"));
  871. ent=&CFG_MEANDEOP;
  872. break;
  873. case DEFLAG_MEAN_KICK:
  874. strcpy(tmp, STR("Kicked bot in +mean channel"));
  875. ent=&CFG_MEANDEOP;
  876. break;
  877. case DEFLAG_MEAN_BAN:
  878. strcpy(tmp, STR("Banned bot in +mean channel"));
  879. ent=&CFG_MEANDEOP;
  880. break;
  881. #endif
  882. case DEFLAG_MDOP:
  883. strcpy(tmp, STR("Mass deop"));
  884. ent=&CFG_MDOP;
  885. break;
  886. default:
  887. ent=NULL;
  888. sprintf(tmp, STR("Reason #%i"), why);
  889. }
  890. if (ent && ent->gdata && !strcmp(ent->gdata, STR("deop"))) {
  891. putlog(LOG_WARN, "*", STR("Setting %s +d (%s): %s\n"), u->handle, tmp, msg);
  892. sprintf(tmp2, STR("+d: %s (%s)"), tmp, msg);
  893. set_user(&USERENTRY_COMMENT, u, tmp2);
  894. get_user_flagrec(u, &fr, chan->dname);
  895. fr.global = USER_DEOP;
  896. fr.chan = USER_DEOP;
  897. set_user_flagrec(u, &fr, chan->dname);
  898. } else if (ent && ent->gdata && !strcmp(ent->gdata, STR("kick"))) {
  899. putlog(LOG_WARN, "*", STR("Setting %s +dk (%s): %s\n"), u->handle, tmp, msg);
  900. sprintf(tmp2, STR("+dk: %s (%s)"), tmp, msg);
  901. set_user(&USERENTRY_COMMENT, u, tmp2);
  902. get_user_flagrec(u, &fr, chan->dname);
  903. fr.global = USER_DEOP | USER_KICK;
  904. fr.chan = USER_DEOP | USER_KICK;
  905. set_user_flagrec(u, &fr, chan->dname);
  906. } else if (ent && ent->gdata && !strcmp(ent->gdata, STR("delete"))) {
  907. putlog(LOG_WARN, "*", STR("Deleting %s (%s): %s\n"), u->handle, tmp, msg);
  908. deluser(u->handle);
  909. } else {
  910. putlog(LOG_WARN, "*", STR("No user flag effects for %s (%s): %s\n"), u->handle, tmp, msg);
  911. sprintf(tmp2, STR("Warning: %s (%s)"), tmp, msg);
  912. set_user(&USERENTRY_COMMENT, u, tmp2);
  913. }
  914. }
  915. void init_userrec() {
  916. add_cfg(&CFG_BADCOOKIE);
  917. add_cfg(&CFG_MANUALOP);
  918. #ifdef G_MEAN
  919. add_cfg(&CFG_MEANDEOP);
  920. add_cfg(&CFG_MEANKICK);
  921. add_cfg(&CFG_MEANBAN);
  922. #endif
  923. add_cfg(&CFG_MDOP);
  924. }