]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_notice.cpp
ced17072fd0b6021cbececd78127419972d30274
[user/henk/code/inspircd.git] / src / commands / cmd_notice.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 /** Handle /NOTICE. These command handlers can be reloaded by the core,
16  * and handle basic RFC1459 commands. Commands within modules work
17  * the same way, however, they can be fully unloaded, where these
18  * may not.
19  */
20 class CommandNotice : public Command
21 {
22  public:
23         /** Constructor for notice.
24          */
25         CommandNotice ( Module* parent) : Command(parent,"NOTICE",2,2) { syntax = "<target>{,<target>} <message>"; }
26         /** Handle command.
27          * @param parameters The parameters to the comamnd
28          * @param pcnt The number of parameters passed to teh command
29          * @param user The user issuing the command
30          * @return A value from CmdResult to indicate command success or failure.
31          */
32         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
33 };
34
35
36 CmdResult CommandNotice::Handle (const std::vector<std::string>& parameters, User *user)
37 {
38         User *dest;
39         Channel *chan;
40
41         CUList exempt_list;
42
43         user->idle_lastmsg = ServerInstance->Time();
44
45         if (ServerInstance->Parser->LoopCall(user, this, parameters, 0))
46                 return CMD_SUCCESS;
47         if (parameters[0][0] == '$')
48         {
49                 if (!user->HasPrivPermission("users/mass-message"))
50                         return CMD_SUCCESS;
51
52                 ModResult MOD_RESULT;
53                 std::string temp = parameters[1];
54                 FIRST_MOD_RESULT(ServerInstance, OnUserPreNotice, MOD_RESULT, (user, (void*)parameters[0].c_str(), TYPE_SERVER, temp, 0, exempt_list));
55                 if (MOD_RESULT == MOD_RES_DENY)
56                         return CMD_FAILURE;
57                 const char* text = temp.c_str();
58                 const char* servermask = (parameters[0].c_str()) + 1;
59
60                 FOREACH_MOD(I_OnText,OnText(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, exempt_list));
61                 if (InspIRCd::Match(ServerInstance->Config->ServerName,servermask, NULL))
62                 {
63                         user->SendAll("NOTICE", "%s", text);
64                 }
65                 FOREACH_MOD(I_OnUserNotice,OnUserNotice(user, (void*)parameters[0].c_str(), TYPE_SERVER, text, 0, exempt_list));
66                 return CMD_SUCCESS;
67         }
68         char status = 0;
69         const char* target = parameters[0].c_str();
70
71         if (ServerInstance->Modes->FindPrefix(*target))
72         {
73                 status = *target;
74                 target++;
75         }
76         if (*target == '#')
77         {
78                 chan = ServerInstance->FindChan(target);
79
80                 exempt_list.insert(user);
81
82                 if (chan)
83                 {
84                         if (IS_LOCAL(user))
85                         {
86                                 if ((chan->IsModeSet('n')) && (!chan->HasUser(user)))
87                                 {
88                                         user->WriteNumeric(404, "%s %s :Cannot send to channel (no external messages)", user->nick.c_str(), chan->name.c_str());
89                                         return CMD_FAILURE;
90                                 }
91                                 if ((chan->IsModeSet('m')) && (chan->GetPrefixValue(user) < VOICE_VALUE))
92                                 {
93                                         user->WriteNumeric(404, "%s %s :Cannot send to channel (+m)", user->nick.c_str(), chan->name.c_str());
94                                         return CMD_FAILURE;
95                                 }
96                         }
97                         ModResult MOD_RESULT;
98
99                         std::string temp = parameters[1];
100                         FIRST_MOD_RESULT(ServerInstance, OnUserPreNotice, MOD_RESULT, (user,chan,TYPE_CHANNEL,temp,status, exempt_list));
101                         if (MOD_RESULT == MOD_RES_DENY)
102                                 return CMD_FAILURE;
103
104                         const char* text = temp.c_str();
105
106                         if (temp.empty())
107                         {
108                                 user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
109                                 return CMD_FAILURE;
110                         }
111
112                         FOREACH_MOD(I_OnText,OnText(user,chan,TYPE_CHANNEL,text,status,exempt_list));
113
114                         if (status)
115                         {
116                                 if (ServerInstance->Config->UndernetMsgPrefix)
117                                 {
118                                         chan->WriteAllExcept(user, false, status, exempt_list, "NOTICE %c%s :%c %s", status, chan->name.c_str(), status, text);
119                                 }
120                                 else
121                                 {
122                                         chan->WriteAllExcept(user, false, status, exempt_list, "NOTICE %c%s :%s", status, chan->name.c_str(), text);
123                                 }
124                         }
125                         else
126                         {
127                                 chan->WriteAllExcept(user, false, status, exempt_list, "NOTICE %s :%s", chan->name.c_str(), text);
128                         }
129
130                         FOREACH_MOD(I_OnUserNotice,OnUserNotice(user,chan,TYPE_CHANNEL,text,status,exempt_list));
131                 }
132                 else
133                 {
134                         /* no such nick/channel */
135                         user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), target);
136                         return CMD_FAILURE;
137                 }
138                 return CMD_SUCCESS;
139         }
140
141         const char* destnick = parameters[0].c_str();
142
143         if (IS_LOCAL(user))
144         {
145                 const char* targetserver = strchr(destnick, '@');
146
147                 if (targetserver)
148                 {
149                         std::string nickonly;
150
151                         nickonly.assign(destnick, 0, targetserver - destnick);
152                         dest = ServerInstance->FindNickOnly(nickonly);
153                         if (dest && strcasecmp(dest->server, targetserver + 1))
154                         {
155                                 /* Incorrect server for user */
156                                 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
157                                 return CMD_FAILURE;
158                         }
159                 }
160                 else
161                         dest = ServerInstance->FindNickOnly(destnick);
162         }
163         else
164                 dest = ServerInstance->FindNick(destnick);
165
166         if (dest)
167         {
168                 if (parameters[1].empty())
169                 {
170                         user->WriteNumeric(412, "%s :No text to send", user->nick.c_str());
171                         return CMD_FAILURE;
172                 }
173
174                 ModResult MOD_RESULT;
175                 std::string temp = parameters[1];
176                 FIRST_MOD_RESULT(ServerInstance, OnUserPreNotice, MOD_RESULT, (user,dest,TYPE_USER,temp,0,exempt_list));
177                 if (MOD_RESULT == MOD_RES_DENY) {
178                         return CMD_FAILURE;
179                 }
180                 const char* text = temp.c_str();
181
182                 FOREACH_MOD(I_OnText,OnText(user,dest,TYPE_USER,text,0,exempt_list));
183
184                 if (IS_LOCAL(dest))
185                 {
186                         // direct write, same server
187                         user->WriteTo(dest, "NOTICE %s :%s", dest->nick.c_str(), text);
188                 }
189
190                 FOREACH_MOD(I_OnUserNotice,OnUserNotice(user,dest,TYPE_USER,text,0,exempt_list));
191         }
192         else
193         {
194                 /* no such nick/channel */
195                 user->WriteNumeric(401, "%s %s :No such nick/channel",user->nick.c_str(), parameters[0].c_str());
196                 return CMD_FAILURE;
197         }
198
199         return CMD_SUCCESS;
200
201 }
202
203 COMMAND_INIT(CommandNotice)