summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Powell <petpow@saberuk.com>2019-02-08 13:31:38 +0000
committerPeter Powell <petpow@saberuk.com>2019-02-09 10:08:35 +0000
commit2680c7907a0b05f65214fc63043322d57a61f455 (patch)
tree4730fd434883a24e4bf3bd97b3774b8b5be8b342
parent6b23b222bc6e0d3ea7fbf81a2499a4e97cb63ae4 (diff)
Move message handling code to Handle{Channel,Server,User}Target.
This makes the logic for this module considerably easier to read.
-rw-r--r--src/coremods/core_message.cpp302
1 files changed, 156 insertions, 146 deletions
diff --git a/src/coremods/core_message.cpp b/src/coremods/core_message.cpp
index dca01e370..961307b06 100644
--- a/src/coremods/core_message.cpp
+++ b/src/coremods/core_message.cpp
@@ -135,199 +135,209 @@ class CommandMessage : public Command
ChanModeReference moderatedmode;
ChanModeReference noextmsgmode;
- /** Send a PRIVMSG or NOTICE message to all local users from the given user
- * @param source The user sending the message.
- * @param msg The details of the message to send.
- */
- static void SendAll(User* source, const MessageDetails& details)
+ CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
{
- ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, source, "$*", details.text, details.type);
- message.AddTags(details.tags_out);
- message.SetSideEffect(true);
- ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
-
- const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
- for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
+ Channel* chan = ServerInstance->FindChan(target);
+ if (!chan)
{
- LocalUser* user = *i;
- if ((user->registered == REG_ALL) && (!details.exemptions.count(user)))
- user->Send(messageevent);
+ // The target channel does not exist.
+ source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
+ return CMD_FAILURE;
}
- }
- public:
- CommandMessage(Module* parent, MessageType mt)
- : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
- , msgtype(mt)
- , moderatedmode(parent, "moderated")
- , noextmsgmode(parent, "noextmsg")
- {
- syntax = "<target>{,<target>} <message>";
- }
+ if (IS_LOCAL(source))
+ {
+ if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(source))
+ {
+ // The noextmsg mode is set and the source is not in the channel.
+ source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
+ return CMD_FAILURE;
+ }
- /** Handle command.
- * @param parameters The parameters to the command
- * @param user The user issuing the command
- * @return A value from CmdResult to indicate command success or failure.
- */
- CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
+ bool no_chan_priv = chan->GetPrefixValue(source) < VOICE_VALUE;
+ if (no_chan_priv && chan->IsModeSet(moderatedmode))
+ {
+ // The moderated mode is set and the source has no status rank.
+ source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
+ return CMD_FAILURE;
+ }
- RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
- {
- if (IS_LOCAL(user))
- // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
- return ROUTE_LOCALONLY;
- else
- return ROUTE_MESSAGE(parameters[0]);
- }
-};
+ if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(source))
+ {
+ // The source is banned in the channel and restrictbannedusers is enabled.
+ if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
+ source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
+ return CMD_FAILURE;
+ }
+ }
-CmdResult CommandMessage::Handle(User* user, const Params& parameters)
-{
- User *dest;
- Channel *chan;
+ // Fire the pre-message events.
+ MessageTarget msgtarget(chan, pm ? pm->GetPrefix() : 0);
+ MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
+ msgdetails.exemptions.insert(source);
+ if (!FirePreEvents(source, msgtarget, msgdetails))
+ return CMD_FAILURE;
- if (CommandParser::LoopCall(user, this, parameters, 0))
- return CMD_SUCCESS;
+ // Send the message to the members of the channel.
+ ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, chan, msgdetails.text, msgdetails.type, msgtarget.status);
+ privmsg.AddTags(msgdetails.tags_out);
+ privmsg.SetSideEffect(true);
+ chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
- if (parameters[0][0] == '$')
+ // Create the outgoing message and message event.
+ return FirePostEvent(source, msgtarget, msgdetails);
+ }
+
+ CmdResult HandleServerTarget(User* source, const Params& parameters)
{
- if (!user->HasPrivPermission("users/mass-message"))
- return CMD_SUCCESS;
+ // If the source isn't allowed to mass message users then reject
+ // the attempt to mass-message users.
+ if (!source->HasPrivPermission("users/mass-message"))
+ return CMD_FAILURE;
+ // Extract the server glob match from the target parameter.
std::string servername(parameters[0], 1);
+
+ // Fire the pre-message events.
MessageTarget msgtarget(&servername);
MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
- if (!FirePreEvents(user, msgtarget, msgdetails))
+ if (!FirePreEvents(source, msgtarget, msgdetails))
return CMD_FAILURE;
- if (InspIRCd::Match(ServerInstance->Config->ServerName, servername, NULL))
- {
- SendAll(user, msgdetails);
- }
- return FirePostEvent(user, msgtarget, msgdetails);
- }
-
- char status = 0;
- const char* target = parameters[0].c_str();
-
- if (ServerInstance->Modes->FindPrefix(*target))
- {
- status = *target;
- target++;
- }
- if (*target == '#')
- {
- chan = ServerInstance->FindChan(target);
-
- if (chan)
+ // If the current server name matches the server name glob then send
+ // the message out to the local users.
+ if (InspIRCd::Match(ServerInstance->Config->ServerName, servername))
{
- if (IS_LOCAL(user) && chan->GetPrefixValue(user) < VOICE_VALUE)
+ // Create the outgoing message and message event.
+ ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, source, "$*", msgdetails.text, msgdetails.type);
+ message.AddTags(msgdetails.tags_out);
+ message.SetSideEffect(true);
+ ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
+
+ const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
+ for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
{
- if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
- {
- user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
- return CMD_FAILURE;
- }
-
- if (chan->IsModeSet(moderatedmode))
- {
- user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
- return CMD_FAILURE;
- }
-
- if (ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL)
- {
- if (chan->IsBanned(user))
- {
- if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
- user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
- return CMD_FAILURE;
- }
- }
- }
+ LocalUser* luser = *i;
- MessageTarget msgtarget(chan, status);
- MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
- msgdetails.exemptions.insert(user);
- if (!FirePreEvents(user, msgtarget, msgdetails))
- return CMD_FAILURE;
+ // Don't send to unregistered users or the user who is the source.
+ if (luser->registered != REG_ALL || luser == source)
+ continue;
- ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, chan, msgdetails.text, msgdetails.type, msgtarget.status);
- privmsg.AddTags(msgdetails.tags_out);
- privmsg.SetSideEffect(true);
- chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
- return FirePostEvent(user, msgtarget, msgdetails);
- }
- else
- {
- /* channel does not exist */
- user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
- return CMD_FAILURE;
+ // Only send to non-exempt users.
+ if (!msgdetails.exemptions.count(luser))
+ luser->Send(messageevent);
+ }
}
- }
- const char* destnick = parameters[0].c_str();
+ // Fire the post-message event.
+ return FirePostEvent(source, msgtarget, msgdetails);
+ }
- if (IS_LOCAL(user))
+ CmdResult HandleUserTarget(User* source, const Params& parameters)
{
- const char* targetserver = strchr(destnick, '@');
-
- if (targetserver)
+ User* target;
+ if (IS_LOCAL(source))
{
- std::string nickonly;
-
- nickonly.assign(destnick, 0, targetserver - destnick);
- dest = ServerInstance->FindNickOnly(nickonly);
- if (dest && strcasecmp(dest->server->GetName().c_str(), targetserver + 1))
+ // Local sources can specify either a nick or a nick@server mask as the target.
+ const char* targetserver = strchr(parameters[0].c_str(), '@');
+ if (targetserver)
{
- /* Incorrect server for user */
- user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
- return CMD_FAILURE;
+ // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
+ target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
+ if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
+ target = NULL;
+ }
+ else
+ {
+ // If the source is a local user then we only look up the target by nick.
+ target = ServerInstance->FindNickOnly(parameters[0]);
}
}
else
- dest = ServerInstance->FindNickOnly(destnick);
- }
- else
- dest = ServerInstance->FindNick(destnick);
-
- if ((dest) && (dest->registered == REG_ALL))
- {
- if (parameters[1].empty())
{
- user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
- return CMD_FAILURE;
+ // Remote users can only specify a nick or UUID as the target.
+ target = ServerInstance->FindNick(parameters[0]);
}
- if ((dest->IsAway()) && (msgtype == MSG_PRIVMSG))
+ if (!target || target->registered != REG_ALL)
{
- /* auto respond with aweh msg */
- user->WriteNumeric(RPL_AWAY, dest->nick, dest->awaymsg);
+ // The target user does not exist or is not fully registered.
+ source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
+ return CMD_FAILURE;
}
- MessageTarget msgtarget(dest);
+ // If the target is away then inform the user.
+ if (target->IsAway() && msgtype == MSG_PRIVMSG)
+ source->WriteNumeric(RPL_AWAY, target->nick, target->awaymsg);
+
+ // Fire the pre-message events.
+ MessageTarget msgtarget(target);
MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
- if (!FirePreEvents(user, msgtarget, msgdetails))
+ if (!FirePreEvents(source, msgtarget, msgdetails))
return CMD_FAILURE;
- LocalUser* const localtarget = IS_LOCAL(dest);
+ LocalUser* const localtarget = IS_LOCAL(target);
if (localtarget)
{
- // direct write, same server
- ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, localtarget->nick, msgdetails.text, msgtype);
+ // Send to the target if they are a local user.
+ ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, localtarget->nick, msgdetails.text, msgtype);
privmsg.AddTags(msgdetails.tags_out);
privmsg.SetSideEffect(true);
localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
}
- return FirePostEvent(user, msgtarget, msgdetails);
+
+ // Fire the post-message event.
+ return FirePostEvent(source, msgtarget, msgdetails);
}
- else
+
+ public:
+ CommandMessage(Module* parent, MessageType mt)
+ : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
+ , msgtype(mt)
+ , moderatedmode(parent, "moderated")
+ , noextmsgmode(parent, "noextmsg")
{
- /* no such nick/channel */
- user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
- return CMD_FAILURE;
+ syntax = "<target>{,<target>} <message>";
}
+
+ /** Handle command.
+ * @param parameters The parameters to the command
+ * @param user The user issuing the command
+ * @return A value from CmdResult to indicate command success or failure.
+ */
+ CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
+
+ RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
+ {
+ if (IS_LOCAL(user))
+ // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
+ return ROUTE_LOCALONLY;
+ else
+ return ROUTE_MESSAGE(parameters[0]);
+ }
+};
+
+CmdResult CommandMessage::Handle(User* user, const Params& parameters)
+{
+ if (CommandParser::LoopCall(user, this, parameters, 0))
+ return CMD_SUCCESS;
+
+ // The target is a server glob.
+ if (parameters[0][0] == '$')
+ return HandleServerTarget(user, parameters);
+
+ // If the message begins with a status character then look it up.
+ const char* target = parameters[0].c_str();
+ PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
+ if (pmh)
+ target++;
+
+ // The target is a channel name.
+ if (*target == '#')
+ return HandleChannelTarget(user, parameters, target, pmh);
+
+ // The target is a nickname.
+ return HandleUserTarget(user, parameters);
}
class ModuleCoreMessage : public Module