Share Pastebin
Guest
Public paste!

BulkServer WIP

By: a guest | Mar 21st, 2010 | Syntax: Lua | Size: 13.62 KB | Hits: 59 | Expires: Never
Copy text to clipboard
  1. # BulkServer 1.1 by Filipe Dobreira, dobreira @ gmail.com
  2. # March 2010.
  3. @name BulkServer1
  4. @inputs Scr:wirelink Input:string
  5. @outputs
  6. @persist [PRT STK HOLD UNPACK SEND LOG]:array [V BL SCHEDULE GLOBALS]:table
  7. @persist Start End
  8. @trigger
  9.  
  10.  
  11.     if(first())
  12.     {
  13.         # Server Variables +---------------------------+
  14.         # server variables should use an underscore prefix ('_') to prevent being overwritten.
  15.         # and are accessible in code unless declared in the blacklist.
  16.         # server variables (_prefix) cannot be written to by scripts.
  17.        
  18.         V["_range",string]             =     "2-16"             # Sweep range, where the server looks for connections.
  19.         V["_lookupPort",number]        =     1                  # Swap port, for server-use.
  20.         V["_serverVersion",string]     =     "1.1"              # Server version/identification.
  21.         V["_serverName",string]        =     "Test Server."     # Server identification, sent in the header.
  22.         V["_frequency",number]         =     50                 # Interval between port sweeps.
  23.         V["_group",string]             =     "fnet"             # Default connection group.
  24.         V["_timeout",number]           =     150                # Maximum operations allowed for client scripts.
  25.         V["_allowRun",number]          =     1                  # If set to 1, allows the use of the RUN command.      
  26.         V["_refreshRate",number]       =     1000               # Interval in MS between data refreshing.
  27.         V["_day",number]               =     time("day")        # The current day.
  28.         V["_dir",string]               =     "/bulkserv/"       # Directory used for file-functions.
  29.         V["_fileFunctions",string]     =     "RWL"              # File-function flags to control access (R-read,W-write,L-load)
  30.         V["_log",number]               =     1                  # If 1, actions are logged to---...
  31.         V["_logfile",string]           =     "_log.txt"         # Location of log file, relative to the _dir SVar.
  32.        
  33.         # Black List +----------------------------------+
  34.         # the black list is a multi-purpose table used to block access to certain data.
  35.         # the KEY is the variable name, the VALUE can be 0(unlocked),1(locked) or a STRING(auth).
  36.         # May also be used to blacklist files, with FILENAME as the key, value follows same rules.
  37.        
  38.         BL["_frequency",number]        =     1
  39.         BL[V["_logfile",string],number]=     1
  40.        
  41.         # Globals List +-------------------------------+
  42.         # The globals list marks variables that should not be dumped after the session ends.
  43.         # Scripts can set these globals, or you can set them manually here.
  44.         GLOBALS["bacon",number]        =     1
  45.        
  46.         # Identifiers +---------------------------------+
  47.         # used in the stack processor, identifiers tell the script who or what
  48.         # is using the server at that exact time. The optional SVar identifiers
  49.         # can be used to identify known sources, with a persistant ID.
  50.        
  51.         V["_%kbrd",string]                =     "Keyboard Module."
  52.         V["_%"+owner():steamID(),string]  =     "BulkServer Administrator."
  53.        
  54.         # Boot operations +-----------------------------+
  55.         # anything else that should be done at boot time goes here:
  56.         Scr[2041]  =  1 V["_bootTime",number] = int(curtime())
  57.         timer("refresh",V["_refreshRate",number])
  58.        
  59.        
  60.         PRT:pushString("444:+----------------------------+")
  61.         PRT:pushString("444:BulkServer online.")
  62.         PRT:pushString("444:serverName - "+V["_serverName",string])
  63.         PRT:pushString("444:group      - "+V["_group",string])
  64.         PRT:pushString("444:+----------------------------+")
  65.        
  66.         LOG:pushString("SERVER Initiated - "+V["_serverName",string])
  67.     }
  68.    
  69.     # Optional Input Module:
  70.     if(changed(Input) & Input != "") {
  71.         STK:pushString("{%kbrd%}"+Input)
  72.     }
  73.    
  74.     # Frequency +-----------------------------+
  75.     interval(V["_frequency",number])
  76.    
  77.     # Data Refresh:
  78.     if ( clk("refresh") ) {
  79.        
  80.         V["_curtime",number]    = int(curtime())
  81.         V["_hour",number]       = time("hour")
  82.         V["_min",number]        = time("min")
  83.         V["_sec",number]        = time("sec")
  84.         V["_time",string]       = time("hour")+":"+time("min")+":"+time("sec")
  85.         V["_uptime",number]     = int(curtime()) - V["_bootTime",number]
  86.    
  87.         timer("refresh",V["_refreshRate",number])
  88.     }
  89.    
  90.     # Port Sweep +----------------------------+
  91.     if( first() | V["_switchPort",number] ) {
  92.         SweepRange = V["_range",string]:explode("-")
  93.         Start      = SweepRange[1,string]:toNumber()
  94.         End        = SweepRange[2,string]:toNumber()
  95.        
  96.         V["_switchPort",number] = 0
  97.     }
  98.    
  99.     gSetGroup(V["_group",string])      gShare(1)
  100.     for ( I = Start, End ) {
  101.         FetchVal = gDeleteStr(I)        
  102.         if(FetchVal != ""){ UNPACK:pushString(FetchVal) }
  103.     }
  104.    
  105.     # Unpack Buffer function:
  106.     while(UNPACK:count()>0)
  107.     {
  108.         # Unpack:
  109.         Package         =   glonDecodeTable(UNPACK[1,string])
  110.         UNPACK:remove(1)
  111.         # Get sender id:
  112.         SenderId                    =   Package["_sender",string]
  113.         # Look for script:
  114.         Script                      =   Package["_script",string]
  115.         # Save HOLD index:
  116.         V["_HOLD_"+SenderId,number] =   HOLD:count()+1
  117.         # Unpack variables (skip hidden vars):
  118.         Keys                        =   Package:keys()
  119.         Variables                   =   array()
  120.         for(I=1,Keys:count())
  121.         {
  122.             if(Keys[I,string][1] != "_") {
  123.                 K = Package[Keys[I,string],string]
  124.                 N  = Package[Keys[I,string],number]
  125.             }
  126.            
  127.             if(K!="") {
  128.                 Variables:pushString(Keys[I,string])
  129.                 V[Keys[I,string],string] = Package[Keys[I,string],string]
  130.             }
  131.             elseif(N) {
  132.                 Variables:pushString(Keys[I,string])
  133.                 V[Keys[I,string],number] = Package[Keys[I,string],number]
  134.             }
  135.         }
  136.        
  137.         # Store variable manifest:
  138.         Package["_vars",string] = Variables:concat(";")
  139.        
  140.         # Store package:
  141.         HOLD:pushString(glonEncode(Package))
  142.        
  143.         # Run script (if exists):
  144.         if(Script == "") { Script = "send" }
  145.         STK:pushString("{%"+SenderId+"%}run "+Script:trim())
  146.        
  147.     }
  148.    
  149.     # Schedule Service:
  150.     Curtime = int(curtime()):toString()
  151.     if( SCHEDULE[Curtime,string] !="" ) {
  152.         STK:pushString("run "+SCHEDULE[Curtime,string])
  153.         SCHEDULE[Curtime,string] = ""
  154.     }
  155.    
  156.     # Stack Processor +-----------------------+
  157.     if(clk("continue") | STK:count()>0) {
  158.     while(STK:count()>0 & perf()) {    
  159.        
  160.         # Initiate error buffer:
  161.         ERROR = array()
  162.        
  163.         # Fetch string:
  164.         P  = STK[1,string]:explode(" ") STK:remove(1)
  165.         Line = P[1,string]
  166.        
  167.         # Fetch identifier:
  168.         if( Line:find("{%") == 1) {
  169.             Id          =   Line:sub(3,Line:find("%}")-1)
  170.             P[1,string] =   Line:sub(Line:find("%}")+2)
  171.         }
  172.        
  173.         # Fetch package:
  174.         Pack               =   glonDecodeTable(HOLD[V["_HOLD_"+Id,number],string])
  175.        
  176.         V["_id",string]    = Id
  177.         V["_ident",string] = V["_%"+Id,string]
  178.        
  179.         # Expand variables:
  180.         for( I = 1, P:count() ) {
  181.             Key = P[I,string]
  182.             if( Key[1] == "$" ) {
  183.                 Key = Key:right(Key:length() - 1)
  184.                 if( V[Key,number] & !BL[Key,number] ) { P[I,string] = V[Key,number]:toString() }
  185.                 elseif( !BL[Key,number] ) { P[I,string] = V[Key,string] }
  186.             }
  187.         }
  188.        
  189.         # Fetch command (command can be fetched from a variable):
  190.         C = P[1,string] P:remove(1)
  191.        
  192.         #   Helper PRT function, used to print a string to the console.
  193.         if( C == "prt" )
  194.         {
  195.             PRT:pushString(P:concat(" "))    
  196.         }
  197.        
  198.         #   Allows scripts to write into the log file:
  199.         if( C == "log" ) {
  200.             LOG:pushString(V["_ident",string]+" || "+P:concat(" "))          
  201.         }
  202.        
  203.         #   SET, used to set variables, server variables are blocked by default.
  204.         elseif( C == "set" ) {
  205.             TargetVar = P[1,string] P:remove(1)
  206.                      
  207.             if(TargetVar[1] != "_") {
  208.                 Var = P:concat(" ")
  209.                 if(Var:toNumber()) {
  210.                     V[TargetVar,number] = Var:toNumber()
  211.                 }
  212.                 else{ V[TargetVar,string] = Var }
  213.                
  214.                 Pack["_vars",string] = Pack["_vars",string] +";"+TargetVar
  215.             }
  216.             else{
  217.                 PRT:pushString("900:Attempt to (re)declare "+TargetVar+"!")
  218.                 PRT:pushString("Cannot (re)declare server variables.")
  219.                 }
  220.         }
  221.        
  222.         #   GLOBAL-ize a variable, may be done before it's even declared.
  223.        elseif( C == "global" ) { GLOBALS[P[1,string],number] = 1 }
  224.        
  225.        #   PUT, used to store variables in the package.
  226.        elseif( C == "put" ) {
  227.                TargetVar = P[1,string] P:remove(1)
  228.                Value = P:concat(" ")
  229.                if(Value:toNumber()) {
  230.                    Pack[TargetVar,number] = Value:toNumber()
  231.                }
  232.                else {
  233.                    Pack[TargetVar,string] = Value
  234.                }
  235.        }
  236.        
  237.        #  ROT13, for those that take security as seriously as...well, you get the idea.
  238.        elseif( C == "rot13" ) {
  239.                TargetVar = P[1,string] P:remove(1)
  240.                Offset = 13 % 26
  241.                S = P:concat(" "):explode("")
  242.                for(I = 1,S:count())
  243.                {
  244.                    A = toByte(S[I,string])
  245.                    if (A >= 97 & A <= 122) {
  246.                        S[I,string] = toChar((A - 71 + Offset) % 26 + 97)
  247.                    }
  248.                    elseif (A >= 65 & A <= 90) {
  249.                        S[I,string] = toChar((A - 39 + Offset) % 26 + 65)
  250.                    }    
  251.                }
  252.                V[TargetVar,string] = S:concat("")
  253.        }
  254.        #  RUN command is used to parse a string of code into the stack.
  255.        elseif( C == "run" ) {
  256.            if(V["_allowRun",number]) {
  257.                Code = P:concat(" "):explode(";")
  258.                for(I=1,Code:count()) {
  259.                    print(Code[I,string]:trim())
  260.                    STK:pushString("{%"+Id+"%}" + Code[I,string]:trim() )
  261.                }
  262.            }
  263.            else{
  264.                PRT:pushString("900:RUN blocked by SVar.")
  265.                ERROR:pushString("RUN blocked by SVar.")
  266.            }
  267.        }
  268.        
  269.        #  Script scheduler, can be used to run functions in the future:
  270.        elseif( C == "schedule" )
  271.        {
  272.                ScheduleOffset = int(curtime()) + P[1,string]:toNumber()
  273.                P:remove(1) Script = P:concat(" ")
  274.                Sch = ScheduleOffset:toString()
  275.                SCHEDULE[Sch,string] = SCHEDULE[Sch,string] + Script
  276.                
  277.                LOG:pushString("SCHEDULE: "+V["_id",string]+"|"+V["_ident",string])
  278.        }
  279.        
  280.        #  SEND command, packages and sends package to destination:
  281.        elseif( C == "send" )
  282.        {
  283.                PRT:pushString("--SENT!")
  284.                SEND:pushString(glonEncode(Pack))
  285.        }
  286.        
  287.        # Re-package and store data:
  288.        HOLD[V["_HOLD_"+Id,number],string] = glonEncode(Pack)
  289.        
  290.    }} # <- stack wrapper.
  291.    # Stack loopback +------------------------+
  292.    if(STK:count()>0) { timer("continue",10) }
  293.    
  294.    # SEND buffer:
  295.    while( SEND:count() > 0 )
  296.    {
  297.        Data    = SEND[1,string]
  298.        Package = glonDecodeTable(Data)
  299.        Target  = Package["_target",number]
  300.        
  301.        # If no target specified, look for target variable.
  302.        if(!Target) { Target = Package["target",number] }    
  303.        
  304.        # Connect to target group:
  305.        Group = Package["_group",string]
  306.        if(Group == "") {
  307.            Group = Package["group",string]
  308.        }
  309.        
  310.        # Load variable manifest, dump session variables:
  311.        Keys    = Package["_vars",string]:explode(";")
  312.        for(I=1,Keys:count()) {
  313.            K   = Keys[I,string]
  314.            if(K[1] != "_" & !GLOBALS[K,number])
  315.            { V[K,string] = ""
  316.              V[K,number] = 0
  317.            }
  318.        }
  319.        
  320.        # Check and send:
  321.        if ( Target & Group !="") {
  322.            # Adios!
  323.            Package["_handler",string] = "BulkServer - "+V["_serverName",string]
  324.            gSetGroup(Group) gShare(1)
  325.            gSetStr(Target,glonEncode(Package))
  326.        }
  327.        
  328.    }
  329.    
  330.    # PRT output buffer: +--------------------+
  331.    while( PRT:count() > 0 )
  332.    {
  333.        Print = PRT[1,string] PRT:remove(1)
  334.        # Text color:
  335.        Color = 999
  336.        if(inrange(Print:find(":"),1,4)) {
  337.            Color = Print:sub(1,Print:find(":")-1):toNumber()
  338.            Print = Print:sub( Print:find(":")+1 )
  339.        }
  340.        Scr:writeString(Print,0,17,Color,Scr[2042])
  341.        Scr[2038] = 1
  342.    }
  343.    
  344.    # LOG output buffer +---------------------+
  345.    while(LOG:count()>0)
  346.    {
  347.        Line = LOG[1,string] LOG:remove(1)
  348.        if(V["_log",number])
  349.        {
  350.            fileAppend(V["_dir",string]+V["_logfile",string],V["_time",string]+": "+Line+"\n")    
  351.        }
  352.    }