chiark / gitweb /
Bugfixes. New channel management stuff.
[ircbot] / bot.tcl
CommitLineData
9bc33297
IJ
1#!/usr/bin/tclsh8.2
2
3set host chiark
4set port 6667
7ce72032 5if {![info exists nick]} { set nick Blight }
a69f7d2c 6if {![info exists ownfullname]} { set ownfullname "here to Help" }
e1ba63be 7set ownmailaddr blight@chiark.greenend.org.uk
e1ba63be
IJ
8
9if {![info exists globalsecret]} {
10 set gsfile [open /dev/urandom r]
11 fconfigure $gsfile -translation binary
12 set globalsecret [read $gsfile 32]
13 binary scan $globalsecret H* globalsecret
14 close $gsfile
15 unset gsfile
16}
9bc33297 17
20087363
IJ
18proc manyset {list args} {
19 foreach val $list var $args {
20 upvar 1 $var my
21 set my $val
22 }
23}
24
7a70431a
IJ
25proc try_except_finally {try except finally} {
26 global errorInfo errorCode
27 set er [catch { uplevel 1 $try } emsg]
28 if {$er} {
29 set ei $errorInfo
30 set ec $errorCode
31 if {[catch { uplevel 1 $except } emsg3]} {
32 append ei "\nALSO ERROR HANDLING ERROR:\n$emsg3"
33 }
34 }
35 set er2 [catch { uplevel 1 $finally } emsg2]
36 if {$er} {
37 if {$er2} {
38 append ei "\nALSO ERROR CLEANING UP:\n$emsg2"
39 }
40 return -code $er -errorinfo $ei -errorcode $ec $emsg
41 } elseif {$er2} {
42 return -code $er2 -errorinfo $errorInfo -errorcode $errorCode $emsg2
43 } else {
44 return $emsg
45 }
46}
47
cc2d31de
IJ
48proc sendout {command args} {
49 global sock
9bc33297
IJ
50 if {[llength $args]} {
51 set la [lindex $args end]
52 set args [lreplace $args end end]
53 foreach i $args {
54 if {[regexp {[: ]} $i]} {
cc2d31de 55 error "bad argument in output $i ($command $args)"
9bc33297
IJ
56 }
57 }
cc2d31de 58 lappend args :$la
9bc33297
IJ
59 }
60 set args [lreplace $args 0 -1 $command]
cc2d31de 61 set string [join $args { }]
83dd1224 62 puts "[clock seconds] -> $string"
9bc33297
IJ
63 puts $sock $string
64}
9bc33297
IJ
65
66proc log {data} {
67 puts $data
68}
69
70proc logerror {data} {
71 log $data
cc2d31de
IJ
72}
73
74proc saveeic {} {
75 global saveei saveec errorInfo errorCode
76
77 set saveei $errorInfo
78 set saveec $errorCode
79
80 puts ">$saveec|$saveei<"
81}
82
83proc bgerror {msg} {
84 global save
85 logerror $msg
86 saveeic
87}
9bc33297
IJ
88
89proc onread {args} {
17bfe24c 90 global sock nick calling_nick errorInfo errorCode
9bc33297 91
cc2d31de
IJ
92 if {[gets $sock line] == -1} { set terminate 1; return }
93 regsub -all "\[^ -\176\240-\376\]" $line ? line
9bc33297 94 set org $line
17bfe24c
IJ
95
96 set ei $errorInfo
97 set ec $errorCode
a4a1f396 98 catch { unset calling_nick }
17bfe24c
IJ
99 set errorInfo $ei
100 set errorCode $ec
101
9bc33297
IJ
102 if {[regexp -nocase {^:([^ ]+) (.*)} $line dummy prefix remain]} {
103 set line $remain
a4a1f396
IJ
104 if {[regexp {^([^!]+)!} $prefix dummy maybenick]} {
105 set calling_nick $maybenick
106 if {"[irctolower $maybenick]" == "[irctolower $nick]"} return
107 }
9bc33297
IJ
108 } else {
109 set prefix {}
110 }
cc2d31de 111 if {![string length $line]} { return }
9bc33297
IJ
112 if {![regexp -nocase {^([0-9a-z]+) *(.*)} $line dummy command line]} {
113 log "bad command: $org"
114 return
115 }
cc2d31de 116 set command [string toupper $command]
9bc33297 117 set params {}
cc2d31de 118 while {[regexp {^([^ :]+) *(.*)} $line dummy thisword line]} {
9bc33297
IJ
119 lappend params $thisword
120 }
121 if {[regexp {^:(.*)} $line dummy thisword]} {
122 lappend params $thisword
123 } elseif {[string length $line]} {
124 log "junk at end: $org"
125 return
126 }
cc2d31de
IJ
127 if {"$command" == "PRIVMSG" &&
128 [regexp {^[&#+!]} [lindex $params 0]] &&
129 ![regexp {^!} [lindex $params 1]]} {
130 # on-channel message, ignore
422f52e4 131 catch {
83dd1224 132 recordlastseen_p $prefix "talking on [lindex $params 0]" 1
422f52e4 133 }
cc2d31de
IJ
134 return
135 }
83dd1224 136 log "[clock seconds] <- $org"
9bc33297 137 set procname msg_$command
cc2d31de 138 if {[catch { info body $procname }]} { return }
9bc33297
IJ
139 if {[catch {
140 eval [list $procname $prefix $command] $params
141 } emsg]} {
142 logerror "error: $emsg ($prefix $command $params)"
cc2d31de 143 saveeic
9bc33297
IJ
144 }
145}
146
7ce72032 147proc sendprivmsg {dest l} {
20087363
IJ
148 foreach v [split $l "\n"] {
149 sendout [expr {[ischan $dest] ? "PRIVMSG" : "NOTICE"}] $dest $v
150 }
83dd1224 151}
7ce72032
IJ
152proc sendaction {dest what} { sendout PRIVMSG $dest "\001ACTION $what\001" }
153proc msendprivmsg {dest ll} { foreach l $ll { sendprivmsg $dest $l } }
154proc msendprivmsg_delayed {delay dest ll} { after $delay [list msendprivmsg $dest $ll] }
83dd1224 155
cc2d31de
IJ
156proc prefix_none {} {
157 upvar 1 p p
9bc33297 158 if {[string length $p]} { error "prefix specified" }
cc2d31de 159}
9bc33297 160
cc2d31de
IJ
161proc msg_PING {p c s1} {
162 prefix_none
163 sendout PONG $s1
9bc33297
IJ
164}
165
cc2d31de
IJ
166proc check_nick {n} {
167 if {[regexp -nocase {[^][\\`_^{|}a-z0-9-]} $n]} { error "bad char in nick" }
168 if {[regexp {^[-0-9]} $n]} { error "bad nick start" }
169}
170
422f52e4
IJ
171proc ischan {dest} {
172 return [regexp {^[&#+!]} $dest]
173}
174
175proc irctolower {v} {
176 foreach {from to} [list "\\\[" "{" \
177 "\\\]" "}" \
178 "\\\\" "|" \
179 "~" "^"] {
180 regsub -all $from $v $to v
181 }
182 return [string tolower $v]
183}
184
cc2d31de
IJ
185proc prefix_nick {} {
186 global nick
187 upvar 1 p p
188 upvar 1 n n
189 if {![regexp {^([^!]+)!} $p dummy n]} { error "not from nick" }
190 check_nick $n
a056c4bd
IJ
191 if {"[irctolower $n]" == "[irctolower $nick]"} {
192 error "from myself" {} {}
193 }
422f52e4
IJ
194}
195
7ce72032 196proc showintervalsecs {howlong} {
d83fb8db
IJ
197 return [showintervalsecs/[opt timeformat] $howlong]
198}
199
200proc showintervalsecs/ks {howlong} {
7ce72032
IJ
201 if {$howlong < 1000} {
202 return "${howlong}s"
83dd1224
IJ
203 } else {
204 if {$howlong < 1000000} {
205 set pfx k
206 set scale 1000
207 } else {
208 set pfx M
209 set scale 1000000
210 }
211 set value [expr "$howlong.0 / $scale"]
212 foreach {min format} {100 %.0f 10 %.1f 1 %.2f} {
213 if {$value < $min} continue
7ce72032 214 return [format "$format${pfx}s" $value]
83dd1224
IJ
215 }
216 }
217}
218
d83fb8db
IJ
219proc format_qty {qty unit} {
220 set o $qty
221 append o " "
222 append o $unit
223 if {$qty != 1} { append o s }
224 return $o
225}
226
227proc showintervalsecs/hms {qty} {
228 set ul {second 60 minute 60 hour 24 day 7 week}
229 set remainv 0
230 while {[llength $ul] > 1 && $qty >= [set uv [lindex $ul 1]]} {
231 set remainu [lindex $ul 0]
232 set remainv [expr {$qty % $uv}]
233 set qty [expr {($qty-$remainv)/$uv}]
234 set ul [lreplace $ul 0 1]
235 }
236 set o [format_qty $qty [lindex $ul 0]]
237 if {$remainv} {
238 append o " "
239 append o [format_qty $remainv $remainu]
240 }
241 return $o
242}
243
7ce72032
IJ
244proc showinterval {howlong} {
245 if {$howlong <= 0} {
246 return {just now}
247 } else {
248 return "[showintervalsecs $howlong] ago"
249 }
250}
251
83dd1224
IJ
252proc showtime {when} {
253 return [showinterval [expr {[clock seconds] - $when}]]
254}
255
256proc def_msgproc {name argl body} {
257 proc msg_$name "varbase $argl" "\
258 upvar #0 msg/\$varbase/dest d\n\
259 upvar #0 msg/\$varbase/str s\n\
260 upvar #0 msg/\$varbase/accum a\n\
261$body"
262}
263
264def_msgproc begin {dest str} {
265 set d $dest
266 set s $str
267 set a {}
268}
269
270def_msgproc append {str} {
271 set ns "$s$str"
272 if {[string length $s] && [string length $ns] > 65} {
273 msg__sendout $varbase
274 set s " [string trimleft $str]"
275 } else {
276 set s $ns
277 }
278}
279
280def_msgproc finish {} {
281 msg__sendout $varbase
282 unset s
283 unset d
284 return $a
285}
286
287def_msgproc _sendout {} {
288 lappend a [string trimright $s]
289 set s {}
290}
291
292proc looking_whenwhere {when where} {
293 set str [showtime [expr {$when-1}]]
294 if {[string length $where]} { append str " on $where" }
295 return $str
296}
297
298proc recordlastseen_n {n how here} {
299 global lastseen lookedfor
422f52e4 300 set lastseen([irctolower $n]) [list $n [clock seconds] $how]
83dd1224
IJ
301 if {!$here} return
302 upvar #0 lookedfor([irctolower $n]) lf
303 if {[info exists lf]} {
304 switch -exact [llength $lf] {
305 0 {
306 set ml {}
307 }
308 1 {
309 manyset [lindex $lf 0] when who where
310 set ml [list \
311 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
312 }
313 default {
314 msg_begin tosend $n "FYI, people have been looking for you:"
315 set i 0
316 set fin ""
317 foreach e $lf {
318 incr i
319 if {$i == 1} {
320 msg_append tosend " "
321 } elseif {$i == [llength $lf]} {
322 msg_append tosend " and "
323 set fin .
324 } else {
325 msg_append tosend ", "
326 }
327 manyset $e when who where
328 msg_append tosend \
329 "$who ([looking_whenwhere $when $where])$fin"
330 }
331 set ml [msg_finish tosend]
332 }
333 }
334 unset lf
335 msendprivmsg_delayed 1000 $n $ml
336 }
422f52e4
IJ
337}
338
83dd1224 339proc recordlastseen_p {p how here} {
422f52e4 340 prefix_nick
83dd1224 341 recordlastseen_n $n $how $here
422f52e4
IJ
342}
343
344proc chanmode_arg {} {
345 upvar 2 args cm_args
346 set rv [lindex $cm_args 0]
347 set cm_args [lreplace cm_args 0 0]
348 return $rv
349}
350
83dd1224 351proc chanmode_o1 {m g p chan} {
20087363 352 global nick chan_initialop
83dd1224
IJ
353 prefix_nick
354 set who [chanmode_arg]
355 recordlastseen_n $n "being nice to $who" 1
356 if {"[irctolower $who]" == "[irctolower $nick]"} {
20087363
IJ
357 set nl [irctolower $n]
358 upvar #0 nick_unique($n) u
359 if {[chandb_exists $chan]} {
360 sendprivmsg $n Thanks.
361 } elseif {![info exists u]} {
362 sendprivmsg $n {Op me while not on the channel, why don't you ?}
363 } else {
364 set chan_initialop([irctolower $chan]) $u
365 sendprivmsg $n \
366 "Thanks. You can use `channel manager ...' to register this channel."
367 if {![nickdb_exists $n] || ![string length [nickdb_get $n username]]} {
368 sendprivmsg $n \
369 "(But to do that you must register your nick securely first.)"
370 }
371 }
83dd1224
IJ
372 }
373}
374
422f52e4
IJ
375proc chanmode_o0 {m g p chan} {
376 global nick chandeop
377 prefix_nick
378 set who [chanmode_arg]
83dd1224 379 recordlastseen_p $p "being mean to $who" 1
422f52e4
IJ
380 if {"[irctolower $who]" == "[irctolower $nick]"} {
381 set chandeop($chan) [list [clock seconds] $p]
382 }
cc2d31de 383}
9bc33297 384
422f52e4
IJ
385proc msg_MODE {p c dest modelist args} {
386 if {![ischan $dest]} return
387 if {[regexp {^\-(.+)$} $modelist dummy modelist]} {
388 set give 0
389 } elseif {[regexp {^\+(.+)$} $modelist dummy modelist]} {
390 set give 1
391 } else {
392 error "invalid modelist"
393 }
394 foreach m [split $modelist] {
395 set procname chanmode_$m$give
396 if {[catch { info body $procname }]} {
83dd1224 397 recordlastseen_p $p "fiddling with $dest" 1
422f52e4
IJ
398 } else {
399 $procname $m $give $p $dest
400 }
401 }
402}
403
a4a1f396
IJ
404proc channel_noone_seen {chan} {
405 global nick_onchans
406 foreach n [array names nick_onchans] {
407 upvar #0 nick_onchans($n) oc
408 set oc [grep tc {"$tc" != "$chan"} $oc]
409 }
410}
411
a056c4bd 412proc process_kickpart {chan user} {
a4a1f396 413 global nick
a056c4bd
IJ
414 check_nick $user
415 if {![ischan $chan]} { error "not a channel" }
a4a1f396
IJ
416 if {"[irctolower $user]" == "[irctolower $nick]"} {
417 channel_noone_seen $chan
418 }
a056c4bd
IJ
419 upvar #0 nick_onchans($user) oc
420 set lc [irctolower $chan]
421 set oc [grep tc {"$tc" != "$lc"} $oc]
d83fb8db 422 if {![llength $oc]} { nick_forget $user }
a4a1f396 423 nick_case $user
a056c4bd
IJ
424}
425
426proc msg_KICK {p c chans users comment} {
427 set chans [split $chans ,]
428 set users [split $users ,]
429 if {[llength $chans] > 1} {
430 foreach chan $chans user $users { process_kickpart $chan $user }
431 } else {
432 foreach user $users { process_kickpart [lindex $chans 0] $user }
433 }
434}
435
436proc msg_KILL {p c user why} {
437 nick_forget $user
438}
439
20087363
IJ
440set nick_counter 0
441set nick_arys {onchans username unique}
a056c4bd
IJ
442
443proc nick_forget {n} {
444 global nick_arys
445 foreach ary $nick_arys {
446 upvar #0 nick_${ary}($n) av
447 catch { unset av }
448 }
a4a1f396
IJ
449 nick_case $n
450}
451
452proc nick_case {n} {
453 global nick_case
454 set nick_case([irctolower $n]) $n
a056c4bd
IJ
455}
456
83dd1224 457proc msg_NICK {p c newnick} {
a4a1f396 458 global nick_arys nick_case
83dd1224
IJ
459 prefix_nick
460 recordlastseen_n $n "changing nicks to $newnick" 0
461 recordlastseen_n $newnick "changing nicks from $n" 1
a056c4bd
IJ
462 foreach ary $nick_arys {
463 upvar #0 nick_${ary}($n) old
464 upvar #0 nick_${ary}($newnick) new
465 if {[info exists new]} { error "nick collision ?! $ary $n $newnick" }
466 if {[info exists old]} { set new $old; unset old }
467 }
a4a1f396 468 nick_case $newnick
83dd1224
IJ
469}
470
20087363
IJ
471proc nick_ishere {n} {
472 global nick_counter
473 upvar #0 nick_unique($n) u
474 if {![info exists u]} { set u [incr nick_counter].$n.[clock seconds] }
475 nick_case $n
476}
477
a056c4bd
IJ
478proc msg_JOIN {p c chan} {
479 prefix_nick
480 recordlastseen_n $n "joining $chan" 1
481 upvar #0 nick_onchans($n) oc
482 lappend oc [irctolower $chan]
20087363 483 nick_ishere $n
a056c4bd
IJ
484}
485proc msg_PART {p c chan} {
486 prefix_nick
487 recordlastseen_n $n "leaving $chan" 1
488 process_kickpart $chan $n
489}
490proc msg_QUIT {p c why} {
491 prefix_nick
492 recordlastseen_n $n "leaving ($why)" 0
493 nick_forget $n
494}
422f52e4 495
cc2d31de
IJ
496proc msg_PRIVMSG {p c dest text} {
497 prefix_nick
422f52e4 498 if {[ischan $dest]} {
83dd1224 499 recordlastseen_n $n "invoking me in $dest" 1
422f52e4 500 set output $dest
cc2d31de 501 } else {
83dd1224 502 recordlastseen_n $n "talking to me" 1
422f52e4
IJ
503 set output $n
504 }
a4a1f396 505 nick_case $n
422f52e4
IJ
506
507 if {[catch {
508 regsub {^! *} $text {} text
509 set ucmd [ta_word]
83dd1224 510 set procname ucmd/[string tolower $ucmd]
422f52e4
IJ
511 if {[catch { info body $procname }]} {
512 error "unknown command; try help for help"
513 }
83dd1224 514 $procname $p $dest
422f52e4 515 } rv]} {
7ce72032 516 sendprivmsg $n "error: $rv"
422f52e4 517 } else {
7ce72032 518 manyset $rv priv_msgs pub_msgs priv_acts pub_acts
7a70431a 519 foreach {td val} [list $n $priv_acts $output $pub_acts] {
7ce72032 520 foreach l [split $val "\n"] {
7a70431a 521 sendaction $td $l
7ce72032
IJ
522 }
523 }
7a70431a 524 foreach {td val} [list $n $priv_msgs $output $pub_msgs] {
422f52e4 525 foreach l [split $val "\n"] {
7a70431a 526 sendprivmsg $td $l
422f52e4
IJ
527 }
528 }
529 }
530}
531
a056c4bd
IJ
532proc msg_INVITE {p c n chan} {
533 after 1000 [list sendout JOIN $chan]
534}
535
536proc grep {var predicate list} {
537 set o {}
538 upvar 1 $var v
539 foreach v $list {
540 if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
541 }
542 return $o
543}
544
545proc msg_353 {p c dest type chan nicklist} {
546 global names_chans nick_onchans
547 if {![info exists names_chans]} { set names_chans {} }
548 set chan [irctolower $chan]
549 lappend names_chans $chan
a4a1f396 550 channel_noone_seen $chan
a056c4bd
IJ
551 foreach n [split $nicklist { }] {
552 regsub {^[@+]} $n {} n
a056c4bd 553 if {![string length $n]} continue
a4a1f396 554 check_nick $n
a056c4bd
IJ
555 upvar #0 nick_onchans($n) oc
556 lappend oc $chan
20087363 557 nick_ishere $n
a056c4bd
IJ
558 }
559}
560
561proc msg_366 {p c args} {
562 global names_chans nick_onchans
563 if {[llength names_chans] > 1} {
564 foreach n [array names nick_onchans] {
565 upvar #0 nick_onchans($n) oc
566 set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
567 if {![llength $oc]} { nick_forget $n }
568 }
569 }
570 unset names_chans
571}
572
11d9bff9
IJ
573proc ta_anymore {} {
574 upvar 1 text text
575 return [expr {!![string length $text]}]
576}
577
422f52e4
IJ
578proc ta_nomore {} {
579 upvar 1 text text
580 if {[string length $text]} { error "too many parameters" }
581}
582
583proc ta_word {} {
584 upvar 1 text text
585 if {![regexp {^([^ ]+) *(.*)} $text dummy firstword text]} {
586 error "too few parameters"
587 }
588 return $firstword
589}
590
591proc ta_nick {} {
592 upvar 1 text text
593 set v [ta_word]
594 check_nick $v
595 return $v
596}
597
83dd1224
IJ
598proc def_ucmd {cmdname body} {
599 proc ucmd/$cmdname {p dest} " upvar 1 text text\n$body"
600}
601
7ce72032
IJ
602proc ucmdr {priv pub args} {
603 return -code return [concat [list $priv $pub] $args]
422f52e4 604}
e1ba63be 605
e6cc22dc
IJ
606proc loadhelp {} {
607 global help_topics
608
609 catch { unset help_topics }
610 set f [open helpinfos r]
d83fb8db
IJ
611 try_except_finally {
612 set lno 0
613 while {[gets $f l] >= 0} {
614 incr lno
615 if {[regexp {^#.*} $l]} {
616 } elseif {[regexp {^ *$} $l]} {
617 if {[info exists topic]} {
618 set help_topics($topic) [join $lines "\n"]
619 unset topic
620 unset lines
621 }
622 } elseif {[regexp {^!([-+._0-9a-z]*)$} $l dummy newtopic]} {
623 if {[info exists topic]} {
624 error "help $newtopic while in $topic"
625 }
626 set topic $newtopic
627 set lines {}
628 } elseif {[regexp {^[^!#]} $l]} {
629 set topic
630 lappend lines [string trimright $l]
631 } else {
632 error "eh ? $lno: $l"
e6cc22dc 633 }
e6cc22dc 634 }
d83fb8db
IJ
635 if {[info exists topic]} { error "unfinished topic $topic" }
636 } {} {
637 close $f
e6cc22dc 638 }
422f52e4
IJ
639}
640
e6cc22dc
IJ
641def_ucmd help {
642 upvar #0 help_topics([irctolower [string trim $text]]) info
4fd2739c 643 if {![info exists info]} { ucmdr "No help on $text, sorry." {} }
e6cc22dc
IJ
644 ucmdr $info {}
645}
e1ba63be 646
e6cc22dc
IJ
647def_ucmd ? {
648 global help_topics
649 ucmdr $help_topics() {}
650}
e1ba63be 651
4fd2739c 652proc check_username {target} {
7ce72032
IJ
653 if {
654 [string length $target] > 8 ||
655 [regexp {[^-0-9a-z]} $target] ||
656 ![regexp {^[a-z]} $target]
657 } { error "invalid username" }
4fd2739c
IJ
658}
659
20087363 660proc somedb__head {} {
7a70431a 661 uplevel 1 {
20087363
IJ
662 set idl [irctolower $id]
663 upvar #0 ${nickchan}db($idl) ndbe
664 binary scan $idl H* idh
665 set idfn $fprefix$idh
666 if {![info exists iddbe] && [file exists $idfn]} {
667 set f [open $idfn r]
7a70431a
IJ
668 try_except_finally { set newval [read $f] } {} { close $f }
669 if {[llength $newval] % 2} { error "invalid length" }
20087363 670 set iddbe $newval
7a70431a
IJ
671 }
672 }
673}
674
20087363
IJ
675proc def_somedb {name arglist body} {
676 foreach {nickchan fprefix} {nick users/n chan chans/c} {
677 proc ${nickchan}db_$name $arglist \
678 "set nickchan $nickchan; set fprefix $fprefix; somedb__head; $body"
679 }
7a70431a
IJ
680}
681
20087363
IJ
682def_somedb exists {id} {
683 return [info exists iddbe]
7a70431a
IJ
684}
685
20087363
IJ
686def_somedb delete {id} {
687 catch { unset iddbe }
688 file delete $idfn
7a70431a
IJ
689}
690
20087363
IJ
691set default_settings_nick {timeformat ks}
692set default_settings_chan {autojoin 1}
7a70431a 693
20087363
IJ
694def_somedb set {id args} {
695 upvar #0 default_settings_$nickchan def
696 if {![info exists iddbe]} { set iddbe $def }
697 foreach {key value} [concat $iddbe $args] { set a($key) $value }
7a70431a
IJ
698 set newval {}
699 foreach {key value} [array get a] { lappend newval $key $value }
20087363 700 set f [open $idfn.new w]
7a70431a
IJ
701 try_except_finally {
702 puts $f $newval
703 close $f
20087363 704 file rename -force $idfn.new $idfn
7a70431a 705 } {
7a70431a 706 } {
d83fb8db 707 catch { close $f }
7a70431a 708 }
20087363 709 set iddbe $newval
7a70431a
IJ
710}
711
20087363
IJ
712def_somedb get {id key} {
713 upvar #0 default_settings_$nickchan def
714 if {[info exists iddbe]} {
715 set l $iddbe
7a70431a 716 } else {
20087363 717 set l $def
7a70431a
IJ
718 }
719 foreach {tkey value} $l {
720 if {"$tkey" == "$key"} { return $value }
721 }
722 error "unset setting $key"
723}
724
20087363
IJ
725proc opt {key} {
726 global calling_nick
727 if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
728 return [nickdb_get $n $key]
729}
730
7a70431a
IJ
731proc check_notonchan {} {
732 upvar 1 dest dest
733 if {[ischan $dest]} { error "that command must be sent privately" }
734}
735
736proc nick_securitycheck {strict} {
737 upvar 1 n n
738 if {![nickdb_exists $n]} { error "you are unknown to me, use `register'." }
20087363 739 set wantu [nickdb_get $n username]
7a70431a
IJ
740 if {![string length $wantu]} {
741 if {$strict} {
742 error "that feature is only available to secure users, sorry."
743 } else {
744 return
745 }
746 }
747 upvar #0 nick_username($n) nu
748 if {![info exists nu]} {
749 error "nick $n is secure, you must identify yourself first."
750 }
751 if {"$wantu" != "$nu"} {
752 error "you are the wrong user - the nick $n belongs to $wantu, not $nu"
753 }
754}
755
20087363
IJ
756proc channel_securitycheck {channel n} {
757 # You must also call `nick_securitycheck 1'
758 if {[lsearch -exact [irctolower [chandb_get $channel managers]] $n] < 0} {
759 error "you are not a manager of $channel"
760 }
761}
762
763proc def_chancmd {name body} {
764 proc channel/$name {} \
765 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
766}
767
768def_chancmd manager {
769 set opcode [ta_word]
770 switch -exact _$opcode {
771 _= { set ml {} }
772 _+ - _- {
773 if {[chandb_exists $chan]} {
774 set ml [chandb_get $chan managers]
775 } else {
776 set ml [list [irctolower $n]]
777 }
778 }
779 default {
780 error "`channel manager' opcode must be one of + - ="
781 }
782 }
783 foreach nn [split $text " "] {
784 if {![string length $nn]} continue
785 check_nick $nn
786 set nn [irctolower $nn]
787 if {"$opcode" != "-"} {
788 lappend ml $nn
789 } else {
790 set ml [grep nq {"$nq" != "$nn"} $ml]
791 }
792 }
793 if {[llength $ml]} {
794 chandb_set $chan managers $ml
795 ucmdr "Managers of $chan: $ml" {}
796 } else {
797 chandb_delete $chan
798 ucmdr {} {} "forgets about managing $chan." {}
799 }
800}
801
802def_chancmd autojoin {
803 set yesno [ta_word]
804 switch -exact [string tolower $yesno] {
805 no { set nv 0 }
806 yes { set nv 1 }
807 default { error "channel autojoin must be `yes' or `no' }
808 }
809 chandb_set $chan autojoin $nv
810}
811
812def_chancmd show {
813 if {[chandb_exists $chan]} {
814 set l "Settings for $chan: autojoin "
815 append l [lindex {no yes} [chandb_get $chan autojoin]]
816 append l "\nManagers: "
817 append l [join [chandb_get $chan managers] " "]
818 ucmdr {} $l
819 } else {
820 ucmdr {} "The channel $chan is not managed."
821 }
822}
823
824def_ucmd op {
825 if {[ischan $dest]} { set target $dest }
826 if {[ta_anymore]} { set target [ta_word] }
827 ta_nomore
828 if {![info exists target]} { error "you must specify, or !... on, the channel" }
829 if {![ischan $target]} { error "not a valid channel" }
830 if {![chandb_exists $target]} { error "$target is not a managed channel." }
831 prefix_nick
832 nick_securitycheck 1
833 channel_securitycheck $target $n
834 sendout MODE $target +o $n
835}
836
837def_ucmd channel {
838 if {[ischan $dest]} { set target $dest }
839 if {![ta_anymore]} {
840 set subcmd show
841 } else {
842 set subcmd [ta_word]
843 }
844 if {[ischan $subcmd]} {
845 set target $subcmd
846 if {![ta_anymore]} {
847 set subcmd show
848 } else {
849 set subcmd [ta_word]
850 }
851 }
852 if {![info exists target]} { error "privately, you must specify a channel" }
853 set procname channel/$subcmd
854 if {"$subcmd" != "show"} {
855 if {[catch { info body $procname }]} { error "unknown channel setting $subcmd" }
856 prefix_nick
857 nick_securitycheck 1
858 if {[chandb_exists $target]} {
859 channel_securitycheck $target $n
860 } else {
861 upvar #0 chan_initialop([irctolower $target]) io
862 upvar #0 nick_unique($n) u
863 if {![info exists io]} { error "$target is not a managed channel" }
864 if {"$io" != "$u"} { error "you are not the interim manager of $target" }
865 if {"$subcmd" != "manager"} { error "use `channel manager' first" }
866 }
867 }
868 channel/$subcmd
869}
870
a4a1f396
IJ
871def_ucmd who {
872 if {[ta_anymore]} {
873 set target [ta_word]; ta_nomore
874 set myself 1
875 } else {
876 prefix_nick
877 set target $n
878 set myself [expr {"$target" != "$n"}]
879 }
880 upvar #0 nick_case([irctolower $target]) nc
881 set nshow $target
882 if {[info exists nc]} {
883 upvar #0 nick_onchans($nc) oc
884 upvar #0 nick_username($nc) nu
885 if {[info exists oc]} { set nshow $nc }
886 }
887 if {![nickdb_exists $target]} {
888 set ol "$nshow is not a registered nick."
20087363 889 } elseif {[string length [set username [nickdb_get $target username]]]} {
a4a1f396
IJ
890 set ol "The nick $nshow belongs to the user $username."
891 } else {
892 set ol "The nick $nshow is registered (but not to a username)."
893 }
894 if {![info exists nc] || ![info exists oc]} {
895 if {$myself} {
896 append ol "\nI can't see $nshow on anywhere."
897 } else {
898 append ol "\nYou aren't on any channels with me."
899 }
900 } elseif {![info exists nu]} {
901 append ol "\n$nshow has not identified themselves."
902 } elseif {![info exists username]} {
903 append ol "\n$nshow has identified themselves as the user $nu."
904 } elseif {"$nu" != "$username"} {
905 append ol "\nHowever, $nshow is being used by the user $nu."
906 } else {
907 append ol "\n$nshow has identified themselves to me."
908 }
909 ucmdr {} $ol
910}
911
7a70431a
IJ
912def_ucmd register {
913 prefix_nick
914 check_notonchan
915 set old [nickdb_exists $n]
916 if {$old} { nick_securitycheck 0 }
917 switch -exact [string tolower [string trim $text]] {
918 {} {
919 upvar #0 nick_username($n) nu
920 if {![info exists nu]} {
921 ucmdr {} \
a4a1f396 922 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
7a70431a
IJ
923 }
924 nickdb_set $n username $nu
925 ucmdr {} {} "makes a note of your username." {}
926 }
927 delete {
928 nickdb_delete $n
929 ucmdr {} {} "forgets your nickname." {}
930 }
931 insecure {
932 nickdb_set $n username {}
933 if {$old} {
934 ucmdr {} "Security is now disabled for your nickname !"
935 } else {
936 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."
937 }
938 }
939 }
11d9bff9
IJ
940}
941
942proc timeformat_desc {tf} {
943 switch -exact $tf {
86892128 944 ks { return "Times will be displayed in seconds or kiloseconds." }
11d9bff9
IJ
945 hms { return "Times will be displayed in hours, minutes, etc." }
946 default { error "invalid timeformat: $v" }
947 }
948}
949
950proc def_setting {opt show_body set_body} {
951 proc set_show/$opt {} "
952 upvar 1 n n
953 set opt $opt
954 $show_body"
955 if {![string length $set_body]} return
956 proc set_set/$opt {} "
957 upvar 1 n n
958 upvar 1 text text
959 set opt $opt
960 $set_body"
961}
962
963def_setting timeformat {
20087363 964 set tf [nickdb_get $n timeformat]
11d9bff9
IJ
965 return "$tf: [timeformat_desc $tf]"
966} {
967 set tf [string tolower [ta_word]]
968 ta_nomore
969 set desc [timeformat_desc $tf]
970 nickdb_set $n timeformat $tf
971 ucmdr {} $desc
972}
973
974def_setting security {
20087363 975 set s [nickdb_get $n username]
11d9bff9
IJ
976 if {[string length $s]} {
977 return "Your nick, $n, is controlled by the user $s."
978 } else {
979 return "Your nick, $n, is not secure."
980 }
981} {}
982
983def_ucmd set {
984 prefix_nick
985 check_notonchan
986 if {![nickdb_exists $n]} {
d83fb8db 987 ucmdr {} "You are unknown to me and so have no settings. (Use `register'.)"
11d9bff9
IJ
988 }
989 if {![ta_anymore]} {
990 set ol {}
991 foreach proc [lsort [info procs]] {
992 if {![regexp {^set_show/(.*)$} $proc dummy opt]} continue
993 lappend ol [format "%-10s %s" $opt [set_show/$opt]]
994 }
995 ucmdr {} [join $ol "\n"]
996 } else {
997 set opt [ta_word]
998 if {[catch { info body set_show/$opt }]} {
999 error "no setting $opt"
1000 }
1001 if {![ta_anymore]} {
1002 ucmdr {} "$opt [set_show/$opt]"
1003 } else {
86892128 1004 nick_securitycheck 0
11d9bff9
IJ
1005 if {[catch { info body set_set/$opt }]} {
1006 error "setting $opt cannot be set with `set'"
1007 }
1008 set_set/$opt
1009 }
1010 }
1011}
7a70431a 1012
4fd2739c
IJ
1013def_ucmd identpass {
1014 set username [ta_word]
d83fb8db 1015 set passmd5 [md5sum "[ta_word]\n"]
4fd2739c
IJ
1016 ta_nomore
1017 prefix_nick
11d9bff9 1018 check_notonchan
4fd2739c
IJ
1019 upvar #0 nick_onchans($n) onchans
1020 if {![info exists onchans] || ![llength $onchans]} {
7a70431a 1021 ucmdr "You must be on a channel with me to identify yourself." {}
4fd2739c
IJ
1022 }
1023 check_username $username
1024 exec userv --timeout 3 $username << "$passmd5\n" > /dev/null \
1025 irc-identpass $n
1026 upvar #0 nick_username($n) rec_username
1027 set rec_username $username
7a70431a 1028 ucmdr "Pleased to see you, $username." {}
4fd2739c
IJ
1029}
1030
1031def_ucmd summon {
1032 set target [ta_word]
1033 ta_nomore
1034 check_username $target
7ce72032
IJ
1035 prefix_nick
1036
1037 upvar #0 lastsummon($target) ls
1038 set now [clock seconds]
1039 if {[info exists ls]} {
1040 set interval [expr {$now - $ls}]
1041 if {$interval < 30} {
1042 ucmdr {} \
1043 "Please be patient; $target was summoned only [showinterval $interval]."
1044 }
1045 }
1046 regsub {^[^!]*!} $p {} path
1047 if {[catch {
1048 exec userv --timeout 3 $target irc-summon $n $path \
1049 [expr {[ischan $dest] ? "$dest" : ""}] \
1050 < /dev/null
1051 } rv]} {
1052 regsub -all "\n" $rv { / } rv
1053 error $rv
1054 }
1055 if {[regexp {^problem (.*)} $rv dummy problem]} {
8a8d337d 1056 ucmdr {} "The user `$target' $problem."
7ce72032
IJ
1057 } elseif {[regexp {^ok ([^ ]+) ([0-9]+)$} $rv dummy tty idlesince]} {
1058 set idletime [expr {$now - $idlesince}]
1059 set ls $now
1060 ucmdr {} {} {} "invites $target ($tty[expr {
1061 $idletime > 10 ? ", idle for [showintervalsecs $idletime]" : ""
1062 }]) to [expr {
1063 [ischan $dest] ? "join us here" : "talk to you"
1064 }]."
1065 } else {
1066 error "unexpected response from userv service: $rv"
1067 }
1068}
1069
a69f7d2c
IJ
1070proc md5sum {value} { exec md5sum << $value }
1071
83dd1224 1072def_ucmd seen {
422f52e4 1073 global lastseen nick
83dd1224
IJ
1074 prefix_nick
1075 set ncase [ta_nick]
1076 set nlower [irctolower $ncase]
422f52e4 1077 ta_nomore
83dd1224
IJ
1078 set now [clock seconds]
1079 if {"$nlower" == "[irctolower $nick]"} {
422f52e4 1080 error "I am not self-aware."
83dd1224
IJ
1081 } elseif {![info exists lastseen($nlower)]} {
1082 set rstr "I've never seen $ncase."
422f52e4 1083 } else {
83dd1224
IJ
1084 manyset $lastseen($nlower) realnick time what
1085 set howlong [expr {$now - $time}]
1086 set string [showinterval $howlong]
1087 set rstr "I last saw $realnick $string, $what."
1088 }
1089 if {[ischan $dest]} {
1090 set where $dest
1091 } else {
1092 set where {}
1093 }
1094 upvar #0 lookedfor($nlower) lf
1095 if {[info exists lf]} { set oldvalue $lf } else { set oldvalue {} }
1096 set lf [list [list $now $n $where]]
1097 foreach v $oldvalue {
1098 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1099 lappend lf $v
cc2d31de 1100 }
83dd1224 1101 ucmdr {} $rstr
cc2d31de
IJ
1102}
1103
1104if {![info exists sock]} {
1105 set sock [socket $host $port]
1106 fconfigure $sock -buffering line
1107 #fconfigure $sock -translation binary
1108 fconfigure $sock -translation crlf
1109
b3d361ab 1110 sendout USER blight 0 * $ownfullname
cc2d31de
IJ
1111 sendout NICK $nick
1112 fileevent $sock readable onread
1113}
1114
e6cc22dc
IJ
1115loadhelp
1116
8979e0d6
IJ
1117#if {![regexp {tclsh} $argv0]} {
1118# vwait terminate
1119#}