chiark / gitweb /
Dies if doesn't get a ping. Configs separated.
[ircbot] / bot.tcl
diff --git a/bot.tcl b/bot.tcl
index 240e70b774ad9348e3453be2462fe2ef8db6d611..5d19dd063f6c6db8fb2f5a33e4f31b7c593250c3 100755 (executable)
--- a/bot.tcl
+++ b/bot.tcl
@@ -1,32 +1,22 @@
-#!/usr/bin/tclsh8.2
+# Core bot code
 
-set host chiark
-set port 6667
-if {![info exists nick]} { set nick Blight }
-if {![info exists ownfullname]} { set ownfullname "here to Help" }
-set ownmailaddr blight@chiark.greenend.org.uk
+proc defset {varname val} {
+    upvar #0 $varname var
+    if {![info exists var]} { set var $val }
+}
 
-set out_maxburst 6
-set out_interval 2100
-set out_lag_lag 5000
-set out_lag_very 25000
+# must set host
+defset port 6667
 
-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
-}
+defset nick testbot
+defset ownfullname "testing bot"
+defset ownmailaddr test-irc-bot@example.com
 
-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
-}
+defset musthaveping_ms 10000
+defset out_maxburst 6
+defset out_interval 2100
+defset out_lag_lag 5000
+defset out_lag_very 25000
 
 proc manyset {list args} {
     foreach val $list var $args {
@@ -173,7 +163,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 +235,13 @@ 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
+       unset musthaveping_after
+    }
 }
 
 proc check_nick {n} {
@@ -1200,19 +1195,51 @@ 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
-
-#if {![regexp {tclsh} $argv0]} {
-#    vwait terminate
-#}
+ensure_connecting