userrec.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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[], 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
  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], backup[DIRMAX];
  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. snprintf(backup, sizeof backup, "%s/%s~", tempdir, userfile);
  490. copyfile(userfile, backup);
  491. movefile(new_userfile, userfile);
  492. nfree(new_userfile);
  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. }
  776. struct cfg_entry
  777. CFG_BADCOOKIE,
  778. CFG_MANUALOP,
  779. #ifdef G_MEAN
  780. CFG_MEANDEOP,
  781. CFG_MEANKICK,
  782. CFG_MEANBAN,
  783. #endif
  784. CFG_MDOP;
  785. int deflag_dontshare=0;
  786. char deflag_tmp[20];
  787. #define DEFL_IGNORE 0
  788. #define DEFL_DEOP 1
  789. #define DEFL_KICK 2
  790. #define DEFL_DELETE 3
  791. void deflag_describe(struct cfg_entry *cfgent, int idx)
  792. {
  793. if (cfgent == &CFG_BADCOOKIE)
  794. dprintf(idx, STR("bad-cookie decides what happens to a bot if it does an illegal op (no/incorrect op cookie)\n"));
  795. else if (cfgent==&CFG_MANUALOP)
  796. dprintf(idx, STR("manop decides what happens to a user doing a manual op in a -manop channel\n"));
  797. #ifdef G_MEAN
  798. else if (cfgent==&CFG_MEANDEOP)
  799. dprintf(idx, STR("mean-deop decides what happens to a user deopping a bot in a +mean channel\n"));
  800. else if (cfgent==&CFG_MEANKICK)
  801. dprintf(idx, STR("mean-kick decides what happens to a user kicking a bot in a +mean channel\n"));
  802. else if (cfgent==&CFG_MEANBAN)
  803. dprintf(idx, STR("mean-ban decides what happens to a user banning a bot in a +mean channel\n"));
  804. #endif
  805. else if (cfgent==&CFG_MDOP)
  806. dprintf(idx, STR("mdop decides what happens to a user doing a mass deop\n"));
  807. dprintf(idx, STR("Valid settings are: ignore (No flag changes), deop (give -afmnop+d), kick (give -afmnop+dk) or delete (remove from userlist)\n"));
  808. }
  809. void deflag_changed(struct cfg_entry * entry, char * oldval, int * valid) {
  810. char * p = (char *) entry->gdata;
  811. if (!p)
  812. return;
  813. if (strcmp(p, STR("ignore")) && strcmp(p, STR("deop")) && strcmp(p, STR("kick")) && strcmp(p, STR("delete")))
  814. *valid=0;
  815. }
  816. struct cfg_entry CFG_BADCOOKIE = {
  817. "bad-cookie", CFGF_GLOBAL, NULL, NULL,
  818. deflag_changed,
  819. NULL,
  820. deflag_describe
  821. };
  822. struct cfg_entry CFG_MANUALOP = {
  823. "manop", CFGF_GLOBAL, NULL, NULL,
  824. deflag_changed,
  825. NULL,
  826. deflag_describe
  827. };
  828. #ifdef G_MEAN
  829. struct cfg_entry CFG_MEANDEOP = {
  830. "mean-deop", CFGF_GLOBAL, NULL, NULL,
  831. deflag_changed,
  832. NULL,
  833. deflag_describe
  834. };
  835. struct cfg_entry CFG_MEANKICK = {
  836. "mean-kick", CFGF_GLOBAL, NULL, NULL,
  837. deflag_changed,
  838. NULL,
  839. deflag_describe
  840. };
  841. struct cfg_entry CFG_MEANBAN = {
  842. "mean-ban", CFGF_GLOBAL, NULL, NULL,
  843. deflag_changed,
  844. NULL,
  845. deflag_describe
  846. };
  847. #endif
  848. struct cfg_entry CFG_MDOP = {
  849. "mdop", CFGF_GLOBAL, NULL, NULL,
  850. deflag_changed,
  851. NULL,
  852. deflag_describe
  853. };
  854. void deflag_user(struct userrec *u, int why, char *msg, struct chanset_t *chan)
  855. {
  856. char tmp[256], tmp2[1024];
  857. struct cfg_entry * ent = NULL;
  858. struct flag_record fr = {FR_GLOBAL, FR_CHAN, 0, 0, 0};
  859. if (!u)
  860. return;
  861. switch (why) {
  862. case DEFLAG_BADCOOKIE:
  863. strcpy(tmp, STR("Bad op cookie"));
  864. ent=&CFG_BADCOOKIE;
  865. break;
  866. case DEFLAG_MANUALOP:
  867. strcpy(tmp, STR("Manual op in -manop channel"));
  868. ent=&CFG_MANUALOP;
  869. break;
  870. #ifdef G_MEAN
  871. case DEFLAG_MEAN_DEOP:
  872. strcpy(tmp, STR("Deopped bot in +mean channel"));
  873. ent=&CFG_MEANDEOP;
  874. break;
  875. case DEFLAG_MEAN_KICK:
  876. strcpy(tmp, STR("Kicked bot in +mean channel"));
  877. ent=&CFG_MEANDEOP;
  878. break;
  879. case DEFLAG_MEAN_BAN:
  880. strcpy(tmp, STR("Banned bot in +mean channel"));
  881. ent=&CFG_MEANDEOP;
  882. break;
  883. #endif
  884. case DEFLAG_MDOP:
  885. strcpy(tmp, STR("Mass deop"));
  886. ent=&CFG_MDOP;
  887. break;
  888. default:
  889. ent=NULL;
  890. sprintf(tmp, STR("Reason #%i"), why);
  891. }
  892. if (ent && ent->gdata && !strcmp(ent->gdata, STR("deop"))) {
  893. putlog(LOG_WARN, "*", STR("Setting %s +d (%s): %s\n"), u->handle, tmp, msg);
  894. sprintf(tmp2, STR("+d: %s (%s)"), tmp, msg);
  895. set_user(&USERENTRY_COMMENT, u, tmp2);
  896. get_user_flagrec(u, &fr, chan->dname);
  897. fr.global = USER_DEOP;
  898. fr.chan = USER_DEOP;
  899. set_user_flagrec(u, &fr, chan->dname);
  900. } else if (ent && ent->gdata && !strcmp(ent->gdata, STR("kick"))) {
  901. putlog(LOG_WARN, "*", STR("Setting %s +dk (%s): %s\n"), u->handle, tmp, msg);
  902. sprintf(tmp2, STR("+dk: %s (%s)"), tmp, msg);
  903. set_user(&USERENTRY_COMMENT, u, tmp2);
  904. get_user_flagrec(u, &fr, chan->dname);
  905. fr.global = USER_DEOP | USER_KICK;
  906. fr.chan = USER_DEOP | USER_KICK;
  907. set_user_flagrec(u, &fr, chan->dname);
  908. } else if (ent && ent->gdata && !strcmp(ent->gdata, STR("delete"))) {
  909. putlog(LOG_WARN, "*", STR("Deleting %s (%s): %s\n"), u->handle, tmp, msg);
  910. deluser(u->handle);
  911. } else {
  912. putlog(LOG_WARN, "*", STR("No user flag effects for %s (%s): %s\n"), u->handle, tmp, msg);
  913. sprintf(tmp2, STR("Warning: %s (%s)"), tmp, msg);
  914. set_user(&USERENTRY_COMMENT, u, tmp2);
  915. }
  916. }
  917. void init_userrec() {
  918. add_cfg(&CFG_BADCOOKIE);
  919. add_cfg(&CFG_MANUALOP);
  920. #ifdef G_MEAN
  921. add_cfg(&CFG_MEANDEOP);
  922. add_cfg(&CFG_MEANKICK);
  923. add_cfg(&CFG_MEANBAN);
  924. #endif
  925. add_cfg(&CFG_MDOP);
  926. }