mode.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585
  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. if (mv->user) {
  480. get_user_flagrec(mv->user, &victim, chan->dname, chan);
  481. // Did some other bot just get opped, and I'm not opped yet?
  482. if (mv->user->bot && !me_opped && !meop) {
  483. chan->channel.do_opreq = 1;
  484. }
  485. }
  486. /* Flags need to be set correctly right from the beginning now, so that
  487. * add_mode() doesn't get irritated.
  488. */
  489. mv->flags |= CHANOP;
  490. /* Added new meaning of WASOP:
  491. * in mode binds it means: was he op before get (de)opped
  492. * (stupid IrcNet allows opped users to be opped again and
  493. * opless users to be deopped)
  494. * script now can use [wasop nick chan] proc to check
  495. * if user was op or wasnt (drummer)
  496. */
  497. mv->flags &= ~SENTOP;
  498. /* This skips the recheck_channel until we get the end of the WHO list */
  499. if (channel_pending(chan))
  500. return;
  501. /* I'm opped, and the opper isn't me, and it isn't a server op */
  502. if (m && meop && !me_opped) {
  503. /* deop if they are +d or it is +bitch */
  504. int bitch = chan_bitch(chan);
  505. if (reversing || chk_deop(victim, chan) || (!loading && userlist && bitch && !chk_op(victim, chan))) { /* chk_op covers +private */
  506. int num = randint(10);
  507. char outbuf[101] = "";
  508. size_t len = 0;
  509. /* should kick the oppee first, then deal with the opper */
  510. #ifdef old
  511. if (num == 4) {
  512. len = simple_snprintf(outbuf, sizeof(outbuf), "MODE %s -o %s\r\n", chan->name, mv->nick);
  513. mv->flags |= SENTDEOP;
  514. } else if (num == 5) {
  515. len = simple_snprintf(outbuf, sizeof(outbuf), "MODE %s -o %s\r\n", chan->name, m->nick);
  516. m->flags |= SENTDEOP;
  517. #endif
  518. if (num == 5) {
  519. add_mode(chan, '-', 'o', m->nick);
  520. } else if (bitch && num == 6) {
  521. len = simple_snprintf(outbuf, sizeof(outbuf), "KICK %s %s :%s\r\n", chan->name, mv->nick, response(RES_BITCHOPPED));
  522. mv->flags |= SENTKICK;
  523. } else if (bitch && num == 7) {
  524. len = simple_snprintf(outbuf, sizeof(outbuf), "KICK %s %s :%s\r\n", chan->name, m->nick, response(RES_BITCHOP));
  525. m->flags |= SENTKICK;
  526. } else
  527. add_mode(chan, '-', 'o', mv->nick);
  528. if (len)
  529. dprintf_real(DP_MODE_NEXT, outbuf, len, sizeof(outbuf));
  530. }
  531. } else if (reversing && !me_opped)
  532. add_mode(chan, '-', 'o', mv->nick);
  533. /* server op */
  534. if (!m && meop && !me_opped) {
  535. if (chk_deop(victim, chan) || (chan_bitch(chan) && !chk_op(victim, chan))) {
  536. mv->flags |= FAKEOP;
  537. add_mode(chan, '-', 'o', mv->nick);
  538. }
  539. }
  540. mv->flags |= WASOP;
  541. if (check_chan) {
  542. #ifdef no
  543. /* tell other bots to set jointime to 0 and join */
  544. char *buf = NULL;
  545. size_t siz = strlen(chan->dname) + 3 + 1;
  546. buf = my_calloc(1, siz);
  547. simple_snprintf(buf, siz, "jn %s", chan->dname);
  548. putallbots(buf);
  549. free(buf);
  550. #endif
  551. /* check exempts/invites and shit */
  552. recheck_channel(chan, 2);
  553. }
  554. }
  555. static void
  556. got_deop(struct chanset_t *chan, memberlist *m, memberlist *mv, char *isserver)
  557. {
  558. const bool was_op = chan_hasop(mv);
  559. if (mdop_reversing && !was_op)
  560. return;
  561. /* m is NULL if a server made the change */
  562. if (mv->user) {
  563. get_user_flagrec(mv->user, &victim, chan->dname, chan);
  564. }
  565. /* Flags need to be set correctly right from the beginning now, so that
  566. * add_mode() doesn't get irritated.
  567. */
  568. mv->flags &= ~(CHANOP | SENTDEOP | FAKEOP);
  569. /* Check comments in got_op() (drummer) */
  570. mv->flags &= ~WASOP;
  571. if (channel_pending(chan))
  572. return;
  573. /* Deop'd someone on my oplist? */
  574. if (me_op(chan)) {
  575. /* do we want to reop victim? */
  576. if (
  577. /* I didn't deop them, another bot didn't deop them and they didn't deop themselves. */
  578. (was_op && ((m && !m->is_me && mv != m && !(m->user && m->user->bot)) || (!m))) && (
  579. /*
  580. * reversing
  581. * They are either an op or this chan is -bitch
  582. */
  583. (reversing && (!chan_bitch(chan) || chk_op(victim, chan))) ||
  584. /* Reop bots to avoid them needing to ask */
  585. (role == 3 && mv->user && mv->user->bot && chk_op(victim, chan))
  586. )
  587. ) {
  588. /* Then we'll bless the victim */
  589. do_op(mv->nick, chan, 0, 0);
  590. }
  591. }
  592. if (isserver[0]) /* !m */
  593. putlog(LOG_MODES, chan->dname, "TS resync (%s): %s deopped by %s", chan->dname, mv->nick, isserver);
  594. /* Check for mass deop */
  595. else if (m) {
  596. detect_chan_flood(m, m->from, chan, FLOOD_DEOP, mv->nick);
  597. }
  598. /* Having op hides your +v and +h status -- so now that someone's lost ops,
  599. * check to see if they have +v or +h
  600. */
  601. // WHO CARES
  602. // if (!channel_take(chan) && !chan_bitch(chan) && !(mv->flags & (CHANVOICE | STOPWHO))) {
  603. // send_chan_who(DP_HELP, chan);
  604. // mv->flags |= STOPWHO;
  605. // }
  606. /* Was the bot deopped? */
  607. if (mv->is_me) {
  608. /* Cancel any pending kicks and modes */
  609. memberlist *m2 = NULL;
  610. for (m2 = chan->channel.member; m2 && m2->nick[0]; m2 = m2->next)
  611. m2->flags &= ~(SENTKICK | SENTDEOP | SENTOP | SENTVOICE | SENTDEVOICE);
  612. chan->channel.do_opreq = 1;
  613. /* request_op(chan); */
  614. /* need: op */
  615. if (!m)
  616. putlog(LOG_MODES, chan->dname, "TS resync deopped me on %s :(", chan->dname);
  617. } else {
  618. // Revenge kick clients that deop our bots
  619. if (chan->revenge && m && m != mv && mv->user && mv->user->bot && !(m->user && m->user->bot)) {
  620. if (role < 5 && !chan_sentkick(m) && me_op(chan)) {
  621. m->flags |= SENTKICK;
  622. dprintf(DP_MODE_NEXT, "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_REVENGE));
  623. } else {
  624. if (m->user) {
  625. char tmp[128] = "";
  626. simple_snprintf(tmp, sizeof(tmp), "Deopped bot %s on %s", mv->nick, chan->dname);
  627. deflag_user(m->user, DEFLAG_EVENT_REVENGE_DEOP, tmp, chan);
  628. }
  629. }
  630. }
  631. }
  632. }
  633. static void
  634. got_ban(struct chanset_t *chan, memberlist *m, char *mask, char *isserver)
  635. {
  636. const memberlist *me = ismember(chan, botname);
  637. char s[UHOSTLEN] = "";
  638. simple_snprintf(s, sizeof s, "%s!%s", m ? m->nick : "", m ? m->userhost : isserver);
  639. if (m && newban(chan, mask, m->from))
  640. return; /* The ban was already set, don't bother with it */
  641. if (channel_pending(chan) || !me_op(chan))
  642. return;
  643. // Make an array of all matching users
  644. bd::Array<memberlist*> matchedUserMembers;
  645. memberlist *matched_bot = NULL;
  646. for (memberlist *mv = chan->channel.member; mv && mv->nick[0]; mv = mv->next) {
  647. member_getuser(mv);
  648. if (mv->user) {
  649. if ((wild_match(mask, mv->from) || match_cidr(mask, mv->from))) {
  650. if (!matched_bot && mv != m && mv->user->bot) {
  651. matched_bot = mv;
  652. }
  653. if (mv->user && !isexempted(chan, mv->from)) {
  654. matchedUserMembers << mv;
  655. }
  656. }
  657. }
  658. }
  659. // Revenge kick clients that ban our bots
  660. if (chan->revenge && m && matched_bot && !(m->user && m->user->bot)) {
  661. if (role < 5 && !chan_sentkick(m)) {
  662. m->flags |= SENTKICK;
  663. dprintf(DP_MODE_NEXT, "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_REVENGE));
  664. } else {
  665. if (m->user) {
  666. char tmp[128] = "";
  667. simple_snprintf(tmp, sizeof(tmp), "Banned bot %s (%s) on %s", matched_bot->nick, mask, chan->dname);
  668. deflag_user(m->user, DEFLAG_EVENT_REVENGE_BAN, tmp, chan);
  669. }
  670. }
  671. }
  672. if ((wild_match(mask, me->from) || match_cidr(mask, me->fromip)) && !isexempted(chan, me->from)) {
  673. add_mode(chan, '-', 'b', mask);
  674. reversing = 1;
  675. return;
  676. }
  677. if (m && !m->is_me) {
  678. if (channel_nouserbans(chan) && !glob_bot(user)) {
  679. add_mode(chan, '-', 'b', mask);
  680. return;
  681. }
  682. /* remove bans on ops unless a master/bot set it */
  683. for (size_t n = 0; n < matchedUserMembers.size(); ++n) {
  684. const memberlist *mv = matchedUserMembers[n];
  685. get_user_flagrec(mv->user, &victim, chan->dname, chan);
  686. if (!(glob_kick(victim) || chan_kick(victim)) &&
  687. (((chk_op(victim, chan) && !chan_master(user) && !glob_master(user) && !glob_bot(user)) ||
  688. (mv->user->bot && findbot(mv->user->handle))))) {
  689. /* if (target_priority(chan, m, 0)) */
  690. add_mode(chan, '-', 'b', mask);
  691. return;
  692. }
  693. }
  694. }
  695. refresh_exempt(chan, mask);
  696. /* This looks for bans added through bot and tacks on banned: if a description is found */
  697. if (m && channel_enforcebans(chan)) {
  698. register maskrec *b = NULL;
  699. char resn[512] = "";
  700. /* The point of this cycle crap is to first check chan->bans then global_bans for a reason */
  701. for (int cycle = 0; cycle < 2; cycle++) {
  702. for (b = cycle ? chan->bans : global_bans; b; b = b->next) {
  703. if (wild_match(b->mask, mask) || match_cidr(b->mask, mask)) {
  704. if (b->desc && b->desc[0] != '@')
  705. simple_snprintf(resn, sizeof resn, "banned: %s", b->desc);
  706. else
  707. resn[0] = 0;
  708. }
  709. }
  710. }
  711. kick_all(chan, mask, resn[0] ? (const char *) resn : r_banned(chan), m->is_me ? 0 : 1);
  712. }
  713. if (!m && (bounce_bans || bounce_modes) &&
  714. (!u_equals_mask(global_bans, mask) || !u_equals_mask(chan->bans, mask)))
  715. add_mode(chan, '-', 'b', mask);
  716. }
  717. static void
  718. got_unban(struct chanset_t *chan, memberlist *m, char *mask)
  719. {
  720. masklist *b = NULL, *old = NULL;
  721. for (b = chan->channel.ban; b->mask[0] && rfc_casecmp(b->mask, mask); old = b, b = b->next) ;
  722. if (b->mask[0]) {
  723. if (old)
  724. old->next = b->next;
  725. else
  726. chan->channel.ban = b->next;
  727. free(b->mask);
  728. free(b->who);
  729. free(b);
  730. } else {
  731. /* The ban was not even set, why continue? */
  732. return;
  733. }
  734. if (channel_pending(chan))
  735. return;
  736. if (u_sticky_mask(chan->bans, mask) || u_sticky_mask(global_bans, mask)) {
  737. /* That's a sticky ban! No point in being
  738. * sticky unless we enforce it!!
  739. */
  740. add_mode(chan, '+', 'b', mask);
  741. }
  742. if ((u_equals_mask(global_bans, mask) || u_equals_mask(chan->bans, mask)) &&
  743. me_op(chan) && !channel_dynamicbans(chan)) {
  744. /* That's a permban! */
  745. if (!glob_bot(user) && !chk_op(user, chan))
  746. add_mode(chan, '+', 'b', mask);
  747. }
  748. }
  749. static void
  750. got_exempt(struct chanset_t *chan, memberlist *m, char *mask, char *isserver)
  751. {
  752. char s[UHOSTLEN] = "";
  753. simple_snprintf(s, sizeof(s), "%s!%s", m ? m->nick : "", m ? m->userhost : isserver);
  754. if (newexempt(chan, mask, s))
  755. return; /* The exempt was already set, don't bother with it */
  756. if (channel_pending(chan))
  757. return;
  758. if (m && !m->is_me) { /* It's not my exemption */
  759. if (channel_nouserexempts(chan) && !glob_bot(user) && !glob_master(user) && !chan_master(user)) {
  760. /* No exempts made by users */
  761. add_mode(chan, '-', 'e', mask);
  762. return;
  763. }
  764. }
  765. if (reversing || (!m && bounce_exempts &&
  766. (!u_equals_mask(global_exempts, mask) || !u_equals_mask(chan->exempts, mask))))
  767. add_mode(chan, '-', 'e', mask);
  768. }
  769. static void
  770. got_unexempt(struct chanset_t *chan, memberlist *m, char *mask)
  771. {
  772. masklist *e = chan->channel.exempt, *old = NULL;
  773. masklist *b = NULL;
  774. int match = 0;
  775. while (e && e->mask[0] && rfc_casecmp(e->mask, mask)) {
  776. old = e;
  777. e = e->next;
  778. }
  779. if (e && e->mask[0]) {
  780. if (old)
  781. old->next = e->next;
  782. else
  783. chan->channel.exempt = e->next;
  784. free(e->mask);
  785. free(e->who);
  786. free(e);
  787. } else {
  788. /* The exempt was not even set, why continue? */
  789. return;
  790. }
  791. if (channel_pending(chan))
  792. return;
  793. if (u_sticky_mask(chan->exempts, mask) || u_sticky_mask(global_exempts, mask)) {
  794. /* That's a sticky exempt! No point in being sticky unless we enforce it!!
  795. */
  796. add_mode(chan, '+', 'e', mask);
  797. }
  798. /* If exempt was removed by master then leave it else check for bans */
  799. /* FIXME: this is impossible, if server !isbot ? */
  800. if (!m && glob_bot(user) && !glob_master(user) && !chan_master(user)) {
  801. b = chan->channel.ban;
  802. while (b->mask[0] && !match) {
  803. if (wild_match(b->mask, mask) || wild_match(mask, b->mask)) {
  804. add_mode(chan, '+', 'e', mask);
  805. match = 1;
  806. } else
  807. b = b->next;
  808. }
  809. }
  810. if ((u_equals_mask(global_exempts, mask) || u_equals_mask(chan->exempts, mask)) &&
  811. me_op(chan) && !channel_dynamicexempts(chan) && !glob_bot(user))
  812. add_mode(chan, '+', 'e', mask);
  813. if (channel_enforcebans(chan))
  814. enforce_bans(chan);
  815. }
  816. static void
  817. got_invite(struct chanset_t *chan, memberlist *m, char *mask, char *isserver)
  818. {
  819. char s[UHOSTLEN] = "";
  820. simple_snprintf(s, sizeof(s), "%s!%s", m ? m->nick : "", m ? m->userhost : isserver);
  821. if (newinvite(chan, mask, s))
  822. return; /* The Invite was already set, don't bother with it */
  823. if (channel_pending(chan))
  824. return;
  825. if (m && !m->is_me) { /* It's not my invitation */
  826. if (channel_nouserinvites(chan) && !glob_bot(user) && !glob_master(user) && !chan_master(user)) {
  827. /* No exempts made by users */
  828. add_mode(chan, '-', 'I', mask);
  829. return;
  830. }
  831. }
  832. if (reversing || (bounce_invites && !m &&
  833. (!u_equals_mask(global_invites, mask) || !u_equals_mask(chan->invites, mask))))
  834. add_mode(chan, '-', 'I', mask);
  835. }
  836. static void
  837. got_uninvite(struct chanset_t *chan, memberlist *m, char *mask)
  838. {
  839. masklist *inv = chan->channel.invite, *old = NULL;
  840. while (inv->mask[0] && rfc_casecmp(inv->mask, mask)) {
  841. old = inv;
  842. inv = inv->next;
  843. }
  844. if (inv->mask[0]) {
  845. if (old)
  846. old->next = inv->next;
  847. else
  848. chan->channel.invite = inv->next;
  849. free(inv->mask);
  850. free(inv->who);
  851. free(inv);
  852. } else {
  853. /* The Invite was not even set, why continue? */
  854. return;
  855. }
  856. if (channel_pending(chan))
  857. return;
  858. if (u_sticky_mask(chan->invites, mask) || u_sticky_mask(global_invites, mask)) {
  859. /* That's a sticky invite! No point in being sticky unless we enforce it!!
  860. */
  861. add_mode(chan, '+', 'I', mask);
  862. }
  863. if (!m && glob_bot(user) && !glob_master(user) && !chan_master(user) && (chan->channel.mode & CHANINV))
  864. add_mode(chan, '+', 'I', mask);
  865. if ((u_equals_mask(global_invites, mask) ||
  866. u_equals_mask(chan->invites, mask)) && me_op(chan) && !channel_dynamicinvites(chan) && !glob_bot(user))
  867. add_mode(chan, '+', 'I', mask);
  868. }
  869. static memberlist *assert_ismember(struct chanset_t *chan, const char *nick)
  870. {
  871. memberlist *m = ismember(chan, nick);
  872. if (m) {
  873. member_getuser(m);
  874. } else {
  875. if (channel_pending(chan))
  876. return NULL;
  877. putlog(LOG_MISC, chan->dname, "* Mode change on %s for nonexistant %s!", chan->dname, nick);
  878. send_chan_who(DP_MODE, chan);
  879. }
  880. return m;
  881. }
  882. static int
  883. gotmode(char *from, char *msg)
  884. {
  885. #define msign modes[i][0]
  886. #define mmode modes[i][1]
  887. /* 2 = space */
  888. #define mparam &modes[i][3]
  889. /* Usermode changes? */
  890. if (msg[0] && (strchr(CHANMETA, msg[0]) != NULL)) {
  891. char *ch = newsplit(&msg);
  892. struct chanset_t *chan = findchan(ch);
  893. if (unlikely(!chan)) {
  894. if (unlikely(match_my_nick(ch)))
  895. return 0;
  896. putlog(LOG_MISC, "*", "Oops. Someone made me join %s... leaving...", ch);
  897. dprintf(DP_SERVER, "PART %s\n", ch);
  898. return 0;
  899. }
  900. /* let's pre-emptively check for mass op/deop, manual ops and cookieops */
  901. if ((channel_active(chan) || channel_pending(chan))) {
  902. char isserver[100] = "";
  903. size_t z = strlen(msg);
  904. struct userrec *u = NULL;
  905. memberlist *m = NULL;
  906. char *nick = NULL;
  907. if (!strchr(from, '!'))
  908. strlcpy(isserver, from, sizeof(isserver));
  909. if (msg[--z] == ' ') /* I hate cosmetic bugs :P -poptix */
  910. msg[z] = 0;
  911. /* Split up from */
  912. if (!isserver[0]) {
  913. nick = splitnick(&from);
  914. if ((m = ismember(chan, nick))) {
  915. m->last = now;
  916. member_getuser(m);
  917. u = m->user;
  918. } else {
  919. if (channel_pending(chan))
  920. return 0;
  921. dprintf(DP_MODE, "KICK %s %s :Desync\n", chan->name[0] ? chan->name : chan->dname, nick);
  922. putlog(LOG_MISC, chan->dname, "* Mode change on %s from nonexistant %s!", chan->dname, nick);
  923. send_chan_who(DP_MODE, chan);
  924. return 0;
  925. }
  926. } else
  927. nick = splitnick(&from);
  928. int i = 0, modecnt = 0, ops = 0, deops = 0, bans = 0, unbans = 0;
  929. bool me_opped = 0;
  930. char **modes = (char **) my_calloc(1, sizeof(char *));
  931. char s[UHOSTLEN] = "";
  932. memberlist *mv = NULL;
  933. massop = reversing = 0;
  934. irc_log(chan, "%s!%s sets mode: %s", nick, from, msg);
  935. get_user_flagrec(u, &user, ch);
  936. if (1) { // Place in block to hint chg/sign to be destroyed when done
  937. /* Split up the mode: #chan modes param param param param */
  938. char *chg = newsplit(&msg);
  939. char sign = 0;
  940. while (*chg) { /* +MODES PARAM PARAM PARAM ... */
  941. if (strchr("+-", *chg))
  942. sign = *chg;
  943. else {
  944. char* mp = NULL;
  945. if (strchr("beIlkov", chg[0])) {
  946. mp = newsplit(&msg); /* PARAM as noted above */
  947. fixcolon(mp);
  948. }
  949. /* Just want o's and b's */
  950. modes = (char **) my_realloc(modes, (modecnt * sizeof(char *)) + sizeof(char *));
  951. // char **modes = (char **) my_calloc(modesperline + 1, sizeof(char *));
  952. size_t siz = (mp ? strlen(mp) : 0) + 3 + 1;
  953. modes[modecnt] = (char *) my_calloc(1, siz);
  954. simple_snprintf(modes[modecnt], siz, "%c%c %s", sign, chg[0], mp ? mp : "");
  955. ++modecnt;
  956. if (chg[0] == 'o') {
  957. if (sign == '+') {
  958. ++ops;
  959. if (match_my_nick(mp))
  960. me_opped = 1;
  961. } else {
  962. deops++;
  963. if (match_my_nick(mp))
  964. me_opped = 0;
  965. }
  966. } else if (chg[0] == 'b') {
  967. if (sign == '+')
  968. ++bans;
  969. else
  970. ++unbans;
  971. }
  972. }
  973. ++chg;
  974. }
  975. }
  976. /* take ASAP */
  977. if (me_opped && !me_op(chan) && channel_take(chan))
  978. do_take(chan);
  979. /* Now we got modes[], chan, u, nick, and count of each relevant mode */
  980. /* check for mdop */
  981. if (me_op(chan)) {
  982. char tmp[1024] = "";
  983. if (!isserver[0] && role && (!u || (u && !u->bot))) {
  984. if (m && deops >= 3) {
  985. if (chan->mdop) {
  986. if (role < 5 && !chan_sentkick(m)) {
  987. m->flags |= SENTKICK;
  988. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_MASSDEOP));
  989. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  990. } else {
  991. if (u) {
  992. simple_snprintf(tmp, sizeof(tmp), "Mass deop on %s by %s", chan->dname, m->nick);
  993. deflag_user(u, DEFLAG_EVENT_MDOP, tmp, chan);
  994. }
  995. }
  996. reversing = mdop_reversing = 1;
  997. }
  998. if (channel_protect(chan))
  999. do_protect(chan, "Mass Deop");
  1000. }
  1001. /* check for mop */
  1002. if (ops >= 3) {
  1003. if (chan->mop) {
  1004. if (m && !chan_sentkick(m)) {
  1005. if (role < 5) {
  1006. m->flags |= SENTKICK;
  1007. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_MANUALOP));
  1008. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  1009. } else {
  1010. if (u) {
  1011. simple_snprintf(tmp, sizeof(tmp), "Mass op on %s by %s", chan->dname, m->nick);
  1012. deflag_user(u, DEFLAG_EVENT_MOP, tmp, chan);
  1013. }
  1014. }
  1015. }
  1016. // Don't mass deop if protect is set, it'll happen anyway below
  1017. if (channel_bitch(chan) && !channel_protect(chan))
  1018. enforce_bitch(chan); /* deop quick! */
  1019. else
  1020. reversing = massop = 1; // Reverse via got_op
  1021. }
  1022. if (channel_protect(chan))
  1023. do_protect(chan, "Mass OP");
  1024. }
  1025. }
  1026. if (ops) {
  1027. int n = 0;
  1028. /* Check cookies */
  1029. if (u && m && u->bot && !channel_fastop(chan) && !channel_take(chan) && !cookies_disabled) {
  1030. int isbadop = 0;
  1031. bool failure = 0;
  1032. /* If no unbans or the -b is not the LAST mode, it's bad. */
  1033. if (unbans != 1 || (strncmp(modes[modecnt - 1], "-b", 2))) {
  1034. isbadop = BC_NOCOOKIE;
  1035. failure = 1;
  1036. } else {
  1037. /* Check the hash for each opped nick and punish the opped client if it fails
  1038. * Punish the opper lastly (and once)
  1039. */
  1040. for (i = 0; i < (modecnt - 1); i++) { /* Don't need to hit the -b */
  1041. if (msign == '+' && mmode == 'o') {
  1042. mv = assert_ismember(chan, mparam);
  1043. // Unknown client - I am desycned, don't punish or it may lead to fight
  1044. if (!mv) continue;
  1045. const char *cookie = &(modes[modecnt - 1][3]);
  1046. if ((isbadop = checkcookie(chan->dname, m, mv, cookie, i))) {
  1047. //if (!failure) { /* First failure */
  1048. failure = 1;
  1049. //}
  1050. /* Kick the opped client */
  1051. if (randint(7) == (unsigned int) i) {
  1052. if (!mv || !chan_sentkick(mv)) {
  1053. if (mv)
  1054. mv->flags |= SENTKICK;
  1055. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, mparam, kickprefix, response(RES_BADOPPED));
  1056. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  1057. }
  1058. }
  1059. }
  1060. }
  1061. }
  1062. }
  1063. if (unlikely(failure)) { /* One of the hashes failed! */
  1064. /* Did *I* do this heinous act? */
  1065. if (m->is_me) {
  1066. detected(DETECT_HIJACK, "Possible Hijack: bad cookie");
  1067. }
  1068. if (randint(7) == 0) {
  1069. /* Kick opper */
  1070. if (!chan_sentkick(m)) {
  1071. m->flags |= SENTKICK;
  1072. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_BADOP));
  1073. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  1074. }
  1075. simple_snprintf(tmp, sizeof(tmp), "%s MODE %s %s", m->from, chan->dname, modes[modecnt - 1]);
  1076. deflag_user(u, DEFLAG_EVENT_BADCOOKIE, tmp, chan);
  1077. }
  1078. /* Do the logging last as it can slow down the KICK pushing */
  1079. putlog(LOG_WARNING, "*", "%s opped in %s with bad cookie(%d): %s", m->nick, chan->dname, isbadop, msg);
  1080. if (isbadop == BC_NOCOOKIE)
  1081. putlog(LOG_WARN, "*", "Missing cookie: %s!%s MODE %s %s",
  1082. m->nick, m->userhost, chan->dname, modes[modecnt - 1]);
  1083. else if (isbadop == BC_HASH)
  1084. putlog(LOG_WARN, "*", "Invalid cookie (bad hash): %s!%s MODE %s %s",
  1085. m->nick, m->userhost, chan->dname, modes[modecnt - 1]);
  1086. else if (isbadop == BC_SLACK)
  1087. putlog(LOG_WARN, "*", "Invalid cookie (bad time): %s!%s MODE %s %s",
  1088. m->nick, m->userhost, chan->dname, modes[modecnt - 1]);
  1089. else if (isbadop == BC_COUNTER)
  1090. putlog(LOG_WARN, "*", "Invalid cookie (bad count): %s!%s MODE %s %s",
  1091. m->nick, m->userhost, chan->dname, modes[modecnt - 1]);
  1092. }
  1093. #ifdef DEBUG
  1094. else
  1095. putlog(LOG_DEBUG, "@", "Good op: %s", modes[modecnt - 1]);
  1096. #endif
  1097. }
  1098. /* manop */
  1099. if (chan->manop && u && !u->bot) {
  1100. n = i = 0;
  1101. switch (role) {
  1102. case 0:
  1103. break;
  1104. case 1:
  1105. if (m) {
  1106. /* Kick opper */
  1107. if (!chan_sentkick(m)) {
  1108. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, m->nick, kickprefix, response(RES_MANUALOP));
  1109. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  1110. m->flags |= SENTKICK;
  1111. }
  1112. simple_snprintf(tmp, sizeof(tmp), "%s MODE %s %s", m->from, chan->dname, modes[modecnt - 1]);
  1113. deflag_user(u, DEFLAG_EVENT_MANUALOP, tmp, chan);
  1114. }
  1115. break;
  1116. default:
  1117. /* KICK the opped */
  1118. n = role - 1;
  1119. i = 0;
  1120. while ((i < modecnt) && (n > 0)) {
  1121. if (modes[i] && !strncmp(modes[i], "+o", 2))
  1122. n--;
  1123. if (n)
  1124. i++;
  1125. }
  1126. if (!n) {
  1127. for (i = 0; i < modecnt; i++) {
  1128. if (msign == '+' && mmode == 'o' && !match_my_nick(mparam)) {
  1129. mv = ismember(chan, mparam);
  1130. if (!mv || !chan_sentkick(mv)) {
  1131. if (mv)
  1132. mv->flags |= SENTKICK;
  1133. const size_t len = simple_snprintf(tmp, sizeof(tmp), "KICK %s %s :%s%s\r\n", chan->name, mparam, kickprefix, response(RES_MANUALOPPED));
  1134. dprintf_real(DP_MODE_NEXT, tmp, len, sizeof(tmp));
  1135. }
  1136. }
  1137. }
  1138. }
  1139. }
  1140. }
  1141. }
  1142. }
  1143. /* Now do the modes again, this time thoroughly... */
  1144. if (m && channel_active(chan) && me_op(chan)) {
  1145. if (chan_fakeop(m)) {
  1146. putlog(LOG_MODES, ch, "Mode change by fake op on %s! Reversing...", ch);
  1147. dprintf(DP_MODE, "KICK %s %s :%sAbusing ill-gained server ops\n", ch, m->nick, kickprefix);
  1148. m->flags |= SENTKICK;
  1149. reversing = 1;
  1150. } else if (!chan_hasop(m) && !channel_nodesynch(chan)) {
  1151. if (u && u->bot && chk_op(user, chan)) {
  1152. putlog(LOG_MODES, ch, "Mode change by friendly non-chanop on %s! Opping...", ch);
  1153. do_op(m->nick, chan, 0, 0);
  1154. } else {
  1155. putlog(LOG_MODES, ch, "Mode change by non-chanop on %s! Reversing...", ch);
  1156. dprintf(DP_MODE, "KICK %s %s :%sAbusing desync\n", ch, m->nick, kickprefix);
  1157. m->flags |= SENTKICK;
  1158. reversing = 1;
  1159. }
  1160. }
  1161. }
  1162. for (i = 0; i < modecnt; i++) {
  1163. int todo = 0;
  1164. if (isserver[0] && bounce_modes)
  1165. reversing = 1;
  1166. switch (mmode) { /* parse mode */
  1167. case 'i':
  1168. todo = CHANINV;
  1169. if (isserver[0] && (bounce_modes))
  1170. reversing = 1;
  1171. break;
  1172. case 'p':
  1173. todo = CHANPRIV;
  1174. if (isserver[0] && (bounce_modes))
  1175. reversing = 1;
  1176. break;
  1177. case 's':
  1178. todo = CHANSEC;
  1179. if (isserver[0] && (bounce_modes))
  1180. reversing = 1;
  1181. break;
  1182. case 'm':
  1183. todo = CHANMODER;
  1184. if (isserver[0] && (bounce_modes))
  1185. reversing = 1;
  1186. break;
  1187. case 'c':
  1188. todo = CHANNOCLR;
  1189. if (isserver[0] && (bounce_modes))
  1190. reversing = 1;
  1191. break;
  1192. case 'C':
  1193. todo = CHANNOCTCP;
  1194. if (isserver[0] && (bounce_modes))
  1195. reversing = 1;
  1196. break;
  1197. case 'R':
  1198. todo = CHANREGON;
  1199. if (isserver[0] && (bounce_modes))
  1200. reversing = 1;
  1201. break;
  1202. case 'M':
  1203. todo = CHANMODR;
  1204. if (isserver[0] && (bounce_modes))
  1205. reversing = 1;
  1206. break;
  1207. case 'r':
  1208. todo = CHANLONLY;
  1209. if (isserver[0] && (bounce_modes))
  1210. reversing = 1;
  1211. break;
  1212. case 't':
  1213. todo = CHANTOPIC;
  1214. if (isserver[0] && (bounce_modes))
  1215. reversing = 1;
  1216. break;
  1217. case 'n':
  1218. todo = CHANNOMSG;
  1219. if (isserver[0] && (bounce_modes))
  1220. reversing = 1;
  1221. break;
  1222. case 'a':
  1223. todo = CHANANON;
  1224. if (isserver[0] && (bounce_modes))
  1225. reversing = 1;
  1226. break;
  1227. case 'q':
  1228. todo = CHANQUIET;
  1229. if (isserver[0] && (bounce_modes))
  1230. reversing = 1;
  1231. break;
  1232. case 'l':
  1233. if (isserver[0] && (bounce_modes))
  1234. reversing = 1;
  1235. if (msign == '-') {
  1236. if (channel_active(chan)) {
  1237. if ((reversing) && (chan->channel.maxmembers != 0)) {
  1238. simple_snprintf(s, sizeof(s), "%d", chan->channel.maxmembers);
  1239. add_mode(chan, '+', 'l', s);
  1240. } else if ((chan->limit_prot != 0) && !glob_master(user) && !chan_master(user)) {
  1241. simple_snprintf(s, sizeof(s), "%d", chan->limit_prot);
  1242. add_mode(chan, '+', 'l', s);
  1243. } else {
  1244. if (chan->limitraise && dolimit(chan) && (!chan_master(user) && !glob_master(user) && !glob_bot(user))) {
  1245. chan->channel.maxmembers = 0; /* set this to 0 so a new limit is generated */
  1246. raise_limit(chan);
  1247. }
  1248. }
  1249. }
  1250. chan->channel.maxmembers = 0;
  1251. } else { /* + */
  1252. if (mparam == '\0')
  1253. break;
  1254. chan->channel.maxmembers = atoi(mparam);
  1255. if (channel_pending(chan))
  1256. break;
  1257. if (((reversing) &&
  1258. !(chan->mode_pls_prot & CHANLIMIT)) ||
  1259. ((chan->mode_mns_prot & CHANLIMIT) &&
  1260. !glob_bot(user) && !glob_master(user) && !chan_master(user)))
  1261. add_mode(chan, '-', 'l', "");
  1262. if ((chan->limit_prot != chan->channel.maxmembers) && (chan->mode_pls_prot & CHANLIMIT) && (chan->limit_prot != 0) &&
  1263. !glob_bot(user) && !glob_master(user) && !chan_master(user)) {
  1264. simple_snprintf(s, sizeof(s), "%d", chan->limit_prot);
  1265. add_mode(chan, '+', 'l', s);
  1266. }
  1267. if (chan->limitraise && dolimit(chan) && !glob_bot(user) && (!chan_master(user) && !glob_master(user)))
  1268. raise_limit(chan);
  1269. }
  1270. break;
  1271. case 'k':
  1272. if (msign == '+')
  1273. chan->channel.mode |= CHANKEY;
  1274. else
  1275. chan->channel.mode &= ~CHANKEY;
  1276. if (mparam == '\0')
  1277. break;
  1278. if (msign == '+') {
  1279. my_setkey(chan, mparam);
  1280. if (channel_active(chan))
  1281. got_key(chan, mparam);
  1282. } else {
  1283. if (channel_active(chan)) {
  1284. if ((reversing) && (chan->channel.key[0]))
  1285. add_mode(chan, '+', 'k', chan->channel.key);
  1286. else if ((chan->key_prot[0]) && !glob_master(user) && !chan_master(user))
  1287. add_mode(chan, '+', 'k', chan->key_prot);
  1288. }
  1289. my_setkey(chan, NULL);
  1290. }
  1291. break;
  1292. case 'o':
  1293. if (!u || (u && !u->bot))
  1294. chan->channel.fighting++;
  1295. mv = assert_ismember(chan, mparam);
  1296. if (mv) {
  1297. if (msign == '+')
  1298. got_op(chan, m, mv);
  1299. else
  1300. got_deop(chan, m, mv, isserver);
  1301. }
  1302. break;
  1303. case 'v':
  1304. mv = assert_ismember(chan, mparam);
  1305. if (mv) {
  1306. bool dv = 0;
  1307. get_user_flagrec(mv->user, &victim, chan->dname, chan);
  1308. if (msign == '+') {
  1309. if (mv->flags & EVOICE) {
  1310. /* FIXME: This is a lame check, we need to expand on this more */
  1311. if (!chan_master(user) && !glob_master(user) && !chk_voice(victim, chan)) {
  1312. dv = 1;
  1313. } else {
  1314. mv->flags &= ~EVOICE;
  1315. }
  1316. }
  1317. mv->flags &= ~SENTVOICE;
  1318. mv->flags |= CHANVOICE;
  1319. if (channel_active(chan) && dovoice(chan)) {
  1320. if (dv || chk_devoice(victim) || (channel_voicebitch(chan) && !chk_voice(victim, chan))) {
  1321. add_mode(chan, '-', 'v', mparam);
  1322. } else if (reversing) {
  1323. add_mode(chan, '-', 'v', mparam);
  1324. }
  1325. }
  1326. } else if (msign == '-') {
  1327. mv->flags &= ~SENTDEVOICE;
  1328. mv->flags &= ~CHANVOICE;
  1329. if (channel_active(chan) && dovoice(chan) && !chan_hasop(mv)) {
  1330. /* revoice +v users */
  1331. if (chk_voice(victim, chan)) {
  1332. add_mode(chan, '+', 'v', mparam);
  1333. } else if (reversing) {
  1334. add_mode(chan, '+', 'v', mparam);
  1335. /* if they arent +v|v and VOICER is m+ then EVOICE them */
  1336. } else {
  1337. /* FIXME: same thing here */
  1338. if (!match_my_nick(nick) && channel_voice(chan) && !glob_bot(user) &&
  1339. (glob_master(user) || chan_master(user)) &&
  1340. rfc_casecmp(nick, mparam)) {
  1341. /* if the user is not +q set them norEVOICE. */
  1342. if (!chan_quiet(victim) && !(mv->flags & EVOICE)) {
  1343. putlog(LOG_DEBUG, "@", "Giving EVOICE flag to: %s (%s)", mv->nick, chan->dname);
  1344. mv->flags |= EVOICE;
  1345. }
  1346. }
  1347. }
  1348. }
  1349. }
  1350. }
  1351. break;
  1352. case 'b':
  1353. if (!u || (u && !u->bot))
  1354. chan->channel.fighting++;
  1355. if (msign == '+')
  1356. got_ban(chan, m, mparam, isserver);
  1357. else
  1358. got_unban(chan, m, mparam);
  1359. break;
  1360. case 'e':
  1361. if (!u || (u && !u->bot))
  1362. chan->channel.fighting++;
  1363. if (msign == '+')
  1364. got_exempt(chan, m, mparam, isserver);
  1365. else
  1366. got_unexempt(chan, m, mparam);
  1367. break;
  1368. case 'I':
  1369. if (!u || (u && !u->bot))
  1370. chan->channel.fighting++;
  1371. if (msign == '+')
  1372. got_invite(chan, m, mparam, isserver);
  1373. else
  1374. got_uninvite(chan, m, mparam);
  1375. break;
  1376. }
  1377. if (todo) {
  1378. if (msign == '+')
  1379. chan->channel.mode |= todo;
  1380. else
  1381. chan->channel.mode &= ~todo;
  1382. if (channel_active(chan)) {
  1383. if ((((msign == '+') && (chan->mode_mns_prot & todo)) ||
  1384. ((msign == '-') && (chan->mode_pls_prot & todo))) &&
  1385. !glob_master(user) && !chan_master(user))
  1386. add_mode(chan, msign == '+' ? '-' : '+', mmode, "");
  1387. else if (reversing &&
  1388. ((msign == '+') || (chan->mode_pls_prot & todo)) &&
  1389. ((msign == '-') || (chan->mode_mns_prot & todo)))
  1390. add_mode(chan, msign == '+' ? '-' : '+', mmode, "");
  1391. }
  1392. }
  1393. }
  1394. // If reversing, dump the modes out
  1395. if (reversing)
  1396. flush_mode(chan, QUICK);
  1397. for (i = 0; i < modecnt; i++)
  1398. if (modes[i])
  1399. free(modes[i]);
  1400. free(modes);
  1401. if (chan->channel.do_opreq)
  1402. request_op(chan);
  1403. if (!me_op(chan) && isserver[0]) /* FIXME, WTF IS THIS? */
  1404. chan->ircnet_status |= CHAN_ASKEDMODES;
  1405. }
  1406. }
  1407. return 0;
  1408. }