]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/mode.cpp
Convert remaining InspIRCd::Log() calls to new logging system
[user/henk/code/inspircd.git] / src / mode.cpp
index acfd16da96bb185523100f92f03aec7d3b4a7feb..b3f05bd3710639237bbcb7ac209c271baa2bbfe0 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
  * See: http://www.inspircd.org/wiki/index.php/Credits
  *
  * This program is free but copyrighted software; see
  * ---------------------------------------------------
  */
 
+/* $Core: libIRCDmode */
+/* $ExtraDeps: $(RELCPPFILES) */
+/* $ExtraObjects: modes/modeclasses.a */
+/* $ExtraBuild: @${MAKE} -C "modes" DIRNAME="src/modes" CC="$(CC)" $(MAKEARGS) CPPFILES="$(CPPFILES)" */
+
 #include "inspircd.h"
 #include "inspstring.h"
 
@@ -49,8 +54,8 @@
 /* +n (notice mask - our implementation of snomasks) */
 #include "modes/umode_n.h"
 
-ModeHandler::ModeHandler(InspIRCd* Instance, char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly, char mprefix)
-       : ServerInstance(Instance), mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly), prefix(mprefix), count(0)
+ModeHandler::ModeHandler(InspIRCd* Instance, char modeletter, int parameters_on, int parameters_off, bool listmode, ModeType type, bool operonly, char mprefix, char prefixrequired)
+       : ServerInstance(Instance), mode(modeletter), n_params_on(parameters_on), n_params_off(parameters_off), list(listmode), m_type(type), oper(operonly), prefix(mprefix), count(0), prefixneeded(prefixrequired)
 {
 }
 
@@ -63,6 +68,16 @@ bool ModeHandler::IsListMode()
        return list;
 }
 
+char ModeHandler::GetNeededPrefix()
+{
+       return prefixneeded;
+}
+
+void ModeHandler::SetNeededPrefix(char needsprefix)
+{
+       prefixneeded = needsprefix;
+}
+
 unsigned int ModeHandler::GetPrefixRank()
 {
        return 0;
@@ -76,7 +91,7 @@ unsigned int ModeHandler::GetCount()
 void ModeHandler::ChangeCount(int modifier)
 {
        count += modifier;
-       ServerInstance->Log(DEBUG,"Change count for mode %c is now %d", mode, count);
+       ServerInstance->Logs->Log("MODE", DEBUG,"Change count for mode %c is now %d", mode, count);
 }
 
 ModeType ModeHandler::GetModeType()
@@ -104,12 +119,12 @@ char ModeHandler::GetModeChar()
        return mode;
 }
 
-ModeAction ModeHandler::OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
+ModeAction ModeHandler::OnModeChange(User*, User*, Channel*, std::string&, bool, bool)
 {
        return MODEACTION_DENY;
 }
 
-ModePair ModeHandler::ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
+ModePair ModeHandler::ModeSet(User*, User* dest, Channel* channel, const std::string&)
 {
        if (dest)
        {
@@ -121,15 +136,15 @@ ModePair ModeHandler::ModeSet(userrec* source, userrec* dest, chanrec* channel,
        }
 }
 
-void ModeHandler::DisplayList(userrec* user, chanrec* channel)
+void ModeHandler::DisplayList(User*, Channel*)
 {
 }
 
-void ModeHandler::DisplayEmptyList(userrec* user, chanrec* channel)
+void ModeHandler::DisplayEmptyList(User*, Channel*)
 {
 }
 
-bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string &their_param, const std::string &our_param, chanrec* channel)
+bool ModeHandler::CheckTimeStamp(time_t theirs, time_t ours, const std::string&, const std::string&, Channel*)
 {
        return (ours < theirs);
 }
@@ -152,18 +167,18 @@ ModeType ModeWatcher::GetModeType()
        return m_type;
 }
 
