mode.c 51 KB

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