2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2018 Sadie Powell <sadie@witchery.services>
5 * Copyright (C) 2013-2015 Attila Molnar <attilamolnar@hush.com>
7 * This file is part of InspIRCd. InspIRCd is free software: you can
8 * redistribute it and/or modify it under the terms of the GNU General Public
9 * License as published by the Free Software Foundation, version 2.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "treeserver.h"
26 class ProtocolException : public ModuleException
29 ProtocolException(const std::string& msg)
30 : ModuleException("Protocol violation: " + msg)
35 /** Base class for server-to-server commands that may have a (remote) user source or server source.
37 class ServerCommand : public CommandBase
40 ServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0);
42 /** Register this object in the ServerCommandManager
44 void RegisterService() CXX11_OVERRIDE;
46 virtual CmdResult Handle(User* user, Params& parameters) = 0;
47 RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE;
50 * Extract the TS from a string.
51 * @param tsstr The string containing the TS.
52 * @return The raw timestamp value.
53 * This function throws a ProtocolException if it considers the TS invalid. Note that the detection of
54 * invalid timestamps is not designed to be bulletproof, only some cases - like "0" - trigger an exception.
56 static time_t ExtractTS(const std::string& tsstr);
59 /** Base class for server-to-server command handlers which are only valid if their source is a user.
60 * When a server sends a command of this type and the source is a server (sid), the link is aborted.
63 class UserOnlyServerCommand : public ServerCommand
66 UserOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
67 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
69 CmdResult Handle(User* user, Params& parameters) CXX11_OVERRIDE
71 RemoteUser* remoteuser = IS_REMOTE(user);
73 throw ProtocolException("Invalid source");
74 return static_cast<T*>(this)->HandleRemote(remoteuser, parameters);
78 /** Base class for server-to-server command handlers which are only valid if their source is a server.
79 * When a server sends a command of this type and the source is a user (uuid), the link is aborted.
82 class ServerOnlyServerCommand : public ServerCommand
85 ServerOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
86 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
88 CmdResult Handle(User* user, CommandBase::Params& parameters) CXX11_OVERRIDE
91 throw ProtocolException("Invalid source");
92 TreeServer* server = TreeServer::Get(user);
93 return static_cast<T*>(this)->HandleServer(server, parameters);
97 class ServerCommandManager
99 typedef TR1NS::unordered_map<std::string, ServerCommand*> ServerCommandMap;
100 ServerCommandMap commands;
103 ServerCommand* GetHandler(const std::string& command) const;
104 bool AddCommand(ServerCommand* cmd);