/*
** Module   :SRVHOOK.SCR
** Abstract :
**
** Copyright (C) 1999, 2000  Link Guard Solutions Ltd.
**
** Log: Sun  01/10/2000 Updated
**
*/

tracefile = "ldir\trace."||date("S")
stack.ppp="sr_pppstack"
stack.ppp1="sr_pppstack1"
stack.ppp1="sr_pppstack2"
portname.sr_pppstack="PPP"
portname.sr_pppstack1="PPP1"
portname.sr_pppstack2="PPP2"

dt = DATE("B")
tm = TIME("S")
if tm<100 then
    dt = DATE("B") /* We could get new day between DATE and TIME calls */

if arg(1)='MSG_00000810' then
do
    source = TRANSLATE(arg(4))
    if right(source,8) = '.INBYTES' then
    do
        stk = left(source, length(source) - 8)
        port = portname.stk
        i = sf_Get_Var("srvhook."port".wait", 0)
        if i<>0 then
        do
            parse value arg(3) with id':'lang':'val
            if val<>'' then
            do
                call sf_Set_Var "srvhook."port".inbytes", val
                call checkport port
            end
        end
    end
    else if right(source,9) = '.OUTBYTES' then
    do
        stk = left(source, length(source) - 9)
        port = portname.stk
        i = sf_Get_Var("srvhook."port".wait", 0)
        if i<>0 then
        do
            parse value arg(3) with id':'lang':'val
            if val<>'' then
            do
                call sf_Set_Var "srvhook."port".outbytes", val
                call checkport port
            end
        end
    end
end
else if arg(1)='MSG_00000001' then
do
    x=sf_Get_Var("srvhook.list.num", 0)
    do i = 1 to x
        if (dt > sf_Get_Var("srvhook.list."i".dt", dt) | tm >= sf_Get_Var("srvhook.list."i".tm", tm + 1)) & ,
            sf_Get_Var("srvhook.list."i".done", 0) = 0
        then
        do
            call showbed sf_Get_Var("srvhook.list."i".port", "Unknown")
    	    call sf_Set_Var "srvhook.list."i".done",  1
        end
    end
end
else
if arg(1)='MSG_00000062' then
do
    call sf_Set_Var "srvhook."||arg(4)||".ip", arg(3)
end
else
if arg(1)='MSG_00000003' then
do
    x=sf_Get_Var("srvhook."Arg(4)".wait", 0)
    if x<>0 then
    do
        call showbed arg(4)
        call sf_Set_Var "srvhook.list."x".done", 1
    end
    call lineout tracefile, DATE("E")||' '||TIME("L")||" User "||sf_Get_Var("srvhook."||arg(4)||".name", "Unknown")||" logged in. Port="||ARG(4)||" IP="||sf_Get_Var("srvhook."||arg(4)||".ip", "Unknown")
    call sf_Set_Var "srvhook."||arg(4)||".date", dt
    call sf_Set_Var "srvhook."||arg(4)||".time", tm
end
else
if arg(1)='MSG_00000004' then
do
    num = sf_Get_Var("srvhook.list.num", 0)
    set = num + 1
    do i = 1 to num
        if sf_Get_Var("srvhook.list."i".done",0) = 1 then
        do
            set = i
            leave
        end
    end
    if set > num then
        call sf_Set_Var "srvhook.list.num", set

    call sf_Set_Var "srvhook.list."set".done", 0
    call sf_Set_Var "srvhook.list."set".dt", dt
    call sf_Set_Var "srvhook.list."set".tm", tm + 2
    call sf_Set_Var "srvhook.list."set".port", arg(4)
    call sf_Set_Var "srvhook."Arg(4)".wait", set
    x = translate(arg(4))
    stackname=stack.x
    call sf_PostMsg "MSG_00000804", "::"stackname".inbytes"
    call sf_PostMsg "MSG_00000804", "::"stackname".outbytes"
end

call stream tracefile, "c", "close"

return

checkport:

parse arg port
    listnum = sf_Get_Var("srvhook."port".wait", 0)
    if listnum<>0 then
    do
        trace intermediates
        if (sf_Get_Var("srvhook."port".inbytes", '') <> '') & (sf_Get_Var("srvhook."port".outbytes", '') <> '') then
        do
            /* We've got all the info */
    	    secs = (dt - sf_Get_Var("srvhook."||port||".date", dt))*(60*60*24) + (tm - sf_Get_Var("srvhook."||port||".time", tm -1))
            call lineout tracefile, DATE("E")||' '||TIME("L")||" User "||sf_Get_Var("srvhook."||port||".name", "Unknown")||" logged out. "||,
                                    "Port="||port||" User was online "||secs||" seconds. "||,
                                    "inbytes="||sf_Get_Var("srvhook."port".inbytes", "Unknown")||,
                                    " outbytes="||sf_Get_Var("srvhook."port".outbytes", "Unknown")
            call Sf_Drop_Var "srvhook."||port
            call sf_Set_Var "srvhook.list."listnum".done", 1
        end
    end

return

showbed:

parse arg port
    secs = (dt - sf_Get_Var("srvhook."||port||".date", dt))*(60*60*24) + (tm - sf_Get_Var("srvhook."||port||".time", tm -1))
    call lineout tracefile, DATE("E")||' '||TIME("L")||" User "||sf_Get_Var("srvhook."||port||".name", "Unknown")||" logged out. Port="||port||" User was online "||secs||" seconds. Warning: No response from stack"
    call Sf_Drop_Var "srvhook."||port
return

