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