channels.c 24 KB

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