]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/ircv3_replies.h
Add a link to the packaging advice to the configure help.
[user/henk/code/inspircd.git] / include / modules / ircv3_replies.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2019-2020 Sadie Powell <sadie@witchery.services>
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
20 #pragma once
21
22 #include "modules/cap.h"
23
24 namespace IRCv3
25 {
26         namespace Replies
27         {
28                 class CapReference;
29                 class Reply;
30                 class Fail;
31                 class Note;
32                 class Warn;
33         }
34 }
35
36 /** Reference to the inspircd.org/standard-replies cap. */
37 class IRCv3::Replies::CapReference
38         : public Cap::Reference
39 {
40  public:
41         CapReference(Module* mod)
42                 : Cap::Reference(mod, "inspircd.org/standard-replies")
43         {
44         }
45 };
46
47 /** Base class for standard replies. */
48 class IRCv3::Replies::Reply
49 {
50  private:
51         /** The name of the command for this reply. */
52         const std::string cmd;
53
54         /** The event provider for this reply. */
55         ClientProtocol::EventProvider evprov;
56
57         /** Wraps a message in an event and sends it to a user.
58          * @param user The user to send the message to.
59          * @param msg The message to send to the user.
60          */
61         void SendInternal(LocalUser* user, ClientProtocol::Message& msg)
62         {
63                 ClientProtocol::Event ev(evprov, msg);
64                 user->Send(ev);
65         }
66
67         void SendNoticeInternal(LocalUser* user, Command* command, const std::string& description)
68         {
69                 user->WriteNotice(InspIRCd::Format("*** %s: %s", command->name.c_str(), description.c_str()));
70         }
71
72  protected:
73         /** Initializes a new instance of the Reply class.
74          * @param Creator The module which created this instance.
75          * @param Cmd The name of the command to reply with.
76          */
77         Reply(Module* Creator, const std::string& Cmd)
78                 : cmd(Cmd)
79                 , evprov(Creator, Cmd)
80         {
81         }
82
83  public:
84         /**
85          * Sends a standard reply to the specified user.
86          * @param user The user to send the reply to.
87          * @param command The command that the reply relates to.
88          * @param code A machine readable code for this reply.
89          * @param description A human readable description of this reply.
90          */
91         void Send(LocalUser* user, Command* command, const std::string& code, const std::string& description)
92         {
93                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
94                 msg.PushParamRef(command->name);
95                 msg.PushParam(code);
96                 msg.PushParam(description);
97                 SendInternal(user, msg);
98         }
99
100         template<typename T1>
101         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const std::string& description)
102         {
103                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
104                 msg.PushParamRef(command->name);
105                 msg.PushParam(code);
106                 msg.PushParam(ConvToStr(p1));
107                 msg.PushParam(description);
108                 SendInternal(user, msg);
109         }
110
111         template<typename T1, typename T2>
112         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
113                 const std::string& description)
114         {
115                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
116                 msg.PushParamRef(command->name);
117                 msg.PushParam(code);
118                 msg.PushParam(ConvToStr(p1));
119                 msg.PushParam(ConvToStr(p2));
120                 msg.PushParam(description);
121                 SendInternal(user, msg);
122         }
123
124         template<typename T1, typename T2, typename T3>
125         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
126                 const T3& p3, 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(description);
135                 SendInternal(user, msg);
136         }
137
138         template<typename T1, typename T2, typename T3, typename T4>
139         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
140                 const T3& p3, const T4& p4, const std::string& description)
141         {
142                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
143                 msg.PushParamRef(command->name);
144                 msg.PushParam(code);
145                 msg.PushParam(ConvToStr(p1));
146                 msg.PushParam(ConvToStr(p2));
147                 msg.PushParam(ConvToStr(p3));
148                 msg.PushParam(ConvToStr(p4));
149                 msg.PushParam(description);
150                 SendInternal(user, msg);
151         }
152
153         template<typename T1, typename T2, typename T3, typename T4, typename T5>
154         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
155                 const T3& p3, const T4& p4, const T5& p5, const std::string& description)
156         {
157                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
158                 if (command)
159                         msg.PushParamRef(command->name);
160                 else
161                         msg.PushParam("*");
162                 msg.PushParam(code);
163                 msg.PushParam(ConvToStr(p1));
164                 msg.PushParam(ConvToStr(p2));
165                 msg.PushParam(ConvToStr(p3));
166                 msg.PushParam(ConvToStr(p4));
167                 msg.PushParam(ConvToStr(p5));
168                 msg.PushParam(description);
169                 SendInternal(user, msg);
170         }
171
172         /**
173          * Sends a standard reply to the specified user if they have the specified cap
174          * or a notice if they do not.
175          * @param user The user to send the reply to.
176          * @param command The command that the reply relates to.
177          * @param code A machine readable code for this reply.
178          * @param description A human readable description of this reply.
179          */
180         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
181                 const std::string& description)
182         {
183                 if (cap.get(user))
184                         Send(user, command, code, description);
185                 else
186                         SendNoticeInternal(user, command, description);
187         }
188
189         template<typename T1>
190         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
191                 const T1& p1, const std::string& description)
192         {
193                 if (cap.get(user))
194                         Send(user, command, code, p1, description);
195                 else
196                         SendNoticeInternal(user, command, description);
197         }
198
199         template<typename T1, typename T2>
200         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
201                 const T1& p1, const T2& p2, const std::string& description)
202         {
203                 if (cap.get(user))
204                         Send(user, command, code, p1, p2, description);
205                 else
206                         SendNoticeInternal(user, command, description);
207         }
208
209         template<typename T1, typename T2, typename T3>
210         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
211                 const T1& p1, const T2& p2, const T3& p3, const std::string& description)
212         {
213                 if (cap.get(user))
214                         Send(user, command, code, p1, p2, p3, description);
215                 else
216                         SendNoticeInternal(user, command, description);
217         }
218
219         template<typename T1, typename T2, typename T3, typename T4>
220         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
221                 const T1& p1, const T2& p2, const T3& p3, const T4& p4, const std::string& description)
222         {
223                 if (cap.get(user))
224                         Send(user, command, code, p1, p2, p3, p4, description);
225                 else
226                         SendNoticeInternal(user, command, description);
227         }
228
229         template<typename T1, typename T2, typename T3, typename T4, typename T5>
230         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
231                 const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5, const std::string& description)
232         {
233                 if (cap.get(user))
234                         Send(user, command, code, p1, p2, p3, p4, p5, description);
235                 else
236                         SendNoticeInternal(user, command, description);
237         }
238 };
239
240 /** Sends a FAIL standard reply. */
241 class IRCv3::Replies::Fail
242         : public IRCv3::Replies::Reply
243 {
244 public:
245         /** Initializes a new instance of the Fail class.
246          * @param Creator The module which created this instance.
247          */
248         Fail(Module* Creator)
249                 : Reply(Creator, "FAIL")
250         {
251         }
252 };
253
254 /** Sends a NOTE standard reply. */
255 class IRCv3::Replies::Note
256         : public IRCv3::Replies::Reply
257 {
258 public:
259         /** Initializes a new instance of the Note class.
260          * @param Creator The module which created this instance.
261          */
262         Note(Module* Creator)
263                 : Reply(Creator, "NOTE")
264         {
265         }
266 };
267
268 /** Sends a WARN standard reply. */
269 class IRCv3::Replies::Warn
270         : public IRCv3::Replies::Reply
271 {
272 public:
273         /** Initializes a new instance of the Warn class.
274          * @param Creator The module which created this instance.
275          */
276         Warn(Module* Creator)
277                 : Reply(Creator, "WARN")
278         {
279         }
280 };