userchan.cc 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * userchan.c -- part of channels.mod
  22. *
  23. */
  24. #include <bdlib/src/String.h>
  25. #include <bdlib/src/Stream.h>
  26. extern struct cmd_pass *cmdpass;
  27. struct chanuserrec *get_chanrec(struct userrec *u, char *chname)
  28. {
  29. for (struct chanuserrec *ch = u->chanrec; ch; ch = ch->next)
  30. if (!rfc_casecmp(ch->channel, chname))
  31. return ch;
  32. return NULL;
  33. }
  34. struct chanuserrec *add_chanrec(struct userrec *u, char *chname)
  35. {
  36. if (findchan_by_dname(chname)) {
  37. struct chanuserrec *ch = (struct chanuserrec *) calloc(1, sizeof(struct chanuserrec));
  38. ch->next = u->chanrec;
  39. u->chanrec = ch;
  40. ch->info = NULL;
  41. ch->flags = 0;
  42. ch->laston = 0;
  43. strlcpy(ch->channel, chname, sizeof(ch->channel));
  44. ch->channel[80] = 0;
  45. if (!noshare)
  46. shareout("+cr %s %s\n", u->handle, chname);
  47. return ch;
  48. }
  49. return NULL;
  50. }
  51. void add_chanrec_by_handle(struct userrec *bu, char *hand, char *chname)
  52. {
  53. struct userrec *u = get_user_by_handle(bu, hand);
  54. if (!u)
  55. return;
  56. if (!get_chanrec(u, chname))
  57. add_chanrec(u, chname);
  58. }
  59. void get_handle_chaninfo(char *handle, char *chname, char *s)
  60. {
  61. struct userrec *u = get_user_by_handle(userlist, handle);
  62. if (u == NULL) {
  63. s[0] = 0;
  64. return;
  65. }
  66. struct chanuserrec *ch = get_chanrec(u, chname);
  67. if (ch == NULL) {
  68. s[0] = 0;
  69. return;
  70. }
  71. if (ch->info == NULL) {
  72. s[0] = 0;
  73. return;
  74. }
  75. strcpy(s, ch->info);
  76. return;
  77. }
  78. void set_handle_chaninfo(struct userrec *bu, char *handle, char *chname, char *info)
  79. {
  80. struct userrec *u = get_user_by_handle(bu, handle);
  81. if (!u)
  82. return;
  83. struct chanuserrec *ch = get_chanrec(u, chname);
  84. if (!ch) {
  85. add_chanrec_by_handle(bu, handle, chname);
  86. ch = get_chanrec(u, chname);
  87. }
  88. if (info) {
  89. if (strlen(info) > 80)
  90. info[80] = 0;
  91. }
  92. free(ch->info);
  93. if (info && info[0]) {
  94. ch->info = strdup(info);
  95. } else
  96. ch->info = NULL;
  97. if ((!noshare) && (bu == userlist)) {
  98. shareout("chchinfo %s %s %s\n", handle, chname, info ? info : "");
  99. }
  100. }
  101. void del_chanrec(struct userrec *u, char *chname)
  102. {
  103. struct chanuserrec *ch = u->chanrec, *lst = NULL;
  104. while (ch) {
  105. if (!rfc_casecmp(chname, ch->channel)) {
  106. if (lst == NULL)
  107. u->chanrec = ch->next;
  108. else
  109. lst->next = ch->next;
  110. free(ch->info);
  111. free(ch);
  112. if (!noshare)
  113. shareout("-cr %s %s\n", u->handle, chname);
  114. return;
  115. }
  116. lst = ch;
  117. ch = ch->next;
  118. }
  119. }
  120. void set_handle_laston(char *chan, struct userrec *u, time_t n)
  121. {
  122. if (!u)
  123. return;
  124. touch_laston(u, chan, n);
  125. struct chanuserrec *ch = get_chanrec(u, chan);
  126. if (!ch)
  127. return;
  128. ch->laston = n;
  129. }
  130. /* Is this mask sticky?
  131. */
  132. int
  133. __attribute__((pure))
  134. u_sticky_mask(const maskrec *u, const char *uhost)
  135. {
  136. for (; u; u = u->next)
  137. if (!rfc_casecmp(u->mask, uhost))
  138. return (u->flags & MASKREC_STICKY);
  139. return 0;
  140. }
  141. /* Set sticky attribute for a mask.
  142. */
  143. int u_setsticky_mask(struct chanset_t *chan, maskrec *u, char *uhost, int sticky, const char type)
  144. {
  145. int j;
  146. if (str_isdigit(uhost))
  147. j = atoi(uhost);
  148. else
  149. j = (-1);
  150. while(u) {
  151. if (j >= 0)
  152. j--;
  153. if (!j || ((j < 0) && !rfc_casecmp(u->mask, uhost))) {
  154. if (sticky > 0)
  155. u->flags |= MASKREC_STICKY;
  156. else if (!sticky)
  157. u->flags &= ~MASKREC_STICKY;
  158. else /* We don't actually want to change, just skip over */
  159. return 0;
  160. if (!j)
  161. strcpy(uhost, u->mask);
  162. if (!noshare)
  163. shareout("ms %c %s %d %s\n", type, uhost, sticky, (chan) ? chan->dname : "");
  164. return 1;
  165. }
  166. u = u->next;
  167. }
  168. if (j >= 0)
  169. return -j;
  170. return 0;
  171. }
  172. /* Merge of u_equals_ban(), u_equals_exempt() and u_equals_invite().
  173. *
  174. * Returns:
  175. * 0 not a ban
  176. * 1 temporary ban
  177. * 2 perm ban
  178. */
  179. int
  180. __attribute__((pure))
  181. u_equals_mask(const maskrec *u, const char *mask)
  182. {
  183. for (; u; u = u->next)
  184. if (!rfc_casecmp(u->mask, mask)) {
  185. if (u->flags & MASKREC_PERM)
  186. return 2;
  187. else
  188. return 1;
  189. }
  190. return 0;
  191. }
  192. bool
  193. __attribute__((pure))
  194. u_match_mask(const maskrec *rec, const char *mask)
  195. {
  196. for (; rec; rec = rec->next)
  197. if (wild_match(rec->mask, mask) || match_cidr(rec->mask, mask))
  198. return 1;
  199. return 0;
  200. }
  201. static int
  202. __attribute__((pure))
  203. count_mask(maskrec *rec)
  204. {
  205. int ret = 0;
  206. for (; rec; rec = rec->next)
  207. ret++;
  208. return ret;
  209. }
  210. int u_delmask(char type, struct chanset_t *c, char *who, int doit)
  211. {
  212. int j, i = 0, n_mask = 0;
  213. char temp[256] = "";
  214. maskrec **u = NULL, *t;
  215. if (type == 'b') {
  216. u = c ? &c->bans : &global_bans;
  217. n_mask = count_mask(global_bans);
  218. } else if (type == 'e') {
  219. u = c ? &c->exempts : &global_exempts;
  220. n_mask = count_mask(global_exempts);
  221. } else if (type == 'I') {
  222. u = c ? &c->invites : &global_invites;
  223. n_mask = count_mask(global_invites);
  224. }
  225. if (!strchr(who, '!') && str_isdigit(who)) {
  226. j = atoi(who);
  227. if (j) {
  228. j--; /* our list starts at 0 */
  229. if (c)
  230. j -= n_mask; /* subtract out the globals as the number given is globals+j */
  231. for (; (*u) && j; u = &((*u)->next), j--)
  232. ;
  233. if (*u) {
  234. strlcpy(temp, (*u)->mask, sizeof temp);
  235. i = 1;
  236. } else
  237. return -j - 1;
  238. } else
  239. return 0;
  240. } else {
  241. /* Find matching host, if there is one */
  242. for (; *u && !i; u = &((*u)->next))
  243. if (!rfc_casecmp((*u)->mask, who)) {
  244. strlcpy(temp, who, sizeof temp);
  245. i = 1;
  246. break;
  247. }
  248. if (!*u)
  249. return 0;
  250. }
  251. if (i && doit) {
  252. if (!noshare) {
  253. char *mask = str_escape(temp, ':', '\\');
  254. if (mask) {
  255. /* Distribute chan bans differently */
  256. if (c)
  257. shareout("-mc %c %s %s\n", type, c->dname, mask);
  258. else
  259. shareout("-m %c %s\n", type, mask);
  260. free(mask);
  261. }
  262. }
  263. free(lastdeletedmask);
  264. lastdeletedmask = (*u)->mask;
  265. free((*u)->desc);
  266. free((*u)->user);
  267. t = *u;
  268. *u = (*u)->next;
  269. free(t);
  270. }
  271. return i;
  272. }
  273. /* Note: If first char of note is '*' it's a sticky mask.
  274. */
  275. bool u_addmask(char type, struct chanset_t *chan, char *who, const char *from, const char *note, time_t expire_time, int flags)
  276. {
  277. char host[UHOSTLEN] = "", s[UHOSTLEN] = "";
  278. maskrec *p = NULL, *l = NULL, **u = NULL;
  279. if (type == 'b')
  280. u = chan ? &chan->bans : &global_bans;
  281. else if (type == 'e')
  282. u = chan ? &chan->exempts : &global_exempts;
  283. else if (type == 'I')
  284. u = chan ? &chan->invites : &global_invites;
  285. strlcpy(host, who, sizeof(host));
  286. /* Choke check: fix broken bans (must have '!' and '@') */
  287. if ((strchr(host, '!') == NULL) && (strchr(host, '@') == NULL))
  288. strlcat(host, "!*@*", sizeof(host));
  289. else if (strchr(host, '@') == NULL)
  290. strlcat(host, "@*", sizeof(host));
  291. else if (strchr(host, '!') == NULL) {
  292. char *i = strchr(host, '@');
  293. strlcpy(s, i, sizeof(s));
  294. *i = 0;
  295. strlcat(host, "!*", sizeof(host));
  296. strlcat(host, s, sizeof(host));
  297. }
  298. if (conf.bot->hub)
  299. simple_snprintf(s, sizeof(s), "%s!%s@%s", origbotname, botuser, conf.bot->net.host);
  300. else
  301. simple_snprintf(s, sizeof(s), "%s!%s", botname, botuserhost);
  302. if (s[0] && type == 'b' && wild_match(host, s)) {
  303. putlog(LOG_MISC, "*", "Wanted to ban myself--deflected.");
  304. return 0;
  305. }
  306. if (expire_time == now)
  307. return 1;
  308. for (l = *u; l; l = l->next)
  309. if (!rfc_casecmp(l->mask, host)) {
  310. p = l;
  311. break;
  312. }
  313. /* It shouldn't expire and be sticky also */
  314. if (note[0] == '*') {
  315. flags |= MASKREC_STICKY;
  316. note++;
  317. }
  318. if ((expire_time == 0L) || (flags & MASKREC_PERM)) {
  319. flags |= MASKREC_PERM;
  320. expire_time = 0L;
  321. }
  322. if (p == NULL) {
  323. p = (maskrec *) calloc(1, sizeof(maskrec));
  324. p->next = *u;
  325. *u = p;
  326. }
  327. else {
  328. free(p->mask);
  329. free(p->user);
  330. free(p->desc);
  331. }
  332. p->expire = expire_time;
  333. p->added = now;
  334. p->lastactive = 0;
  335. p->flags = flags;
  336. p->mask = strdup(host);
  337. p->user = strdup(from);
  338. p->desc = strdup(note);
  339. if (!noshare) {
  340. char *mask = str_escape(host, ':', '\\');
  341. if (mask) {
  342. if (!chan)
  343. shareout("+m %c %s %d %s%s %s %s\n",
  344. type, mask, (int) (expire_time - now),
  345. (flags & MASKREC_STICKY) ? "s" : "",
  346. (flags & MASKREC_PERM) ? "p" : "-", from, note);
  347. else
  348. shareout("+mc %c %s %d %s %s%s %s %s\n",
  349. type, mask, (int) (expire_time - now), chan->dname,
  350. (flags & MASKREC_STICKY) ? "s" : "",
  351. (flags & MASKREC_PERM) ? "p" : "-", from, note);
  352. free(mask);
  353. }
  354. }
  355. return 1;
  356. }
  357. /* Take host entry from ban list and display it ban-style.
  358. */
  359. static void display_mask(const char type, int idx, int number, maskrec *mask, struct chanset_t *chan, bool show_inact)
  360. {
  361. char dates[81] = "", s[41] = "";
  362. const char *str_type = (type == 'b' ? "BAN" : type == 'e' ? "EXEMPT" : "INVITE");
  363. if (mask->added) {
  364. daysago(now, mask->added, s, sizeof(s));
  365. simple_snprintf(dates, sizeof(dates), "Created %s", s);
  366. if (mask->added < mask->lastactive) {
  367. strlcat(dates, ", ", sizeof(dates));
  368. strlcat(dates, "last used", sizeof(dates));
  369. strlcat(dates, " ", sizeof(dates));
  370. daysago(now, mask->lastactive, s, sizeof(s));
  371. strlcat(dates, s, sizeof(dates));
  372. }
  373. } else
  374. dates[0] = 0;
  375. if (mask->flags & MASKREC_PERM)
  376. strlcpy(s, "(perm)", sizeof(s));
  377. else {
  378. char s1[41] = "";
  379. days(mask->expire, now, s1, sizeof(s1));
  380. simple_snprintf(s, sizeof(s), "(expires %s)", s1);
  381. }
  382. if (mask->flags & MASKREC_STICKY)
  383. strlcat(s, " (sticky)", sizeof(s));
  384. /* always show mask on hubs */
  385. if (!chan || ischanmask(type, chan, mask->mask) || conf.bot->hub) {
  386. if (number >= 0) {
  387. dprintf(idx, " [%3d] %s %s\n", number, mask->mask, s);
  388. } else {
  389. dprintf(idx, "%s: %s %s\n", str_type, mask->mask, s);
  390. }
  391. } else if (show_inact) {
  392. if (number >= 0) {
  393. dprintf(idx, "! [%3d] %s %s\n", number, mask->mask, s);
  394. } else {
  395. dprintf(idx, "%s (inactive): %s %s\n", str_type, mask->mask, s);
  396. }
  397. } else
  398. return;
  399. dprintf(idx, " %s: %s\n", mask->user, mask->desc);
  400. if (dates[0])
  401. dprintf(idx, " %s\n", dates);
  402. }
  403. static void tell_masks(const char type, int idx, bool show_inact, char *match, bool all)
  404. {
  405. int k = 1;
  406. char *chname = NULL;
  407. const char *str_type = (type == 'b' ? "ban" : type == 'e' ? "exempt" : "invite");
  408. maskrec *global_masks = (type == 'b' ? global_bans : type == 'e' ? global_exempts : global_invites);
  409. struct chanset_t *chan = NULL;
  410. maskrec *mr = NULL;
  411. /* Was a channel given? */
  412. if (match && match[0]) {
  413. chname = newsplit(&match);
  414. if (chname[0] && (strchr(CHANMETA, chname[0]))) {
  415. chan = findchan_by_dname(chname);
  416. if (!chan) {
  417. dprintf(idx, "No such channel defined.\n");
  418. return;
  419. }
  420. } else
  421. match = chname;
  422. }
  423. if (all)
  424. chan = chanset;
  425. /* don't return here, we want to show global masks even if no chan */
  426. if (!chan && !(chan = findchan_by_dname(dcc[idx].u.chat->con_chan)) && !(chan = chanset))
  427. chan = NULL;
  428. while (chan) {
  429. get_user_flagrec(dcc[idx].user, &user, chan->dname, chan);
  430. if (privchan(user, chan, PRIV_OP)) {
  431. if (all) goto next;
  432. dprintf(idx, "No such channel defined.\n");
  433. return;
  434. } else if (!chk_op(user, chan)) {
  435. if (all) goto next;
  436. dprintf(idx, "You don't have access to view %ss on %s\n", str_type, chan->dname);
  437. return;
  438. }
  439. if (chan && show_inact)
  440. dprintf(idx, "Global %ss: (! = not active on %s)\n", str_type, chan->dname);
  441. else
  442. dprintf(idx, "Global %ss:\n", str_type);
  443. for (mr = global_masks; mr; mr = mr->next) {
  444. if (match && match[0]) {
  445. if ((wild_match(match, mr->mask)) ||
  446. (wild_match(match, mr->desc)) ||
  447. (wild_match(match, mr->user)))
  448. display_mask(type, idx, k, mr, chan, 1);
  449. k++;
  450. } else
  451. display_mask(type, idx, k++, mr, chan, show_inact);
  452. }
  453. if (chan) {
  454. maskrec *chan_masks = (type == 'b' ? chan->bans : type == 'e' ? chan->exempts : chan->invites);
  455. if (show_inact)
  456. dprintf(idx, "Channel %ss for %s: (! = not active on, * = not placed by bot)\n", str_type, chan->dname);
  457. else
  458. dprintf(idx, "Channel %ss for %s: (* = not placed by bot)\n", str_type, chan->dname);
  459. for (mr = chan_masks; mr; mr = mr->next) {
  460. if (match && match[0]) {
  461. if ((wild_match(match, mr->mask)) ||
  462. (wild_match(match, mr->desc)) ||
  463. (wild_match(match, mr->user)))
  464. display_mask(type, idx, k, mr, chan, 1);
  465. k++;
  466. } else
  467. display_mask(type, idx, k++, mr, chan, show_inact);
  468. }
  469. if (chan->ircnet_status & CHAN_ACTIVE) {
  470. masklist *ml = NULL;
  471. masklist *channel_list = (type == 'b' ? chan->channel.ban : type == 'e' ? chan->channel.exempt : chan->channel.invite);
  472. char s[UHOSTLEN] = "", *s2 = NULL, fill[256] = "";
  473. const char *s1;
  474. int min, sec;
  475. for (ml = channel_list; ml && ml->mask[0]; ml = ml->next) {
  476. if ((!u_equals_mask(global_masks, ml->mask)) &&
  477. (!u_equals_mask(chan_masks, ml->mask))) {
  478. strlcpy(s, ml->who, sizeof(s));
  479. s2 = s;
  480. s1 = splitnick(&s2);
  481. if (s1[0])
  482. simple_snprintf(fill, sizeof(fill), "%s (%s!%s)", ml->mask, s1, s2);
  483. else
  484. simple_snprintf(fill, sizeof(fill), "%s (server %s)", ml->mask, s2);
  485. if (ml->timer != 0) {
  486. min = (now - ml->timer) / 60;
  487. sec = (now - ml->timer) - (min * 60);
  488. simple_snprintf(s, sizeof(s), " (active %02d:%02d)", min, sec);
  489. strlcat(fill, s, sizeof(fill));
  490. }
  491. if (!match || !match[0] || wild_match(match, ml->mask))
  492. dprintf(idx, "* [%3d] %s\n", k, fill);
  493. k++;
  494. }
  495. }
  496. }
  497. }
  498. next:;
  499. if (!all)
  500. chan = NULL;
  501. else
  502. chan = chan->next;
  503. }
  504. if (k == 1)
  505. dprintf(idx, "(There are no %ss, permanent or otherwise.)\n", str_type);
  506. if ((!show_inact) && !(match && match[0]))
  507. dprintf(idx, "Use '%ss all' to see the total list.\n", str_type);
  508. }
  509. /* Write the ban lists and the ignore list to a file.
  510. */
  511. void write_bans(bd::Stream& stream, int idx)
  512. {
  513. if (global_ign)
  514. stream << bd::String::printf(IGNORE_NAME " - -\n");
  515. char *mask = NULL;
  516. for (struct igrec *i = global_ign; i; i = i->next) {
  517. mask = str_escape(i->igmask, ':', '\\');
  518. if (mask) {
  519. stream << bd::String::printf("- %s:%s%li:%s:%li:%s\n", mask,
  520. (i->flags & IGREC_PERM) ? "+" : "", (long) i->expire,
  521. i->user ? i->user : conf.bot->nick, (long) i->added,
  522. i->msg ? i->msg : "");
  523. free(mask);
  524. }
  525. }
  526. if (global_bans)
  527. stream << bd::String::printf(BAN_NAME " - -\n");
  528. maskrec *b = NULL;
  529. for (b = global_bans; b; b = b->next) {
  530. mask = str_escape(b->mask, ':', '\\');
  531. if (mask) {
  532. stream << bd::String::printf("- %s:%s%li%s:+%li:%li:%s:%s\n", mask,
  533. (b->flags & MASKREC_PERM) ? "+" : "", (long) b->expire,
  534. (b->flags & MASKREC_STICKY) ? "*" : "", (long) b->added,
  535. (long) b->lastactive, b->user ? b->user : conf.bot->nick,
  536. b->desc ? b->desc : "requested");
  537. free(mask);
  538. }
  539. }
  540. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  541. stream << bd::String::printf("::%s bans\n", chan->dname);
  542. for (b = chan->bans; b; b = b->next) {
  543. mask = str_escape(b->mask, ':', '\\');
  544. if (mask) {
  545. stream << bd::String::printf("- %s:%s%li%s:+%li:%li:%s:%s\n", mask,
  546. (b->flags & MASKREC_PERM) ? "+" : "", (long) b->expire,
  547. (b->flags & MASKREC_STICKY) ? "*" : "", (long) b->added,
  548. (long) b->lastactive, b->user ? b->user : conf.bot->nick,
  549. b->desc ? b->desc : "requested");
  550. free(mask);
  551. }
  552. }
  553. }
  554. }
  555. /* Write the exemptlists to a file.
  556. */
  557. void write_exempts(bd::Stream& stream, int idx)
  558. {
  559. if (global_exempts)
  560. stream << bd::String::printf(EXEMPT_NAME " - -\n");
  561. maskrec *e = NULL;
  562. char *mask = NULL;
  563. for (e = global_exempts; e; e = e->next) {
  564. mask = str_escape(e->mask, ':', '\\');
  565. if (mask) {
  566. stream << bd::String::printf("%s %s:%s%li%s:+%li:%li:%s:%s\n", "%", mask,
  567. (e->flags & MASKREC_PERM) ? "+" : "", (long) e->expire,
  568. (e->flags & MASKREC_STICKY) ? "*" : "", (long) e->added,
  569. (long) e->lastactive, e->user ? e->user : conf.bot->nick,
  570. e->desc ? e->desc : "requested");
  571. free(mask);
  572. }
  573. }
  574. for (struct chanset_t *chan = chanset;chan ;chan = chan->next) {
  575. stream << bd::String::printf("&&%s exempts\n", chan->dname);
  576. for (e = chan->exempts; e; e = e->next) {
  577. mask = str_escape(e->mask, ':', '\\');
  578. if (mask) {
  579. stream << bd::String::printf("%s %s:%s%li%s:+%li:%li:%s:%s\n","%", mask,
  580. (e->flags & MASKREC_PERM) ? "+" : "", (long) e->expire,
  581. (e->flags & MASKREC_STICKY) ? "*" : "", (long) e->added,
  582. (long) e->lastactive, e->user ? e->user : conf.bot->nick,
  583. e->desc ? e->desc : "requested");
  584. free(mask);
  585. }
  586. }
  587. }
  588. }
  589. /* Write the invitelists to a file.
  590. */
  591. void write_invites(bd::Stream& stream, int idx)
  592. {
  593. if (global_invites)
  594. stream << bd::String::printf(INVITE_NAME " - -\n");
  595. maskrec *ir = NULL;
  596. char *mask = NULL;
  597. for (ir = global_invites; ir; ir = ir->next) {
  598. mask = str_escape(ir->mask, ':', '\\');
  599. if (mask) {
  600. stream << bd::String::printf("@ %s:%s%li%s:+%li:%li:%s:%s\n", mask,
  601. (ir->flags & MASKREC_PERM) ? "+" : "", (long) ir->expire,
  602. (ir->flags & MASKREC_STICKY) ? "*" : "", (long) ir->added,
  603. (long) ir->lastactive, ir->user ? ir->user : conf.bot->nick,
  604. ir->desc ? ir->desc : "requested");
  605. free(mask);
  606. }
  607. }
  608. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  609. stream << bd::String::printf("$$%s invites\n", chan->dname);
  610. for (ir = chan->invites; ir; ir = ir->next) {
  611. mask = str_escape(ir->mask, ':', '\\');
  612. if (mask) {
  613. stream << bd::String::printf("@ %s:%s%li%s:+%li:%li:%s:%s\n", mask,
  614. (ir->flags & MASKREC_PERM) ? "+" : "", (long) ir->expire,
  615. (ir->flags & MASKREC_STICKY) ? "*" : "", (long) ir->added,
  616. (long) ir->lastactive, ir->user ? ir->user : conf.bot->nick,
  617. ir->desc ? ir->desc : "requested");
  618. free(mask);
  619. }
  620. }
  621. }
  622. }
  623. /* Write the channels to the userfile
  624. */
  625. static void write_chan(bd::Stream& stream, int idx, struct chanset_t* chan)
  626. {
  627. putlog(LOG_DEBUG, "*", "writing channel %s to userfile..", chan->dname);
  628. bool force_inactive = 0;
  629. stream << bd::String::printf("+ channel add %s { ", chan->dname);
  630. if (chan != chanset_default)
  631. stream << bd::String::printf("addedby %s addedts %li ", chan->added_by, (long)chan->added_ts);
  632. stream << channel_to_string(chan, force_inactive);
  633. stream << bd::String::printf("}\n");
  634. }
  635. bd::String channel_to_string(struct chanset_t* chan, bool force_inactive) {
  636. char w[1024] = "";
  637. get_mode_protect(chan, w, sizeof(w));
  638. return bd::String::printf("\
  639. chanmode { %s } groups { %s } bad-cookie %d manop %d mdop %d mop %d limit %d revenge %d ban-type %d \
  640. homechan-user %d \
  641. flood-chan %d:%d flood-bytes %d:%d flood-ctcp %d:%d flood-join %d:%d \
  642. flood-kick %d:%d flood-deop %d:%d flood-nick %d:%d flood-mjoin %d:%d \
  643. flood-mpub %d:%d flood-mbytes %d:%d flood-mctcp %d:%d \
  644. capslimit %d colorlimit %d closed-ban %d closed-invite %d closed-private %d closed-exempt %d ban-time %d \
  645. exempt-time %d invite-time %d voice-non-ident %d voice-moderate %d auto-delay %d \
  646. flood-exempt %d flood-lock-time %d knock %d fish-key { %s } \
  647. %cenforcebans %cdynamicbans %cuserbans %cbitch %cfloodban \
  648. %cprivate %ccycle %cinactive %cdynamicexempts %cuserexempts \
  649. %cdynamicinvites %cuserinvites %cnodesynch %cclosed %cvoice \
  650. %cfastop %cautoop %cbotbitch %cbackup %crbl %cvoicebitch %cprotect protect-backup %d %c%s",
  651. w,
  652. /* Chanchar template
  653. * temp,
  654. * also include temp %s in dprintf.
  655. */
  656. chan->groups && chan->groups->length() ? static_cast<bd::String>(chan->groups->join(" ")).c_str() : "",
  657. chan->bad_cookie,
  658. chan->manop,
  659. chan->mdop,
  660. chan->mop,
  661. chan->limitraise,
  662. chan->revenge,
  663. chan->ban_type,
  664. chan->homechan_user,
  665. chan->flood_pub_thr, chan->flood_pub_time,
  666. chan->flood_bytes_thr, chan->flood_bytes_time,
  667. chan->flood_ctcp_thr, chan->flood_ctcp_time,
  668. chan->flood_join_thr, chan->flood_join_time,
  669. chan->flood_kick_thr, chan->flood_kick_time,
  670. chan->flood_deop_thr, chan->flood_deop_time,
  671. chan->flood_nick_thr, chan->flood_nick_time,
  672. chan->flood_mjoin_thr, chan->flood_mjoin_time,
  673. chan->flood_mpub_thr, chan->flood_mpub_time,
  674. chan->flood_mbytes_thr, chan->flood_mbytes_time,
  675. chan->flood_mctcp_thr, chan->flood_mctcp_time,
  676. chan->capslimit,
  677. chan->colorlimit,
  678. chan->closed_ban,
  679. /* Chanint template
  680. * chan->temp,
  681. * also include temp %d in dprintf
  682. */
  683. chan->closed_invite,
  684. chan->closed_private,
  685. chan->closed_exempt_mode,
  686. chan->ban_time,
  687. chan->exempt_time,
  688. chan->invite_time,
  689. chan->voice_non_ident,
  690. chan->voice_moderate,
  691. chan->auto_delay,
  692. chan->flood_exempt_mode,
  693. chan->flood_lock_time,
  694. chan->knock_flags,
  695. chan->fish_key,
  696. PLSMNS(channel_enforcebans(chan)),
  697. PLSMNS(channel_dynamicbans(chan)),
  698. PLSMNS(!channel_nouserbans(chan)),
  699. PLSMNS(channel_bitch(chan)),
  700. PLSMNS(channel_floodban(chan)),
  701. PLSMNS(channel_privchan(chan)),
  702. PLSMNS(channel_cycle(chan)),
  703. force_inactive ? '+' : PLSMNS(channel_inactive(chan)),
  704. PLSMNS(channel_dynamicexempts(chan)),
  705. PLSMNS(!channel_nouserexempts(chan)),
  706. PLSMNS(channel_dynamicinvites(chan)),
  707. PLSMNS(!channel_nouserinvites(chan)),
  708. PLSMNS(channel_nodesynch(chan)),
  709. PLSMNS(channel_closed(chan)),
  710. PLSMNS(channel_voice(chan)),
  711. PLSMNS(channel_fastop(chan)),
  712. PLSMNS(channel_autoop(chan)),
  713. PLSMNS(channel_botbitch(chan)),
  714. PLSMNS(channel_backup(chan)),
  715. PLSMNS(channel_rbl(chan)),
  716. PLSMNS(channel_voicebitch(chan)),
  717. PLSMNS(channel_protect(chan)),
  718. chan->protect_backup,
  719. HAVE_TAKE ? PLSMNS(channel_take(chan)) : ' ',
  720. HAVE_TAKE ? "take " : " "
  721. /* Chanflag template
  722. * also include a %ctemp above.
  723. * PLSMNS(channel_temp(chan)),
  724. */
  725. );
  726. }
  727. /* Write the channels to the userfile
  728. */
  729. void write_chans(bd::Stream& stream, int idx, bool old)
  730. {
  731. putlog(LOG_DEBUG, "*", "Writing channels..");
  732. stream << bd::String::printf(CHANS_NAME " - -\n");
  733. /* FIXME: Remove after 1.2.15 */
  734. if (!old)
  735. write_chan(stream, idx, chanset_default);
  736. for (struct chanset_t *chan = chanset; chan; chan = chan->next)
  737. write_chan(stream, idx, chan);
  738. }
  739. /* FIXME: remove after 1.2.14 */
  740. /* Write the channels to the userfile
  741. */
  742. void write_chans_compat(bd::Stream& stream, int idx)
  743. {
  744. putlog(LOG_DEBUG, "*", "Writing channels..");
  745. stream << bd::String::printf(CHANS_NAME " - -\n");
  746. char w[1024] = "";
  747. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  748. char inactive = 0;
  749. putlog(LOG_DEBUG, "*", "writing channel %s to userfile..", chan->dname);
  750. get_mode_protect(chan, w, sizeof(w));
  751. inactive = PLSMNS(channel_inactive(chan));
  752. stream << bd::String::printf("\
  753. + channel add %s { chanmode { %s } addedby %s addedts %li \
  754. bad-cookie %d manop %d mdop %d mop %d limit %d \
  755. flood-chan %d:%d flood-ctcp %d:%d flood-join %d:%d \
  756. flood-kick %d:%d flood-deop %d:%d flood-nick %d:%d \
  757. caps-limit %d color-limit %d closed-ban %d closed-invite %d closed-private %d ban-time %d \
  758. exempt-time %d invite-time %d voice-non-ident %d auto-delay %d \
  759. %cenforcebans %cdynamicbans %cuserbans %cbitch \
  760. %cprivate %ccycle %cinactive %cdynamicexempts %cuserexempts \
  761. %cdynamicinvites %cuserinvites %cnodesynch %cclosed %cvoice \
  762. %cfastop %cautoop %cbotbitch %cbackup %c%s}\n",
  763. chan->dname,
  764. w,
  765. chan->added_by,
  766. (long)chan->added_ts,
  767. chan->bad_cookie,
  768. chan->manop,
  769. chan->mdop,
  770. chan->mop,
  771. chan->limitraise,
  772. chan->flood_pub_thr, chan->flood_pub_time,
  773. chan->flood_ctcp_thr, chan->flood_ctcp_time,
  774. chan->flood_join_thr, chan->flood_join_time,
  775. chan->flood_kick_thr, chan->flood_kick_time,
  776. chan->flood_deop_thr, chan->flood_deop_time,
  777. chan->flood_nick_thr, chan->flood_nick_time,
  778. chan->capslimit,
  779. chan->colorlimit,
  780. chan->closed_ban,
  781. chan->closed_invite,
  782. chan->closed_private,
  783. chan->ban_time,
  784. chan->exempt_time,
  785. chan->invite_time,
  786. chan->voice_non_ident,
  787. chan->auto_delay,
  788. PLSMNS(channel_enforcebans(chan)),
  789. PLSMNS(channel_dynamicbans(chan)),
  790. PLSMNS(!channel_nouserbans(chan)),
  791. PLSMNS(channel_bitch(chan)),
  792. PLSMNS(channel_privchan(chan)),
  793. PLSMNS(channel_cycle(chan)),
  794. inactive,
  795. PLSMNS(channel_dynamicexempts(chan)),
  796. PLSMNS(!channel_nouserexempts(chan)),
  797. PLSMNS(channel_dynamicinvites(chan)),
  798. PLSMNS(!channel_nouserinvites(chan)),
  799. PLSMNS(channel_nodesynch(chan)),
  800. PLSMNS(channel_closed(chan)),
  801. PLSMNS(channel_voice(chan)),
  802. PLSMNS(channel_fastop(chan)),
  803. PLSMNS(channel_autoop(chan)),
  804. PLSMNS(channel_botbitch(chan)),
  805. PLSMNS(channel_backup(chan)),
  806. HAVE_TAKE ? PLSMNS(channel_take(chan)) : ' ',
  807. HAVE_TAKE ? "take " : " "
  808. );
  809. }
  810. }
  811. void channels_writeuserfile(bd::Stream& stream, int old)
  812. {
  813. putlog(LOG_DEBUG, "@", "Writing channel/ban/exempt/invite entries.");
  814. if (old != 1)
  815. write_chans(stream, -1, old == 0 ? 0 : 1);
  816. else /* flood-* hacks */
  817. write_chans_compat(stream, -1);
  818. write_vars_and_cmdpass(stream, -1);
  819. write_bans(stream, -1);
  820. write_exempts(stream, -1);
  821. write_invites(stream, -1);
  822. }
  823. /* Expire mask originally set by `who' on `chan'?
  824. *
  825. * We might not want to expire masks in all cases, as other bots
  826. * often tend to immediately reset masks they've listed in their
  827. * internal ban list, making it quite senseless for us to remove
  828. * them in the first place.
  829. *
  830. * Returns 1 if a mask on `chan' by `who' may be expired and 0 if
  831. * not.
  832. */
  833. bool expired_mask(struct chanset_t *chan, char *who)
  834. {
  835. memberlist *m = NULL, *m2 = NULL;
  836. char buf[UHOSTLEN] = "", *sfrom = NULL;
  837. const char *snick = NULL;
  838. struct userrec *u = NULL;
  839. strlcpy(buf, who, sizeof(buf));
  840. sfrom = buf;
  841. snick = splitnick(&sfrom);
  842. if (!snick[0])
  843. return 1;
  844. m = ismember(chan, snick);
  845. if (!m)
  846. for (m2 = chan->channel.member; m2 && m2->nick[0]; m2 = m2->next)
  847. if (!strcasecmp(sfrom, m2->userhost)) {
  848. m = m2;
  849. break;
  850. }
  851. if (!m || !chan_hasop(m) || m->is_me)
  852. return 1;
  853. /* At this point we know the person/bot who set the mask is currently
  854. * present in the channel and has op.
  855. */
  856. if (m->user)
  857. u = m->user;
  858. else {
  859. simple_snprintf(buf, sizeof(buf), "%s!%s", m->nick, m->userhost);
  860. u = get_user_by_host(buf);
  861. }
  862. /* Do not expire masks set by bots. */
  863. if (u && u->bot)
  864. return 0;
  865. else
  866. return 1;
  867. }
  868. /* Check for expired timed-bans.
  869. */
  870. static void check_expired_mask(const char type)
  871. {
  872. maskrec *u = NULL, *u2 = NULL, *list = NULL;
  873. struct chanset_t *chan = NULL;
  874. masklist *b = NULL, *chanlist = NULL;
  875. const char *str_typing = (type == 'b' ? "banning" : type == 'e' ? "ban exempting" : "inviting");
  876. bool remove, match;
  877. list = (type == 'b' ? global_bans : type == 'e' ? global_exempts : global_invites);
  878. for (u = list; u; u = u2) {
  879. u2 = u->next;
  880. if (!(u->flags & MASKREC_PERM) && (now >= u->expire)) {
  881. putlog(LOG_MISC, "*", "No longer %s %s (expired)", str_typing, u->mask);
  882. if (!conf.bot->hub) {
  883. for (chan = chanset; chan; chan = chan->next) {
  884. remove = 1; /* hack for 'e' */
  885. if (type == 'e') {
  886. match = 0;
  887. b = chan->channel.ban;
  888. while (b->mask[0] && !match) {
  889. if (wild_match(b->mask, u->mask) || wild_match(u->mask, b->mask))
  890. match = 1;
  891. else
  892. b = b->next;
  893. }
  894. if (match) {
  895. putlog(LOG_MISC, chan->dname, "Exempt not expired on channel %s. Ban still set!", chan->dname);
  896. remove = 0;
  897. }
  898. }
  899. if (remove) {
  900. chanlist = (type == 'b' ? chan->channel.ban : type == 'e' ? chan->channel.exempt : chan->channel.invite);
  901. for (b = chanlist; b->mask[0]; b = b->next) {
  902. if (!rfc_casecmp(b->mask, u->mask) && expired_mask(chan, b->who) && b->timer != now) {
  903. add_mode(chan, '-', type, u->mask);
  904. b->timer = now;
  905. }
  906. }
  907. u_delmask(type, NULL, u->mask, 1);
  908. }
  909. }
  910. } else
  911. u_delmask(type, NULL, u->mask, 1);
  912. }
  913. }
  914. /* Check for specific channel-domain bans expiring */
  915. for (chan = chanset; chan; chan = chan->next) {
  916. list = (type == 'b' ? chan->bans : type == 'e' ? chan->exempts : chan->invites);
  917. for (u = list; u; u = u2) {
  918. u2 = u->next;
  919. if (!(u->flags & MASKREC_PERM) && (now >= u->expire)) {
  920. remove = 1;
  921. if (!conf.bot->hub && type == 'e') {
  922. match = 0;
  923. b = chan->channel.ban;
  924. while (b->mask[0] && !match) {
  925. if (wild_match(b->mask, u->mask) || wild_match(u->mask, b->mask))
  926. match = 1;
  927. else
  928. b = b->next;
  929. }
  930. if (match) {
  931. putlog(LOG_MISC, chan->dname, "Exempt not expired on channel %s. Ban still set!", chan->dname);
  932. remove = 0;
  933. }
  934. }
  935. if (remove) {
  936. putlog(LOG_MISC, "*", "No longer %s %s on %s (expired)", str_typing, u->mask, chan->dname);
  937. if (!conf.bot->hub) {
  938. chanlist = (type == 'b' ? chan->channel.ban : type == 'e' ? chan->channel.exempt : chan->channel.invite);
  939. for (b = chanlist; b->mask[0]; b = b->next) {
  940. if (!rfc_casecmp(b->mask, u->mask) && expired_mask(chan, b->who) && b->timer != now) {
  941. if (!conf.bot->hub)
  942. add_mode(chan, '-', type, u->mask);
  943. b->timer = now;
  944. }
  945. }
  946. }
  947. u_delmask(type, chan, u->mask, 1);
  948. }
  949. }
  950. }
  951. }
  952. }
  953. void check_expired_masks()
  954. {
  955. check_expired_mask('b');
  956. if (use_exempts)
  957. check_expired_mask('e');
  958. if (use_invites)
  959. check_expired_mask('I');
  960. }
  961. /* vim: set sts=2 sw=2 ts=8 et: */