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