]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/mode.cpp
m_spanningtree Remove unneeded #includes
[user/henk/code/inspircd.git] / src / mode.cpp
index 5efc98250d7e1851b93d88cce54d6a900e1e6145..768623bd5fa944c0249539fd678bba4120370dc2 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * InspIRCd -- Internet Relay Chat Daemon
  *
+ *   Copyright (C) 2012 Shawn Smith <shawn@inspircd.org>
  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
  *   Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
  *   Copyright (C) 2006-2008 Robin Burchell <robin+git@viroteck.net>
 
 
 #include "inspircd.h"
-#include "inspstring.h"
-
-/* +s (secret) */
-#include "modes/cmode_s.h"
-/* +p (private) */
-#include "modes/cmode_p.h"
-/* +b (bans) */
-#include "modes/cmode_b.h"
-/* +m (moderated) */
-#include "modes/cmode_m.h"
-/* +t (only (half) ops can change topic) */
-#include "modes/cmode_t.h"
-/* +n (no external messages) */
-#include "modes/cmode_n.h"
-/* +i (invite only) */
-#include "modes/cmode_i.h"
-/* +k (keyed channel) */
-#include "modes/cmode_k.h"
-/* +l (channel user limit) */
-#include "modes/cmode_l.h"
-/* +o (channel op) */
-#include "modes/cmode_o.h"
-/* +v (channel voice) */
-#include "modes/cmode_v.h"
-/* +w (see wallops) */
-#include "modes/umode_w.h"
-/* +i (invisible) */
-#include "modes/umode_i.h"
-/* +o (operator) */
-#include "modes/umode_o.h"
-/* +s (server notice masks) */
-#include "modes/umode_s.h"
+#include "builtinmodes.h"
 
 ModeHandler::ModeHandler(Module* Creator, const std::string& Name, char modeletter, ParamSpec Params, ModeType type)
        : ServiceProvider(Creator, Name, SERVICE_MODE), m_paramtype(TR_TEXT),
@@ -132,47 +102,39 @@ bool ModeHandler::ResolveModeConflict(std::string& theirs, const std::string& ou
 
 ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
 {
-       if (adding)
-       {
-               if (!dest->IsModeSet(this->GetModeChar()))
-               {
-                       dest->SetMode(this->GetModeChar(),true);
-                       return MODEACTION_ALLOW;
-               }
-       }
-       else
-       {
-               if (dest->IsModeSet(this->GetModeChar()))
-               {
-                       dest->SetMode(this->GetModeChar(),false);
-                       return MODEACTION_ALLOW;
-               }
-       }
+       /* We're either trying to add a mode we already have or
+               remove a mode we don't have, deny. */
+       if (dest->IsModeSet(this->GetModeChar()) == adding)
+               return MODEACTION_DENY;
 
-       return MODEACTION_DENY;
+       /* adding will be either true or false, depending on if we
+               are adding or removing the mode, since we already checked
+               to make sure we aren't adding a mode we have or that we
+               aren't removing a mode we don't have, we don't have to do any
+               other checks here to see if it's true or false, just add or
+               remove the mode */
+       dest->SetMode(this->GetModeChar(), adding);
+
+       return MODEACTION_ALLOW;
 }
 
 
 ModeAction SimpleChannelModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
 {
-       if (adding)
-       {
-               if (!channel->IsModeSet(this->GetModeChar()))
-               {
-                       channel->SetMode(this->GetModeChar(),true);
-                       return MODEACTION_ALLOW;
-               }
-       }
-       else
-       {
-               if (channel->IsModeSet(this->GetModeChar()))
-               {
-                       channel->SetMode(this->GetModeChar(),false);
-                       return MODEACTION_ALLOW;
-               }
-       }
+       /* We're either trying to add a mode we already have or
+               remove a mode we don't have, deny. */
+       if (channel->IsModeSet(this->GetModeChar()) == adding)
+               return MODEACTION_DENY;
 
-       return MODEACTION_DENY;
+       /* adding will be either true or false, depending on if we
+               are adding or removing the mode, since we already checked
+               to make sure we aren't adding a mode we have or that we
+               aren't removing a mode we don't have, we don't have to do any
+               other checks here to see if it's true or false, just add or
+               remove the mode */
+       channel->SetMode(this->GetModeChar(), adding);
+
+       return MODEACTION_ALLOW;
 }
 
 ModeAction ParamChannelModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
@@ -253,7 +215,7 @@ void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targ
                {
                        /* Display user's current mode string */
                        user->WriteNumeric(RPL_UMODEIS, "%s :+%s",targetuser->nick.c_str(),targetuser->FormatModes());
-                       if (IS_OPER(targetuser))
+                       if ((targetuser->IsOper()))
                                user->WriteNumeric(RPL_SNOMASKIS, "%s +%s :Server notice mask", targetuser->nick.c_str(), targetuser->FormatNoticeMasks());
                        return;
                }
@@ -334,7 +296,7 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, bool
                        return MODEACTION_DENY;
        }
 
