]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/servercommand.h
Split ProtocolInterface::SendMetaData() into multiple functions
[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
24 class TreeServer;
25
26 /** Base class for server-to-server commands that may have a (remote) user source or server source.
27  */
28 class ServerCommand : public CommandBase
29 {
30  public:
31         ServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0);
32
33         virtual CmdResult Handle(User* user, std::vector<std::string>& parameters) = 0;
34         virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters);
35 };
36
37 /** Base class for server-to-server command handlers which are only valid if their source is a user.
38  * When a server sends a command of this type and the source is a server (sid), the link is aborted.
39  */
40 template <class T>
41 class UserOnlyServerCommand : public ServerCommand
42 {
43  public:
44         UserOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
45                 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
46
47         CmdResult Handle(User* user, std::vector<std::string>& parameters)
48         {
49                 RemoteUser* remoteuser = IS_REMOTE(user);
50                 if (!remoteuser)
51                         return CMD_INVALID;
52                 return static_cast<T*>(this)->HandleRemote(remoteuser, parameters);
53         }
54 };
55
56 /** Base class for server-to-server command handlers which are only valid if their source is a server.
57  * When a server sends a command of this type and the source is a user (uuid), the link is aborted.
58  */
59 template <class T>
60 class ServerOnlyServerCommand : public ServerCommand
61 {
62  public:
63         ServerOnlyServerCommand(Module* Creator, const std::string& Name, unsigned int MinPara = 0, unsigned int MaxPara = 0)
64                 : ServerCommand(Creator, Name, MinPara, MaxPara) { }
65
66         CmdResult Handle(User* user, std::vector<std::string>& parameters)
67         {
68                 if (!IS_SERVER(user))
69                         return CMD_INVALID;
70                 TreeServer* server = Utils->FindServer(user->server);
71                 return static_cast<T*>(this)->HandleServer(server, parameters);
72         }
73 };
74
75 class ServerCommandManager
76 {
77         typedef TR1NS::unordered_map<std::string, ServerCommand*> ServerCommandMap;
78         ServerCommandMap commands;
79
80  public:
81         ServerCommand* GetHandler(const std::string& command) const;
82         bool AddCommand(ServerCommand* cmd);
83 };