InspIRCd  4.0
ctables.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  * Copyright (C) 2012-2015, 2018 Attila Molnar <[email protected]>
5  * Copyright (C) 2012-2013, 2017-2018, 2020 Sadie Powell <[email protected]>
6  * Copyright (C) 2012 Robby <[email protected]>
7  * Copyright (C) 2009-2010 Daniel De Graaf <[email protected]>
8  * Copyright (C) 2008 Thomas Stagner <[email protected]ircd.org>
9  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
10  * Copyright (C) 2007 Dennis Friis <[email protected]>
11  * Copyright (C) 2003, 2006-2010 Craig Edwards <[email protected]>
12  *
13  * This file is part of InspIRCd. InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 
27 #pragma once
28 
30 enum class CmdAccess : uint8_t
31 {
32  /* The command has no special attributes. */
33  NORMAL = 0,
34 
36  OPERATOR = 1,
37 
39  SERVER = 2,
40 };
41 
43 enum class CmdResult : uint8_t
44 {
46  SUCCESS = 0,
47 
49  FAILURE = 1,
50 
51  /* The command does not exist. */
52  INVALID = 2,
53 };
54 
60 enum TranslateType
61 {
62  TR_TEXT, /* Raw text, leave as-is */
63  TR_NICK, /* Nickname, translate to UUID for server->server */
64  TR_CUSTOM /* Custom translation handled by EncodeParameter/DecodeParameter */
65 };
66 
72 enum RouteType
73 {
74  ROUTE_TYPE_LOCALONLY,
75  ROUTE_TYPE_BROADCAST,
76  ROUTE_TYPE_UNICAST,
77  ROUTE_TYPE_MESSAGE,
78  ROUTE_TYPE_OPT_BCAST,
79  ROUTE_TYPE_OPT_UCAST
80 };
81 
86 {
89  RouteType type;
92  std::string serverdest;
93 
97 
100  RouteDescriptor(RouteType t, const std::string &d)
101  : type(t), serverdest(d), server(NULL) { }
102 
103  RouteDescriptor(RouteType t, Server* srv)
104  : type(t), server(srv) { }
105 };
106 
108 #define ROUTE_LOCALONLY (RouteDescriptor(ROUTE_TYPE_LOCALONLY, ""))
110 #define ROUTE_BROADCAST (RouteDescriptor(ROUTE_TYPE_BROADCAST, ""))
112 #define ROUTE_UNICAST(x) (RouteDescriptor(ROUTE_TYPE_UNICAST, x))
114 #define ROUTE_MESSAGE(x) (RouteDescriptor(ROUTE_TYPE_MESSAGE, x))
116 #define ROUTE_OPT_BCAST (RouteDescriptor(ROUTE_TYPE_OPT_BCAST, ""))
118 #define ROUTE_OPT_UCAST(x) (RouteDescriptor(ROUTE_TYPE_OPT_UCAST, x))
119 
123 class CoreExport CommandBase : public ServiceProvider
124 {
125  public:
127  class Params : public std::vector<std::string>
128  {
129  private:
130  /* IRCv3 message tags. */
132 
133  public:
138  Params(const std::vector<std::string>& paramsref, const ClientProtocol::TagMap& tagsref)
139  : std::vector<std::string>(paramsref)
140  , tags(tagsref)
141  {
142  }
143 
148  template<typename Iterator>
149  Params(Iterator first, Iterator last)
150  : std::vector<std::string>(first, last)
151  {
152  }
153 
155  Params() = default;
156 
158  const ClientProtocol::TagMap& GetTags() const { return tags; }
159  ClientProtocol::TagMap& GetTags() { return tags; }
160  };
161 
164  const unsigned int min_params;
165 
170  const unsigned int max_params;
171 
179  bool allow_empty_last_param = true;
180 
184  std::vector<TranslateType> translation;
185 
193  CommandBase(Module* me, const std::string& cmd, unsigned int minpara = 0, unsigned int maxpara = 0);
194 
195  virtual RouteDescriptor GetRouting(User* user, const CommandBase::Params& parameters);
196 
202  virtual void EncodeParameter(std::string& parameter, unsigned int index);
203 
204  virtual ~CommandBase() = default;
205 };
206 
207 class CoreExport Command : public CommandBase
208 {
209  protected:
216  Command(Module* me, const std::string& cmd, unsigned int minpara = 0, unsigned int maxpara = 0);
217 
218  public:
220  ~Command() override;
221 
223  CmdAccess access_needed = CmdAccess::NORMAL;
224 
226  bool force_manual_route = false;
227 
229  unsigned int Penalty = 1;
230 
232  unsigned long use_count = 0;
233 
235  std::vector<std::string> syntax;
236 
238  bool works_before_reg = false;
239 
246  virtual CmdResult Handle(User* user, const Params& parameters) = 0;
247 
249  void RegisterService() override;
250 
255  virtual void TellNotEnoughParameters(LocalUser* user, const Params& parameters);
256 
261  virtual void TellNotRegistered(LocalUser* user, const Params& parameters);
262 };
263 
264 class CoreExport SplitCommand : public Command
265 {
266 protected:
273  SplitCommand(Module* me, const std::string& cmd, unsigned int minpara = 0, unsigned int maxpara = 0);
274 
275  public:
277  CmdResult Handle(User* user, const Params& parameters) override;
278 
285  virtual CmdResult HandleLocal(LocalUser* user, const Params& parameters);
286 
293  virtual CmdResult HandleRemote(RemoteUser* user, const Params& parameters);
294 
301  virtual CmdResult HandleServer(FakeUser* user, const Params& parameters);
302 };
Definition: ctables.h:128
const ClientProtocol::TagMap & GetTags() const
Definition: ctables.h:158
Params(const std::vector< std::string > &paramsref, const ClientProtocol::TagMap &tagsref)
Definition: ctables.h:138
Params(Iterator first, Iterator last)
Definition: ctables.h:149
Definition: ctables.h:124
std::vector< TranslateType > translation
Definition: ctables.h:184
const unsigned int min_params
Definition: ctables.h:164
const unsigned int max_params
Definition: ctables.h:170
Definition: ctables.h:208
std::vector< std::string > syntax
Definition: ctables.h:235
virtual CmdResult Handle(User *user, const Params &parameters)=0
Definition: users.h:841
Definition: users.h:658
Definition: modules.h:286
Definition: users.h:832
Definition: server.h:25
Definition: base.h:204
virtual void RegisterService()
Definition: base.cpp:65
Definition: ctables.h:265
Definition: users.h:238
Definition: ctables.h:86
std::string serverdest
Definition: ctables.h:92
RouteType type
Definition: ctables.h:89
RouteDescriptor(RouteType t, const std::string &d)
Definition: ctables.h:100
Server * server
Definition: ctables.h:96