#--------------------------------------------------------------------- # sockreport.tcl # tcl script for IRC bot eggdrop # # open a listen port and echo back to client. # # modified version for eggdrop from original sample at: # http://stage.caldera.com/Technology/tcl/SocketExample.html # http://www.cab.u-szeged.hu/local/doc/tcl/SocketExample.html # # v0: 09-Oct-2003 #--------------------------------------------------------------------- package require eggdrop 1.6.15 package require Tcl 8.4 set svcPort 31000 #--------------------------------------------------------------------- # Accept-Connection handler for Server. # called When client makes a connection to the server # Its passed the channel we're to communicate with the client on, # The address of the client and the port we're using # # Setup a handler for (incoming) communication on # the client channel - send connection Reply and log connection #--------------------------------------------------------------------- proc accept {sock addr port} { # if {[badConnect $addr]} { # close $sock # return # } # Setup handler for future communication on client socket fileevent $sock readable [list svcHandler $sock] # Note we've accepted a connection (show how get peer # info fm socket) socklog $sock "Accept from [fconfigure $sock -peername]" # Read client input in lines, disable blocking I/O # disable blocking I/O allows normal eggdrop operation fconfigure $sock -buffering line -blocking 0 # no buffering can be used for http requests # fconfigure $sock -buffering none -blocking 0 # Send Acceptance string to client puts $sock "$addr:$port, You are connected to the echo server." puts $sock "It is now [exec date]" # log the connection socklog $sock "Accepted connection from $addr at [exec date]" } #--------------------------------------------------------------------- # Handles the input from the client and client shutdown #--------------------------------------------------------------------- proc svcHandler { sock } { socklog $sock "svcHandler called." # read socket till exhausted # original sample did one get and then waited for next fileevent. # loop in eggdrop is quite slow, read as much as possible here. # read line by line while { [gets $sock l] != -1 } { doService $sock $l } # read the sock at once # body in POST method of http does not end with newline # set l [read $sock] # doService $sock $l # client disconnected? if {[eof $sock]} { close $sock socklog $sock "socket closed." } } #--------------------------------------------------------------------- # Implement the service # This example just writes the info back to the client... #--------------------------------------------------------------------- proc doService { sock msg } { puts $sock "Echo: $msg" socklog $sock $msg } proc socklog { sock text } { putlog "SOCKREPORT: ($sock) $text" } #--------------------------------------------------------------------- # open a listening port # Create a server socket on port $svcPort. # Call proc accept when a client attempts a connection. #--------------------------------------------------------------------- if { [info exists sock] } { close $sock socklog $sock "Closed listening socket." } if { [catch {set sock [socket -server accept $svcPort]} errmsg] } { socklog - "ERROR ($errmsg)" } else { socklog $sock "Opened listen sock on port $svcPort." } putlog "Loaded (version 0): sockreport."