2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2017-2020 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2013, 2017-2018 Attila Molnar <attilamolnar@hush.com>
6 * Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9 * Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
11 * This file is part of InspIRCd. InspIRCd is free software: you can
12 * redistribute it and/or modify it under the terms of the GNU General Public
13 * License as published by the Free Software Foundation, version 2.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 class MessageDetailsImpl : public MessageDetails
31 MessageDetailsImpl(MessageType mt, const std::string& msg, const ClientProtocol::TagMap& tags)
32 : MessageDetails(mt, msg, tags)
36 bool IsCTCP(std::string& name, std::string& body) const CXX11_OVERRIDE
41 size_t end_of_name = text.find(' ', 2);
42 size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
43 if (end_of_name == std::string::npos)
45 // The CTCP only contains a name.
46 name.assign(text, 1, text.length() - 1 - end_of_ctcp);
51 // The CTCP contains a name and a body.
52 name.assign(text, 1, end_of_name - 1);
54 size_t start_of_body = text.find_first_not_of(' ', end_of_name + 1);
55 if (start_of_body == std::string::npos)
57 // The CTCP body is provided but empty.
62 // The CTCP body provided was non-empty.
63 body.assign(text, start_of_body, text.length() - start_of_body - end_of_ctcp);
67 bool IsCTCP(std::string& name) const CXX11_OVERRIDE
72 size_t end_of_name = text.find(' ', 2);
73 if (end_of_name == std::string::npos)
75 // The CTCP only contains a name.
76 size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
77 name.assign(text, 1, text.length() - 1 - end_of_ctcp);
81 // The CTCP contains a name and a body.
82 name.assign(text, 1, end_of_name - 1);
86 bool IsCTCP() const CXX11_OVERRIDE
88 // According to draft-oakley-irc-ctcp-02 a valid CTCP must begin with SOH and
89 // contain at least one octet which is not NUL, SOH, CR, LF, or SPACE. As most
90 // of these are restricted at the protocol level we only need to check for SOH
92 return (text.length() >= 2) && (text[0] == '\x1') && (text[1] != '\x1') && (text[1] != ' ');
98 bool FirePreEvents(User* source, MessageTarget& msgtarget, MessageDetails& msgdetails)
100 // Inform modules that a message wants to be sent.
102 FIRST_MOD_RESULT(OnUserPreMessage, modres, (source, msgtarget, msgdetails));
103 if (modres == MOD_RES_DENY)
105 // Inform modules that a module blocked the mssage.
106 FOREACH_MOD(OnUserMessageBlocked, (source, msgtarget, msgdetails));
110 // Check whether a module zapped the message body.
111 if (msgdetails.text.empty())
113 source->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
117 // Inform modules that a message is about to be sent.
118 FOREACH_MOD(OnUserMessage, (source, msgtarget, msgdetails));
122 CmdResult FirePostEvent(User* source, const MessageTarget& msgtarget, const MessageDetails& msgdetails)
124 // If the source is local and was not sending a CTCP reply then update their idle time.
125 LocalUser* lsource = IS_LOCAL(source);
126 if (lsource && msgdetails.update_idle && (msgdetails.type != MSG_NOTICE || !msgdetails.IsCTCP()))
127 lsource->idle_lastmsg = ServerInstance->Time();
129 // Inform modules that a message was sent.
130 FOREACH_MOD(OnUserPostMessage, (source, msgtarget, msgdetails));
135 class CommandMessage : public Command
138 const MessageType msgtype;
140 CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
142 Channel* chan = ServerInstance->FindChan(target);
145 // The target channel does not exist.
146 source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
150 // Fire the pre-message events.
151 MessageTarget msgtarget(chan, pm ? pm->GetPrefix() : 0);
152 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
153 msgdetails.exemptions.insert(source);
154 if (!FirePreEvents(source, msgtarget, msgdetails))
157 // Send the message to the members of the channel.
158 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, chan, msgdetails.text, msgdetails.type, msgtarget.status);
159 privmsg.AddTags(msgdetails.tags_out);
160 privmsg.SetSideEffect(true);
161 chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
163 // Create the outgoing message and message event.
164 return FirePostEvent(source, msgtarget, msgdetails);
167 CmdResult HandleServerTarget(User* source, const Params& parameters)
169 // If the source isn't allowed to mass message users then reject
170 // the attempt to mass-message users.
171 if (!source->HasPrivPermission("users/mass-message"))
173 source->WriteNumeric(ERR_NOPRIVILEGES, "Permission Denied - You do not have the required operator privileges");
177 // Extract the server glob match from the target parameter.
178 std::string servername(parameters[0], 1);
180 // Fire the pre-message events.
181 MessageTarget msgtarget(&servername);
182 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
183 if (!FirePreEvents(source, msgtarget, msgdetails))
186 // If the current server name matches the server name glob then send
187 // the message out to the local users.
188 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername))
190 // Create the outgoing message and message event.
191 ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, source, "$*", msgdetails.text, msgdetails.type);
192 message.AddTags(msgdetails.tags_out);
193 message.SetSideEffect(true);
194 ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
196 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
197 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
199 LocalUser* luser = *i;
201 // Don't send to unregistered users or the user who is the source.
202 if (luser->registered != REG_ALL || luser == source)
205 // Only send to non-exempt users.
206 if (!msgdetails.exemptions.count(luser))
207 luser->Send(messageevent);
211 // Fire the post-message event.
212 return FirePostEvent(source, msgtarget, msgdetails);
215 CmdResult HandleUserTarget(User* source, const Params& parameters)
218 if (IS_LOCAL(source))
220 // Local sources can specify either a nick or a nick@server mask as the target.
221 const char* targetserver = strchr(parameters[0].c_str(), '@');
224 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
225 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
226 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
231 // If the source is a local user then we only look up the target by nick.
232 target = ServerInstance->FindNickOnly(parameters[0]);
237 // Remote users can only specify a nick or UUID as the target.
238 target = ServerInstance->FindNick(parameters[0]);
241 if (!target || target->registered != REG_ALL)
243 // The target user does not exist or is not fully registered.
244 source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
248 // Fire the pre-message events.
249 MessageTarget msgtarget(target);
250 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
251 if (!FirePreEvents(source, msgtarget, msgdetails))
254 // If the target is away then inform the user.
255 if (target->IsAway() && msgdetails.type == MSG_PRIVMSG)
256 source->WriteNumeric(RPL_AWAY, target->nick, target->awaymsg);
258 LocalUser* const localtarget = IS_LOCAL(target);
261 // Send to the target if they are a local user.
262 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, localtarget->nick, msgdetails.text, msgdetails.type);
263 privmsg.AddTags(msgdetails.tags_out);
264 privmsg.SetSideEffect(true);
265 localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
268 // Fire the post-message event.
269 return FirePostEvent(source, msgtarget, msgdetails);
273 CommandMessage(Module* parent, MessageType mt)
274 : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
277 syntax = "<target>[,<target>]+ :<message>";
281 * @param parameters The parameters to the command
282 * @param user The user issuing the command
283 * @return A value from CmdResult to indicate command success or failure.
285 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
287 if (CommandParser::LoopCall(user, this, parameters, 0))
290 // The specified message was empty.
291 if (parameters[1].empty())
293 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
297 // The target is a server glob.
298 if (parameters[0][0] == '$')
299 return HandleServerTarget(user, parameters);
301 // If the message begins with a status character then look it up.
302 const char* target = parameters[0].c_str();
303 PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
307 // The target is a channel name.
309 return HandleChannelTarget(user, parameters, target, pmh);
311 // The target is a nickname.
312 return HandleUserTarget(user, parameters);
315 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
318 // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
319 return ROUTE_LOCALONLY;
321 return ROUTE_MESSAGE(parameters[0]);
325 class CommandSQuery : public SplitCommand
328 CommandSQuery(Module* Creator)
329 : SplitCommand(Creator, "SQUERY", 2, 2)
331 syntax = "<service> :<message>";
334 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
336 // The specified message was empty.
337 if (parameters[1].empty())
339 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
343 // The target can be either a nick or a nick@server mask.
345 const char* targetserver = strchr(parameters[0].c_str(), '@');
348 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
349 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
350 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
355 // The targer can be on any server.
356 target = ServerInstance->FindNickOnly(parameters[0]);
359 if (!target || target->registered != REG_ALL || !target->server->IsULine())
361 // The target user does not exist, is not fully registered, or is not a service.
362 user->WriteNumeric(ERR_NOSUCHSERVICE, parameters[0], "No such service");
366 // Fire the pre-message events.
367 MessageTarget msgtarget(target);
368 MessageDetailsImpl msgdetails(MSG_PRIVMSG, parameters[1], parameters.GetTags());
369 if (!FirePreEvents(user, msgtarget, msgdetails))
372 // The SQUERY command targets a service on a U-lined server. This can never
373 // be on the server local to the source so we don't need to do any routing
374 // logic and can forward it as a PRIVMSG.
376 // Fire the post-message event.
377 return FirePostEvent(user, msgtarget, msgdetails);
381 class ModuleCoreMessage : public Module
384 CommandMessage cmdprivmsg;
385 CommandMessage cmdnotice;
386 CommandSQuery cmdsquery;
387 ChanModeReference moderatedmode;
388 ChanModeReference noextmsgmode;
392 : cmdprivmsg(this, MSG_PRIVMSG)
393 , cmdnotice(this, MSG_NOTICE)
395 , moderatedmode(this, "moderated")
396 , noextmsgmode(this, "noextmsg")
400 ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
402 if (!IS_LOCAL(user) || target.type != MessageTarget::TYPE_CHANNEL)
403 return MOD_RES_PASSTHRU;
405 Channel* chan = target.Get<Channel>();
406 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
408 // The noextmsg mode is set and the user is not in the channel.
409 user->WriteNumeric(Numerics::CannotSendTo(chan, "external messages", *noextmsgmode));
413 bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
414 if (no_chan_priv && chan->IsModeSet(moderatedmode))
416 // The moderated mode is set and the user has no status rank.
417 user->WriteNumeric(Numerics::CannotSendTo(chan, "messages", *moderatedmode));
421 if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
423 // The user is banned in the channel and restrictbannedusers is enabled.
424 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
425 user->WriteNumeric(Numerics::CannotSendTo(chan, "You cannot send messages to this channel whilst banned."));
429 return MOD_RES_PASSTHRU;
432 Version GetVersion() CXX11_OVERRIDE
434 return Version("Provides the NOTICE, PRIVMSG, and SQUERY commands", VF_CORE|VF_VENDOR);
438 MODULE_INIT(ModuleCoreMessage)