]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/servercommand.h
m_spanningtree Rewrite PING logic to use Timers
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / servercommand.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2013 Attila Molnar <attilamolnar@hush.com>
5  *
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.
9  *
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
13  * details.
14  *
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/>.
17  */
18
19
20 #pragma once
21
22 #include "utils.h"
23 #include "treeserver.h"
24
25 class ProtocolException : public ModuleException
26 {
27  public:
28         ProtocolException(const std::string& msg)
29                 : ModuleException("Protocol violation: " + msg)
30         {
31         }
32 };
33
34 /** Base class for server-to-server commands that may have a (remote) user source or server source.
35  */
36 class ServerCommand : public CommandBase
37 {
38  public:
39         ServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0);
40
41         virtual CmdResult Handle(User* user, std::vector<std::string>& parameters) = 0;
42         virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters);
43
44         /**
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.
50          */
51         static time_t ExtractTS(const std::string& tsstr);
52 };
53
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.
56  */
57 template <class T>
58 class UserOnlyServerCommand : public ServerCommand
59 {
60  public:
61         UserOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
62                 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
63
64         CmdResult Handle(User* user, std::vector<std::string>& parameters)
65         {
66                 RemoteUser* remoteuser = IS_REMOTE(user);
67                 if (!remoteuser)
68                         throw ProtocolException("Invalid source");
69                 return static_cast<T*>(this)->HandleRemote(remoteuser, parameters);
70         }
71 };
72
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.
75  */
76 template <class T>
77 class ServerOnlyServerCommand : public ServerCommand
78 {
79  public:
80         ServerOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
81                 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
82
83         CmdResult Handle(User* user, std::vector<std::string>& parameters)
84         {
85                 if (!IS_SERVER(user))
86                         throw ProtocolException("Invalid source");
87                 TreeServer* server = TreeServer::Get(user);
88                 return static_cast<T*>(this)->HandleServer(server, parameters);
89         }
90 };
91
92 class ServerCommandManager
93 {
94         typedef TR1NS::unordered_map<std::string, ServerCommand*> ServerCommandMap;
95         ServerCommandMap commands;
96
97  public:
98         ServerCommand* GetHandler(const std::string& command) const;
99         bool AddCommand(ServerCommand* cmd);
100 };