mode.cc 50 KB

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