Navigation:  Advanced Features >

Lua Language

Previous pageReturn to chapter overviewNext page

About Lua

Lua is a powerful, fast, lightweight, embeddable scripting language. Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.

 

Wing FTP Server includes support for the Lua scripting language. Lua scripts can be used in several cases, such as system schedulers, domain events and web console, and it will help you to complete a complex scheduler task or a useful FTP LOGIN event or some simple console commands.

 

You can write Lua scripts simply by using standard Lua libraries and Wing FTP Server's Lua API. For example, if you want  to add a user to the server, you can call c_AddUser(...) API to implement it, then if you want to delete the user, just call c_DeleteUser(...). It is simple yet powerful. For more functional work, you may require an add-on Lua library written by yourself or other people, such as LuaCOM, which allows Lua programs to use and implement objects that follow Microsoft's Component Object Model (COM) specification and use the ActiveX technology for property access and method calls.

 

Simple example (a daily scheduler task for removing inactive users in domain "default" that have not logged in within 15 days)

do 
    local strUserlist = c_GetUserList("default") 
    local userlist = Split(strUserlist,"\n") 
    for _,username in pairs(userlist) do 
        local user = c_GetUser("default",username)
        local logintime = user.last_logintime
        local logintime_t = c_TranslateTime(logintime)
        if (os.time() - logintime_t) >= 3600*24*15 then 
            c_DeleteUser("default",username)
        end 
    end 
end

 

More complex example (an interesting guess number game that you can play in the web console)

