channels.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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) && !channel_active(chan) && !channel_joining(chan)) {
  332. dprintf(DP_MODE, "JOIN %s %s\n", chan->dname, chan->key_prot);
  333. chan->ircnet_status |= CHAN_JOINING;
  334. }
  335. } else if (channel_closed(chan)) {
  336. enforce_closed(chan);
  337. }
  338. return 0;
  339. }
  340. static void
  341. check_limitraise(struct chanset_t *chan) {
  342. /* only check every other time for now */
  343. chan->checklimit++;
  344. if (chan->checklimit == 2) {
  345. chan->checklimit = 0;
  346. if (chan->limitraise && dolimit(chan))
  347. raise_limit(chan);
  348. }
  349. }
  350. static void
  351. channels_timers()
  352. {
  353. static int cnt = 0;
  354. struct chanset_t *chan_n = NULL, *chan = NULL;
  355. bool reset = 0;
  356. cnt += 10; /* function is called every 10 seconds */
  357. for (chan = chanset; chan; chan = chan_n) {
  358. chan_n = chan->next;
  359. if ((cnt % 10) == 0) {
  360. /* 10 seconds */
  361. if (!conf.bot->hub && check_slowjoinpart(chan)) /* if 1 is returned, chan was removed. */
  362. continue;
  363. }
  364. if ((cnt % 60) == 0) {
  365. /* 60 seconds */
  366. reset = 1;
  367. if (!conf.bot->hub)
  368. check_limitraise(chan);
  369. }
  370. }
  371. if (reset)
  372. cnt = 0;
  373. }
  374. static void got_sj(int idx, char *code, char *par)
  375. {
  376. struct chanset_t *chan = findchan_by_dname(newsplit(&par));
  377. if (chan) {
  378. if (conf.bot->hub) {
  379. chan->status &= ~CHAN_INACTIVE;
  380. write_userfile(-1);
  381. } else
  382. chan->channel.jointime = ((atoi(par) + now) - server_lag);
  383. }
  384. }
  385. static void got_sp(int idx, char *code, char *par)
  386. {
  387. struct chanset_t *chan = findchan_by_dname(newsplit(&par));
  388. if (chan) {
  389. if (conf.bot->hub) {
  390. remove_channel(chan);
  391. write_userfile(-1);
  392. } else
  393. chan->channel.parttime = ((atoi(par) + now) - server_lag);
  394. }
  395. }
  396. #ifdef no
  397. /* got_jn
  398. * We get this when a bot is opped in a +take chan
  399. * we are to set -inactive, jointime = 0, and join.
  400. */
  401. static void got_jn(int idx, char *code, char *par)
  402. {
  403. char *chname = newsplit(&par);
  404. if (!chname || !chname[0])
  405. return;
  406. struct chanset_t *chan = NULL;
  407. if (!(chan = findchan_by_dname(chname))) return;
  408. if (chan->channel.jointime && channel_inactive(chan)) {
  409. chan->status &= ~CHAN_INACTIVE;
  410. chan->channel.jointime = 0;
  411. if (!conf.bot->hub && shouldjoin(chan) && !channel_active(chan) && !channel_joining(chan)) {
  412. dprintf(DP_MODE, "JOIN %s %s\n", chan->name, chan->key_prot);
  413. chan->ircnet_status |= CHAN_JOINING;
  414. }
  415. }
  416. }
  417. #endif
  418. static void set_mode_protect(struct chanset_t *chan, char *set)
  419. {
  420. int i, pos = 1;
  421. char *s = NULL, *s1 = NULL;
  422. /* Clear old modes */
  423. chan->mode_mns_prot = chan->mode_pls_prot = 0;
  424. chan->limit_prot = 0;
  425. chan->key_prot[0] = 0;
  426. for (s = newsplit(&set); *s; s++) {
  427. i = 0;
  428. switch (*s) {
  429. case '+':
  430. pos = 1;
  431. break;
  432. case '-':
  433. pos = 0;
  434. break;
  435. case 'i':
  436. i = CHANINV;
  437. break;
  438. case 'p':
  439. i = CHANPRIV;
  440. break;
  441. case 's':
  442. i = CHANSEC;
  443. break;
  444. case 'm':
  445. i = CHANMODER;
  446. break;
  447. case 'c':
  448. i = CHANNOCLR;
  449. break;
  450. case 'C':
  451. i = CHANNOCTCP;
  452. break;
  453. case 'R':
  454. i = CHANREGON;
  455. break;
  456. case 'M':
  457. i = CHANMODR;
  458. break;
  459. case 'r':
  460. i = CHANLONLY;
  461. break;
  462. case 't':
  463. i = CHANTOPIC;
  464. break;
  465. case 'n':
  466. i = CHANNOMSG;
  467. break;
  468. case 'a':
  469. i = CHANANON;
  470. break;
  471. case 'q':
  472. i = CHANQUIET;
  473. break;
  474. case 'l':
  475. i = CHANLIMIT;
  476. chan->limit_prot = 0;
  477. if (pos) {
  478. s1 = newsplit(&set);
  479. if (s1[0] && !chan->limitraise)
  480. chan->limit_prot = atoi(s1);
  481. }
  482. break;
  483. case 'k':
  484. i = CHANKEY;
  485. chan->key_prot[0] = 0;
  486. if (pos) {
  487. s1 = newsplit(&set);
  488. if (s1[0])
  489. strlcpy(chan->key_prot, s1, sizeof(chan->key_prot));
  490. }
  491. break;
  492. }
  493. if (i) {
  494. if (pos) {
  495. chan->mode_pls_prot |= i;
  496. chan->mode_mns_prot &= ~i;
  497. } else {
  498. chan->mode_pls_prot &= ~i;
  499. chan->mode_mns_prot |= i;
  500. }
  501. }
  502. }
  503. /* Prevents a +s-p +p-s flood (fixed by drummer) */
  504. if (chan->mode_pls_prot & CHANSEC)
  505. chan->mode_pls_prot &= ~CHANPRIV;
  506. if (chan->mode_mns_prot & CHANPRIV && chan->closed_private)
  507. chan->closed_private = 0;
  508. if (chan->mode_mns_prot & CHANINV && chan->closed_invite)
  509. chan->closed_invite = 0;
  510. }
  511. static void get_mode_protect(struct chanset_t *chan, char *s, size_t ssiz)
  512. {
  513. char *p = s, s1[121] = "";
  514. int tst;
  515. bool ok = 0;
  516. for (int i = 0; i < 2; i++) {
  517. ok = 0;
  518. if (i == 0) {
  519. tst = chan->mode_pls_prot;
  520. if ((tst) || (chan->limit_prot != 0) || (chan->key_prot[0]))
  521. *p++ = '+';
  522. if (chan->limit_prot != 0) {
  523. *p++ = 'l';
  524. simple_sprintf(&s1[strlen(s1)], "%d ", chan->limit_prot);
  525. }
  526. if (chan->key_prot[0]) {
  527. *p++ = 'k';
  528. simple_sprintf(&s1[strlen(s1)], "%s ", chan->key_prot);
  529. }
  530. } else {
  531. tst = chan->mode_mns_prot;
  532. if (tst)
  533. *p++ = '-';
  534. if (tst & CHANKEY)
  535. *p++ = 'k';
  536. if (tst & CHANLIMIT)
  537. *p++ = 'l';
  538. }
  539. if (tst & CHANINV)
  540. *p++ = 'i';
  541. if (tst & CHANPRIV)
  542. *p++ = 'p';
  543. if (tst & CHANSEC)
  544. *p++ = 's';
  545. if (tst & CHANMODER)
  546. *p++ = 'm';
  547. if (tst & CHANNOCLR)
  548. *p++ = 'c';
  549. if (tst & CHANNOCTCP)
  550. *p++ = 'C';
  551. if (tst & CHANREGON)
  552. *p++ = 'R';
  553. if (tst & CHANMODR)
  554. *p++ = 'M';
  555. if (tst & CHANLONLY)
  556. *p++ = 'r';
  557. if (tst & CHANTOPIC)
  558. *p++ = 't';
  559. if (tst & CHANNOMSG)
  560. *p++ = 'n';
  561. if (tst & CHANANON)
  562. *p++ = 'a';
  563. if (tst & CHANQUIET)
  564. *p++ = 'q';
  565. }
  566. *p = 0;
  567. if (s1[0]) {
  568. s1[strlen(s1) - 1] = 0;
  569. strlcat(s, " ", ssiz);
  570. strlcat(s, s1, ssiz);
  571. }
  572. }
  573. /* Returns true if this is one of the channel masks
  574. */
  575. bool ismodeline(masklist *m, const char *username)
  576. {
  577. for (; m && m->mask[0]; m = m->next)
  578. if (!rfc_casecmp(m->mask, username))
  579. return 1;
  580. return 0;
  581. }
  582. /* Returns true if user matches one of the masklist -- drummer
  583. */
  584. bool ismasked(masklist *m, const char *username)
  585. {
  586. for (; m && m->mask[0]; m = m->next)
  587. if (wild_match(m->mask, (char *) username))
  588. return 1;
  589. return 0;
  590. }
  591. /* Unlink chanset element from chanset list.
  592. */
  593. static inline bool chanset_unlink(struct chanset_t *chan)
  594. {
  595. struct chanset_t *c_old = NULL;
  596. for (struct chanset_t *c = chanset; c; c_old = c, c = c->next) {
  597. if (c == chan) {
  598. if (c_old)
  599. c_old->next = c->next;
  600. else
  601. chanset = c->next;
  602. return 1;
  603. }
  604. }
  605. return 0;
  606. }
  607. /* Completely removes a channel.
  608. *
  609. * This includes the removal of all channel-bans, -exempts and -invites, as
  610. * well as all user flags related to the channel.
  611. */
  612. void remove_channel(struct chanset_t *chan)
  613. {
  614. int i;
  615. if (chan != chanset_default) {
  616. irc_log(chan, "Parting");
  617. /* Remove the channel from the list, so that noone can pull it
  618. away from under our feet during the check_part() call. */
  619. chanset_unlink(chan);
  620. /* Using chan->name is important here, especially for !chans <cybah> */
  621. if (!conf.bot->hub && shouldjoin(chan) && chan->name[0])
  622. dprintf(DP_SERVER, "PART %s\n", chan->name);
  623. clear_channel(chan, 0);
  624. noshare = 1;
  625. /* Remove channel-bans */
  626. while (chan->bans)
  627. u_delmask('b', chan, chan->bans->mask, 1);
  628. /* Remove channel-exempts */
  629. while (chan->exempts)
  630. u_delmask('e', chan, chan->exempts->mask, 1);
  631. /* Remove channel-invites */
  632. while (chan->invites)
  633. u_delmask('I', chan, chan->invites->mask, 1);
  634. /* Remove channel specific user flags */
  635. user_del_chan(chan->dname);
  636. noshare = 0;
  637. }
  638. free(chan->channel.key);
  639. for (i = 0; i < MODES_PER_LINE_MAX && chan->cmode[i].op; i++)
  640. free(chan->cmode[i].op);
  641. for (i = 0; i < (MODES_PER_LINE_MAX - 1) && chan->ccmode[i].op; i++)
  642. free(chan->ccmode[i].op);
  643. if (chan->key)
  644. free(chan->key);
  645. if (chan->rmkey)
  646. free(chan->rmkey);
  647. free(chan);
  648. }
  649. /* Bind this to chon and *if* the users console channel == ***
  650. * then set it to a specific channel
  651. */
  652. static int channels_chon(char *handle, int idx)
  653. {
  654. struct flag_record fr = {FR_CHAN | FR_ANYWH | FR_GLOBAL, 0, 0, 0 };
  655. int find;
  656. bool found = 0;
  657. struct chanset_t *chan = chanset;
  658. if (dcc[idx].type == &DCC_CHAT) {
  659. if (!findchan_by_dname(dcc[idx].u.chat->con_chan) &&
  660. ((dcc[idx].u.chat->con_chan[0] != '*') ||
  661. (dcc[idx].u.chat->con_chan[1] != 0))) {
  662. get_user_flagrec(dcc[idx].user, &fr, NULL);
  663. if (glob_op(fr))
  664. found = 1;
  665. if (chan_owner(fr))
  666. find = USER_OWNER;
  667. else if (chan_master(fr))
  668. find = USER_MASTER;
  669. else
  670. find = USER_OP;
  671. fr.match = FR_CHAN;
  672. while (chan && !found) {
  673. get_user_flagrec(dcc[idx].user, &fr, chan->dname, chan);
  674. if (fr.chan & find)
  675. found = 1;
  676. else
  677. chan = chan->next;
  678. }
  679. if (!chan)
  680. chan = chanset;
  681. struct chat_info dummy;
  682. if (chan)
  683. strlcpy(dcc[idx].u.chat->con_chan, chan->dname, sizeof(dummy.con_chan));
  684. else
  685. strlcpy(dcc[idx].u.chat->con_chan, "*", 2);
  686. }
  687. }
  688. return 0;
  689. }
  690. static cmd_t my_chon[] =
  691. {
  692. {"*", "", (Function) channels_chon, "channels:chon", 0},
  693. {NULL, NULL, NULL, NULL, 0}
  694. };
  695. void channels_report(int idx, int details)
  696. {
  697. int i;
  698. char s[1024] = "", s2[100] = "";
  699. struct flag_record fr = {FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  700. for (struct chanset_t *chan = chanset; chan; chan = chan->next) {
  701. if (idx != DP_STDOUT)
  702. get_user_flagrec(dcc[idx].user, &fr, chan->dname, chan);
  703. if (!privchan(fr, chan, PRIV_OP) && ((idx == DP_STDOUT) || glob_master(fr) || chan_master(fr))) {
  704. s[0] = 0;
  705. if (chan_bitch(chan))
  706. strlcat(s, "bitch, ", sizeof(s));
  707. if (s[0])
  708. s[strlen(s) - 2] = 0;
  709. if (!s[0])
  710. strlcpy(s, "lurking", sizeof(s));
  711. get_mode_protect(chan, s2, sizeof(s2));
  712. if (channel_closed(chan)) {
  713. if (chan->closed_invite)
  714. strlcat(s2, "i", sizeof(s2));
  715. if (chan->closed_private)
  716. strlcat(s2, "p", sizeof(s2));
  717. }
  718. if (shouldjoin(chan)) {
  719. if (channel_active(chan)) {
  720. /* If it's a !chan, we want to display it's unique name too <cybah> */
  721. if (chan->dname[0]=='!') {
  722. dprintf(idx, " %-10s: %2d member%s enforcing \"%s\" (%s), "
  723. "unique name %s\n", chan->dname, chan->channel.members,
  724. (chan->channel.members==1) ? "," : "s,", s2, s, chan->name);
  725. } else {
  726. dprintf(idx, " %-10s: %2d member%s enforcing \"%s\" (%s)\n",
  727. chan->dname, chan->channel.members,
  728. chan->channel.members == 1 ? "," : "s,", s2, s);
  729. }
  730. } else {
  731. if (!conf.bot->hub)
  732. dprintf(idx, " %-10s: (%s), enforcing \"%s\" (%s)\n", chan->dname,
  733. channel_pending(chan) ? "pending" : "not on channel", s2, s);
  734. else
  735. dprintf(idx, " %-10s: (%s), enforcing \"%s\" (%s)\n", chan->dname,
  736. "limbo", s2, s);
  737. }
  738. } else {
  739. dprintf(idx, " %-10s: channel is set +inactive\n",
  740. chan->dname);
  741. }
  742. if (details) {
  743. s[0] = 0;
  744. i = 0;
  745. i += my_strcpy(s + i, "dynamic ");
  746. if (channel_enforcebans(chan))
  747. i += my_strcpy(s + i, "enforcebans ");
  748. if (channel_dynamicbans(chan))
  749. i += my_strcpy(s + i, "dynamicbans ");
  750. if (!channel_nouserbans(chan))
  751. i += my_strcpy(s + i, "userbans ");
  752. if (channel_bitch(chan))
  753. i += my_strcpy(s + i, "bitch ");
  754. /*
  755. if (channel_revenge(chan))
  756. i += my_strcpy(s + i, "revenge ");
  757. if (channel_revenge(chan))
  758. i += my_strcpy(s + i, "revengebot ");
  759. */
  760. if (channel_secret(chan))
  761. i += my_strcpy(s + i, "secret ");
  762. if (channel_cycle(chan))
  763. i += my_strcpy(s + i, "cycle ");
  764. if (channel_dynamicexempts(chan))
  765. i += my_strcpy(s + i, "dynamicexempts ");
  766. if (!channel_nouserexempts(chan))
  767. i += my_strcpy(s + i, "userexempts ");
  768. if (channel_dynamicinvites(chan))
  769. i += my_strcpy(s + i, "dynamicinvites ");
  770. if (!channel_nouserinvites(chan))
  771. i += my_strcpy(s + i, "userinvites ");
  772. if (!shouldjoin(chan))
  773. i += my_strcpy(s + i, "inactive ");
  774. if (channel_nodesynch(chan))
  775. i += my_strcpy(s + i, "nodesynch ");
  776. if (channel_closed(chan))
  777. i += my_strcpy(s + i, "closed ");
  778. if (HAVE_TAKE && channel_take(chan))
  779. i += my_strcpy(s + i, "take ");
  780. if (channel_voice(chan))
  781. i += my_strcpy(s + i, "voice ");
  782. if (channel_autoop(chan))
  783. i += my_strcpy(s + i, "autoop ");
  784. if (channel_meankicks(chan))
  785. i += my_strcpy(s + i, "meankicks ");
  786. if (channel_rbl(chan))
  787. i += my_strcpy(s + i, "rbl ");
  788. /* Chanflag template
  789. * if (channel_temp(chan))
  790. * i += my_strcpy(s + i, "temp ");
  791. */
  792. if (channel_nomassjoin(chan))
  793. i += my_strcpy(s + i, "nomassjoin ");
  794. if (channel_botbitch(chan))
  795. i += my_strcpy(s + i, "botbitch ");
  796. if (channel_backup(chan))
  797. i += my_strcpy(s + i, "backup ");
  798. if (channel_fastop(chan))
  799. i += my_strcpy(s + i, "fastop ");
  800. if (channel_privchan(chan))
  801. i += my_strcpy(s + i, "private ");
  802. dprintf(idx, " Options: %s\n", s);
  803. if (chan->limitraise)
  804. dprintf(idx, " Raising limit +%d every 2 minutes\n", chan->limitraise);
  805. /*
  806. if (chan->revenge_mode)
  807. dprintf(idx, " revenge-mode %d\n",
  808. chan->revenge_mode);
  809. */
  810. dprintf(idx, " Bans last %d mins.\n", chan->ban_time);
  811. dprintf(idx, " Exemptions last %d mins.\n", chan->exempt_time);
  812. dprintf(idx, " Invitations last %d mins.\n", chan->invite_time);
  813. }
  814. }
  815. }
  816. }
  817. cmd_t channels_bot[] = {
  818. {"cjoin", "", (Function) got_cjoin, NULL, 0},
  819. {"cpart", "", (Function) got_cpart, NULL, 0},
  820. {"cset", "", (Function) got_cset, NULL, 0},
  821. {"cycle", "", (Function) got_cycle, NULL, LEAF},
  822. {"down", "", (Function) got_down, NULL, LEAF},
  823. {"rl", "", (Function) got_role, NULL, 0},
  824. {"kl", "", (Function) got_kl, NULL, 0},
  825. {"sj", "", (Function) got_sj, NULL, 0},
  826. {"sp", "", (Function) got_sp, NULL, 0},
  827. // {"jn", "", (Function) got_jn, NULL, 0},
  828. /*
  829. #ifdef HUB
  830. {"o1", "", (Function) got_o1, NULL, 0},
  831. {"kl", "", (Function) got_kl, NULL, 0},
  832. #endif
  833. {"ltp", "", (Function) got_locktopic, NULL, 0},
  834. */
  835. {NULL, NULL, NULL, NULL, 0}
  836. };
  837. void channels_init()
  838. {
  839. timer_create_secs(60, "check_expired_masks", (Function) check_expired_masks);
  840. if (conf.bot->hub) {
  841. timer_create_secs(30, "rebalance_roles", (Function) rebalance_roles);
  842. timer_create_secs(30, "check_should_close", (Function) check_should_close);
  843. #ifdef G_BACKUP
  844. timer_create_secs(30, "check_should_backup", (Function) check_should_backup);
  845. #endif /* G_BACKUP */
  846. }
  847. timer_create_secs(10, "channels_timers", (Function) channels_timers);
  848. add_builtins("dcc", C_dcc_channels);
  849. add_builtins("bot", channels_bot);
  850. add_builtins("chon", my_chon);
  851. }