]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_privmsg.cpp
Remove OnUserPreNotice and OnUserNotice hooks, add MessageType argument to OnUserMess...
[user/henk/code/inspircd.git] / src / commands / cmd_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 /** Handle /PRIVMSG. These command handlers can be reloaded by the core,
25  * and handle basic RFC1459 commands. Commands within modules work
26  * the same way, however, they can be fully unloaded, where these
27  * may not.
28  */
29 class CommandPrivmsg : public Command
30 {
31  public:
32         /** Constructor for privmsg.
33          */
34         CommandPrivmsg ( Module* parent) : Command(parent,"PRIVMSG",2,2) { syntax = "<target>{,<target>} <message>"; }
35         /** Handle command.
36          * @param parameters The parameters to the comamnd
37          * @param pcnt The number of parameters passed to teh command
38          * @param user The user issuing the command
39          * @return A value from CmdResult to indicate command success or failure.
40          */
41         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
42
43         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
44         {
45                 if (IS_LOCAL(user))
46                         // This is handled by the OnUserMessage hook to split the LoopCall pieces
47                         return ROUTE_LOCALONLY;
48                 else
49                         return ROUTE_MESSAGE(parameters[0]);
50         }
51 };
52
53 CmdResult CommandPrivmsg::Handle (const std::vector<std::string>& parameters, User *user)
54 {
55         User *dest;
56         Channel *chan;
57         CUList except_list;
58
59         LocalUser* localuser = IS_LOCAL(user);
60         if (localuser)
61                 localuser->idle_lastmsg = ServerInstance->Time();
62
63         if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
64                 return CMD_SUCCESS;
65
66         if (parameters[0][0] == '$')
67         {
68                 if (!user->HasPrivPermission("users/mass-message"))
69                         return CMD_SUCCESS;
70
71                 ModResult MOD_RESULT;
72                 std::string temp = parameters[1];
73                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, (void*)parameters[0].c_str(), TYPE_SERVER, temp, 0, except_list, MSG_PRIVMSG));
74                 if (MOD_RESULT == MOD_RES_DENY)
75                         return CMD_FAILURE;
76
77                 const char* text = temp.c_str();
78                 const char* servermask = (parameters[0].c_str()) + 1;
79
80                 FOREACH_MOD(I_OnText,OnText(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
81                 if (InspIRCd::Match(ServerInstance->Config->ServerName, servermask, NULL))
82                 {
83                         user->SendAll("PRIVMSG", "%s", text);
84                 }
85                 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list, MSG_PRIVMSG));
86                 return CMD_SUCCESS;
87         }
88         char status = 0;
89         const char* target = parameters[0].c_str();
90
91         if (ServerInstance->Modes->FindPrefix(*target))
92         {
93                 status = *target;
94                 target++;
95         }
96         if (*target == '#')
97         {
98                 chan = ServerInstance->FindChan(target);
99
100                 except_list.insert(user);
101
102                 if (chan)
103                 {
104                         if (localuser && chan->GetPrefixValue(user) < VOICE_VALUE)
105                         {
106                                 if (chan->IsModeSet('n') && !chan->HasUser(user))
107                                 {
108                                         user->WriteNumeric(404, "%s %s :Cannot send to channel (no external messages)", user->nick.c_str(), chan->name.c_str());
109                                         return CMD_FAILURE;
110                                 }
111
112                                 if (chan->IsModeSet('m'))
113                                 {
114                                         user->WriteNumeric(404, "%s %s :Cannot send to channel (+m)", user->nick.c_str(), chan->name.c_str());
115                                         return CMD_FAILURE;
116                                 }
117
118                                 if (ServerInstance->Config->RestrictBannedUsers)
119                                 {
120                                         if (chan->IsBanned(user))
121                                         {
122                                                 user->WriteNumeric(404, "%s %s :Cannot send to channel (you're banned)", user->nick.c_str(), chan->name.c_str());
123                                                 return CMD_FAILURE;
124                                         }
125                                 }
126                         }
127                         ModResult MOD_RESULT;
128
129                         std::string temp = parameters[1];
130                         FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, chan, TYPE_CHANNEL, temp, status, except_list, MSG_PRIVMSG));
131                         if (MOD_RESULT == MOD_RES_DENY)
132                                 return CMD_FAILURE;
133
134                         const char* text = temp.c_str();
135
136                         /* Check again, a module may have zapped the input string */
137                         if (temp.empty())
138                         {
139                                 user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
140                                 return CMD_FAILURE;
141                         }
142
143                         FOREACH_MOD(I_OnText,OnText(user,chan,TYPE_CHANNEL,text,status,except_list));
144
145                         if (status)
146                         {
147                                 if (ServerInstance->Config->UndernetMsgPrefix)
148                                 {
149                                         chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%c %s", status, chan->name.c_str(), status, text);
150                                 }
151                                 else
152                                 {
153                                         chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%s", status, chan->name.c_str(), text);
154                                 }
155                         }
156                         else
157                         {
158                                 chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %s :%s", chan->name.c_str(), text);
159                         }
160
161                         FOREACH_MOD(I_OnUserMessage, OnUserMessage(user,chan, TYPE_CHANNEL, text, status, except_list, MSG_PRIVMSG));
162                 }
163                 else
164                 {
165                         /* no such nick/channel */
166                         user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), target);
167                         return CMD_FAILURE;
168                 }
169                 return CMD_SUCCESS;
170         }
171
172         const char* destnick = parameters[0].c_str();
173
174         if (localuser)
175         {
176                 const char* targetserver = strchr(destnick, '@');
177
178                 if (targetserver)
179                 {
180                         std::string nickonly;
181
182                         nickonly.assign(destnick, 0, targetserver - destnick);
183                         dest = ServerInstance->FindNickOnly(nickonly);
184                         if (dest && strcasecmp(dest->server.c_str(), targetserver + 1))
185                         {
186                                 /* Incorrect server for user */
187                                 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
188                                 return CMD_FAILURE;
189                         }
190                 }
191                 else
192                         dest = ServerInstance->FindNickOnly(destnick);
193         }
194         else
195                 dest = ServerInstance->FindNick(destnick);
196
197         if ((dest) && (dest->registered == REG_ALL))
198         {
199                 if (parameters[1].empty())
200                 {
201                         user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
202                         return CMD_FAILURE;
203                 }
204
205                 if (dest->IsAway())
206                 {
207                         /* auto respond with aweh msg */
208                         user->WriteNumeric(301, "%s %s :%s", user->nick.c_str(), dest->nick.c_str(), dest->awaymsg.c_str());
209                 }
210
211                 ModResult MOD_RESULT;
212
213                 std::string temp = parameters[1];
214                 FIRST_MOD_RESULT(OnUserPreMessage, MOD_RESULT, (user, dest, TYPE_USER, temp, 0, except_list, MSG_PRIVMSG));
215                 if (MOD_RESULT == MOD_RES_DENY)
216                         return CMD_FAILURE;
217
218                 const char* text = temp.c_str();
219
220                 FOREACH_MOD(I_OnText,OnText(user, dest, TYPE_USER, text, 0, except_list));
221
222                 if (IS_LOCAL(dest))
223                 {
224                         // direct write, same server
225                         user->WriteTo(dest, "PRIVMSG %s :%s", dest->nick.c_str(), text);
226                 }
227
228                 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user, dest, TYPE_USER, text, 0, except_list, MSG_PRIVMSG));
229         }
230         else
231         {
232                 /* no such nick/channel */
233                 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
234                 return CMD_FAILURE;
235         }
236         return CMD_SUCCESS;
237 }
238
239 COMMAND_INIT(CommandPrivmsg)