mode.c 48 KB

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