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/>.
24 class MessageDetailsImpl : public MessageDetails
27 MessageDetailsImpl(MessageType mt, const std::string& msg, const ClientProtocol::TagMap& tags)
28 : MessageDetails(mt, msg, tags)
32 bool IsCTCP(std::string& name, std::string& body) const CXX11_OVERRIDE
37 size_t end_of_name = text.find(' ', 2);
38 size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
39 if (end_of_name == std::string::npos)
41 // The CTCP only contains a name.
42 name.assign(text, 1, text.length() - 1 - end_of_ctcp);
47 // The CTCP contains a name and a body.
48 name.assign(text, 1, end_of_name - 1);
50 size_t start_of_body = text.find_first_not_of(' ', end_of_name + 1);
51 if (start_of_body == std::string::npos)
53 // The CTCP body is provided but empty.
58 // The CTCP body provided was non-empty.
59 body.assign(text, start_of_body, text.length() - start_of_body - end_of_ctcp);
63 bool IsCTCP(std::string& name) const CXX11_OVERRIDE
68 size_t end_of_name = text.find(' ', 2);
69 if (end_of_name == std::string::npos)
71 // The CTCP only contains a name.
72 size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
73 name.assign(text, 1, text.length() - 1 - end_of_ctcp);
77 // The CTCP contains a name and a body.
78 name.assign(text, 1, end_of_name - 1);
82 bool IsCTCP() const CXX11_OVERRIDE
84 // According to draft-oakley-irc-ctcp-02 a valid CTCP must begin with SOH and
85 // contain at least one octet which is not NUL, SOH, CR, LF, or SPACE. As most
86 // of these are restricted at the protocol level we only need to check for SOH
88 return (text.length() >= 2) && (text[0] == '\x1') && (text[1] != '\x1') && (text[1] != ' ');
94 bool FirePreEvents(User* source, MessageTarget& msgtarget, MessageDetails& msgdetails)
96 // Inform modules that a message wants to be sent.
98 FIRST_MOD_RESULT(OnUserPreMessage, modres, (source, msgtarget, msgdetails));
99 if (modres == MOD_RES_DENY)
101 // Inform modules that a module blocked the mssage.
102 FOREACH_MOD(OnUserMessageBlocked, (source, msgtarget, msgdetails));
106 // Check whether a module zapped the message body.
107 if (msgdetails.text.empty())
109 source->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
113 // Inform modules that a message is about to be sent.
114 FOREACH_MOD(OnUserMessage, (source, msgtarget, msgdetails));
118 CmdResult FirePostEvent(User* source, const MessageTarget& msgtarget, const MessageDetails& msgdetails)
120 // If the source is local and was not sending a CTCP reply then update their idle time.
121 LocalUser* lsource = IS_LOCAL(source);
122 if (lsource && (msgdetails.type != MSG_NOTICE || !msgdetails.IsCTCP()))
123 lsource->idle_lastmsg = ServerInstance->Time();
125 // Inform modules that a message was sent.
126 FOREACH_MOD(OnUserPostMessage, (source, msgtarget, msgdetails));
131 class CommandMessage : public Command
134 const MessageType msgtype;
135 ChanModeReference moderatedmode;
136 ChanModeReference noextmsgmode;
138 CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
140 Channel* chan = ServerInstance->FindChan(target);
143 // The target channel does not exist.
144 source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
148 if (IS_LOCAL(source))
150 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(source))
152 // The noextmsg mode is set and the source is not in the channel.
153 source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
157 bool no_chan_priv = chan->GetPrefixValue(source) < VOICE_VALUE;
158 if (no_chan_priv && chan->IsModeSet(moderatedmode))
160 // The moderated mode is set and the source has no status rank.
161 source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
165 if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(source))
167 // The source is banned in the channel and restrictbannedusers is enabled.
168 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
169 source->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
174 // Fire the pre-message events.
175 MessageTarget msgtarget(chan, pm ? pm->GetPrefix() : 0);
176 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
177 msgdetails.exemptions.insert(source);
178 if (!FirePreEvents(source, msgtarget, msgdetails))
181 // Send the message to the members of the channel.
182 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, chan, msgdetails.text, msgdetails.type, msgtarget.status);
183 privmsg.AddTags(msgdetails.tags_out);
184 privmsg.SetSideEffect(true);
185 chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
187 // Create the outgoing message and message event.
188 return FirePostEvent(source, msgtarget, msgdetails);
191 CmdResult HandleServerTarget(User* source, const Params& parameters)
193 // If the source isn't allowed to mass message users then reject
194 // the attempt to mass-message users.
195 if (!source->HasPrivPermission("users/mass-message"))
198 // Extract the server glob match from the target parameter.
199 std::string servername(parameters[0], 1);
201 // Fire the pre-message events.
202 MessageTarget msgtarget(&servername);
203 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
204 if (!FirePreEvents(source, msgtarget, msgdetails))
207 // If the current server name matches the server name glob then send
208 // the message out to the local users.
209 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername))
211 // Create the outgoing message and message event.
212 ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, source, "$*", msgdetails.text, msgdetails.type);
213 message.AddTags(msgdetails.tags_out);
214 message.SetSideEffect(true);
215 ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
217 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
218 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
220 LocalUser* luser = *i;
222 // Don't send to unregistered users or the user who is the source.
223 if (luser->registered != REG_ALL || luser == source)
226 // Only send to non-exempt users.
227 if (!msgdetails.exemptions.count(luser))
228 luser->Send(messageevent);
232 // Fire the post-message event.
233 return FirePostEvent(source, msgtarget, msgdetails);
236 CmdResult HandleUserTarget(User* source, const Params& parameters)
239 if (IS_LOCAL(source))
241 // Local sources can specify either a nick or a nick@server mask as the target.
242 const char* targetserver = strchr(parameters[0].c_str(), '@');
245 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
246 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
247 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
252 // If the source is a local user then we only look up the target by nick.
253 target = ServerInstance->FindNickOnly(parameters[0]);
258 // Remote users can only specify a nick or UUID as the target.
259 target = ServerInstance->FindNick(parameters[0]);
262 if (!target || target->registered != REG_ALL)
264 // The target user does not exist or is not fully registered.
265 source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
269 // If the target is away then inform the user.
270 if (target->IsAway() && msgtype == MSG_PRIVMSG)
271 source->WriteNumeric(RPL_AWAY, target->nick, target->awaymsg);
273 // Fire the pre-message events.
274 MessageTarget msgtarget(target);
275 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
276 if (!FirePreEvents(source, msgtarget, msgdetails))
279 LocalUser* const localtarget = IS_LOCAL(target);
282 // Send to the target if they are a local user.
283 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, localtarget->nick, msgdetails.text, msgtype);
284 privmsg.AddTags(msgdetails.tags_out);
285 privmsg.SetSideEffect(true);
286 localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
289 // Fire the post-message event.
290 return FirePostEvent(source, msgtarget, msgdetails);
294 CommandMessage(Module* parent, MessageType mt)
295 : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
297 , moderatedmode(parent, "moderated")
298 , noextmsgmode(parent, "noextmsg")
300 syntax = "<target>{,<target>} <message>";
304 * @param parameters The parameters to the command
305 * @param user The user issuing the command
306 * @return A value from CmdResult to indicate command success or failure.
308 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
310 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
313 // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
314 return ROUTE_LOCALONLY;
316 return ROUTE_MESSAGE(parameters[0]);
320 CmdResult CommandMessage::Handle(User* user, const Params& parameters)
322 if (CommandParser::LoopCall(user, this, parameters, 0))
325 // The target is a server glob.
326 if (parameters[0][0] == '$')
327 return HandleServerTarget(user, parameters);
329 // If the message begins with a status character then look it up.
330 const char* target = parameters[0].c_str();
331 PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
335 // The target is a channel name.
337 return HandleChannelTarget(user, parameters, target, pmh);
339 // The target is a nickname.
340 return HandleUserTarget(user, parameters);
343 class ModuleCoreMessage : public Module
346 CommandMessage cmdprivmsg;
347 CommandMessage cmdnotice;
351 : cmdprivmsg(this, MSG_PRIVMSG)
352 , cmdnotice(this, MSG_NOTICE)
356 Version GetVersion() CXX11_OVERRIDE
358 return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
362 MODULE_INIT(ModuleCoreMessage)