]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/coremods/core_privmsg.cpp
Call OnUserMessageBlocked when a PRIVMSG or a NOTICE is blocked.
[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 namespace
25 {
26         const char* MessageTypeString[] = { "PRIVMSG", "NOTICE" };
27 }
28
29 class MessageCommandBase : public Command
30 {
31         ChanModeReference moderatedmode;
32         ChanModeReference noextmsgmode;
33
34         /** Send a PRIVMSG or NOTICE message to all local users from the given user
35          * @param user User sending the message
36          * @param msg The message to send
37          * @param mt Type of the message (MSG_PRIVMSG or MSG_NOTICE)
38          */
39         static void SendAll(User* user, const std::string& msg, MessageType mt);
40
41  public:
42         MessageCommandBase(Module* parent, MessageType mt)
43                 : Command(parent, MessageTypeString[mt], 2, 2)
44                 , moderatedmode(parent, "moderated")
45                 , noextmsgmode(parent, "noextmsg")
46         {
47                 syntax = "<target>{,<target>} <message>";
48         }
49
50         /** Handle command.
51          * @param parameters The parameters to the command
52          * @param user The user issuing the command
53          * @return A value from CmdResult to indicate command success or failure.
54          */
55         CmdResult HandleMessage(const std::vector<std::string>& parameters, User* user, MessageType mt);
56
57         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters) CXX11_OVERRIDE
58         {
59                 if (IS_LOCAL(user))
60                         // This is handled by the OnUserPostMessage hook to split the LoopCall pieces
61                         return ROUTE_LOCALONLY;
62                 else
63                         return ROUTE_MESSAGE(parameters[0]);
64         }
65 };
66
67 void MessageCommandBase::SendAll(User* user, const std::string& msg, MessageType mt)
68 {
69         const std::string message = ":" + user->GetFullHost() + " " + MessageTypeString[mt] + " $* :" + msg;
70         const UserManager::LocalList& list = ServerInstance->Users.GetLocalUsers();
71         for (UserManager::LocalList::const_iterator i = list.begin(); i != list.end(); ++i)
72         {
73                 if ((*i)->registered == REG_ALL)
74                         (*i)->Write(message);
75         }
76 }
77
78 CmdResult MessageCommandBase::HandleMessage(const std::vector<std::string>& parameters, User* user, MessageType mt)
79 {
80         User *dest;
81         Channel *chan;
82
83         LocalUser* localuser = IS_LOCAL(user);
84         if (localuser)
85                 localuser->idle_lastmsg = ServerInstance->Time();
86
87         if (CommandParser::LoopCall(user, this, parameters, 0))
88                 return CMD_SUCCESS;
89
90         if (parameters[0][0] == '$')
91         {
92                 if (!user->HasPrivPermission("users/mass-message"))
93                         return CMD_SUCCESS;
94
95                 std::string servername(parameters[0], 1);
96                 MessageTarget msgtarget(&servername);
97                 MessageDetails msgdetails(mt, parameters[1]);
98
99                 ModResult MOD_RESULT;
100                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
101                 if (MOD_RESULT == MOD_RES_DENY)
102                 {
103                         FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
104                         return CMD_FAILURE;
105                 }
106
107                 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
108                 if (InspIRCd::Match(ServerInstance->Config->ServerName, servername, NULL))
109                 {
110                         SendAll(user, msgdetails.text, mt);
111                 }
112                 FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
113                 return CMD_SUCCESS;
114         }
115
116         char status = 0;
117         const char* target = parameters[0].c_str();
118
119         if (ServerInstance->Modes->FindPrefix(*target))
120         {
121                 status = *target;
122                 target++;
123         }
124         if (*target == '#')
125         {
126                 chan = ServerInstance->FindChan(target);
127
128                 if (chan)
129                 {
130                         if (localuser && chan->GetPrefixValue(user) < VOICE_VALUE)
131                         {
132                                 if (chan->IsModeSet(noextmsgmode) && !chan->HasUser(user))
133                                 {
134                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (no external messages)");
135                                         return CMD_FAILURE;
136                                 }
137
138                                 if (chan->IsModeSet(moderatedmode))
139                                 {
140                                         user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (+m)");
141                                         return CMD_FAILURE;
142                                 }
143
144                                 if (ServerInstance->Config->RestrictBannedUsers)
145                                 {
146                                         if (chan->IsBanned(user))
147                                         {
148                                                 user->WriteNumeric(ERR_CANNOTSENDTOCHAN, chan->name, "Cannot send to channel (you're banned)");
149                                                 return CMD_FAILURE;
150                                         }
151                                 }
152                         }
153
154                         MessageTarget msgtarget(chan, status);
155                         MessageDetails msgdetails(mt, parameters[1]);
156                         msgdetails.exemptions.insert(user);
157
158                         ModResult MOD_RESULT;
159                         FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
160                         if (MOD_RESULT == MOD_RES_DENY)
161                         {
162                                 FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
163                                 return CMD_FAILURE;
164                         }
165
166                         /* Check again, a module may have zapped the input string */
167                         if (msgdetails.text.empty())
168                         {
169                                 user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
170                                 return CMD_FAILURE;
171                         }
172
173                         FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
174
175                         if (status)
176                         {
177                                 chan->WriteAllExcept(user, false, status, msgdetails.exemptions, "%s %c%s :%s", MessageTypeString[mt], status, chan->name.c_str(), msgdetails.text.c_str());
178                         }
179                         else
180                         {
181                                 chan->WriteAllExcept(user, false, status, msgdetails.exemptions, "%s %s :%s", MessageTypeString[mt], chan->name.c_str(), msgdetails.text.c_str());
182                         }
183
184                         FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
185                 }
186                 else
187                 {
188                         /* channel does not exist */
189                         user->WriteNumeric(Numerics::NoSuchChannel(parameters[0]));
190                         return CMD_FAILURE;
191                 }
192                 return CMD_SUCCESS;
193         }
194
195         const char* destnick = parameters[0].c_str();
196
197         if (localuser)
198         {
199                 const char* targetserver = strchr(destnick, '@');
200
201                 if (targetserver)
202                 {
203                         std::string nickonly;
204
205                         nickonly.assign(destnick, 0, targetserver - destnick);
206                         dest = ServerInstance->FindNickOnly(nickonly);
207                         if (dest && strcasecmp(dest->server->GetName().c_str(), targetserver + 1))
208                         {
209                                 /* Incorrect server for user */
210                                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
211                                 return CMD_FAILURE;
212                         }
213                 }
214                 else
215                         dest = ServerInstance->FindNickOnly(destnick);
216         }
217         else
218                 dest = ServerInstance->FindNick(destnick);
219
220         if ((dest) && (dest->registered == REG_ALL))
221         {
222                 if (parameters[1].empty())
223                 {
224                         user->WriteNumeric(ERR_NOTEXTTOSEND, "No text to send");
225                         return CMD_FAILURE;
226                 }
227
228                 if ((dest->IsAway()) && (mt == MSG_PRIVMSG))
229                 {
230                         /* auto respond with aweh msg */
231                         user->WriteNumeric(RPL_AWAY, dest->nick, dest->awaymsg);
232                 }
233
234                 MessageTarget msgtarget(dest);
235                 MessageDetails msgdetails(mt, parameters[1]);
236
237                 ModResult MOD_RESULT;
238                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, msgtarget, msgdetails));
239                 if (MOD_RESULT == MOD_RES_DENY)
240                 {
241                         FOREACH_MOD(OnUserMessageBlocked, (user, msgtarget, msgdetails));
242                         return CMD_FAILURE;
243                 }
244
245                 FOREACH_MOD(OnUserMessage, (user, msgtarget, msgdetails));
246
247                 if (IS_LOCAL(dest))
248                 {
249                         // direct write, same server
250                         dest->WriteFrom(user, "%s %s :%s", MessageTypeString[mt], dest->nick.c_str(), msgdetails.text.c_str());
251                 }
252
253                 FOREACH_MOD(OnUserPostMessage, (user, msgtarget, msgdetails));
254         }
255         else
256         {
257                 /* no such nick/channel */
258                 user->WriteNumeric(Numerics::NoSuchNick(parameters[0]));
259                 return CMD_FAILURE;
260         }
261         return CMD_SUCCESS;
262 }
263
264 template<MessageType MT>
265 class CommandMessage : public MessageCommandBase
266 {
267  public:
268         CommandMessage(Module* parent)
269                 : MessageCommandBase(parent, MT)
270         {
271         }
272
273         CmdResult Handle(const std::vector<std::string>& parameters, User* user) CXX11_OVERRIDE
274         {
275                 return HandleMessage(parameters, user, MT);
276         }
277 };
278
279 class ModuleCoreMessage : public Module
280 {
281         CommandMessage<MSG_PRIVMSG> CommandPrivmsg;
282         CommandMessage<MSG_NOTICE> CommandNotice;
283
284  public:
285         ModuleCoreMessage()
286                 : CommandPrivmsg(this), CommandNotice(this)
287         {
288         }
289
290         Version GetVersion() CXX11_OVERRIDE
291         {
292                 return Version("PRIVMSG, NOTICE", VF_CORE|VF_VENDOR);
293         }
294 };
295
296 MODULE_INIT(ModuleCoreMessage)