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