Esempio n. 1
0
int CLuaHTTPDefs::httpSetResponseCookie ( lua_State* luaVM )
{
    if ( lua_type ( luaVM, 1 ) == LUA_TSTRING && lua_type ( luaVM, 2 ) == LUA_TSTRING )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResourceFile * file = pLuaMain->GetResourceFile();
            if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
            {
                CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                char* szHeaderName = (char *) lua_tostring ( luaVM, 1 );
				char* szHeaderValue = (char *) lua_tostring ( luaVM, 2 );
				html->SetResponseCookie ( szHeaderName, szHeaderValue );
                lua_pushboolean ( luaVM, true );
                return 1;
            }
            else
                m_pScriptDebugging->LogError ( luaVM, "httpSetResponseCookie: Can only be used in HTML scripts" );
        } 
        else
            m_pScriptDebugging->LogError ( luaVM, "httpSetResponseCookie" );
    }   
    else
        m_pScriptDebugging->LogBadType ( luaVM, "httpSetResponseCookie" );
    lua_pushboolean ( luaVM, false );
    return 1;
}
Esempio n. 2
0
int CLuaHTTPDefs::httpWrite ( lua_State* luaVM )
{
    if ( lua_type ( luaVM, 1 ) == LUA_TSTRING )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResourceFile * file = pLuaMain->GetResourceFile();
            if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
            {
                CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                unsigned long length = NULL;
                if ( lua_type ( luaVM, 2 ) == LUA_TNUMBER )
                    length = static_cast < unsigned long > ( lua_tonumber ( luaVM, 2 ) );
                else
                    length = lua_objlen ( luaVM, 1 );
                const char* szBuffer = lua_tolstring ( luaVM, 1, NULL );
                html->AppendToPageBuffer ( szBuffer, length );
                lua_pushboolean ( luaVM, true );
                return 1;
            }
            else
                m_pScriptDebugging->LogError ( luaVM, "httpWrite: Can only be used in HTML scripts" );
        } 
        else
            m_pScriptDebugging->LogError ( luaVM, "httpWrite" );
    }   
    else
        m_pScriptDebugging->LogBadType ( luaVM, "httpWrite" );
    lua_pushboolean ( luaVM, false );
    return 1;
}
Esempio n. 3
0
int CLuaHTTPDefs::httpRequestLogin ( lua_State* luaVM )
{
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        CResourceFile * file = pLuaMain->GetResourceFile();
        if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
        {
            CResourceHTMLItem * html = (CResourceHTMLItem *)file;

			char szName[255];
			sprintf ( szName, "Basic realm=\"%s\"", m_pMainConfig->GetServerName ().c_str () );
			html->SetResponseHeader("WWW-Authenticate", szName);
			html->SetResponseCode ( 401 );
            lua_pushboolean ( luaVM, true );
            return 1;
        }
        else
            m_pScriptDebugging->LogError ( luaVM, "httpRequestLogin: Can only be used in HTML scripts" );
    } 
    else
        m_pScriptDebugging->LogError ( luaVM, "httpRequestLogin" );   
    lua_pushboolean ( luaVM, false );
    return 1;
}
Esempio n. 4
0
int CLuaHTTPDefs::httpSetResponseCode ( lua_State* luaVM )
{
    if ( lua_type ( luaVM, 1 ) == LUA_TNUMBER  )
    {
        CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
        if ( pLuaMain )
        {
            CResourceFile * file = pLuaMain->GetResourceFile();
            if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
            {
                CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                unsigned int responseCode = static_cast < unsigned int > ( lua_tonumber ( luaVM, 1 ) );
				html->SetResponseCode ( responseCode );
                lua_pushboolean ( luaVM, true );
                return 1;
            }
            else
                m_pScriptDebugging->LogError ( luaVM, "httpSetResponseCode: Can only be used in HTML scripts" );
        } 
        else
            m_pScriptDebugging->LogError ( luaVM, "httpSetResponseCode" );
    }   
    else
        m_pScriptDebugging->LogBadType ( luaVM, "httpSetResponseCode" );
    lua_pushboolean ( luaVM, false );
    return 1;
}
Esempio n. 5
0
int CLuaHTTPDefs::httpClear ( lua_State* luaVM )
{
    CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine ( luaVM );
    if ( pLuaMain )
    {
        CResourceFile * file = pLuaMain->GetResourceFile();
        if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
        {
            CResourceHTMLItem * html = (CResourceHTMLItem *)file;
			html->ClearPageBuffer();
            lua_pushboolean ( luaVM, true );
            return 1;
        }
        else
            m_pScriptDebugging->LogError ( luaVM, "httpClear: Can only be used in HTML scripts" );
    } 
    else
        m_pScriptDebugging->LogError ( luaVM, "httpClear" );   
    lua_pushboolean ( luaVM, false );
    return 1;
}
void CScriptDebugging::LogString ( const char* szPrePend, lua_State * luaVM, const char* szMessage, unsigned int uiMinimumDebugLevel, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue )
{
    SString strText;
    lua_Debug debugInfo;

    // Initialize values for onDebugMessage
    SString strMsg  = szMessage;
    SString strFile = "";
    int     iLine   = -1;

    // Get a VM from somewhere
    if ( !luaVM && !m_LuaMainStack.empty () )
        luaVM = m_LuaMainStack.back ()->GetVM ();

    for ( int level = 1; level < 3; level++ )
    {
	    if ( luaVM && lua_getstack ( luaVM, level, &debugInfo ) )
	    {
		    lua_getinfo ( luaVM, "nlS", &debugInfo );

		     // Make sure this function isn't defined in a string (eg: from runcode)
            if ( debugInfo.source[0] == '@' )
            {
                // Get and store the location of the debug message
                strFile = debugInfo.source + 1;
                iLine   = debugInfo.currentline;

                // Populate a message to print/send (unless "info" type)
                if ( uiMinimumDebugLevel < 3 )
                    strText = SString ( "%s%s:%d: %s", szPrePend, strFile.c_str (), debugInfo.currentline, szMessage );
                // if the file isn't empty, stop trying any other levels
                break;
            }
            else
            {
                strFile = debugInfo.short_src;

                if ( uiMinimumDebugLevel < 3 )
                    strText = SString ( "%s%s %s", szPrePend, szMessage, strFile.c_str () );
                if ( strFile != "[string \"?\"]" ) // if the file isn't empty, stop trying any other levels
                    break;
            }
        }
        else
        {
            strText = SString ( "%s%s%s", szPrePend, m_strLineAndFile.c_str(), szMessage );
            // no point in trying other levels
            break;
        }
    }

    // Create a different message if type is "INFO"
    if ( uiMinimumDebugLevel > 2 )
        strText = SString ( "%s%s", szPrePend, szMessage );

    // Check whether onDebugMessage is currently being triggered
    if ( !m_bTriggeringOnDebugMessage )
    {
        // Make sure the state of onDebugMessage being triggered can be retrieved later
        m_bTriggeringOnDebugMessage = true;

        // Prepare onDebugMessage
        CLuaArguments Arguments;
        Arguments.PushString ( strMsg.c_str ( ) );
        Arguments.PushNumber ( uiMinimumDebugLevel );

        // Push the file name (if any)
        if ( strFile.length ( ) > 0 )
            Arguments.PushString ( strFile.c_str ( ) );
        else
            Arguments.PushNil ( );

        // Push the line (if any)
        if ( iLine > -1 )
            Arguments.PushNumber ( iLine );
        else
            Arguments.PushNil ( );
        
        // Call onDebugMessage
        g_pGame->GetMapManager ( )->GetRootElement ( )->CallEvent ( "onDebugMessage", Arguments );

        // Reset trigger state, so onDebugMessage can be called again at a later moment
        m_bTriggeringOnDebugMessage = false;
    }

    // Log it to the file if enough level
    if ( m_uiLogFileLevel >= uiMinimumDebugLevel )
    {
        PrintLog ( strText );
    }

    // Log to console
    CLogger::LogPrintf( "%s\n", strText.c_str () );

    // Not sure what this is for, seems pretty useless
    if ( m_uiHtmlLogLevel >= uiMinimumDebugLevel )
    {
        if ( luaVM )
        {
            CLuaMain* pLuaMain = g_pGame->GetLuaManager()->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CResourceFile * file = pLuaMain->GetResourceFile();
                if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
                {
                    CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                    html->AppendToPageBuffer ( strText );
                    html->AppendToPageBuffer ( "<br/>" );
                }
            }
        }
    }

    // Tell the players
    Broadcast ( CDebugEchoPacket ( strText, uiMinimumDebugLevel, ucRed, ucGreen, ucBlue ), uiMinimumDebugLevel );
}
Esempio n. 7
0
void CScriptDebugging::LogString ( const char* szPrePend, const SLuaDebugInfo& luaDebugInfo, const char* szMessage, unsigned int uiMinimumDebugLevel, unsigned char ucRed, unsigned char ucGreen, unsigned char ucBlue )
{
    SString strText = ComposeErrorMessage( szPrePend, luaDebugInfo, szMessage );

    // Create a different message if type is "INFO"
    if ( uiMinimumDebugLevel > 2 )
        strText = SString ( "%s%s", szPrePend, szMessage );

    // Check whether onDebugMessage is currently being triggered
    if ( !m_bTriggeringOnDebugMessage )
    {
        // Make sure the state of onDebugMessage being triggered can be retrieved later
        m_bTriggeringOnDebugMessage = true;

        // Prepare onDebugMessage
        CLuaArguments Arguments;
        Arguments.PushString ( szMessage );
        Arguments.PushNumber ( uiMinimumDebugLevel );

        // Push the file name (if any)
        if ( !luaDebugInfo.strFile.empty() )
            Arguments.PushString ( luaDebugInfo.strFile );
        else
            Arguments.PushNil ( );

        // Push the line (if any)
        if ( luaDebugInfo.iLine != INVALID_LINE_NUMBER )
            Arguments.PushNumber ( luaDebugInfo.iLine );
        else
            Arguments.PushNil ( );
        
        // Call onDebugMessage
        g_pGame->GetMapManager ( )->GetRootElement ( )->CallEvent ( "onDebugMessage", Arguments );

        // Reset trigger state, so onDebugMessage can be called again at a later moment
        m_bTriggeringOnDebugMessage = false;
    }

    // Log it to the file if enough level
    if ( m_uiLogFileLevel >= uiMinimumDebugLevel )
    {
        PrintLog ( strText );
    }

    // Log to console
    CLogger::LogPrintf( "%s\n", strText.c_str () );

#if 0
    // Not sure what this is for, seems pretty useless
    if ( m_uiHtmlLogLevel >= uiMinimumDebugLevel )
    {
        if ( luaVM )
        {
            CLuaMain* pLuaMain = g_pGame->GetLuaManager()->GetVirtualMachine ( luaVM );
            if ( pLuaMain )
            {
                CResourceFile * file = pLuaMain->GetResourceFile();
                if ( file && file->GetType() == CResourceHTMLItem::RESOURCE_FILE_TYPE_HTML )
                {
                    CResourceHTMLItem * html = (CResourceHTMLItem *)file;
                    html->AppendToPageBuffer ( strText );
                    html->AppendToPageBuffer ( "<br/>" );
                }
            }
        }
    }
#endif

    // Tell the players
    Broadcast ( CDebugEchoPacket ( strText, uiMinimumDebugLevel, ucRed, ucGreen, ucBlue ), uiMinimumDebugLevel );
}