]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_message.cpp
Deduplicate firing module events in core_message.
[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         /** Send a PRIVMSG or NOTICE message to all local users from the given user
139          * @param source The user sending the message.
140          * @param msg The details of the message to send.
141          */
142         static void SendAll(User* source, const MessageDetails& details)
143         {
144                 ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, source, "$*", details.text, details.type);
145                 message.AddTags(details.tags_out);
146                 message.SetSideEffect(true);
147                 ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
148
149                 const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
150                 for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
151                 {
152                         LocalUser* user = *i;
153                         if ((user->registered == REG_ALL) && (!details.exemptions.count(user)))
154                                 user->Send(messageevent);
155                 }
156         }
157
158  public:
159         CommandMessage(Module* parent, MessageType mt)
160                 : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
161                 , msgtype(mt)
162                 , moderatedmode(parent, "moderated")
163                 , noextmsgmode(parent, "noextmsg")
164         {
165                 syntax = "<target>{,<target>} <message>";
166         }
167
168         /** Handle command.
169          * @param parameters The parameters to the command
170          * @param user The user issuing the command
171          * @return A value from CmdResult to indicate command success or failure.
172          */
173         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
174
175         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
176         {
177                 if (IS_LOCAL(user))
178                         // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
179                         return ROUTE_LOCALONLY;
180                 else
181                         return ROUTE_MESSAGE(parameters[0]);
182         }
183 };
184
185 CmdResult CommandMessage::Handle(User* user, const Params& parameters)
186 {
187         User *dest;
188         Channel *chan;
189
190         if (CommandParser::LoopCall(user, this, parameters, 0))
191                 return CMD_SUCCESS;
192
193         if (parameters[0][0] == '$')
194         {
195                 if (!user->HasPrivPermission("users/mass-message"))
196                         return CMD_SUCCESS;
197
198                 std::string servername(parameters[0], 1);
199                 MessageTarget msgtarget(&servername);
200                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
201                 if (!FirePreEvents(user, msgtarget, msgdetails))
202                         return CMD_FAILURE;
203
204                 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername, NULL))
205                 {
206                         SendAll(user, msgdetails);
207                 }
208                 return FirePostEvent(user, msgtarget, msgdetails);
209         }
210
211         char status = 0;
212         const char* target = parameters[0].c_str();
213
214         if (ServerInstance->Modes->FindPrefix(*target))
215         {
216                 status = *target;
217                 target++;
218         }
219         if (*target == '#')
220         {
221                 chan = ServerInstance->FindChan(target);
222
223                 if (chan)
224                 {
225                         if (IS_LOCAL(user) && chan->GetPrefixValue(user) < VOICE_VALUE)
226                         {
227                                 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
228                                 {
229                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
230                                         return CMD_FAILURE;
231                                 }
232
233                                 if (chan->IsModeSet(moderatedmode))
234                                 {
235                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
236                                         return CMD_FAILURE;
237                                 }
238
239                                 if (ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL)
240                                 {
241                                         if (chan->IsBanned(user))
242                                         {
243                                                 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
244                                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
245                                                 return CMD_FAILURE;
246                                         }
247                                 }
248                         }
249
250                         MessageTarget msgtarget(chan, status);
251                         MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
252                         msgdetails.exemptions.insert(user);
253                         if (!FirePreEvents(user, msgtarget, msgdetails))
254                                 return CMD_FAILURE;
255
256                         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, chan, msgdetails.text, msgdetails.type, msgtarget.status);
257                         privmsg.AddTags(msgdetails.tags_out);
258                         privmsg.SetSideEffect(true);
259                         chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
260                         return FirePostEvent(user, msgtarget, msgdetails);
261                 }
262                 else
263                 {
264                         /* channel does not exist */
265                         user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
266                         return CMD_FAILURE;
267                 }
268         }
269
270         const char* destnick = parameters[0].c_str();
271
272         if (IS_LOCAL(user))
273         {
274                 const char* targetserver = strchr(destnick, '@');
275
276                 if (targetserver)
277                 {
278                         std::string nickonly;
279
280                         nickonly.assign(destnick, 0, targetserver - destnick);
281                         dest = ServerInstance->FindNickOnly(nickonly);
282                         if (dest && strcasecmp(dest->server->GetName().c_str(), targetserver + 1))
283                         {
284                                 /* Incorrect server for user */
285                                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
286                                 return CMD_FAILURE;
287                         }
288                 }
289                 else
290                         dest = ServerInstance->FindNickOnly(destnick);
291         }
292         else
293                 dest = ServerInstance->FindNick(destnick);
294
295         if ((dest) && (dest->registered == REG_ALL))
296         {
297                 if (parameters[1].empty())
298                 {
299                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
300                         return CMD_FAILURE;
301                 }
302
303                 if ((dest->IsAway()) && (msgtype == MSG_PRIVMSG))
304                 {
305                         /* auto respond with aweh msg */
306                         user->WriteNumeric(RPL_AWAY, dest->nick, dest->awaymsg);
307                 }
308
309                 MessageTarget msgtarget(dest);
310                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
311                 if (!FirePreEvents(user, msgtarget, msgdetails))
312                         return CMD_FAILURE;
313
314                 LocalUser* const localtarget = IS_LOCAL(dest);
315                 if (localtarget)
316                 {
317                         // direct write, same server
318                         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, localtarget->nick, msgdetails.text, msgtype);
319                         privmsg.AddTags(msgdetails.tags_out);
320                         privmsg.SetSideEffect(true);
321                         localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
322                 }
323                 return FirePostEvent(user, msgtarget, msgdetails);
324         }
325         else
326         {
327                 /* no such nick/channel */
328                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
329                 return CMD_FAILURE;
330         }
331 }
332
333 class ModuleCoreMessage : public Module
334 {
335  private:
336         CommandMessage cmdprivmsg;
337         CommandMessage cmdnotice;
338
339  public:
340         ModuleCoreMessage()
341                 : cmdprivmsg(this, MSG_PRIVMSG)
342                 , cmdnotice(this, MSG_NOTICE)
343         {
344         }
345
346         Version GetVersion() CXX11_OVERRIDE
347         {
348                 return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
349         }
350 };
351
352 MODULE_INIT(ModuleCoreMessage)