-bool ModeWatcher::BeforeMode(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding, ModeType type)
+bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool, ModeType, bool)
 {
        return true;
 }
 
-void ModeWatcher::AfterMode(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter, bool adding, ModeType type)
+void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool, ModeType, bool)
 {
 }
 
-userrec* ModeParser::SanityChecks(userrec *user,const char *dest,chanrec *chan,int status)
+User* ModeParser::SanityChecks(User *user, const char *dest, Channel *chan, int)
 {
-       userrec *d;
+       User *d;
        if ((!user) || (!dest) || (!chan) || (!*dest))
        {
                return NULL;
@@ -177,7 +192,7 @@ userrec* ModeParser::SanityChecks(userrec *user,const char *dest,chanrec *chan,i
        return d;
 }
 
-const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
+const char* ModeParser::Grant(User *d,Channel *chan,int MASK)
 {
        if (!chan)
                return "";
@@ -207,7 +222,7 @@ const char* ModeParser::Grant(userrec *d,chanrec *chan,int MASK)
        return "";
 }
 
-const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
+const char* ModeParser::Revoke(User *d,Channel *chan,int MASK)
 {
        if (!chan)
                return "";
@@ -237,7 +252,7 @@ const char* ModeParser::Revoke(userrec *d,chanrec *chan,int MASK)
        return "";
 }
 
-void ModeParser::DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec* targetchannel, const char* text)
+void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targetchannel, const char* text)
 {
        if (targetchannel)
        {
@@ -274,13 +289,13 @@ void ModeParser::DisplayCurrentModes(userrec *user, userrec* targetuser, chanrec
        return;
 }
 
-void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool servermode)
+void ModeParser::Process(const char* const* parameters, int pcnt, User *user, bool servermode)
 {
        std::string target = parameters[0];
        ModeType type = MODETYPE_USER;
        unsigned char mask = 0;
-       chanrec* targetchannel = ServerInstance->FindChan(parameters[0]);
-       userrec* targetuser  = ServerInstance->FindNick(parameters[0]);
+       Channel* targetchannel = ServerInstance->FindChan(parameters[0]);
+       User* targetuser  = ServerInstance->FindNick(parameters[0]);
 
        LastParse.clear();
 
@@ -306,7 +321,7 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool
                                mode++;
                                continue;
                        }
-
+                       
                        /* Ensure the user doesnt request the same mode twice,
                         * so they cant flood themselves off out of idiocy.
                         */
@@ -325,6 +340,14 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool
 
                        if ((mh) && (mh->IsListMode()))
                        {
+                               int MOD_RESULT = 0;
+                               FOREACH_RESULT(I_OnRawMode, OnRawMode(user, targetchannel, *mode, "", true, 0));
+                               if (MOD_RESULT == ACR_DENY)
+                               {
+                                       mode++;
+                                       continue;
+                               }
+
                                if (ServerInstance->Config->HideModeLists[mletter] && (targetchannel->GetStatus(user) < STATUS_HOP))
                                {
                                        user->WriteServ("482 %s %s :Only half-operators and above may view the +%c list",user->nick, targetchannel->name, *mode++);
@@ -364,6 +387,8 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool
        }
        else if (pcnt > 1)
        {
+               bool SkipAccessChecks = false;
+
                if (targetchannel)
                {
                        type = MODETYPE_CHANNEL;
@@ -373,27 +398,14 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool
                         * (e.g. are they a (half)op?
                         */
 
-                       if ((IS_LOCAL(user)) && (targetchannel->GetStatus(user) < STATUS_HOP))
+                       if ((IS_LOCAL(user)) && (!ServerInstance->ULine(user->server)) && (!servermode))
                        {
                                /* We don't have halfop */
                                int MOD_RESULT = 0;
                                FOREACH_RESULT(I_OnAccessCheck,OnAccessCheck(user, NULL, targetchannel, AC_GENERAL_MODE));
                                if (MOD_RESULT == ACR_DENY)
                                        return;
-
-                               if (MOD_RESULT == ACR_DEFAULT)
-                               {
-                                       /* Are we a uline or is it a servermode? */
-                                       if ((!ServerInstance->ULine(user->server)) && (!servermode))
-                                       {
-                                               /* Not enough permission:
-                                                * NOT a uline and NOT a servermode,
-                                                * OR, NOT halfop or above.
-                                                */
-                                               user->WriteServ("482 %s %s :You're not a channel (half)operator",user->nick, targetchannel->name);
-                                               return;
-                                       }
-                               }
+                               SkipAccessChecks = (MOD_RESULT == ACR_ALLOW);
                        }
                }
                else if (targetuser)
@@ -478,6 +490,8 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool
 
                                                if (modehandlers[handler_id]->GetModeType() == type)
                                                {
+                                                       int MOD_RESULT = 0;
+
                                                        if (modehandlers[handler_id]->GetNumParams(adding))
                                                        {
                                                                /* This mode expects a parameter, do we have any parameters left in our list to use? */
@@ -495,42 +509,73 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool
                                                                        continue;
                                                                }
 
-                                                               bool had_parameter = !parameter.empty();
-                                                               
-                                                               for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
-                                                               {
-                                                                       if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == false)
-                                                                       {
-                                                                               abort = true;
-                                                                               break;
-                                                                       }
-                                                                       /* A module whacked the parameter completely, and there was one. abort. */
-                                                                       if ((had_parameter) && (parameter.empty()))
-                                                                       {
-                                                                               abort = true;
-                                                                               break;
-                                                                       }
-                                                               }
-
-                                                               if (abort)
-                                                                       continue;
+                                                               FOREACH_RESULT(I_OnRawMode, OnRawMode(user, targetchannel, modechar, parameter, adding, 1, servermode));
                                                        }
                                                        else
                                                        {
-                                                               /* Fix by brain: mode watchers not being called for parameterless modes */
-                                                               for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
+                                                               FOREACH_RESULT(I_OnRawMode, OnRawMode(user, targetchannel, modechar, "", adding, 0, servermode));
+                                                       }
+
+                                                       if (IS_LOCAL(user) && (MOD_RESULT == ACR_DENY))
+                                                               continue;
+
+                                                       if (!SkipAccessChecks && IS_LOCAL(user) && (MOD_RESULT != ACR_ALLOW))
+                                                       {
+                                                               ServerInstance->Logs->Log("MODE", DEBUG,"Enter minimum prefix check");
+                                                               /* Check access to this mode character */
+                                                               if ((type == MODETYPE_CHANNEL) && (modehandlers[handler_id]->GetNeededPrefix()))
                                                                {
-                                                                       if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type) == false)
+                                                                       char needed = modehandlers[handler_id]->GetNeededPrefix();
+                                                                       ModeHandler* prefixmode = FindPrefix(needed);
+                                                                       ServerInstance->Logs->Log("MODE", DEBUG,"Needed prefix: %c", needed);
+
+                                                                       /* If the mode defined by the handler is not '\0', but the handler for it
+                                                                        * cannot be found, they probably dont have the right module loaded to implement
+                                                                        * the prefix they want to compare the mode against, e.g. '&' for m_chanprotect.
+                                                                        * Revert to checking against the minimum core prefix, '%'.
+                                                                        */
+                                                                       if (needed && !prefixmode)
+                                                                               prefixmode = FindPrefix('%');
+                                               
+                                                                       unsigned int neededrank = prefixmode->GetPrefixRank();
+                                                                       /* Compare our rank on the channel against the rank of the required prefix,
+                                                                        * allow if >= ours. Because mIRC and xchat throw a tizz if the modes shown
+                                                                        * in NAMES(X) are not in rank order, we know the most powerful mode is listed
+                                                                        * first, so we don't need to iterate, we just look up the first instead.
+                                                                        */
+                                                                       std::string modestring = targetchannel->GetAllPrefixChars(user);
+                                                                       char ml = (modestring.empty() ? '\0' : modestring[0]);
+                                                                       ModeHandler* ourmode = FindPrefix(ml);
+                                                                       if (!ourmode || ourmode->GetPrefixRank() < neededrank)
                                                                        {
-                                                                               abort = true;
-                                                                               break;
+                                                                               /* Bog off */
+                                                                               user->WriteServ("482 %s %s :You must have channel privilege %c or above to %sset channel mode %c",
+                                                                                               user->nick, targetchannel->name, needed, adding ? "" : "un", modechar);
+                                                                               continue;
                                                                        }
                                                                }
+                                                       }
 
-                                                               if (abort)
-                                                                       continue;
+                                                       bool had_parameter = !parameter.empty();
+                                                               
+                                                       for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
+                                                       {
+                                                               if ((*watchers)->BeforeMode(user, targetuser, targetchannel, parameter, adding, type, servermode) == false)
+                                                               {
+                                                                       abort = true;
+                                                                       break;
+                                                               }
+                                                               /* A module whacked the parameter completely, and there was one. abort. */
+                                                               if ((had_parameter) && (parameter.empty()))
+                                                               {
+                                                                       abort = true;
+                                                                       break;
+                                                               }
                                                        }
 
+                                                       if (abort)
+                                                               continue;
+
                                                        /* It's an oper only mode, check if theyre an oper. If they arent,
                                                         * eat any parameter that  came with the mode, and continue to next
                                                         */
@@ -543,7 +588,7 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool
                                                        }
 
                                                        /* Call the handler for the mode */
