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 virtual CmdResult Handle(User* user, std::vector<std::string>& parameters) = 0;
42 virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters);
45 * Extract the TS from a string.
46 * @param tsstr The string containing the TS.
47 * @return The raw timestamp value.
48 * This function throws a ProtocolException if it considers the TS invalid. Note that the detection of
49 * invalid timestamps is not designed to be bulletproof, only some cases - like "0" - trigger an exception.
51 static time_t ExtractTS(const std::string& tsstr);
54 /** Base class for server-to-server command handlers which are only valid if their source is a user.
55 * When a server sends a command of this type and the source is a server (sid), the link is aborted.
58 class UserOnlyServerCommand : public ServerCommand
61 UserOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
62 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
64 CmdResult Handle(User* user, std::vector<std::string>& parameters)
66 RemoteUser* remoteuser = IS_REMOTE(user);
68 throw ProtocolException("Invalid source");
69 return static_cast<T*>(this)->HandleRemote(remoteuser, parameters);
73 /** Base class for server-to-server command handlers which are only valid if their source is a server.
74 * When a server sends a command of this type and the source is a user (uuid), the link is aborted.
77 class ServerOnlyServerCommand : public ServerCommand
80 ServerOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
81 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
83 CmdResult Handle(User* user, std::vector<std::string>& parameters)
86 throw ProtocolException("Invalid source");
87 TreeServer* server = TreeServer::Get(user);
88 return static_cast<T*>(this)->HandleServer(server, parameters);
92 class ServerCommandManager
94 typedef TR1NS::unordered_map<std::string, ServerCommand*> ServerCommandMap;
95 ServerCommandMap commands;
98 ServerCommand* GetHandler(const std::string& command) const;
99 bool AddCommand(ServerCommand* cmd);