InspIRCd  3.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 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]>
9  * Copyright (C) 2007-2008 Robin Burchell <[email protected]>
10  * Copyright (C) 2007 Dennis Friis <[email protected]>
11  * Copyright (C) 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 CmdResult
31 {
33  CMD_FAILURE = 0,
34 
36  CMD_SUCCESS = 1,
37 
38  /* The command does not exist. */
39  CMD_INVALID = 2
40 };
41 
43 const char FLAG_SERVERONLY = 7; // technically anything nonzero below 'A' works
44 
50 enum TranslateType
51 {
52  TR_TEXT, /* Raw text, leave as-is */
53  TR_NICK, /* Nickname, translate to UUID for server->server */
54  TR_CUSTOM /* Custom translation handled by EncodeParameter/DecodeParameter */
55 };
56 
62 enum RouteType
63 {
64  ROUTE_TYPE_LOCALONLY,
65  ROUTE_TYPE_BROADCAST,
66  ROUTE_TYPE_UNICAST,
67  ROUTE_TYPE_MESSAGE,
68  ROUTE_TYPE_OPT_BCAST,
69  ROUTE_TYPE_OPT_UCAST
70 };
71 
76 {
79  RouteType type;
82  std::string serverdest;
83 
87 
90  RouteDescriptor(RouteType t, const std::string &d)
91  : type(t), serverdest(d), server(NULL) { }
92 
93  RouteDescriptor(RouteType t, Server* srv)
94  : type(t), server(srv) { }
95 };
96 
98 #define ROUTE_LOCALONLY (RouteDescriptor(ROUTE_TYPE_LOCALONLY, ""))
99 
100 #define ROUTE_BROADCAST (RouteDescriptor(ROUTE_TYPE_BROADCAST, ""))
101 
102 #define ROUTE_UNICAST(x) (RouteDescriptor(ROUTE_TYPE_UNICAST, x))
103 
104 #define ROUTE_MESSAGE(x) (RouteDescriptor(ROUTE_TYPE_MESSAGE, x))
105 
106 #define ROUTE_OPT_BCAST (RouteDescriptor(ROUTE_TYPE_OPT_BCAST, ""))
107 
108 #define ROUTE_OPT_UCAST(x) (RouteDescriptor(ROUTE_TYPE_OPT_UCAST, x))
109 
113 class CoreExport CommandBase : public ServiceProvider
114 {
115  public:
117  class Params : public std::vector<std::string>
118  {
119  private:
120  /* IRCv3 message tags. */
122 
123  public:
128  Params(const std::vector<std::string>& paramsref, const ClientProtocol::TagMap& tagsref)
129  : std::vector<std::string>(paramsref)
130  , tags(tagsref)
131  {
132  }
133 
138  template<typename Iterator>
139  Params(Iterator first, Iterator last)
140  : std::vector<std::string>(first, last)
141  {
142  }
143 
145  Params() { }
146 
148  const ClientProtocol::TagMap& GetTags() const { return tags; }
149  };
150 
153  unsigned char flags_needed;
154 
157  const unsigned int min_params;
158 
163  const unsigned int max_params;
164 
167  unsigned long use_count;
168 
172 
181 
185  std::string syntax;
186 
190  std::vector<TranslateType> translation;
191 
194  unsigned int Penalty;
195 
203  CommandBase(Module* me, const std::string& cmd, unsigned int minpara = 0, unsigned int maxpara = 0);
204 
205  virtual RouteDescriptor GetRouting(User* user, const CommandBase::Params& parameters);
206 
212  virtual void EncodeParameter(std::string& parameter, unsigned int index);
213 
217  {
218  return works_before_reg;
219  }
220 
221  virtual ~CommandBase();
222 };
223 
224 class CoreExport Command : public CommandBase
225 {
226  public:
231 
232  Command(Module* me, const std::string& cmd, unsigned int minpara = 0, unsigned int maxpara = 0);
233 
239  virtual CmdResult Handle(User* user, const Params& parameters) = 0;
240 
243  void RegisterService() CXX11_OVERRIDE;
244 
248  ~Command();
249 };
250 
251 class CoreExport SplitCommand : public Command
252 {
253  public:
254  SplitCommand(Module* me, const std::string &cmd, unsigned int minpara = 0, unsigned int maxpara = 0)
255  : Command(me, cmd, minpara, maxpara) {}
256  CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
257  virtual CmdResult HandleLocal(LocalUser* user, const Params& parameters);
258  virtual CmdResult HandleRemote(RemoteUser* user, const Params& parameters);
259  virtual CmdResult HandleServer(FakeUser* user, const Params& parameters);
260 };
261 
264 #define TRANSLATE1(x1) translation.push_back(x1);
265 #define TRANSLATE2(x1,x2) translation.push_back(x1);translation.push_back(x2);
266 #define TRANSLATE3(x1,x2,x3) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);
267 #define TRANSLATE4(x1,x2,x3,x4) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);
268 #define TRANSLATE5(x1,x2,x3,x4,x5) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
269  translation.push_back(x5);
270 #define TRANSLATE6(x1,x2,x3,x4,x5,x6) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
271  translation.push_back(x5);translation.push_back(x6);
272 #define TRANSLATE7(x1,x2,x3,x4,x5,x6,x7) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
273  translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);
274 #define TRANSLATE8(x1,x2,x3,x4,x5,x6,x7,x8) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
275  translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);translation.push_back(x8);
RouteDescriptor::type
RouteType type
Definition: ctables.h:79
CommandBase::allow_empty_last_param
bool allow_empty_last_param
Definition: ctables.h:180
FakeUser
Definition: users.h:908
CommandBase::works_before_reg
bool works_before_reg
Definition: ctables.h:171
CommandBase::Penalty
unsigned int Penalty
Definition: ctables.h:194
ServiceProvider
Definition: base.h:249
CommandBase::flags_needed
unsigned char flags_needed
Definition: ctables.h:153
RemoteUser
Definition: users.h:900
Server
Definition: server.h:24
insp::flat_map< std::string, MessageTagData, std::greater< std::string > >
RouteDescriptor
Definition: ctables.h:75
RouteDescriptor::RouteDescriptor
RouteDescriptor(RouteType t, const std::string &d)
Definition: ctables.h:90
CommandBase::Params::GetTags
const ClientProtocol::TagMap & GetTags() const
Definition: ctables.h:148
CommandBase::Params::Params
Params(Iterator first, Iterator last)
Definition: ctables.h:139
CommandBase::WorksBeforeReg
bool WorksBeforeReg()
Definition: ctables.h:216
ServiceProvider::RegisterService
virtual void RegisterService()
Definition: base.cpp:102
CommandBase::Params::Params
Params()
Definition: ctables.h:145
CommandBase
Definition: ctables.h:113
CommandBase::max_params
const unsigned int max_params
Definition: ctables.h:163
CommandBase::syntax
std::string syntax
Definition: ctables.h:185
RouteDescriptor::serverdest
std::string serverdest
Definition: ctables.h:82
RouteDescriptor::server
Server * server
Definition: ctables.h:86
CommandBase::use_count
unsigned long use_count
Definition: ctables.h:167
CommandBase::Params::Params
Params(const std::vector< std::string > &paramsref, const ClientProtocol::TagMap &tagsref)
Definition: ctables.h:128
Module
Definition: modules.h:247
Command::force_manual_route
bool force_manual_route
Definition: ctables.h:230
User
Definition: users.h:233
CommandBase::translation
std::vector< TranslateType > translation
Definition: ctables.h:190
Command
Definition: ctables.h:224
SplitCommand
Definition: ctables.h:251
CommandBase::Params
Definition: ctables.h:117
CommandBase::min_params
const unsigned int min_params
Definition: ctables.h:157
LocalUser
Definition: users.h:727