]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_privmsg.cpp
First phase of conversion to dynamic limits on all the lengths, configured via the...
[user/henk/code/inspircd.git] / src / commands / cmd_privmsg.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 #include "wildcard.h"
16 #include "commands/cmd_privmsg.h"
17
18 extern "C" DllExport  Command* init_command(InspIRCd* Instance)
19 {
20         return new CommandPrivmsg(Instance);
21 }
22
23 CmdResult CommandPrivmsg::Handle (const std::vector<std::string>& parameters, User *user)
24 {
25         User *dest;
26         Channel *chan;
27         CUList except_list;
28
29         user->idle_lastmsg = ServerInstance->Time();
30         
31         if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
32                 return CMD_SUCCESS;
33
34         if ((parameters[0][0] == '$') && (IS_OPER(user) || ServerInstance->ULine(user->server)))
35         {
36                 int MOD_RESULT = 0;
37                 std::string temp = parameters[1];
38                 FOREACH_RESULT(I_OnUserPreMessage,OnUserPreMessage(user, (void*)parameters[0].c_str(), TYPE_SERVER, temp, 0, except_list));
39                 if (MOD_RESULT)
40                         return CMD_FAILURE;
41
42                 const char* text = temp.c_str();
43                 const char* servermask = (parameters[0].c_str()) + 1;
44
45                 FOREACH_MOD(I_OnText,OnText(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
46                 if (match(ServerInstance->Config->ServerName,servermask))
47                 {
48                         user->SendAll("PRIVMSG", "%s", text);
49                 }
50                 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, except_list));
51                 return CMD_SUCCESS;
52         }
53         char status = 0;
54         const char* target = parameters[0].c_str();
55
56         if (ServerInstance->Modes->FindPrefix(*target))
57         {
58                 status = *target;
59                 target++;
60         }
61         if (*target == '#')
62         {
63                 chan = ServerInstance->FindChan(target);
64
65                 except_list[user] = user->nick;
66
67                 if (chan)
68                 {
69                         if (IS_LOCAL(user))
70                         {
71                                 if ((chan->IsModeSet('n')) && (!chan->HasUser(user)))
72                                 {
73                                         user->WriteNumeric(404, "%s %s :Cannot send to channel (no external messages)", user->nick.c_str(), chan->name.c_str());
74                                         return CMD_FAILURE;
75                                 }
76                                 if ((chan->IsModeSet('m')) && (chan->GetStatus(user) < STATUS_VOICE))
77                                 {
78                                         user->WriteNumeric(404, "%s %s :Cannot send to channel (+m)", user->nick.c_str(), chan->name.c_str());
79                                         return CMD_FAILURE;
80                                 }
81                         }
82                         int MOD_RESULT = 0;
83
84                         std::string temp = parameters[1];
85                         FOREACH_RESULT(I_OnUserPreMessage,OnUserPreMessage(user,chan,TYPE_CHANNEL,temp,status,except_list));
86                         if (MOD_RESULT) {
87                                 return CMD_FAILURE;
88                         }
89                         const char* text = temp.c_str();
90
91                         /* Check again, a module may have zapped the input string */
92                         if (temp.empty())
93                         {
94                                 user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
95                                 return CMD_FAILURE;
96                         }
97
98                         FOREACH_MOD(I_OnText,OnText(user,chan,TYPE_CHANNEL,text,status,except_list));
99
100                         if (status)
101                         {
102                                 if (ServerInstance->Config->UndernetMsgPrefix)
103                                 {
104                                         chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%c %s", status, chan->name.c_str(), status, text);
105                                 }
106                                 else
107                                 {
108                                         chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%s", status, chan->name.c_str(), text);
109                                 }
110                         }
111                         else 
112                         {
113                                 chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %s :%s", chan->name.c_str(), text);
114                         }
115
116                         FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,chan,TYPE_CHANNEL,text,status,except_list));
117                 }
118                 else
119                 {
120                         /* no such nick/channel */
121                         user->WriteNumeric(401, "%s %s :No such nick/channel", user->nick.c_str(), target);
122                         return CMD_FAILURE;
123                 }
124                 return CMD_SUCCESS;
125         }
126
127         const char* destnick = parameters[0].c_str();
128
129         if (IS_LOCAL(user))
130         {
131                 const char* targetserver = strchr(destnick, '@');
132         
133                 if (targetserver)
134                 {
135                         std::string nickonly;
136
137                         nickonly.assign(destnick, 0, targetserver - destnick + 1);
138                         dest = ServerInstance->FindNickOnly(nickonly);
139                         if (dest && strcasecmp(dest->server, targetserver + 1))
140                         {
141                                 /* Incorrect server for user */
142                                 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
143                                 return CMD_FAILURE;
144                         }
145                 }
146                 else
147                         dest = ServerInstance->FindNickOnly(destnick);
148         }
149         else
150                 dest = ServerInstance->FindNick(destnick);
151
152         if (dest)
153         {
154                 if (parameters[1].empty())
155                 {
156                         user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
157                         return CMD_FAILURE;
158                 }
159
160                 if (IS_AWAY(dest))
161                 {
162                         /* auto respond with aweh msg */
163                         user->WriteNumeric(301, "%s %s :%s", user->nick.c_str(), dest->nick.c_str(), dest->awaymsg.c_str());
164                 }
165
166                 int MOD_RESULT = 0;
167                 
168                 std::string temp = parameters[1];
169                 FOREACH_RESULT(I_OnUserPreMessage,OnUserPreMessage(user, dest, TYPE_USER, temp, 0, except_list));
170                 if (MOD_RESULT) {
171                         return CMD_FAILURE;
172                 }
173                 const char* text = temp.c_str();
174
175                 FOREACH_MOD(I_OnText,OnText(user, dest, TYPE_USER, text, 0, except_list));
176
177                 if (IS_LOCAL(dest))
178                 {
179                         // direct write, same server
180                         user->WriteTo(dest, "PRIVMSG %s :%s", dest->nick.c_str(), text);
181                 }
182
183                 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user, dest, TYPE_USER, text, 0, except_list));
184         }
185         else
186         {
187                 /* no such nick/channel */
188                 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
189                 return CMD_FAILURE;
190         }
191         return CMD_SUCCESS;
192 }
193