EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
APIServiceManager.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: Aknor Jaden
24 */
25 
26 #include "eve-server.h"
27 
29 
31 : m_services(services)
32 {
33  _pXmlDocOuterTag = NULL;
34  _pXmlElementStack = NULL;
36 }
37 
38 std::tr1::shared_ptr<std::string> APIServiceManager::ProcessCall(const APICommandCall * pAPICommandCall)
39 {
40  sLog.Debug("APIServiceManager::ProcessCall()", "EVEmu API - Default Service Manager");
41 
42  // EXAMPLE OF USING ALL FEATURES OF THE INHERITED APISERVICEMANAGER XML BUILDING HELPER FUNCTIONS:
43  /*
44  std::vector<std::string> rowset;
45  _BuildXMLHeader();
46  {
47  _BuildErrorXMLTag( "500", "EVEmu: Invalid Service Manager Specified." );
48 
49  _BuildXMLTag( "attributes" );
50  {
51  _BuildXMLTag( "Aknor_Jaden" );
52  {
53  _BuildSingleXMLTag( "intelligence", "6" );
54  _BuildSingleXMLTag( "memory", "4" );
55  _BuildSingleXMLTag( "charisma", "7" );
56  _BuildSingleXMLTag( "perception", "12" );
57  _BuildSingleXMLTag( "willpower", "10" );
58  }
59  _CloseXMLTag(); // close "Aknor_Jaden" tag
60  }
61  _CloseXMLTag(); // close "attributes" tag
62 
63  rowset.push_back("typeID");
64  rowset.push_back("skillpoints");
65  rowset.push_back("level");
66  rowset.push_back("published");
67  _BuildXMLRowSet( "skills", "typeID", &rowset );
68  {
69  rowset.clear();
70  rowset.push_back("3431");
71  rowset.push_back("8000");
72  rowset.push_back("3");
73  rowset.push_back("1");
74  _BuildXMLRow( &rowset );
75  rowset.clear();
76  rowset.push_back("3413");
77  rowset.push_back("8000");
78  rowset.push_back("3");
79  rowset.push_back("1");
80  _BuildXMLRow( &rowset );
81  rowset.clear();
82  rowset.push_back("21059");
83  rowset.push_back("500");
84  rowset.push_back("1");
85  rowset.push_back("1");
86  _BuildXMLRow( &rowset );
87  rowset.clear();
88  rowset.push_back("3416");
89  rowset.push_back("8000");
90  rowset.push_back("3");
91  rowset.push_back("1");
92  _BuildXMLRow( &rowset );
93  rowset.clear();
94  rowset.push_back("3445");
95  rowset.push_back("512000");
96  rowset.push_back("5");
97  rowset.push_back("0");
98  _BuildXMLRow( &rowset );
99  }
100  _CloseXMLRowSet(); // close rowset "skils"
101 
102  _BuildXMLTag( "attributeEnhancers" );
103  {
104  _BuildXMLTag( "memoryBonus" );
105  {
106  _BuildSingleXMLTag( "augmentatorName", "Memory Augmentation - Basic" );
107  _BuildSingleXMLTag( "augmentatorValue", "3" );
108  }
109  _CloseXMLTag(); // close tag "memoryBonus"
110  _BuildXMLTag( "perceptionBonus" );
111  {
112  _BuildSingleXMLTag( "augmentatorName", "Ocular Filter - Basic" );
113  _BuildSingleXMLTag( "augmentatorValue", "5" );
114  }
115  _CloseXMLTag(); // close tag "perceptionBonus"
116  }
117  _CloseXMLTag(); // close tag "attributeEnhancers"
118  }
119  _CloseXMLHeader( API_CACHE_STYLE_MODIFIED );
120  */
121 
122  _BuildXMLHeader();
123  {
124  _BuildErrorXMLTag( "500", "EVEmu: Invalid Service Manager Specified." );
125  }
127 
128  return _GetXMLDocumentString();
129 }
130 
131 std::tr1::shared_ptr<std::string> APIServiceManager::BuildErrorXMLResponse(std::string errorCode, std::string errorMessage)
132 {
133  _BuildXMLHeader();
134  {
135  _BuildErrorXMLTag( errorCode, errorMessage );
136  }
138 
139  return _GetXMLDocumentString();
140 }
141 
142 bool APIServiceManager::_AuthenticateUserNamePassword(std::string username, std::string password)
143 {
144  // Query account info
145  AccountInfo account_info;
146  if( !services().serviceDB().GetAccountInformation(
147  username.c_str(),
148  password.c_str(),
149  account_info ) )
150  return false;
151 
152  // Compute pass hash
153  std::string passHash;
155  username, password, passHash ) )
156  return false;
157 
158  // Compare the hashes
159  return passHash == account_info.hash;
160 }
161 
162 bool APIServiceManager::_AuthenticateFullAPIQuery(std::string userID, std::string apiKey)
163 {
164  std::string apiFullKey;
165  std::string apiLimitedKey;
166  uint32 apiRole;
167 
168  bool status = m_db.GetApiAccountInfoUsingUserID(userID, &apiFullKey, &apiLimitedKey, &apiRole);
169 
170  if( (apiKey.compare( apiFullKey )) && (status) )
171  return true;
172  else
173  return false;
174 }
175 
176 bool APIServiceManager::_AuthenticateLimitedAPIQuery(std::string userID, std::string apiKey)
177 {
178  std::string apiFullKey;
179  std::string apiLimitedKey;
180  uint32 apiRole;
181 
182  bool status = m_db.GetApiAccountInfoUsingUserID(userID, &apiFullKey, &apiLimitedKey, &apiRole);
183 
184  if( (apiKey.compare( apiLimitedKey )) && (status) )
185  return true;
186  else
187  return false;
188 }
189 
191 {
192  // Build header at beginning of XML document, so clear existing xml document
193  _XmlDoc.Clear();
194  // object pointed to by '_pXmlDocOuterTag' is automatically deleted by the TinyXML system with the above call
195  if( _pXmlElementStack != NULL )
196  {
197  delete _pXmlElementStack;
198  _pXmlElementStack = NULL;
199  }
200  _pXmlElementStack = new std::stack<TiXmlElement *>();
201 
202  TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "" );
203  _XmlDoc.LinkEndChild( decl );
204 
205  _pXmlDocOuterTag = new TiXmlElement( "eveapi" );
206  _XmlDoc.LinkEndChild( _pXmlDocOuterTag );
207  _pXmlDocOuterTag->SetAttribute( "version", "2" );
208 
209  TiXmlElement * currentTime = new TiXmlElement( "currentTime" );
210  currentTime->LinkEndChild( new TiXmlText( Win32TimeToString(Win32TimeNow()).c_str() ));
211  _pXmlDocOuterTag->LinkEndChild( currentTime );
212 }
213 
215 {
216  switch( cacheStyle )
217  {
219  // 2 hour cache timer
220  _BuildSingleXMLTag( "cachedUntil", Win32TimeToString(Win32TimeNow() + 120*Win32Time_Minute).c_str() );
221  break;
223  // 5 minute cache timer
224  _BuildSingleXMLTag( "cachedUntil", Win32TimeToString(Win32TimeNow() + 5*Win32Time_Minute).c_str() );
225  break;
227  // 15 minute cache timer
228  _BuildSingleXMLTag( "cachedUntil", Win32TimeToString(Win32TimeNow() + 15*Win32Time_Minute).c_str() );
229  break;
230  }
231 }
232 
233 void APIServiceManager::_BuildXMLRowSet(std::string name, std::string key, const std::vector<std::string> * columns)
234 {
235  TiXmlElement * rowset = new TiXmlElement( "rowset" );
236 
237  std::vector<std::string>::const_iterator current, end;
238  rowset->SetAttribute( "name", name.c_str() );
239  rowset->SetAttribute( "key", key.c_str() );
240  current = columns->begin();
241  end = columns->end();
242  _CurrentRowSetColumnString = *current;
243  ++current;
244  for(; current != end; ++current)
245  {
247  _CurrentRowSetColumnString += *current;
248  }
249  rowset->SetAttribute( "columns", _CurrentRowSetColumnString.c_str() );
250 
251  _pXmlElementStack->push( rowset );
252 }
253 
255 {
256  _CloseXMLTag();
257 }
258 
259 void APIServiceManager::_BuildXMLRow(const std::vector<std::string> * columns)
260 {
261  TiXmlElement * row = new TiXmlElement( "row" );
262 
263  std::vector<std::string>::const_iterator current, end;
264  std::string column_string = _CurrentRowSetColumnString;
265  std::string column_name;
266  int pos=0;
267  int pos2=0;
268  current = columns->begin();
269  end = columns->end();
270  for(; current != end; ++current)
271  {
272  pos = column_string.find_first_of(",");
273  column_name = column_string.substr(0,pos);
274  column_string = column_string.substr(pos+1);
275  row->SetAttribute( column_name.c_str(), current->c_str() );
276  }
277  _pXmlElementStack->top()->LinkEndChild( row );
278 }
279 
280 void APIServiceManager::_BuildXMLTag(std::string name)
281 {
282  TiXmlElement * tag = new TiXmlElement( name.c_str() );
283  _pXmlElementStack->push( tag );
284 }
285 
286 void APIServiceManager::_BuildXMLTag(std::string name, const std::vector<std::pair<std::string, std::string> > * params)
287 {
288  TiXmlElement * tag = new TiXmlElement( name.c_str() );
289 
290  std::vector<std::pair<std::string, std::string> >::const_iterator current, end;
291  current = params->begin();
292  end = params->end();
293  for(; current != end; ++current)
294  tag->SetAttribute( current->first.c_str(), current->second.c_str() );
295 
296  _pXmlElementStack->push( tag );
297 }
298 
299 void APIServiceManager::_BuildXMLTag(std::string name, const std::vector<std::pair<std::string, std::string> > * params, std::string value)
300 {
301  _BuildXMLTag( name, params );
302  _pXmlElementStack->top()->LinkEndChild( new TiXmlText( value.c_str() ));
303 }
304 
305 void APIServiceManager::_BuildSingleXMLTag(std::string name, std::string value)
306 {
307  TiXmlElement * tag = new TiXmlElement( name.c_str() );
308  tag->LinkEndChild( new TiXmlText( value.c_str() ));
309 
310  if( _pXmlElementStack->empty() )
311  _pXmlDocOuterTag->LinkEndChild( tag );
312  else
313  _pXmlElementStack->top()->LinkEndChild( tag );
314 }
315 
316 void APIServiceManager::_BuildErrorXMLTag(std::string code, std::string param)
317 {
318  TiXmlElement * error = new TiXmlElement( "error" );
319  error->SetAttribute( "code", code.c_str() );
320  error->LinkEndChild( new TiXmlText( param.c_str() ));
321 
322  if( _pXmlElementStack->empty() )
323  _pXmlDocOuterTag->LinkEndChild( error );
324  else
325  _pXmlElementStack->top()->LinkEndChild( error );
326 }
327 
329 {
330  if( _pXmlElementStack->empty() )
331  return;
332 
333  TiXmlElement * _pTopElement = _pXmlElementStack->top();
334  _pXmlElementStack->pop();
335 
336  if( _pXmlElementStack->empty() )
337  _pXmlDocOuterTag->LinkEndChild( _pTopElement );
338  else
339  {
340  TiXmlElement * _pNextTopElement = _pXmlElementStack->top();
341  _pNextTopElement->LinkEndChild( _pTopElement );
342  }
343 }
344 
345 std::tr1::shared_ptr<std::string> APIServiceManager::_GetXMLDocumentString()
346 {
347  TiXmlPrinter xmlPrinter;
348  _XmlDoc.Accept( &xmlPrinter );
349 
350  return std::tr1::shared_ptr<std::string>(new std::string(xmlPrinter.CStr()));
351 }
std::tr1::shared_ptr< std::string > BuildErrorXMLResponse(std::string errorCode, std::string errorMessage)
APIServiceManager(const PyServiceMgr &services)
TiXmlDocument _XmlDoc
std::stack< TiXmlElement * > * _pXmlElementStack
std::tr1::shared_ptr< std::string > _GetXMLDocumentString()
void _BuildSingleXMLTag(std::string name, std::string param)
std::map< std::string, std::string > APICommandCall
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
bool _AuthenticateFullAPIQuery(std::string userID, std::string apiKey)
virtual std::tr1::shared_ptr< std::string > ProcessCall(const APICommandCall *pAPICommandCall)
bool GetApiAccountInfoUsingUserID(std::string userID, std::string *apiFullKey, std::string *apiLimitedKey, uint32 *apiRole)
?
static bool GeneratePassHash(const std::string &user, const std::string &pass, std::string &hash)
Generates a SHA-1 hash from a username and a password.
int64 Win32TimeNow()
Definition: utils_time.cpp:70
std::string _CurrentRowSetColumnString
unsigned __int32 uint32
Definition: eve-compat.h:50
bool _AuthenticateUserNamePassword(std::string userName, std::string password)
void _BuildXMLTag(std::string name)
TiXmlElement * _pXmlDocOuterTag
PyServiceMgr & services()
const int64 Win32Time_Minute
Definition: utils_time.cpp:39
std::string Win32TimeToString(int64 win32t)
Definition: utils_time.cpp:59
typeID Spawn an NPC with the specified type text Search for items matching the specified query() type() key(value)-Send an OnRemoteMessage" ) COMMAND( setbpattr
void _BuildXMLRow(const std::vector< std::string > *columns)
void _BuildErrorXMLTag(std::string code, std::string param)
void _CloseXMLHeader(uint32 cacheStyle)
void _BuildXMLRowSet(std::string name, std::string key, const std::vector< std::string > *columns)
bool _AuthenticateLimitedAPIQuery(std::string userID, std::string apiKey)
static uint32 currentTime
Definition: timer.cpp:33