channels.c 24 KB

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