-                                                       ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding);
+                                                       ModeAction ma = modehandlers[handler_id]->OnModeChange(user, targetuser, targetchannel, parameter, adding, servermode);
 
                                                        if ((modehandlers[handler_id]->GetNumParams(adding)) && (parameter.empty()))
                                                        {
@@ -577,7 +622,7 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool
                                                                        /* Does this mode have a prefix? */
                                                                        if (modehandlers[handler_id]->GetPrefix() && targetchannel)
                                                                        {
-                                                                               userrec* user_to_prefix = ServerInstance->FindNick(parameter);
+                                                                               User* user_to_prefix = ServerInstance->FindNick(parameter);
                                                                                if (user_to_prefix)
                                                                                        targetchannel->SetPrefix(user_to_prefix, modehandlers[handler_id]->GetPrefix(),
                                                                                                        modehandlers[handler_id]->GetPrefixRank(), adding);
@@ -586,7 +631,7 @@ void ModeParser::Process(const char** parameters, int pcnt, userrec *user, bool
 
                                                                /* Call all the AfterMode events in the mode watchers for this mode */
                                                                for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
-                                                                       (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type);
+                                                                       (*watchers)->AfterMode(user, targetuser, targetchannel, parameter, adding, type, servermode);
 
                                                                /* Reset the state change flag */
                                                                state_change = false;
@@ -687,7 +732,7 @@ void ModeParser::CleanMask(std::string &mask)
        }
 }
 
-bool ModeParser::AddMode(ModeHandler* mh, unsigned const char modeletter)
+bool ModeParser::AddMode(ModeHandler* mh)
 {
        unsigned char mask = 0;
        unsigned char pos = 0;
@@ -733,7 +778,7 @@ bool ModeParser::DelMode(ModeHandler* mh)
        switch (mh->GetModeType())
        {
                case MODETYPE_USER:
-                       for (user_hash::iterator i = ServerInstance->clientlist->begin(); i != ServerInstance->clientlist->end(); i++)
+                       for (user_hash::iterator i = ServerInstance->Users->clientlist->begin(); i != ServerInstance->Users->clientlist->end(); i++)
                        {
                                mh->RemoveMode(i->second);
                        }
@@ -833,7 +878,7 @@ ModeHandler* ModeParser::FindPrefix(unsigned const char pfxletter)
        return NULL;
 }
 
-std::string ModeParser::ModeString(userrec* user, chanrec* channel)
+std::string ModeParser::ModeString(User* user, Channel* channel)
 {
        std::string types;
        std::string pars;
@@ -996,7 +1041,7 @@ bool ModeParser::DelModeWatcher(ModeWatcher* mw)
 
 /** This default implementation can remove simple user modes
  */
-void ModeHandler::RemoveMode(userrec* user)
+void ModeHandler::RemoveMode(User* user)
 {
        char moderemove[MAXBUF];
        const char* parameters[] = { user->nick, moderemove };
@@ -1011,7 +1056,7 @@ void ModeHandler::RemoveMode(userrec* user)
 /** This default implementation can remove simple channel modes
  * (no parameters)
  */
-void ModeHandler::RemoveMode(chanrec* channel)
+void ModeHandler::RemoveMode(Channel* channel)
 {
        char moderemove[MAXBUF];
        const char* parameters[] = { channel->name, moderemove };
@@ -1025,31 +1070,26 @@ void ModeHandler::RemoveMode(chanrec* channel)
 
 ModeParser::ModeParser(InspIRCd* Instance) : ServerInstance(Instance)
 {
-       struct Initializer
+       ModeHandler* modes[] =
        {
-               char modechar;
-               ModeHandler* handler;
-       };
-
-       Initializer modes[] = {
-               { 's', new ModeChannelSecret(Instance) },
-               { 'p', new ModeChannelPrivate(Instance) },
-               { 'm', new ModeChannelModerated(Instance) },
-               { 't', new ModeChannelTopicOps(Instance) },
-               { 'n', new ModeChannelNoExternal(Instance) },
-               { 'i', new ModeChannelInviteOnly(Instance) },
-               { 'k', new ModeChannelKey(Instance) },
-               { 'l', new ModeChannelLimit(Instance) },
-               { 'b', new ModeChannelBan(Instance) },
-               { 'o', new ModeChannelOp(Instance) },
-               { 'h', new ModeChannelHalfOp(Instance) },
-               { 'v', new ModeChannelVoice(Instance) },
-               { 's', new ModeUserServerNotice(Instance) },
-               { 'w', new ModeUserWallops(Instance) },
-               { 'i', new ModeUserInvisible(Instance) },
-               { 'o', new ModeUserOperator(Instance) },
-               { 'n', new ModeUserServerNoticeMask(Instance) },
-               { 0, NULL }
+               new ModeChannelSecret(Instance),
+               new ModeChannelPrivate(Instance),
+               new ModeChannelModerated(Instance),
+               new ModeChannelTopicOps(Instance),
+               new ModeChannelNoExternal(Instance),
+               new ModeChannelInviteOnly(Instance),
+               new ModeChannelKey(Instance),
+               new ModeChannelLimit(Instance),
+               new ModeChannelBan(Instance),
+               new ModeChannelOp(Instance),
+               new ModeChannelHalfOp(Instance),
+               new ModeChannelVoice(Instance),
+               new ModeUserServerNotice(Instance),
+               new ModeUserWallops(Instance),
+               new ModeUserInvisible(Instance),
+               new ModeUserOperator(Instance),
+               new ModeUserServerNoticeMask(Instance),
+               NULL
        };
 
        /* Clear mode list */
@@ -1060,6 +1100,6 @@ ModeParser::ModeParser(InspIRCd* Instance) : ServerInstance(Instance)
        LastParse.clear();
 
        /* Initialise the RFC mode letters */
-       for (int index = 0; modes[index].modechar; index++)
-               this->AddMode(modes[index].handler, modes[index].modechar);
+       for (int index = 0; modes[index]; index++)
+               this->AddMode(modes[index]);
 }