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)
452 if ((!parameters) || (pcnt < 2)) {
456 char outlist[MAXBUF];
463 bool k_set = false, l_set = false, previously_set_l = false, previously_unset_l = false, previously_set_k = false, previously_unset_k = false;
469 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,chan,AC_GENERAL_MODE));
470 if (MOD_RESULT == ACR_DENY)
474 char* modelist = parameters[1]; /* mode list, e.g. +oo-o *
475 * parameters[2] onwards are parameters for
476 * modes that require them :) */
477 *outlist = *modelist;
478 char* outl = outlist+1;
480 mdir = (*modelist == '+');
482 log(DEBUG,"process_modes: modelist: %s",modelist);
484 std::string tidied = this->CompressModes(modelist,true);
485 strlcpy(modelist,tidied.c_str(),MAXBUF);
487 int len = tidied.length();
488 while (modelist[len-1] == ' ')
489 modelist[--len] = '\0';
491 for (char* modechar = (modelist + 1); *modechar; ptr++, modechar++)
495 /* If we have more than MAXMODES changes in one line,
496 * drop all after the MAXMODES
517 if ((param >= pcnt)) break;
518 log(DEBUG,"Enough parameters left");
523 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], true, 1));
526 log(DEBUG,"calling GiveOps");
527 r = GiveOps(user,parameters[param++],chan,status);
534 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'o', parameters[param], false, 1));
537 log(DEBUG,"calling TakeOps");
538 r = TakeOps(user,parameters[param++],chan,status);
550 if (((param >= pcnt)) || (!Config->AllowHalfop)) break;
555 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], true, 1));
558 r = GiveHops(user,parameters[param++],chan,status);
565 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'h', parameters[param], false, 1));
568 r = TakeHops(user,parameters[param++],chan,status);
581 if ((param >= pcnt)) break;
586 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], true, 1));
589 r = GiveVoice(user,parameters[param++],chan,status);
596 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'v', parameters[param], false, 1));
599 r = TakeVoice(user,parameters[param++],chan,status);
611 if ((param >= pcnt)) break;
616 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], true, 1));
619 r = AddBan(user,parameters[param++],chan,status);
626 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'b', parameters[param], false, 1));
629 r = TakeBan(user,parameters[param++],chan,status);
636 outpars[pc++] = parameters[param-1];
650 if (previously_unset_k)
652 previously_set_k = true;
657 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], true, 1));
662 strlcpy(key,parameters[param++],32);
664 strlcpy(chan->key,key,MAXBUF);
672 /* checks on -k are case sensitive and only accurate to the
673 first 32 characters */
674 if (previously_set_k)
676 previously_unset_k = true;
680 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'k', parameters[param], false, 1));
683 strlcpy(key,parameters[param++],32);
684 /* only allow -k if correct key given */
685 if (!strcmp(chan->key,key))
699 if (previously_set_l)
701 previously_unset_l = true;
703 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', "", false, 0));
714 if ((param >= pcnt)) break;
719 if (previously_unset_l)
721 previously_set_l = true;
722 bool invalid = false;
723 for (char* f = parameters[param]; *f; f++)
725 if ((*f < '0') || (*f > '9'))
730 /* If the limit is < 1, or the new limit is the current limit, dont allow */
731 if ((atoi(parameters[param]) < 1) || ((chan->limit > 0) && (atoi(parameters[param]) == chan->limit)))
740 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'l', parameters[param], true, 1));
744 chan->limit = atoi(parameters[param]);
746 // reported by mech: large values cause underflow
748 chan->limit = 0x7FFF;
754 outpars[pc++] = parameters[param++];
762 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'i', "", mdir, 0));
767 if (!(chan->binarymodes & CM_INVITEONLY)) *outl++ = 'i';
768 chan->binarymodes |= CM_INVITEONLY;
772 if (chan->binarymodes & CM_INVITEONLY) *outl++ = 'i';
773 chan->binarymodes &= ~CM_INVITEONLY;
780 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 't', "", mdir, 0));
785 if (!(chan->binarymodes & CM_TOPICLOCK)) *outl++ = 't';
786 chan->binarymodes |= CM_TOPICLOCK;
790 if (chan->binarymodes & CM_TOPICLOCK) *outl++ = 't';
791 chan->binarymodes &= ~CM_TOPICLOCK;
798 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'n', "", mdir, 0));
803 if (!(chan->binarymodes & CM_NOEXTERNAL)) *outl++ = 'n';
804 chan->binarymodes |= CM_NOEXTERNAL;
808 if (chan->binarymodes & CM_NOEXTERNAL) *outl++ = 'n';
809 chan->binarymodes &= ~CM_NOEXTERNAL;
816 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'm', "", mdir, 0));
821 if (!(chan->binarymodes & CM_MODERATED)) *outl++ = 'm';
822 chan->binarymodes |= CM_MODERATED;
826 if (chan->binarymodes & CM_MODERATED) *outl++ = 'm';
827 chan->binarymodes &= ~CM_MODERATED;
834 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 's', "", mdir, 0));
839 if (!(chan->binarymodes & CM_SECRET)) *outl++ = 's';
840 chan->binarymodes |= CM_SECRET;
841 if (chan->binarymodes & CM_PRIVATE)
843 chan->binarymodes &= ~CM_PRIVATE;
846 *outl++ = '-'; *outl++ = 'p'; *outl++ = '+';
852 if (chan->binarymodes & CM_SECRET) *outl++ = 's';
853 chan->binarymodes &= ~CM_SECRET;
860 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, 'p', "", mdir, 0));
865 if (!(chan->binarymodes & CM_PRIVATE)) *outl++ = 'p';
866 chan->binarymodes |= CM_PRIVATE;
867 if (chan->binarymodes & CM_SECRET)
869 chan->binarymodes &= ~CM_SECRET;
872 *outl++ = '-'; *outl++ = 's'; *outl++ = '+';
878 if (chan->binarymodes & CM_PRIVATE) *outl++ = 'p';
879 chan->binarymodes &= ~CM_PRIVATE;
885 log(DEBUG,"Preprocessing custom mode %c: modelist: %s",*modechar,chan->custom_modes);
888 if (((!strchr(chan->custom_modes,*modechar)) && (!mdir)) || ((strchr(chan->custom_modes,*modechar)) && (mdir)))
890 if (!ModeIsListMode(*modechar,MT_CHANNEL))
892 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",*modechar,chan->name);
896 if (ModeDefined(*modechar,MT_CHANNEL))
898 log(DEBUG,"A module has claimed this mode");
901 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
903 p.push_back(parameters[param]);
905 if ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir))
907 p.push_back(parameters[param]);
910 bool handled = false;
913 // we're supposed to have a parameter, but none was given... so dont handle the mode.
914 if (((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(*modechar,MT_CHANNEL)>0) && (!mdir)))
916 log(DEBUG,"Not enough parameters for module-mode %c",*modechar);
921 // BIG ASS IDIOTIC CODER WARNING!
922 // Using OnRawMode on another modules mode's behavour
923 // will confuse the crap out of admins! just because you CAN
924 // do it, doesnt mean you SHOULD!
926 std::string para = "";
929 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, chan, *modechar, para, mdir, pcnt));
932 for (int i = 0; i <= MODCOUNT; i++)
936 int t = modules[i]->OnExtendedMode(user,chan,*modechar,MT_CHANNEL,mdir,p);
939 log(DEBUG,"OnExtendedMode returned nonzero for a module");
940 if (ModeIsListMode(*modechar,MT_CHANNEL))
953 outpars[pc++] = parameters[param++];
961 chan->SetCustomMode(*modechar,mdir);
962 // include parameters in output if mode has them
963 if ((ModeDefinedOn(*modechar,MT_CHANNEL)>0) && (mdir))
965 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
966 outpars[pc++] = parameters[param++];
970 // break, because only one module can handle the mode.
979 WriteServ(user->fd,"472 %s %c :is unknown mode char to me",user->nick,*modechar);
985 /* Null terminate it now we're done */
989 /************ Fast, but confusing string tidying ************/
991 while (*outl && (*outl < 'A'))
993 /* outl now points to the first mode character after +'s and -'s */
995 /* Now points at first mode-modifier + or - symbol */
997 /* Now we tidy off any trailing -'s etc */
1000 while ((*--trim == '+') || (*trim == '-'))
1002 /************ Done wih the string tidy functions ************/
1005 /* The mode change must be at least two characters long (+ or - and at least one mode) */
1006 if (((*outl == '+') || (*outl == '-')) && *(outl+1))
1008 for (ptr = 0; ptr < pc; ptr++)
1010 charlcat(outl,' ',MAXBUF);
1011 strlcat(outl,outpars[ptr],MAXBUF-1);
1015 log(DEBUG,"Local mode change");
1016 WriteChannelLocal(chan, user, "MODE %s %s",chan->name,outl);
1017 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1025 WriteChannelWithServ(Config->ServerName,chan,"MODE %s %s",chan->name,outl);
1033 WriteChannel(chan,user,"MODE %s %s",chan->name,outl);
1034 FOREACH_MOD(I_OnMode,OnMode(user, chan, TYPE_CHANNEL, outl));
1041 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
1043 bool ModeParser::AllowedUmode(char umode, char* sourcemodes,bool adding,bool serveroverride)
1045 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
1046 // Servers can +o and -o arbitrarily
1047 if ((serveroverride == true) && (umode == 'o'))
1051 // RFC1459 specified modes
1052 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
1054 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1058 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
1059 if ((strchr(sourcemodes,'o')) && (!adding))
1061 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1064 else if (umode == 'o')
1066 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
1070 // process any module-defined modes that need oper
1071 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
1073 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
1077 if (ModeDefined(umode,MT_CLIENT))
1079 // process any module-defined modes that don't need oper
1080 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
1081 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
1083 // no, this mode needs oper, and this user 'aint got what it takes!
1089 // anything else - return false.
1090 log(DEBUG,"umode %c not known by any ruleset",umode);
1094 bool ModeParser::ProcessModuleUmode(char umode, userrec* source, void* dest, bool adding)
1101 strlcpy(s2->nick,Config->ServerName,NICKMAX-1);
1110 if (ModeDefined(umode,MT_CLIENT))
1112 for (int i = 0; i <= MODCOUNT; i++)
1114 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
1116 log(DEBUG,"Module %s claims umode %c",Config->module_names[i].c_str(),umode);
1120 log(DEBUG,"No module claims umode %c",umode);
1139 void cmd_mode::Handle (char **parameters, int pcnt, userrec *user)
1145 char outpars[MAXBUF];
1146 bool next_ok = true;
1148 dest = Find(parameters[0]);
1155 if ((dest) && (pcnt == 1))
1157 WriteServ(user->fd,"221 %s :+%s",dest->nick,dest->modes);
1161 if ((dest) && (pcnt > 1))
1163 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1164 parameters[1] = (char*)tidied.c_str();
1166 char dmodes[MAXBUF];
1167 strlcpy(dmodes,dest->modes,MAXMODES);
1168 log(DEBUG,"pulled up dest user modes: %s",dmodes);
1173 if ((strchr(user->modes,'o')) || (is_uline(user->server)))
1184 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1188 outpars[0] = *parameters[1];
1190 direction = (*parameters[1] == '+');
1192 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1195 for (char* i = parameters[1]; *i; i++)
1200 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1205 if ((direction != 1) && (next_ok))
1207 charlcat(outpars,'+',MAXBUF);
1215 if ((direction != 0) && (next_ok))
1217 charlcat(outpars,'-',MAXBUF);
1225 if (strchr(user->modes,'o'))
1231 if ((*i == 'i') || (*i == 'w') || (*i == 's') || (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,direction,false)))
1240 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,false)))
1242 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1244 charlcat(dmodes,*i,MAXMODES);
1245 charlcat(outpars,*i,MAXMODES);
1248 FOREACH_MOD(I_OnGlobalOper,OnGlobalOper(dest));
1255 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,false)) && (strchr(dmodes,*i)))
1257 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1259 charlcat(outpars,*i,MAXMODES);
1260 charremove(dmodes,*i);
1277 for (char* i = outpars; *i;)
1280 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1282 // someones playing silly buggers and trying
1283 // to put a +- or -+ into the line...
1288 // Someone's trying to make the last character in
1289 // the line be a + or - symbol.
1290 if ((*i == '-') || (*i == '+'))
1298 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1300 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1301 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1304 log(DEBUG,"Stripped mode line");
1305 log(DEBUG,"Line dest is now %s",dmodes);
1306 strlcpy(dest->modes,dmodes,MAXMODES-1);
1313 Ptr = FindChan(parameters[0]);
1318 /* just /modes #channel */
1319 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr,has_channel(user,Ptr)));
1320 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1326 char* mode = parameters[1];
1330 FOREACH_RESULT(I_OnRawMode,OnRawMode(user, Ptr, *mode, "", false, 0));
1336 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1338 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
1340 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
1343 if ((ModeDefined(*mode,MT_CHANNEL)) && (ModeIsListMode(*mode,MT_CHANNEL)))
1345 // list of items for an extmode
1346 log(DEBUG,"Calling OnSendList for all modules, list output for mode %c",*mode);
1347 FOREACH_MOD(I_OnSendList,OnSendList(user,Ptr,*mode));
1353 if (((Ptr) && (!has_channel(user,Ptr))) && (!is_uline(user->server)) && (IS_LOCAL(user)))
1355 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, Ptr->name);
1362 FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user,NULL,Ptr,AC_GENERAL_MODE));
1364 if (MOD_RESULT == ACR_DENY)
1366 if (MOD_RESULT == ACR_DEFAULT)
1368 if ((cstatus(user,Ptr) < STATUS_HOP) && (IS_LOCAL(user)))
1370 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
1375 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
1380 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
1387 void ModeParser::ServerMode(char **parameters, int pcnt, userrec *user)
1393 char outpars[MAXBUF];
1394 bool next_ok = true;
1396 dest = Find(parameters[0]);
1398 // fix: ChroNiCk found this - we cant use this as debug if its null!
1401 log(DEBUG,"server_mode on %s",dest->nick);
1404 if ((dest) && (pcnt > 1))
1406 std::string tidied = ServerInstance->ModeGrok->CompressModes(parameters[1],false);
1407 parameters[1] = (char*)tidied.c_str();
1409 char dmodes[MAXBUF];
1410 strlcpy(dmodes,dest->modes,MAXBUF);
1412 outpars[0] = *parameters[1];
1414 direction = (*parameters[1] == '+');
1416 if ((*parameters[1] != '+') && (*parameters[1] != '-'))
1419 for (char* i = parameters[1]; *i; i++)
1424 if ((i != parameters[1]) && (*i != '+') && (*i != '-'))
1429 if ((direction != 1) && (next_ok))
1432 charlcat(outpars,'+',MAXBUF);
1439 if ((direction != 0) && (next_ok))
1442 charlcat(outpars,'-',MAXBUF);
1448 log(DEBUG,"begin mode processing entry");
1454 log(DEBUG,"umode %c being added",*i);
1455 if ((!strchr(dmodes,*i)) && (ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,true,true)))
1457 log(DEBUG,"umode %c is an allowed umode",*i);
1458 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1460 charlcat(dmodes,*i,MAXMODES);
1461 charlcat(outpars,*i,MAXMODES);
1467 // can only remove a mode they already have
1468 log(DEBUG,"umode %c being removed",*i);
1469 if ((ServerInstance->ModeGrok->AllowedUmode(*i,user->modes,false,true)) && (strchr(dmodes,*i)))
1471 log(DEBUG,"umode %c is an allowed umode",*i);
1472 if ((ServerInstance->ModeGrok->ProcessModuleUmode(*i, user, dest, direction)) || (*i == 'i') || (*i == 's') || (*i == 'w') || (*i == 'o'))
1474 charlcat(outpars,*i,MAXMODES);
1475 charremove(dmodes,*i);
1487 for (char* i = outpars; *i;)
1490 if (((*i == '-') || (*i == '+')) && ((*(i+1) == '-') || (*(i+1) == '+')))
1492 // someones playing silly buggers and trying
1493 // to put a +- or -+ into the line...
1498 // Someone's trying to make the last character in
1499 // the line be a + or - symbol.
1500 if ((*i == '-') || (*i == '+'))
1508 if ((*b) && (!IS_SINGLE(b,'+')) && (!IS_SINGLE(b,'-')))
1510 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
1511 FOREACH_MOD(I_OnMode,OnMode(user, dest, TYPE_USER, b));
1514 log(DEBUG,"Stripped mode line");
1515 log(DEBUG,"Line dest is now %s",dmodes);
1516 strlcpy(dest->modes,dmodes,MAXMODES-1);
1523 Ptr = FindChan(parameters[0]);
1526 ServerInstance->ModeGrok->ProcessModes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
1530 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);