mode.c 50 KB

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