mode.cc 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  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. * mode.c -- part of irc.mod
  22. * queuing and flushing mode changes made by the bot
  23. * channel mode changes and the bot's reaction to them
  24. * setting and getting the current wanted channel modes
  25. *
  26. */
  27. #include "src/shell.h"
  28. /* Reversing this mode? */
  29. static bool reversing = 0;
  30. static bool massop = 0;
  31. static bool mdop_reversing = 1;
  32. # define PLUS BIT0
  33. # define MINUS BIT1
  34. # define CHOP BIT2
  35. # define BAN BIT3
  36. # define VOICE BIT4
  37. # define EXEMPT BIT5
  38. # define INVITE BIT6
  39. static struct flag_record user = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  40. static struct flag_record victim = { FR_GLOBAL | FR_CHAN, 0, 0, 0 };
  41. /* This implementation wont overrun dst - 'max' is the max bytes that dst
  42. * can be, including the null terminator. So if 'dst' is a 128 byte buffer,
  43. * pass 128 as 'max'. The function will _always_ null-terminate 'dst'.
  44. *
  45. * Returns: The number of characters appended to 'dst'.
  46. *
  47. * Usage eg.
  48. *
  49. * char buf[128];
  50. * size_t bufsize = sizeof(buf);
  51. *
  52. * buf[0] = 0, bufsize--;
  53. *
  54. * while (blah && bufsize) {
  55. * bufsize -= egg_strcatn(buf, <some-long-string>, sizeof(buf));
  56. * }
  57. *
  58. * <Cybah>
  59. */
  60. static size_t egg_strcatn(char *dst, const char *src, size_t max)
  61. {
  62. size_t tmpmax = 0;
  63. /* find end of 'dst' */
  64. while (*dst && max > 0) {
  65. dst++;
  66. max--;
  67. }
  68. /* Store 'max', so we can use it to workout how many characters were
  69. * written later on.
  70. */
  71. tmpmax = max;
  72. /* copy upto, but not including the null terminator */
  73. while (*src && max > 1) {
  74. *dst++ = *src++;
  75. max--;
  76. }
  77. /* null-terminate the buffer */
  78. /* Don't include the terminating null in our count, as it will cumulate
  79. * in loops - causing a headache for the caller.
  80. */
  81. return tmpmax - max;
  82. }
  83. static bool
  84. do_op(memberlist *m, struct chanset_t *chan, bool delay, bool force)
  85. {
  86. if (!me_op(chan) || !m || (!force && (chan_hasop(m) || chan_sentop(m))))
  87. return 0;
  88. if (delay) {
  89. m->delay = now + chan->auto_delay;
  90. m->flags |= SENTOP;
  91. }
  92. if (channel_fastop(chan) || channel_take(chan) || cookies_disabled) {
  93. add_mode(chan, '+', 'o', m->nick);
  94. } else if (!connect_bursting) {
  95. add_cookie(chan, m->nick);
  96. }
  97. return 1;
  98. }
  99. static void
  100. flush_cookies(struct chanset_t *chan, int pri)
  101. {
  102. if (connect_bursting) // Make sure not in burst mode
  103. return;
  104. char out[512] = "", *p = out, post[512] = "";
  105. size_t postsize = sizeof(post) - 1;
  106. memberlist *nicks[3] = { NULL, NULL, NULL };
  107. chan->cbytes = 0;
  108. int nick_i = 0;
  109. for (unsigned int i = 0; i < (modesperline - 1); i++) {
  110. if (chan->ccmode[i].op && postsize > strlen(chan->ccmode[i].op)) {
  111. memberlist* mx = ismember(chan, chan->ccmode[i].op);
  112. /* We queued them, but they might not be in the channel any more! */
  113. if (mx) {
  114. /* Build modes .. */
  115. *p++ = '+';
  116. *p++ = 'o';
  117. /* .. and params */
  118. postsize -= egg_strcatn(post, chan->ccmode[i].op, sizeof(post));
  119. postsize -= egg_strcatn(post, " ", sizeof(post));
  120. nicks[nick_i++] = mx;
  121. }
  122. free(chan->ccmode[i].op);
  123. chan->ccmode[i].op = NULL;
  124. }
  125. }
  126. /* remember to terminate the buffer ('out')... */
  127. if (out[0]) {
  128. *p++ = '-';
  129. *p++ = 'b';
  130. }
  131. *p = 0;
  132. if (post[0]) {
  133. memberlist* me = ismember(chan, botname);
  134. if (me)
  135. member_getuser(me);
  136. /* Am I even on the channel? */
  137. if (!me || !me->user)
  138. return;
  139. /* remove the trailing space... */
  140. size_t myindex = (sizeof(post) - 1) - postsize;
  141. if (myindex > 0 && post[--myindex] == ' ')
  142. post[myindex] = 0;
  143. //myindex is how long post is
  144. egg_strcatn(&out[0], " ", sizeof(out));
  145. egg_strcatn(&out[1], post, sizeof(out));
  146. egg_strcatn(&out[myindex + 1], " ", sizeof(out));
  147. myindex += (p - out) + 2; //(p-out)=outlen + 2 spaces
  148. makecookie(&out[myindex], sizeof(out) - myindex, chan->dname, me, nicks[0], nicks[1], nicks[2]);
  149. }
  150. if (out[0]) {
  151. // Enabling this creates a queued cookie and a dumped cookie, resulting in out-of-order counters.
  152. if (pri == QUICK && 0) {
  153. char outbuf[201] = "";
  154. const size_t len = simple_snprintf(outbuf, sizeof(outbuf), "MODE %s %s\r\n", chan->name, out);
  155. dprintf_real(DP_MODE, outbuf, len, sizeof(outbuf));
  156. /* dprintf(DP_MODE, "MODE %s %s\n", chan->name, out); */
  157. } else
  158. dprintf(DP_MODE, "MODE %s %s\n", chan->name, out);
  159. }
  160. }
  161. static void
  162. flush_mode(struct chanset_t *chan, int pri)
  163. {
  164. if (!modesperline) /* Haven't received 005 yet :) */
  165. return;
  166. char out[512] = "", *p = out, post[512] = "";
  167. size_t postsize = sizeof(post) - 1;
  168. int plus = 2; /* 0 = '-', 1 = '+', 2 = none */
  169. unsigned int i = 0;
  170. flush_cookies(chan, pri);
  171. /* dequeue_op_deop(chan); */
  172. /* now does +o first.. */
  173. if (chan->mns[0]) {
  174. *p++ = '-', plus = 0;
  175. for (i = 0; i < strlen(chan->mns); i++)
  176. *p++ = chan->mns[i];
  177. chan->mns[0] = 0;
  178. }
  179. if (chan->pls[0]) {
  180. *p++ = '+', plus = 1;
  181. for (i = 0; i < strlen(chan->pls); i++)
  182. *p++ = chan->pls[i];
  183. chan->pls[0] = 0;
  184. }
  185. chan->bytes = 0;
  186. chan->compat = 0;
  187. /* +k or +l ? */
  188. if (chan->key && !chan->rmkey) {
  189. if (plus != 1) {
  190. *p++ = '+', plus = 1;
  191. }
  192. *p++ = 'k';
  193. postsize -= egg_strcatn(post, chan->key, sizeof(post));
  194. postsize -= egg_strcatn(post, " ", sizeof(post));
  195. free(chan->key), chan->key = NULL;
  196. }
  197. /* max +l is signed 2^32 on IRCnet at least... so makesure we've got at least
  198. * a 13 char buffer for '-2147483647 \0'. We'll be overwriting the existing
  199. * terminating null in 'post', so makesure postsize >= 12.
  200. */
  201. if (chan->limit != 0 && postsize >= 12) {
  202. if (plus != 1) {
  203. *p++ = '+', plus = 1;
  204. }
  205. *p++ = 'l';
  206. /* 'sizeof(post) - 1' is used because we want to overwrite the old null */
  207. postsize -= simple_sprintf(&post[(sizeof(post) - 1) - postsize], "%d ", chan->limit);
  208. chan->limit = 0;
  209. }
  210. /* -k ? */
  211. if (chan->rmkey) {
  212. if (plus) {
  213. *p++ = '-', plus = 0;
  214. }
  215. *p++ = 'k';
  216. postsize -= egg_strcatn(post, chan->rmkey, sizeof(post));
  217. postsize -= egg_strcatn(post, " ", sizeof(post));
  218. free(chan->rmkey), chan->rmkey = NULL;
  219. }
  220. /* Do -{b,e,I} before +{b,e,I} to avoid the server ignoring overlaps */
  221. for (i = 0; i < modesperline; i++) {
  222. if (chan->cmode[i].op && (chan->cmode[i].type & MINUS) && postsize > strlen(chan->cmode[i].op)) {
  223. if (plus) {
  224. *p++ = '-', plus = 0;
  225. }
  226. *p++ = ((chan->cmode[i].type & BAN) ? 'b' :
  227. ((chan->cmode[i].type & CHOP) ? 'o' :
  228. ((chan->cmode[i].type & EXEMPT) ? 'e' : ((chan->cmode[i].type & INVITE) ? 'I' : 'v'))));
  229. postsize -= egg_strcatn(post, chan->cmode[i].op, sizeof(post));
  230. postsize -= egg_strcatn(post, " ", sizeof(post));
  231. free(chan->cmode[i].op), chan->cmode[i].op = NULL;
  232. chan->cmode[i].type = 0;
  233. }
  234. }
  235. /* now do all the + modes... */
  236. for (i = 0; i < modesperline; i++) {
  237. if (chan->cmode[i].op && (chan->cmode[i].type & PLUS) && postsize > strlen(chan->cmode[i].op)) {
  238. if (plus != 1) {
  239. *p++ = '+', plus = 1;
  240. }
  241. *p++ = ((chan->cmode[i].type & BAN) ? 'b' :
  242. ((chan->cmode[i].type & CHOP) ? 'o' :
  243. ((chan->cmode[i].type & EXEMPT) ? 'e' : ((chan->cmode[i].type & INVITE) ? 'I' : 'v'))));
  244. postsize -= egg_strcatn(post, chan->cmode[i].op, sizeof(post));
  245. postsize -= egg_strcatn(post, " ", sizeof(post));
  246. free(chan->cmode[i].op), chan->cmode[i].op = NULL;
  247. chan->cmode[i].type = 0;
  248. }
  249. }
  250. /* remember to terminate the buffer ('out')... */
  251. *p = 0;
  252. if (post[0]) {
  253. /* remove the trailing space... */
  254. size_t myindex = (sizeof(post) - 1) - postsize;
  255. if (myindex > 0 && post[myindex - 1] == ' ')
  256. post[myindex - 1] = 0;
  257. egg_strcatn(out, " ", sizeof(out));
  258. egg_strcatn(out, post, sizeof(out));
  259. }
  260. if (out[0]) {
  261. if (pri == QUICK) {
  262. /* floods too much
  263. char outbuf[201] = "";
  264. const size_t len = simple_snprintf(outbuf, sizeof(outbuf), "MODE %s %s\r\n", chan->name, out);
  265. tputs(serv, outbuf, len);
  266. */
  267. dprintf(DP_MODE, "MODE %s %s\n", chan->name, out);
  268. } else
  269. dprintf(DP_SERVER, "MODE %s %s\n", chan->name, out);
  270. }
  271. }
  272. /* Queue a channel mode change
  273. */
  274. void
  275. real_add_mode(struct chanset_t *chan, const char plus, const char mode, const char *op, bool cookie)
  276. {
  277. if (!me_op(chan))
  278. return;
  279. memberlist *mx = NULL;
  280. if (mode == 'o' || mode == 'v') {
  281. mx = ismember(chan, op);
  282. if (!mx)
  283. return;
  284. if (plus == '-' && mode == 'o') {
  285. if (chan_sentdeop(mx) || !chan_hasop(mx))
  286. return;
  287. mx->flags |= SENTDEOP;
  288. }
  289. if (plus == '+' && mode == 'o') {
  290. if (chan_sentop(mx) || chan_hasop(mx))
  291. return;
  292. mx->flags |= SENTOP;
  293. }
  294. if (plus == '-' && mode == 'v') {
  295. if (chan_sentdevoice(mx) || !chan_hasvoice(mx))
  296. return;
  297. mx->flags |= SENTDEVOICE;
  298. }
  299. if (plus == '+' && mode == 'v') {
  300. if (chan_sentvoice(mx) || chan_hasvoice(mx))
  301. return;
  302. mx->flags |= SENTVOICE;
  303. }
  304. }
  305. int type, modes;
  306. size_t len = 0;
  307. unsigned int i;
  308. masklist *m = NULL;
  309. char s[21] = "";
  310. if (chan->compat == 0) {
  311. if (mode == 'e' || mode == 'I')
  312. chan->compat = 2;
  313. else
  314. chan->compat = 1;
  315. } else if (mode == 'e' || mode == 'I') {
  316. if (prevent_mixing && chan->compat == 1)
  317. flush_mode(chan, NORMAL);
  318. } else if (prevent_mixing && chan->compat == 2)
  319. flush_mode(chan, NORMAL);
  320. if (mode == 'o' || mode == 'b' || mode == 'v' || mode == 'e' || mode == 'I') {
  321. type = (plus == '+' ? PLUS : MINUS) |
  322. (mode == 'o' ? CHOP : (mode == 'b' ? BAN : (mode == 'v' ? VOICE : (mode == 'e' ? EXEMPT : INVITE))));
  323. /*
  324. * FIXME: Some networks remove overlapped bans,
  325. * IRCnet does not (poptix/drummer)
  326. *
  327. * Note: On IRCnet ischanXXX() should be used, otherwise isXXXed().
  328. */
  329. if ((plus == '-' && ((mode == 'b' && !ischanban(chan, op)) ||
  330. (mode == 'e' && !ischanexempt(chan, op)) ||
  331. (mode == 'I' && !ischaninvite(chan, op)))) || (plus == '+' &&
  332. ((mode == 'b' && ischanban(chan, op))
  333. || (mode == 'e' &&
  334. ischanexempt(chan, op)) ||
  335. (mode == 'I' &&
  336. ischaninvite(chan, op)))))
  337. return;
  338. /* If there are already max_bans bans, max_exempts exemptions,
  339. * max_invites invitations or max_modes +b/+e/+I modes on the
  340. * channel, don't try to add one more.
  341. */
  342. if (plus == '+' && (mode == 'b' || mode == 'e' || mode == 'I')) {
  343. int bans = 0, exempts = 0, invites = 0;
  344. for (m = chan->channel.ban; m && m->mask[0]; m = m->next)
  345. bans++;
  346. if ((mode == 'b') && (bans >= max_bans))
  347. return;
  348. for (m = chan->channel.exempt; m && m->mask[0]; m = m->next)
  349. exempts++;
  350. if ((mode == 'e') && (exempts >= max_exempts))
  351. return;
  352. for (m = chan->channel.invite; m && m->mask[0]; m = m->next)
  353. invites++;
  354. if ((mode == 'I') && (invites >= max_invites))
  355. return;
  356. if (bans + exempts + invites >= max_modes)
  357. return;
  358. }
  359. /* op-type mode change */
  360. /* for cookie ops, use ccmode instead of cmode */
  361. if (cookie) {
  362. for (i = 0; i < (modesperline - 1); i++)
  363. if (chan->ccmode[i].op != NULL && !rfc_casecmp(chan->ccmode[i].op, op))
  364. return; /* Already in there :- duplicate */
  365. len = strlen(op) + 1;
  366. if (chan->cbytes + len > mode_buf_len)
  367. flush_mode(chan, NORMAL);
  368. for (i = 0; i < (modesperline - 1); i++)
  369. if (!chan->ccmode[i].op) {
  370. chan->ccmode[i].op = (char *) calloc(1, len);
  371. chan->cbytes += len; /* Add 1 for safety */
  372. strlcpy(chan->ccmode[i].op, op, len);
  373. break;
  374. }
  375. } else {
  376. for (i = 0; i < modesperline; i++)
  377. if (chan->cmode[i].type == type && chan->cmode[i].op != NULL && !rfc_casecmp(chan->cmode[i].op, op))
  378. return; /* Already in there :- duplicate */
  379. len = strlen(op) + 1;
  380. if (chan->bytes + len > mode_buf_len)
  381. flush_mode(chan, NORMAL);
  382. for (i = 0; i < modesperline; i++)
  383. if (chan->cmode[i].type == 0) {
  384. chan->cmode[i].type = type;
  385. chan->cmode[i].op = (char *) calloc(1, len);
  386. chan->bytes += len; /* Add 1 for safety */
  387. strlcpy(chan->cmode[i].op, op, len);
  388. break;
  389. }
  390. }
  391. }
  392. /* +k ? store key */
  393. else if (plus == '+' && mode == 'k') {
  394. if (chan->key)
  395. free(chan->key);
  396. len = strlen(op) + 1;
  397. chan->key = (char *) calloc(1, len);
  398. strlcpy(chan->key, op, len);
  399. }
  400. /* -k ? store removed key */
  401. else if (plus == '-' && mode == 'k') {
  402. if (chan->rmkey)
  403. free(chan->rmkey);
  404. len = strlen(op) + 1;
  405. chan->rmkey = (char *) calloc(1, len);
  406. strlcpy(chan->rmkey, op, len);
  407. }
  408. /* +l ? store limit */
  409. else if (plus == '+' && mode == 'l')
  410. chan->limit = atoi(op);
  411. else {
  412. /* Typical mode changes */
  413. if (plus == '+')
  414. strlcpy(s, chan->pls, sizeof(s));
  415. else
  416. strlcpy(s, chan->mns, sizeof(s));
  417. if (!strchr(s, mode)) {
  418. if (plus == '+') {
  419. chan->pls[strlen(chan->pls) + 1] = 0;
  420. chan->pls[strlen(chan->pls)] = mode;
  421. } else {
  422. chan->mns[strlen(chan->mns) + 1] = 0;
  423. chan->mns[strlen(chan->mns)] = mode;
  424. }
  425. }
  426. }
  427. modes = modesperline; /* Check for full buffer. */
  428. for (i = 0; i < modesperline; i++)
  429. if (chan->cmode[i].type)
  430. modes--;
  431. if (include_lk && chan->limit)
  432. modes--;
  433. if (include_lk && chan->rmkey)
  434. modes--;
  435. if (include_lk && chan->key)
  436. modes--;
  437. if (modes < 1)
  438. flush_mode(chan, NORMAL); /* Full buffer! Flush modes. */
  439. /* flush full cookie queue */
  440. modes = modesperline - 1;
  441. for (i = 0; i < (modesperline - 1); i++)
  442. if (chan->ccmode[i].op)
  443. modes--;
  444. if (modes < 1)
  445. flush_cookies(chan, NORMAL);
  446. }
  447. /*
  448. * Mode parsing functions
  449. */
  450. static void
  451. got_key(struct chanset_t *chan, char *key)
  452. {
  453. if (((reversing) && !(chan->key_prot[0])) ||
  454. ((chan->mode_mns_prot & CHANKEY) && !(glob_master(user) || glob_bot(user) || chan_master(user)))) {
  455. if (key && key[0]) {
  456. add_mode(chan, '-', 'k', key);
  457. } else {
  458. add_mode(chan, '-', 'k', "");
  459. }
  460. }
  461. }
  462. /* m may not exist */
  463. static void
  464. got_op(struct chanset_t *chan, memberlist *m, memberlist *mv)
  465. {
  466. bool check_chan = 0;
  467. const bool me_opped = mv->is_me;
  468. bool meop = me_op(chan);
  469. const bool was_op = chan_hasop(mv);
  470. // No need to punish someone who was already opped.
  471. if (massop && was_op)
  472. return;
  473. /* Did *I* just get opped? */
  474. if (!meop && me_opped) {
  475. check_chan = 1;
  476. meop = 1;
  477. }
  478. get_user_flagrec(mv->user, &victim, chan->dname, chan);
  479. // Did some other bot just get opped, and I'm not opped yet?
  480. if (mv->user && mv->user->bot && !me_opped && !meop) {
  481. chan->channel.do_opreq = 1;
  482. }
  483. /* Flags need to be set correctly right from the beginning now, so that
  484. * add_mode() doesn't get irritated.
  485. */
  486. mv->flags |= CHANOP;
  487. /* Added new meaning of WASOP:
  488. * in mode binds it means: was he op before get (de)opped
  489. * (stupid IrcNet allows opped users to be opped again and
  490. * opless users to be deopped)
  491. * script now can use [wasop nick chan] proc to check
  492. * if user was op or wasnt (drummer)
  493. */
  494. mv->flags &= ~SENTOP;
  495. /* This skips the recheck_channel until we get the end of the WHO list */
  496. if (channel_pending(chan))
  497. return;
  498. /* I'm opped, and the opper isn't me, and it isn't a server op */
  499. if (m && meop && !me_opped) {
  500. /* deop if they are +d or it is +bitch */
  501. int bitch = chan_bitch(chan);
  502. if (reversing || chk_deop(victim, chan) || (!loading && userlist && bitch && !chk_op(victim, chan))) { /* chk_op covers +private */
  503. int num = randint(10);
  504. char outbuf[101] = "";
  505. size_t len = 0;
  506. /* should kick the oppee first, then deal with the opper */
  507. #ifdef old
  508. if (num == 4) {
  509. len = simple_snprintf(outbuf, sizeof(outbuf), "MODE %s -o %s\r\n", chan->name, mv->nick);
  510. mv->flags |= SENTDEOP;
  511. } else if (num == 5) {
  512. len = simple_snprintf(outbuf, sizeof(outbuf), "MODE %s -o %s\r\n", chan->name, m->nick);
  513. m->flags |= SENTDEOP;
  514. #endif
  515. if (num == 5) {
  516. add_mode(chan, '-', 'o', m->nick);
  517. } else if (bitch && num == 6) {
  518. len = simple_snprintf(outbuf, sizeof(outbuf), "KICK %s %s :%s\r\n", chan->name, mv->nick, response(RES_BITCHOPPED));
  519. mv->flags |= SENTKICK;
  520. } else if (bitch && num == 7) {
  521. len = simple_snprintf(outbuf, sizeof(outbuf), "KICK %s %s :%s\r\n", chan->name, m->nick, response(RES_BITCHOP));
  522. m->flags |= SENTKICK;
  523. } else
  524. add_mode(chan, '-', 'o', mv->nick);
  525. if (len)
  526. dprintf_real(DP_MODE_NEXT, outbuf, len, sizeof(outbuf));
  527. }
  528. } else if (reversing && !me_opped)
  529. add_mode(chan, '-', 'o', mv->nick);
  530. /* server op */
  531. if (!m && meop && !me_opped) {
  532. if (chk_deop(victim, chan) || (chan_bitch(chan) && !chk_op(victim, chan))) {
  533. mv->flags |= FAKEOP;
  534. add_mode(chan, '-', 'o', mv->nick);
  535. }
  536. }
  537. mv->flags |= WASOP;
  538. if (check_chan) {
  539. #ifdef no
  540. /* tell other bots to set jointime to 0 and join */
  541. char *buf = NULL;
  542. size_t siz = strlen(chan->dname) + 3 + 1;
  543. buf = calloc(1, siz);
  544. simple_snprintf(buf, siz, "jn %s", chan->dname);
  545. putallbots(buf);
  546. free(buf);
  547. #endif
  548. /* check exempts/invites and shit */
  549. recheck_channel(chan, 2);
  550. }
  551. }
  552. static void
  553. got_deop(struct chanset_t *chan, memberlist *m, memberlist *mv, char *isserver)
  554. {
  555. const bool was_op = chan_hasop(mv);
  556. if (mdop_reversing && !was_op)
  557. return;
  558. /* m is NULL if a server made the change */
  559. get_user_flagrec(mv->user, &victim, chan->dname, chan);
  560. /* Flags need to be set correctly right from the beginning now, so that
  561. * add_mode() doesn't get irritated.
  562. */
  563. mv->flags &= ~(CHANOP | SENTDEOP | FAKEOP);
  564. /* Check comments in got_op() (drummer) */
  565. mv->flags &= ~WASOP;
  566. if (channel_pending(chan))
  567. return;
  568. /* Deop'd someone on my oplist? */
  569. if (me_op(chan)) {
  570. /* do we want to reop victim? */
  571. if (
  572. /* I didn't deop them, another bot didn't deop them and they didn't deop themselves. */
  573. (was_op && ((m && !m->is_me && mv != m && !(m->user && m->user->bot)) || (!m))) && (
  574. /*
  575. * reversing
  576. * They are either an op or this chan is -bitch
  577. */
  578. (reversing && (!chan_bitch(chan) || chk_op(victim, chan))) ||
  579. /* Reop bots to avoid them needing to ask */
  580. ((chan->role & ROLE_PROTECT) && mv->user && mv->user->bot && chk_op(victim, chan))
  581. )
  582. ) {
  583. /* Then we'll bless the victim */
  584. do_op(mv, chan, 0, 0);
  585. }
  586. }
  587. if (isserver[0]) /* !m */
  588. putlog(LOG_MODES, chan->dname, "TS resync (%s): %s deopped by %s", chan->dname, mv->nick, isserver);
  589. /* Check for mass deop */
  590. else if (m) {
  591. detect_chan_flood(m, m->from, chan, FLOOD_DEOP, mv->nick);
  592. }
  593. /* Having op hides your +v and +h status -- so now that someone's lost ops,
  594. * check to see if they have +v or +h
  595. */
  596. // WHO CARES
  597. // if (!channel_take(chan) && !chan_bitch(chan) && !(mv->flags & (CHANVOICE | STOPWHO))) {
  598. // send_chan_who(DP_HELP, chan);
  599. // mv->flags |= STOPWHO;
  600. // }
  601. /* Was the bot deopped? */
  602. if (mv->is_me) {
  603. /* Cancel any pending kicks and modes */
  604. memberlist *m2 = NULL;
  605. for (m2 = chan->channel.member; m2 && m2->nick[0]; m2 = m2->next)
  606. m2->flags &= ~(SENTKICK | SENTDEOP | SENTOP | SENTVOICE | SENTDEVOICE);
  607. chan->channel.do_opreq = 1;
  608. /* request_op(chan); */
  609. /* need: op */
  610. if (!m)
  611. putlog(LOG_MODES, chan->dname, "TS resync deopped me on %s :(", chan->dname);
  612. } else {
  613. // Revenge kick clients that deop our bots
  614. if (chan->revenge && m && m != mv && mv->user && mv->user->bot && !(m->user && m->user->bot)) {
  615. if ((chan->role & ROLE_REVENGE) && !chan_sentkick(m) && me_op(chan)) {
  616. m->flags |= SENTKICK;
  617. dprintf(DP_MODE_NEXT, "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_REVENGE));
  618. } else {
  619. if (m->user) {
  620. char tmp[128] = "";
  621. simple_snprintf(tmp, sizeof(tmp), "Deopped bot %s on %s", mv->nick, chan->dname);
  622. deflag_user(m->user, DEFLAG_EVENT_REVENGE_DEOP, tmp, chan);
  623. }
  624. }
  625. }
  626. }
  627. }
  628. static void
  629. got_ban(struct chanset_t *chan, memberlist *m, char *mask, char *isserver)
  630. {
  631. const memberlist *me = ismember(chan, botname);
  632. char s[UHOSTLEN] = "";
  633. simple_snprintf(s, sizeof s, "%s!%s", m ? m->nick : "", m ? m->userhost : isserver);
  634. if (m && newban(chan, mask, m->from))
  635. return; /* The ban was already set, don't bother with it */
  636. if (channel_pending(chan) || !me_op(chan))
  637. return;
  638. // Make an array of all matching users
  639. bd::Array<memberlist*> matchedUserMembers;
  640. memberlist *matched_bot = NULL;
  641. for (memberlist *mv = chan->channel.member; mv && mv->nick[0]; mv = mv->next) {
  642. member_getuser(mv);
  643. if (mv->user) {
  644. if ((wild_match(mask, mv->from) || match_cidr(mask, mv->from))) {
  645. if (!matched_bot && mv != m && mv->user->bot) {
  646. matched_bot = mv;
  647. }
  648. if (mv->user && !isexempted(chan, mv->from)) {
  649. matchedUserMembers << mv;
  650. }
  651. }
  652. }
  653. }
  654. // Revenge kick clients that ban our bots
  655. if (chan->revenge && m && matched_bot && !(m->user && m->user->bot)) {
  656. if ((chan->role & ROLE_REVENGE) && !chan_sentkick(m)) {
  657. m->flags |= SENTKICK;
  658. dprintf(DP_MODE_NEXT, "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_REVENGE));
  659. } else {
  660. if (m->user) {
  661. char tmp[128] = "";
  662. simple_snprintf(tmp, sizeof(tmp), "Banned bot %s (%s) on %s", matched_bot->nick, mask, chan->dname);
  663. deflag_user(m->user, DEFLAG_EVENT_REVENGE_BAN, tmp, chan);
  664. }
  665. }
  666. }
  667. if ((wild_match(mask, me->from) || match_cidr(mask, me->fromip)) && !isexempted(chan, me->from)) {
  668. add_mode(chan, '-', 'b', mask);
  669. reversing = 1;
  670. return;
  671. }
  672. if (m && !m->is_me) {
  673. if (channel_nouserbans(chan) && !glob_bot(user)) {
  674. add_mode(chan, '-', 'b', mask);
  675. return;
  676. }
  677. /* remove bans on ops unless a master/bot set it */
  678. for (size_t n = 0; n < matchedUserMembers.size(); ++n) {
  679. const memberlist *mv = matchedUserMembers[n];
  680. get_user_flagrec(mv->user, &victim, chan->dname, chan);
  681. if (!(glob_kick(victim) || chan_kick(victim)) &&
  682. (((chk_op(victim, chan) && !chan_master(user) && !glob_master(user) && !glob_bot(user)) ||
  683. (mv->user->bot && findbot(mv->user->handle))))) {
  684. /* if (target_priority(chan, m, 0)) */
  685. add_mode(chan, '-', 'b', mask);
  686. return;
  687. }
  688. }
  689. }
  690. refresh_exempt(chan, mask);
  691. /* This looks for bans added through bot and tacks on banned: if a description is found */
  692. if (m && channel_enforcebans(chan)) {
  693. maskrec *b = NULL;
  694. char resn[512] = "";
  695. /* The point of this cycle crap is to first check chan->bans then global_bans for a reason */
  696. for (int cycle = 0; cycle < 2; cycle++) {
  697. for (b = cycle ? chan->bans : global_bans; b; b = b->next) {
  698. if (wild_match(b->mask, mask) || match_cidr(b->mask, mask)) {
  699. if (b->desc && b->desc[0] != '@')
  700. simple_snprintf(resn, sizeof resn, "banned: %s", b->desc);
  701. else
  702. resn[0] = 0;
  703. }
  704. }
  705. }
  706. kick_all(chan, mask, resn[0] ? (const char *) resn : r_banned(chan), m->is_me ? 0 : 1);
  707. }
  708. if (!m && (bounce_bans || bounce_modes) &&
  709. (!u_equals_mask(global_bans, mask) || !u_equals_mask(chan->bans, mask)))
  710. add_mode(chan, '-', 'b', mask);
  711. }
  712. static void
  713. got_unban(struct chanset_t *chan, memberlist *m, char *mask)
  714. {
  715. masklist *b = NULL, *old = NULL;
  716. for (b = chan->channel.ban; b->mask[0] && rfc_casecmp(b->mask, mask); old = b, b = b->next) ;
  717. if (b->mask[0]) {
  718. if (old)
  719. old->next = b->next;
  720. else
  721. chan->channel.ban = b->next;
  722. free(b->mask);
  723. free(b->who);
  724. free(b);
  725. } else {
  726. /* The ban was not even set, why continue? */
  727. return;
  728. }
  729. if (channel_pending(chan))
  730. return;
  731. if (u_sticky_mask(chan->bans, mask) || u_sticky_mask(global_bans, mask)) {
  732. /* That's a sticky ban! No point in being
  733. * sticky unless we enforce it!!
  734. */
  735. add_mode(chan, '+', 'b', mask);
  736. }
  737. if ((u_equals_mask(global_bans, mask) || u_equals_mask(chan->bans, mask)) &&
  738. me_op(chan) && !channel_dynamicbans(chan)) {
  739. /* That's a permban! */
  740. if (!glob_bot(user) && !chk_op(user, chan))
  741. add_mode(chan, '+', 'b', mask);
  742. }
  743. }
  744. static void
  745. got_exempt(struct chanset_t *chan, memberlist *m, char *mask, char *isserver)
  746. {
  747. char s[UHOSTLEN] = "";
  748. simple_snprintf(s, sizeof(s), "%s!%s", m ? m->nick : "", m ? m->userhost : isserver);
  749. if (newexempt(chan, mask, s))
  750. return; /* The exempt was already set, don't bother with it */
  751. if (channel_pending(chan))
  752. return;
  753. if (m && !m->is_me) { /* It's not my exemption */
  754. if (channel_nouserexempts(chan) && !glob_bot(user) && !glob_master(user) && !chan_master(user)) {
  755. /* No exempts made by users */
  756. add_mode(chan, '-', 'e', mask);
  757. return;
  758. }
  759. }
  760. if (reversing || (!m && bounce_exempts &&
  761. (!u_equals_mask(global_exempts, mask) || !u_equals_mask(chan->exempts, mask))))
  762. add_mode(chan, '-', 'e', mask);
  763. }
  764. static void
  765. got_unexempt(struct chanset_t *chan, memberlist *m, char *mask)
  766. {
  767. masklist *e = chan->channel.exempt, *old = NULL;
  768. masklist *b = NULL;
  769. int match = 0;
  770. while (e && e->mask[0] && rfc_casecmp(e->mask, mask)) {
  771. old = e;
  772. e = e->next;
  773. }
  774. if (e && e->mask[0]) {
  775. if (old)
  776. old->next = e->next;
  777. else
  778. chan->channel.exempt = e->next;
  779. free(e->mask);
  780. free(e->who);
  781. free(e);
  782. } else {
  783. /* The exempt was not even set, why continue? */
  784. return;
  785. }
  786. if (channel_pending(chan))
  787. return;
  788. if (u_sticky_mask(chan->exempts, mask) || u_sticky_mask(global_exempts, mask)) {
  789. /* That's a sticky exempt! No point in being sticky unless we enforce it!!
  790. */
  791. add_mode(chan, '+', 'e', mask);
  792. }
  793. /* If exempt was removed by master then leave it else check for bans */
  794. /* FIXME: this is impossible, if server !isbot ? */
  795. if (!m && glob_bot(user) && !glob_master(user) && !chan_master(user)) {
  796. b = chan->channel.ban;
  797. while (b->mask[0] && !match) {
  798. if (wild_match(b->mask, mask) || wild_match(mask, b->mask)) {
  799. add_mode(chan, '+', 'e', mask);
  800. match = 1;
  801. } else
  802. b = b->next;
  803. }
  804. }
  805. if ((u_equals_mask(global_exempts, mask) || u_equals_mask(chan->exempts, mask)) &&
  806. me_op(chan) && !channel_dynamicexempts(chan) && !glob_bot(user))
  807. add_mode(chan, '+', 'e', mask);
  808. if (channel_enforcebans(chan))
  809. enforce_bans(chan);
  810. }
  811. static void
  812. got_invite(struct chanset_t *chan, memberlist *m, char *mask, char *isserver)
  813. {
  814. char s[UHOSTLEN] = "";
  815. simple_snprintf(s, sizeof(s), "%s!%s", m ? m->nick : "", m ? m->userhost : isserver);
  816. if (newinvite(chan, mask, s))
  817. return; /* The Invite was already set, don't bother with it */
  818. if (channel_pending(chan))
  819. return;
  820. if (m && !m->is_me) { /* It's not my invitation */
  821. if (channel_nouserinvites(chan) && !glob_bot(user) && !glob_master(user) && !chan_master(user)) {
  822. /* No exempts made by users */
  823. add_mode(chan, '-', 'I', mask);
  824. return;
  825. }
  826. }
  827. if (reversing || (bounce_invites && !m &&
  828. (!u_equals_mask(global_invites, mask) || !u_equals_mask(chan->invites, mask))))
  829. add_mode(chan, '-', 'I', mask);
  830. }
  831. static void
  832. got_uninvite(struct chanset_t *chan, memberlist *m, char *mask)
  833. {
  834. masklist *inv = chan->channel.invite, *old = NULL;
  835. while (inv->mask[0] && rfc_casecmp(inv->mask, mask)) {
  836. old = inv;
  837. inv = inv->next;
  838. }
  839. if (inv->mask[0]) {
  840. if (old)
  841. old->next = inv->next;
  842. else
  843. chan->channel.invite = inv->next;
  844. free(inv->mask);
  845. free(inv->who);
  846. free(inv);
  847. } else {
  848. /* The Invite was not even set, why continue? */
  849. return;
  850. }
  851. if (channel_pending(chan))
  852. return;
  853. if (u_sticky_mask(chan->invites, mask) || u_sticky_mask(global_invites, mask)) {
  854. /* That's a sticky invite! No point in being sticky unless we enforce it!!
  855. */
  856. add_mode(chan, '+', 'I', mask);
  857. }
  858. if (!m && glob_bot(user) && !glob_master(user) && !chan_master(user) && (chan->channel.mode & CHANINV))
  859. add_mode(chan, '+', 'I', mask);
  860. if ((u_equals_mask(global_invites, mask) ||
  861. u_equals_mask(chan->invites, mask)) && me_op(chan) && !channel_dynamicinvites(chan) && !glob_bot(user))
  862. add_mode(chan, '+', 'I', mask);
  863. }
  864. static memberlist *assert_ismember(struct chanset_t *chan, const char *nick)
  865. {
  866. memberlist *m = ismember(chan, nick);
  867. if (m) {
  868. member_getuser(m);
  869. } else {
  870. if (channel_pending(chan))
  871. return NULL;
  872. putlog(LOG_MISC, chan->dname, "* Mode change on %s for nonexistant %s!", chan->dname, nick);
  873. send_chan_who(DP_MODE, chan);
  874. }
  875. return m;
  876. }
  877. static int
  878. gotmode(char *from, char *msg)
  879. {
  880. #define msign modes[i][0]
  881. #define mmode modes[i][1]
  882. /* 2 = space */
  883. #define mparam &modes[i][3]
  884. /* Usermode changes? */
  885. if (msg[0] && (strchr(CHANMETA, msg[0]) != NULL)) {
  886. char *ch = newsplit(&msg);
  887. struct chanset_t *chan = findchan(ch);
  888. if (unlikely(!chan)) {
  889. if (unlikely(match_my_nick(ch)))
  890. return 0;
  891. putlog(LOG_MISC, "*", "Oops. Someone made me join %s... leaving...", ch);
  892. dprintf(DP_SERVER, "PART %s\n", ch);
  893. return 0;
  894. }
  895. /* let's pre-emptively check for mass op/deop, manual ops and cookieops */
  896. if ((channel_active(chan) || channel_pending(chan))) {
  897. char isserver[100] = "";
  898. size_t z = strlen(msg);
  899. struct userrec *u = NULL;
  900. memberlist *m = NULL;
  901. char *nick = NULL;
  902. if (!strchr(from, '!'))
  903. strlcpy(isserver, from, sizeof(isserver));
  904. if (msg[--z] == ' ') /* I hate cosmetic bugs :P -poptix */
  905. msg[z] = 0;
  906. /* Split up from */
  907. if (!isserver[0]) {
  908. nick = splitnick(&from);
  909. if ((m = ismember(chan, nick))) {
  910. m->last = now;
  911. member_getuser(m);
  912. u = m->user;
  913. } else {
  914. if (channel_pending(chan))
  915. return 0;
  916. dprintf(DP_MODE, "KICK %s %s :Desync\n", chan->name[0] ? chan->name : chan->dname, nick);
  917. putlog(LOG_MISC, chan->dname, "* Mode change on %s from nonexistant %s!", chan->dname, nick);
  918. send_chan_who(DP_MODE, chan);
  919. return 0;
  920. }
  921. } else
  922. nick = splitnick(&from);
  923. int i = 0, modecnt = 0, ops = 0, deops = 0, bans = 0, unbans = 0;
  924. bool me_opped = 0;
  925. char **modes = (char **) calloc(1, sizeof(char *));
  926. char s[UHOSTLEN] = "";
  927. memberlist *mv = NULL;
  928. massop = reversing = 0;
  929. irc_log(chan, "%s!%s sets mode: %s", nick, from, msg);
  930. get_user_flagrec(u, &user, ch);
  931. if (1) { // Place in block to hint chg/sign to be destroyed when done
  932. /* Split up the mode: #chan modes param param param param */
  933. char *chg = newsplit(&msg);
  934. char sign = 0;
  935. while (*chg) { /* +MODES PARAM PARAM PARAM ... */
  936. if (strchr("+-", *chg))
  937. sign = *chg;
  938. else {
  939. char* mp = NULL;
  940. if (strchr("beIlkov", chg[0])) {
  941. mp = newsplit(&msg); /* PARAM as noted above */
  942. fixcolon(mp);
  943. }
  944. /* Just want o's and b's */
  945. modes = (char **) realloc(modes, (modecnt * sizeof(char *)) + sizeof(char *));
  946. // char **modes = (char **) calloc(modesperline + 1, sizeof(char *));
  947. size_t siz = (mp ? strlen(mp) : 0) + 3 + 1;
  948. modes[modecnt] = (char *) calloc(1, siz);
  949. simple_snprintf(modes[modecnt], siz, "%c%c %s", sign, chg[0], mp ? mp : "");
  950. ++modecnt;
  951. if (chg[0] == 'o') {
  952. if (sign == '+') {
  953. ++ops;
  954. if (match_my_nick(mp))
  955. me_opped = 1;
  956. } else {
  957. deops++;
  958. if (match_my_nick(mp))
  959. me_opped = 0;
  960. }
  961. } else if (chg[0] == 'b') {
  962. if (sign == '+')
  963. ++bans;
  964. else
  965. ++unbans;
  966. }
  967. }
  968. ++chg;
  969. }
  970. }
  971. /* take ASAP */
  972. if (me_opped && !me_op(chan) && channel_take(chan))
  973. do_take(chan);
  974. /* Now we got modes[], chan, u, nick, and count of each relevant mode */
  975. /* check for mdop */
  976. if (me_op(chan)) {
  977. char tmp[1024] = "";
  978. if (!isserver[0] && chan->role && (!u || (u && !u->bot))) {
  979. if (m && deops >= 3) {
  980. if (chan->mdop) {
  981. if ((chan->role & ROLE_PROTECT) && !chan_sentkick(m)) {
  982. m->flags |= SENTKICK;
  983. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_MASSDEOP));
  984. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  985. } else {
  986. if (u) {
  987. simple_snprintf(tmp, sizeof(tmp), "Mass deop on %s by %s", chan->dname, m->nick);
  988. deflag_user(u, DEFLAG_EVENT_MDOP, tmp, chan);
  989. }
  990. }
  991. reversing = mdop_reversing = 1;
  992. }
  993. if (channel_protect(chan))
  994. do_protect(chan, "Mass Deop");
  995. }
  996. /* check for mop */
  997. if (ops >= 3) {
  998. if (chan->mop) {
  999. if (m && !chan_sentkick(m)) {
  1000. if ((chan->role & ROLE_PROTECT)) {
  1001. m->flags |= SENTKICK;
  1002. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_MANUALOP));
  1003. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  1004. } else {
  1005. if (u) {
  1006. simple_snprintf(tmp, sizeof(tmp), "Mass op on %s by %s", chan->dname, m->nick);
  1007. deflag_user(u, DEFLAG_EVENT_MOP, tmp, chan);
  1008. }
  1009. }
  1010. }
  1011. // Don't mass deop if protect is set, it'll happen anyway below
  1012. if (channel_bitch(chan) && !channel_protect(chan))
  1013. enforce_bitch(chan); /* deop quick! */
  1014. else
  1015. reversing = massop = 1; // Reverse via got_op
  1016. }
  1017. if (channel_protect(chan))
  1018. do_protect(chan, "Mass OP");
  1019. }
  1020. }
  1021. if (ops) {
  1022. /* Check cookies */
  1023. if (u && m && u->bot && !channel_fastop(chan) && !channel_take(chan) && !cookies_disabled) {
  1024. int isbadop = 0;
  1025. bool failure = 0;
  1026. /* If no unbans or the -b is not the LAST mode, it's bad. */
  1027. if (unbans != 1 || (strncmp(modes[modecnt - 1], "-b", 2))) {
  1028. isbadop = BC_NOCOOKIE;
  1029. failure = 1;
  1030. } else {
  1031. /* Check the hash for each opped nick and punish the opped client if it fails
  1032. * Punish the opper lastly (and once)
  1033. */
  1034. for (i = 0; i < (modecnt - 1); i++) { /* Don't need to hit the -b */
  1035. if (msign == '+' && mmode == 'o') {
  1036. mv = assert_ismember(chan, mparam);
  1037. // Unknown client - I am desycned, don't punish or it may lead to fight
  1038. if (!mv) continue;
  1039. const char *cookie = &(modes[modecnt - 1][3]);
  1040. if ((isbadop = checkcookie(chan->dname, m, mv, cookie, i))) {
  1041. //if (!failure) { /* First failure */
  1042. failure = 1;
  1043. //}
  1044. /* Kick the opped client */
  1045. if (randint(7) == (unsigned int) i) {
  1046. if (!mv || !chan_sentkick(mv)) {
  1047. if (mv)
  1048. mv->flags |= SENTKICK;
  1049. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, mparam, kickprefix, response(RES_BADOPPED));
  1050. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  1051. }
  1052. }
  1053. }
  1054. }
  1055. }
  1056. }
  1057. if (unlikely(failure)) { /* One of the hashes failed! */
  1058. /* Did *I* do this heinous act? */
  1059. if (m->is_me) {
  1060. detected(DETECT_HIJACK, "Possible Hijack: bad cookie");
  1061. }
  1062. if (randint(7) == 0) {
  1063. /* Kick opper */
  1064. if (!chan_sentkick(m)) {
  1065. m->flags |= SENTKICK;
  1066. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_BADOP));
  1067. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  1068. }
  1069. simple_snprintf(tmp, sizeof(tmp), "%s MODE %s %s", m->from, chan->dname, modes[modecnt - 1]);
  1070. deflag_user(u, DEFLAG_EVENT_BADCOOKIE, tmp, chan);
  1071. }
  1072. /* Do the logging last as it can slow down the KICK pushing */
  1073. putlog(LOG_WARNING, "*", "%s opped in %s with bad cookie(%d): %s", m->nick, chan->dname, isbadop, msg);
  1074. if (isbadop == BC_NOCOOKIE)
  1075. putlog(LOG_WARN, "*", "Missing cookie: %s!%s MODE %s %s",
  1076. m->nick, m->userhost, chan->dname, modes[modecnt - 1]);
  1077. else if (isbadop == BC_HASH)
  1078. putlog(LOG_WARN, "*", "Invalid cookie (bad hash): %s!%s MODE %s %s",
  1079. m->nick, m->userhost, chan->dname, modes[modecnt - 1]);
  1080. else if (isbadop == BC_SLACK)
  1081. putlog(LOG_WARN, "*", "Invalid cookie (bad time): %s!%s MODE %s %s",
  1082. m->nick, m->userhost, chan->dname, modes[modecnt - 1]);
  1083. else if (isbadop == BC_COUNTER)
  1084. putlog(LOG_WARN, "*", "Invalid cookie (bad count): %s!%s MODE %s %s",
  1085. m->nick, m->userhost, chan->dname, modes[modecnt - 1]);
  1086. }
  1087. #ifdef DEBUG
  1088. else
  1089. putlog(LOG_DEBUG, "@", "Good op: %s", modes[modecnt - 1]);
  1090. #endif
  1091. }
  1092. /* manop */
  1093. if (chan->manop && u && !u->bot) {
  1094. if (m && (chan->role & ROLE_PROTECT)) {
  1095. /* Kick opper */
  1096. if (!chan_sentkick(m)) {
  1097. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_MANUALOP));
  1098. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  1099. m->flags |= SENTKICK;
  1100. }
  1101. simple_snprintf(tmp, sizeof(tmp), "%s MODE %s %s", m->from, chan->dname, modes[modecnt - 1]);
  1102. deflag_user(u, DEFLAG_EVENT_MANUALOP, tmp, chan);
  1103. }
  1104. if (chan->role & ROLE_KICK) {
  1105. for (i = 0; i < modecnt; i++) {
  1106. if (msign == '+' && mmode == 'o' && !match_my_nick(mparam)) {
  1107. mv = ismember(chan, mparam);
  1108. if (!mv || !chan_sentkick(mv)) {
  1109. if (mv)
  1110. mv->flags |= SENTKICK;
  1111. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, mparam, kickprefix, response(RES_MANUALOPPED));
  1112. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  1113. }
  1114. }
  1115. }
  1116. }
  1117. }
  1118. }
  1119. }
  1120. /* Now do the modes again, this time thoroughly... */
  1121. if (m && channel_active(chan) && me_op(chan)) {
  1122. if (chan_fakeop(m)) {
  1123. putlog(LOG_MODES, ch, "Mode change by fake op on %s! Reversing...", ch);
  1124. dprintf(DP_MODE, "KICK %s %s :%sAbusing ill-gained server ops\n", ch, m->nick, kickprefix);
  1125. m->flags |= SENTKICK;
  1126. reversing = 1;
  1127. } else if (!chan_hasop(m) && !channel_nodesynch(chan)) {
  1128. if (u && u->bot && chk_op(user, chan)) {
  1129. putlog(LOG_MODES, ch, "Mode change by friendly non-chanop on %s! Opping...", ch);
  1130. do_op(m, chan, 0, 0);
  1131. } else {
  1132. putlog(LOG_MODES, ch, "Mode change by non-chanop on %s! Reversing...", ch);
  1133. dprintf(DP_MODE, "KICK %s %s :%sAbusing desync\n", ch, m->nick, kickprefix);
  1134. m->flags |= SENTKICK;
  1135. reversing = 1;
  1136. }
  1137. }
  1138. }
  1139. for (i = 0; i < modecnt; i++) {
  1140. int todo = 0;
  1141. if (isserver[0] && bounce_modes)
  1142. reversing = 1;
  1143. switch (mmode) { /* parse mode */
  1144. case 'i':
  1145. todo = CHANINV;
  1146. if (isserver[0] && (bounce_modes))
  1147. reversing = 1;
  1148. break;
  1149. case 'p':
  1150. todo = CHANPRIV;
  1151. if (isserver[0] && (bounce_modes))
  1152. reversing = 1;
  1153. break;
  1154. case 's':
  1155. todo = CHANSEC;
  1156. if (isserver[0] && (bounce_modes))
  1157. reversing = 1;
  1158. break;
  1159. case 'm':
  1160. todo = CHANMODER;
  1161. if (isserver[0] && (bounce_modes))
  1162. reversing = 1;
  1163. break;
  1164. case 'c':
  1165. todo = CHANNOCLR;
  1166. if (isserver[0] && (bounce_modes))
  1167. reversing = 1;
  1168. break;
  1169. case 'C':
  1170. todo = CHANNOCTCP;
  1171. if (isserver[0] && (bounce_modes))
  1172. reversing = 1;
  1173. break;
  1174. case 'R':
  1175. todo = CHANREGON;
  1176. if (isserver[0] && (bounce_modes))
  1177. reversing = 1;
  1178. break;
  1179. case 'M':
  1180. todo = CHANMODR;
  1181. if (isserver[0] && (bounce_modes))
  1182. reversing = 1;
  1183. break;
  1184. case 'r':
  1185. todo = CHANLONLY;
  1186. if (isserver[0] && (bounce_modes))
  1187. reversing = 1;
  1188. break;
  1189. case 't':
  1190. todo = CHANTOPIC;
  1191. if (isserver[0] && (bounce_modes))
  1192. reversing = 1;
  1193. break;
  1194. case 'n':
  1195. todo = CHANNOMSG;
  1196. if (isserver[0] && (bounce_modes))
  1197. reversing = 1;
  1198. break;
  1199. case 'a':
  1200. todo = CHANANON;
  1201. if (isserver[0] && (bounce_modes))
  1202. reversing = 1;
  1203. break;
  1204. case 'q':
  1205. todo = CHANQUIET;
  1206. if (isserver[0] && (bounce_modes))
  1207. reversing = 1;
  1208. break;
  1209. case 'l':
  1210. if (isserver[0] && (bounce_modes))
  1211. reversing = 1;
  1212. if (msign == '-') {
  1213. if (channel_active(chan)) {
  1214. if ((reversing) && (chan->channel.maxmembers != 0)) {
  1215. simple_snprintf(s, sizeof(s), "%d", chan->channel.maxmembers);
  1216. add_mode(chan, '+', 'l', s);
  1217. } else if ((chan->limit_prot != 0) && !glob_master(user) && !chan_master(user)) {
  1218. simple_snprintf(s, sizeof(s), "%d", chan->limit_prot);
  1219. add_mode(chan, '+', 'l', s);
  1220. } else {
  1221. if (chan->limitraise && dolimit(chan) && (!chan_master(user) && !glob_master(user) && !glob_bot(user))) {
  1222. chan->channel.maxmembers = 0; /* set this to 0 so a new limit is generated */
  1223. raise_limit(chan);
  1224. }
  1225. }
  1226. }
  1227. chan->channel.maxmembers = 0;
  1228. } else { /* + */
  1229. if (*mparam == '\0')
  1230. break;
  1231. chan->channel.maxmembers = atoi(mparam);
  1232. if (channel_pending(chan))
  1233. break;
  1234. if (((reversing) &&
  1235. !(chan->mode_pls_prot & CHANLIMIT)) ||
  1236. ((chan->mode_mns_prot & CHANLIMIT) &&
  1237. !glob_bot(user) && !glob_master(user) && !chan_master(user)))
  1238. add_mode(chan, '-', 'l', "");
  1239. if ((chan->limit_prot != chan->channel.maxmembers) && (chan->mode_pls_prot & CHANLIMIT) && (chan->limit_prot != 0) &&
  1240. !glob_bot(user) && !glob_master(user) && !chan_master(user)) {
  1241. simple_snprintf(s, sizeof(s), "%d", chan->limit_prot);
  1242. add_mode(chan, '+', 'l', s);
  1243. }
  1244. if (chan->limitraise && dolimit(chan) && !glob_bot(user) && (!chan_master(user) && !glob_master(user)))
  1245. raise_limit(chan);
  1246. }
  1247. break;
  1248. case 'k':
  1249. if (msign == '+')
  1250. chan->channel.mode |= CHANKEY;
  1251. else
  1252. chan->channel.mode &= ~CHANKEY;
  1253. if (*mparam == '\0')
  1254. break;
  1255. if (msign == '+') {
  1256. my_setkey(chan, mparam);
  1257. if (channel_active(chan))
  1258. got_key(chan, mparam);
  1259. } else {
  1260. if (channel_active(chan)) {
  1261. if ((reversing) && (chan->channel.key[0]))
  1262. add_mode(chan, '+', 'k', chan->channel.key);
  1263. else if ((chan->key_prot[0]) && !glob_master(user) && !chan_master(user))
  1264. add_mode(chan, '+', 'k', chan->key_prot);
  1265. }
  1266. my_setkey(chan, NULL);
  1267. }
  1268. break;
  1269. case 'o':
  1270. if (!u || (u && !u->bot))
  1271. chan->channel.fighting++;
  1272. mv = assert_ismember(chan, mparam);
  1273. if (mv) {
  1274. if (msign == '+')
  1275. got_op(chan, m, mv);
  1276. else
  1277. got_deop(chan, m, mv, isserver);
  1278. }
  1279. break;
  1280. case 'v':
  1281. mv = assert_ismember(chan, mparam);
  1282. if (mv) {
  1283. bool dv = 0;
  1284. get_user_flagrec(mv->user, &victim, chan->dname, chan);
  1285. if (msign == '+') {
  1286. if (mv->flags & EVOICE) {
  1287. if (!chk_op(user, chan) && !chk_voice(victim, chan)) {
  1288. dv = 1;
  1289. } else {
  1290. mv->flags &= ~EVOICE;
  1291. }
  1292. }
  1293. mv->flags &= ~SENTVOICE;
  1294. mv->flags |= CHANVOICE;
  1295. if (channel_active(chan) && dovoice(chan)) {
  1296. if (dv || chk_devoice(victim) || (channel_voicebitch(chan) && !chk_voice(victim, chan))) {
  1297. add_mode(chan, '-', 'v', mparam);
  1298. } else if (reversing) {
  1299. add_mode(chan, '-', 'v', mparam);
  1300. }
  1301. }
  1302. } else if (msign == '-') {
  1303. mv->flags &= ~SENTDEVOICE;
  1304. mv->flags &= ~CHANVOICE;
  1305. if (channel_active(chan) && dovoice(chan) && !chan_hasop(mv)) {
  1306. /* revoice +v users */
  1307. if (chk_voice(victim, chan)) {
  1308. add_mode(chan, '+', 'v', mparam);
  1309. } else if (reversing) {
  1310. add_mode(chan, '+', 'v', mparam);
  1311. /* if they arent +v|v and VOICER is m+ then EVOICE them */
  1312. } else {
  1313. if (!match_my_nick(nick) && channel_voice(chan) &&
  1314. (chk_op(user, chan) || glob_bot(user)) &&
  1315. rfc_casecmp(nick, mparam)) {
  1316. /* if the user is not +q set them norEVOICE. */
  1317. if (!chan_quiet(victim)) {
  1318. set_devoice(chan, mv);
  1319. }
  1320. }
  1321. }
  1322. }
  1323. }
  1324. }
  1325. break;
  1326. case 'b':
  1327. if (!u || (u && !u->bot))
  1328. chan->channel.fighting++;
  1329. if (msign == '+')
  1330. got_ban(chan, m, mparam, isserver);
  1331. else
  1332. got_unban(chan, m, mparam);
  1333. break;
  1334. case 'e':
  1335. if (!u || (u && !u->bot))
  1336. chan->channel.fighting++;
  1337. if (msign == '+')
  1338. got_exempt(chan, m, mparam, isserver);
  1339. else
  1340. got_unexempt(chan, m, mparam);
  1341. break;
  1342. case 'I':
  1343. if (!u || (u && !u->bot))
  1344. chan->channel.fighting++;
  1345. if (msign == '+')
  1346. got_invite(chan, m, mparam, isserver);
  1347. else
  1348. got_uninvite(chan, m, mparam);
  1349. break;
  1350. }
  1351. if (todo) {
  1352. if (msign == '+')
  1353. chan->channel.mode |= todo;
  1354. else
  1355. chan->channel.mode &= ~todo;
  1356. if (channel_active(chan)) {
  1357. if ((((msign == '+') && (chan->mode_mns_prot & todo)) ||
  1358. ((msign == '-') && (chan->mode_pls_prot & todo))) &&
  1359. !glob_master(user) && !chan_master(user))
  1360. add_mode(chan, msign == '+' ? '-' : '+', mmode, "");
  1361. else if (reversing &&
  1362. ((msign == '+') || (chan->mode_pls_prot & todo)) &&
  1363. ((msign == '-') || (chan->mode_mns_prot & todo)))
  1364. add_mode(chan, msign == '+' ? '-' : '+', mmode, "");
  1365. }
  1366. }
  1367. }
  1368. // If reversing, dump the modes out
  1369. if (reversing)
  1370. flush_mode(chan, QUICK);
  1371. for (i = 0; i < modecnt; i++)
  1372. if (modes[i])
  1373. free(modes[i]);
  1374. free(modes);
  1375. if (chan->channel.do_opreq)
  1376. request_op(chan);
  1377. if (!me_op(chan) && isserver[0]) /* FIXME, WTF IS THIS? */
  1378. chan->ircnet_status |= CHAN_ASKEDMODES;
  1379. }
  1380. }
  1381. return 0;
  1382. }
  1383. /* vim: set sts=2 sw=2 ts=8 et: */