-       if (IS_LOCAL(user) && !IS_OPER(user))
+       if (IS_LOCAL(user) && !user->IsOper())
        {
                char* disabled = (type == MODETYPE_CHANNEL) ? ServerInstance->Config->DisabledCModes : ServerInstance->Config->DisabledUModes;
                if (disabled[modechar - 'A'])
@@ -348,7 +310,7 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, bool
        if (adding && IS_LOCAL(user) && mh->NeedsOper() && !user->HasModePermission(modechar, type))
        {
                /* It's an oper only mode, and they don't have access to it. */
-               if (IS_OPER(user))
+               if (user->IsOper())
                {
                        user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - Oper type %s does not have access to set %s mode %c",
                                        user->nick.c_str(), user->oper->NameStr(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar);
@@ -402,7 +364,7 @@ void ModeParser::Process(const std::vector<std::string>& parameters, User *user,
        LastParseParams.clear();
        LastParseTranslate.clear();
 
-       if (!targetchannel && !targetuser)
+       if ((!targetchannel) && ((!targetuser) || (IS_SERVER(targetuser))))
        {
                user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel",user->nick.c_str(),target.c_str());
                return;
@@ -457,7 +419,7 @@ void ModeParser::Process(const std::vector<std::string>& parameters, User *user,
                        continue;
                }
 
-               std::string parameter = "";
+               std::string parameter;
                int pcnt = mh->GetNumParams(adding);
                if (pcnt && param_at == parameters.size())
                {
@@ -690,15 +652,20 @@ bool ModeParser::DelMode(ModeHandler* mh)
        switch (mh->GetModeType())
        {
                case MODETYPE_USER:
-                       for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
+                       for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); )
                        {
-                               mh->RemoveMode(i->second);
+                               User* user = i->second;
+                               ++i;
+                               mh->RemoveMode(user);
                        }
                break;
                case MODETYPE_CHANNEL:
-                       for (chan_hash::iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); i++)
+                       for (chan_hash::iterator i = ServerInstance->chanlist->begin(); i != ServerInstance->chanlist->end(); )
                        {
-                               mh->RemoveMode(i->second);
+                               // The channel may not be in the hash after RemoveMode(), see m_permchannels
+                               Channel* chan = i->second;
+                               ++i;
+                               mh->RemoveMode(chan);
                        }
                break;
        }
@@ -906,9 +873,6 @@ bool ModeParser::DelModeWatcher(ModeWatcher* mw)
  */
 void ModeHandler::RemoveMode(User* user, irc::modestacker* stack)
 {
-       char moderemove[MAXBUF];
-       std::vector<std::string> parameters;
-
        if (user->IsModeSet(this->GetModeChar()))
        {
                if (stack)
@@ -917,9 +881,10 @@ void ModeHandler::RemoveMode(User* user, irc::modestacker* stack)
                }
                else
                {
-                       sprintf(moderemove,"-%c",this->GetModeChar());
+                       std::vector<std::string> parameters;
                        parameters.push_back(user->nick);
-                       parameters.push_back(moderemove);
+                       parameters.push_back("-");
+                       parameters[1].push_back(this->GetModeChar());
                        ServerInstance->Modes->Process(parameters, ServerInstance->FakeClient);
                }
        }
@@ -930,9 +895,6 @@ void ModeHandler::RemoveMode(User* user, irc::modestacker* stack)
  */
 void ModeHandler::RemoveMode(Channel* channel, irc::modestacker* stack)
 {
-       char moderemove[MAXBUF];
-       std::vector<std::string> parameters;
-
        if (channel->IsModeSet(this->GetModeChar()))
        {
                if (stack)
@@ -941,9 +903,10 @@ void ModeHandler::RemoveMode(Channel* channel, irc::modestacker* stack)
                }
                else
                {
-                       sprintf(moderemove,"-%c",this->GetModeChar());
+                       std::vector<std::string> parameters;
                        parameters.push_back(channel->name);
-                       parameters.push_back(moderemove);
+                       parameters.push_back("-");
+                       parameters[1].push_back(this->GetModeChar());
                        ServerInstance->SendMode(parameters, ServerInstance->FakeClient);
                }
        }
@@ -970,28 +933,22 @@ struct builtin_modes
        ModeUserOperator uo;
        ModeUserServerNoticeMask us;
 
-       void init(ModeParser* modes)
-       {
-               modes->AddMode(&s);
-               modes->AddMode(&p);
-               modes->AddMode(&m);
-               modes->AddMode(&t);
-               modes->AddMode(&n);
-               modes->AddMode(&i);
-               modes->AddMode(&k);
-               modes->AddMode(&l);
-               modes->AddMode(&b);
-               modes->AddMode(&o);
-               modes->AddMode(&v);
-               modes->AddMode(&uw);
-               modes->AddMode(&ui);
-               modes->AddMode(&uo);
-               modes->AddMode(&us);
+       void init()
+       {
+               ServiceProvider* modes[] = { &s, &p, &m, &t, &n, &i, &k, &l, &b, &o, &v,
+                                                                        &uw, &ui, &uo, &us };
+               ServerInstance->Modules->AddServices(modes, sizeof(modes)/sizeof(ServiceProvider*));
        }
 };
 
 static builtin_modes static_modes;
 
+void ModeParser::InitBuiltinModes()
+{
+       static_modes.init();
+       static_modes.b.DoRehash();
+}
+
 ModeParser::ModeParser()
 {
        /* Clear mode handler list */
@@ -1002,8 +959,6 @@ ModeParser::ModeParser()
 
        seq = 0;
        memset(&sent, 0, sizeof(sent));
-
-       static_modes.init(this);
 }
 
 ModeParser::~ModeParser()