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