chiark / gitweb /
Reorganise a bit. Now fails if the server goes away, or it fails to connect properly.
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 13 Dec 2000 20:30:17 +0000 (20:30 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 13 Dec 2000 20:30:17 +0000 (20:30 +0000)
bot.tcl

diff --git a/bot.tcl b/bot.tcl
index 240e70b774ad9348e3453be2462fe2ef8db6d611..55d93c7b3295105cccfd33d34713a716b4ac60d5 100755 (executable)
--- a/bot.tcl
+++ b/bot.tcl
@@ -6,28 +6,12 @@ if {![info exists nick]} { set nick Blight }
 if {![info exists ownfullname]} { set ownfullname "here to Help" }
 set ownmailaddr blight@chiark.greenend.org.uk
 
+set musthaveping_ms 10000
 set out_maxburst 6
 set out_interval 2100
 set out_lag_lag 5000
 set out_lag_very 25000
 
-if {![info exists out_queue]} {
-    set out_creditms [expr {$out_maxburst*$out_interval}]
-    set out_creditat [clock seconds]
-    set out_queue {}
-    set out_lag_reported 0
-    set out_lag_reportwhen $out_creditat
-}
-
-if {![info exists globalsecret]} {
-    set gsfile [open /dev/urandom r]
-    fconfigure $gsfile -translation binary
-    set globalsecret [read $gsfile 32]
-    binary scan $globalsecret H* globalsecret
-    close $gsfile
-    unset gsfile
-}
-
 proc manyset {list args} {
     foreach val $list var $args {
        upvar 1 $var my
@@ -173,7 +157,7 @@ proc bgerror {msg} {
 proc onread {args} {
     global sock nick calling_nick errorInfo errorCode
     
-    if {[gets $sock line] == -1} { set terminate 1; return }
+    if {[gets $sock line] == -1} { fail "EOF/error on input" }
     regsub -all "\[^ -\176\240-\376\]" $line ? line
     set org $line
     
@@ -245,8 +229,10 @@ proc prefix_none {} {
 }
 
 proc msg_PING {p c s1} {
+    global musthaveping_after
     prefix_none
     sendout PONG $s1
+    if {[info exists musthaveping_after]} { after cancel $musthaveping_after] }
 }
 
 proc check_nick {n} {
@@ -1200,18 +1186,54 @@ def_ucmd seen {
     ucmdr {} $rstr
 }
 
-if {![info exists sock]} {
+proc ensure_globalsecret {} {
+    global globalsecret
+    
+    if {[info exists globalsecret]} return
+    set gsfile [open /dev/urandom r]
+    fconfigure $gsfile -translation binary
+    set globalsecret [read $gsfile 32]
+    binary scan $globalsecret H* globalsecret
+    close $gsfile
+    unset gsfile
+}
+
+proc ensure_outqueue {} {
+    out__vars
+    if {[info exists out_queue]} return
+    set out_creditms [expr {$out_maxburst*$out_interval}]
+    set out_creditat [clock seconds]
+    set out_queue {}
+    set out_lag_reported 0
+    set out_lag_reportwhen $out_creditat
+}
+
+proc fail {msg} {
+    logerror "failing: $msg"
+    exit 1
+}
+
+proc ensure_connecting {} {
+    global sock ownfullname host port nick
+    global musthaveping_ms musthaveping_after
+    
+    if {[info exists sock]} return
     set sock [socket $host $port]
     fconfigure $sock -buffering line
-    #fconfigure $sock -translation binary
     fconfigure $sock -translation crlf
 
     sendout USER blight 0 * $ownfullname
     sendout NICK $nick
     fileevent $sock readable onread
+
+    set musthaveping_after [after $musthaveping_ms \
+           {fail "no ping within timeout"}]
 }
 
+ensure_globalsecret
+ensure_outqueue
 loadhelp
+ensure_connecting
 
 #if {![regexp {tclsh} $argv0]} {
 #    vwait terminate