]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_privmsg.cpp
OOPS! We try again, since I'm smoking craq. LF is 0x0a NOT CR.
[user/henk/code/inspircd.git] / src / cmd_privmsg.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 "configreader.h"
16 #include "users.h"
17 #include "modules.h"
18 #include "wildcard.h"
19 #include "commands/cmd_privmsg.h"
20
21 extern "C" DllExport  command_t* init_command(InspIRCd* Instance)
22 {
23         return new cmd_privmsg(Instance);
24 }
25
26 CmdResult cmd_privmsg::Handle (const char** parameters, int pcnt, userrec *user)
27 {
28         userrec *dest;
29         chanrec *chan;
30         CUList except_list;
31
32         user->idle_lastmsg = ServerInstance->Time();
33         
34         if (ServerInstance->Parser->LoopCall(user, this, parameters, pcnt, 0))
35                 return CMD_SUCCESS;
36
37         if ((parameters[0][0] == '$') && (IS_OPER(user) || ServerInstance->ULine(user->server)))
38         {
39                 int MOD_RESULT = 0;
40                 std::string temp = parameters[1];
41                 FOREACH_RESULT(I_OnUserPreMessage,OnUserPreMessage(user,(void*)parameters[0],TYPE_SERVER,temp,0,except_list));
42                 if (MOD_RESULT)
43                         return CMD_FAILURE;
44                 parameters[1] = temp.c_str();
45                 // notice to server mask
46                 const char* servermask = parameters[0] + 1;
47                 if (match(ServerInstance->Config->ServerName,servermask))
48                 {
49                         user->SendAll("PRIVMSG", "%s", parameters[1]);
50                 }
51                 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,(void*)parameters[0],TYPE_SERVER,parameters[1],0,except_list));
52                 return CMD_SUCCESS;
53         }
54         char status = 0;
55         if ((*parameters[0] == '@') || (*parameters[0] == '%') || (*parameters[0] == '+'))
56         {
57                 status = *parameters[0];
58                 parameters[0]++;
59         }
60         if (parameters[0][0] == '#')
61         {
62                 chan = ServerInstance->FindChan(parameters[0]);
63
64                 except_list[user] = user->nick;
65
66                 if (chan)
67                 {
68                         if (IS_LOCAL(user))
69                         {
70                                 if ((chan->modes[CM_NOEXTERNAL]) && (!chan->HasUser(user)))
71                                 {
72                                         user->WriteServ("404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
73                                         return CMD_FAILURE;
74                                 }
75                                 if ((chan->modes[CM_MODERATED]) && (chan->GetStatus(user) < STATUS_VOICE))
76                                 {
77                                         user->WriteServ("404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
78                                         return CMD_FAILURE;
79                                 }
80                         }
81                         int MOD_RESULT = 0;
82
83                         std::string temp = parameters[1];
84                         FOREACH_RESULT(I_OnUserPreMessage,OnUserPreMessage(user,chan,TYPE_CHANNEL,temp,status,except_list));
85                         if (MOD_RESULT) {
86                                 return CMD_FAILURE;
87                         }
88                         parameters[1] = temp.c_str();
89
90                         /* Check again, a module may have zapped the input string */
91                         if (temp.empty())
92                         {
93                                 user->WriteServ("412 %s :No text to send", user->nick);
94                                 return CMD_FAILURE;
95                         }
96
97                         if (status)
98                         {
99                                 if (ServerInstance->Config->UndernetMsgPrefix)
100                                 {
101                                         chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%c %s", status, chan->name, status, parameters[1]);
102                                 }
103                                 else
104                                 {
105                                         chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %c%s :%s", status, chan->name, parameters[1]);
106                                 }
107                         }
108                         else 
109                         {
110                                 chan->WriteAllExcept(user, false, status, except_list, "PRIVMSG %s :%s", chan->name, parameters[1]);
111                         }
112
113                         FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,chan,TYPE_CHANNEL,parameters[1],status,except_list));
114                 }
115                 else
116                 {
117                         /* no such nick/channel */
118                         user->WriteServ("401 %s %s :No such nick/channel",user->nick, parameters[0]);
119                         return CMD_FAILURE;
120                 }
121                 return CMD_SUCCESS;
122         }
123
124         dest = ServerInstance->FindNick(parameters[0]);
125         if (dest)
126         {
127                 if (!*parameters[1])
128                 {
129                         user->WriteServ("412 %s :No text to send", user->nick);
130                         return CMD_FAILURE;
131                 }
132
133                 if (IS_AWAY(dest))
134                 {
135                         /* auto respond with aweh msg */
136                         user->WriteServ("301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
137                 }
138
139                 int MOD_RESULT = 0;
140                 
141                 std::string temp = parameters[1];
142                 FOREACH_RESULT(I_OnUserPreMessage,OnUserPreMessage(user,dest,TYPE_USER,temp,0,except_list));
143                 if (MOD_RESULT) {
144                         return CMD_FAILURE;
145                 }
146                 parameters[1] = (char*)temp.c_str();
147
148                 if (IS_LOCAL(dest))
149                 {
150                         // direct write, same server
151                         user->WriteTo(dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
152                 }
153
154                 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,dest,TYPE_USER,parameters[1],0,except_list));
155         }
156         else
157         {
158                 /* no such nick/channel */
159                 user->WriteServ("401 %s %s :No such nick/channel",user->nick, parameters[0]);
160                 return CMD_FAILURE;
161         }
162         return CMD_SUCCESS;
163 }
164