mode.c 47 KB

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