]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/servercommand.h
Remove m_silence pending a complete rewrite.
[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         /** Register this object in the ServerCommandManager
42          */
43         void RegisterService() CXX11_OVERRIDE;
44
45         virtual CmdResult Handle(User* user, Params& parameters) = 0;
46         RouteDescriptor GetRouting(User* user, const Params& parameters) CXX11_OVERRIDE;
47
48         /**
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.
54          */
55         static time_t ExtractTS(const std::string& tsstr);
56 };
57
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.
60  */
61 template <class T>
62 class UserOnlyServerCommand : public ServerCommand
63 {
64  public:
65         UserOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
66                 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
67
68         CmdResult Handle(User* user, Params& parameters) CXX11_OVERRIDE
69         {
70                 RemoteUser* remoteuser = IS_REMOTE(user);
71                 if (!remoteuser)
72                         throw ProtocolException("Invalid source");
73                 return static_cast<T*>(this)->HandleRemote(remoteuser, parameters);
74         }
75 };
76
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.
79  */
80 template <class T>
81 class ServerOnlyServerCommand : public ServerCommand
82 {
83  public:
84         ServerOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
85                 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
86
87         CmdResult Handle(User* user, CommandBase::Params& parameters) CXX11_OVERRIDE
88         {
89                 if (!IS_SERVER(user))
90                         throw ProtocolException("Invalid source");
91                 TreeServer* server = TreeServer::Get(user);
92                 return static_cast<T*>(this)->HandleServer(server, parameters);
93         }
94 };
95
96 class ServerCommandManager
97 {
98         typedef TR1NS::unordered_map<std::string, ServerCommand*> ServerCommandMap;
99         ServerCommandMap commands;
100
101  public:
102         ServerCommand* GetHandler(const std::string& command) const;
103         bool AddCommand(ServerCommand* cmd);
104 };