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