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