1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
23 #include <sys/errno.h>
27 #include <ext/hash_map>
35 #include "connection.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
50 extern std::vector<Module*> modules;
51 extern std::vector<ircd_module*> factory;
52 extern InspIRCd* ServerInstance;
53 extern ServerConfig* Config;
57 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
61 if ((!user) || (!dest) || (!chan))
63 log(DEFAULT,"*** BUG *** GiveOps was given an invalid parameter");
69 log(DEFAULT,"the target nickname given to GiveOps was invalid");
70 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
76 log(DEFAULT,"the target nickname given to GiveOps couldnt be found");
77 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
84 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_OP));
86 if (MOD_RESULT == ACR_DENY)
88 if (MOD_RESULT == ACR_DEFAULT)
90 if ((status < STATUS_OP) && (!is_uline(user->server)))
92 log(DEBUG,"%s cant give ops to %s because they nave status %d and needs %d",user->nick,dest,status,STATUS_OP);
93 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
99 for (unsigned int i = 0; i < d->chans.size(); i++)
101 if ((d->chans[i].channel != NULL) && (chan != NULL))
102 if (!strcasecmp(d->chans[i].channel->name,chan->name))
104 if (d->chans[i].uc_modes & UCMODE_OP)
106 /* mode already set on user, dont allow multiple */
107 log(DEFAULT,"The target user given to GiveOps was already opped on the channel");
110 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
111 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
115 log(DEFAULT,"The target channel given to GiveOps was not in the users mode list");
120 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
124 if ((!user) || (!dest) || (!chan))
126 log(DEFAULT,"*** BUG *** GiveHops was given an invalid parameter");
133 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
138 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
144 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_HALFOP));
146 if (MOD_RESULT == ACR_DENY)
148 if (MOD_RESULT == ACR_DEFAULT)
150 if ((status < STATUS_OP) && (!is_uline(user->server)))
152 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
157 for (unsigned int i = 0; i < d->chans.size(); i++)
159 if ((d->chans[i].channel != NULL) && (chan != NULL))
160 if (!strcasecmp(d->chans[i].channel->name,chan->name))
162 if (d->chans[i].uc_modes & UCMODE_HOP)
164 /* mode already set on user, dont allow multiple */
167 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
168 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
176 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
180 if ((!user) || (!dest) || (!chan))
182 log(DEFAULT,"*** BUG *** GiveVoice was given an invalid parameter");
189 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
194 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
200 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_VOICE));
202 if (MOD_RESULT == ACR_DENY)
204 if (MOD_RESULT == ACR_DEFAULT)
206 if ((status < STATUS_HOP) && (!is_uline(user->server)))
208 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
213 for (unsigned int i = 0; i < d->chans.size(); i++)
215 if ((d->chans[i].channel != NULL) && (chan != NULL))
216 if (!strcasecmp(d->chans[i].channel->name,chan->name))
218 if (d->chans[i].uc_modes & UCMODE_VOICE)
220 /* mode already set on user, dont allow multiple */
223 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
224 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
232 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
236 if ((!user) || (!dest) || (!chan))
238 log(DEFAULT,"*** BUG *** TakeOps was given an invalid parameter");
245 log(DEBUG,"TakeOps was given an invalid target nickname of %s",dest);
246 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
251 log(DEBUG,"TakeOps couldnt resolve the target nickname: %s",dest);
252 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
258 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEOP));
260 if (MOD_RESULT == ACR_DENY)
262 if (MOD_RESULT == ACR_DEFAULT)
264 if ((status < STATUS_OP) && (!is_uline(user->server)))
266 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
271 for (unsigned int i = 0; i < d->chans.size(); i++)
273 if ((d->chans[i].channel != NULL) && (chan != NULL))
274 if (!strcasecmp(d->chans[i].channel->name,chan->name))
276 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
278 /* mode already set on user, dont allow multiple */
281 d->chans[i].uc_modes ^= UCMODE_OP;
282 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
286 log(DEBUG,"TakeOps couldnt locate the target channel in the target users list");
291 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
295 if ((!user) || (!dest) || (!chan))
297 log(DEFAULT,"*** BUG *** TakeHops was given an invalid parameter");
304 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
309 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
315 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEHALFOP));
317 if (MOD_RESULT == ACR_DENY)
319 if (MOD_RESULT == ACR_DEFAULT)
321 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
322 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))))
324 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
329 for (unsigned int i = 0; i < d->chans.size(); i++)
331 if ((d->chans[i].channel != NULL) && (chan != NULL))
332 if (!strcasecmp(d->chans[i].channel->name,chan->name))
334 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
336 /* mode already set on user, dont allow multiple */
339 d->chans[i].uc_modes ^= UCMODE_HOP;
340 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
348 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
352 if ((!user) || (!dest) || (!chan))
354 log(DEFAULT,"*** BUG *** TakeVoice was given an invalid parameter");
361 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
366 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
372 FOREACH_RESULT(OnAccessCheck(user,d,chan,AC_DEVOICE));
374 if (MOD_RESULT == ACR_DENY)
376 if (MOD_RESULT == ACR_DEFAULT)
378 if ((status < STATUS_HOP) && (!is_uline(user->server)))
380 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
385 for (unsigned int i = 0; i < d->chans.size(); i++)
387 if ((d->chans[i].channel != NULL) && (chan != NULL))
388 if (!strcasecmp(d->chans[i].channel->name,chan->name))
390 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
392 /* mode already set on user, dont allow multiple */
395 d->chans[i].uc_modes ^= UCMODE_VOICE;
396 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
404 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
406 if ((!user) || (!dest) || (!chan)) {
407 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
412 if ((!user) || (!dest) || (!chan))
414 unsigned int l = strlen(dest);
415 if (strchr(dest,'!')==0)
417 if (strchr(dest,'@')==0)
419 for (unsigned int i = 0; i < l; i++)
422 for (unsigned int i = 0; i < l; i++)
426 for (unsigned int i = 0; i < l; i++)
432 for (unsigned int i = 0; i < l; i++)
438 long maxbans = GetMaxBans(chan->name);
439 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
441 WriteServ(user->fd,"478 %s %s :Channel ban list for %s is full (maximum entries for this channel is %d)",user->nick, chan->name,chan->name,maxbans);
445 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
448 FOREACH_RESULT(OnAddBan(user,chan,dest));
453 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
455 if (!strcasecmp(i->data,dest))
457 // dont allow a user to set the same ban twice
463 strncpy(b.data,dest,MAXBUF);
464 strncpy(b.set_by,user->nick,NICKMAX);
465 chan->bans.push_back(b);
469 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
471 if ((!user) || (!dest) || (!chan)) {
472 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
476 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
477 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
479 if (!strcasecmp(i->data,dest))
482 FOREACH_RESULT(OnDelBan(user,chan,dest));
492 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
493 // a section further down the chain tidies up the +-+- crap.
494 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
498 memset(counts,0,sizeof(counts));
499 memset(active,0,sizeof(active));
500 for (unsigned int i = 0; i < modes.length(); i++)
502 if ((modes[i] == '+') || (modes[i] == '-'))
506 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
508 log(DEBUG,"Tidy mode %c",modes[i]);
509 counts[(unsigned int)modes[i]]++;
510 active[(unsigned int)modes[i]] = true;
515 log(DEBUG,"Tidy mode %c",modes[i]);
516 counts[(unsigned int)modes[i]]++;
517 active[(unsigned int)modes[i]] = true;
520 for (int j = 65; j < 127; j++)
522 if ((counts[j] > 1) && (active[j] == true))
525 v[0] = (unsigned char)j;
527 std::string mode_str = v;
528 std::string::size_type pos = modes.find(mode_str);
529 if (pos != std::string::npos)
531 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
532 while (modes.find(mode_str) != std::string::npos)
533 modes.erase(modes.find(mode_str),1);
534 log(DEBUG,"New mode line: %s",modes.c_str());
541 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
544 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
548 char modelist[MAXBUF];
549 char outlist[MAXBUF];
551 char outpars[32][MAXBUF];
557 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
565 FOREACH_RESULT(OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
567 if (MOD_RESULT == ACR_DENY)
570 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
572 strlcpy(modelist,parameters[1],MAXBUF); /* mode list, e.g. +oo-o *
573 * parameters[2] onwards are parameters for
574 * modes that require them :) */
575 strlcpy(outlist,"+",MAXBUF);
578 log(DEBUG,"process_modes: modelist: %s",modelist);
580 std::string tidied = this->CompressModes(modelist,true);
581 strlcpy(modelist,tidied.c_str(),MAXBUF);
583 int len = strlen(modelist);
584 while (modelist[len-1] == ' ')
585 modelist[--len] = '\0';
586 for (ptr = 0; ptr < len; ptr++)
591 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
593 char modechar = modelist[ptr];
594 switch (modelist[ptr])
599 int t = strlen(outlist)-1;
600 if ((outlist[t] == '+') || (outlist[t] == '-'))
616 int t = strlen(outlist)-1;
617 if ((outlist[t] == '+') || (outlist[t] == '-'))
631 if ((param >= pcnt)) break;
632 log(DEBUG,"Enough parameters left");
636 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], true, 1));
639 log(DEBUG,"calling GiveOps");
640 r = GiveOps(user,parameters[param++],chan,status);
647 FOREACH_RESULT(OnRawMode(user, chan, 'o', parameters[param], false, 1));
650 log(DEBUG,"calling TakeOps");
651 r = TakeOps(user,parameters[param++],chan,status);
657 strlcat(outlist,"o",MAXBUF);
658 strlcpy(outpars[pc++],r,MAXBUF);
663 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
667 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], true, 1));
670 r = GiveHops(user,parameters[param++],chan,status);
677 FOREACH_RESULT(OnRawMode(user, chan, 'h', parameters[param], false, 1));
680 r = TakeHops(user,parameters[param++],chan,status);
686 strlcat(outlist,"h",MAXBUF);
687 strlcpy(outpars[pc++],r,MAXBUF);
693 if ((param >= pcnt)) break;
697 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], true, 1));
700 r = GiveVoice(user,parameters[param++],chan,status);
707 FOREACH_RESULT(OnRawMode(user, chan, 'v', parameters[param], false, 1));
710 r = TakeVoice(user,parameters[param++],chan,status);
716 strlcat(outlist,"v",MAXBUF);
717 strlcpy(outpars[pc++],r,MAXBUF);
722 if ((param >= pcnt)) break;
726 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], true, 1));
729 r = AddBan(user,parameters[param++],chan,status);
736 FOREACH_RESULT(OnRawMode(user, chan, 'b', parameters[param], false, 1));
739 r = TakeBan(user,parameters[param++],chan,status);
745 strlcat(outlist,"b",MAXBUF);
746 strlcpy(outpars[pc++],parameters[param-1],MAXBUF);
760 if (previously_unset_k)
762 previously_set_k = true;
764 if (!strcmp(chan->key,""))
767 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], true, 1));
772 strlcpy(key,parameters[param++],32);
773 strlcpy(outpars[pc++],key,MAXBUF);
774 strlcpy(chan->key,key,MAXBUF);
782 /* checks on -k are case sensitive and only accurate to the
783 first 32 characters */
784 if (previously_set_k)
786 previously_unset_k = true;
790 FOREACH_RESULT(OnRawMode(user, chan, 'k', parameters[param], false, 1));
793 strlcpy(key,parameters[param++],32);
794 /* only allow -k if correct key given */
795 if (!strcmp(chan->key,key))
797 strlcat(outlist,"k",MAXBUF);
798 strlcpy(chan->key,"",MAXBUF);
799 strlcpy(outpars[pc++],key,MAXBUF);
809 if (previously_set_l)
811 previously_unset_l = true;
813 FOREACH_RESULT(OnRawMode(user, chan, 'l', "", false, 0));
824 if ((param >= pcnt)) break;
829 if (previously_unset_l)
831 previously_set_l = true;
832 bool invalid = false;
833 for (unsigned int i = 0; i < strlen(parameters[param]); i++)
835 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
840 if (atoi(parameters[param]) < 1)
849 FOREACH_RESULT(OnRawMode(user, chan, 'l', parameters[param], true, 1));
853 chan->limit = atoi(parameters[param]);
855 // reported by mech: large values cause underflow
857 chan->limit = 0x7FFF;
862 strlcat(outlist,"l",MAXBUF);
863 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
871 FOREACH_RESULT(OnRawMode(user, chan, 'i', "", mdir, 0));
876 if (!(chan->binarymodes & CM_INVITEONLY)) strlcat(outlist,"i",MAXBUF);
877 chan->binarymodes |= CM_INVITEONLY;
881 if (chan->binarymodes & CM_INVITEONLY) strlcat(outlist,"i",MAXBUF);
882 chan->binarymodes &= ~CM_INVITEONLY;
889 FOREACH_RESULT(OnRawMode(user, chan, 't', "", mdir, 0));
894 if (!(chan->binarymodes & CM_TOPICLOCK)) strlcat(outlist,"t",MAXBUF);
895 chan->binarymodes |= CM_TOPICLOCK;
899 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"t",MAXBUF);
900 chan->binarymodes &= ~CM_TOPICLOCK;
907 FOREACH_RESULT(OnRawMode(user, chan, 'n', "", mdir, 0));
912 if (!(chan->binarymodes & CM_NOEXTERNAL)) strlcat(outlist,"n",MAXBUF);
913 chan->binarymodes |= CM_NOEXTERNAL;
917 if (chan->binarymodes & CM_NOEXTERNAL) strlcat(outlist,"n",MAXBUF);
918 chan->binarymodes &= ~CM_NOEXTERNAL;
925 FOREACH_RESULT(OnRawMode(user, chan, 'm', "", mdir, 0));
930 if (!(chan->binarymodes & CM_MODERATED)) strlcat(outlist,"m",MAXBUF);
931 chan->binarymodes |= CM_MODERATED;
935 if (chan->binarymodes & CM_MODERATED) strlcat(outlist,"m",MAXBUF);
936 chan->binarymodes &= ~CM_MODERATED;
943 FOREACH_RESULT(OnRawMode(user, chan, 's', "", mdir, 0));
948 if (!(chan->binarymodes & CM_SECRET)) strlcat(outlist,"s",MAXBUF);
949 chan->binarymodes |= CM_SECRET;
950 if (chan->binarymodes & CM_PRIVATE)
952 chan->binarymodes &= ~CM_PRIVATE;
955 strlcat(outlist,"-p+",MAXBUF);
961 if (chan->binarymodes & CM_SECRET) strlcat(outlist,"s",MAXBUF);
962 chan->binarymodes &= ~CM_SECRET;
969 FOREACH_RESULT(OnRawMode(user, chan, 'p', "", mdir, 0));
974 if (!(chan->binarymodes & CM_PRIVATE)) strlcat(outlist,"p",MAXBUF);
975 chan->binarymodes |= CM_PRIVATE;
976 if (chan->binarymodes & CM_SECRET)
978 chan->binarymodes &= ~CM_SECRET;
981 strlcat(outlist,"-s+",MAXBUF);
987 if (chan->binarymodes & CM_PRIVATE) strlcat(outlist,"p",MAXBUF);
988 chan->binarymodes &= ~CM_PRIVATE;
994 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",modechar,chan->custom_modes);
997 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
999 if (!ModeIsListMode(modechar,MT_CHANNEL))
1001 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
1005 if (ModeDefined(modechar,MT_CHANNEL))
1007 log(DEBUG,"A module has claimed this mode");
1010 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1012 p.push_back(parameters[param]);
1014 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
1016 p.push_back(parameters[param]);
1019 bool handled = false;
1022 // we're supposed to have a parameter, but none was given... so dont handle the mode.
1023 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
1025 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
1031 // BIG ASS IDIOTIC CODER WARNING!
1032 // Using OnRawMode on another modules mode's behavour
1033 // will confuse the crap out of admins! just because you CAN
1034 // do it, doesnt mean you SHOULD!
1036 std::string para = "";
1039 FOREACH_RESULT(OnRawMode(user, chan, modechar, para, mdir, pcnt));
1042 for (int i = 0; i <= MODCOUNT; i++)
1046 int t = modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p);
1049 log(DEBUG,"OnExtendedMode returned nonzero for a module");
1050 char app[] = {modechar, 0};
1051 if (ModeIsListMode(modechar,MT_CHANNEL))
1062 strlcat(outlist, app,MAXBUF);
1064 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1071 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
1073 strlcat(outlist, app,MAXBUF);
1075 else if (!strchr(outlist,modechar))
1077 strlcat(outlist, app,MAXBUF);
1080 chan->SetCustomMode(modechar,mdir);
1081 // include parameters in output if mode has them
1082 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
1084 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1085 strlcpy(outpars[pc++],parameters[param++],MAXBUF);
1088 // break, because only one module can handle the mode.
1097 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,modechar);
1105 /* this ensures only the *valid* modes are sent out onto the network */
1106 int xt = strlen(outlist)-1;
1107 while ((outlist[xt] == '-') || (outlist[xt] == '+'))
1110 xt = strlen(outlist)-1;
1114 strlcpy(outstr,outlist,MAXBUF);
1115 for (ptr = 0; ptr < pc; ptr++)
1117 strlcat(outstr," ",MAXBUF);
1118 strlcat(outstr,outpars[ptr],MAXBUF);
1122 log(DEBUG,"Local mode change");
1123 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outstr);
1124 FOREACH_MOD OnMode(user, chan, TYPE_CHANNEL, outstr);
1132 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outstr);
1140 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1141 FOREACH_MOD OnMode(user, chan, TYPE_CHANNEL, outstr);
1148 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1150 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1152 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1153 // Servers can +o and -o arbitrarily
1154 if ((serveroverride == true) && (umode == 'o'))
1158 // RFC1459 specified modes
1159 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1161 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1165 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1166 if ((strchr(sourcemodes,'o')) && (!adding))
1168 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1171 else if (umode == 'o')
1173 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1177 // process any module-defined modes that need oper
1178 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1180 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1184 if (ModeDefined(umode,MT_CLIENT))
1186 // process any module-defined modes that don't need oper
1187 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1188 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1190 // no, this mode needs oper, and this user 'aint got what it takes!
1196 // anything else - return false.
1197 log(DEBUG,"umode %c not known by any ruleset",umode);
1201 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1208 strlcpy(s2->nick,Config->ServerName,NICKMAX);
1209 strlcpy(s2->modes,"o",52);
1216 if (ModeDefined(umode,MT_CLIENT))
1218 for (int i = 0; i <= MODCOUNT; i++)
1220 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1222 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1226 log(DEBUG,"No module claims umode %c",umode);
1245 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1251 char outpars[MAXBUF];
1253 dest = Find(parameters[0]);
1260 if ((dest) && (pcnt == 1))
1262 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1266 if ((dest) && (pcnt > 1))
1268 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1269 parameters[1] = (char*)tidied.c_str();
1271 char dmodes[MAXBUF];
1272 strlcpy(dmodes,dest->modes,52);
1273 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1278 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1289 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1297 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1300 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1302 if (parameters[1][i] == ' ')
1304 if (parameters[1][i] == '+')
1308 int t = strlen(outpars)-1;
1309 if ((outpars[t] == '+') || (outpars[t] == '-'))
1315 strcat(outpars,"+");
1321 if (parameters[1][i] == '-')
1325 int t = strlen(outpars)-1;
1326 if ((outpars[t] == '+') || (outpars[t] == '-'))
1332 strcat(outpars,"-");
1340 if (strchr(user->modes,'o'))
1346 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,direction,false)))
1355 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,false)))
1357 char umode = parameters[1][i];
1358 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1360 int q = strlen(dmodes);
1361 int r = strlen(outpars);
1363 dmodes[q] = parameters[1][i];
1365 outpars[r] = parameters[1][i];
1366 if (parameters[1][i] == 'o')
1368 FOREACH_MOD OnGlobalOper(dest);
1375 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,false)) && (strchr(dmodes,parameters[1][i])))
1377 char umode = parameters[1][i];
1378 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1384 unsigned int r = strlen(outpars);
1386 outpars[r] = parameters[1][i];
1389 for (q = 0; q < strlen(dmodes); q++)
1391 if (dmodes[q] != parameters[1][i])
1395 strlcat(temp,moo,MAXBUF);
1398 strlcpy(dmodes,temp,52);
1411 strlcpy(b,"",MAXBUF);
1414 while (i < strlen (outpars))
1416 b[z++] = outpars[i++];
1418 if (i<strlen(outpars)-1)
1420 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1422 // someones playing silly buggers and trying
1423 // to put a +- or -+ into the line...
1427 if (i == strlen(outpars)-1)
1429 if ((outpars[i] == '-') || (outpars[i] == '+'))
1437 if ((b[z] == '-') || (b[z] == '+'))
1440 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1445 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1446 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
1449 if (strlen(dmodes)>MAXMODES)
1451 dmodes[MAXMODES-1] = '\0';
1453 log(DEBUG,"Stripped mode line");
1454 log(DEBUG,"Line dest is now %s",dmodes);
1455 strlcpy(dest->modes,dmodes,52);
1462 Ptr = FindChan(parameters[0]);
1467 /* just /modes #channel */
1468 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
1469 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1475 char* mode = parameters[1];
1479 FOREACH_RESULT(OnRawMode(user, Ptr, *mode, "", false, 0));
1485 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1487 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1489 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1492 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1494 // list of items for an extmode
1495 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1496 FOREACH_MOD OnSendList(user,Ptr,*mode);
1502 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)))
1504 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1511 FOREACH_RESULT(OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1513 if (MOD_RESULT == ACR_DENY)
1515 if (MOD_RESULT == ACR_DEFAULT)
1517 if (cstatus(user,Ptr) < STATUS_HOP)
1519 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1524 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1529 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1536 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1542 char outpars[MAXBUF];
1544 dest = Find(parameters[0]);
1546 // fix: ChroNiCk found this - we cant use this as debug if its null!
1549 log(DEBUG,"server_mode on %s",dest->nick);
1552 if ((dest) && (pcnt > 1))
1554 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1555 parameters[1] = (char*)tidied.c_str();
1557 char dmodes[MAXBUF];
1558 strlcpy(dmodes,dest->modes,52);
1564 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1567 for (unsigned int i = 0; i < strlen(parameters[1]); i++)
1569 if (parameters[1][i] == ' ')
1571 if (parameters[1][i] == '+')
1575 int t = strlen(outpars)-1;
1576 if ((outpars[t] == '+') || (outpars[t] == '-'))
1582 strcat(outpars,"+");
1588 if (parameters[1][i] == '-')
1592 int t = strlen(outpars)-1;
1593 if ((outpars[t] == '+') || (outpars[t] == '-'))
1599 strcat(outpars,"-");
1606 log(DEBUG,"begin mode processing entry");
1612 log(DEBUG,"umode %c being added",parameters[1][i]);
1613 if ((!strchr(dmodes,parameters[1][i])) && (ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,true,true)))
1615 char umode = parameters[1][i];
1616 log(DEBUG,"umode %c is an allowed umode",umode);
1617 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1619 int v1 = strlen(dmodes);
1620 int v2 = strlen(outpars);
1622 dmodes[v1] = parameters[1][i];
1624 outpars[v2] = parameters[1][i];
1630 // can only remove a mode they already have
1631 log(DEBUG,"umode %c being removed",parameters[1][i]);
1632 if ((ServerInstance->ModeGrok->AllowedUmode(parameters[1][i],user->modes,false,true)) && (strchr(dmodes,parameters[1][i])))
1634 char umode = parameters[1][i];
1635 log(DEBUG,"umode %c is an allowed umode",umode);
1636 if ((ServerInstance->ModeGrok->ProcessModuleUmode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
1642 unsigned int v1 = strlen(outpars);
1644 outpars[v1] = parameters[1][i];
1646 for (q = 0; q < strlen(dmodes); q++)
1648 if (dmodes[q] != parameters[1][i])
1652 strlcat(temp,moo,MAXBUF);
1655 strlcpy(dmodes,temp,52);
1665 strlcpy(b,"",MAXBUF);
1668 while (i < strlen (outpars))
1670 b[z++] = outpars[i++];
1672 if (i<strlen(outpars)-1)
1674 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
1676 // someones playing silly buggers and trying
1677 // to put a +- or -+ into the line...
1681 if (i == strlen(outpars)-1)
1683 if ((outpars[i] == '-') || (outpars[i] == '+'))
1691 if ((b[z] == '-') || (b[z] == '+'))
1694 if ((!b[0]) || (!strcmp(b,"+")) || (!strcmp(b,"-")))
1699 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1700 FOREACH_MOD OnMode(user, dest, TYPE_USER, b);
1703 if (strlen(dmodes)>MAXMODES)
1705 dmodes[MAXMODES-1] = '\0';
1707 log(DEBUG,"Stripped mode line");
1708 log(DEBUG,"Line dest is now %s",dmodes);
1709 strlcpy(dest->modes,dmodes,MAXMODES);
1716 Ptr = FindChan(parameters[0]);
1719 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1723 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);