+source irccore.tcl
+source parsecmd.tcl
+source stdhelp.tcl
+
+defset marktime_min 300
+defset marktime_join_startdelay 5000
+
+proc privmsg_unlogged {prefix ischan params} {
+ if {!$ischan ||
+ [regexp {^![a-z][-a-z]*[a-z]( .*)?$} [lindex $params 1]]} {
+ return 0
+ }
+ # on-channel message, ignore
+ set chan [lindex $params 0]
+ upvar #0 chan_lastactivity([irctolower $chan]) la
+ set la [clock seconds]
+ catch_logged { recordlastseen_p $prefix "talking on $chan" 2 }
+ return 1
+}
+
+proc showintervalsecs {howlong abbrev} {
+ return [showintervalsecs/[opt timeformat] $howlong $abbrev]
+}
+
+proc showintervalsecs/ks {howlong abbrev} {
+ if {$howlong < 1000} {
+ return "${howlong}s"
+ } else {
+ if {$howlong < 1000000} {
+ set pfx k
+ set scale 1000
+ } else {
+ set pfx M
+ set scale 1000000
+ }
+ set value [expr "$howlong.0 / $scale"]
+ foreach {min format} {100 %.0f 10 %.1f 1 %.2f} {
+ if {$value < $min} continue
+ return [format "$format${pfx}s" $value]
+ }
+ }
+}
+
+proc format_qty {qty unit abbrev} {
+ set o $qty
+ if {$abbrev} {
+ append o [string range $unit 0 0]
+ } else {
+ append o " "
+ append o $unit
+ if {$qty != 1} { append o s }
+ }
+ return $o
+}
+
+proc showintervalsecs/hms {qty abbrev} {
+ set ul {second 60 minute 60 hour 24 day 7 week}
+ set remainv 0
+ while {[llength $ul] > 1 && $qty >= [set uv [lindex $ul 1]]} {
+ set remainu [lindex $ul 0]
+ set remainv [expr {$qty % $uv}]
+ set qty [expr {($qty-$remainv)/$uv}]
+ set ul [lreplace $ul 0 1]
+ }
+ set o [format_qty $qty [lindex $ul 0] $abbrev]
+ if {$remainv} {
+ if {!$abbrev} { append o " " }
+ append o [format_qty $remainv $remainu $abbrev]
+ }
+ return $o
+}
+
+proc showinterval {howlong} {
+ if {$howlong <= 0} {
+ return {just now}
+ } else {
+ return "[showintervalsecs $howlong 0] ago"
+ }
+}
+
+proc showtime {when} {
+ return [showinterval [expr {[clock seconds] - $when}]]
+}
+
+proc parse_interval {specified min} {
+ if {![regexp {^([0-9]+)([a-z]+)$} $specified dummy value unit]} {
+ error "invalid syntax for interval"
+ }
+ switch -exact $unit {
+ s { set u 1 }
+ ks { set u 1000 }
+ m { set u 60 }
+ h { set u 3600 }
+ default { error "unknown unit of time $unit" }
+ }
+ if {$value > 86400*21/$u} { error "interval too large" }
+ set result [expr {$value*$u}]
+ if {$result < $min} { error "interval too small (<${min}s)" }
+ return $result
+}
+
+proc def_msgproc {name argl body} {
+ proc msg_$name "varbase $argl" "\
+ upvar #0 msg/\$varbase/dest d\n\
+ upvar #0 msg/\$varbase/str s\n\
+ upvar #0 msg/\$varbase/accum a\n\
+$body"
+}
+
+def_msgproc begin {dest str} {
+ set d $dest
+ set s $str
+ set a {}
+}
+
+def_msgproc append {str} {
+ set ns "$s$str"
+ if {[string length $s] && [string length $ns] > 65} {
+ msg__sendout $varbase
+ set s " [string trimleft $str]"
+ } else {
+ set s $ns
+ }
+}
+
+def_msgproc finish {} {
+ msg__sendout $varbase
+ unset s
+ unset d
+ return $a
+}
+
+def_msgproc _sendout {} {
+ lappend a [string trimright $s]
+ set s {}
+}
+
+proc looking_whenwhere {when where} {
+ set str [showtime [expr {$when-1}]]
+ if {[string length $where]} { append str " on $where" }
+ return $str
+}
+
+proc tell_event {nl event} {
+ # For `act' we *haven't* yet done the 750ms delay; we implement
+ # that here. Also, here we turn `talk' into `talk' now and `act'
+ # later. We also support the psuedo-event `none'. The del msg
+ # and new msg events are handled by the command procedures, not here.
+ global calling_nick
+ if {[info exists calling_nick]} { set save $calling_nick }
+ switch -exact $event {
+ none { }
+ talk {
+ tell_event_core $nl talk
+ tell_event $nl act
+ }
+ act {
+ after 750 [list tell_event_core $nl $event]
+ }
+ ident - msgsarrive {
+ tell_event_core $nl $event
+ }
+ default {
+ error "tell_event $nl $event"
+ }
+ }
+ if {[info exists save]} { set calling_nick $save }
+}
+
+proc tell_getcstate {} {
+ # uses nl from caller's context
+ # imports telling (as the nick_telling) and u
+ # sets stt, telling_when
+ uplevel 1 {
+ upvar #0 nick_telling($nl) telling
+ upvar #0 nick_unique($nl) u
+
+ if {[info exists telling]} {
+ manyset $telling u_last stt telling_when
+ if {![info exists u] || "$u_last" != "$u"} {
+ unset telling; unset stt; unset telling_when
+ }
+ }
+
+ if {![info exists stt]} {
+ set stt norecord
+ set telling_when $now
+ }
+ }
+}
+
+proc tell_event_core {nl event} {
+ catch_logged { tell_event_core1 $nl $event }
+}
+
+proc tell_event_core1 {nl event} {
+ # event is `talk', `act', `ident' or `msgsarrive'
+ # When user talks we actually get talk now and act later
+ global calling_nick
+ set calling_nick $nl
+ set iml [msgsdb_get $nl inbound]
+ if {![llength $iml]} return
+
+ set now [clock seconds]
+ tell_getcstate
+ set ago [expr {$now - $telling_when}]
+
+ # Now we have the components of a telling state
+ # u - nick_unique (unset if not visible)
+ # stt - state: norecord, mentioned, passede
+ # ago - how long ago since we did anything
+
+ # We compute an evstate to dispatch on as follows:
+
+ # evstate is string of letters
+ # current state
+ # n NORECORD (MESSAGES)
+ # m MENTIONED
+ # p PASSED
+ # event
+ # t talk
+ # a act
+ # i ident
+ # m msgsarrive
+ # security level and timing
+ # ii Insecure
+ # ss Secure and soon (before interval)
+ # sl Secure and late (after interval)
+ # current identification
+ # i Identified
+ # u Unidentified
+ # reliability and timing
+ # uu Unreliable
+ # rv Remind, very soon (before within-interval)
+ # rs Remind, soon (between)
+ # rl Remind, late (after every-interval)
+ # ps Pester, soon (before interval)
+ # pl Pester, late (after interval)
+
+ set evstate {}
+
+ append evstate [string range $stt 0 0]
+ append evstate [string range $event 0 0]
+
+ manyset [nickdb_get_sec_effective $nl] sec secwhen
+ switch -exact $sec {
+ insecure { append evstate ii }
+ secure { append evstate [expr {$ago<$secwhen ? "sl" : "ss"}] }
+ default { append evstate "#$sec#" }
+ }
+
+ upvar #0 nick_username($nl) nu
+ if {[info exists nu] && "$nu" == "[nickdb_get_username $nl]"} {
+ append evstate i
+ } else {
+ append evstate u
+ }
+
+ manyset [nickdb_get $nl tellrel] rel relint relwithin
+ switch -exact $rel {
+ unreliable { append evstate uu }
+ remind { append evstate [expr {
+ $ago<$relwithin ? "rv" : $ago<$relint ? "rs" : "rl"
+ }]}
+ pester { append evstate [expr {$ago<$relint ? "ps" : "pl"}] }
+ default { append evstate "#$rel#" }
+ }
+
+ log "tell_event_core1 core evstate=$evstate (ago=$ago)"
+ switch -glob $evstate {
+ pt???rv {
+ # consider delivered:
+ # (very recently passed, and the user talks)
+ tell_delete_msgs {} $nl
+ return
+ }
+ pm????? {
+ # oops, messages passed are now out of date
+ catch_restoreei { unset telling }
+ return
+ }
+ ?m????? {
+ # ignore new msgs if we haven't passed yet
+ return
+ }
+ nt????? - mt????? -
+ pt???uu - pt???rs - pt???rl - pt???p? {
+ # ignore (any other `talk's) - act handles these
+ return
+ }
+ ni????? - naii??? - nas?i?? - mi????? - pa????l {
+ # pass and then stuff
+ if {[llength $iml] == 3} {
+ manyset $iml sender sentwhen msg
+ sendprivmsg $nl \
+ "$sender asked me [showinterval [expr {$now-$sentwhen}]]\
+ to tell you: $msg"
+ } else {
+ sendprivmsg $nl \
+ "Here are the [expr {[llength $iml]/3}] messages there are for you:"
+ while {[llength $iml] >= 3} {
+ manyset [lrange $iml 0 2] sender sentwhen msg
+ set iml [lrange $iml 3 end]
+ sendprivmsg $nl \
+ " [showintervalsecs [expr {$now-$sentwhen}] 1] <$sender> $msg"
+ }
+ }
+ if {"$rel" == "unreliable"} {
+ tell_delete_msgs {} $nl
+ return
+ }
+ set stt passed
+ }
+ nas?u?? {
+ sendprivmsg $nl {You have messages (so identify yourself please).}
+ set stt mentioned
+ }
+ masl??? {
+ sendprivmsg $nl {Don't forget about your messages.}
+ }
+ pi????? {
+ return
+ }
+ mass??? - pa????v - pa????s {
+ # too soon
+ return
+ }
+ * {
+ error "tell_event_core nl=$nl evstate=$evstate ?"
+ }
+ }
+ if {![info exists u]} {
+ catch_restoreei { unset telling }
+ } else {
+ set telling [list $u $stt $now]
+ }
+}
+
+proc recordlastseen_n {n how here} {
+ # here is:
+ # 0 - nick was seen leaving (or changing to another nicks or some such)
+ # 1 - nick was seen doing something else
+ # 2 - nick was seen talking on channel
+ global lastseen lookedfor
+ set nl [irctolower $n]
+ set now [clock seconds]
+ set lastseen($nl) [list $n $now $how]
+
+ if {!$here} return
+
+ tell_event $nl [lindex {none act talk} $here]
+
+ upvar #0 lookedfor($nl) lf
+ if {[info exists lf]} {
+ switch -exact [llength $lf] {
+ 0 {
+ set ml {}
+ }
+ 1 {
+ manyset [lindex $lf 0] when who where
+ set ml [list \
+ "FYI, $who was looking for you [looking_whenwhere $when $where]."]
+ }
+ default {
+ msg_begin tosend $n "FYI, people have been looking for you:"
+ set i 0
+ set fin ""
+ foreach e $lf {
+ incr i
+ if {$i == 1} {
+ msg_append tosend " "
+ } elseif {$i == [llength $lf]} {
+ msg_append tosend " and "
+ set fin .
+ } else {
+ msg_append tosend ", "
+ }
+ manyset $e when who where
+ msg_append tosend \
+ "$who ([looking_whenwhere $when $where])$fin"
+ }
+ set ml [msg_finish tosend]
+ }
+ }
+ unset lf
+ msendprivmsg_delayed 1000 $n $ml
+ }
+}
+
+proc note_topic {showoff whoby topic} {
+ set msg "FYI, $whoby has changed the topic on $showoff"
+ if {[string length $topic] < 160} {
+ append msg " to $topic"
+ } else {
+ append msg " but it is too long to reproduce here !"
+ }
+ set showoff [irctolower $showoff]
+ set tell [chandb_get $showoff topictell]
+ if {[lsearch -exact $tell *] >= 0} {
+ set tryspies [chandb_list]
+ } else {
+ set tryspies $tell
+ }
+ foreach spy $tryspies {
+ set see [chandb_get $spy topicsee]
+ if {[lsearch -exact $see $showoff] >= 0 || \
+ ([lsearch -exact $see *] >= 0 && \
+ [lsearch -exact $tell $spy] >= 0)} {
+ sendprivmsg $spy $msg
+ }
+ }
+}
+
+proc recordlastseen_p {p how here} {
+ prefix_nick
+ recordlastseen_n $n $how $here
+}
+
+proc chanmode_arg {} {
+ upvar 2 args cm_args
+ set rv [lindex $cm_args 0]
+ set cm_args [lreplace cm_args 0 0]
+ return $rv
+}
+
+proc chanmode_o1 {m g p chan} {
+ global nick chan_initialop
+ prefix_nick
+ set who [chanmode_arg]
+ recordlastseen_n $n "being nice to $who" 1
+ if {"[irctolower $who]" == "[irctolower $nick]"} {
+ set nlower [irctolower $n]
+ upvar #0 nick_unique($nlower) u
+ if {[chandb_exists $chan]} {
+ sendprivmsg $n Thanks.
+ } elseif {![info exists u]} {
+ sendprivmsg $n {Op me while not on the channel, why don't you ?}
+ } else {
+ set chan_initialop([irctolower $chan]) $u
+ sendprivmsg $n \
+ "Thanks. You can use `channel manager ...' to register this channel."
+ if {![string length [nickdb_get_username $n username]]} {
+ sendprivmsg $n \
+ "(But to do that you must register your nick securely first.)"