]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/modules/ircv3_replies.h
Add support for the IRCv3 standard replies extension.
[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  protected:
49         /** Initializes a new instance of the Reply class.
50          * @param Creator The module which created this instance.
51          * @param Cmd The name of the command to reply with.
52          */
53         Reply(Module* Creator, const std::string& Cmd)
54                 : cmd(Cmd)
55                 , evprov(Creator, Cmd)
56         {
57         }
58
59  public:
60         /**
61          * Sends a standard reply to the specified user.
62          * @param user The user to send the reply to.
63          * @param command The command that the reply relates to.
64          * @param code A machine readable code for this reply.
65          * @param description A human readable description of this reply.
66          */
67         void Send(LocalUser* user, Command* command, const std::string& code, const std::string& description)
68         {
69                 ClientProtocol::Message msg(cmd.c_str(), ServerInstance->Config->ServerName);
70                 msg.PushParamRef(command->name);
71                 msg.PushParam(code);
72                 msg.PushParam(description);
73
74                 ClientProtocol::Event ev(evprov, msg);
75                 user->Send(ev);
76         }
77
78         /**
79          * Sends a standard reply to the specified user if they have the specified cap
80          * or a notice if they do not.s
81          * @param user The user to send the reply to.
82          * @param command The command that the reply relates to.
83          * @param code A machine readable code for this reply.
84          * @param description A human readable description of this reply.
85          */
86         void SendIfCap(LocalUser* user, const Cap::Capability& cap, Command* command, const std::string& code, const std::string& description)
87         {
88                 if (cap.get(user))
89                         Send(user, command, code, description);
90                 else
91                         user->WriteNotice(InspIRCd::Format("*** %s: %s", command->name.c_str(), description.c_str()));
92         }
93 };
94
95 /** Sends a FAIL standard reply. */
96 class IRCv3::Replies::Fail
97         : public IRCv3::Replies::Reply
98 {
99 public:
100         /** Initializes a new instance of the Fail class.
101          * @param Creator The module which created this instance.
102          */
103         Fail(Module* Creator)
104                 : Reply(Creator, "FAIL")
105         {
106         }
107 };
108
109 /** Sends a NOTE standard reply. */
110 class IRCv3::Replies::Note
111         : public IRCv3::Replies::Reply
112 {
113 public:
114         /** Initializes a new instance of the Note class.
115          * @param Creator The module which created this instance.
116          */
117         Note(Module* Creator)
118                 : Reply(Creator, "NOTE")
119         {
120         }
121 };
122
123 /** Sends a WARN standard reply. */
124 class IRCv3::Replies::Warn
125         : public IRCv3::Replies::Reply
126 {
127 public:
128         /** Initializes a new instance of the Warn class.
129          * @param Creator The module which created this instance.
130          */
131         Warn(Module* Creator)
132                 : Reply(Creator, "WARN")
133         {
134         }
135 };