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