-/* +------------------------------------+
- * | Inspire Internet Relay Chat Daemon |
- * +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
*
- * InspIRCd: (C) 2002-2009 InspIRCd Development Team
- * See: http://wiki.inspircd.org/Credits
+ * 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>
+ * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
+ * Copyright (C) 2004-2008 Craig Edwards <craigedwards@brainbox.cc>
+ * Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
*
- * This program is free but copyrighted software; see
- * the file COPYING for details.
+ * This file is part of InspIRCd. InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
*
- * ---------------------------------------------------
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+
#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"
+/* +w (see wallops) */
+/* +i (invisible) */
+#include "modes/simplemodes.h"
+/* +b (bans) */
+#include "modes/cmode_b.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"
-/* +h (channel halfop) */
-#include "modes/cmode_h.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"
ModeHandler::ModeHandler(Module* Creator, const std::string& Name, char modeletter, ParamSpec Params, ModeType type)
- : m_paramtype(TR_TEXT), parameters_taken(Params), mode(modeletter), prefix(0), oper(false),
- list(false), m_type(type), count(0), levelrequired(HALFOP_VALUE), creator(Creator), name(Name)
+ : ServiceProvider(Creator, Name, SERVICE_MODE), m_paramtype(TR_TEXT),
+ parameters_taken(Params), mode(modeletter), prefix(0), oper(false),
+ list(false), m_type(type), levelrequired(HALFOP_VALUE)
{
}
ModeHandler::~ModeHandler()
{
- if (ServerInstance && ServerInstance->Modes && ServerInstance->Modes->FindMode(mode, m_type) == this)
- ServerInstance->Logs->Log("MODE", ERROR, "ERROR: Destructor for mode %c called while still registered", mode);
}
bool ModeHandler::IsListMode()
return 0;
}
-unsigned int ModeHandler::GetCount()
-{
- return 0;
-}
-
-void ModeHandler::ChangeCount(int modifier)
-{
- count += modifier;
- ServerInstance->Logs->Log("MODE", DEBUG,"Change count for mode %c is now %d", mode, count);
-}
-
int ModeHandler::GetNumParams(bool adding)
{
switch (parameters_taken)
return 0;
}
-char ModeHandler::GetModeChar()
-{
- return mode;
-}
-
std::string ModeHandler::GetUserParameter(User* user)
{
return "";
return MODEACTION_DENY;
}
-ModePair ModeHandler::ModeSet(User*, User* dest, Channel* channel, const std::string&)
-{
- if (dest)
- {
- return std::make_pair(dest->IsModeSet(this->mode), "");
- }
- else
- {
- return std::make_pair(channel->IsModeSet(this->mode), "");
- }
-}
-
void ModeHandler::DisplayList(User*, Channel*)
{
}
ModeAction SimpleUserModeHandler::OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, 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 ¶meter, bool adding)
{
+ /* 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;
+
+ /* 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 ¶meter, bool adding)
+{
+ if (adding && !ParamValidate(parameter))
+ return MODEACTION_DENY;
+ std::string now = channel->GetModeParameter(this);
+ if (parameter == now)
+ return MODEACTION_DENY;
if (adding)
- {
- if (!channel->IsModeSet(this->GetModeChar()))
- {
- channel->SetMode(this->GetModeChar(),true);
- return MODEACTION_ALLOW;
- }
- }
+ channel->SetModeParam(this, parameter);
else
- {
- if (channel->IsModeSet(this->GetModeChar()))
- {
- channel->SetMode(this->GetModeChar(),false);
- return MODEACTION_ALLOW;
- }
- }
+ channel->SetModeParam(this, "");
+ return MODEACTION_ALLOW;
+}
- return MODEACTION_DENY;
+bool ParamChannelModeHandler::ParamValidate(std::string& parameter)
+{
+ return true;
}
ModeWatcher::ModeWatcher(Module* Creator, char modeletter, ModeType type)
ModeHandler *mh = FindMode(modechar, type);
int pcnt = mh->GetNumParams(adding);
+ // crop mode parameter size to 250 characters
+ if (parameter.length() > 250 && adding)
+ parameter = parameter.substr(0, 250);
+
ModResult MOD_RESULT;
FIRST_MOD_RESULT(OnRawMode, MOD_RESULT, (user, chan, modechar, parameter, adding, pcnt));
unsigned int ourrank = chan->GetPrefixValue(user);
if (ourrank < neededrank)
{
- /* Bog off */
- // TODO replace with a real search for the proper prefix
- char needed = neededrank > HALFOP_VALUE ? '@' : '%';
- user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must have channel privilege %c or above to %sset channel mode %c",
- user->nick.c_str(), chan->name.c_str(), needed, adding ? "" : "un", modechar);
+ ModeHandler* neededmh = NULL;
+ for(char c='A'; c <= 'z'; c++)
+ {
+ ModeHandler *privmh = FindMode(c, MODETYPE_CHANNEL);
+ if (privmh && privmh->GetPrefixRank() >= neededrank)
+ {
+ // this mode is sufficient to allow this action
+ if (!neededmh || privmh->GetPrefixRank() < neededmh->GetPrefixRank())
+ neededmh = privmh;
+ }
+ }
+ if (neededmh)
+ user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You must have channel %s access or above to %sset channel mode %c",
+ user->nick.c_str(), chan->name.c_str(), neededmh->name.c_str(), adding ? "" : "un", modechar);
+ else
+ user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You cannot %sset channel mode %c",
+ user->nick.c_str(), chan->name.c_str(), adding ? "" : "un", modechar);
return MODEACTION_DENY;
}
}
if (IS_OPER(user))
{
user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Permission Denied - Oper type %s does not have access to set %s mode %c",
- user->nick.c_str(), irc::Spacify(user->oper.c_str()), type == MODETYPE_CHANNEL ? "channel" : "user", modechar);
+ user->nick.c_str(), user->oper->NameStr(), type == MODETYPE_CHANNEL ? "channel" : "user", modechar);
}
else
{
return MODEACTION_DENY;
}
+ if (mh->GetPrefixRank() && chan)
+ {
+ User* user_to_prefix = ServerInstance->FindNick(parameter);
+ if (!user_to_prefix)
+ return MODEACTION_DENY;
+ if (!chan->SetPrefix(user_to_prefix, modechar, adding))
+ return MODEACTION_DENY;
+ }
+
/* Call the handler for the mode */
ModeAction ma = mh->OnModeChange(user, targetuser, chan, parameter, adding);
if (ma != MODEACTION_ALLOW)
return ma;
- mh->ChangeCount(adding ? 1 : -1);
-
- if (mh->GetPrefixRank() && chan)
- {
- User* user_to_prefix = ServerInstance->FindNick(parameter);
- if (user_to_prefix)
- chan->SetPrefix(user_to_prefix, modechar, adding);
- }
-
for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
(*watchers)->AfterMode(user, targetuser, chan, parameter, adding, type);
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;
continue;
}
- std::string parameter = "";
+ std::string parameter;
int pcnt = mh->GetNumParams(adding);
if (pcnt && param_at == parameters.size())
{
bool display = true;
if (!user->HasPrivPermission("channels/auspex") && ServerInstance->Config->HideModeLists[mletter] && (chan->GetPrefixValue(user) < HALFOP_VALUE))
{
- user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :Only half-operators and above may view the +%c list",
+ user->WriteNumeric(ERR_CHANOPRIVSNEEDED, "%s %s :You do not have access to view the +%c list",
user->nick.c_str(), chan->name.c_str(), mletter);
display = false;
}
- /** See below for a description of what craq this is :D
- */
unsigned char handler_id = (mletter - 'A') | MASK_CHANNEL;
for(ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
if ((mh->GetPrefix() == ',') || (mh->GetPrefix() == ':') || (mh->GetPrefix() == '#'))
return false;
+ if (mh->GetPrefix() && FindPrefix(mh->GetPrefix()))
+ return false;
+
mh->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
pos = (mh->GetModeChar()-65) | mask;
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;
}
return NULL;
}
-std::string ModeParser::ModeString(User* user, Channel* channel, bool nick_suffix)
-{
- std::string types;
- std::string pars;
-
- if (!channel || !user)
- return "";
-
- for (unsigned char mode = 'A'; mode <= 'z'; mode++)
- {
- unsigned char pos = (mode-65) | MASK_CHANNEL;
- ModeHandler* mh = modehandlers[pos];
- if ((mh) && (mh->GetNumParams(true)) && (mh->GetNumParams(false)))
- {
- ModePair ret;
- ret = mh->ModeSet(NULL, user, channel, user->nick);
- if ((ret.first) && (ret.second == user->nick))
- {
- if (nick_suffix)
- {
- pars.append(" ");
- pars.append(user->nick);
- }
- types.push_back(mh->GetModeChar());
- }
- }
- }
-
- if (nick_suffix)
- return types+pars;
- else
- return types;
-}
-
std::string ModeParser::GiveModeList(ModeMasks m)
{
std::string type1; /* Listmodes EXCEPT those with a prefix */
return type1 + "," + type2 + "," + type3 + "," + type4;
}
-std::string ModeParser::BuildPrefixes()
+std::string ModeParser::BuildPrefixes(bool lettersAndModes)
{
std::string mletters;
std::string mprefixes;
mprefixes = mprefixes + n->second.second;
}
- return "(" + mprefixes + ")" + mletters;
+ return lettersAndModes ? "(" + mprefixes + ")" + mletters : mletters;
}
bool ModeParser::AddModeWatcher(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)
}
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);
}
}
*/
void ModeHandler::RemoveMode(Channel* channel, irc::modestacker* stack)
{
- char moderemove[MAXBUF];
- std::vector<std::string> parameters;
-
if (channel->IsModeSet(this->GetModeChar()))
{
if (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);
}
}
ModeChannelBan b;
ModeChannelOp o;
- // halfop is added by configreader
ModeChannelVoice v;
ModeUserWallops uw;
ModeParser::~ModeParser()
{
- ModeHandler* mh = FindMode('h', MODETYPE_CHANNEL);
- if (mh)
- {
- mh->cull();
- delete mh;
- }
}