#--------------------------------------------------------------------- # demo-listen.tcl # Tcl script for irc bot eggdrop # demo of the listen/control commands # # v0: 30-Jan-2003 #--------------------------------------------------------------------- package require eggdrop 1.6.13 package require Tcl 8.0 #--------------------------------------------------------------------- # define a port the bot will listen on #--------------------------------------------------------------------- set demolistenport 4000 #--------------------------------------------------------------------- # Attempt to open the listening port. # If the system decides for whatever reason to open a different port # then the [listen] command returns a different port. # In case the system can't "grab a nearby port" eggdrop aborts by # itself. Such situation can be [catch]'ed (not implemented here). #--------------------------------------------------------------------- set demolistenopen [listen $demolistenport script demolisten:onconnect] if { $demolistenopen != $demolistenport } { putlog "PANIC!!! system changed listen port. Purged listen." listen $demolistenopen off } #--------------------------------------------------------------------- # Upon an incoming connection the procedure "demolisten:onconnect" is # called with one argument: the idx of the connection. # Using the idx it is now possible to look up the hostname/ip of the # connection. # This procedure must then hand over "control" to a next procedure # which handles the incoming text. #--------------------------------------------------------------------- proc demolisten:onconnect { idx } { # iterate on the [dcclist] and find the corresponding idx. foreach connection [dcclist] { if {[lindex $connection 0] == $idx} { putlog "HANDLE ($idx): [lindex $connection 1]" putlog "HOSTNAME ($idx): [lindex $connection 2]" putlog "TYPE ($idx): [lindex $connection 3]" putlog "OTHER ($idx): [lindex $connection 4]" putlog "TIMESTAMP ($idx): [lindex $connection 5]" break } } # hand over control to the "demolisten:observe" proc. control $idx demolisten:observe return 0 } #--------------------------------------------------------------------- # Upon a succesfull connection control is handed over to the # "demolisten:observe" proc. This procedure handles all incoming text # and returns control back to eggdrop when the user terminates the # connection. #--------------------------------------------------------------------- proc demolisten:observe { idx text } { # An empty text has the meaning of a user terminated connection. # In this case "return 1" to return control back to eggdrop. if { $text == "" } { putlog "Connection ($idx) terminated by user." return 1 } # allow quitting by typing BYE if { $text == "BYE" } { putlog "User ($idx) desires termination. Terminated!" # it is possible to kill the connection using [killdcc] # killdcc $idx # return 1: control back to eggdrop, eggdrop closes connection. return 1 } # Putlog text. putlog "OBSERVE ($idx): $text" # "return 0" indicates that control stays at this proc i.e. all # new incoming text will come to this proc. return 0 } putlog "Demo listen version 0 loaded."