channels.cc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. * channels.c -- part of channels.mod
  22. * support for channels within the bot
  23. *
  24. */
  25. #define MAKING_CHANNELS
  26. #include "src/common.h"
  27. #include "src/mod/share.mod/share.h"
  28. #include "src/mod/irc.mod/irc.h"
  29. #include "src/mod/server.mod/server.h"
  30. #include "src/chanprog.h"
  31. #include "src/egg_timer.h"
  32. #include "src/misc.h"
  33. #include "src/main.h"
  34. #include "src/color.h"
  35. #include "src/userrec.h"
  36. #include "src/users.h"
  37. #include "src/set.h"
  38. #include "src/rfc1459.h"
  39. #include "src/match.h"
  40. #include "src/settings.h"
  41. #include "src/tandem.h"
  42. #include "src/botnet.h"
  43. #include "src/botmsg.h"
  44. #include "src/net.h"
  45. #include "src/binds.h"
  46. #include "src/cmds.h"
  47. #include <bdlib/src/String.h>
  48. #include <sys/stat.h>
  49. static bool use_info = 1;
  50. static char glob_chanmode[64] = "nt"; /* Default chanmode (drummer,990731) */
  51. static interval_t global_ban_time;
  52. static interval_t global_exempt_time;
  53. static interval_t global_invite_time;
  54. static char *lastdeletedmask = NULL;
  55. static int killed_bots = 0;
  56. #include "channels.h"
  57. #include "cmdschan.cc"
  58. #include "chanmisc.cc"
  59. #include "userchan.cc"
  60. /* This will close channels if the HUB:leaf count is skewed from config setting */
  61. static void
  62. check_should_close()
  63. {
  64. int H = close_threshold.count, L = close_threshold.time;
  65. if ((H <= 0) || (L <= 0))
  66. return;
  67. int hc = 1, lc = 0;
  68. struct userrec *u = NULL;
  69. for (tand_t *bot = tandbot; bot; bot = bot->next) {
  70. if ((u = get_user_by_handle(userlist, bot->bot))) {
  71. if (bot_hublevel(u) < 999)
  72. hc++;
  73. else
  74. lc++;
  75. }
  76. }
  77. if ((hc >= H) && (lc <= L)) {
  78. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  79. if (!channel_closed(chan)) {
  80. do_chanset(NULL, chan, "+closed chanmode +stni", DO_LOCAL | DO_NET);
  81. #ifdef G_BACKUP
  82. chan->channel.backup_time = now + 30;
  83. #endif /* G_BACKUP */
  84. }
  85. }
  86. }
  87. }
  88. static void got_cset(char *botnick, char *code, char *par)
  89. {
  90. if (!par || !par[0])
  91. return;
  92. bool all = 0, isdefault = 0;
  93. char *chname = NULL;
  94. struct chanset_t *chan = NULL;
  95. if (par[0] == '*' && par[1] == ' ') {
  96. all = 1;
  97. newsplit(&par);
  98. } else {
  99. chname = newsplit(&par);
  100. if (!strcasecmp(chname, "default"))
  101. isdefault = 1;
  102. else if (!strchr(CHANMETA, chname[0])) {
  103. putlog(LOG_ERROR, "*", "Got bad cset: bot: %s code: %s par: %s %s", botnick, code, chname, par);
  104. return;
  105. }
  106. if (isdefault)
  107. chan = chanset_default;
  108. else if (!(chan = findchan_by_dname(chname)))
  109. return;
  110. }
  111. if (all)
  112. chan = NULL;
  113. do_chanset(NULL, chan, par, DO_LOCAL);
  114. }
  115. /* returns 1 if botn is in bots */
  116. static int
  117. parsebots(char *bots, char *botn) {
  118. if (!strcmp(bots, "*")) {
  119. return 1;
  120. } else {
  121. char *list = strdup(bots), *bot = strtok(list, ",");
  122. while(bot && *bot) {
  123. if (!strcasecmp(bot, botn))
  124. return 1;
  125. bot = strtok((char*) NULL, ",");
  126. }
  127. free(list);
  128. }
  129. return 0;
  130. }
  131. static void got_cpart(char *botnick, char *code, char *par)
  132. {
  133. if (!par[0])
  134. return;
  135. char *chname = newsplit(&par);
  136. struct chanset_t *chan = NULL;
  137. if (!(chan = findchan_by_dname(chname)))
  138. return;
  139. char *bots = newsplit(&par);
  140. int match = 0;
  141. /* if bots is '*' just remove_channel */
  142. if (!strcmp(bots, "*"))
  143. match = 0;
  144. else
  145. match = parsebots(bots, conf.bot->nick);
  146. if (match)
  147. do_chanset(NULL, chan, "+inactive", DO_LOCAL);
  148. else
  149. remove_channel(chan);
  150. if (conf.bot->hub)
  151. write_userfile(-1);
  152. }
  153. void rcmd_chans(char *fbot, char *fhand, char *fidx) {
  154. if (conf.bot->hub)
  155. return;
  156. struct chanset_t *chan = NULL;
  157. char buf[1024] = "", reply[1024] = "";
  158. if (server_online) {
  159. for (chan = chanset; chan; chan = chan->next) {
  160. if (!channel_active(chan) && (shouldjoin(chan) || chan->channel.jointime)) {
  161. if (buf[0])
  162. strlcat(buf, " ", sizeof(buf));
  163. strlcat(buf, chan->dname, sizeof(buf));
  164. }
  165. }
  166. if (buf[0])
  167. simple_snprintf(reply, sizeof(reply), "[%s] I am not in: %s", cursrvname, buf);
  168. } else
  169. simple_snprintf(reply, sizeof(reply), "I am not online.");
  170. if (reply[0])
  171. botnet_send_cmdreply(conf.bot->nick, fbot, fhand, fidx, reply);
  172. }
  173. static void got_cjoin(char *botnick, char *code, char *par)
  174. {
  175. if (!par[0])
  176. return;
  177. char *chname = newsplit(&par), *options = NULL;
  178. struct chanset_t *chan = findchan_by_dname(chname);
  179. int match = 0;
  180. if (conf.bot->hub) {
  181. newsplit(&par); /* hubs ignore the botmatch param */
  182. options = par;
  183. } else {
  184. /* ALL hubs should add the channel, leaf should check the list for a match */
  185. bool inactive = 0;
  186. char *bots = newsplit(&par);
  187. match = parsebots(bots, conf.bot->nick);
  188. if (strstr(par, "+inactive"))
  189. inactive = 1;
  190. if (chan && !match)
  191. return;
  192. if (!match) {
  193. size_t size = strlen(par) + 12 + 1;
  194. options = (char *) my_calloc(1, size);
  195. simple_snprintf(options, size, "%s +inactive", par);
  196. } else if (match && chan && !shouldjoin(chan)) {
  197. if (!inactive)
  198. do_chanset(NULL, chan, "-inactive", DO_LOCAL);
  199. return;
  200. } else
  201. options = par;
  202. }
  203. if (chan)
  204. return;
  205. sdprintf("OPTIONS: %s", options);
  206. char result[RESULT_LEN] = "";
  207. if (channel_add(result, chname, options) == ERROR) /* drummer */
  208. putlog(LOG_BOTS, "@", "Invalid channel or channel options from %s for %s: %s", botnick, chname, result);
  209. if (conf.bot->hub)
  210. write_userfile(-1);
  211. if (!match && !conf.bot->hub)
  212. free(options);
  213. }
  214. static void got_cycle(char *botnick, char *code, char *par)
  215. {
  216. if (!par[0])
  217. return;
  218. char *chname = newsplit(&par);
  219. struct chanset_t *chan = NULL;
  220. if (!(chan = findchan_by_dname(chname)))
  221. return;
  222. interval_t delay = 10;
  223. if (par[0])
  224. delay = atoi(newsplit(&par));
  225. do_chanset(NULL, chan, "+inactive", DO_LOCAL);
  226. dprintf(DP_SERVER, "PART %s\n", chan->name);
  227. chan->channel.jointime = ((now + delay) - server_lag); /* rejoin in 10 seconds */
  228. }
  229. static void got_down(char *botnick, char *code, char *par)
  230. {
  231. if (!par[0])
  232. return;
  233. char *chname = newsplit(&par);
  234. struct chanset_t *chan = NULL;
  235. if (!(chan = findchan_by_dname(chname)))
  236. return;
  237. chan->channel.no_op = (now + 10);
  238. add_mode(chan, '-', 'o', botname);
  239. }
  240. static void got_role(char *botnick, char *code, char *par)
  241. {
  242. role = atoi(newsplit(&par));
  243. putlog(LOG_DEBUG, "@", "Got role index %d", role);
  244. }
  245. void got_kl(char *botnick, char *code, char *par)
  246. {
  247. killed_bots++;
  248. if (kill_threshold && (killed_bots == kill_threshold)) {
  249. for (struct chanset_t *ch = chanset; ch; ch = ch->next)
  250. do_chanset(NULL, ch, "+closed +bitch +backup", DO_LOCAL | DO_NET);
  251. /* FIXME: we should randomize nick here ... */
  252. }
  253. }
  254. static void rebalance_roles()
  255. {
  256. struct bot_addr *ba = NULL;
  257. int r[5] = { 0, 0, 0, 0, 0 };
  258. unsigned int hNdx, lNdx, i;
  259. char tmp[10] = "";
  260. for (i = 0; i < (unsigned) dcc_total; i++) {
  261. if (dcc[i].type && dcc[i].user && dcc[i].user->bot && bot_hublevel(dcc[i].user) == 999) {
  262. ba = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, dcc[i].user);
  263. if (ba && (ba->roleid > 0) && (ba->roleid < 5))
  264. r[(ba->roleid - 1)]++;
  265. }
  266. }
  267. /*
  268. Find high & low
  269. while (high-low) > 2
  270. move from highNdx to lowNdx
  271. */
  272. hNdx = 0;
  273. lNdx = 0;
  274. for (i = 1; i <= 4; i++) {
  275. if (r[i] < r[lNdx])
  276. lNdx = i;
  277. if (r[i] > r[hNdx])
  278. hNdx = i;
  279. }
  280. while (r[hNdx] - r[lNdx] >= 2) {
  281. for (i = 0; i < (unsigned) dcc_total; i++) {
  282. if (dcc[i].type && dcc[i].user && dcc[i].user->bot && bot_hublevel(dcc[i].user) == 999) {
  283. ba = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, dcc[i].user);
  284. if (ba && (ba->roleid == (hNdx + 1))) {
  285. ba->roleid = lNdx + 1;
  286. simple_snprintf(tmp, sizeof(tmp), "rl %d", lNdx + 1);
  287. putbot(dcc[i].nick, tmp);
  288. }
  289. }
  290. }
  291. r[hNdx]--;
  292. r[lNdx]++;
  293. hNdx = 0;
  294. lNdx = 0;
  295. for (i = 1; i <= 4; i++) {
  296. if (r[i] < r[lNdx])
  297. lNdx = i;
  298. if (r[i] > r[hNdx])
  299. hNdx = i;
  300. }
  301. }
  302. }
  303. static int
  304. check_slowjoinpart(struct chanset_t *chan)
  305. {
  306. /* slowpart */
  307. if (chan->channel.parttime && (chan->channel.parttime < now)) {
  308. chan->channel.parttime = 0;
  309. dprintf(DP_MODE, "PART %s\n", chan->name);
  310. if (chan) /* this should NOT be necesary, but some unforseen bug requires it.. */
  311. remove_channel(chan);
  312. return 1; /* if we keep looping, we'll segfault. */
  313. /* slowjoin */
  314. } else if ((chan->channel.jointime) && (chan->channel.jointime < now)) {
  315. chan->status &= ~CHAN_INACTIVE;
  316. chan->channel.jointime = 0;
  317. if (shouldjoin(chan))
  318. join_chan(chan);
  319. } else if (channel_closed(chan)) {
  320. enforce_closed(chan);
  321. }
  322. return 0;
  323. }
  324. static void
  325. check_limitraise(struct chanset_t *chan) {
  326. /* only check every other time for now */
  327. chan->checklimit++;
  328. if (chan->checklimit == 2) {
  329. chan->checklimit = 0;
  330. if (chan->limitraise && dolimit(chan))
  331. raise_limit(chan);
  332. }
  333. }
  334. static void
  335. channels_timers()
  336. {
  337. static int cnt = 0;
  338. struct chanset_t *chan_n = NULL, *chan = NULL;
  339. bool reset = 0;
  340. cnt += 10; /* function is called every 10 seconds */
  341. for (chan = chanset; chan; chan = chan_n) {
  342. chan_n = chan->next;
  343. if ((cnt % 10) == 0) {
  344. /* 10 seconds */
  345. if (!conf.bot->hub && check_slowjoinpart(chan)) /* if 1 is returned, chan was removed. */
  346. continue;
  347. }
  348. if ((cnt % 60) == 0) {
  349. /* 60 seconds */
  350. reset = 1;
  351. if (!conf.bot->hub)
  352. check_limitraise(chan);
  353. }
  354. }
  355. if (reset)
  356. cnt = 0;
  357. }
  358. static void got_sj(int idx, char *code, char *par)
  359. {
  360. struct chanset_t *chan = findchan_by_dname(newsplit(&par));
  361. if (chan) {
  362. if (conf.bot->hub) {
  363. chan->status &= ~CHAN_INACTIVE;
  364. write_userfile(-1);
  365. } else
  366. chan->channel.jointime = ((atoi(par) + now) - server_lag);
  367. }
  368. }
  369. static void got_sp(int idx, char *code, char *par)
  370. {
  371. struct chanset_t *chan = findchan_by_dname(newsplit(&par));
  372. if (chan) {
  373. if (conf.bot->hub) {
  374. remove_channel(chan);
  375. write_userfile(-1);
  376. } else
  377. chan->channel.parttime = ((atoi(par) + now) - server_lag);
  378. }
  379. }
  380. #ifdef no
  381. /* got_jn
  382. * We get this when a bot is opped in a +take chan
  383. * we are to set -inactive, jointime = 0, and join.
  384. */
  385. static void got_jn(int idx, char *code, char *par)
  386. {
  387. char *chname = newsplit(&par);
  388. if (!chname || !chname[0])
  389. return;
  390. struct chanset_t *chan = NULL;
  391. if (!(chan = findchan_by_dname(chname))) return;
  392. if (chan->channel.jointime && channel_inactive(chan)) {
  393. chan->status &= ~CHAN_INACTIVE;
  394. chan->channel.jointime = 0;
  395. if (!conf.bot->hub && shouldjoin(chan))
  396. join_chan(chan);
  397. }
  398. }
  399. #endif
  400. static void set_mode_protect(struct chanset_t *chan, char *set)
  401. {
  402. int i, pos = 1;
  403. char *s = NULL, *s1 = NULL;
  404. /* Clear old modes */
  405. chan->mode_mns_prot = chan->mode_pls_prot = 0;
  406. chan->limit_prot = 0;
  407. chan->key_prot[0] = 0;
  408. for (s = newsplit(&set); *s; s++) {
  409. i = 0;
  410. switch (*s) {
  411. case '+':
  412. pos = 1;
  413. break;
  414. case '-':
  415. pos = 0;
  416. break;
  417. case 'i':
  418. i = CHANINV;
  419. break;
  420. case 'p':
  421. i = CHANPRIV;
  422. break;
  423. case 's':
  424. i = CHANSEC;
  425. break;
  426. case 'm':
  427. i = CHANMODER;
  428. break;
  429. case 'c':
  430. i = CHANNOCLR;
  431. break;
  432. case 'C':
  433. i = CHANNOCTCP;
  434. break;
  435. case 'R':
  436. i = CHANREGON;
  437. break;
  438. case 'M':
  439. i = CHANMODR;
  440. break;
  441. case 'r':
  442. i = CHANLONLY;
  443. break;
  444. case 't':
  445. i = CHANTOPIC;
  446. break;
  447. case 'n':
  448. i = CHANNOMSG;
  449. break;
  450. case 'a':
  451. i = CHANANON;
  452. break;
  453. case 'q':
  454. i = CHANQUIET;
  455. break;
  456. case 'l':
  457. i = CHANLIMIT;
  458. chan->limit_prot = 0;
  459. if (pos) {
  460. s1 = newsplit(&set);
  461. if (s1[0] && !chan->limitraise)
  462. chan->limit_prot = atoi(s1);
  463. }
  464. break;
  465. case 'k':
  466. i = CHANKEY;
  467. chan->key_prot[0] = 0;
  468. if (pos) {
  469. s1 = newsplit(&set);
  470. if (s1[0])
  471. strlcpy(chan->key_prot, s1, sizeof(chan->key_prot));
  472. }
  473. break;
  474. }
  475. if (i) {
  476. if (pos) {
  477. chan->mode_pls_prot |= i;
  478. chan->mode_mns_prot &= ~i;
  479. } else {
  480. chan->mode_pls_prot &= ~i;
  481. chan->mode_mns_prot |= i;
  482. }
  483. }
  484. }
  485. /* Prevents a +s-p +p-s flood (fixed by drummer) */
  486. if (chan->mode_pls_prot & CHANSEC)
  487. chan->mode_pls_prot &= ~CHANPRIV;
  488. if (chan->mode_mns_prot & CHANPRIV && chan->closed_private)
  489. chan->closed_private = 0;
  490. if (chan->mode_mns_prot & CHANINV && chan->closed_invite)
  491. chan->closed_invite = 0;
  492. if (chan->mode_mns_prot & CHANMODER && chan->voice_moderate)
  493. chan->voice_moderate = 0;
  494. }
  495. static void get_mode_protect(struct chanset_t *chan, char *s, size_t ssiz)
  496. {
  497. char *p = s, s1[121] = "";
  498. int tst;
  499. for (int i = 0; i < 2; i++) {
  500. if (i == 0) {
  501. tst = chan->mode_pls_prot;
  502. if ((tst) || (chan->limit_prot != 0) || (chan->key_prot[0]))
  503. *p++ = '+';
  504. if (chan->limit_prot != 0) {
  505. *p++ = 'l';
  506. simple_sprintf(&s1[strlen(s1)], "%d ", chan->limit_prot);
  507. }
  508. if (chan->key_prot[0]) {
  509. *p++ = 'k';
  510. simple_sprintf(&s1[strlen(s1)], "%s ", chan->key_prot);
  511. }
  512. } else {
  513. tst = chan->mode_mns_prot;
  514. if (tst)
  515. *p++ = '-';
  516. if (tst & CHANKEY)
  517. *p++ = 'k';
  518. if (tst & CHANLIMIT)
  519. *p++ = 'l';
  520. }
  521. if (tst & CHANINV)
  522. *p++ = 'i';
  523. if (tst & CHANPRIV)
  524. *p++ = 'p';
  525. if (tst & CHANSEC)
  526. *p++ = 's';
  527. if (tst & CHANMODER)
  528. *p++ = 'm';
  529. if (tst & CHANNOCLR)
  530. *p++ = 'c';
  531. if (tst & CHANNOCTCP)
  532. *p++ = 'C';
  533. if (tst & CHANREGON)
  534. *p++ = 'R';
  535. if (tst & CHANMODR)
  536. *p++ = 'M';
  537. if (tst & CHANLONLY)
  538. *p++ = 'r';
  539. if (tst & CHANTOPIC)
  540. *p++ = 't';
  541. if (tst & CHANNOMSG)
  542. *p++ = 'n';
  543. if (tst & CHANANON)
  544. *p++ = 'a';
  545. if (tst & CHANQUIET)
  546. *p++ = 'q';
  547. }
  548. *p = 0;
  549. if (s1[0]) {
  550. s1[strlen(s1) - 1] = 0;
  551. strlcat(s, " ", ssiz);
  552. strlcat(s, s1, ssiz);
  553. }
  554. }
  555. /* Returns true if this is one of the channel masks
  556. */
  557. bool ismodeline(masklist *m, const char *username)
  558. {
  559. for (; m && m->mask[0]; m = m->next)
  560. if (!rfc_casecmp(m->mask, username))
  561. return 1;
  562. return 0;
  563. }
  564. /* Returns true if user matches one of the masklist -- drummer
  565. */
  566. bool ismasked(masklist *m, const char *username)
  567. {
  568. for (; m && m->mask[0]; m = m->next)
  569. if (wild_match(m->mask, (char *) username))
  570. return 1;
  571. return 0;
  572. }
  573. /* Unlink chanset element from chanset list.
  574. */
  575. static inline bool chanset_unlink(struct chanset_t *chan)
  576. {
  577. struct chanset_t *c_old = NULL;
  578. for (struct chanset_t *c = chanset; c; c_old = c, c = c->next) {
  579. if (c == chan) {
  580. if (c_old)
  581. c_old->next = c->next;
  582. else
  583. chanset = c->next;
  584. return 1;
  585. }
  586. }
  587. return 0;
  588. }
  589. /* Completely removes a channel.
  590. *
  591. * This includes the removal of all channel-bans, -exempts and -invites, as
  592. * well as all user flags related to the channel.
  593. */
  594. void remove_channel(struct chanset_t *chan)
  595. {
  596. if (chan != chanset_default) {
  597. irc_log(chan, "Parting");
  598. /* Remove the channel from the list, so that noone can pull it
  599. away from under our feet during the check_part() call. */
  600. chanset_unlink(chan);
  601. /* Using chan->name is important here, especially for !chans <cybah> */
  602. if (!conf.bot->hub && shouldjoin(chan) && chan->name[0])
  603. dprintf(DP_SERVER, "PART %s\n", chan->name);
  604. clear_channel(chan, 0);
  605. noshare = 1;
  606. /* Remove channel-bans */
  607. while (chan->bans)
  608. u_delmask('b', chan, chan->bans->mask, 1);
  609. /* Remove channel-exempts */
  610. while (chan->exempts)
  611. u_delmask('e', chan, chan->exempts->mask, 1);
  612. /* Remove channel-invites */
  613. while (chan->invites)
  614. u_delmask('I', chan, chan->invites->mask, 1);
  615. /* Remove channel specific user flags */
  616. user_del_chan(chan->dname);
  617. noshare = 0;
  618. }
  619. free(chan->channel.key);
  620. if (chan->key)
  621. free(chan->key);
  622. if (chan->rmkey)
  623. free(chan->rmkey);
  624. if (chan->groups) {
  625. delete(chan->groups);
  626. }
  627. delete chan->channel.floodtime;
  628. delete chan->channel.floodnum;
  629. free(chan);
  630. }
  631. /* Bind this to chon and *if* the users console channel == ***
  632. * then set it to a specific channel
  633. */
  634. static int channels_chon(char *handle, int idx)
  635. {
  636. struct flag_record fr = {FR_CHAN | FR_ANYWH | FR_GLOBAL, 0, 0, 0 };
  637. int find;
  638. bool found = 0;
  639. struct chanset_t *chan = chanset;
  640. if (dcc[idx].type == &DCC_CHAT) {
  641. if (!findchan_by_dname(dcc[idx].u.chat->con_chan) &&
  642. ((dcc[idx].u.chat->con_chan[0] != '*') ||
  643. (dcc[idx].u.chat->con_chan[1] != 0))) {
  644. get_user_flagrec(dcc[idx].user, &fr, NULL);
  645. if (glob_op(fr))
  646. found = 1;
  647. if (chan_owner(fr))
  648. find = USER_OWNER;
  649. else if (chan_master(fr))
  650. find = USER_MASTER;
  651. else
  652. find = USER_OP;
  653. fr.match = FR_CHAN;
  654. while (chan && !found) {
  655. get_user_flagrec(dcc[idx].user, &fr, chan->dname, chan);
  656. if (fr.chan & find)
  657. found = 1;
  658. else
  659. chan = chan->next;
  660. }
  661. if (!chan)
  662. chan = chanset;
  663. struct chat_info dummy;
  664. if (chan)
  665. strlcpy(dcc[idx].u.chat->con_chan, chan->dname, sizeof(dummy.con_chan));
  666. else
  667. strlcpy(dcc[idx].u.chat->con_chan, "*", 2);
  668. }
  669. }
  670. return 0;
  671. }
  672. static cmd_t my_chon[] =
  673. {
  674. {"*", "", (Function) channels_chon, "channels:chon", 0},
  675. {NULL, NULL, NULL, NULL, 0}
  676. };
  677. void channels_report(int idx, int details)
  678. {
  679. int i;
  680. char s[1024] = "", s2[100] = "";
  681. struct flag_record fr = {FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  682. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  683. if (idx != DP_STDOUT)
  684. get_user_flagrec(dcc[idx].user, &fr, chan->dname, chan);
  685. if (!privchan(fr, chan, PRIV_OP) && ((idx == DP_STDOUT) || glob_master(fr) || chan_master(fr))) {
  686. s[0] = 0;
  687. if (chan_bitch(chan))
  688. strlcat(s, "bitch, ", sizeof(s));
  689. if (s[0])
  690. s[strlen(s) - 2] = 0;
  691. if (!s[0])
  692. strlcpy(s, "lurking", sizeof(s));
  693. get_mode_protect(chan, s2, sizeof(s2));
  694. if (channel_closed(chan)) {
  695. if (chan->closed_invite)
  696. strlcat(s2, "i", sizeof(s2));
  697. if (chan->closed_private)
  698. strlcat(s2, "p", sizeof(s2));
  699. }
  700. if (channel_voice(chan) && chan->voice_moderate) {
  701. strlcat(s2, "m", sizeof(s2));
  702. }
  703. if (conf.bot->hub || shouldjoin(chan)) {
  704. if (channel_active(chan)) {
  705. /* If it's a !chan, we want to display it's unique name too <cybah> */
  706. if (chan->dname[0]=='!') {
  707. dprintf(idx, " %-20s: %2d member%s enforcing \"%s\" (%s), "
  708. "unique name %s\n", chan->dname, chan->channel.members,
  709. (chan->channel.members==1) ? "," : "s,", s2, s, chan->name);
  710. } else {
  711. dprintf(idx, " %-20s: %2d member%s enforcing \"%s\" (%s)\n",
  712. chan->dname, chan->channel.members,
  713. chan->channel.members == 1 ? "," : "s,", s2, s);
  714. }
  715. } else {
  716. if (!conf.bot->hub)
  717. dprintf(idx, " %-20s: (%s), enforcing \"%s\" (%s)\n", chan->dname,
  718. channel_pending(chan) ? "pending" : "not on channel", s2, s);
  719. else
  720. dprintf(idx, " %-20s: (%s), enforcing \"%s\" (%s)\n", chan->dname,
  721. "limbo", s2, s);
  722. }
  723. } else {
  724. dprintf(idx, " %-20s: channel is set +inactive\n",
  725. chan->dname);
  726. }
  727. if (details) {
  728. s[0] = 0;
  729. i = 0;
  730. i += my_strcpy(s + i, "dynamic ");
  731. if (channel_enforcebans(chan))
  732. i += my_strcpy(s + i, "enforcebans ");
  733. if (channel_dynamicbans(chan))
  734. i += my_strcpy(s + i, "dynamicbans ");
  735. if (!channel_nouserbans(chan))
  736. i += my_strcpy(s + i, "userbans ");
  737. if (channel_bitch(chan))
  738. i += my_strcpy(s + i, "bitch ");
  739. if (channel_secret(chan))
  740. i += my_strcpy(s + i, "secret ");
  741. if (channel_cycle(chan))
  742. i += my_strcpy(s + i, "cycle ");
  743. if (channel_dynamicexempts(chan))
  744. i += my_strcpy(s + i, "dynamicexempts ");
  745. if (!channel_nouserexempts(chan))
  746. i += my_strcpy(s + i, "userexempts ");
  747. if (channel_dynamicinvites(chan))
  748. i += my_strcpy(s + i, "dynamicinvites ");
  749. if (!channel_nouserinvites(chan))
  750. i += my_strcpy(s + i, "userinvites ");
  751. if (!shouldjoin(chan))
  752. i += my_strcpy(s + i, "inactive ");
  753. if (channel_nodesynch(chan))
  754. i += my_strcpy(s + i, "nodesynch ");
  755. if (channel_closed(chan))
  756. i += my_strcpy(s + i, "closed ");
  757. if (HAVE_TAKE && channel_take(chan))
  758. i += my_strcpy(s + i, "take ");
  759. if (channel_voice(chan))
  760. i += my_strcpy(s + i, "voice ");
  761. if (channel_autoop(chan))
  762. i += my_strcpy(s + i, "autoop ");
  763. if (channel_rbl(chan))
  764. i += my_strcpy(s + i, "rbl ");
  765. if (channel_voicebitch(chan))
  766. i += my_strcpy(s + i, "voicebitch ");
  767. if (channel_protect(chan))
  768. i += my_strcpy(s + i, "protect ");
  769. /* Chanflag template
  770. * if (channel_temp(chan))
  771. * i += my_strcpy(s + i, "temp ");
  772. */
  773. if (channel_botbitch(chan))
  774. i += my_strcpy(s + i, "botbitch ");
  775. if (channel_backup(chan))
  776. i += my_strcpy(s + i, "backup ");
  777. if (channel_fastop(chan))
  778. i += my_strcpy(s + i, "fastop ");
  779. if (channel_privchan(chan))
  780. i += my_strcpy(s + i, "private ");
  781. dprintf(idx, " Options: %s\n", s);
  782. if (chan->limitraise)
  783. dprintf(idx, " Raising limit +%d every 2 minutes\n", chan->limitraise);
  784. dprintf(idx, " Bans last %d mins.\n", chan->ban_time);
  785. dprintf(idx, " Exemptions last %d mins.\n", chan->exempt_time);
  786. dprintf(idx, " Invitations last %d mins.\n", chan->invite_time);
  787. }
  788. }
  789. }
  790. }
  791. cmd_t channels_bot[] = {
  792. {"cjoin", "", (Function) got_cjoin, NULL, 0},
  793. {"cpart", "", (Function) got_cpart, NULL, 0},
  794. {"cset", "", (Function) got_cset, NULL, 0},
  795. {"cycle", "", (Function) got_cycle, NULL, LEAF},
  796. {"down", "", (Function) got_down, NULL, LEAF},
  797. {"rl", "", (Function) got_role, NULL, 0},
  798. {"kl", "", (Function) got_kl, NULL, 0},
  799. {"sj", "", (Function) got_sj, NULL, 0},
  800. {"sp", "", (Function) got_sp, NULL, 0},
  801. // {"jn", "", (Function) got_jn, NULL, 0},
  802. /*
  803. #ifdef HUB
  804. {"o1", "", (Function) got_o1, NULL, 0},
  805. {"kl", "", (Function) got_kl, NULL, 0},
  806. #endif
  807. {"ltp", "", (Function) got_locktopic, NULL, 0},
  808. */
  809. {NULL, NULL, NULL, NULL, 0}
  810. };
  811. void channels_init()
  812. {
  813. timer_create_secs(60, "check_expired_masks", (Function) check_expired_masks);
  814. if (conf.bot->hub) {
  815. timer_create_secs(30, "rebalance_roles", (Function) rebalance_roles);
  816. timer_create_secs(30, "check_should_close", (Function) check_should_close);
  817. #ifdef G_BACKUP
  818. timer_create_secs(30, "check_should_backup", (Function) check_should_backup);
  819. #endif /* G_BACKUP */
  820. }
  821. timer_create_secs(10, "channels_timers", (Function) channels_timers);
  822. add_builtins("dcc", C_dcc_channels);
  823. add_builtins("bot", channels_bot);
  824. add_builtins("chon", my_chon);
  825. }
  826. /* vim: set sts=2 sw=2 ts=8 et: */