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