mode.c 50 KB

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