1 proc defset {varname val} {
3 if {![info exists var]} { set var $val }
10 defset ownfullname "testing bot"
11 defset ownmailaddr test-irc-bot@example.com
13 defset musthaveping_ms 10000
15 defset out_interval 2100
16 defset out_lag_lag 5000
17 defset out_lag_very 25000
19 defset marktime_min 300
20 defset marktime_join_startdelay 5000
22 proc manyset {list args} {
23 foreach val $list var $args {
29 proc try_except_finally {try except finally} {
30 global errorInfo errorCode
31 set er [catch { uplevel 1 $try } emsg]
35 if {[catch { uplevel 1 $except } emsg3]} {
36 append ei "\nALSO ERROR HANDLING ERROR:\n$emsg3"
39 set er2 [catch { uplevel 1 $finally } emsg2]
42 append ei "\nALSO ERROR CLEANING UP:\n$emsg2"
44 return -code $er -errorinfo $ei -errorcode $ec $emsg
46 return -code $er2 -errorinfo $errorInfo -errorcode $errorCode $emsg2
54 global out_queue out_creditms out_creditat out_interval out_maxburst
55 global out_lag_lag out_lag_very
56 #set pr [lindex [info level 0] 0]
57 #puts $pr>[clock seconds]|$out_creditat|$out_creditms|[llength $out_queue]<
63 if {[llength $out_queue]*$out_interval > $out_lag_very} {
65 } elseif {[llength $out_queue]*$out_interval > $out_lag_lag} {
75 set now [clock seconds]
76 incr out_creditms [expr {($now - $out_creditat) * 1000}]
78 if {$out_creditms > $out_maxburst*$out_interval} {
79 set out_creditms [expr {$out_maxburst*$out_interval}]
84 proc out_runqueue {now} {
88 while {[llength $out_queue] && $out_creditms >= $out_interval} {
89 #puts rq>$now|$out_creditat|$out_creditms|[llength $out_queue]<
90 manyset [lindex $out_queue 0] orgwhen msg
91 set out_queue [lrange $out_queue 1 end]
92 if {[llength $out_queue]} {
93 append orgwhen "+[expr {$now - $orgwhen}]"
94 append orgwhen "([llength $out_queue])"
96 puts "$orgwhen -> $msg"
98 incr out_creditms -$out_interval
100 if {[llength $out_queue]} {
101 after $out_interval out_nextmessage
105 proc out_nextmessage {} {
107 set now [clock seconds]
108 incr out_creditms $out_interval
109 set out_creditat $now
113 proc sendout_priority {priority command args} {
114 global sock out_queue
115 if {[llength $args]} {
116 set la [lindex $args end]
117 set args [lreplace $args end end]
119 if {[regexp {[: ]} $i]} {
120 error "bad argument in output $i ($command $args)"
125 set args [lreplace $args 0 -1 $command]
126 set string [join $args { }]
127 set now [clock seconds]
128 set newe [list $now $string]
130 set out_queue [concat [list $newe] $out_queue]
132 lappend out_queue $newe
134 if {[llength $out_queue] == 1} {
139 proc sendout {command args} { eval sendout_priority [list 0 $command] $args }
145 proc logerror {data} {
150 global saveei saveec errorInfo errorCode
152 set saveei $errorInfo
153 set saveec $errorCode
155 puts ">$saveec|$saveei<"
165 global sock nick calling_nick errorInfo errorCode line_org_1char
167 if {[gets $sock line] == -1} { fail "EOF/error on input" }
168 set line_org_1char [string range $line 0 0]
169 regsub -all "\[^ -\176\240-\376\]" $line ? line
174 if {[regexp -nocase {^:([^ ]+) (.*)} $line dummy prefix remain]} {
176 if {[regexp {^([^!]+)!} $prefix dummy maybenick]} {
177 set calling_nick $maybenick
178 if {"[irctolower $maybenick]" == "[irctolower $nick]"} return
183 if {![string length $line]} { return }
184 if {![regexp -nocase {^([0-9a-z]+) *(.*)} $line dummy command line]} {
185 log "bad command: $org"
188 set command [string toupper $command]
190 while {[regexp {^([^ :]+) *(.*)} $line dummy thisword line]} {
191 lappend params $thisword
193 if {[regexp {^:(.*)} $line dummy thisword]} {
194 lappend params $thisword
195 } elseif {[string length $line]} {
196 log "junk at end: $org"
199 if {"$command" == "PRIVMSG" && \
200 [privmsg_unlogged $prefix [ischan [lindex $params 0]] $params]} {
203 log "[clock seconds] <- $org"
204 set procname msg_$command
205 if {[catch { info body $procname }]} { return }
207 eval [list $procname $prefix $command] $params
209 logerror "error: $emsg ($prefix $command $params)"
214 proc sendprivmsg {dest l} {
215 foreach v [split $l "\n"] {
216 sendout [expr {[ischan $dest] ? "PRIVMSG" : "NOTICE"}] $dest $v
219 proc sendaction_priority {priority dest what} {
220 sendout_priority $priority PRIVMSG $dest "\001ACTION $what\001"
222 proc msendprivmsg {dest ll} { foreach l $ll { sendprivmsg $dest $l } }
223 proc msendprivmsg_delayed {delay dest ll} { after $delay [list msendprivmsg $dest $ll] }
225 proc check_nick {n} {
226 if {[regexp -nocase {[^][\\`_^{|}a-z0-9-]} $n]} { error "bad char in nick" }
227 if {[regexp {^[-0-9]} $n]} { error "bad nick start" }
231 return [regexp {^[&#+!]} $dest]
234 proc irctolower {v} {
235 foreach {from to} [list "\\\[" "{" \
239 regsub -all $from $v $to v
241 return [string tolower $v]
244 proc prefix_none {} {
246 if {[string length $p]} { error "prefix specified" }
249 proc prefix_nick {} {
253 if {![regexp {^([^!]+)!} $p dummy n]} { error "not from nick" }
255 if {"[irctolower $n]" == "[irctolower $nick]"} {
256 error "from myself" {} {}
260 proc msg_PING {p c s1} {
261 global musthaveping_after
264 if {[info exists musthaveping_after]} {
265 after cancel $musthaveping_after
266 unset musthaveping_after
271 proc ensure_outqueue {} {
273 if {[info exists out_queue]} return
275 set out_creditat [clock seconds]
277 set out_lag_reported 0
278 set out_lag_reportwhen $out_creditat
282 logerror "failing: $msg"
286 proc ensure_connecting {} {
287 global sock ownfullname host port nick socketargs
288 global musthaveping_ms musthaveping_after
292 if {[info exists sock]} return
293 set sock [eval socket $socketargs [list $host $port]]
294 fconfigure $sock -buffering line
295 fconfigure $sock -translation crlf
297 sendout USER blight 0 * $ownfullname
299 fileevent $sock readable onread
301 set musthaveping_after [after $musthaveping_ms \
302 {fail "no ping within timeout"}]