mode.c 45 KB

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