]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_message.cpp
Move message handling code to Handle{Channel,Server,User}Target.
[user/henk/code/inspircd.git] / src / coremods / core_message.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
7  *
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.
11  *
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
15  * details.
16  *
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/>.
19  */
20
21
22 #include "inspircd.h"
23
24 class MessageDetailsImpl : public MessageDetails
25 {
26 public:
27         MessageDetailsImpl(MessageType mt, const std::string& msg, const ClientProtocol::TagMap& tags)
28                 : MessageDetails(mt, msg, tags)
29         {
30         }
31
32         bool IsCTCP(std::string& name, std::string& body) const CXX11_OVERRIDE
33         {
34                 if (!this->IsCTCP())
35                         return false;
36
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)
40                 {
41                         // The CTCP only contains a name.
42                         name.assign(text, 1, text.length() - 1 - end_of_ctcp);
43                         body.clear();
44                         return true;
45                 }
46
47                 // The CTCP contains a name and a body.
48                 name.assign(text, 1, end_of_name - 1);
49
50                 size_t start_of_body = text.find_first_not_of(' ', end_of_name + 1);
51                 if (start_of_body == std::string::npos)
52                 {
53                         // The CTCP body is provided but empty.
54                         body.clear();
55                         return true;
56                 }
57
58                 // The CTCP body provided was non-empty.
59                 body.assign(text, start_of_body, text.length() - start_of_body - end_of_ctcp);
60                 return true;
61         }
62
63         bool IsCTCP(std::string& name) const CXX11_OVERRIDE
64         {
65                 if (!this->IsCTCP())
66                         return false;
67
68                 size_t end_of_name = text.find(' ', 2);
69                 if (end_of_name == std::string::npos)
70                 {
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);
74                         return true;
75                 }
76
77                 // The CTCP contains a name and a body.
78                 name.assign(text, 1, end_of_name - 1);
79                 return true;
80         }
81
82         bool IsCTCP() const CXX11_OVERRIDE
83         {
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
87                 // and SPACE.
88                 return (text.length() >= 2) && (text[0] == '\x1') &&  (text[1] != '\x1') && (text[1] != ' ');
89         }
90 };
91
92 namespace
93 {
94         bool FirePreEvents(User* source, MessageTarget& msgtarget, MessageDetails& msgdetails)
95         {
96                 // Inform modules that a message wants to be sent.
97                 ModResult modres;
98                 FIRST_MOD_RESULT(OnUserPreMessage, modres, (source, msgtarget, msgdetails));
99                 if (modres == MOD_RES_DENY)
100                 {
101                         // Inform modules that a module blocked the mssage.
102                         FOREACH_MOD(OnUserMessageBlocked, (source, msgtarget, msgdetails));
103                         return false;
104                 }
105
106                 // Check whether a module zapped the message body.
107                 if (msgdetails.text.empty())
108                 {
109                         source->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
110                         return false;
111                 }
112
113                 // Inform modules that a message is about to be sent.
114                 FOREACH_MOD(OnUserMessage, (source, msgtarget, msgdetails));
115                 return true;
116         }
117
118         CmdResult FirePostEvent(User* source, const MessageTarget& msgtarget, const MessageDetails& msgdetails)
119         {
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();
124
125                 // Inform modules that a message was sent.
126                 FOREACH_MOD(OnUserPostMessage, (source, msgtarget, msgdetails));
127                 return CMD_SUCCESS;
128         }
129 }
130
131 class CommandMessage : public Command
132 {
133  private:
134         const MessageType msgtype;
135         ChanModeReference moderatedmode;
136         ChanModeReference noextmsgmode;
137
138         CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
139         {
140                 Channel* chan = ServerInstance->FindChan(target);
141                 if (!chan)
142                 {
143                         // The target channel does not exist.
144                         source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
145                         return CMD_FAILURE;
146                 }
147
148                 if (IS_LOCAL(source))
149                 {
150                         if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(source))
151                         {
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)");
154                                 return CMD_FAILURE;
155                         }
156
157                         bool no_chan_priv = chan->GetPrefixValue(source) < VOICE_VALUE;
158                         if (no_chan_priv && chan->IsModeSet(moderatedmode))
159                         {
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)");
162                                 return CMD_FAILURE;
163                         }
164
165                         if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(source))
166                         {
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)");
170                                 return CMD_FAILURE;
171                         }
172                 }
173
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))
179                         return CMD_FAILURE;
180
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);
186
187                 // Create the outgoing message and message event.
188                 return FirePostEvent(source, msgtarget, msgdetails);
189         }
190
191         CmdResult HandleServerTarget(User* source, const Params& parameters)
192         {
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"))
196                         return CMD_FAILURE;
197
198                 // Extract the server glob match from the target parameter.
199                 std::string servername(parameters[0], 1);
200
201                 // Fire the pre-message events.
202                 MessageTarget msgtarget(&servername);
203                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
204                 if (!FirePreEvents(source, msgtarget, msgdetails))
205                         return CMD_FAILURE;
206
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))
210                 {
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);
216
217                         const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
218                         for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
219                         {
220                                 LocalUser* luser = *i;
221
222                                 // Don't send to unregistered users or the user who is the source.
223                                 if (luser->registered != REG_ALL || luser == source)
224                                         continue;
225
226                                 // Only send to non-exempt users.
227                                 if (!msgdetails.exemptions.count(luser))
228                                         luser->Send(messageevent);
229                         }
230                 }
231
232                 // Fire the post-message event.
233                 return FirePostEvent(source, msgtarget, msgdetails);
234         }
235
236         CmdResult HandleUserTarget(User* source, const Params& parameters)
237         {
238                 User* target;
239                 if (IS_LOCAL(source))
240                 {
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(), '@');
243                         if (targetserver)
244                         {
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))
248                                         target = NULL;
249                         }
250                         else
251                         {
252                                 // If the source is a local user then we only look up the target by nick.
253                                 target = ServerInstance->FindNickOnly(parameters[0]);
254                         }
255                 }
256                 else
257                 {
258                         // Remote users can only specify a nick or UUID as the target.
259                         target = ServerInstance->FindNick(parameters[0]);
260                 }
261
262                 if (!target || target->registered != REG_ALL)
263                 {
264                         // The target user does not exist or is not fully registered.
265                         source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
266                         return CMD_FAILURE;
267                 }
268
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);
272
273                 // Fire the pre-message events.
274                 MessageTarget msgtarget(target);
275                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
276                 if (!FirePreEvents(source, msgtarget, msgdetails))
277                         return CMD_FAILURE;
278
279                 LocalUser* const localtarget = IS_LOCAL(target);
280                 if (localtarget)
281                 {
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);
287                 }
288
289                 // Fire the post-message event.
290                 return FirePostEvent(source, msgtarget, msgdetails);
291         }
292
293  public:
294         CommandMessage(Module* parent, MessageType mt)
295                 : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
296                 , msgtype(mt)
297                 , moderatedmode(parent, "moderated")
298                 , noextmsgmode(parent, "noextmsg")
299         {
300                 syntax = "<target>{,<target>} <message>";
301         }
302
303         /** Handle command.
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.
307          */
308         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
309
310         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
311         {
312                 if (IS_LOCAL(user))
313                         // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
314                         return ROUTE_LOCALONLY;
315                 else
316                         return ROUTE_MESSAGE(parameters[0]);
317         }
318 };
319
320 CmdResult CommandMessage::Handle(User* user, const Params& parameters)
321 {
322         if (CommandParser::LoopCall(user, this, parameters, 0))
323                 return CMD_SUCCESS;
324
325         // The target is a server glob.
326         if (parameters[0][0] == '$')
327                 return HandleServerTarget(user, parameters);
328
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]);
332         if (pmh)
333                 target++;
334
335         // The target is a channel name.
336         if (*target == '#')
337                 return HandleChannelTarget(user, parameters, target, pmh);
338
339         // The target is a nickname.
340         return HandleUserTarget(user, parameters);
341 }
342
343 class ModuleCoreMessage : public Module
344 {
345  private:
346         CommandMessage cmdprivmsg;
347         CommandMessage cmdnotice;
348
349  public:
350         ModuleCoreMessage()
351                 : cmdprivmsg(this, MSG_PRIVMSG)
352                 , cmdnotice(this, MSG_NOTICE)
353         {
354         }
355
356         Version GetVersion() CXX11_OVERRIDE
357         {
358                 return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
359         }
360 };
361
362 MODULE_INIT(ModuleCoreMessage)