2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2007-2008 Craig Edwards <craigedwards@brainbox.cc>
6 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 ERR_NOSUCHSERVICE = 408
30 class MessageDetailsImpl : public MessageDetails
33 MessageDetailsImpl(MessageType mt, const std::string& msg, const ClientProtocol::TagMap& tags)
34 : MessageDetails(mt, msg, tags)
38 bool IsCTCP(std::string& name, std::string& body) const CXX11_OVERRIDE
43 size_t end_of_name = text.find(' ', 2);
44 size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
45 if (end_of_name == std::string::npos)
47 // The CTCP only contains a name.
48 name.assign(text, 1, text.length() - 1 - end_of_ctcp);
53 // The CTCP contains a name and a body.
54 name.assign(text, 1, end_of_name - 1);
56 size_t start_of_body = text.find_first_not_of(' ', end_of_name + 1);
57 if (start_of_body == std::string::npos)
59 // The CTCP body is provided but empty.
64 // The CTCP body provided was non-empty.
65 body.assign(text, start_of_body, text.length() - start_of_body - end_of_ctcp);
69 bool IsCTCP(std::string& name) const CXX11_OVERRIDE
74 size_t end_of_name = text.find(' ', 2);
75 if (end_of_name == std::string::npos)
77 // The CTCP only contains a name.
78 size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
79 name.assign(text, 1, text.length() - 1 - end_of_ctcp);
83 // The CTCP contains a name and a body.
84 name.assign(text, 1, end_of_name - 1);
88 bool IsCTCP() const CXX11_OVERRIDE
90 // According to draft-oakley-irc-ctcp-02 a valid CTCP must begin with SOH and
91 // contain at least one octet which is not NUL, SOH, CR, LF, or SPACE. As most
92 // of these are restricted at the protocol level we only need to check for SOH
94 return (text.length() >= 2) && (text[0] == '\x1') && (text[1] != '\x1') && (text[1] != ' ');
100 bool FirePreEvents(User* source, MessageTarget& msgtarget, MessageDetails& msgdetails)
102 // Inform modules that a message wants to be sent.
104 FIRST_MOD_RESULT(OnUserPreMessage, modres, (source, msgtarget, msgdetails));
105 if (modres == MOD_RES_DENY)
107 // Inform modules that a module blocked the mssage.
108 FOREACH_MOD(OnUserMessageBlocked, (source, msgtarget, msgdetails));
112 // Check whether a module zapped the message body.
113 if (msgdetails.text.empty())
115 source->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
119 // Inform modules that a message is about to be sent.
120 FOREACH_MOD(OnUserMessage, (source, msgtarget, msgdetails));
124 CmdResult FirePostEvent(User* source, const MessageTarget& msgtarget, const MessageDetails& msgdetails)
126 // If the source is local and was not sending a CTCP reply then update their idle time.
127 LocalUser* lsource = IS_LOCAL(source);
128 if (lsource && (msgdetails.type != MSG_NOTICE || !msgdetails.IsCTCP()))
129 lsource->idle_lastmsg = ServerInstance->Time();
131 // Inform modules that a message was sent.
132 FOREACH_MOD(OnUserPostMessage, (source, msgtarget, msgdetails));
137 class CommandMessage : public Command
140 const MessageType msgtype;
141 ChanModeReference moderatedmode;
142 ChanModeReference noextmsgmode;
144 CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
146 Channel* chan = ServerInstance->FindChan(target);
149 // The target channel does not exist.
150 source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
154 if (IS_LOCAL(source))
156 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(source))
158 // The noextmsg mode is set and the source is not in the channel.
159 source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
163 bool no_chan_priv = chan->GetPrefixValue(source) < VOICE_VALUE;
164 if (no_chan_priv && chan->IsModeSet(moderatedmode))
166 // The moderated mode is set and the source has no status rank.
167 source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m is set)");
171 if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(source))
173 // The source is banned in the channel and restrictbannedusers is enabled.
174 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
175 source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
180 // Fire the pre-message events.
181 MessageTarget msgtarget(chan, pm ? pm->GetPrefix() : 0);
182 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
183 msgdetails.exemptions.insert(source);
184 if (!FirePreEvents(source, msgtarget, msgdetails))
187 // Send the message to the members of the channel.
188 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, chan, msgdetails.text, msgdetails.type, msgtarget.status);
189 privmsg.AddTags(msgdetails.tags_out);
190 privmsg.SetSideEffect(true);
191 chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
193 // Create the outgoing message and message event.
194 return FirePostEvent(source, msgtarget, msgdetails);
197 CmdResult HandleServerTarget(User* source, const Params& parameters)
199 // If the source isn't allowed to mass message users then reject
200 // the attempt to mass-message users.
201 if (!source->HasPrivPermission("users/mass-message"))
204 // Extract the server glob match from the target parameter.
205 std::string servername(parameters[0], 1);
207 // Fire the pre-message events.
208 MessageTarget msgtarget(&servername);
209 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
210 if (!FirePreEvents(source, msgtarget, msgdetails))
213 // If the current server name matches the server name glob then send
214 // the message out to the local users.
215 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername))
217 // Create the outgoing message and message event.
218 ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, source, "$*", msgdetails.text, msgdetails.type);
219 message.AddTags(msgdetails.tags_out);
220 message.SetSideEffect(true);
221 ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
223 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
224 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
226 LocalUser* luser = *i;
228 // Don't send to unregistered users or the user who is the source.
229 if (luser->registered != REG_ALL || luser == source)
232 // Only send to non-exempt users.
233 if (!msgdetails.exemptions.count(luser))
234 luser->Send(messageevent);
238 // Fire the post-message event.
239 return FirePostEvent(source, msgtarget, msgdetails);
242 CmdResult HandleUserTarget(User* source, const Params& parameters)
245 if (IS_LOCAL(source))
247 // Local sources can specify either a nick or a nick@server mask as the target.
248 const char* targetserver = strchr(parameters[0].c_str(), '@');
251 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
252 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
253 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
258 // If the source is a local user then we only look up the target by nick.
259 target = ServerInstance->FindNickOnly(parameters[0]);
264 // Remote users can only specify a nick or UUID as the target.
265 target = ServerInstance->FindNick(parameters[0]);
268 if (!target || target->registered != REG_ALL)
270 // The target user does not exist or is not fully registered.
271 source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
275 // If the target is away then inform the user.
276 if (target->IsAway() && msgtype == MSG_PRIVMSG)
277 source->WriteNumeric(RPL_AWAY, target->nick, target->awaymsg);
279 // Fire the pre-message events.
280 MessageTarget msgtarget(target);
281 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
282 if (!FirePreEvents(source, msgtarget, msgdetails))
285 LocalUser* const localtarget = IS_LOCAL(target);
288 // Send to the target if they are a local user.
289 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, localtarget->nick, msgdetails.text, msgtype);
290 privmsg.AddTags(msgdetails.tags_out);
291 privmsg.SetSideEffect(true);
292 localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
295 // Fire the post-message event.
296 return FirePostEvent(source, msgtarget, msgdetails);
300 CommandMessage(Module* parent, MessageType mt)
301 : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
303 , moderatedmode(parent, "moderated")
304 , noextmsgmode(parent, "noextmsg")
306 syntax = "<target>[,<target>]+ :<message>";
310 * @param parameters The parameters to the command
311 * @param user The user issuing the command
312 * @return A value from CmdResult to indicate command success or failure.
314 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
316 if (CommandParser::LoopCall(user, this, parameters, 0))
319 // The specified message was empty.
320 if (parameters[1].empty())
322 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
326 // The target is a server glob.
327 if (parameters[0][0] == '$')
328 return HandleServerTarget(user, parameters);
330 // If the message begins with a status character then look it up.
331 const char* target = parameters[0].c_str();
332 PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
336 // The target is a channel name.
338 return HandleChannelTarget(user, parameters, target, pmh);
340 // The target is a nickname.
341 return HandleUserTarget(user, parameters);
344 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
347 // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
348 return ROUTE_LOCALONLY;
350 return ROUTE_MESSAGE(parameters[0]);
354 class CommandSQuery : public SplitCommand
357 CommandSQuery(Module* Creator)
358 : SplitCommand(Creator, "SQUERY", 2, 2)
360 syntax = "<service> :<message>";
363 CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
365 // The specified message was empty.
366 if (parameters[1].empty())
368 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
372 // The target can be either a nick or a nick@server mask.
374 const char* targetserver = strchr(parameters[0].c_str(), '@');
377 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
378 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
379 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
384 // The targer can be on any server.
385 target = ServerInstance->FindNickOnly(parameters[0]);
388 if (!target || target->registered != REG_ALL || !target->server->IsULine())
390 // The target user does not exist, is not fully registered, or is not a service.
391 user->WriteNumeric(ERR_NOSUCHSERVICE, parameters[0], "No such service");
395 // Fire the pre-message events.
396 MessageTarget msgtarget(target);
397 MessageDetailsImpl msgdetails(MSG_PRIVMSG, parameters[1], parameters.GetTags());
398 if (!FirePreEvents(user, msgtarget, msgdetails))
401 // The SQUERY command targets a service on a U-lined server. This can never
402 // be on the server local to the source so we don't need to do any routing
403 // logic and can forward it as a PRIVMSG.
405 // Fire the post-message event.
406 return FirePostEvent(user, msgtarget, msgdetails);
410 class ModuleCoreMessage : public Module
413 CommandMessage cmdprivmsg;
414 CommandMessage cmdnotice;
415 CommandSQuery cmdsquery;
419 : cmdprivmsg(this, MSG_PRIVMSG)
420 , cmdnotice(this, MSG_NOTICE)
425 Version GetVersion() CXX11_OVERRIDE
427 return Version("Provides the NOTICE, PRIVMSG, and SQUERY commands", VF_CORE|VF_VENDOR);
431 MODULE_INIT(ModuleCoreMessage)