1 # Code for starting up bnbot
3 proc def_bnbot
{name argl body
} {
4 proc "bnbot_$name" [concat botid
$argl] \
10 global bnbot_callervars
13 host port nick pass channel
14 chan mbokafter state chanfn
15 } $bnbot_callervars] {
16 uplevel 1 [list upvar #0 "bot/$botid/$v" bn$v]
20 def_bnbot ensure_connecting
{} {
21 global muststartby_ms bnbot
23 if {[info exists bnchan
]} return
25 set bnchan
[open [list |
$bnbot $bnhost $bnport] w
+]
26 fconfigure $bnchan -buffering line
27 set bnmbokafter
[after $muststartby_ms \
28 "fail {bot $botid not ok within timeout}"]
30 fileevent $bnchan readable
[list bnbot_onread
$botid]
33 def_bnbot write
{str
} {
34 log
"[clock seconds] -$botid-> $str"
38 def_bnbot writemsg
{str
} {
39 if {[regexp {^
/} $str]} { set str
" $str" }
40 bnbot_write
$botid $str
43 def_bnbot onread
{args
} {
45 if {[gets $bnchan l
] == -1} { fail
"bot $botid EOF/error on input" }
46 if {[regexp {^
1005 TALK
([^
]+) \w
+ \"(.
*)\"$} $l dummy n
text]} {
47 sendprivmsg
$channel "\[$n] $text"
49 } elseif
{[regexp {^
1023 EMOTE
([^
]+) \w
+ \"(.
*)\"$} $l dummy n
text]} {
50 if {![ircnick_compare
$n $bnnick]} return
51 sendprivmsg
$channel "* $n $text"
54 log
"[clock seconds] <-$botid- $l"
55 if {[string length
$bnstate] && [regexp "^$bnstate" $l]} {
56 switch -exact $bnstate {
57 Connected
{ set bnstate Username
}
58 Username
{ set bnstate Password
; bnbot_write
$botid $bnnick }
60 set bnstate
"1007 CHANNEL"
65 bnbot_write
$botid "/CHANNEL $bnchannel"
67 default { error "wrong bnstate: $bnstate" }
69 } elseif
{[regexp {^
1007 CHANNEL
"(.*)"} $l dummy bnchanfn
]} {
70 after cancel
$bnmbokafter
72 } elseif
{[info exists bnchanfn
]} {