1 proc defset
{varname val
} {
3 if {![info exists var
]} { set var
$val }
11 defset ownfullname
"testing bot"
12 defset ownmailaddr test-irc-bot
@example.com
14 defset muststartby_ms
10000
16 defset out_interval
2100
17 defset out_lag_lag
5000
18 defset out_lag_very
25000
19 defset ownping_every
300000
21 defset marktime_min
300
22 defset marktime_join_startdelay
5000
24 proc manyset
{list args
} {
25 foreach val
$list var
$args {
31 proc try_except_finally
{try except finally
} {
32 global errorInfo errorCode
33 set er
[catch { uplevel 1 $try } emsg
]
37 if {[catch { uplevel 1 $except } emsg3
]} {
38 append ei
"\nALSO ERROR HANDLING ERROR:\n$emsg3"
41 set er2
[catch { uplevel 1 $finally } emsg2
]
44 append ei
"\nALSO ERROR CLEANING UP:\n$emsg2"
46 return -code $er -errorinfo $ei -errorcode $ec $emsg
48 return -code $er2 -errorinfo $errorInfo -errorcode $errorCode $emsg2
56 global out_queue out_creditms out_creditat out_interval out_maxburst
57 global out_lag_lag out_lag_very
58 #set pr [lindex [info level 0] 0]
59 #puts $pr>[clock seconds]|$out_creditat|$out_creditms|[llength $out_queue]<
65 if {[llength $out_queue]*$out_interval > $out_lag_very} {
67 } elseif
{[llength $out_queue]*$out_interval > $out_lag_lag} {
77 set now
[clock seconds
]
78 incr out_creditms
[expr {($now - $out_creditat) * 1000}]
80 if {$out_creditms > $out_maxburst*$out_interval} {
81 set out_creditms
[expr {$out_maxburst*$out_interval}]
86 proc out_runqueue
{now
} {
90 while {[llength $out_queue] && $out_creditms >= $out_interval} {
91 #puts rq>$now|$out_creditat|$out_creditms|[llength $out_queue]<
92 manyset
[lindex $out_queue 0] orgwhen msg
93 set out_queue
[lrange $out_queue 1 end
]
94 if {[llength $out_queue]} {
95 append orgwhen
"+[expr {$now - $orgwhen}]"
96 append orgwhen
"([llength $out_queue])"
98 puts "$orgwhen -> $msg"
100 incr out_creditms
-$out_interval
102 if {[llength $out_queue]} {
103 after $out_interval out_nextmessage
107 proc out_nextmessage
{} {
109 set now
[clock seconds
]
110 incr out_creditms
$out_interval
111 set out_creditat
$now
115 proc sendout_priority
{priority command args
} {
116 global sock out_queue
117 if {[llength $args]} {
118 set la
[lindex $args end
]
119 set args
[lreplace $args end end
]
121 if {[regexp {[: ]} $i]} {
122 error "bad argument in output $i ($command $args)"
127 set args
[lreplace $args 0 -1 $command]
128 set string [join $args { }]
129 set now
[clock seconds
]
130 set newe
[list $now $string]
132 set out_queue
[concat [list $newe] $out_queue]
134 lappend out_queue
$newe
136 if {[llength $out_queue] == 1} {
141 proc sendout
{command args
} { eval sendout_priority
[list 0 $command] $args }
147 proc logerror
{data
} {
152 global saveei saveec errorInfo errorCode
154 set saveei
$errorInfo
155 set saveec
$errorCode
157 puts ">$saveec|$saveei<"
167 global sock nick calling_nick errorInfo errorCode line_org_endchar
169 if {[catch { gets $sock line
} rv
]} { fail
"error on input: $rv" }
170 if {$rv == -1} { fail
"EOF on input" }
172 set line_org_endchar
[string range
$line end end
]
173 regsub -all "\[^ -\176\240-\376\]" $line ? line
178 if {[regexp -nocase {^
:([^
]+) (.
*)} $line dummy prefix remain
]} {
180 if {[regexp {^
([^
!]+)!} $prefix dummy maybenick
]} {
181 set calling_nick
$maybenick
182 if {"[irctolower $maybenick]" == "[irctolower $nick]"} return
187 if {![string length
$line]} { return }
188 if {![regexp -nocase {^
([0-9a-z
]+) *(.
*)} $line dummy command line
]} {
189 log
"bad command: $org"
192 set command
[string toupper
$command]
194 while {[regexp {^
([^
:]+) *(.
*)} $line dummy thisword line
]} {
195 lappend params
$thisword
197 if {[regexp {^
:(.
*)} $line dummy thisword
]} {
198 lappend params
$thisword
199 } elseif
{[string length
$line]} {
200 log
"junk at end: $org"
203 if {"$command" == "PRIVMSG" && \
204 [privmsg_unlogged
$prefix [ischan
[lindex $params 0]] $params]} {
207 log
"[clock seconds] <- $org"
208 set procname msg_
$command
209 if {[catch { info body
$procname }]} { return }
211 eval [list $procname $prefix $command] $params
213 logerror
"error: $emsg ($prefix $command $params)"
218 proc sendprivmsg
{dest l
} {
219 foreach v
[split $l "\n"] {
220 sendout
[expr {[ischan
$dest] ?
"PRIVMSG" : "NOTICE"}] $dest $v
223 proc sendaction_priority
{priority dest what
} {
224 sendout_priority
$priority PRIVMSG
$dest "\001ACTION $what\001"
226 proc msendprivmsg
{dest ll
} { foreach l
$ll { sendprivmsg
$dest $l } }
227 proc msendprivmsg_delayed
{delay dest ll
} { after $delay [list msendprivmsg
$dest $ll] }
229 proc check_nick
{n
} {
230 if {[regexp -nocase {[^
][\\`_^
{|
}a-z0-9-
]} $n]} { error "bad char in nick" }
231 if {[regexp {^
[-0-9]} $n]} { error "bad nick start" }
235 return [regexp {^
[&#+!]} $dest]
238 proc irctolower
{v
} {
239 foreach {from to
} [list "\\\[" "{" \
243 regsub -all $from $v $to v
245 return [string tolower
$v]
248 proc prefix_none
{} {
250 if {[string length
$p]} { error "prefix specified" }
253 proc prefix_nick
{} {
257 if {![regexp {^
([^
!]+)!} $p dummy n
]} { error "not from nick" }
259 if {"[irctolower $n]" == "[irctolower $nick]"} {
260 error "from myself" {} {}
264 proc msg_PING
{p c s1
} {
269 proc sendownping
{} {
270 global ownping_every nick
272 after $ownping_every sendownping
275 proc msg_001
{args
} {
276 global muststartby_after
277 if {[info exists muststartby_after
]} {
278 after cancel
$muststartby_after
279 unset muststartby_after
285 proc ensure_outqueue
{} {
287 if {[info exists out_queue
]} return
289 set out_creditat
[clock seconds
]
291 set out_lag_reported
0
292 set out_lag_reportwhen
$out_creditat
296 logerror
"failing: $msg"
300 proc ensure_connecting
{} {
301 global sock ownfullname host port nick ident socketargs
302 global muststartby_ms muststartby_after
306 if {[info exists sock
]} return
307 set sock
[eval socket $socketargs [list $host $port]]
308 fconfigure $sock -buffering line
309 fconfigure $sock -translation crlf
311 sendout USER
$ident 0 * $ownfullname
313 fileevent $sock readable onread
315 set muststartby_after
[after $muststartby_ms \
316 {fail
"no successfuly connect within timeout"}]