chiark / gitweb /
topicedit: add a timeout; better error handling
[ircbot.git] / topicedit.tcl
1 #!/usr/bin/tclsh8.5
2 # usage:
3 #   cd ../ircbot
4 #   ./topicedit.tcl SERVER PORT NICK IDENT FULLNAME CHANNEL TOPICINFO
5
6 source irccore.tcl
7
8 manyset $argv host port nick ident ownfullname channel topicinfo
9
10 set done 0
11
12 after 30000 { set errorInfo {}; bgerror timeout }
13
14 proc bgerror {msg} {
15     global errorInfo errorCode
16     puts stderr "$msg\n$errorCode\n$errorInfo\n\nERROR: $msg\n"
17     exit 16
18 }
19
20 proc connected {} {
21     global channel
22     sendout JOIN $channel
23 }
24 proc new_event {} { }
25 proc privmsg_unlogged {args} { return 1 }
26 proc msg_332 {server code us channel topic args} {
27     global oldtopic nick
28     if {[string compare $nick $us]} return
29     set oldtopic $topic
30 }
31 proc msg_333 {server code us channel setter when} {
32     global nick done
33     # might happen as a result of us connecting, or as a result of
34     # our own TOPIC query
35     set now [clock seconds]
36     if {![string compare $nick $setter] && $when > $now - 3600} {
37         if {$done} {
38             puts "topic set."
39             exit 0
40         } else {
41             puts stderr "*** topic recently set by us, not setting again!"
42             exit 1
43         }
44     }
45 }
46 proc msg_366 {args} {
47     global oldtopic topicinfo channel done
48     if {![info exists oldtopic]} return
49     set newtopic "$topicinfo | $oldtopic"
50     sendout TOPIC $channel $newtopic
51     sendout TOPIC $channel
52     set done 1
53 }
54
55 ensure_connecting
56 vwait forever