2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
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.
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
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/>.
23 #include "treeserver.h"
25 class ProtocolException : public ModuleException
28 ProtocolException(const std::string& msg)
29 : ModuleException("Protocol violation: " + msg)
34 /** Base class for server-to-server commands that may have a (remote) user source or server source.
36 class ServerCommand : public CommandBase
39 ServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0);
41 /** Register this object in the ServerCommandManager
43 void RegisterService() CXX11_OVERRIDE;
45 virtual CmdResult Handle(User* user, std::vector<std::string>& parameters) = 0;
46 virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters) CXX11_OVERRIDE;
49 * Extract the TS from a string.
50 * @param tsstr The string containing the TS.
51 * @return The raw timestamp value.
52 * This function throws a ProtocolException if it considers the TS invalid. Note that the detection of
53 * invalid timestamps is not designed to be bulletproof, only some cases - like "0" - trigger an exception.
55 static time_t ExtractTS(const std::string& tsstr);
58 /** Base class for server-to-server command handlers which are only valid if their source is a user.
59 * When a server sends a command of this type and the source is a server (sid), the link is aborted.
62 class UserOnlyServerCommand : public ServerCommand
65 UserOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
66 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
68 CmdResult Handle(User* user, std::vector<std::string>& parameters)
70 RemoteUser* remoteuser = IS_REMOTE(user);
72 throw ProtocolException("Invalid source");
73 return static_cast<T*>(this)->HandleRemote(remoteuser, parameters);
77 /** Base class for server-to-server command handlers which are only valid if their source is a server.
78 * When a server sends a command of this type and the source is a user (uuid), the link is aborted.
81 class ServerOnlyServerCommand : public ServerCommand
84 ServerOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
85 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
87 CmdResult Handle(User* user, std::vector<std::string>& parameters)
90 throw ProtocolException("Invalid source");
91 TreeServer* server = TreeServer::Get(user);
92 return static_cast<T*>(this)->HandleServer(server, parameters);
96 class ServerCommandManager
98 typedef TR1NS::unordered_map<std::string, ServerCommand*> ServerCommandMap;
99 ServerCommandMap commands;
102 ServerCommand* GetHandler(const std::string& command) const;
103 bool AddCommand(ServerCommand* cmd);