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