2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
|
|
|
|
2012-01-27 18:47:32 -05:00
|
|
|
#include "cWebPlugin_Lua.h"
|
|
|
|
#include "cPlugin_NewLua.h"
|
|
|
|
|
|
|
|
#include "tolua++.h"
|
|
|
|
#include "cWebAdmin.h"
|
|
|
|
|
2012-01-29 14:28:19 -05:00
|
|
|
|
2012-01-31 19:02:48 -05:00
|
|
|
static std::string SafeString( const std::string& a_String )
|
|
|
|
{
|
|
|
|
std::string RetVal;
|
|
|
|
for( unsigned int i = 0; i < a_String.size(); ++i )
|
|
|
|
{
|
|
|
|
char c = a_String[i];
|
|
|
|
if( c == ' ' )
|
|
|
|
{
|
|
|
|
c = '_';
|
|
|
|
}
|
|
|
|
RetVal.push_back( c );
|
|
|
|
}
|
|
|
|
return RetVal;
|
|
|
|
}
|
2012-01-29 14:28:19 -05:00
|
|
|
|
|
|
|
|
2012-01-27 18:47:32 -05:00
|
|
|
extern bool report_errors(lua_State* lua, int status);
|
|
|
|
extern std::vector<std::string> StringSplit(std::string str, std::string delim);
|
|
|
|
|
|
|
|
struct cWebPlugin_Lua::sWebPluginTab
|
|
|
|
{
|
|
|
|
std::string Title;
|
|
|
|
std::string SafeTitle;
|
|
|
|
|
|
|
|
int Reference;
|
|
|
|
};
|
|
|
|
|
|
|
|
cWebPlugin_Lua::cWebPlugin_Lua( cPlugin_NewLua* a_Plugin )
|
|
|
|
: cWebPlugin( a_Plugin->GetLuaState() )
|
|
|
|
, m_Plugin( a_Plugin )
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cWebPlugin_Lua::~cWebPlugin_Lua()
|
|
|
|
{
|
|
|
|
for( TabList::iterator itr = m_Tabs.begin(); itr != m_Tabs.end(); ++itr )
|
|
|
|
{
|
|
|
|
delete *itr;
|
|
|
|
}
|
|
|
|
m_Tabs.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cWebPlugin_Lua::AddTab( const char* a_Title, lua_State * a_LuaState, int a_FunctionReference )
|
|
|
|
{
|
|
|
|
if( a_LuaState != m_Plugin->GetLuaState() )
|
|
|
|
{
|
|
|
|
LOGERROR("Only allowed to add a tab to a WebPlugin of your own Plugin!");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sWebPluginTab* Tab = new sWebPluginTab();
|
|
|
|
Tab->Title = a_Title;
|
2012-01-31 19:02:48 -05:00
|
|
|
Tab->SafeTitle = SafeString( a_Title );
|
2012-01-27 18:47:32 -05:00
|
|
|
|
|
|
|
Tab->Reference = a_FunctionReference;
|
|
|
|
|
|
|
|
m_Tabs.push_back( Tab );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string cWebPlugin_Lua::HandleRequest( HTTPRequest* a_Request )
|
|
|
|
{
|
|
|
|
lua_State* LuaState = m_Plugin->GetLuaState();
|
|
|
|
std::string RetVal = "";
|
|
|
|
|
2012-01-31 19:02:48 -05:00
|
|
|
std::pair< std::string, std::string > TabName = GetTabNameForRequest(a_Request);
|
|
|
|
std::string SafeTabName = TabName.second;
|
|
|
|
if( SafeTabName.empty() )
|
2012-01-27 18:47:32 -05:00
|
|
|
return "";
|
|
|
|
|
|
|
|
sWebPluginTab* Tab = 0;
|
|
|
|
for( TabList::iterator itr = m_Tabs.begin(); itr != m_Tabs.end(); ++itr )
|
|
|
|
{
|
2012-01-31 19:02:48 -05:00
|
|
|
if( (*itr)->SafeTitle.compare( SafeTabName ) == 0 ) // This is the one! Rawr
|
2012-01-27 18:47:32 -05:00
|
|
|
{
|
|
|
|
Tab = *itr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Tab )
|
|
|
|
{
|
2012-01-30 19:38:18 -05:00
|
|
|
//LOGINFO("1. Stack size: %i", lua_gettop(LuaState) );
|
2012-01-27 18:47:32 -05:00
|
|
|
lua_rawgeti( LuaState, LUA_REGISTRYINDEX, Tab->Reference); // same as lua_getref()
|
|
|
|
|
2012-01-30 19:38:18 -05:00
|
|
|
//LOGINFO("2. Stack size: %i", lua_gettop(LuaState) );
|
2012-01-27 18:47:32 -05:00
|
|
|
// Push HTTPRequest
|
|
|
|
tolua_pushusertype( LuaState, a_Request, "HTTPRequest" );
|
2012-01-30 19:38:18 -05:00
|
|
|
//LOGINFO("Calling bound function! :D");
|
2012-01-27 18:47:32 -05:00
|
|
|
int s = lua_pcall( LuaState, 1, 1, 0);
|
2012-01-30 11:47:26 -05:00
|
|
|
|
|
|
|
if ( s != 0 )
|
2012-01-27 18:47:32 -05:00
|
|
|
{
|
2012-01-30 11:47:26 -05:00
|
|
|
std::string err = lua_tostring(LuaState, -1);
|
|
|
|
LOGERROR("-- %s", err.c_str() );
|
|
|
|
lua_pop(LuaState, 1);
|
2012-01-27 18:47:32 -05:00
|
|
|
LOGINFO("error. Stack size: %i", lua_gettop(LuaState) );
|
2012-01-30 11:47:26 -05:00
|
|
|
return err; // Show the error message in the web page, looks cool
|
2012-01-27 18:47:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !lua_isstring( LuaState, -1 ) )
|
|
|
|
{
|
|
|
|
LOGWARN("WARNING: WebPlugin tab '%s' did not return a string!", Tab->Title.c_str() );
|
|
|
|
lua_pop(LuaState, 1); // Pop return value
|
2012-01-30 11:47:26 -05:00
|
|
|
return std::string("WARNING: WebPlugin tab '") + Tab->Title + std::string("' did not return a string!");
|
2012-01-27 18:47:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
RetVal += tolua_tostring(LuaState, -1, 0);
|
|
|
|
lua_pop(LuaState, 1); // Pop return value
|
2012-01-30 19:38:18 -05:00
|
|
|
//LOGINFO("ok. Stack size: %i", lua_gettop(LuaState) );
|
2012-01-27 18:47:32 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return RetVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cWebPlugin_Lua::Initialize()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-01-31 19:02:48 -05:00
|
|
|
std::pair< std::string, std::string > cWebPlugin_Lua::GetTabNameForRequest( HTTPRequest* a_Request )
|
2012-01-27 18:47:32 -05:00
|
|
|
{
|
2012-01-31 19:02:48 -05:00
|
|
|
std::pair< std::string, std::string > Names;
|
2012-01-27 18:47:32 -05:00
|
|
|
std::vector<std::string> Split = StringSplit( a_Request->Path, "/" );
|
|
|
|
|
|
|
|
if( Split.size() > 1 )
|
|
|
|
{
|
|
|
|
sWebPluginTab* Tab = 0;
|
|
|
|
if( Split.size() > 2 ) // If we got the tab name, show that page
|
|
|
|
{
|
|
|
|
for( TabList::iterator itr = m_Tabs.begin(); itr != m_Tabs.end(); ++itr )
|
|
|
|
{
|
|
|
|
if( (*itr)->SafeTitle.compare( Split[2] ) == 0 ) // This is the one! Rawr
|
|
|
|
{
|
|
|
|
Tab = *itr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // Otherwise show the first tab
|
|
|
|
{
|
|
|
|
if( m_Tabs.size() > 0 )
|
|
|
|
Tab = *m_Tabs.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
if( Tab )
|
|
|
|
{
|
2012-01-31 19:02:48 -05:00
|
|
|
Names.first = Tab->Title;
|
|
|
|
Names.second = Tab->SafeTitle;
|
2012-01-27 18:47:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 19:02:48 -05:00
|
|
|
return Names;
|
2012-01-27 18:47:32 -05:00
|
|
|
}
|
|
|
|
|
2012-01-31 19:02:48 -05:00
|
|
|
std::list< std::pair<std::string, std::string> > cWebPlugin_Lua::GetTabNames()
|
2012-01-27 18:47:32 -05:00
|
|
|
{
|
2012-01-31 19:02:48 -05:00
|
|
|
std::list< std::pair< std::string, std::string > > NameList;
|
2012-01-27 18:47:32 -05:00
|
|
|
for( TabList::iterator itr = m_Tabs.begin(); itr != m_Tabs.end(); ++itr )
|
|
|
|
{
|
2012-01-31 19:02:48 -05:00
|
|
|
std::pair< std::string, std::string > StringPair;
|
|
|
|
StringPair.first = (*itr)->Title;
|
|
|
|
StringPair.second = (*itr)->SafeTitle;
|
|
|
|
NameList.push_back( StringPair );
|
2012-01-27 18:47:32 -05:00
|
|
|
}
|
|
|
|
return NameList;
|
|
|
|
}
|