]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_privmsg.cpp
Implement proper CTCP parsing in MessageDetails.
[user/henk/code/inspircd.git] / src / coremods / core_privmsg.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 class MessageCommandBase : public Command
93 {
94         ChanModeReference moderatedmode;
95         ChanModeReference noextmsgmode;
96
97         /** Send a PRIVMSG or NOTICE message to all local users from the given user
98          * @param user User sending the message
99          * @param msg The message to send
100          * @param mt Type of the message (MSG_PRIVMSG or MSG_NOTICE)
101          * @param tags Message tags to include in the outgoing protocol message
102          */
103         static void SendAll(User* user, const std::string& msg, MessageType mt, const ClientProtocol::TagMap& tags);
104
105  public:
106         MessageCommandBase(Module* parent, MessageType mt)
107                 : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
108                 , moderatedmode(parent, "moderated")
109                 , noextmsgmode(parent, "noextmsg")
110         {
111                 syntax = "<target>{,<target>} <message>";
112         }
113
114         /** Handle command.
115          * @param parameters The parameters to the command
116          * @param user The user issuing the command
117          * @return A value from CmdResult to indicate command success or failure.
118          */
119         CmdResult HandleMessage(User* user, const Params& parameters, MessageType mt);
120
121         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
122         {
123                 if (IS_LOCAL(user))
124                         // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
125                         return ROUTE_LOCALONLY;
126                 else
127                         return ROUTE_MESSAGE(parameters[0]);
128         }
129 };
130
131 void MessageCommandBase::SendAll(User* user, const std::string& msg, MessageType mt, const ClientProtocol::TagMap& tags)
132 {
133         ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, user, "$*", msg, mt);
134         message.AddTags(tags);
135         message.SetSideEffect(true);
136         ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
137
138         const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
139         for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
140         {
141                 if ((*i)->registered == REG_ALL)
142                         (*i)->Send(messageevent);
143         }
144 }
145
146 CmdResult MessageCommandBase::HandleMessage(User* user, const Params& parameters, MessageType mt)
147 {
148         User *dest;
149         Channel *chan;
150
151         LocalUser* localuser = IS_LOCAL(user);
152         if (localuser)
153                 localuser->idle_lastmsg = ServerInstance->Time();
154
155         if (CommandParser::LoopCall(user, this, parameters, 0))
156                 return CMD_SUCCESS;
157
158         if (parameters[0][0] == '$')
159         {
160                 if (!user->HasPrivPermission("users/mass-message"))
161                         return CMD_SUCCESS;
162
163                 std::string servername(parameters[0], 1);
164                 MessageTarget msgtarget(&servername);
165                 MessageDetailsImpl msgdetails(mt, parameters[1], parameters.GetTags());
166
167                 ModResult MOD_RESULT;
168                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
169                 if (MOD_RESULT == MOD_RES_DENY)
170                 {
171                         FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
172                         return CMD_FAILURE;
173                 }
174
175                 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
176                 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername, NULL))
177                 {
178                         SendAll(user, msgdetails.text, mt, msgdetails.tags_out);
179                 }
180                 FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
181                 return CMD_SUCCESS;
182         }
183
184         char status = 0;
185         const char* target = parameters[0].c_str();
186
187         if (ServerInstance->Modes->FindPrefix(*target))
188         {
189                 status = *target;
190                 target++;
191         }
192         if (*target == '#')
193         {
194                 chan = ServerInstance->FindChan(target);
195
196                 if (chan)
197                 {
198                         if (localuser && chan->GetPrefixValue(user) < VOICE_VALUE)
199                         {
200                                 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
201                                 {
202                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
203                                         return CMD_FAILURE;
204                                 }
205
206                                 if (chan->IsModeSet(moderatedmode))
207                                 {
208                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
209                                         return CMD_FAILURE;
210                                 }
211
212                                 if (ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL)
213                                 {
214                                         if (chan->IsBanned(user))
215                                         {
216                                                 if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
217                                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
218                                                 return CMD_FAILURE;
219                                         }
220                                 }
221                         }
222
223                         MessageTarget msgtarget(chan, status);
224                         MessageDetailsImpl msgdetails(mt, parameters[1], parameters.GetTags());
225                         msgdetails.exemptions.insert(user);
226
227                         ModResult MOD_RESULT;
228                         FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
229                         if (MOD_RESULT == MOD_RES_DENY)
230                         {
231                                 FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
232                                 return CMD_FAILURE;
233                         }
234
235                         /* Check again, a module may have zapped the input string */
236                         if (msgdetails.text.empty())
237                         {
238                                 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
239                                 return CMD_FAILURE;
240                         }
241
242                         FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
243
244                         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, chan, msgdetails.text, msgdetails.type, msgtarget.status);
245                         privmsg.AddTags(msgdetails.tags_out);
246                         privmsg.SetSideEffect(true);
247                         chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
248
249                         FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
250                 }
251                 else
252                 {
253                         /* channel does not exist */
254                         user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
255                         return CMD_FAILURE;
256                 }
257                 return CMD_SUCCESS;
258         }
259
260         const char* destnick = parameters[0].c_str();
261
262         if (localuser)
263         {
264                 const char* targetserver = strchr(destnick, '@');
265
266                 if (targetserver)
267                 {
268                         std::string nickonly;
269
270                         nickonly.assign(destnick, 0, targetserver - destnick);
271                         dest = ServerInstance->FindNickOnly(nickonly);
272                         if (dest && strcasecmp(dest->server->GetName().c_str(), targetserver + 1))
273                         {
274                                 /* Incorrect server for user */
275                                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
276                                 return CMD_FAILURE;
277                         }
278                 }
279                 else
280                         dest = ServerInstance->FindNickOnly(destnick);
281         }
282         else
283                 dest = ServerInstance->FindNick(destnick);
284
285         if ((dest) && (dest->registered == REG_ALL))
286         {
287                 if (parameters[1].empty())
288                 {
289                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
290                         return CMD_FAILURE;
291                 }
292
293                 if ((dest->IsAway()) && (mt == MSG_PRIVMSG))
294                 {
295                         /* auto respond with aweh msg */
296                         user->WriteNumeric(RPL_AWAY, dest->nick, dest->awaymsg);
297                 }
298
299                 MessageTarget msgtarget(dest);
300                 MessageDetailsImpl msgdetails(mt, parameters[1], parameters.GetTags());
301
302
303                 ModResult MOD_RESULT;
304                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
305                 if (MOD_RESULT == MOD_RES_DENY)
306                 {
307                         FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
308                         return CMD_FAILURE;
309                 }
310
311                 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
312
313                 LocalUser* const localtarget = IS_LOCAL(dest);
314                 if (localtarget)
315                 {
316                         // direct write, same server
317                         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, user, localtarget->nick, msgdetails.text, mt);
318                         privmsg.AddTags(msgdetails.tags_out);
319                         privmsg.SetSideEffect(true);
320                         localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
321                 }
322
323                 FOREACH_MOD(OnUserPostMessage, (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         return CMD_SUCCESS;
332 }
333
334 template<MessageType MT>
335 class CommandMessage : public MessageCommandBase
336 {
337  public:
338         CommandMessage(Module* parent)
339                 : MessageCommandBase(parent, MT)
340         {
341         }
342
343         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
344         {
345                 return HandleMessage(user, parameters, MT);
346         }
347 };
348
349 class ModuleCoreMessage : public Module
350 {
351         CommandMessage<MSG_PRIVMSG> CommandPrivmsg;
352         CommandMessage<MSG_NOTICE> CommandNotice;
353
354  public:
355         ModuleCoreMessage()
356                 : CommandPrivmsg(this), CommandNotice(this)
357         {
358         }
359
360         Version GetVersion() CXX11_OVERRIDE
361         {
362                 return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
363         }
364 };
365
366 MODULE_INIT(ModuleCoreMessage)