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