function GuessGame(code) 
    if _SESSION['guessgame_code'] == nil or code == "start" then 
        print("Welcome to the NumberGuess(v1.0) game!\r\nPlease input 4 different number,such as 'guess 0538',then system will give some tip.\r\nIf the tip is '2A1B',this means you got two number right,but only one of those is in right position.\r\nKeep guessing until all numbers are right.\r\nYou can restart game by command 'guess start'."
        local codeString = "" 
        local charArray = {"0","1","2","3","4","5","6","7","8","9"} 
        math.randomseed(os.time()) 
        math.random() 
        for i=1,4 do 
            local nowindex = math.random(1,table.maxn(charArray)) 
            codeString = codeString..charArray[nowindex] 
            table.remove(charArray,nowindex) 
        end 
        rawset(_SESSION,"guessgame_code",codeString) 
        SessionModule.save(_SESSION_ID) 
    else 
        local codeString = _SESSION['guessgame_code'
        local strlen = string.len(code) 
        if strlen ~= 4 then 
            print("Please input 4 different number!\r\n") 
            return 
        else 
            for i=1,4 do 
                local temp = string.gsub(code,string.sub(code,i,i),"") 
                if string.len(temp) ~= 3 then 
                    print("Please input 4 different number!\r\n") 
                    return 
                end 
            end 
 
            local rightnum = 0 
            local rightpos = 0 
            for i=1,4 do 
                if string.find(codeString,string.sub(code,i,i)) then 
                    rightnum = rightnum+1 
                end 
                if string.sub(codeString,i,i) == string.sub(code,i,i) then 
                    rightpos = rightpos+1 
                end 
            end 
            local result = string.format("%dA%dB",rightnum,rightpos); 
            if result == "4A4B" then 
                local count = _SESSION['guessgame_count'or "1
                if count <= 3 then 
                    print("You are god!!! just "..tostring(count).." tries!!!") 
                elseif count <= 7 then 
                    print("Good guess! You got it after just "..tostring(count).." tries!") 
                else 
                    print("Congratulations! You got it after "..tostring(count).." tries.") 
                end 
                _SESSION['guessgame_code'] = nil 
                _SESSION['guessgame_count'] = nil 
                SessionModule.save(_SESSION_ID) 
            else 
                print(result) 
                if _SESSION['guessgame_count'] == nil then 
                    _SESSION['guessgame_count'] = 1 
                else 
                    _SESSION['guessgame_count'] = _SESSION['guessgame_count']+1 
                end 
                SessionModule.save(_SESSION_ID) 
            end 
        end 
    end 
end

 

This example is written in the end of file "lua/ServerInterface.lua" and it is just a demo for fun.

There are several other useful lua functions in the file "lua/ServerInterface.lua". You can add your lua function code for effective administration.

 

Wing FTP Server also provides a useful REST web service for administration, and you can call it for executing lua script in any external programming language.

The RESTful web service URL may look like this: http://127.0.0.1:5466/admin_webservice.html?admin=demo&pass=demo123&cmd=xxxxx

There are three URL parameters in the above string. The first parameter "admin" means the administrator's username, the second parameter "pass" means the administrator's password, and the third parameter "cmd" means the lua script with url encoded.

If the web service call fails, it will return a string startting with "[ERROR RESULT]".

 

Here we will present an example in some programming languages. This example is very simple, just for calculating the number of all the domains' online sessions.

 

 

PHP example:

$strUrl = "http://127.0.0.1:5466/admin_webservice.html"
$strUrlParam = "?admin=demo&pass=demo123&cmd="
$strLuaScript = <<<EOT 
    local nSessionCnt = 0 
    for _,domain in pairs(c_GetDomainList()) do 
        nSessionCnt = nSessionCnt + c_GetSessionCount(domain) 
    end 
    print(nSessionCnt) 
EOT; 
 
$strResult = file_get_contents($strUrl.$strUrlParam.rawurlencode($strLuaScript));

 

 

 

VB script/ASP example:

Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP"
strUrl = "http://127.0.0.1:5466/admin_webservice.html" 
strUrlParam = "?admin=demo&pass=demo123&cmd=" 
strLuaScript = "local nSessionCnt = 0 "
              &"for _,domain in pairs(c_GetDomainList()) do "
              &" nSessionCnt = nSessionCnt + c_GetSessionCount(domain) "
              &"end "
              &"print(nSessionCnt)" 
xmlHttp.open "GET", strUrl&strUrlParam&URLEncode(strLuaScript), False 
xmlHttp.send 
strResult = xmlHttp.responseText
 
Function URLEncode(strInput) 
    For i = 1 To Len(strInput) 
        intAscii = Asc(Mid(strInput, i, 1)) 
        If ((intAscii < 58And (intAscii > 47)) Or ((intAscii < 91And (intAscii > 64)) Or ((intAscii < 123And (intAscii > 96)) Then 
            strOutput = strOutput & Chr(intAscii) 
        Else 
            If intAscii < 16 Then 
                strOutput = strOutput & "%0" & Trim(Hex(intAscii)) 
            Else 
                strOutput = strOutput & "%" & Trim(Hex(intAscii)) 
            End If 
        End If 
    Next 
    URLEncode = strOutput 
End Function

 

 

 

JAVA example:

import java.io.*; 
import java.net.*; 
 
class GetUrlContent { 
    public static void main(String[] args) throws IOException { 
        String strUrl = "http://127.0.0.1:5466/admin_webservice.html"; 
        String strUrlParam = "admin=demo&pass=demo123&cmd="; 
 
        String strLuaScript = "local nSessionCnt = 0 "+ 
                              "for _,domain in pairs(c_GetDomainList()) do "+ 
                              " nSessionCnt = nSessionCnt + c_GetSessionCount(domain) "+ 
                              "end "+ 
                              "print(nSessionCnt)" 
        String strResult = SendGetRequest(strUrl, strUrlParam+java.net.URLEncoder.encode(strLuaScript));
    } 
 
    public static String SendGetRequest(String url, String param) { 
        String result = "";
        try { 
            String urlName = url + "?" + param; 
            URL U = new URL(urlName); 
            URLConnection connection = U.openConnection(); 
            connection.connect(); 
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
            String line; 
 
            while ((line = in.readLine()) != null) { 
                result += line; 
            } 
            in.close(); 
        } catch (Exception e) { 
            result = ""; 
        } 
        return result; 
    } 
}