chiark / gitweb /
Add people to the relevant chan_nicks when they join. New leave channel manager...
[ircbot] / bot.tcl
CommitLineData
e58e51a8 1# Core bot code
9bc33297 2
e58e51a8
IJ
3proc defset {varname val} {
4 upvar #0 $varname var
5 if {![info exists var]} { set var $val }
6}
7
8# must set host
9defset port 6667
10
11defset nick testbot
12defset ownfullname "testing bot"
13defset ownmailaddr test-irc-bot@example.com
e1ba63be 14
e58e51a8
IJ
15defset musthaveping_ms 10000
16defset out_maxburst 6
17defset out_interval 2100
18defset out_lag_lag 5000
19defset out_lag_very 25000
7818b6af 20
20087363
IJ
21proc manyset {list args} {
22 foreach val $list var $args {
23 upvar 1 $var my
24 set my $val
25 }
26}
27
7a70431a
IJ
28proc try_except_finally {try except finally} {
29 global errorInfo errorCode
30 set er [catch { uplevel 1 $try } emsg]
31 if {$er} {
32 set ei $errorInfo
33 set ec $errorCode
34 if {[catch { uplevel 1 $except } emsg3]} {
35 append ei "\nALSO ERROR HANDLING ERROR:\n$emsg3"
36 }
37 }
38 set er2 [catch { uplevel 1 $finally } emsg2]
39 if {$er} {
40 if {$er2} {
41 append ei "\nALSO ERROR CLEANING UP:\n$emsg2"
42 }
43 return -code $er -errorinfo $ei -errorcode $ec $emsg
44 } elseif {$er2} {
45 return -code $er2 -errorinfo $errorInfo -errorcode $errorCode $emsg2
46 } else {
47 return $emsg
48 }
49}
50
7818b6af
IJ
51proc out__vars {} {
52 uplevel 1 {
53 global out_queue out_creditms out_creditat out_interval out_maxburst
54 global out_lag_lag out_lag_very
55#set pr [lindex [info level 0] 0]
56#puts $pr>[clock seconds]|$out_creditat|$out_creditms|[llength $out_queue]<
57 }
58}
59
60proc out_lagged {} {
61 out__vars
62 if {[llength $out_queue]*$out_interval > $out_lag_very} {
63 return 2
64 } elseif {[llength $out_queue]*$out_interval > $out_lag_lag} {
65 return 1
66 } else {
67 return 0
68 }
69}
70
71proc out_restart {} {
72 out__vars
73
74 set now [clock seconds]
75 incr out_creditms [expr {($now - $out_creditat) * 1000}]
76 set out_creditat $now
77 if {$out_creditms > $out_maxburst*$out_interval} {
78 set out_creditms [expr {$out_maxburst*$out_interval}]
79 }
80 out_runqueue $now
81}
82
83proc out_runqueue {now} {
cc2d31de 84 global sock
7818b6af
IJ
85 out__vars
86
87 while {[llength $out_queue] && $out_creditms >= $out_interval} {
88#puts rq>$now|$out_creditat|$out_creditms|[llength $out_queue]<
89 manyset [lindex $out_queue 0] orgwhen msg
90 set out_queue [lrange $out_queue 1 end]
91 if {[llength $out_queue]} {
92 append orgwhen "+[expr {$now - $orgwhen}]"
dce9eb94 93 append orgwhen "([llength $out_queue])"
7818b6af
IJ
94 }
95 puts "$orgwhen -> $msg"
96 puts $sock $msg
97 incr out_creditms -$out_interval
98 }
99 if {[llength $out_queue]} {
100 after $out_interval out_nextmessage
101 }
102}
103
104proc out_nextmessage {} {
105 out__vars
106 set now [clock seconds]
107 incr out_creditms $out_interval
108 set out_creditat $now
109 out_runqueue $now
110}
111
112proc sendout_priority {priority command args} {
113 global sock out_queue
9bc33297
IJ
114 if {[llength $args]} {
115 set la [lindex $args end]
116 set args [lreplace $args end end]
117 foreach i $args {
118 if {[regexp {[: ]} $i]} {
cc2d31de 119 error "bad argument in output $i ($command $args)"
9bc33297
IJ
120 }
121 }
cc2d31de 122 lappend args :$la
9bc33297
IJ
123 }
124 set args [lreplace $args 0 -1 $command]
cc2d31de 125 set string [join $args { }]
7818b6af
IJ
126 set now [clock seconds]
127 set newe [list $now $string]
128 if {$priority} {
129 set out_queue [concat [list $newe] $out_queue]
130 } else {
131 lappend out_queue $newe
132 }
133 if {[llength $out_queue] == 1} {
134 out_restart
135 }
9bc33297 136}
9bc33297 137
7818b6af
IJ
138proc sendout {command args} { eval sendout_priority [list 0 $command] $args }
139
9bc33297
IJ
140proc log {data} {
141 puts $data
142}
143
144proc logerror {data} {
145 log $data
cc2d31de
IJ
146}
147
148proc saveeic {} {
149 global saveei saveec errorInfo errorCode
150
151 set saveei $errorInfo
152 set saveec $errorCode
153
154 puts ">$saveec|$saveei<"
155}
156
157proc bgerror {msg} {
158 global save
159 logerror $msg
160 saveeic
161}
9bc33297
IJ
162
163proc onread {args} {
17bfe24c 164 global sock nick calling_nick errorInfo errorCode
9bc33297 165
28c8ab78 166 if {[gets $sock line] == -1} { fail "EOF/error on input" }
cc2d31de 167 regsub -all "\[^ -\176\240-\376\]" $line ? line
9bc33297 168 set org $line
17bfe24c
IJ
169
170 set ei $errorInfo
171 set ec $errorCode
a4a1f396 172 catch { unset calling_nick }
17bfe24c
IJ
173 set errorInfo $ei
174 set errorCode $ec
175
9bc33297
IJ
176 if {[regexp -nocase {^:([^ ]+) (.*)} $line dummy prefix remain]} {
177 set line $remain
a4a1f396
IJ
178 if {[regexp {^([^!]+)!} $prefix dummy maybenick]} {
179 set calling_nick $maybenick
180 if {"[irctolower $maybenick]" == "[irctolower $nick]"} return
181 }
9bc33297
IJ
182 } else {
183 set prefix {}
184 }
cc2d31de 185 if {![string length $line]} { return }
9bc33297
IJ
186 if {![regexp -nocase {^([0-9a-z]+) *(.*)} $line dummy command line]} {
187 log "bad command: $org"
188 return
189 }
cc2d31de 190 set command [string toupper $command]
9bc33297 191 set params {}
cc2d31de 192 while {[regexp {^([^ :]+) *(.*)} $line dummy thisword line]} {
9bc33297
IJ
193 lappend params $thisword
194 }
195 if {[regexp {^:(.*)} $line dummy thisword]} {
196 lappend params $thisword
197 } elseif {[string length $line]} {
198 log "junk at end: $org"
199 return
200 }
cc2d31de
IJ
201 if {"$command" == "PRIVMSG" &&
202 [regexp {^[&#+!]} [lindex $params 0]] &&
cf6ea4de 203 ![regexp {^![a-z][-a-z]*[a-z]( .*)?$} [lindex $params 1]]} {
cc2d31de 204 # on-channel message, ignore
422f52e4 205 catch {
83dd1224 206 recordlastseen_p $prefix "talking on [lindex $params 0]" 1
422f52e4 207 }
cc2d31de
IJ
208 return
209 }
83dd1224 210 log "[clock seconds] <- $org"
9bc33297 211 set procname msg_$command
cc2d31de 212 if {[catch { info body $procname }]} { return }
9bc33297
IJ
213 if {[catch {
214 eval [list $procname $prefix $command] $params
215 } emsg]} {
216 logerror "error: $emsg ($prefix $command $params)"
cc2d31de 217 saveeic
9bc33297
IJ
218 }
219}
220
7ce72032 221proc sendprivmsg {dest l} {
20087363
IJ
222 foreach v [split $l "\n"] {
223 sendout [expr {[ischan $dest] ? "PRIVMSG" : "NOTICE"}] $dest $v
224 }
83dd1224 225}
7818b6af
IJ
226proc sendaction_priority {priority dest what} {
227 sendout_priority $priority PRIVMSG $dest "\001ACTION $what\001"
228}
7ce72032
IJ
229proc msendprivmsg {dest ll} { foreach l $ll { sendprivmsg $dest $l } }
230proc msendprivmsg_delayed {delay dest ll} { after $delay [list msendprivmsg $dest $ll] }
83dd1224 231
cc2d31de
IJ
232proc prefix_none {} {
233 upvar 1 p p
9bc33297 234 if {[string length $p]} { error "prefix specified" }
cc2d31de 235}
9bc33297 236
cc2d31de 237proc msg_PING {p c s1} {
28c8ab78 238 global musthaveping_after
cc2d31de
IJ
239 prefix_none
240 sendout PONG $s1
534e26a9 241 if {[info exists musthaveping_after]} connected
9bc33297
IJ
242}
243
cc2d31de
IJ
244proc check_nick {n} {
245 if {[regexp -nocase {[^][\\`_^{|}a-z0-9-]} $n]} { error "bad char in nick" }
246 if {[regexp {^[-0-9]} $n]} { error "bad nick start" }
247}
248
422f52e4
IJ
249proc ischan {dest} {
250 return [regexp {^[&#+!]} $dest]
251}
252
253proc irctolower {v} {
254 foreach {from to} [list "\\\[" "{" \
255 "\\\]" "}" \
256 "\\\\" "|" \
257 "~" "^"] {
258 regsub -all $from $v $to v
259 }
260 return [string tolower $v]
261}
262
cc2d31de
IJ
263proc prefix_nick {} {
264 global nick
265 upvar 1 p p
266 upvar 1 n n
267 if {![regexp {^([^!]+)!} $p dummy n]} { error "not from nick" }
268 check_nick $n
a056c4bd
IJ
269 if {"[irctolower $n]" == "[irctolower $nick]"} {
270 error "from myself" {} {}
271 }
422f52e4
IJ
272}
273
7ce72032 274proc showintervalsecs {howlong} {
d83fb8db
IJ
275 return [showintervalsecs/[opt timeformat] $howlong]
276}
277
278proc showintervalsecs/ks {howlong} {
7ce72032
IJ
279 if {$howlong < 1000} {
280 return "${howlong}s"
83dd1224
IJ
281 } else {
282 if {$howlong < 1000000} {
283 set pfx k
284 set scale 1000
285 } else {
286 set pfx M
287 set scale 1000000
288 }
289 set value [expr "$howlong.0 / $scale"]
290 foreach {min format} {100 %.0f 10 %.1f 1 %.2f} {
291 if {$value < $min} continue
7ce72032 292 return [format "$format${pfx}s" $value]
83dd1224
IJ
293 }
294 }
295}
296
d83fb8db
IJ
297proc format_qty {qty unit} {
298 set o $qty
299 append o " "
300 append o $unit
301 if {$qty != 1} { append o s }
302 return $o
303}
304
305proc showintervalsecs/hms {qty} {
306 set ul {second 60 minute 60 hour 24 day 7 week}
307 set remainv 0
308 while {[llength $ul] > 1 && $qty >= [set uv [lindex $ul 1]]} {
309 set remainu [lindex $ul 0]
310 set remainv [expr {$qty % $uv}]
311 set qty [expr {($qty-$remainv)/$uv}]
312 set ul [lreplace $ul 0 1]
313 }
314 set o [format_qty $qty [lindex $ul 0]]
315 if {$remainv} {
316 append o " "
317 append o [format_qty $remainv $remainu]
318 }
319 return $o
320}
321
7ce72032
IJ
322proc showinterval {howlong} {
323 if {$howlong <= 0} {
324 return {just now}
325 } else {
326 return "[showintervalsecs $howlong] ago"
327 }
328}
329
83dd1224
IJ
330proc showtime {when} {
331 return [showinterval [expr {[clock seconds] - $when}]]
332}
333
334proc def_msgproc {name argl body} {
335 proc msg_$name "varbase $argl" "\
336 upvar #0 msg/\$varbase/dest d\n\
337 upvar #0 msg/\$varbase/str s\n\
338 upvar #0 msg/\$varbase/accum a\n\
339$body"
340}
341
342def_msgproc begin {dest str} {
343 set d $dest
344 set s $str
345 set a {}
346}
347
348def_msgproc append {str} {
349 set ns "$s$str"
350 if {[string length $s] && [string length $ns] > 65} {
351 msg__sendout $varbase
352 set s " [string trimleft $str]"
353 } else {
354 set s $ns
355 }
356}
357
358def_msgproc finish {} {
359 msg__sendout $varbase
360 unset s
361 unset d
362 return $a
363}
364
365def_msgproc _sendout {} {
366 lappend a [string trimright $s]
367 set s {}
368}
369
370proc looking_whenwhere {when where} {
371 set str [showtime [expr {$when-1}]]
372 if {[string length $where]} { append str " on $where" }
373 return $str
374}
375
376proc recordlastseen_n {n how here} {
377 global lastseen lookedfor
422f52e4 378 set lastseen([irctolower $n]) [list $n [clock seconds] $how]
83dd1224
IJ
379 if {!$here} return
380 upvar #0 lookedfor([irctolower $n]) lf
381 if {[info exists lf]} {
382 switch -exact [llength $lf] {
383 0 {
384 set ml {}
385 }
386 1 {
387 manyset [lindex $lf 0] when who where
388 set ml [list \
389 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
390 }
391 default {
392 msg_begin tosend $n "FYI, people have been looking for you:"
393 set i 0
394 set fin ""
395 foreach e $lf {
396 incr i
397 if {$i == 1} {
398 msg_append tosend " "
399 } elseif {$i == [llength $lf]} {
400 msg_append tosend " and "
401 set fin .
402 } else {
403 msg_append tosend ", "
404 }
405 manyset $e when who where
406 msg_append tosend \
407 "$who ([looking_whenwhere $when $where])$fin"
408 }
409 set ml [msg_finish tosend]
410 }
411 }
412 unset lf
413 msendprivmsg_delayed 1000 $n $ml
414 }
422f52e4 415}
281b5f05
IJ
416
417proc note_topic {showoff whoby topic} {
35e19219 418 set msg "FYI, $whoby has changed the topic on $showoff"
281b5f05
IJ
419 if {[string length $topic] < 160} {
420 append msg " to $topic"
421 } else {
422 append msg " but it is too long to reproduce here !"
423 }
424 set showoff [irctolower $showoff]
425 set tell [chandb_get $showoff topictell]
426 if {[lsearch -exact $tell *] >= 0} {
427 set tryspies [chandb_list]
428 } else {
429 set tryspies $tell
430 }
281b5f05
IJ
431 foreach spy $tryspies {
432 set see [chandb_get $spy topicsee]
281b5f05
IJ
433 if {[lsearch -exact $see $showoff] >= 0 || \
434 ([lsearch -exact $see *] >= 0 && \
435 [lsearch -exact $tell $spy] >= 0)} {
436 sendprivmsg $spy $msg
437 }
438 }
439}
440
83dd1224 441proc recordlastseen_p {p how here} {
422f52e4 442 prefix_nick
83dd1224 443 recordlastseen_n $n $how $here
422f52e4
IJ
444}
445
446proc chanmode_arg {} {
447 upvar 2 args cm_args
448 set rv [lindex $cm_args 0]
449 set cm_args [lreplace cm_args 0 0]
450 return $rv
451}
452
83dd1224 453proc chanmode_o1 {m g p chan} {
20087363 454 global nick chan_initialop
83dd1224
IJ
455 prefix_nick
456 set who [chanmode_arg]
457 recordlastseen_n $n "being nice to $who" 1
458 if {"[irctolower $who]" == "[irctolower $nick]"} {
534e26a9
IJ
459 set nlower [irctolower $n]
460 upvar #0 nick_unique($nlower) u
20087363
IJ
461 if {[chandb_exists $chan]} {
462 sendprivmsg $n Thanks.
463 } elseif {![info exists u]} {
464 sendprivmsg $n {Op me while not on the channel, why don't you ?}
465 } else {
466 set chan_initialop([irctolower $chan]) $u
467 sendprivmsg $n \
468 "Thanks. You can use `channel manager ...' to register this channel."
469 if {![nickdb_exists $n] || ![string length [nickdb_get $n username]]} {
470 sendprivmsg $n \
471 "(But to do that you must register your nick securely first.)"
472 }
473 }
83dd1224
IJ
474 }
475}
476
422f52e4
IJ
477proc chanmode_o0 {m g p chan} {
478 global nick chandeop
479 prefix_nick
480 set who [chanmode_arg]
83dd1224 481 recordlastseen_p $p "being mean to $who" 1
422f52e4
IJ
482 if {"[irctolower $who]" == "[irctolower $nick]"} {
483 set chandeop($chan) [list [clock seconds] $p]
484 }
cc2d31de 485}
9bc33297 486
422f52e4
IJ
487proc msg_MODE {p c dest modelist args} {
488 if {![ischan $dest]} return
489 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
490 set give 0
491 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
492 set give 1
493 } else {
494 error "invalid modelist"
495 }
496 foreach m [split $modelist] {
497 set procname chanmode_$m$give
498 if {[catch { info body $procname }]} {
83dd1224 499 recordlastseen_p $p "fiddling with $dest" 1
422f52e4
IJ
500 } else {
501 $procname $m $give $p $dest
502 }
503 }
504}
505
534e26a9
IJ
506proc leaving {lchan} {
507 foreach luser [array names nick_onchans] {
508 upvar #0 nick_onchans($luser) oc
509 set oc [grep tc {"$tc" != "$lchan"} $oc]
510 }
511 upvar #0 chan_nicks($lchan) nlist
512 unset nlist
513}
514
cf6ea4de
IJ
515proc doleave {lchan} {
516 sendout PART $lchan
517 leaving $lchan
518}
519
534e26a9
IJ
520proc dojoin {lchan} {
521 global chan_nicks
522 sendout JOIN $lchan
523 set chan_nicks($lchan) {}
524}
525
526proc check_justme {lchan} {
527 global nick
528 upvar #0 chan_nicks($lchan) nlist
529 if {[llength $nlist] != 1} return
e2af9bcb 530 if {"[lindex $nlist 0]" != "[irctolower $nick]"} return
534e26a9
IJ
531 if {[chandb_exists $lchan]} {
532 set mode [chandb_get $lchan mode]
533 if {"$mode" != "*"} {
534 sendout MODE $lchan $mode
535 }
281b5f05
IJ
536 set topic [chandb_get $lchan topicset]
537 if {[string length $topic]} {
538 sendout TOPIC $lchan $topic
281b5f05 539 }
534e26a9 540 } else {
cf6ea4de 541 doleave $lchan
a4a1f396
IJ
542 }
543}
544
a056c4bd 545proc process_kickpart {chan user} {
a4a1f396 546 global nick
a056c4bd 547 check_nick $user
534e26a9
IJ
548 set luser [irctolower $user]
549 set lchan [irctolower $chan]
a056c4bd 550 if {![ischan $chan]} { error "not a channel" }
534e26a9
IJ
551 if {"$luser" == "[irctolower $nick]"} {
552 leaving $lchan
553 } else {
554 upvar #0 nick_onchans($luser) oc
555 upvar #0 chan_nicks($lchan) nlist
556 set oc [grep tc {"$tc" != "$lchan"} $oc]
557 set nlist [grep tn {"$tn" != "$luser"} $nlist]
558 nick_case $user
559 if {![llength $oc]} {
560 nick_forget $luser
561 } else {
562 check_justme $lchan
563 }
a4a1f396 564 }
534e26a9 565}
a056c4bd 566
281b5f05
IJ
567proc msg_TOPIC {p c dest topic} {
568 prefix_nick
569 if {![ischan $dest]} return
570 recordlastseen_n $n "changing the topic on $dest" 1
571 note_topic [irctolower $dest] $n $topic
572}
573
a056c4bd
IJ
574proc msg_KICK {p c chans users comment} {
575 set chans [split $chans ,]
576 set users [split $users ,]
577 if {[llength $chans] > 1} {
578 foreach chan $chans user $users { process_kickpart $chan $user }
579 } else {
580 foreach user $users { process_kickpart [lindex $chans 0] $user }
581 }
582}
583
584proc msg_KILL {p c user why} {
585 nick_forget $user
586}
587
20087363
IJ
588set nick_counter 0
589set nick_arys {onchans username unique}
534e26a9
IJ
590# nick_onchans($luser) -> [list ... $lchan ...]
591# nick_username($luser) -> <securely known local username>
592# nick_unique($luser) -> <counter>
593# nick_case($luser) -> $user (valid even if no longer visible)
594
595# chan_nicks($lchan) -> [list ... $luser ...]
a056c4bd 596
534e26a9
IJ
597proc lnick_forget {luser} {
598 global nick_arys chan_nicks
a056c4bd 599 foreach ary $nick_arys {
534e26a9 600 upvar #0 nick_${ary}($luser) av
a056c4bd
IJ
601 catch { unset av }
602 }
534e26a9
IJ
603 foreach lch [array names chan_nicks] {
604 upvar #0 chan_nicks($lch) nlist
605 set nlist [grep tn {"$tn" != "$luser"} $nlist]
606 check_justme $lch
607 }
608}
609
610proc nick_forget {user} {
611 global nick_arys chan_nicks
612 lnick_forget [irctolower $user]
613 nick_case $user
a4a1f396
IJ
614}
615
534e26a9 616proc nick_case {user} {
a4a1f396 617 global nick_case
534e26a9 618 set nick_case([irctolower $user]) $user
a056c4bd
IJ
619}
620
83dd1224 621proc msg_NICK {p c newnick} {
a4a1f396 622 global nick_arys nick_case
83dd1224
IJ
623 prefix_nick
624 recordlastseen_n $n "changing nicks to $newnick" 0
625 recordlastseen_n $newnick "changing nicks from $n" 1
6425c2d6
IJ
626 set luser [irctolower $n]
627 set lusernew [irctolower $newnick]
a056c4bd 628 foreach ary $nick_arys {
6425c2d6
IJ
629 upvar #0 nick_${ary}($luser) old
630 upvar #0 nick_${ary}($lusernew) new
a056c4bd
IJ
631 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
632 if {[info exists old]} { set new $old; unset old }
633 }
6425c2d6 634 upvar #0 nick_onchans($lusernew) oc
534e26a9
IJ
635 foreach ch $oc {
636 upvar #0 chan_nicks($ch) nlist
637 set nlist [grep tn {"$tn" != "$luser"} $nlist]
638 lappend nlist $lusernew
639 }
a4a1f396 640 nick_case $newnick
83dd1224
IJ
641}
642
20087363
IJ
643proc nick_ishere {n} {
644 global nick_counter
534e26a9 645 upvar #0 nick_unique([irctolower $n]) u
20087363
IJ
646 if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] }
647 nick_case $n
648}
649
a056c4bd
IJ
650proc msg_JOIN {p c chan} {
651 prefix_nick
652 recordlastseen_n $n "joining $chan" 1
cf6ea4de
IJ
653 set nl [irctolower $n]
654 set lchan [irctolower $chan]
655 upvar #0 nick_onchans($nl) oc
656 upvar #0 chan_nicks($lchan) nlist
657 lappend oc $lchan
658 lappend nlist $nl
20087363 659 nick_ishere $n
a056c4bd
IJ
660}
661proc msg_PART {p c chan} {
662 prefix_nick
663 recordlastseen_n $n "leaving $chan" 1
664 process_kickpart $chan $n
665}
666proc msg_QUIT {p c why} {
667 prefix_nick
668 recordlastseen_n $n "leaving ($why)" 0
669 nick_forget $n
670}
422f52e4 671
cc2d31de
IJ
672proc msg_PRIVMSG {p c dest text} {
673 prefix_nick
422f52e4 674 if {[ischan $dest]} {
83dd1224 675 recordlastseen_n $n "invoking me in $dest" 1
422f52e4 676 set output $dest
cc2d31de 677 } else {
83dd1224 678 recordlastseen_n $n "talking to me" 1
422f52e4
IJ
679 set output $n
680 }
a4a1f396 681 nick_case $n
422f52e4
IJ
682
683 if {[catch {
684 regsub {^! *} $text {} text
685 set ucmd [ta_word]
83dd1224 686 set procname ucmd/[string tolower $ucmd]
422f52e4
IJ
687 if {[catch { info body $procname }]} {
688 error "unknown command; try help for help"
689 }
83dd1224 690 $procname $p $dest
422f52e4 691 } rv]} {
7ce72032 692 sendprivmsg $n "error: $rv"
422f52e4 693 } else {
7ce72032 694 manyset $rv priv_msgs pub_msgs priv_acts pub_acts
7a70431a 695 foreach {td val} [list $n $priv_acts $output $pub_acts] {
7ce72032 696 foreach l [split $val "\n"] {
7818b6af 697 sendaction_priority 0 $td $l
7ce72032
IJ
698 }
699 }
7a70431a 700 foreach {td val} [list $n $priv_msgs $output $pub_msgs] {
422f52e4 701 foreach l [split $val "\n"] {
7a70431a 702 sendprivmsg $td $l
422f52e4
IJ
703 }
704 }
705 }
706}
707
a056c4bd 708proc msg_INVITE {p c n chan} {
534e26a9 709 after 1000 [list dojoin [irctolower $chan]]
a056c4bd
IJ
710}
711
712proc grep {var predicate list} {
713 set o {}
714 upvar 1 $var v
715 foreach v $list {
716 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
717 }
718 return $o
719}
720
721proc msg_353 {p c dest type chan nicklist} {
722 global names_chans nick_onchans
534e26a9
IJ
723 set lchan [irctolower $chan]
724 upvar #0 chan_nicks($lchan) nlist
725 lappend names_chans $lchan
726 if {![info exists nlist]} {
727 # We don't think we're on this channel, so ignore it !
728 # Unfortunately, because we don't get a reply to PART,
729 # we have to remember ourselves whether we're on a channel,
730 # and ignore stuff if we're not, to avoid races. Feh.
731 return
732 }
733 set nlist_new {}
734 foreach user [split $nicklist { }] {
735 regsub {^[@+]} $user {} user
736 if {![string length $user]} continue
737 check_nick $user
738 set luser [irctolower $user]
739 upvar #0 nick_onchans($luser) oc
740 lappend oc $lchan
741 lappend nlist_new $luser
742 nick_ishere $user
a056c4bd 743 }
534e26a9 744 set nlist $nlist_new
a056c4bd
IJ
745}
746
747proc msg_366 {p c args} {
748 global names_chans nick_onchans
534e26a9
IJ
749 set lchan [irctolower $c]
750 foreach luser [array names nick_onchans] {
751 upvar #0 nick_onchans($luser) oc
752 if {[llength names_chans] > 1} {
a056c4bd 753 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
a056c4bd 754 }
534e26a9 755 if {![llength $oc]} { lnick_forget $n }
a056c4bd
IJ
756 }
757 unset names_chans
758}
759
11d9bff9
IJ
760proc ta_anymore {} {
761 upvar 1 text text
762 return [expr {!![string length $text]}]
763}
764
422f52e4
IJ
765proc ta_nomore {} {
766 upvar 1 text text
767 if {[string length $text]} { error "too many parameters" }
768}
769
770proc ta_word {} {
771 upvar 1 text text
772 if {![regexp {^([^ ]+) *(.*)} $text dummy firstword text]} {
773 error "too few parameters"
774 }
775 return $firstword
776}
777
778proc ta_nick {} {
779 upvar 1 text text
780 set v [ta_word]
781 check_nick $v
782 return $v
783}
784
83dd1224
IJ
785proc def_ucmd {cmdname body} {
786 proc ucmd/$cmdname {p dest} " upvar 1 text text\n$body"
787}
788
7ce72032
IJ
789proc ucmdr {priv pub args} {
790 return -code return [concat [list $priv $pub] $args]
422f52e4 791}
e1ba63be 792
e6cc22dc 793proc loadhelp {} {
5e5be903 794 global help_topics errorInfo
e6cc22dc
IJ
795
796 catch { unset help_topics }
797 set f [open helpinfos r]
d83fb8db
IJ
798 try_except_finally {
799 set lno 0
800 while {[gets $f l] >= 0} {
801 incr lno
802 if {[regexp {^#.*} $l]} {
803 } elseif {[regexp {^ *$} $l]} {
804 if {[info exists topic]} {
805 set help_topics($topic) [join $lines "\n"]
806 unset topic
807 unset lines
808 }
5e5be903
IJ
809 } elseif {[regexp {^\:\:} $l]} {
810 } elseif {[regexp {^\:([-+._0-9a-z]*)$} $l dummy newtopic]} {
d83fb8db
IJ
811 if {[info exists topic]} {
812 error "help $newtopic while in $topic"
813 }
814 set topic $newtopic
815 set lines {}
5e5be903 816 } elseif {[regexp {^[^:#]} $l]} {
d83fb8db 817 set topic
5e5be903
IJ
818 regsub -all {([^\\])\!\$?} _$l {\1} l
819 regsub -all {\\(.)} $l {\1} l
820 regsub {^_} $l {} l
d83fb8db
IJ
821 lappend lines [string trimright $l]
822 } else {
823 error "eh ? $lno: $l"
e6cc22dc 824 }
e6cc22dc 825 }
d83fb8db 826 if {[info exists topic]} { error "unfinished topic $topic" }
5e5be903
IJ
827 } {
828 set errorInfo "in helpinfos line $lno\n$errorInfo"
829 } {
d83fb8db 830 close $f
e6cc22dc 831 }
422f52e4
IJ
832}
833
e6cc22dc 834def_ucmd help {
cf6ea4de
IJ
835 upvar 1 n n
836
837 set topic [irctolower [string trim $text]]
838 if {[string length $topic]} {
839 set ontopic " on `$topic'"
840 } else {
841 set ontopic ""
842 }
7818b6af
IJ
843 if {[set lag [out_lagged]]} {
844 if {[ischan $dest]} { set replyto $dest } else { set replyto $n }
845 if {$lag > 1} {
846 sendaction_priority 1 $replyto \
cf6ea4de 847 "is very lagged. Please ask for help$ontopic again later."
7818b6af
IJ
848 ucmdr {} {}
849 } else {
850 sendaction_priority 1 $replyto \
cf6ea4de 851 "is lagged. Your help$ontopic will arrive shortly ..."
7818b6af
IJ
852 }
853 }
854
cf6ea4de
IJ
855 upvar #0 help_topics($topic) info
856 if {![info exists info]} { ucmdr "No help on $topic, sorry." {} }
e6cc22dc
IJ
857 ucmdr $info {}
858}
e1ba63be 859
e6cc22dc
IJ
860def_ucmd ? {
861 global help_topics
862 ucmdr $help_topics() {}
863}
e1ba63be 864
4fd2739c 865proc check_username {target} {
7ce72032
IJ
866 if {
867 [string length $target] > 8 ||
868 [regexp {[^-0-9a-z]} $target] ||
869 ![regexp {^[a-z]} $target]
870 } { error "invalid username" }
4fd2739c
IJ
871}
872
20087363 873proc somedb__head {} {
7a70431a 874 uplevel 1 {
20087363
IJ
875 set idl [irctolower $id]
876 upvar #0 ${nickchan}db($idl) ndbe
877 binary scan $idl H* idh
878 set idfn $fprefix$idh
879 if {![info exists iddbe] && [file exists $idfn]} {
880 set f [open $idfn r]
7a70431a
IJ
881 try_except_finally { set newval [read $f] } {} { close $f }
882 if {[llength $newval] % 2} { error "invalid length" }
20087363 883 set iddbe $newval
7a70431a
IJ
884 }
885 }
886}
887
20087363
IJ
888proc def_somedb {name arglist body} {
889 foreach {nickchan fprefix} {nick users/n chan chans/c} {
890 proc ${nickchan}db_$name $arglist \
534e26a9
IJ
891 "set nickchan $nickchan; set fprefix $fprefix; $body"
892 }
893}
894
895def_somedb list {} {
896 set list {}
897 foreach path [glob -nocomplain -path $fprefix *] {
898 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
899 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
900 lappend list [binary format H* $thinghex]
20087363 901 }
534e26a9
IJ
902 return $list
903}
904
905proc def_somedb_id {name arglist body} {
906 def_somedb $name [concat id $arglist] "somedb__head; $body"
7a70431a
IJ
907}
908
534e26a9 909def_somedb_id exists {} {
20087363 910 return [info exists iddbe]
7a70431a
IJ
911}
912
534e26a9 913def_somedb_id delete {} {
20087363
IJ
914 catch { unset iddbe }
915 file delete $idfn
7a70431a
IJ
916}
917
20087363 918set default_settings_nick {timeformat ks}
281b5f05
IJ
919set default_settings_chan {
920 autojoin 1
921 mode *
922 userinvite pub
923 topicset {}
924 topicsee {}
925 topictell {}
926}
7a70431a 927
534e26a9 928def_somedb_id set {args} {
20087363
IJ
929 upvar #0 default_settings_$nickchan def
930 if {![info exists iddbe]} { set iddbe $def }
931 foreach {key value} [concat $iddbe $args] { set a($key) $value }
7a70431a
IJ
932 set newval {}
933 foreach {key value} [array get a] { lappend newval $key $value }
20087363 934 set f [open $idfn.new w]
7a70431a
IJ
935 try_except_finally {
936 puts $f $newval
937 close $f
20087363 938 file rename -force $idfn.new $idfn
7a70431a 939 } {
7a70431a 940 } {
d83fb8db 941 catch { close $f }
7a70431a 942 }
20087363 943 set iddbe $newval
7a70431a
IJ
944}
945
534e26a9 946def_somedb_id get {key} {
20087363
IJ
947 upvar #0 default_settings_$nickchan def
948 if {[info exists iddbe]} {
534e26a9 949 set l [concat $iddbe $def]
7a70431a 950 } else {
20087363 951 set l $def
7a70431a
IJ
952 }
953 foreach {tkey value} $l {
954 if {"$tkey" == "$key"} { return $value }
955 }
956 error "unset setting $key"
957}
958
20087363
IJ
959proc opt {key} {
960 global calling_nick
961 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
962 return [nickdb_get $n $key]
963}
964
7a70431a
IJ
965proc check_notonchan {} {
966 upvar 1 dest dest
967 if {[ischan $dest]} { error "that command must be sent privately" }
968}
969
970proc nick_securitycheck {strict} {
971 upvar 1 n n
972 if {![nickdb_exists $n]} { error "you are unknown to me, use `register'." }
20087363 973 set wantu [nickdb_get $n username]
7a70431a
IJ
974 if {![string length $wantu]} {
975 if {$strict} {
976 error "that feature is only available to secure users, sorry."
977 } else {
978 return
979 }
980 }
534e26a9
IJ
981 set luser [irctolower $n]
982 upvar #0 nick_username($luser) nu
7a70431a
IJ
983 if {![info exists nu]} {
984 error "nick $n is secure, you must identify yourself first."
985 }
986 if {"$wantu" != "$nu"} {
987 error "you are the wrong user - the nick $n belongs to $wantu, not $nu"
988 }
989}
990
20087363
IJ
991proc channel_securitycheck {channel n} {
992 # You must also call `nick_securitycheck 1'
c362e172
IJ
993 set mgrs [chandb_get $channel managers]
994 if {[lsearch -exact [irctolower $mgrs] [irctolower $n]] < 0} {
20087363
IJ
995 error "you are not a manager of $channel"
996 }
997}
998
999proc def_chancmd {name body} {
1000 proc channel/$name {} \
1001 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
1002}
1003
281b5f05
IJ
1004proc ta_listop {findnow procvalue} {
1005 # findnow and procvalue are code fragments which will be executed
1006 # in the caller's level. findnow should set ta_listop_ev to
1007 # the current list, and procvalue should treat ta_listop_ev as
1008 # a proposed value in the list and check and possibly modify
1009 # (canonicalise?) it. After ta_listop, ta_listop_ev will
1010 # be the new value of the list.
1011 upvar 1 ta_listop_ev exchg
1012 upvar 1 text text
20087363
IJ
1013 set opcode [ta_word]
1014 switch -exact _$opcode {
281b5f05 1015 _= { }
20087363 1016 _+ - _- {
281b5f05
IJ
1017 uplevel 1 $findnow
1018 foreach item $exchg { set array($item) 1 }
20087363
IJ
1019 }
1020 default {
281b5f05 1021 error "list change opcode must be one of + - ="
20087363
IJ
1022 }
1023 }
281b5f05
IJ
1024 foreach exchg [split $text " "] {
1025 if {![string length $exchg]} continue
1026 uplevel 1 $procvalue
20087363 1027 if {"$opcode" != "-"} {
281b5f05 1028 set array($exchg) 1
20087363 1029 } else {
281b5f05 1030 catch { unset array($exchg) }
20087363
IJ
1031 }
1032 }
281b5f05
IJ
1033 set exchg [lsort [array names array]]
1034}
1035
1036def_chancmd manager {
1037 ta_listop {
1038 if {[chandb_exists $chan]} {
1039 set ta_listop_ev [chandb_get $chan managers]
1040 } else {
1041 set ta_listop_ev [list [irctolower $n]]
1042 }
1043 } {
1044 check_nick $ta_listop_ev
1045 set ta_listop_ev [irctolower $ta_listop_ev]
1046 }
1047 if {[llength $ta_listop_ev]} {
1048 chandb_set $chan managers $ta_listop_ev
1049 ucmdr "Managers of $chan: $ta_listop_ev" {}
20087363
IJ
1050 } else {
1051 chandb_delete $chan
1052 ucmdr {} {} "forgets about managing $chan." {}
1053 }
1054}
1055
1056def_chancmd autojoin {
1057 set yesno [ta_word]
1058 switch -exact [string tolower $yesno] {
1059 no { set nv 0 }
1060 yes { set nv 1 }
1061 default { error "channel autojoin must be `yes' or `no' }
1062 }
1063 chandb_set $chan autojoin $nv
a49f2997
IJ
1064 ucmdr [expr {$nv ? "I will join $chan when I'm restarted " : \
1065 "I won't join $chan when I'm restarted "}] {}
534e26a9
IJ
1066}
1067
5e5be903
IJ
1068def_chancmd userinvite {
1069 set nv [string tolower [ta_word]]
1070 switch -exact $nv {
1071 pub { set txt "!invite will work for $chan, but it won't work by /msg" }
1072 here { set txt "!invite and /msg invite will work, but only for users who are already on $chan." }
1073 all { set txt "Any user will be able to invite themselves or anyone else to $chan." }
1074 none { set txt "I will not invite anyone to $chan." }
1075 default {
1076 error "channel userinvite must be `pub', `here', `all' or `none'
1077 }
1078 }
1079 chandb_set $chan userinvite $nv
1080 ucmdr $txt {}
1081}
1082
281b5f05
IJ
1083def_chancmd topic {
1084 set what [ta_word]
1085 switch -exact $what {
1086 leave {
1087 ta_nomore
1088 chandb_set $chan topicset {}
1089 ucmdr "I won't ever change the topic of $chan." {}
1090 }
1091 set {
1092 set t [string trim $text]
1093 if {![string length $t]} {
1094 error "you must specific the topic to set"
1095 }
1096 chandb_set $chan topicset $t
a49f2997 1097 ucmdr "Whenever I'm alone on $chan, I'll set the topic to $t." {}
281b5f05
IJ
1098 }
1099 see - tell {
1100 ta_listop {
1101 set ta_listop_ev [chandb_get $chan topic$what]
1102 } {
1103 if {"$ta_listop_ev" != "*"} {
1104 if {![ischan $ta_listop_ev]} {
1105 error "bad channel \`$ta_listop_ev' in topic $what"
1106 }
1107 set ta_listop_ev [irctolower $ta_listop_ev]
1108 }
1109 }
1110 chandb_set $chan topic$what $ta_listop_ev
1111 ucmdr "Topic $what list for $chan: $ta_listop_ev" {}
1112 }
1113 default {
1114 error "unknown channel topic subcommand - see help channel"
1115 }
1116 }
1117}
1118
534e26a9
IJ
1119def_chancmd mode {
1120 set mode [ta_word]
1121 if {"$mode" != "*" && ![regexp {^(([-+][imnpst]+)+)$} $mode mode]} {
1122 error {channel mode must be * or match ([-+][imnpst]+)+}
1123 }
1124 chandb_set $chan mode $mode
1125 if {"$mode" == "*"} {
281b5f05 1126 ucmdr "I won't ever change the mode of $chan." {}
534e26a9 1127 } else {
281b5f05 1128 ucmdr "Whenever I'm alone on $chan, I'll set the mode to $mode." {}
534e26a9 1129 }
20087363
IJ
1130}
1131
1132def_chancmd show {
1133 if {[chandb_exists $chan]} {
1134 set l "Settings for $chan: autojoin "
1135 append l [lindex {no yes} [chandb_get $chan autojoin]]
b60b5a0f
IJ
1136 append l ", mode " [chandb_get $chan mode]
1137 append l ", userinvite " [chandb_get $chan userinvite] "."
20087363 1138 append l "\nManagers: "
8b6ee626 1139 append l [join [chandb_get $chan managers] " "]
281b5f05
IJ
1140 foreach {ts sep} {see "\n" tell " "} {
1141 set t [chandb_get $chan topic$ts]
1142 append l $sep
1143 if {[llength $t]} {
1144 append l "Topic $ts list: $t."
1145 } else {
1146 append l "Topic $ts list is empty."
1147 }
1148 }
8b6ee626
IJ
1149 append l "\n"
1150 set t [chandb_get $chan topicset]
1151 if {[string length $t]} {
1152 append l "Topic to set: $t"
1153 } else {
1154 append l "I will not change the topic."
1155 }
20087363
IJ
1156 ucmdr {} $l
1157 } else {
1158 ucmdr {} "The channel $chan is not managed."
1159 }
1160}
1161
cf6ea4de
IJ
1162proc channelmgr_monoop {} {
1163 upvar 1 dest dest
1164 upvar 1 text text
1165 upvar 1 n n
1166 upvar 1 p p
1167 upvar 1 target target
1168 global chan_nicks
1169
20087363
IJ
1170 if {[ischan $dest]} { set target $dest }
1171 if {[ta_anymore]} { set target [ta_word] }
1172 ta_nomore
cf6ea4de
IJ
1173 if {![info exists target]} {
1174 error "you must specify, or invoke me on, the relevant channel"
1175 }
1176 if {![info exists chan_nicks([irctolower $target])]} {
1177 error "I am not on $target."
1178 }
20087363
IJ
1179 if {![ischan $target]} { error "not a valid channel" }
1180 if {![chandb_exists $target]} { error "$target is not a managed channel." }
1181 prefix_nick
1182 nick_securitycheck 1
1183 channel_securitycheck $target $n
cf6ea4de
IJ
1184}
1185
1186def_ucmd op {
1187 channelmgr_monoop
20087363
IJ
1188 sendout MODE $target +o $n
1189}
1190
cf6ea4de
IJ
1191def_ucmd leave {
1192 channelmgr_monoop
1193 doleave $target
1194}
1195
5e5be903
IJ
1196def_ucmd invite {
1197 global chan_nicks
1198
1199 if {[ischan $dest]} {
1200 set target $dest
1201 set onchan 1
1202 } else {
1203 set target [ta_word]
1204 set onchan 0
1205 }
1206 set ltarget [irctolower $target]
1207 if {![ischan $target]} { error "$target is not a channel." }
1208 if {![info exists chan_nicks($ltarget)]} { error "I am not on $target." }
1209 set ui [chandb_get $ltarget userinvite]
1210 if {"$ui" == "pub" && !$onchan} {
1211 error "Invitations to $target must be made with !invite."
1212 }
1213 if {"$ui" != "all"} {
1214 prefix_nick
1215 if {[lsearch -exact $chan_nicks($ltarget) [irctolower $n]] < 0} {
1216 error "Invitations to $target may only be made by a user on the channel."
1217 }
1218 }
1219 if {"$ui" == "none"} {
1220 error "Sorry, I've not been authorised to invite people to $target."
1221 }
1222 if {![ta_anymore]} {
1223 error "You have to say who to invite."
1224 }
1225 set invitees {}
1226 while {[ta_anymore]} {
1227 set invitee [ta_word]
1228 check_nick $invitee
1229 lappend invitees $invitee
1230 }
1231 foreach invitee $invitees {
1232 sendout INVITE $invitee $ltarget
1233 }
1234 set who [lindex $invitees 0]
1235 switch -exact llength $invitees {
1236 0 { error "zero invitees" }
1237 1 { }
1238 2 { append who " and [lindex $invitees 1]" }
1239 * {
1240 set who [join [lreplace $invitees end end] ", "]
1241 append who " and [lindex $invitees [llength $invitees]]"
1242 }
1243 }
1244 ucmdr {} "invites $who to $target."
1245}
1246
20087363
IJ
1247def_ucmd channel {
1248 if {[ischan $dest]} { set target $dest }
1249 if {![ta_anymore]} {
1250 set subcmd show
1251 } else {
1252 set subcmd [ta_word]
1253 }
1254 if {[ischan $subcmd]} {
1255 set target $subcmd
1256 if {![ta_anymore]} {
1257 set subcmd show
1258 } else {
1259 set subcmd [ta_word]
1260 }
1261 }
1262 if {![info exists target]} { error "privately, you must specify a channel" }
1263 set procname channel/$subcmd
1264 if {"$subcmd" != "show"} {
1265 if {[catch { info body $procname }]} { error "unknown channel setting $subcmd" }
1266 prefix_nick
1267 nick_securitycheck 1
1268 if {[chandb_exists $target]} {
1269 channel_securitycheck $target $n
1270 } else {
1271 upvar #0 chan_initialop([irctolower $target]) io
534e26a9 1272 upvar #0 nick_unique([irctolower $n]) u
20087363
IJ
1273 if {![info exists io]} { error "$target is not a managed channel" }
1274 if {"$io" != "$u"} { error "you are not the interim manager of $target" }
1275 if {"$subcmd" != "manager"} { error "use `channel manager' first" }
1276 }
1277 }
1278 channel/$subcmd
1279}
1280
a4a1f396
IJ
1281def_ucmd who {
1282 if {[ta_anymore]} {
1283 set target [ta_word]; ta_nomore
1284 set myself 1
1285 } else {
1286 prefix_nick
1287 set target $n
1288 set myself [expr {"$target" != "$n"}]
1289 }
534e26a9
IJ
1290 set ltarget [irctolower $target]
1291 upvar #0 nick_case($ltarget) ctarget
a4a1f396 1292 set nshow $target
534e26a9
IJ
1293 if {[info exists ctarget]} {
1294 upvar #0 nick_onchans($ltarget) oc
1295 upvar #0 nick_username($ltarget) nu
1296 if {[info exists oc]} { set nshow $ctarget }
a4a1f396 1297 }
534e26a9 1298 if {![nickdb_exists $ltarget]} {
a4a1f396 1299 set ol "$nshow is not a registered nick."
20087363 1300 } elseif {[string length [set username [nickdb_get $target username]]]} {
a4a1f396
IJ
1301 set ol "The nick $nshow belongs to the user $username."
1302 } else {
1303 set ol "The nick $nshow is registered (but not to a username)."
1304 }
534e26a9 1305 if {![info exists ctarget] || ![info exists oc]} {
a4a1f396
IJ
1306 if {$myself} {
1307 append ol "\nI can't see $nshow on anywhere."
1308 } else {
1309 append ol "\nYou aren't on any channels with me."
1310 }
1311 } elseif {![info exists nu]} {
1312 append ol "\n$nshow has not identified themselves."
1313 } elseif {![info exists username]} {
1314 append ol "\n$nshow has identified themselves as the user $nu."
1315 } elseif {"$nu" != "$username"} {
1316 append ol "\nHowever, $nshow is being used by the user $nu."
1317 } else {
1318 append ol "\n$nshow has identified themselves to me."
1319 }
1320 ucmdr {} $ol
1321}
1322
7a70431a
IJ
1323def_ucmd register {
1324 prefix_nick
1325 check_notonchan
1326 set old [nickdb_exists $n]
1327 if {$old} { nick_securitycheck 0 }
534e26a9 1328 set luser [irctolower $n]
7a70431a
IJ
1329 switch -exact [string tolower [string trim $text]] {
1330 {} {
534e26a9 1331 upvar #0 nick_username($luser) nu
7a70431a
IJ
1332 if {![info exists nu]} {
1333 ucmdr {} \
a4a1f396 1334 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
7a70431a
IJ
1335 }
1336 nickdb_set $n username $nu
1337 ucmdr {} {} "makes a note of your username." {}
1338 }
1339 delete {
1340 nickdb_delete $n
1341 ucmdr {} {} "forgets your nickname." {}
1342 }
1343 insecure {
1344 nickdb_set $n username {}
1345 if {$old} {
1346 ucmdr {} "Security is now disabled for your nickname !"
1347 } else {
1348 ucmdr {} "This is fine, but bear in mind that people will be able to mess with your settings. Channel management features need a secure registration." "makes an insecure registration for your nick."
1349 }
1350 }
1351 }
11d9bff9
IJ
1352}
1353
1354proc timeformat_desc {tf} {
1355 switch -exact $tf {
86892128 1356 ks { return "Times will be displayed in seconds or kiloseconds." }
11d9bff9
IJ
1357 hms { return "Times will be displayed in hours, minutes, etc." }
1358 default { error "invalid timeformat: $v" }
1359 }
1360}
1361
1362proc def_setting {opt show_body set_body} {
1363 proc set_show/$opt {} "
1364 upvar 1 n n
1365 set opt $opt
1366 $show_body"
1367 if {![string length $set_body]} return
1368 proc set_set/$opt {} "
1369 upvar 1 n n
1370 upvar 1 text text
1371 set opt $opt
1372 $set_body"
1373}
1374
1375def_setting timeformat {
20087363 1376 set tf [nickdb_get $n timeformat]
11d9bff9
IJ
1377 return "$tf: [timeformat_desc $tf]"
1378} {
1379 set tf [string tolower [ta_word]]
1380 ta_nomore
1381 set desc [timeformat_desc $tf]
1382 nickdb_set $n timeformat $tf
1383 ucmdr {} $desc
1384}
1385
1386def_setting security {
20087363 1387 set s [nickdb_get $n username]
11d9bff9
IJ
1388 if {[string length $s]} {
1389 return "Your nick, $n, is controlled by the user $s."
1390 } else {
1391 return "Your nick, $n, is not secure."
1392 }
1393} {}
1394
1395def_ucmd set {
1396 prefix_nick
1397 check_notonchan
1398 if {![nickdb_exists $n]} {
d83fb8db 1399 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
11d9bff9
IJ
1400 }
1401 if {![ta_anymore]} {
1402 set ol {}
1403 foreach proc [lsort [info procs]] {
1404 if {![regexp {^set_show/(.*)$} $proc dummy opt]} continue
1405 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
1406 }
1407 ucmdr {} [join $ol "\n"]
1408 } else {
1409 set opt [ta_word]
1410 if {[catch { info body set_show/$opt }]} {
1411 error "no setting $opt"
1412 }
1413 if {![ta_anymore]} {
1414 ucmdr {} "$opt [set_show/$opt]"
1415 } else {
86892128 1416 nick_securitycheck 0
11d9bff9
IJ
1417 if {[catch { info body set_set/$opt }]} {
1418 error "setting $opt cannot be set with `set'"
1419 }
1420 set_set/$opt
1421 }
1422 }
1423}
7a70431a 1424
4fd2739c
IJ
1425def_ucmd identpass {
1426 set username [ta_word]
d83fb8db 1427 set passmd5 [md5sum "[ta_word]\n"]
4fd2739c
IJ
1428 ta_nomore
1429 prefix_nick
11d9bff9 1430 check_notonchan
534e26a9
IJ
1431 set luser [irctolower $n]
1432 upvar #0 nick_onchans($luser) onchans
4fd2739c 1433 if {![info exists onchans] || ![llength $onchans]} {
7a70431a 1434 ucmdr "You must be on a channel with me to identify yourself." {}
4fd2739c
IJ
1435 }
1436 check_username $username
1437 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1438 irc-identpass $n
534e26a9 1439 upvar #0 nick_username($luser) rec_username
4fd2739c 1440 set rec_username $username
7a70431a 1441 ucmdr "Pleased to see you, $username." {}
4fd2739c
IJ
1442}
1443
1444def_ucmd summon {
1445 set target [ta_word]
1446 ta_nomore
1447 check_username $target
7ce72032
IJ
1448 prefix_nick
1449
1450 upvar #0 lastsummon($target) ls
1451 set now [clock seconds]
1452 if {[info exists ls]} {
1453 set interval [expr {$now - $ls}]
1454 if {$interval < 30} {
1455 ucmdr {} \
1456 "Please be patient; $target was summoned only [showinterval $interval]."
1457 }
1458 }
1459 regsub {^[^!]*!} $p {} path
1460 if {[catch {
1461 exec userv --timeout 3 $target irc-summon $n $path \
1462 [expr {[ischan $dest] ? "$dest" : ""}] \
1463 < /dev/null
1464 } rv]} {
1465 regsub -all "\n" $rv { / } rv
1466 error $rv
1467 }
1468 if {[regexp {^problem (.*)} $rv dummy problem]} {
8a8d337d 1469 ucmdr {} "The user `$target' $problem."
7ce72032
IJ
1470 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1471 set idletime [expr {$now - $idlesince}]
1472 set ls $now
1473 ucmdr {} {} {} "invites $target ($tty[expr {
1474 $idletime > 10 ? ", idle for [showintervalsecs $idletime]" : ""
1475 }]) to [expr {
1476 [ischan $dest] ? "join us here" : "talk to you"
1477 }]."
1478 } else {
1479 error "unexpected response from userv service: $rv"
1480 }
1481}
1482
a69f7d2c
IJ
1483proc md5sum {value} { exec md5sum << $value }
1484
83dd1224 1485def_ucmd seen {
422f52e4 1486 global lastseen nick
83dd1224
IJ
1487 prefix_nick
1488 set ncase [ta_nick]
1489 set nlower [irctolower $ncase]
422f52e4 1490 ta_nomore
83dd1224
IJ
1491 set now [clock seconds]
1492 if {"$nlower" == "[irctolower $nick]"} {
422f52e4 1493 error "I am not self-aware."
83dd1224
IJ
1494 } elseif {![info exists lastseen($nlower)]} {
1495 set rstr "I've never seen $ncase."
422f52e4 1496 } else {
83dd1224
IJ
1497 manyset $lastseen($nlower) realnick time what
1498 set howlong [expr {$now - $time}]
1499 set string [showinterval $howlong]
1500 set rstr "I last saw $realnick $string, $what."
1501 }
1502 if {[ischan $dest]} {
1503 set where $dest
1504 } else {
1505 set where {}
1506 }
1507 upvar #0 lookedfor($nlower) lf
1508 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1509 set lf [list [list $now $n $where]]
1510 foreach v $oldvalue {
1511 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1512 lappend lf $v
cc2d31de 1513 }
83dd1224 1514 ucmdr {} $rstr
cc2d31de
IJ
1515}
1516
28c8ab78
IJ
1517proc ensure_globalsecret {} {
1518 global globalsecret
1519
1520 if {[info exists globalsecret]} return
1521 set gsfile [open /dev/urandom r]
1522 fconfigure $gsfile -translation binary
1523 set globalsecret [read $gsfile 32]
1524 binary scan $globalsecret H* globalsecret
1525 close $gsfile
1526 unset gsfile
1527}
1528
1529proc ensure_outqueue {} {
1530 out__vars
1531 if {[info exists out_queue]} return
8bf6ec5b 1532 set out_creditms 0
28c8ab78
IJ
1533 set out_creditat [clock seconds]
1534 set out_queue {}
1535 set out_lag_reported 0
1536 set out_lag_reportwhen $out_creditat
1537}
1538
1539proc fail {msg} {
1540 logerror "failing: $msg"
1541 exit 1
1542}
1543
1544proc ensure_connecting {} {
a867f547 1545 global sock ownfullname host port nick socketargs
28c8ab78
IJ
1546 global musthaveping_ms musthaveping_after
1547
1548 if {[info exists sock]} return
a867f547 1549 set sock [eval socket $socketargs [list $host $port]]
cc2d31de 1550 fconfigure $sock -buffering line
cc2d31de
IJ
1551 fconfigure $sock -translation crlf
1552
b3d361ab 1553 sendout USER blight 0 * $ownfullname
cc2d31de
IJ
1554 sendout NICK $nick
1555 fileevent $sock readable onread
28c8ab78
IJ
1556
1557 set musthaveping_after [after $musthaveping_ms \
1558 {fail "no ping within timeout"}]
cc2d31de
IJ
1559}
1560
534e26a9
IJ
1561proc connected {} {
1562 global musthaveping_after
1563
1564 after cancel $musthaveping_after
1565 unset musthaveping_after
1566
1567 foreach chan [chandb_list] {
1568 if {[chandb_get $chan autojoin]} { dojoin $chan }
1569 }
1570}
1571
28c8ab78
IJ
1572ensure_globalsecret
1573ensure_outqueue
e6cc22dc 1574loadhelp
28c8ab78 1575ensure_connecting