]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_privmsg.cpp
Move to entirely using insp_sockaddr and insp_inaddr for socket stuff, first step...
[user/henk/code/inspircd.git] / src / cmd_privmsg.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include <string>
18 #include "inspircd_config.h"
19 #include "inspircd.h"
20 #include "configreader.h"
21 #include "users.h"
22 #include "modules.h"
23 #include "message.h"
24 #include "wildcard.h"
25 #include "commands.h"
26 #include "helperfuncs.h"
27 #include "commands/cmd_privmsg.h"
28
29 extern ServerConfig* Config;
30 extern InspIRCd* ServerInstance;
31 extern int MODCOUNT;
32 extern ModuleList modules;
33 extern FactoryList factory;
34 extern time_t TIME;
35
36 void cmd_privmsg::Handle (const char** parameters, int pcnt, userrec *user)
37 {
38         userrec *dest;
39         chanrec *chan;
40
41         user->idle_lastmsg = TIME;
42         
43         if (ServerInstance->Parser->LoopCall(user, this, parameters, pcnt, 0))
44                 return;
45
46         if ((parameters[0][0] == '$') && ((*user->oper) || (is_uline(user->server))))
47         {
48                 int MOD_RESULT = 0;
49                 std::string temp = parameters[1];
50                 FOREACH_RESULT(I_OnUserPreMessage,OnUserPreMessage(user,(void*)parameters[0],TYPE_SERVER,temp,0));
51                 if (MOD_RESULT)
52                         return;
53                 parameters[1] = (char*)temp.c_str();
54                 // notice to server mask
55                 const char* servermask = parameters[0] + 1;
56                 if (match(Config->ServerName,servermask))
57                 {
58                         ServerPrivmsgAll("%s",parameters[1]);
59                 }
60                 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,(void*)parameters[0],TYPE_SERVER,parameters[1],0));
61                 return;
62         }
63         char status = 0;
64         if ((*parameters[0] == '@') || (*parameters[0] == '%') || (*parameters[0] == '+'))
65         {
66                 status = *parameters[0];
67                 parameters[0]++;
68         }
69         if (parameters[0][0] == '#')
70         {
71                 chan = FindChan(parameters[0]);
72                 if (chan)
73                 {
74                         if (IS_LOCAL(user))
75                         {
76                                 if ((chan->modes[CM_NOEXTERNAL]) && (!chan->HasUser(user)))
77                                 {
78                                         WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
79                                         return;
80                                 }
81                                 if ((chan->modes[CM_MODERATED]) && (cstatus(user,chan)<STATUS_VOICE))
82                                 {
83                                         WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
84                                         return;
85                                 }
86                         }
87                         int MOD_RESULT = 0;
88
89                         std::string temp = parameters[1];
90                         FOREACH_RESULT(I_OnUserPreMessage,OnUserPreMessage(user,chan,TYPE_CHANNEL,temp,status));
91                         if (MOD_RESULT) {
92                                 return;
93                         }
94                         parameters[1] = (char*)temp.c_str();
95
96                         if (temp == "")
97                         {
98                                 WriteServ(user->fd,"412 %s No text to send", user->nick);
99                                 return;
100                         }
101                         
102                         ChanExceptSender(chan, user, status, "PRIVMSG %s :%s", chan->name, parameters[1]);
103                         FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,chan,TYPE_CHANNEL,parameters[1],status));
104                 }
105                 else
106                 {
107                         /* no such nick/channel */
108                         WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
109                 }
110                 return;
111         }
112
113         dest = Find(parameters[0]);
114         if (dest)
115         {
116                 if ((IS_LOCAL(user)) && (*dest->awaymsg))
117                 {
118                         /* auto respond with aweh msg */
119                         WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
120                 }
121
122                 int MOD_RESULT = 0;
123                 
124                 std::string temp = parameters[1];
125                 FOREACH_RESULT(I_OnUserPreMessage,OnUserPreMessage(user,dest,TYPE_USER,temp,0));
126                 if (MOD_RESULT) {
127                         return;
128                 }
129                 parameters[1] = (char*)temp.c_str();
130
131                 if (dest->fd > -1)
132                 {
133                         // direct write, same server
134                         WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
135                 }
136
137                 FOREACH_MOD(I_OnUserMessage,OnUserMessage(user,dest,TYPE_USER,parameters[1],0));
138         }
139         else
140         {
141                 /* no such nick/channel */
142                 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
143         }
144 }