EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CommandDispatcher.cpp
Go to the documentation of this file.
1 /*
2  ------------------------------------------------------------------------------------
3  LICENSE:
4  ------------------------------------------------------------------------------------
5  This file is part of EVEmu: EVE Online Server Emulator
6  Copyright 2006 - 2021 The EVEmu Team
7  For the latest information visit https://evemu.dev
8  ------------------------------------------------------------------------------------
9  This program is free software; you can redistribute it and/or modify it under
10  the terms of the GNU Lesser General Public License as published by the Free Software
11  Foundation; either version 2 of the License, or (at your option) any later
12  version.
13 
14  This program is distributed in the hope that it will be useful, but WITHOUT
15  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
17 
18  You should have received a copy of the GNU Lesser General Public License along with
19  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20  Place - Suite 330, Boston, MA 02111-1307, USA, or go to
21  http://www.gnu.org/copyleft/lesser.txt.
22  ------------------------------------------------------------------------------------
23  Author: Zhur
24  Updates: Allan
25 */
26 
27 #include "eve-server.h"
28 
29 #include "Client.h"
30 #include "PyCallable.h"
32 #include "system/DestinyManager.h"
33 
35 : m_services( services )
36 {
37  m_commands.clear();
38 }
39 
41  for (auto cur : m_commands)
42  SafeDelete(cur.second);
43  m_commands.clear();
44 }
45 
46 PyResult CommandDispatcher::Execute( Client* from, const char* msg )
47 {
58  Seperator sep( &msg[1] );
59  if (!sep.argCount()) {
60  //empty command, return list of commands
61  std::string reason = "Commands: ";
62 
63  std::map<std::string, CommandRecord *>::const_iterator cur = m_commands.begin();
64  reason += "[";
65  for(; cur != m_commands.end(); cur++)
66  reason += "'" + cur->second->command + "',";
67  reason += "]";
68 
69  throw UserError ("").AddFormatValue ("reason", new PyString (reason));
70  }
71 
72  std::map<std::string, CommandRecord*>::const_iterator itr = m_commands.find( sep.arg( 0 ) );
73  if (m_commands.end() == itr ) {
74  _log(COMMAND__ERROR, "Unable to find command '%s' for %s", sep.arg( 0 ).c_str(), from->GetName() );
75  throw CustomError ("Unknown command '%s'", sep.arg (0).c_str ());
76  }
77 
78  CommandRecord* rec = itr->second;
79 
80  _log(COMMAND__INFO, "Request access to command '%s' with role %p for '%s' with role %p.", rec->command.c_str(), rec->required_role, from->GetName(), from->GetAccountRole() );
81  if ((from->GetAccountRole() & rec->required_role) != rec->required_role) {
82  _log(COMMAND__ERROR, "Access denied to %s for command '%s'. --have role %p, need role %p", from->GetName(), rec->command.c_str(), from->GetAccountRole(), rec->required_role );
83  throw CustomError ("Access denied to command '%s'", sep.arg (0).c_str ());
84  }
85 
86  return ( *rec->function )( from, &m_db, &m_services, sep );
87 }
88 
89 void CommandDispatcher::AddCommand( const char* cmd, const char* desc, int64 required_role, CommandFunc function )
90 {
91  std::map<std::string, CommandRecord*>::iterator itr = m_commands.find( cmd );
92  if (itr != m_commands.end())
93  SafeDelete( itr->second );
94 
95  m_commands[cmd] = new CommandRecord( cmd, desc, required_role, function );
96 }
97 
99  sLog.Green(" EVEmu", "Currently Loaded %u Commands:", m_commands.size());
100  std::map<std::string, CommandDispatcher::CommandRecord*>::iterator itr = m_commands.begin();
101  for (; itr != m_commands.end(); ++itr) {
102  sLog.Magenta(" Call and Role", "%s - %p (%li)",
103  itr->first.c_str(), itr->second->required_role, itr->second->required_role);
104  }
105 }
106 
108  for (auto cur : m_commands)
109  SafeDelete(cur.second);
110  m_commands.clear();
111 }
int64 GetAccountRole() const
Definition: Client.h:118
#define _log(type, fmt,...)
Definition: logsys.h:124
const std::string & arg(size_t index) const
Definition: Seperator.h:43
Python string.
Definition: PyRep.h:430
CommandDispatcher(PyServiceMgr &services)
PyResult Execute(Client *from, const char *msg)
UserError & AddFormatValue(const char *name, PyRep *value)
Fluent version of the protected AddKeyword, allows for adding a keyword to the exception.
Separates string to arguments.
Definition: Seperator.h:36
Advanced version of UserError that allows to send a full custom message.
Definition: PyExceptions.h:453
void SafeDelete(T *&p)
Deletes and nullifies a pointer.
Definition: SafeMem.h:83
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
size_t argCount() const
Definition: Seperator.h:44
void AddCommand(const char *cmd, const char *desc, int64 required_role, CommandFunc function)
std::map< std::string, CommandRecord * > m_commands
PyServiceMgr & m_services
const char * GetName() const
Definition: Client.h:94
Python object "ccp_exceptions.UserError".
Definition: PyExceptions.h:121
Definition: Client.h:66
signed __int64 int64
Definition: eve-compat.h:51