]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/ircv3_replies.h
6a490c4d10202e94a042d81646ff0d0ffef0d222
[user/henk/code/inspircd.git] / include / modules / ircv3_replies.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019 Peter Powell <petpow@saberuk.com>
5  *
6  * This file is part of InspIRCd.  InspIRCd is free software: you can
7  * redistribute it and/or modify it under the terms of the GNU General Public
8  * License as published by the Free Software Foundation, version 2.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 // IMPORTANT: The contents of this file are experimental and are not presently
20 // covered by the InspIRCd API stability guarantee.
21
22
23 #pragma once
24
25 #include "modules/cap.h"
26
27 namespace IRCv3
28 {
29         namespace Replies
30         {
31                 class Reply;
32                 class Fail;
33                 class Note;
34                 class Warn;
35         }
36 }
37
38 /** Base class for standard replies. */
39 class IRCv3::Replies::Reply
40 {
41  private:
42         /** The name of the command for this reply. */
43         std::string cmd;
44
45         /** The event provider for this reply. */
46         ClientProtocol::EventProvider evprov;
47
48         /** Wraps a message in an event and sends it to a user.
49          * @param user The user to send the message to.
50          * @param msg The message to send to the user.
51          */
52         void SendInternal(LocalUser* user, ClientProtocol::Message& msg)
53         {
54                 ClientProtocol::Event ev(evprov, msg);
55                 user->Send(ev);
56         }
57
58  protected:
59         /** Initializes a new instance of the Reply class.
60          * @param Creator The module which created this instance.
61          * @param Cmd The name of the command to reply with.
62          */
63         Reply(Module* Creator, const std::string& Cmd)
64                 : cmd(Cmd)
65                 , evprov(Creator, Cmd)
66         {
67         }
68
69  public:
70         /**
71          * Sends a standard reply to the specified user.
72          * @param user The user to send the reply to.
73          * @param command The command that the reply relates to.
74          * @param code A machine readable code for this reply.
75          * @param description A human readable description of this reply.
76          */
77         void Send(LocalUser* user, Command* command, const std::string& code, const std::string& description)
78         {
79                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
80                 msg.PushParamRef(command->name);
81                 msg.PushParam(code);
82                 msg.PushParam(description);
83                 SendInternal(user, msg);
84         }
85
86         template<typename T1>
87         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const std::string& description)
88         {
89                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
90                 msg.PushParamRef(command->name);
91                 msg.PushParam(code);
92                 msg.PushParam(ConvToStr(p1));
93                 msg.PushParam(description);
94                 SendInternal(user, msg);
95         }
96
97         template<typename T1, typename T2>
98         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
99                 const std::string& description)
100         {
101                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
102                 msg.PushParamRef(command->name);
103                 msg.PushParam(code);
104                 msg.PushParam(ConvToStr(p1));
105                 msg.PushParam(ConvToStr(p2));
106                 msg.PushParam(description);
107                 SendInternal(user, msg);
108         }
109
110         template<typename T1, typename T2, typename T3>
111         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
112                 const T3& p3, const std::string& description)
113         {
114                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
115                 msg.PushParamRef(command->name);
116                 msg.PushParam(code);
117                 msg.PushParam(ConvToStr(p1));
118                 msg.PushParam(ConvToStr(p2));
119                 msg.PushParam(ConvToStr(p3));
120                 msg.PushParam(description);
121                 SendInternal(user, msg);
122         }
123
124         template<typename T1, typename T2, typename T3, typename T4>
125         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
126                 const T3& p3, const T4& p4, const std::string& description)
127         {
128                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
129                 msg.PushParamRef(command->name);
130                 msg.PushParam(code);
131                 msg.PushParam(ConvToStr(p1));
132                 msg.PushParam(ConvToStr(p2));
133                 msg.PushParam(ConvToStr(p3));
134                 msg.PushParam(ConvToStr(p4));
135                 msg.PushParam(description);
136                 SendInternal(user, msg);
137         }
138
139         template<typename T1, typename T2, typename T3, typename T4, typename T5>
140         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
141                 const T3& p3, const T4& p4, const T5& p5, const std::string& description)
142         {
143                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
144                 msg.PushParamRef(command->name);
145                 msg.PushParam(code);
146                 msg.PushParam(ConvToStr(p1));
147                 msg.PushParam(ConvToStr(p2));
148                 msg.PushParam(ConvToStr(p3));
149                 msg.PushParam(ConvToStr(p4));
150                 msg.PushParam(ConvToStr(p5));
151                 msg.PushParam(description);
152                 SendInternal(user, msg);
153         }
154
155         /**
156          * Sends a standard reply to the specified user if they have the specified cap
157          * or a notice if they do not.
158          * @param user The user to send the reply to.
159          * @param command The command that the reply relates to.
160          * @param code A machine readable code for this reply.
161          * @param description A human readable description of this reply.
162          */
163         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
164                 const std::string& description)
165         {
166                 if (cap.get(user))
167                         Send(user, command, code, description);
168                 else
169                         user->WriteNotice(InspIRCd::Format("*** %s: %s", command->name.c_str(), description.c_str()));
170         }
171 };
172
173 /** Sends a FAIL standard reply. */
174 class IRCv3::Replies::Fail
175         : public IRCv3::Replies::Reply
176 {
177 public:
178         /** Initializes a new instance of the Fail class.
179          * @param Creator The module which created this instance.
180          */
181         Fail(Module* Creator)
182                 : Reply(Creator, "FAIL")
183         {
184         }
185 };
186
187 /** Sends a NOTE standard reply. */
188 class IRCv3::Replies::Note
189         : public IRCv3::Replies::Reply
190 {
191 public:
192         /** Initializes a new instance of the Note class.
193          * @param Creator The module which created this instance.
194          */
195         Note(Module* Creator)
196                 : Reply(Creator, "NOTE")
197         {
198         }
199 };
200
201 /** Sends a WARN standard reply. */
202 class IRCv3::Replies::Warn
203         : public IRCv3::Replies::Reply
204 {
205 public:
206         /** Initializes a new instance of the Warn class.
207          * @param Creator The module which created this instance.
208          */
209         Warn(Module* Creator)
210                 : Reply(Creator, "WARN")
211         {
212         }
213 };