mode.c 51 KB

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