All pastes #265830 Raw Edit

Untitled

public text v1 · immutable
·published 2006-12-03 18:05 UTC
rendered paste body
set ircsnapshot_port 1234

#---------------------------------------------------
# script comes from here

proc ircsnapshot_StartServer {port} {
    set s [socket -server ircsnapshot_OnAccept $port]
    putlog "\[ircsnapshot\] Startred"
}

proc ircsnapshot_OnAccept {sock addr port} {
    global ircsnapshot_TimeoutTimers

    fconfigure $sock -buffering line -blocking no
    puts $sock "HELLO_FROM_IRCSNAPSHOT_SERVER"
    fileevent $sock readable [list ircsnapshot_OnReceive $sock]
    putlog "\[ircsnapshot\] Accepted connection from $addr:$port -> $sock"
    set ircsnapshot_TimeoutTimers($sock) [utimer 30 [list ircsnapshot_TimeOut $sock]]
}

proc ircsnapshot_TimeOut {sock} {
    global ircsnapshot_TimeoutTimers

    close $sock
    putlog "\[ircsnapshot\] $sock timeout, closed..."
    unset ircsnapshot_TimeoutTimers($sock)
}

proc ircsnapshot_OnReceive {sock} {
    global ircsnapshot_TimeoutTimers

putlog "AHA!!"

    killutimer $ircsnapshot_TimeoutTimers($sock)
    unset ircsnapshot_TimeoutTimers($sock)

    if {not ([eof $sock] && [catch {gets $sock line})]} {
        putlog "\[ircsnapshot\] $sock sent: $line"
        if {$line == "GETSNAPSHOT"} {
            puts $sock "hello"
        } else {
            puts $sock "BAD_PROTOCOL"
        }
    }
    putlog "\[ircsnapshot\] $sock closed."
    close $sock
}