]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/ircv3_replies.h
Remove the Kiwi links from the readme.
[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->GetServerName());
94                 if (command)
95                         msg.PushParamRef(command->name);
96                 else
97                         msg.PushParam("*");
98                 msg.PushParam(code);
99                 msg.PushParam(description);
100                 SendInternal(user, msg);
101         }
102
103         template<typename T1>
104         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const std::string& description)
105         {
106                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->GetServerName());
107                 if (command)
108                         msg.PushParamRef(command->name);
109                 else
110                         msg.PushParam("*");
111                 msg.PushParam(code);
112                 msg.PushParam(ConvToStr(p1));
113                 msg.PushParam(description);
114                 SendInternal(user, msg);
115         }
116
117         template<typename T1, typename T2>
118         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
119                 const std::string& description)
120         {
121                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->GetServerName());
122                 if (command)
123                         msg.PushParamRef(command->name);
124                 else
125                         msg.PushParam("*");
126                 msg.PushParam(code);
127                 msg.PushParam(ConvToStr(p1));
128                 msg.PushParam(ConvToStr(p2));
129                 msg.PushParam(description);
130                 SendInternal(user, msg);
131         }
132
133         template<typename T1, typename T2, typename T3>
134         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
135                 const T3& p3, const std::string& description)
136         {
137                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->GetServerName());
138                 if (command)
139                         msg.PushParamRef(command->name);
140                 else
141                         msg.PushParam("*");
142                 msg.PushParam(code);
143                 msg.PushParam(ConvToStr(p1));
144                 msg.PushParam(ConvToStr(p2));
145                 msg.PushParam(ConvToStr(p3));
146                 msg.PushParam(description);
147                 SendInternal(user, msg);
148         }
149
150         template<typename T1, typename T2, typename T3, typename T4>
151         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
152                 const T3& p3, const T4& p4, const std::string& description)
153         {
154                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->GetServerName());
155                 if (command)
156                         msg.PushParamRef(command->name);
157                 else
158                         msg.PushParam("*");
159                 msg.PushParam(code);
160                 msg.PushParam(ConvToStr(p1));
161                 msg.PushParam(ConvToStr(p2));
162                 msg.PushParam(ConvToStr(p3));
163                 msg.PushParam(ConvToStr(p4));
164                 msg.PushParam(description);
165                 SendInternal(user, msg);
166         }
167
168         template<typename T1, typename T2, typename T3, typename T4, typename T5>
169         void Send(LocalUser* user, Command* command, const std::string& code, const T1& p1, const T2& p2,
170                 const T3& p3, const T4& p4, const T5& p5, const std::string& description)
171         {
172                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->GetServerName());
173                 if (command)
174                         msg.PushParamRef(command->name);
175                 else
176                         msg.PushParam("*");
177                 msg.PushParam(code);
178                 msg.PushParam(ConvToStr(p1));
179                 msg.PushParam(ConvToStr(p2));
180                 msg.PushParam(ConvToStr(p3));
181                 msg.PushParam(ConvToStr(p4));
182                 msg.PushParam(ConvToStr(p5));
183                 msg.PushParam(description);
184                 SendInternal(user, msg);
185         }
186
187         /**
188          * Sends a standard reply to the specified user if they have the specified cap
189          * or a notice if they do not.
190          * @param user The user to send the reply to.
191          * @param command The command that the reply relates to.
192          * @param code A machine readable code for this reply.
193          * @param description A human readable description of this reply.
194          */
195         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
196                 const std::string& description)
197         {
198                 if (cap.get(user))
199                         Send(user, command, code, description);
200                 else
201                         SendNoticeInternal(user, command, description);
202         }
203
204         template<typename T1>
205         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
206                 const T1& p1, const std::string& description)
207         {
208                 if (cap.get(user))
209                         Send(user, command, code, p1, description);
210                 else
211                         SendNoticeInternal(user, command, description);
212         }
213
214         template<typename T1, typename T2>
215         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
216                 const T1& p1, const T2& p2, const std::string& description)
217         {
218                 if (cap.get(user))
219                         Send(user, command, code, p1, p2, description);
220                 else
221                         SendNoticeInternal(user, command, description);
222         }
223
224         template<typename T1, typename T2, typename T3>
225         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
226                 const T1& p1, const T2& p2, const T3& p3, const std::string& description)
227         {
228                 if (cap.get(user))
229                         Send(user, command, code, p1, p2, p3, description);
230                 else
231                         SendNoticeInternal(user, command, description);
232         }
233
234         template<typename T1, typename T2, typename T3, typename T4>
235         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
236                 const T1& p1, const T2& p2, const T3& p3, const T4& p4, const std::string& description)
237         {
238                 if (cap.get(user))
239                         Send(user, command, code, p1, p2, p3, p4, description);
240                 else
241                         SendNoticeInternal(user, command, description);
242         }
243
244         template<typename T1, typename T2, typename T3, typename T4, typename T5>
245         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code,
246                 const T1& p1, const T2& p2, const T3& p3, const T4& p4, const T5& p5, const std::string& description)
247         {
248                 if (cap.get(user))
249                         Send(user, command, code, p1, p2, p3, p4, p5, description);
250                 else
251                         SendNoticeInternal(user, command, description);
252         }
253 };
254
255 /** Sends a FAIL standard reply. */
256 class IRCv3::Replies::Fail
257         : public IRCv3::Replies::Reply
258 {
259 public:
260         /** Initializes a new instance of the Fail class.
261          * @param Creator The module which created this instance.
262          */
263         Fail(Module* Creator)
264                 : Reply(Creator, "FAIL")
265         {
266         }
267 };
268
269 /** Sends a NOTE standard reply. */
270 class IRCv3::Replies::Note
271         : public IRCv3::Replies::Reply
272 {
273 public:
274         /** Initializes a new instance of the Note class.
275          * @param Creator The module which created this instance.
276          */
277         Note(Module* Creator)
278                 : Reply(Creator, "NOTE")
279         {
280         }
281 };
282
283 /** Sends a WARN standard reply. */
284 class IRCv3::Replies::Warn
285         : public IRCv3::Replies::Reply
286 {
287 public:
288         /** Initializes a new instance of the Warn class.
289          * @param Creator The module which created this instance.
290          */
291         Warn(Module* Creator)
292                 : Reply(Creator, "WARN")
293         {
294         }
295 };