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] != ' ');
92 class MessageCommandBase : public Command
94 ChanModeReference moderatedmode;
95 ChanModeReference noextmsgmode;
97 /** Send a PRIVMSG or NOTICE message to all local users from the given user
98 * @param source The user sending the message.
99 * @param msg The details of the message to send.
101 static void SendAll(User* source, const MessageDetails& details)
103 ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, source, "$*", details.text, details.type);
104 message.AddTags(details.tags_out);
105 message.SetSideEffect(true);
106 ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
108 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
109 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
111 LocalUser* user = *i;
112 if ((user->registered == REG_ALL) && (!details.exemptions.count(user)))
113 user->Send(messageevent);
118 MessageCommandBase(Module* parent, MessageType mt)
119 : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
120 , moderatedmode(parent, "moderated")
121 , noextmsgmode(parent, "noextmsg")
123 syntax = "<target>{,<target>} <message>";
127 * @param parameters The parameters to the command
128 * @param user The user issuing the command
129 * @return A value from CmdResult to indicate command success or failure.
131 CmdResult HandleMessage(User* user, const Params& parameters, MessageType mt);
133 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
136 // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
137 return ROUTE_LOCALONLY;
139 return ROUTE_MESSAGE(parameters[0]);
143 CmdResult MessageCommandBase::HandleMessage(User* user, const Params& parameters, MessageType mt)
148 if (CommandParser::LoopCall(user, this, parameters, 0))
151 if (parameters[0][0] == '$')
153 if (!user->HasPrivPermission("users/mass-message"))
156 std::string servername(parameters[0], 1);
157 MessageTarget msgtarget(&servername);
158 MessageDetailsImpl msgdetails(mt, parameters[1], parameters.GetTags());
160 ModResult MOD_RESULT;
161 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
162 if (MOD_RESULT == MOD_RES_DENY)
164 FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
168 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
169 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername, NULL))
171 SendAll(user, msgdetails);
173 FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
178 const char* target = parameters[0].c_str();
180 if (ServerInstance->Modes->FindPrefix(*target))
187 chan = ServerInstance->FindChan(target);
191 if (IS_LOCAL(user) && chan->GetPrefixValue(user) < VOICE_VALUE)
193 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
195 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
199 if (chan->IsModeSet(moderatedmode))
201 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
205 if (ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL)
207 if (chan->IsBanned(user))
209 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
210 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
216 MessageTarget msgtarget(chan, status);
217 MessageDetailsImpl msgdetails(mt, parameters[1], parameters.GetTags());
218 msgdetails.exemptions.insert(user);
220 ModResult MOD_RESULT;
221 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
222 if (MOD_RESULT == MOD_RES_DENY)
224 FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
228 /* Check again, a module may have zapped the input string */
229 if (msgdetails.text.empty())
231 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
235 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
237 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, chan, msgdetails.text, msgdetails.type, msgtarget.status);
238 privmsg.AddTags(msgdetails.tags_out);
239 privmsg.SetSideEffect(true);
240 chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
242 FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
246 /* channel does not exist */
247 user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
253 const char* destnick = parameters[0].c_str();
257 const char* targetserver = strchr(destnick, '@');
261 std::string nickonly;
263 nickonly.assign(destnick, 0, targetserver - destnick);
264 dest = ServerInstance->FindNickOnly(nickonly);
265 if (dest && strcasecmp(dest->server->GetName().c_str(), targetserver + 1))
267 /* Incorrect server for user */
268 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
273 dest = ServerInstance->FindNickOnly(destnick);
276 dest = ServerInstance->FindNick(destnick);
278 if ((dest) && (dest->registered == REG_ALL))
280 if (parameters[1].empty())
282 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
286 if ((dest->IsAway()) && (mt == MSG_PRIVMSG))
288 /* auto respond with aweh msg */
289 user->WriteNumeric(RPL_AWAY, dest->nick, dest->awaymsg);
292 MessageTarget msgtarget(dest);
293 MessageDetailsImpl msgdetails(mt, parameters[1], parameters.GetTags());
296 ModResult MOD_RESULT;
297 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
298 if (MOD_RESULT == MOD_RES_DENY)
300 FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
304 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
306 LocalUser* const localtarget = IS_LOCAL(dest);
309 // direct write, same server
310 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, localtarget->nick, msgdetails.text, mt);
311 privmsg.AddTags(msgdetails.tags_out);
312 privmsg.SetSideEffect(true);
313 localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
316 FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
320 /* no such nick/channel */
321 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
327 template<MessageType MT>
328 class CommandMessage : public MessageCommandBase
331 CommandMessage(Module* parent)
332 : MessageCommandBase(parent, MT)
336 CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
338 return HandleMessage(user, parameters, MT);
342 class ModuleCoreMessage : public Module
344 CommandMessage<MSG_PRIVMSG> CommandPrivmsg;
345 CommandMessage<MSG_NOTICE> CommandNotice;
349 : CommandPrivmsg(this), CommandNotice(this)
353 void OnUserPostMessage(User* user, const MessageTarget& target, const MessageDetails& details) CXX11_OVERRIDE
355 // We only handle the idle times of local users.
356 LocalUser* luser = IS_LOCAL(user);
360 // We don't update the idle time when a CTCP reply is sent.
361 if (details.type == MSG_NOTICE && details.IsCTCP())
364 luser->idle_lastmsg = ServerInstance->Time();
367 Version GetVersion() CXX11_OVERRIDE
369 return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
373 MODULE_INIT(ModuleCoreMessage)