mode.c 49 KB

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