9 defset marktime_min
300
10 defset marktime_join_startdelay
5000
12 proc privmsg_unlogged
{prefix ischan params
} {
14 [regexp {^
![a-z
][-a-z
]*[a-z
]( .
*)?
$} [lindex $params 1]]} {
17 # on-channel message, ignore
18 set chan
[lindex $params 0]
19 upvar #0 chan_lastactivity([irctolower $chan]) la
20 set la
[clock seconds
]
21 catch_logged
{ recordlastseen_p
$prefix "talking on $chan" 2 }
25 proc showintervalsecs
{howlong abbrev
} {
26 return [showintervalsecs
/[opt timeformat
] $howlong $abbrev]
29 proc showintervalsecs
/ks
{howlong abbrev
} {
30 if {$howlong < 1000} {
33 if {$howlong < 1000000} {
40 set value
[expr "$howlong.0 / $scale"]
41 foreach {min
format} {100 %.0f
10 %.1f
1 %.2f
} {
42 if {$value < $min} continue
43 return [format "$format${pfx}s" $value]
48 proc format_qty
{qty unit abbrev
} {
51 append o
[string range
$unit 0 0]
55 if {$qty != 1} { append o s
}
60 proc showintervalsecs
/hms
{qty abbrev
} {
61 set ul
{second
60 minute
60 hour
24 day
7 week
}
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]
69 set o
[format_qty
$qty [lindex $ul 0] $abbrev]
71 if {!$abbrev} { append o
" " }
72 append o
[format_qty
$remainv $remainu $abbrev]
77 proc showinterval
{howlong
} {
81 return "[showintervalsecs $howlong 0] ago"
85 proc showtime
{when
} {
86 return [showinterval
[expr {[clock seconds
] - $when}]]
89 proc parse_interval
{specified min
} {
90 if {![regexp {^
([0-9]+)([a-z
]+)$} $specified dummy value unit
]} {
91 error "invalid syntax for interval"
98 default { error "unknown unit of time $unit" }
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)" }
106 proc 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\
114 def_msgproc begin
{dest str
} {
120 def_msgproc
append {str
} {
122 if {[string length
$s] && [string length
$ns] > 65} {
123 msg__sendout
$varbase
124 set s
" [string trimleft $str]"
130 def_msgproc finish
{} {
131 msg__sendout
$varbase
137 def_msgproc _sendout
{} {
138 lappend a
[string trimright
$s]
142 proc looking_whenwhere
{when where
} {
143 set str
[showtime
[expr {$when-1}]]
144 if {[string length
$where]} { append str
" on $where" }
148 proc tell_event
{nl
event} {
149 # For `act' we *haven't* yet done the 750ms delay; we implement
150 # that here. Also, here we turn `talk' into `talk' now and `act'
151 # later. We also support the psuedo-event `none'. The del msg
152 # and new msg events are handled by the command procedures, not here.
154 if {[info exists calling_nick
] { set save
$calling_nick }
155 switch -exact $event {
158 tell_event_core
$nl talk
162 after 750 [list tell_event_core
$nl $event]
165 tell_event_core
$nl $event
168 error "tell_event $nl $event"
171 if {[info exists save
]} { set calling_nick
$save }
174 proc tell_event_core
{nl
event} {
175 # event is `talk', `act' or `ident'
176 # When user talks we actually get talk now and act later
177 FIXME
- implement all cmds
178 FIXME
- implement tells_delete_msgs
catch { unset stt
} ?
179 FIXME
- document the extra param to recordlastseen
182 set iml
[msgdb_get
$nl inbound
]
183 if {![llength $iml]} return
185 upvar #0 nick_telling($nl) telling
186 upvar #0 nick_unique($nl) u
188 if {[info exists telling
]} {
189 manyset
$telling u_last stt telling_when
190 if {![info exists u
] ||
"$u_last" != "$u"} {
191 unset telling
; unset stt
; unset telling_when
195 if {![info exists stt
]} {
197 set telling_when
$now
200 set ago
[expr {$now - $telling_when}]
202 # Now we have the components of a telling state
203 # u - nick_unique (unset if not visible)
204 # stt - state: norecord, mentioned, passede
205 # ago - how long ago since we did anything
207 # We compute an evstate to dispatch on as follows:
209 # evstate is string of letters
211 # n NORECORD (MESSAGES)
218 # security level and timing
220 # ss Secure and soon (before interval)
221 # sl Secure and late (after interval)
222 # current identification
225 # reliability and timing
227 # rv Remind, very soon (before within-interval)
228 # rs Remind, soon (between)
229 # rl Remind, late (after every-interval)
230 # ps Pester, soon (before interval)
231 # pl Pester, late (after interval)
235 append evstate
[string range
$stt 0 0]
236 append evstate
[string range
$event 0 0]
238 manyset
[tell_effective_sec
$n] sec secwhen
240 insecure
{ append evstate ii
}
241 secure
{ append evstate
[expr {$ago<$secwhen ?
"sl" : "ss"}] }
242 default { append evstate
"#$sec#" }
245 upvar #0 nick_username($nl) nu
246 if {[info exists nu
] && "$nu" == "[nickdb_get $nl username]"} {
252 manyset
[nickdb_set
$n tellrel
] rel relint relwithin
254 unreliable
{ append evstate uu
}
255 remind
{ append evstate
[expr {
256 $ago<$relwithin ?
"rv" : $ago<$relint ?
"rs" : "rl"
258 pester
{ append evstate
[expr {$ago<$relint ?
"ps" : "pl"}] }
259 default { append evstate
"#$rel#" }
262 switch -glob $evstate {
264 # consider delivered:
265 # (very recently passed, and the user talks)
266 tell_delete_msgs
{} $nl
270 pt???uu
- pt???rs
- pt???rl
- pt???p?
{
271 # ignore (any other `talk's) - act handles these
274 ni?????
- naii???
- nas?i??
- mi?????
- pa????l
{
275 # pass and then stuff
276 if {[length
$iml] == 3} {
277 manyset
$iml sender sentwhen msg
279 "$sender asked me [showintervalsecs [expr {$now-$sentwhen}] 0]\
283 "Here are the [expr {[llength $iml]/3}] messages there are for you:"
284 while {[llength $iml] >= 3} {
285 manyset
[lrange $iml 0 2] sender sentwhen msg
286 set iml
[lrange $iml 3 end
]
288 " [showintervalsecs [expr {$now-$sentwhen}] 1] <$sender> $msg"
291 if {"$rel" == "unreliable"} {
292 tell_delete_msgs
{} $nl
298 sendprivmsg
$nl {You have messages
(so identify yourself please
).
}]
302 sendprivmsg
$nl {Don't forget about your messages.
}]
307 mass???
- pa????v
- pa????s
{
312 error "tell_event_core nl=$nl evstate=$evstate ?"
315 if {![info exists u
]} {
316 catch { unset telling
}
318 set telling
[list $u $stt $now]
322 proc recordlastseen_n
{n how here
} {
323 global lastseen lookedfor
324 set nl
[irctolower
$n]
325 set now
[clock seconds
]
326 set lastseen
($nl) [list $n $now $how]
330 tell_event
$nl [lindex {none act talk
} $here]
332 upvar #0 lookedfor($nl) lf
333 if {[info exists lf
]} {
334 switch -exact [llength $lf] {
339 manyset
[lindex $lf 0] when who where
341 "FYI, $who was looking for you [looking_whenwhere $when $where]."]
344 msg_begin tosend
$n "FYI, people have been looking for you:"
350 msg_append tosend
" "
351 } elseif
{$i == [llength $lf]} {
352 msg_append tosend
" and "
355 msg_append tosend
", "
357 manyset
$e when who where
359 "$who ([looking_whenwhere $when $where])$fin"
361 set ml
[msg_finish tosend
]
365 msendprivmsg_delayed
1000 $n $ml
369 proc note_topic
{showoff whoby topic
} {
370 set msg
"FYI, $whoby has changed the topic on $showoff"
371 if {[string length
$topic] < 160} {
372 append msg
" to $topic"
374 append msg
" but it is too long to reproduce here !"
376 set showoff
[irctolower
$showoff]
377 set tell [chandb_get
$showoff topictell
]
378 if {[lsearch -exact $tell *] >= 0} {
379 set tryspies
[chandb_list
]
383 foreach spy
$tryspies {
384 set see
[chandb_get
$spy topicsee
]
385 if {[lsearch -exact $see $showoff] >= 0 ||
\
386 ([lsearch -exact $see *] >= 0 && \
387 [lsearch -exact $tell $spy] >= 0)} {
388 sendprivmsg
$spy $msg
393 proc recordlastseen_p
{p how here
} {
395 recordlastseen_n
$n $how $here
398 proc chanmode_arg
{} {
400 set rv
[lindex $cm_args 0]
401 set cm_args
[lreplace cm_args
0 0]
405 proc chanmode_o1
{m g p chan
} {
406 global nick chan_initialop
408 set who
[chanmode_arg
]
409 recordlastseen_n
$n "being nice to $who" 1
410 if {"[irctolower $who]" == "[irctolower $nick]"} {
411 set nlower
[irctolower
$n]
412 upvar #0 nick_unique($nlower) u
413 if {[chandb_exists
$chan]} {
414 sendprivmsg
$n Thanks.
415 } elseif
{![info exists u
]} {
416 sendprivmsg
$n {Op me
while not on the channel
, why don't you ?
}
418 set chan_initialop
([irctolower
$chan]) $u
420 "Thanks. You can use `channel manager ...' to register this channel."
421 if {![nickdb_exists
$n] ||
![string length
[nickdb_get
$n username
]]} {
423 "(But to do that you must register your nick securely first.)"
429 proc chanmode_o0
{m g p chan
} {
432 set who
[chanmode_arg
]
433 recordlastseen_p
$p "being mean to $who" 1
434 if {"[irctolower $who]" == "[irctolower $nick]"} {
435 set chandeop
($chan) [list [clock seconds
] $p]
439 proc msg_MODE
{p c dest modelist args
} {
440 if {![ischan
$dest]} return
441 if {[regexp {^
\-(.
+)$} $modelist dummy modelist
]} {
443 } elseif
{[regexp {^
\+(.
+)$} $modelist dummy modelist
]} {
446 error "invalid modelist"
448 foreach m
[split $modelist] {
449 set procname chanmode_
$m$give
450 if {[catch { info body
$procname }]} {
451 recordlastseen_p
$p "fiddling with $dest" 1
453 $procname $m $give $p $dest
458 proc leaving
{lchan
} {
459 foreach luser
[array names nick_onchans
] {
460 upvar #0 nick_onchans($luser) oc
461 set oc
[grep tc
{"$tc" != "$lchan"} $oc]
463 upvar #0 chan_nicks($lchan) nlist
465 upvar #0 chan_lastactivity($lchan) la
469 proc doleave
{lchan
} {
474 proc dojoin
{lchan
} {
477 set chan_nicks
($lchan) {}
480 proc check_justme
{lchan
} {
482 upvar #0 chan_nicks($lchan) nlist
483 if {[llength $nlist] != 1} return
484 if {"[lindex $nlist 0]" != "[irctolower $nick]"} return
485 if {[chandb_exists
$lchan]} {
486 set mode
[chandb_get
$lchan mode
]
487 if {"$mode" != "*"} {
488 sendout MODE
$lchan $mode
490 set topic
[chandb_get
$lchan topicset
]
491 if {[string length
$topic]} {
492 sendout TOPIC
$lchan $topic
499 proc process_kickpart
{chan user
} {
502 set luser
[irctolower
$user]
503 set lchan
[irctolower
$chan]
504 if {![ischan
$chan]} { error "not a channel" }
505 if {"$luser" == "[irctolower $nick]"} {
508 upvar #0 nick_onchans($luser) oc
509 upvar #0 chan_nicks($lchan) nlist
510 set oc
[grep tc
{"$tc" != "$lchan"} $oc]
511 set nlist
[grep tn
{"$tn" != "$luser"} $nlist]
513 if {![llength $oc]} {
521 proc msg_TOPIC
{p c dest topic
} {
523 if {![ischan
$dest]} return
524 recordlastseen_n
$n "changing the topic on $dest" 1
525 note_topic
[irctolower
$dest] $n $topic
528 proc msg_KICK
{p c chans users comment
} {
529 set chans
[split $chans ,]
530 set users
[split $users ,]
531 if {[llength $chans] > 1} {
532 foreach chan
$chans user
$users { process_kickpart
$chan $user }
534 foreach user
$users { process_kickpart
[lindex $chans 0] $user }
538 proc msg_KILL
{p c user why
} {
543 set nick_arys
{onchans username unique
}
544 # nick_onchans($luser) -> [list ... $lchan ...]
545 # nick_username($luser) -> <securely known local username>
546 # nick_unique($luser) -> <includes-counter>
547 # nick_case($luser) -> $user (valid even if no longer visible)
548 # nick_markid($luser) -> <after id for marktime>
549 # nick_telling($luser) -> <unique> mentioned|passed <when>
551 # chan_nicks($lchan) -> [list ... $luser ...]
552 # chan_lastactivity($lchan) -> [clock seconds]
554 proc lnick_forget
{luser
} {
555 global nick_arys chan_nicks
556 lnick_marktime_cancel
$luser
557 foreach ary
$nick_arys {
558 upvar #0 nick_${ary}($luser) av
561 foreach lch
[array names chan_nicks
] {
562 upvar #0 chan_nicks($lch) nlist
563 set nlist
[grep tn
{"$tn" != "$luser"} $nlist]
568 proc nick_forget
{user
} {
569 global nick_arys chan_nicks
570 lnick_forget
[irctolower
$user]
574 proc nick_case
{user
} {
576 set nick_case
([irctolower
$user]) $user
579 proc msg_NICK
{p c newnick
} {
580 global nick_arys nick_case calling_nick
582 recordlastseen_n
$n "changing nicks to $newnick" 0
583 set calling_nick
$newnick
584 recordlastseen_n
$newnick "changing nicks from $n" 1
585 set luser
[irctolower
$n]
586 lnick_marktime_cancel
$luser
587 set lusernew
[irctolower
$newnick]
588 foreach ary
$nick_arys {
589 upvar #0 nick_${ary}($luser) old
590 upvar #0 nick_${ary}($lusernew) new
591 if {[info exists new
]} { error "nick collision ?! $ary $n $newnick" }
592 if {[info exists old
]} { set new
$old; unset old
}
594 upvar #0 nick_onchans($lusernew) oc
596 upvar #0 chan_nicks($ch) nlist
597 set nlist
[grep tn
{"$tn" != "$luser"} $nlist]
598 lappend nlist
$lusernew
600 lnick_marktime_start
$lusernew "Hi." 500 1
604 proc nick_ishere
{n
} {
606 upvar #0 nick_unique([irctolower $n]) u
607 if {![info exists u
]} { set u
[incr nick_counter
].
$n.
[clock seconds
] }
611 proc msg_JOIN
{p c chan
} {
614 recordlastseen_n
$n "joining $chan" 1
615 set nl
[irctolower
$n]
616 set lchan
[irctolower
$chan]
617 upvar #0 nick_onchans($nl) oc
618 upvar #0 chan_nicks($lchan) nlist
619 if {![info exists oc
]} {
620 global marktime_join_startdelay
621 lnick_marktime_start
$nl "Welcome." $marktime_join_startdelay 1
626 proc msg_PART
{p c chan args
} {
628 set msg
"leaving $chan"
629 if {[llength $args]} {
630 set why
[lindex $args 0]
631 if {"[irctolower $why]" != "[irctolower $n]"} { append msg
" ($why)" }
633 recordlastseen_n
$n $msg 1
634 process_kickpart
$chan $n
636 proc msg_QUIT
{p c why
} {
638 recordlastseen_n
$n "leaving ($why)" 0
642 proc msg_PRIVMSG
{p c dest
text} {
646 if {[ischan
$dest]} {
647 recordlastseen_n
$n "invoking me in $dest" 1
650 recordlastseen_n
$n "talking to me" 1
655 execute_usercommand
$p $c $n $output $dest $text
658 proc msg_INVITE
{p c n chan
} {
659 after 1000 [list dojoin
[irctolower
$chan]]
662 proc grep
{var predicate
list} {
666 if {[uplevel 1 [list expr $predicate]]} { lappend o
$v }
671 proc msg_353
{p c dest type chan nicklist
} {
672 global names_chans nick_onchans
673 set lchan
[irctolower
$chan]
674 upvar #0 chan_nicks($lchan) nlist
675 lappend names_chans
$lchan
676 if {![info exists nlist
]} {
677 # We don't think we're on this channel, so ignore it !
678 # Unfortunately, because we don't get a reply to PART,
679 # we have to remember ourselves whether we're on a channel,
680 # and ignore stuff if we're not, to avoid races. Feh.
684 foreach user
[split $nicklist { }] {
685 regsub {^
[@+]} $user {} user
686 if {![string length
$user]} continue
688 set luser
[irctolower
$user]
689 upvar #0 nick_onchans($luser) oc
691 lappend nlist_new
$luser
697 proc msg_366
{p c args
} {
698 global names_chans nick_onchans
699 set lchan
[irctolower
$c]
700 foreach luser
[array names nick_onchans
] {
701 upvar #0 nick_onchans($luser) oc
702 if {[llength names_chans
] > 1} {
703 set oc
[grep tc
{[lsearch -exact $tc $names_chans] >= 0} $oc]
705 if {![llength $oc]} { lnick_forget
$n }
710 proc check_username
{target
} {
712 [string length
$target] > 8 ||
713 [regexp {[^
-0-9a-z
]} $target] ||
714 ![regexp {^
[a-z
]} $target]
715 } { error "invalid username" }
718 proc somedb__head
{} {
720 set idl
[irctolower
$id]
721 upvar #0 ${nickchan}db($idl) ndbe
722 binary scan $idl H
* idh
723 set idfn
$fprefix$idh
724 if {![info exists iddbe
] && [file exists
$idfn]} {
726 try_except_finally
{ set newval
[read $f] } {} { close $f }
727 if {[llength $newval] % 2} { error "invalid length" }
733 proc def_somedb
{name arglist body
} {
734 foreach {nickchan fprefix
} {
739 proc ${nickchan
}db_
$name $arglist \
740 "set nickchan $nickchan; set fprefix $fprefix; $body"
746 foreach path
[glob -nocomplain -path $fprefix *] {
747 binary scan $path "A[string length $fprefix]A*" afprefix thinghex
748 if {"$afprefix" != "$fprefix"} { error "wrong prefix $path $afprefix" }
749 lappend list [binary format H
* $thinghex]
754 proc def_somedb_id
{name arglist body
} {
755 def_somedb
$name [concat id
$arglist] "somedb__head; $body"
758 def_somedb_id exists
{} {
759 return [info exists iddbe
]
762 def_somedb_id delete
{} {
763 catch { unset iddbe
}
767 set default_settings_nick
{
771 tellrel
{remind
3600 30}
774 set default_settings_chan
{
783 set default_settings_msgs
{
787 # inbound -> [<nick> <time_t> <message>] ...
788 # outbound -> [<nick> <time_t(earliest)> <count>] ...
789 # neither are sorted particularly; only one entry per recipient in
790 # output; both sender and recipient are cased
792 def_somedb_id
set {args
} {
793 upvar #0 default_settings_$nickchan def
794 if {![info exists iddbe
]} { set iddbe
$def }
795 foreach {key value
} [concat $iddbe $args] { set a
($key) $value }
797 foreach {key value
} [array get a
] { lappend newval
$key $value }
798 set f
[open $idfn.new w
]
802 file rename -force $idfn.new
$idfn
810 def_somedb_id get
{key
} {
811 upvar #0 default_settings_$nickchan def
812 if {[info exists iddbe
]} {
813 set l
[concat $iddbe $def]
817 foreach {tkey value
} $l {
818 if {"$tkey" == "$key"} { return $value }
820 error "unset setting $key"
825 if {[info exists calling_nick
]} { set n
$calling_nick } { set n
{} }
826 return [nickdb_get
$n $key]
829 proc check_notonchan
{} {
831 if {[ischan
$dest]} { usererror
"That command must be sent privately." }
834 proc nick_securitycheck
{strict
} {
836 if {![nickdb_exists
$n]} {
837 usererror
"You are unknown to me, use `register'."
839 set wantu
[nickdb_get
$n username
]
840 if {![string length
$wantu]} {
842 usererror
"That feature is only available to secure users, sorry."
847 set luser
[irctolower
$n]
848 upvar #0 nick_username($luser) nu
849 if {![info exists nu
]} {
850 usererror
"Nick $n is secure, you must identify yourself first."
852 if {"$wantu" != "$nu"} {
853 usererror
"You are the wrong user -\
854 the nick $n belongs to $wantu, not $nu."
858 proc channel_ismanager
{channel n
} {
859 set mgrs
[chandb_get
$channel managers
]
860 return [expr {[lsearch -exact [irctolower
$mgrs] [irctolower
$n]] >= 0}]
863 proc channel_securitycheck
{channel
} {
865 if {![channel_ismanager
$channel $n]} {
866 usererror
"You are not a manager of $channel."
871 proc def_chancmd
{name body
} {
872 proc channel
/$name {} \
873 " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
876 proc ta_listop
{findnow procvalue
} {
877 # findnow and procvalue are code fragments which will be executed
878 # in the caller's level. findnow should set ta_listop_ev to
879 # the current list, and procvalue should treat ta_listop_ev as
880 # a proposed value in the list and check and possibly modify
881 # (canonicalise?) it. After ta_listop, ta_listop_ev will
882 # be the new value of the list.
883 upvar 1 ta_listop_ev exchg
886 switch -exact _
$opcode {
890 foreach item
$exchg { set array($item) 1 }
893 error "list change opcode must be one of + - ="
896 foreach exchg
[split $text " "] {
897 if {![string length
$exchg]} continue
899 if {"$opcode" != "-"} {
902 catch { unset array($exchg) }
905 set exchg
[lsort [array names
array]]
908 def_chancmd manager
{
910 if {[chandb_exists
$chan]} {
911 set ta_listop_ev
[chandb_get
$chan managers
]
913 set ta_listop_ev
[list [irctolower
$n]]
916 check_nick
$ta_listop_ev
917 set ta_listop_ev
[irctolower
$ta_listop_ev]
919 if {[llength $ta_listop_ev]} {
920 chandb_set
$chan managers
$ta_listop_ev
921 ucmdr
"Managers of $chan: $ta_listop_ev" {}
924 ucmdr
{} {} "forgets about managing $chan." {}
928 def_chancmd autojoin
{
930 switch -exact [string tolower
$yesno] {
933 default { error "channel autojoin must be `yes' or `no' }
935 chandb_set $chan autojoin $nv
936 ucmdr [expr {$nv ? "I will
join $chan when I'm restarted
" : \
937 "I won't
join $chan when I'm restarted
"}] {}
940 def_chancmd userinvite {
941 set nv [string tolower [ta_word]]
943 pub { set txt "!invite will work
for $chan, but it won't work by
/msg
" }
944 here { set txt "!invite and
/msg invite will work
, but only
for users who are already on
$chan.
" }
945 all { set txt "Any user will be able to invite themselves or anyone
else to
$chan.
" }
946 none { set txt "I will not invite anyone to
$chan.
" }
948 error "channel userinvite must be `pub'
, `here'
, `all' or `none'
951 chandb_set
$chan userinvite
$nv
957 switch -exact $what {
960 chandb_set
$chan topicset
{}
961 ucmdr
"I won't ever change the topic of $chan." {}
964 set t
[string trim
$text]
965 if {![string length
$t]} {
966 error "you must specific the topic to set"
968 chandb_set
$chan topicset
$t
969 ucmdr
"Whenever I'm alone on $chan, I'll set the topic to $t." {}
973 set ta_listop_ev
[chandb_get
$chan topic
$what]
975 if {"$ta_listop_ev" != "*"} {
976 if {![ischan
$ta_listop_ev]} {
977 error "bad channel \`$ta_listop_ev' in topic $what"
979 set ta_listop_ev
[irctolower
$ta_listop_ev]
982 chandb_set
$chan topic
$what $ta_listop_ev
983 ucmdr
"Topic $what list for $chan: $ta_listop_ev" {}
986 usererror
"Unknown channel topic subcommand - see help channel."
993 if {"$mode" != "*" && ![regexp {^
(([-+][imnpst
]+)+)$} $mode mode
]} {
994 error {channel mode must be
* or match
([-+][imnpst
]+)+}
996 chandb_set
$chan mode
$mode
997 if {"$mode" == "*"} {
998 ucmdr
"I won't ever change the mode of $chan." {}
1000 ucmdr
"Whenever I'm alone on $chan, I'll set the mode to $mode." {}
1005 if {[chandb_exists
$chan]} {
1006 set l
"Settings for $chan: autojoin "
1007 append l
[lindex {no yes
} [chandb_get
$chan autojoin
]]
1008 append l
", mode " [chandb_get
$chan mode
]
1009 append l
", userinvite " [chandb_get
$chan userinvite
] "."
1010 append l
"\nManagers: "
1011 append l
[join [chandb_get
$chan managers
] " "]
1012 foreach {ts sep
} {see
"\n" tell " "} {
1013 set t
[chandb_get
$chan topic
$ts]
1016 append l
"Topic $ts list: $t."
1018 append l
"Topic $ts list is empty."
1022 set t
[chandb_get
$chan topicset
]
1023 if {[string length
$t]} {
1024 append l
"Topic to set: $t"
1026 append l
"I will not change the topic."
1030 ucmdr
{} "The channel $chan is not managed."
1034 proc channelmgr_monoop
{} {
1039 upvar 1 target target
1044 if {[ischan
$dest]} { set target
$dest }
1045 if {[ta_anymore
]} { set target
[ta_word
] }
1047 if {![info exists target
]} {
1048 usererror
"You must specify, or invoke me on, the relevant channel."
1050 if {![info exists chan_nicks
([irctolower
$target])]} {
1051 usererror
"I am not on $target."
1053 if {![ischan
$target]} { error "not a valid channel" }
1055 if {![chandb_exists
$target]} {
1056 usererror
"$target is not a managed channel."
1058 channel_securitycheck
$target
1063 sendout MODE
$target +o
$n
1072 global chan_nicks errorCode errorInfo
1075 if {[ischan
$dest]} {
1079 set target
[ta_word
]
1082 set ltarget
[irctolower
$target]
1083 if {![ischan
$target]} { error "$target is not a channel" }
1084 if {![info exists chan_nicks
($ltarget)]} {
1085 usererror
"I am not on $target."
1087 set ui
[chandb_get
$ltarget userinvite
]
1089 if {"$ui" == "pub" && !$onchan} {
1090 usererror
"Invitations to $target must be made there with !invite."
1092 if {"$ui" != "all"} {
1093 if {[lsearch -exact $chan_nicks($ltarget) [irctolower
$n]] < 0} {
1094 usererror
"Invitations to $target may only be made\
1095 by a user on the channel."
1098 if {"$ui" == "none"} {
1099 usererror
"Sorry, I've not been authorised\
1100 to invite people to $target."
1103 if {"$errorCode" == "BLIGHT USER" && [channel_ismanager
$target $n]} {
1105 nick_securitycheck
1
1107 if {"$errorCode" == "BLIGHT USER"} {
1108 usererror
"$emsg2 Therefore you can't use your\
1109 channel manager privilege. $emsg"
1111 error $error $errorInfo $errorCode
1115 error $emsg $errorInfo $errorCode
1118 if {![ta_anymore
]} {
1119 usererror
"You have to say who to invite."
1122 while {[ta_anymore
]} {
1123 set invitee
[ta_word
]
1125 lappend invitees
$invitee
1127 foreach invitee
$invitees {
1128 sendout INVITE
$invitee $ltarget
1130 set who
[lindex $invitees 0]
1131 switch -exact llength $invitees {
1132 0 { error "zero invitees" }
1134 2 { append who
" and [lindex $invitees 1]" }
1136 set who
[join [lreplace $invitees end end
] ", "]
1137 append who
" and [lindex $invitees [llength $invitees]]"
1140 ucmdr
{} {} {} "invites $who to $target."
1144 if {[ischan
$dest]} { set target
$dest }
1145 if {![ta_anymore
]} {
1148 set subcmd
[ta_word
]
1150 if {[ischan
$subcmd]} {
1152 if {![ta_anymore
]} {
1155 set subcmd
[ta_word
]
1158 if {![info exists target
]} { error "privately, you must specify a channel" }
1159 set procname channel
/$subcmd
1160 if {"$subcmd" != "show"} {
1161 if {[catch { info body
$procname }]} {
1162 usererror
"unknown channel setting $subcmd."
1165 if {[chandb_exists
$target]} {
1166 channel_securitycheck
$target
1168 nick_securitycheck
1
1169 upvar #0 chan_initialop([irctolower $target]) io
1170 upvar #0 nick_unique([irctolower $n]) u
1171 if {![info exists io
]} {
1172 usererror
"$target is not a managed channel."
1174 if {"$io" != "$u"} {
1175 usererror
"You are not the interim manager of $target."
1177 if {"$subcmd" != "manager"} {
1178 usererror
"Please use `channel manager' first."
1185 proc tell_effective_sec
{n
} {
1186 set l
[nickdb_get
$n tellsec
]
1187 set u
[nickdb_get
$n username
]
1188 if {"[lindex $l 0]" == "secure" && ![string length
$u]} { set l insecure
}
1192 proc tell_peernicks
{text} {
1193 set text [irctolower
[string trim
$text]]
1194 set senders
[split $text " "]
1195 foreach sender
$senders {
1196 if {[catch { check_nick
$sender } emsg
]} {
1197 error "invalid sender nick `$sender': $emsg" $errorInfo $errorCode
1203 proc tell_delete_msgs
{lsenders lrecip
} {
1204 FIXME
- check callers pass l
* not mixed case
1207 foreach {s t m
} [msgsdb_get
$recip inbound
] {
1208 if {[llength $senders]} {
1209 if {[lsearch -exact $senders [irctolower
$s]] == -1} {
1210 lappend ninbound
$s $t $m
1217 msgsdb_set
$recip inbound
$ninbound
1218 foreach s
[array names rsenders
] {
1220 foreach {r t c
} [msgsdb_get
$s outbound
] {
1221 if {"[irctolower $r]" == "$lrecip"} continue
1222 lappend noutbound
$r $t $c
1224 msgsdb_set
$s outbound
$noutbound
1232 nick_securitycheck
0
1233 set recipients
[tell_peernicks
$text]
1234 if {![llength $recipients]} {
1235 usererror
"You must say which recipients' messages from you to forget."
1238 foreach recip
$recipients {
1239 incr ndel
[tell_delete_msgs
$n $recip]
1241 ucmdr
"Removed $ndel as yet undelivered message(s)." {}
1245 global errorInfo errorCode
1248 manyset
[tell_effective_sec
$n] sec secwhen
1249 switch -exact $sec {
1253 "There are no messages to delete\
1254 because your message disposition prevents them from being left."
1257 nick_securitycheck
1
1260 error "delmsg sec $sec"
1263 set senders
[tell_peernicks
$text]
1264 set ndel
[tell_delete_msgs
[irctolower
$senders] [irctolower
$n]]
1265 if {!$ndel} { ucmdr
"No incoming messages deleted." {} }
1266 switch -exact [llength $senders] {
1267 0 { ucmdr
{} {} "deletes your $ndel message(s)." }
1268 1 { ucmdr
{} {} "deletes your $ndel message(s) from $senders." }
1270 ucmdr
{} {} "deletes your $ndel message(s) from\
1271 [lreplace $senders end end] and [lindex $senders end]."
1277 global nick_case ownmailaddr ownfullname
1280 set target
[ta_word
]
1281 if {![string length
$text]} { error "tell them what?" }
1282 if {[string length
$text] > 400} { error "message too long" }
1284 set ltarget
[irctolower
$target]
1286 if {[info exists nick_case
($ltarget)]} { set ctarget
$nick_case($ltarget) }
1288 manyset
[tell_effective_sec
$target] sec mailtoint mailwhy
1289 manyset
[nickdb_get
$target tellrel
] rel relint relwithin
1290 switch -exact $sec {
1292 set now
[clock seconds
]
1293 set inbound
[msgsdb_get
$ltarget inbound
]
1294 lappend inbound
$n $now $text
1295 msgsdb_set
$ltarget inbound
$inbound
1297 set outbound
[msgsdb_get
$n outbound
]
1300 foreach {recip
time count
} $outbound {
1301 if {"[irctolower $recip]" == "$ltarget"} {
1306 lappend noutbound
$recip $time $count
1309 lappend noutbound
$ctarget $now 1
1311 msgsdb_set
$n outbound
$noutbound
1312 set msg
"OK, I'll tell $ctarget"
1313 if {$found} { append msg
" that too" }
1315 if {"$sec" != "secure"} {
1316 switch -exact $rel {
1317 unreliable
{ append msg
"neither reliably nor securely" }
1318 remind
{ append msg
"pretty reliably, but not securely" }
1319 pester
{ append msg
"reliably but not securely" }
1322 switch -exact $rel {
1323 unreliable
{ append msg
"securely but not reliably" }
1324 remind
{ append msg
"securely and pretty reliably" }
1325 pester
{ append msg
"reliably and securely" }
1332 set fmtmsg
[exec fmt
<< " $text"]
1333 exec /usr
/sbin
/sendmail
-odb -oi -t -oee -f $mailwhy \
1335 "From: $ownmailaddr ($ownfullname)
1337 Subject: IRC tell from $n
1339 $n asked me[expr {[ischan $dest] ? " on
$dest" : ""}] to tell you:
1340 [exec fmt << " $text"]
1342 (This message was for your nick $ctarget; your account $mailwhy
1343 arranged for it to be forwarded to $mailtoint.)
1346 "I've mailed $ctarget, which is what they prefer." \
1350 usererror
"Sorry, $ctarget does not want me to take messages."
1353 error "bad tellsec $sec"
1360 set target
[ta_word
]; ta_nomore
1365 set myself
[expr {"$target" != "$n"}]
1367 set ltarget
[irctolower
$target]
1368 upvar #0 nick_case($ltarget) ctarget
1370 if {[info exists ctarget
]} {
1371 upvar #0 nick_onchans($ltarget) oc
1372 upvar #0 nick_username($ltarget) nu
1373 if {[info exists oc
]} { set nshow
$ctarget }
1375 if {![nickdb_exists
$ltarget]} {
1376 set ol
"$nshow is not a registered nick."
1377 } elseif
{[string length
[set username
[nickdb_get
$target username
]]]} {
1378 set ol
"The nick $nshow belongs to the user $username."
1380 set ol
"The nick $nshow is registered (but not to a username)."
1382 if {![info exists ctarget
] ||
![info exists oc
]} {
1384 append ol
"\nI can't see $nshow on anywhere."
1386 append ol
"\nYou aren't on any channels with me."
1388 } elseif
{![info exists nu
]} {
1389 append ol
"\n$nshow has not identified themselves."
1390 } elseif
{![info exists username
]} {
1391 append ol
"\n$nshow has identified themselves as the user $nu."
1392 } elseif
{"$nu" != "$username"} {
1393 append ol
"\nHowever, $nshow is being used by the user $nu."
1395 append ol
"\n$nshow has identified themselves to me."
1403 set old
[nickdb_exists
$n]
1404 if {$old} { nick_securitycheck
0 }
1405 set luser
[irctolower
$n]
1406 switch -exact [string tolower
[string trim
$text]] {
1408 upvar #0 nick_username($luser) nu
1409 if {![info exists nu
]} {
1411 "You must identify yourself before using `register'. See `help identify', or use `register insecure'."
1413 nickdb_set
$n username
$nu
1414 ucmdr
{} {} "makes a note of your username." {}
1418 ucmdr
{} {} "forgets your nickname." {}
1421 nickdb_set
$n username
{}
1423 ucmdr
{} "Security is now disabled for your nickname !"
1425 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."
1429 error "you mean register / register delete / register insecure"
1434 proc timeformat_desc
{tf
} {
1436 ks
{ return "Times will be displayed in seconds or kiloseconds." }
1437 hms
{ return "Times will be displayed in hours, minutes, etc." }
1438 default { error "invalid timeformat: $v" }
1443 proc def_setting
{opt show_body set_body
} {
1445 lappend settings
$opt
1446 proc set_show
/$opt {} "
1450 if {![string length
$set_body]} return
1451 proc set_set
/$opt {} "
1458 proc tellme_sec_desc
{v n
} {
1459 manyset
$v sec mailtoint
1460 switch -exact $sec {
1462 return "I'll tell you your messages whenever I see you."
1465 if {[string length
[nickdb_get
$n username
]]} {
1467 "I'll keep the bodies of your messages private until you identify yourself, reminding you every [showintervalsecs $mailtoint 1]."
1470 "I'll tell you your messages whenever I see you.\
1471 (Secure message delivery is enabled, but your nick is not registered\
1472 securely. See `help register'.)"
1476 return "I shan't accept messages for you."
1479 return "I'll forward your messages by email to $mailtoint."
1482 error "bad tellsec $sec"
1487 proc tellme_rel_desc
{v
} {
1488 manyset
$v rel every within
1489 switch -exact $rel {
1491 return "As soon as I've told you, I'll forget the message - note that this means messages can get lost !"
1497 set u
", or talk on channel within [showintervalsecs $within 1] of me having told you"
1500 error "bad tellrel $rel"
1503 return "I'll remind you every [showintervalsecs $every 1] until you say delmsg$u."
1506 def_setting timeformat
{
1507 set tf
[nickdb_get
$n timeformat
]
1508 return "$tf: [timeformat_desc $tf]"
1510 set tf
[string tolower
[ta_word
]]
1512 set desc
[timeformat_desc
$tf]
1513 nickdb_set
$n timeformat
$tf
1517 proc marktime_desc
{mt
} {
1518 if {"$mt" == "off"} {
1519 return "I will not send you periodic messages."
1520 } elseif
{"$mt" == "once"} {
1521 return "I will send you one informational message when I see you."
1523 return "I'll send you a message every [showintervalsecs $mt 0]."
1527 def_setting marktime
{
1528 set mt
[nickdb_get
$n marktime
]
1530 if {[string match
{[0-9]*} $mt]} { append p s
}
1532 append p
[marktime_desc
$mt]
1536 set mt
[string tolower
[ta_word
]]
1539 if {"$mt" == "off" ||
"$mt" == "once"} {
1541 set mt
[parse_interval
$mt $marktime_min]
1543 nickdb_set
$n marktime
$mt
1544 lnick_marktime_start
[irctolower
$n] "So:" 500 0
1545 ucmdr
{} [marktime_desc
$mt]
1548 def_setting security
{
1549 set s
[nickdb_get
$n username
]
1550 if {[string length
$s]} {
1551 return "Your nick, $n, is controlled by the user $s."
1553 return "Your nick, $n, is not secure."
1557 proc tellme_setting_sec_simple
{} {
1565 proc tellme_setting_neednomsgs
{} {
1567 if {[llength [msgsdb_get
$n inbound
]]} {
1568 usererror
"You must delete the messages you have, first."
1573 def_setting tellme
{
1574 set secv
[nickdb_get
$n tellsec
]
1575 set ms
[tellme_sec_desc
$secv $n]
1577 switch -exact $sec {
1579 set mr
[tellme_rel_desc
[nickdb_get
$n tellrel
]]
1587 set setting
[string tolower
[ta_word
]]
1588 switch -exact $setting {
1590 tellme_setting_sec_simple
1593 set every
[ta_interval_optional
60 600]
1596 set v
[list secure
$every]
1599 telling_setting_neednomsgs
1600 telling_setting_sec_simple
1603 telling_setting_neednomsgs
1604 set u
[nickdb_get
$n username
]
1605 if {[string length
$u]} {
1607 "Sorry, you must register securely to have your messages mailed\
1608 (to prevent the use of this feature for spamming). See `help register'."
1611 set v
[list mailto
[ta_word
] $u]
1613 unreliable
- pester
- remind
{
1614 manyset
[nickdb_get
$n tellsec
] sec
1615 switch -exact $sec {
1618 "You can't change your message delivery conditions when\
1619 your message disposition prevents messages from being left."
1624 if {"$setting" != "unreliable"} {
1625 set every
[parse_interval_optional
300 3600]
1628 if {"$setting" == "remind"} {
1629 set within
[ta_interval_optional
5 30]
1630 if {$within > $every} {
1631 error "remind interval must be at least time to respond"
1638 error "invalid tellme setting $setting"
1641 nickdb_set
$n tell$sr $v
1642 ucmdr
[tellme_
${sr
}_desc
$v] {}
1645 proc lnick_checktold
{luser
} {
1646 set ml
[msgsdb_get
$luser outbound
]
1647 if {![llength $ml]} return
1648 set is1
[expr {[llength $ml]==3}]
1649 set m1
"FYI, I haven't yet passed on your"
1651 set now
[clock seconds
]
1652 while {[llength $ml]} {
1654 set ml
[lreplace $ml 0 2]
1655 set td
[expr {$now-$t}]
1657 set iv
[showinterval
$td]
1659 set if1
"message to $r, $iv."
1661 set iv
[showintervalsecs
$td 0]
1662 set ifo
"$r, $n messages, oldest $iv"
1663 set if1
"$n messages to $r, oldest $iv."
1666 sendprivmsg
$luser "$m1 $if1"
1669 lappend ol
" to $ifo[expr {[llength $ml] ? ";" : ".
"}]"
1672 sendprivmsg
$luser "$m1 messages:"
1673 msendprivmsg
$luser $ol
1680 if {![nickdb_exists
$n]} {
1681 ucmdr
{} "You are unknown to me and so have no settings. (Use `register'.)"
1683 if {![ta_anymore
]} {
1685 foreach opt
$settings {
1686 lappend ol
[format "%-10s %s" $opt [set_show
/$opt]]
1688 ucmdr
{} [join $ol "\n"]
1691 if {[catch { info body set_show
/$opt }]} {
1692 error "no setting $opt"
1694 if {![ta_anymore
]} {
1695 ucmdr
{} "$opt: [set_show/$opt]"
1697 nick_securitycheck
0
1698 if {[catch { info body set_set
/$opt }]} {
1699 error "setting $opt cannot be set with `set'"
1706 def_ucmd identpass
{
1707 set username
[ta_word
]
1708 set passmd5
[md5sum
"[ta_word]\n"]
1712 set luser
[irctolower
$n]
1713 upvar #0 nick_onchans($luser) onchans
1714 if {![info exists onchans
] ||
![llength $onchans]} {
1715 ucmdr
"You must be on a channel with me to identify yourself." {}
1717 check_username
$username
1718 exec userv
--timeout 3 $username << "$passmd5\n" > /dev
/null
\
1720 upvar #0 nick_username($luser) rec_username
1721 set rec_username
$username
1722 ucmdr
"Pleased to see you, $username." {}
1723 tell_event
$luser ident
1727 set target
[ta_word
]
1729 check_username
$target
1732 upvar #0 lastsummon($target) ls
1733 set now
[clock seconds
]
1734 if {[info exists ls
]} {
1735 set interval
[expr {$now - $ls}]
1736 if {$interval < 30} {
1738 "Please be patient; $target was summoned only [showinterval $interval]."
1741 regsub {^
[^
!]*!} $p {} path
1743 exec userv
--timeout 3 $target irc-summon
$n $path \
1744 [expr {[ischan
$dest] ?
"$dest" : ""}] \
1747 regsub -all "\n" $rv { / } rv
1750 if {[regexp {^problem
(.
*)} $rv dummy problem
]} {
1751 ucmdr
{} "The user `$target' $problem."
1752 } elseif
{[regexp {^ok
([^
]+) ([0-9]+)$} $rv dummy tty idlesince
]} {
1753 set idletime
[expr {$now - $idlesince}]
1755 ucmdr
{} {} {} "invites $target ($tty[expr {
1756 $idletime > 10 ? ", idle
for [showintervalsecs
$idletime 0]" : ""
1758 [ischan $dest] ? "join us here
" : "talk to you
"
1761 error "unexpected response from userv service: $rv"
1765 proc md5sum
{value
} { exec md5sum
<< $value }
1768 global lastseen nick
1771 set nlower
[irctolower
$ncase]
1773 set now
[clock seconds
]
1774 if {"$nlower" == "[irctolower $nick]"} {
1775 usererror
"I am not self-aware."
1776 } elseif
{![info exists lastseen
($nlower)]} {
1777 set rstr
"I've never seen $ncase."
1779 manyset
$lastseen($nlower) realnick
time what
1780 set howlong
[expr {$now - $time}]
1781 set string [showinterval
$howlong]
1782 set rstr
"I last saw $realnick $string, $what."
1784 if {[ischan
$dest]} {
1789 upvar #0 lookedfor($nlower) lf
1790 if {[info exists lf
]} { set oldvalue
$lf } else { set oldvalue
{} }
1791 set lf
[list [list $now $n $where]]
1792 foreach v
$oldvalue {
1793 if {"[irctolower [lindex $v 1]]" == "[irctolower $n]"} continue
1799 proc lnick_marktime_cancel
{luser
} {
1800 upvar #0 nick_markid($luser) mi
1801 if {![info exists mi
]} return
1802 catch { after cancel
$mi }
1806 proc lnick_marktime_doafter
{luser why ms mentiontold
} {
1807 lnick_marktime_cancel
$luser
1808 upvar #0 nick_markid($luser) mi
1809 set mi
[after $ms [list lnick_marktime_now
$luser $why 0]]
1812 proc lnick_marktime_reset
{luser
} {
1813 set mt
[nickdb_get
$luser marktime
]
1814 if {"$mt" == "off" ||
"$mt" == "once"} return
1815 lnick_marktime_doafter
$luser "Time passes." [expr {$mt*1000}] 0
1818 proc lnick_marktime_start
{luser why ms mentiontold
} {
1819 set mt
[nickdb_get
$luser marktime
]
1820 if {"$mt" == "off"} {
1821 lnick_marktime_cancel
$luser
1822 after $ms [list lnick_checktold
$luser]
1824 lnick_marktime_doafter
$luser $why $ms $mentiontold
1828 proc lnick_marktime_now
{luser why mentiontold
} {
1829 upvar #0 nick_onchans($luser) oc
1831 set calling_nick
$luser
1832 sendprivmsg
$luser [lnick_pingstring
$why $oc ""]
1833 if {$mentiontold} { lnick_checktold
$luser }
1834 lnick_marktime_reset
$luser
1837 proc lnick_pingstring
{why oc apstring
} {
1839 catch { exec uptime
} uptime
1840 set nnicks
[llength [array names nick_onchans
]]
1842 {^
*([0-9:apm
]+) +up.
*, +(\d
+) users?
, +load average
: +([0-9.
, ]+) *$} \
1843 $uptime dummy
time users
load]} {
1844 regsub -all , $load {} load
1845 set uptime
"$time $nnicks/$users $load"
1847 append uptime
", $nnicks nicks"
1849 if {[llength $oc]} {
1853 upvar #0 chan_lastactivity($ch) la
1854 if {![info exists la
]} continue
1855 if {$la <= $best_la} continue
1856 set since
[showintervalsecs
[expr {[clock seconds
]-$la}] 1]
1857 set activity
"$ch $since"
1864 append str
" " $uptime " " $activity
1865 if {[string length
$apstring]} { append str
" " $apstring }
1871 set ln
[irctolower
$n]
1872 if {[ischan
$dest]} {
1873 set oc
[irctolower
$dest]
1876 if {[info exists nick_onchans
($ln)]} {
1877 set oc
$nick_onchans($ln)
1881 if {[llength $oc]} { lnick_marktime_reset
$ln }
1884 ucmdr
{} [lnick_pingstring
"Pong!" $oc $text]
1887 proc ensure_globalsecret
{} {
1890 if {[info exists globalsecret
]} return
1891 set gsfile
[open /dev
/urandom r
]
1892 fconfigure $gsfile -translation binary
1893 set globalsecret
[read $gsfile 32]
1894 binary scan $globalsecret H
* globalsecret
1900 foreach chan
[chandb_list
] {
1901 if {[chandb_get
$chan autojoin
]} { dojoin
$chan }