1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 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 userrec* ModeParser::SanityChecks(userrec *user,char *dest,chanrec *chan,int status)
60 if ((!user) || (!dest) || (!chan) || (!*dest))
67 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, dest);
73 char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
75 for (unsigned int i = 0; i < d->chans.size(); i++)
77 if ((d->chans[i].channel != NULL) && (chan != NULL))
78 if (d->chans[i].channel == chan)
80 if (d->chans[i].uc_modes & MASK)
84 d->chans[i].uc_modes = d->chans[i].uc_modes | MASK;
88 d->chans[i].channel->AddOppedUser((char*)d);
91 d->chans[i].channel->AddHalfoppedUser((char*)d);
94 d->chans[i].channel->AddVoicedUser((char*)d);
97 log(DEBUG,"grant: %s %s",d->chans[i].channel->name,d->nick);
104 char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
106 for (unsigned int i = 0; i < d->chans.size(); i++)
108 if ((d->chans[i].channel != NULL) && (chan != NULL))
109 if (d->chans[i].channel == chan)
111 if ((d->chans[i].uc_modes & MASK) == 0)
115 d->chans[i].uc_modes ^= MASK;
119 d->chans[i].channel->DelOppedUser((char*)d);
122 d->chans[i].channel->DelHalfoppedUser((char*)d);
125 d->chans[i].channel->DelVoicedUser((char*)d);
128 log(DEBUG,"revoke: %s %s",d->chans[i].channel->name,d->nick);
135 char* ModeParser::GiveOps(userrec *user,char *dest,chanrec *chan,int status)
137 userrec *d = this->SanityChecks(user,dest,chan,status);
141 if (user->server == d->server)
144 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_OP));
146 if (MOD_RESULT == ACR_DENY)
148 if (MOD_RESULT == ACR_DEFAULT)
150 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
152 log(DEBUG,"%s cant give ops to %s because they have status %d and needs %d",user->nick,dest,status,STATUS_OP);
153 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
159 return this->Grant(d,chan,UCMODE_OP);
164 char* ModeParser::GiveHops(userrec *user,char *dest,chanrec *chan,int status)
166 userrec *d = this->SanityChecks(user,dest,chan,status);
170 if (user->server == d->server)
173 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_HALFOP));
175 if (MOD_RESULT == ACR_DENY)
177 if (MOD_RESULT == ACR_DEFAULT)
179 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
181 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
187 return this->Grant(d,chan,UCMODE_HOP);
192 char* ModeParser::GiveVoice(userrec *user,char *dest,chanrec *chan,int status)
194 userrec *d = this->SanityChecks(user,dest,chan,status);
198 if (user->server == d->server)
201 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_VOICE));
203 if (MOD_RESULT == ACR_DENY)
205 if (MOD_RESULT == ACR_DEFAULT)
207 if ((status < STATUS_HOP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
209 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
215 return this->Grant(d,chan,UCMODE_VOICE);
220 char* ModeParser::TakeOps(userrec *user,char *dest,chanrec *chan,int status)
222 userrec *d = this->SanityChecks(user,dest,chan,status);
226 if (user->server == d->server)
229 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEOP));
231 if (MOD_RESULT == ACR_DENY)
233 if (MOD_RESULT == ACR_DEFAULT)
235 if ((status < STATUS_OP) && (!is_uline(user->server)) && (IS_LOCAL(user)) && (IS_LOCAL(user)))
237 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
243 return this->Revoke(d,chan,UCMODE_OP);
248 char* ModeParser::TakeHops(userrec *user,char *dest,chanrec *chan,int status)
250 userrec *d = this->SanityChecks(user,dest,chan,status);
254 if (user->server == d->server)
257 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEHALFOP));
259 if (MOD_RESULT == ACR_DENY)
261 if (MOD_RESULT == ACR_DEFAULT)
263 /* Tweak by Brain suggested by w00t, allow a halfop to dehalfop themselves */
264 if ((user != d) && ((status < STATUS_OP) && (!is_uline(user->server))) && (IS_LOCAL(user)))
266 WriteServ(user->fd,"482 %s %s :You are not a channel operator",user->nick, chan->name);
272 return this->Revoke(d,chan,UCMODE_HOP);
277 char* ModeParser::TakeVoice(userrec *user,char *dest,chanrec *chan,int status)
279 userrec *d = this->SanityChecks(user,dest,chan,status);
283 if (user->server == d->server)
286 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,d,chan,AC_DEVOICE));
288 if (MOD_RESULT == ACR_DENY)
290 if (MOD_RESULT == ACR_DEFAULT)
292 if ((status < STATUS_HOP) && (!is_uline(user->server)) && (IS_LOCAL(user)))
294 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
300 return this->Revoke(d,chan,UCMODE_VOICE);
305 char* ModeParser::AddBan(userrec *user,char *dest,chanrec *chan,int status)
308 int toomanyexclamation = 0;
311 if ((!user) || (!dest) || (!chan) || (!*dest))
313 log(DEFAULT,"*** BUG *** AddBan was given an invalid parameter");
317 for (char* i = dest; *i; i++)
319 if ((*i < 32) || (*i > 126))
325 toomanyexclamation++;
333 if (toomanyexclamation != 1 || toomanyat != 1)
335 * this stops sillyness like n!u!u!u@h, though note that most
336 * ircds don't actually verify banmask validity. --w00t
340 long maxbans = GetMaxBans(chan->name);
341 if ((unsigned)chan->bans.size() > (unsigned)maxbans)
343 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);
347 log(DEBUG,"AddBan: %s %s",chan->name,user->nick);
350 FOREACH_RESULT(I_OnAddBan,OnAddBan(user,chan,dest));
355 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
357 if (!strcasecmp(i->data,dest))
359 // dont allow a user to set the same ban twice
365 strlcpy(b.data,dest,MAXBUF);
368 strlcpy(b.set_by,user->nick,NICKMAX-1);
372 strlcpy(b.set_by,Config->ServerName,NICKMAX-1);
374 chan->bans.push_back(b);
378 char* ModeParser::TakeBan(userrec *user,char *dest,chanrec *chan,int status)
380 if ((!user) || (!dest) || (!chan) || (!*dest)) {
381 log(DEFAULT,"*** BUG *** TakeBan was given an invalid parameter");
385 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
386 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
388 if (!strcasecmp(i->data,dest))
391 FOREACH_RESULT(I_OnDelBan,OnDelBan(user,chan,dest));
401 // tidies up redundant modes, e.g. +nt-nt+i becomes +-+i,
402 // a section further down the chain tidies up the +-+- crap.
403 std::string ModeParser::CompressModes(std::string modes,bool channelmodes)
407 memset(counts,0,sizeof(counts));
408 memset(active,0,sizeof(active));
409 for (unsigned int i = 0; i < modes.length(); i++)
411 if ((modes[i] == '+') || (modes[i] == '-'))
415 if ((strchr("itnmsp",modes[i])) || ((ModeDefined(modes[i],MT_CHANNEL)) && (ModeDefinedOn(modes[i],MT_CHANNEL)==0) && (ModeDefinedOff(modes[i],MT_CHANNEL)==0)))
417 log(DEBUG,"Tidy mode %c",modes[i]);
418 counts[(unsigned int)modes[i]]++;
419 active[(unsigned int)modes[i]] = true;
424 log(DEBUG,"Tidy mode %c",modes[i]);
425 counts[(unsigned int)modes[i]]++;
426 active[(unsigned int)modes[i]] = true;
429 for (int j = 65; j < 127; j++)
431 if ((counts[j] > 1) && (active[j] == true))
434 v[0] = (unsigned char)j;
436 std::string mode_str = v;
437 std::string::size_type pos = modes.find(mode_str);
438 if (pos != std::string::npos)
440 log(DEBUG,"all occurances of mode %c to be deleted...",(unsigned char)j);
441 while (modes.find(mode_str) != std::string::npos)
442 modes.erase(modes.find(mode_str),1);
443 log(DEBUG,"New mode line: %s",modes.c_str());
450 void ModeParser::ProcessModes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
453 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
457 char outlist[MAXBUF];
464 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
475 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
476 if (MOD_RESULT == ACR_DENY)
480 log(DEBUG,"process_modes: start: parameters=%d",pcnt);
482 char* modelist = parameters[1]; /* mode list, e.g. +oo-o *
483 * parameters[2] onwards are parameters for
484 * modes that require them :) */
485 *outlist = *modelist;
486 char* outl = outlist+1;
488 mdir = (*modelist == '+');
490 log(DEBUG,"process_modes: modelist: %s",modelist);
492 std::string tidied = this->CompressModes(modelist,true);
493 strlcpy(modelist,tidied.c_str(),MAXBUF);
495 int len = tidied.length();
496 while (modelist[len-1] == ' ')
497 modelist[--len] = '\0';
499 bool next_cant_be_modifier = false;
502 for (modechar = (modelist + 1); *modechar; ptr++, modechar++)
506 /* If we have more than MAXMODES changes in one line,
507 * drop all after the MAXMODES
512 log(DEBUG,"Mode %c",*modechar);
520 next_cant_be_modifier = true;
527 next_cant_be_modifier = true;
532 if ((param >= pcnt)) break;
533 log(DEBUG,"Enough parameters left");
538 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
541 log(DEBUG,"calling GiveOps");
542 r = GiveOps(user,parameters[param++],chan,status);
549 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
552 log(DEBUG,"calling TakeOps");
553 r = TakeOps(user,parameters[param++],chan,status);
565 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
570 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
573 r = GiveHops(user,parameters[param++],chan,status);
580 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
583 r = TakeHops(user,parameters[param++],chan,status);
596 if ((param >= pcnt)) break;
601 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
604 r = GiveVoice(user,parameters[param++],chan,status);
611 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
614 r = TakeVoice(user,parameters[param++],chan,status);
626 if ((param >= pcnt)) break;
631 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
634 r = AddBan(user,parameters[param++],chan,status);
641 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
644 r = TakeBan(user,parameters[param++],chan,status);
651 outpars[pc++] = parameters[param-1];
665 if (previously_unset_k)
667 previously_set_k = true;
672 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
677 strlcpy(key,parameters[param++],32);
679 strlcpy(chan->key,key,MAXBUF);
687 /* checks on -k are case sensitive and only accurate to the
688 first 32 characters */
689 if (previously_set_k)
691 previously_unset_k = true;
695 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
698 strlcpy(key,parameters[param++],32);
699 /* only allow -k if correct key given */
700 if (!strcmp(chan->key,key))
714 if (previously_set_l)
716 previously_unset_l = true;
718 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
729 if ((param >= pcnt)) break;
734 if (previously_unset_l)
736 previously_set_l = true;
737 bool invalid = false;
738 for (char* f = parameters[param]; *f; f++)
740 if ((*f < '0') || (*f > '9'))
745 /* If the limit is < 1, or the new limit is the current limit, dont allow */
746 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
755 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
759 chan->limit = atoi(parameters[param]);
761 // reported by mech: large values cause underflow
763 chan->limit = 0x7FFF;
769 outpars[pc++] = parameters[param++];
777 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
782 if (!(chan->binarymodes & CM_INVITEONLY)) *outl++ = 'i';
783 chan->binarymodes |= CM_INVITEONLY;
787 if (chan->binarymodes & CM_INVITEONLY) *outl++ = 'i';
788 chan->binarymodes &= ~CM_INVITEONLY;
795 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
800 if (!(chan->binarymodes & CM_TOPICLOCK)) *outl++ = 't';
801 chan->binarymodes |= CM_TOPICLOCK;
805 if (chan->binarymodes & CM_TOPICLOCK) *outl++ = 't';
806 chan->binarymodes &= ~CM_TOPICLOCK;
813 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
818 if (!(chan->binarymodes & CM_NOEXTERNAL)) *outl++ = 'n';
819 chan->binarymodes |= CM_NOEXTERNAL;
823 if (chan->binarymodes & CM_NOEXTERNAL) *outl++ = 'n';
824 chan->binarymodes &= ~CM_NOEXTERNAL;
831 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
836 if (!(chan->binarymodes & CM_MODERATED)) *outl++ = 'm';
837 chan->binarymodes |= CM_MODERATED;
841 if (chan->binarymodes & CM_MODERATED) *outl++ = 'm';
842 chan->binarymodes &= ~CM_MODERATED;
849 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
854 if (!(chan->binarymodes & CM_SECRET)) *outl++ = 's';
855 chan->binarymodes |= CM_SECRET;
856 if (chan->binarymodes & CM_PRIVATE)
858 chan->binarymodes &= ~CM_PRIVATE;
861 *outl++ = '-'; *outl++ = 'p'; *outl++ = '+';
867 if (chan->binarymodes & CM_SECRET) *outl++ = 's';
868 chan->binarymodes &= ~CM_SECRET;
875 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
880 if (!(chan->binarymodes & CM_PRIVATE)) *outl++ = 'p';
881 chan->binarymodes |= CM_PRIVATE;
882 if (chan->binarymodes & CM_SECRET)
884 chan->binarymodes &= ~CM_SECRET;
887 *outl++ = '-'; *outl++ = 's'; *outl++ = '+';
893 if (chan->binarymodes & CM_PRIVATE) *outl++ = 'p';
894 chan->binarymodes &= ~CM_PRIVATE;
900 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",*modechar,chan->custom_modes);
903 if (((!strchr(chan->custom_modes,*modechar)) && (!mdir)) || ((strchr(chan->custom_modes,*modechar)) && (mdir)))
905 if (!ModeIsListMode(*modechar,MT_CHANNEL))
907 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
911 if (ModeDefined(*modechar,MT_CHANNEL))
913 log(DEBUG,"A module has claimed this mode");
916 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
918 p.push_back(parameters[param]);
920 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
922 p.push_back(parameters[param]);
925 bool handled = false;
928 // we're supposed to have a parameter, but none was given... so dont handle the mode.
929 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
931 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
937 // BIG ASS IDIOTIC CODER WARNING!
938 // Using OnRawMode on another modules mode's behavour
939 // will confuse the crap out of admins! just because you CAN
940 // do it, doesnt mean you SHOULD!
942 std::string para = "";
945 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
948 for (int i = 0; i <= MODCOUNT; i++)
952 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
955 log(DEBUG,"OnExtendedMode returned nonzero for a module");
956 if (ModeIsListMode(*modechar,MT_CHANNEL))
969 outpars[pc++] = parameters[param++];
977 chan->SetCustomMode(*modechar,mdir);
978 // include parameters in output if mode has them
979 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
981 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
982 outpars[pc++] = parameters[param++];
986 // break, because only one module can handle the mode.
995 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
1003 /* Null terminate it now we're done */
1007 /************ Fast, but confusing string tidying ************/
1009 while (*outl && (*outl < 'A'))
1011 /* outl now points to the first mode character after +'s and -'s */
1013 /* Now points at first mode-modifier + or - symbol */
1015 /* Now we tidy off any trailing -'s etc */
1018 while ((*--trim == '+') || (*trim == '-'))
1020 /************ Done wih the string tidy functions ************/
1023 /* The mode change must be at least two characters long (+ or - and at least one mode) */
1024 if (((*outl == '+') || (*outl == '-')) && *(outl+1))
1026 for (ptr = 0; ptr < pc; ptr++)
1028 charlcat(outl,' ',MAXBUF);
1029 strlcat(outl,outpars[ptr],MAXBUF-1);
1033 log(DEBUG,"Local mode change");
1034 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outl);
1035 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1043 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outl);
1051 WriteChannel(chan,user,"MODE %s %s",chan->name,outl);
1052 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1059 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1061 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1063 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1064 // Servers can +o and -o arbitrarily
1065 if ((serveroverride == true) && (umode == 'o'))
1069 // RFC1459 specified modes
1070 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1072 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1076 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1077 if ((strchr(sourcemodes,'o')) && (!adding))
1079 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1082 else if (umode == 'o')
1084 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1088 // process any module-defined modes that need oper
1089 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1091 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1095 if (ModeDefined(umode,MT_CLIENT))
1097 // process any module-defined modes that don't need oper
1098 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1099 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1101 // no, this mode needs oper, and this user 'aint got what it takes!
1107 // anything else - return false.
1108 log(DEBUG,"umode %c not known by any ruleset",umode);
1112 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1119 strlcpy(s2->nick,Config->ServerName,NICKMAX-1);
1128 if (ModeDefined(umode,MT_CLIENT))
1130 for (int i = 0; i <= MODCOUNT; i++)
1132 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1134 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1138 log(DEBUG,"No module claims umode %c",umode);
1157 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1163 char outpars[MAXBUF];
1164 bool next_ok = true;
1166 dest = Find(parameters[0]);
1173 if ((dest) && (pcnt == 1))
1175 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1179 if ((dest) && (pcnt > 1))
1181 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1182 parameters[1] = (char*)tidied.c_str();
1184 char dmodes[MAXBUF];
1185 strlcpy(dmodes,dest->modes,MAXMODES);
1186 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1191 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1202 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1206 outpars[0] = *parameters[1];
1208 direction = (*parameters[1] == '+');
1210 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1213 for (char* i = parameters[1]; *i; i++)
1218 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1223 if ((direction != 1) && (next_ok))
1225 charlcat(outpars,'+',MAXBUF);
1233 if ((direction != 0) && (next_ok))
1235 charlcat(outpars,'-',MAXBUF);
1243 if (strchr(user->modes,'o'))
1249 if ((*i == 'i') || (*i == 'w') || (*i == 's') || (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,direction,false)))
1258 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,false)))
1260 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1262 charlcat(dmodes,*i,MAXMODES);
1263 charlcat(outpars,*i,MAXMODES);
1266 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1273 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,false)) && (strchr(dmodes,*i)))
1275 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1277 charlcat(outpars,*i,MAXMODES);
1278 charremove(dmodes,*i);
1295 for (char* i = outpars; *i;)
1298 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1300 // someones playing silly buggers and trying
1301 // to put a +- or -+ into the line...
1306 // Someone's trying to make the last character in
1307 // the line be a + or - symbol.
1308 if ((*i == '-') || (*i == '+'))
1316 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1318 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1319 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1322 log(DEBUG,"Stripped mode line");
1323 log(DEBUG,"Line dest is now %s",dmodes);
1324 strlcpy(dest->modes,dmodes,MAXMODES-1);
1331 Ptr = FindChan(parameters[0]);
1336 /* just /modes #channel */
1337 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr,has_channel(user,Ptr)));
1338 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1344 char* mode = parameters[1];
1348 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, Ptr, *mode, "", false, 0));
1354 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1356 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1358 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1361 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1363 // list of items for an extmode
1364 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1365 FOREACH_MOD(I_OnSendList,OnSendList(user,Ptr,*mode));
1371 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)) && (IS_LOCAL(user)))
1373 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1380 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1382 if (MOD_RESULT == ACR_DENY)
1384 if (MOD_RESULT == ACR_DEFAULT)
1386 if ((cstatus(user,Ptr) < STATUS_HOP) && (IS_LOCAL(user)))
1388 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1393 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1398 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1405 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1411 char outpars[MAXBUF];
1412 bool next_ok = true;
1414 dest = Find(parameters[0]);
1416 // fix: ChroNiCk found this - we cant use this as debug if its null!
1419 log(DEBUG,"server_mode on %s",dest->nick);
1422 if ((dest) && (pcnt > 1))
1424 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1425 parameters[1] = (char*)tidied.c_str();
1427 char dmodes[MAXBUF];
1428 strlcpy(dmodes,dest->modes,MAXBUF);
1430 outpars[0] = *parameters[1];
1432 direction = (*parameters[1] == '+');
1434 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1437 for (char* i = parameters[1]; *i; i++)
1442 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1447 if ((direction != 1) && (next_ok))
1450 charlcat(outpars,'+',MAXBUF);
1457 if ((direction != 0) && (next_ok))
1460 charlcat(outpars,'-',MAXBUF);
1466 log(DEBUG,"begin mode processing entry");
1472 log(DEBUG,"umode %c being added",*i);
1473 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,true)))
1475 log(DEBUG,"umode %c is an allowed umode",*i);
1476 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1478 charlcat(dmodes,*i,MAXMODES);
1479 charlcat(outpars,*i,MAXMODES);
1485 // can only remove a mode they already have
1486 log(DEBUG,"umode %c being removed",*i);
1487 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,true)) && (strchr(dmodes,*i)))
1489 log(DEBUG,"umode %c is an allowed umode",*i);
1490 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1492 charlcat(outpars,*i,MAXMODES);
1493 charremove(dmodes,*i);
1505 for (char* i = outpars; *i;)
1508 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1510 // someones playing silly buggers and trying
1511 // to put a +- or -+ into the line...
1516 // Someone's trying to make the last character in
1517 // the line be a + or - symbol.
1518 if ((*i == '-') || (*i == '+'))
1526 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1528 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1529 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1532 log(DEBUG,"Stripped mode line");
1533 log(DEBUG,"Line dest is now %s",dmodes);
1534 strlcpy(dest->modes,dmodes,MAXMODES-1);
1541 Ptr = FindChan(parameters[0]);
1544 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1548 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);