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