]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_message.cpp
Don't kill cloaking users when hash/md5 is missing.
[user/henk/code/inspircd.git] / src / coremods / core_message.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017-2020 Sadie Powell <sadie@witchery.services>
5  *   Copyright (C) 2013, 2017-2018 Attila Molnar <attilamolnar@hush.com>
6  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
7  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
8  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
9  *   Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
10  *
11  * This file is part of InspIRCd.  InspIRCd is free software: you can
12  * redistribute it and/or modify it under the terms of the GNU General Public
13  * License as published by the Free Software Foundation, version 2.
14  *
15  * This program is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23
24
25 #include "inspircd.h"
26
27 enum
28 {
29         // From RFC 2812.
30         ERR_NOSUCHSERVICE = 408
31 };
32
33 class MessageDetailsImpl : public MessageDetails
34 {
35 public:
36         MessageDetailsImpl(MessageType mt, const std::string& msg, const ClientProtocol::TagMap& tags)
37                 : MessageDetails(mt, msg, tags)
38         {
39         }
40
41         bool IsCTCP(std::string& name, std::string& body) const CXX11_OVERRIDE
42         {
43                 if (!this->IsCTCP())
44                         return false;
45
46                 size_t end_of_name = text.find(' ', 2);
47                 size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
48                 if (end_of_name == std::string::npos)
49                 {
50                         // The CTCP only contains a name.
51                         name.assign(text, 1, text.length() - 1 - end_of_ctcp);
52                         body.clear();
53                         return true;
54                 }
55
56                 // The CTCP contains a name and a body.
57                 name.assign(text, 1, end_of_name - 1);
58
59                 size_t start_of_body = text.find_first_not_of(' ', end_of_name + 1);
60                 if (start_of_body == std::string::npos)
61                 {
62                         // The CTCP body is provided but empty.
63                         body.clear();
64                         return true;
65                 }
66
67                 // The CTCP body provided was non-empty.
68                 body.assign(text, start_of_body, text.length() - start_of_body - end_of_ctcp);
69                 return true;
70         }
71
72         bool IsCTCP(std::string& name) const CXX11_OVERRIDE
73         {
74                 if (!this->IsCTCP())
75                         return false;
76
77                 size_t end_of_name = text.find(' ', 2);
78                 if (end_of_name == std::string::npos)
79                 {
80                         // The CTCP only contains a name.
81                         size_t end_of_ctcp = *text.rbegin() == '\x1' ? 1 : 0;
82                         name.assign(text, 1, text.length() - 1 - end_of_ctcp);
83                         return true;
84                 }
85
86                 // The CTCP contains a name and a body.
87                 name.assign(text, 1, end_of_name - 1);
88                 return true;
89         }
90
91         bool IsCTCP() const CXX11_OVERRIDE
92         {
93                 // According to draft-oakley-irc-ctcp-02 a valid CTCP must begin with SOH and
94                 // contain at least one octet which is not NUL, SOH, CR, LF, or SPACE. As most
95                 // of these are restricted at the protocol level we only need to check for SOH
96                 // and SPACE.
97                 return (text.length() >= 2) && (text[0] == '\x1') &&  (text[1] != '\x1') && (text[1] != ' ');
98         }
99 };
100
101 namespace
102 {
103         bool FirePreEvents(User* source, MessageTarget& msgtarget, MessageDetails& msgdetails)
104         {
105                 // Inform modules that a message wants to be sent.
106                 ModResult modres;
107                 FIRST_MOD_RESULT(OnUserPreMessage, modres, (source, msgtarget, msgdetails));
108                 if (modres == MOD_RES_DENY)
109                 {
110                         // Inform modules that a module blocked the mssage.
111                         FOREACH_MOD(OnUserMessageBlocked, (source, msgtarget, msgdetails));
112                         return false;
113                 }
114
115                 // Check whether a module zapped the message body.
116                 if (msgdetails.text.empty())
117                 {
118                         source->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
119                         return false;
120                 }
121
122                 // Inform modules that a message is about to be sent.
123                 FOREACH_MOD(OnUserMessage, (source, msgtarget, msgdetails));
124                 return true;
125         }
126
127         CmdResult FirePostEvent(User* source, const MessageTarget& msgtarget, const MessageDetails& msgdetails)
128         {
129                 // If the source is local and was not sending a CTCP reply then update their idle time.
130                 LocalUser* lsource = IS_LOCAL(source);
131                 if (lsource && msgdetails.update_idle && (msgdetails.type != MSG_NOTICE || !msgdetails.IsCTCP()))
132                         lsource->idle_lastmsg = ServerInstance->Time();
133
134                 // Inform modules that a message was sent.
135                 FOREACH_MOD(OnUserPostMessage, (source, msgtarget, msgdetails));
136                 return CMD_SUCCESS;
137         }
138 }
139
140 class CommandMessage : public Command
141 {
142  private:
143         const MessageType msgtype;
144
145         CmdResult HandleChannelTarget(User* source, const Params& parameters, const char* target, PrefixMode* pm)
146         {
147                 Channel* chan = ServerInstance->FindChan(target);
148                 if (!chan)
149                 {
150                         // The target channel does not exist.
151                         source->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
152                         return CMD_FAILURE;
153                 }
154
155                 // Fire the pre-message events.
156                 MessageTarget msgtarget(chan, pm ? pm->GetPrefix() : 0);
157                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
158                 msgdetails.exemptions.insert(source);
159                 if (!FirePreEvents(source, msgtarget, msgdetails))
160                         return CMD_FAILURE;
161
162                 // Send the message to the members of the channel.
163                 ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, chan, msgdetails.text, msgdetails.type, msgtarget.status);
164                 privmsg.AddTags(msgdetails.tags_out);
165                 privmsg.SetSideEffect(true);
166                 chan->Write(ServerInstance->GetRFCEvents().privmsg, privmsg, msgtarget.status, msgdetails.exemptions);
167
168                 // Create the outgoing message and message event.
169                 return FirePostEvent(source, msgtarget, msgdetails);
170         }
171
172         CmdResult HandleServerTarget(User* source, const Params& parameters)
173         {
174                 // If the source isn't allowed to mass message users then reject
175                 // the attempt to mass-message users.
176                 if (!source->HasPrivPermission("users/mass-message"))
177                 {
178                         source->WriteNumeric(ERR_NOPRIVILEGES, "Permission Denied - You do not have the required operator privileges");
179                         return CMD_FAILURE;
180                 }
181
182                 // Extract the server glob match from the target parameter.
183                 std::string servername(parameters[0], 1);
184
185                 // Fire the pre-message events.
186                 MessageTarget msgtarget(&servername);
187                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
188                 if (!FirePreEvents(source, msgtarget, msgdetails))
189                         return CMD_FAILURE;
190
191                 // If the current server name matches the server name glob then send
192                 // the message out to the local users.
193                 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername))
194                 {
195                         // Create the outgoing message and message event.
196                         ClientProtocol::Messages::Privmsg message(ClientProtocol::Messages::Privmsg::nocopy, source, "$*", msgdetails.text, msgdetails.type);
197                         message.AddTags(msgdetails.tags_out);
198                         message.SetSideEffect(true);
199                         ClientProtocol::Event messageevent(ServerInstance->GetRFCEvents().privmsg, message);
200
201                         const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
202                         for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
203                         {
204                                 LocalUser* luser = *i;
205
206                                 // Don't send to unregistered users or the user who is the source.
207                                 if (luser->registered != REG_ALL || luser == source)
208                                         continue;
209
210                                 // Only send to non-exempt users.
211                                 if (!msgdetails.exemptions.count(luser))
212                                         luser->Send(messageevent);
213                         }
214                 }
215
216                 // Fire the post-message event.
217                 return FirePostEvent(source, msgtarget, msgdetails);
218         }
219
220         CmdResult HandleUserTarget(User* source, const Params& parameters)
221         {
222                 User* target;
223                 if (IS_LOCAL(source))
224                 {
225                         // Local sources can specify either a nick or a nick@server mask as the target.
226                         const char* targetserver = strchr(parameters[0].c_str(), '@');
227                         if (targetserver)
228                         {
229                                 // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
230                                 target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
231                                 if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
232                                         target = NULL;
233                         }
234                         else
235                         {
236                                 // If the source is a local user then we only look up the target by nick.
237                                 target = ServerInstance->FindNickOnly(parameters[0]);
238                         }
239                 }
240                 else
241                 {
242                         // Remote users can only specify a nick or UUID as the target.
243                         target = ServerInstance->FindNick(parameters[0]);
244                 }
245
246                 if (!target || target->registered != REG_ALL)
247                 {
248                         // The target user does not exist or is not fully registered.
249                         source->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
250                         return CMD_FAILURE;
251                 }
252
253                 // Fire the pre-message events.
254                 MessageTarget msgtarget(target);
255                 MessageDetailsImpl msgdetails(msgtype, parameters[1], parameters.GetTags());
256                 if (!FirePreEvents(source, msgtarget, msgdetails))
257                         return CMD_FAILURE;
258
259                 // If the target is away then inform the user.
260                 if (target->IsAway() && msgdetails.type == MSG_PRIVMSG)
261                         source->WriteNumeric(RPL_AWAY, target->nick, target->awaymsg);
262
263                 LocalUser* const localtarget = IS_LOCAL(target);
264                 if (localtarget)
265                 {
266                         // Send to the target if they are a local user.
267                         ClientProtocol::Messages::Privmsg privmsg(ClientProtocol::Messages::Privmsg::nocopy, source, localtarget->nick, msgdetails.text, msgdetails.type);
268                         privmsg.AddTags(msgdetails.tags_out);
269                         privmsg.SetSideEffect(true);
270                         localtarget->Send(ServerInstance->GetRFCEvents().privmsg, privmsg);
271                 }
272
273                 // Fire the post-message event.
274                 return FirePostEvent(source, msgtarget, msgdetails);
275         }
276
277  public:
278         CommandMessage(Module* parent, MessageType mt)
279                 : Command(parent, ClientProtocol::Messages::Privmsg::CommandStrFromMsgType(mt), 2, 2)
280                 , msgtype(mt)
281         {
282                 syntax = "<target>[,<target>]+ :<message>";
283         }
284
285         /** Handle command.
286          * @param parameters The parameters to the command
287          * @param user The user issuing the command
288          * @return A value from CmdResult to indicate command success or failure.
289          */
290         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
291         {
292                 if (CommandParser::LoopCall(user, this, parameters, 0))
293                         return CMD_SUCCESS;
294
295                 // The specified message was empty.
296                 if (parameters[1].empty())
297                 {
298                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
299                         return CMD_FAILURE;
300                 }
301
302                 // The target is a server glob.
303                 if (parameters[0][0] == '$')
304                         return HandleServerTarget(user, parameters);
305
306                 // If the message begins with a status character then look it up.
307                 const char* target = parameters[0].c_str();
308                 PrefixMode* pmh = ServerInstance->Modes->FindPrefix(target[0]);
309                 if (pmh)
310                         target++;
311
312                 // The target is a channel name.
313                 if (*target == '#')
314                         return HandleChannelTarget(user, parameters, target, pmh);
315
316                 // The target is a nickname.
317                 return HandleUserTarget(user, parameters);
318         }
319
320         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE
321         {
322                 if (IS_LOCAL(user))
323                         // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
324                         return ROUTE_LOCALONLY;
325                 else
326                         return ROUTE_MESSAGE(parameters[0]);
327         }
328 };
329
330 class CommandSQuery : public SplitCommand
331 {
332  public:
333         CommandSQuery(Module* Creator)
334                 : SplitCommand(Creator, "SQUERY", 2, 2)
335         {
336                 syntax = "<service> :<message>";
337         }
338
339         CmdResult HandleLocal(LocalUser* user, const Params& parameters) CXX11_OVERRIDE
340         {
341                 // The specified message was empty.
342                 if (parameters[1].empty())
343                 {
344                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
345                         return CMD_FAILURE;
346                 }
347
348                 // The target can be either a nick or a nick@server mask.
349                 User* target;
350                 const char* targetserver = strchr(parameters[0].c_str(), '@');
351                 if (targetserver)
352                 {
353                         // The target is a user on a specific server (e.g. jto@tolsun.oulu.fi).
354                         target = ServerInstance->FindNickOnly(parameters[0].substr(0, targetserver - parameters[0].c_str()));
355                         if (target && strcasecmp(target->server->GetName().c_str(), targetserver + 1))
356                                 target = NULL;
357                 }
358                 else
359                 {
360                         // The targer can be on any server.
361                         target = ServerInstance->FindNickOnly(parameters[0]);
362                 }
363
364                 if (!target || target->registered != REG_ALL || !target->server->IsULine())
365                 {
366                         // The target user does not exist, is not fully registered, or is not a service.
367                         user->WriteNumeric(ERR_NOSUCHSERVICE, parameters[0], "No such service");
368                         return CMD_FAILURE;
369                 }
370
371                 // Fire the pre-message events.
372                 MessageTarget msgtarget(target);
373                 MessageDetailsImpl msgdetails(MSG_PRIVMSG, parameters[1], parameters.GetTags());
374                 if (!FirePreEvents(user, msgtarget, msgdetails))
375                         return CMD_FAILURE;
376
377                 // The SQUERY command targets a service on a U-lined server. This can never
378                 // be on the server local to the source so we don't need to do any routing
379                 // logic and can forward it as a PRIVMSG.
380
381                 // Fire the post-message event.
382                 return FirePostEvent(user, msgtarget, msgdetails);
383         }
384 };
385
386 class ModuleCoreMessage : public Module
387 {
388  private:
389         CommandMessage cmdprivmsg;
390         CommandMessage cmdnotice;
391         CommandSQuery cmdsquery;
392         ChanModeReference moderatedmode;
393         ChanModeReference noextmsgmode;
394
395  public:
396         ModuleCoreMessage()
397                 : cmdprivmsg(this, MSG_PRIVMSG)
398                 , cmdnotice(this, MSG_NOTICE)
399                 , cmdsquery(this)
400                 , moderatedmode(this, "moderated")
401                 , noextmsgmode(this, "noextmsg")
402         {
403         }
404
405         ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
406         {
407                 if (!IS_LOCAL(user) || target.type != MessageTarget::TYPE_CHANNEL)
408                         return MOD_RES_PASSTHRU;
409
410                 Channel* chan = target.Get<Channel>();
411                 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
412                 {
413                         // The noextmsg mode is set and the user is not in the channel.
414                         user->WriteNumeric(Numerics::CannotSendTo(chan, "external messages", *noextmsgmode));
415                         return MOD_RES_DENY;
416                 }
417
418                 bool no_chan_priv = chan->GetPrefixValue(user) < VOICE_VALUE;
419                 if (no_chan_priv && chan->IsModeSet(moderatedmode))
420                 {
421                         // The moderated mode is set and the user has no status rank.
422                         user->WriteNumeric(Numerics::CannotSendTo(chan, "messages", *moderatedmode));
423                         return MOD_RES_DENY;
424                 }
425
426                 if (no_chan_priv && ServerInstance->Config->RestrictBannedUsers != ServerConfig::BUT_NORMAL && chan->IsBanned(user))
427                 {
428                         // The user is banned in the channel and restrictbannedusers is enabled.
429                         if (ServerInstance->Config->RestrictBannedUsers == ServerConfig::BUT_RESTRICT_NOTIFY)
430                                 user->WriteNumeric(Numerics::CannotSendTo(chan, "You cannot send messages to this channel whilst banned."));
431                         return MOD_RES_DENY;
432                 }
433
434                 return MOD_RES_PASSTHRU;
435         }
436
437         Version GetVersion() CXX11_OVERRIDE
438         {
439                 return Version("Provides the NOTICE, PRIVMSG, and SQUERY commands", VF_CORE|VF_VENDOR);
440         }
441 };
442
443 MODULE_INIT(ModuleCoreMessage)