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
167 if {[gets $sock line] == -1} { fail "EOF/error on input" }
168 regsub -all "\[^ -\176\240-\376\]" $line ? line
173 catch { unset calling_nick }
177 if {[regexp -nocase {^:([^ ]+) (.*)} $line dummy prefix remain]} {
179 if {[regexp {^([^!]+)!} $prefix dummy maybenick]} {
180 set calling_nick $maybenick
181 if {"[irctolower $maybenick]" == "[irctolower $nick]"} return
186 if {![string length $line]} { return }
187 if {![regexp -nocase {^([0-9a-z]+) *(.*)} $line dummy command line]} {
188 log "bad command: $org"
191 set command [string toupper $command]
193 while {[regexp {^([^ :]+) *(.*)} $line dummy thisword line]} {
194 lappend params $thisword
196 if {[regexp {^:(.*)} $line dummy thisword]} {
197 lappend params $thisword
198 } elseif {[string length $line]} {
199 log "junk at end: $org"
202 if {"$command" == "PRIVMSG" && [privmsg_unlogged $prefix $params]} {
205 log "[clock seconds] <- $org"
206 set procname msg_$command
207 if {[catch { info body $procname }]} { return }
209 eval [list $procname $prefix $command] $params
211 logerror "error: $emsg ($prefix $command $params)"
216 proc sendprivmsg {dest l} {
217 foreach v [split $l "\n"] {
218 sendout [expr {[ischan $dest] ? "PRIVMSG" : "NOTICE"}] $dest $v
221 proc sendaction_priority {priority dest what} {
222 sendout_priority $priority PRIVMSG $dest "\001ACTION $what\001"
224 proc msendprivmsg {dest ll} { foreach l $ll { sendprivmsg $dest $l } }
225 proc msendprivmsg_delayed {delay dest ll} { after $delay [list msendprivmsg $dest $ll] }
227 proc check_nick {n} {
228 if {[regexp -nocase {[^][\\`_^{|}a-z0-9-]} $n]} { error "bad char in nick" }
229 if {[regexp {^[-0-9]} $n]} { error "bad nick start" }
233 return [regexp {^[&#+!]} $dest]
236 proc irctolower {v} {
237 foreach {from to} [list "\\\[" "{" \
241 regsub -all $from $v $to v
243 return [string tolower $v]
246 proc msg_PING {p c s1} {
247 global musthaveping_after
250 if {[info exists musthaveping_after]} connected