+proc nickdb_get_username {n} {
+ if {![nickdb_exists $n]} { return "" }
+ return [nickdb_get $n username]
+}
+
+proc nickdb_get_sec_effective {n} {
+ set l [nickdb_get $n tellsec]
+ set u [nickdb_get_username $n]
+ if {![string compare [lindex $l 0] "secure"] &&
+ ![string length $u]} { set l insecure }
+ return $l
+}
+
+proc tell_peernicks {text} {
+ global errorInfo errorCode
+ set text [irctolower [string trim $text]]
+ set senders [split $text " "]
+ foreach sender $senders {
+ if {[catch { check_nick $sender } emsg]} {
+ error "invalid nick `$sender': $emsg" $errorInfo $errorCode
+ }
+ }
+ return $senders
+}
+
+proc msgsdb_set_maydelete {n key l otherkey} {
+ msgsdb_set $n $key $l
+ if {[llength $l]} return
+ if {[llength [msgsdb_get $n $otherkey]]} return
+ msgsdb_delete $n
+}
+
+proc tell_delete_msgs {lsenders lrecip} {
+ set new_inbound {}
+ set ndel 0
+ foreach {s t m} [msgsdb_get $lrecip inbound] {
+ if {[llength $lsenders]} {
+ if {[lsearch -exact $lsenders [irctolower $s]] == -1} {
+ lappend new_inbound $s $t $m
+ continue
+ }
+ }
+ set rsenders($s) 1
+ incr ndel
+ }
+ msgsdb_set_maydelete $lrecip inbound $new_inbound outbound
+ if {![llength $new_inbound]} {
+ upvar #0 nick_telling($lrecip) telling
+ catch { unset telling }
+ }
+ foreach s [array names rsenders] {
+ set new_outbound {}
+ foreach {r t c} [msgsdb_get $s outbound] {
+ if {![ircnick_compare $r $lrecip]} continue
+ lappend new_outbound $r $t $c
+ }
+ msgsdb_set_maydelete $s outbound $new_outbound inbound
+ }
+ return $ndel
+}
+
+def_ucmd untell {
+ prefix_nick
+ check_notonchan
+ if {[nickdb_exists $n]} { nick_securitycheck 0 }
+ set recipients [tell_peernicks $text]
+ if {![llength $recipients]} {
+ usererror "You must say which recipients' messages from you to forget."
+ }
+ set ndel 0
+ foreach recip $recipients {
+ incr ndel [tell_delete_msgs [irctolower $n] $recip]
+ }
+ ucmdr "Removed $ndel as yet undelivered message(s)." {}
+}
+
+def_ucmd_alias delmsgs delmsg
+def_ucmd delmsg {
+ global errorInfo errorCode
+ prefix_nick
+ set nl [irctolower $n]
+ check_notonchan
+ manyset [nickdb_get_sec_effective $n] sec secwhen
+ switch -exact $sec {
+ insecure { }
+ reject - mailto {
+ usererror \
+ "There are no messages to delete\
+ because your message disposition prevents them from being left."
+ }
+ secure {
+ nick_securitycheck 1
+ }
+ default {
+ error "delmsg sec $sec"
+ }
+ }
+ if {![llength [msgsdb_get $n inbound]]} {
+ ucmdr "No incoming messages to delete." {}
+ }
+ tell_getcstate
+ if {![info exists u]} {
+ usererror \
+ "I can't delete your messages unless I can see you on a channel with me.\
+ Otherwise I might delete a message I hadn't told you about yet."
+ }
+ if {"$stt" != "passed"} {
+ set telling [list $u undelivered 0]
+ usererror \
+ "There are message(s) you may not yet have seen;\
+ I'll deliver them to you now.\
+ If you actually want to delete them, just tell me `delmsg' again."
+ }
+ set senders [tell_peernicks $text]
+ set ndel [tell_delete_msgs [irctolower $senders] [irctolower $n]]
+ if {!$ndel} {
+ if {[llength $senders]} {
+ ucmdr "No relevant incoming messages to delete." {}
+ }
+ }
+ switch -exact [llength $senders] {
+ 0 { ucmdr {} {} "deletes your $ndel message(s)." }
+ 1 { ucmdr {} {} "deletes your $ndel message(s) from $senders." }
+ default {
+ ucmdr {} {} "deletes your $ndel message(s) from\
+ [lreplace $senders end end] and/or [lindex $senders end]."
+ }
+ }
+}
+
+def_ucmd tellme {
+ prefix_nick
+ ta_nomore
+ check_notonchan
+ manyset [nickdb_get $n tellsec] sec
+ switch -exact $sec {
+ reject { ucmdr "But, you asked me to reject messages for you !" {} }
+ mailto { ucmdr "But, you asked me to mail your messages to you !" {} }
+ }
+ switch -exact [tell_event [irctolower $n] tellme] {
+ ERROR - INVALID { ucmdr {} {is ill. Help!} }
+ nomsgs { ucmdr {You have no messages.} {} }
+ default { }
+ }
+}
+
+def_ucmd tell {
+ global nick_case ownmailaddr ownfullname
+
+ prefix_nick
+ set target [ta_nick]
+ if {![string length $text]} { error "tell them what?" }
+ if {[string length $text] > 400} { error "message too long" }
+
+ set ltarget [irctolower $target]
+ set ctarget $target
+ if {[info exists nick_case($ltarget)]} { set ctarget $nick_case($ltarget) }
+
+ manyset [nickdb_get_sec_effective $target] sec mailtoint mailwhy
+ manyset [nickdb_get $target tellrel] rel relint relwithin
+ switch -exact $sec {
+ insecure - secure {
+ set now [clock seconds]
+ set inbound [msgsdb_get $ltarget inbound]
+ lappend inbound $n $now $text
+ msgsdb_set $ltarget inbound $inbound
+
+ set outbound [msgsdb_get $n outbound]
+ set noutbound {}
+ set found 0
+ foreach {recip time count} $outbound {
+ if {![ircnick_compare $recip $ltarget]} {
+ incr count
+ set recip $ctarget
+ set found 1
+ }
+ lappend noutbound $recip $time $count
+ }
+ if {!$found} {
+ lappend noutbound $ctarget $now 1
+ }
+ msgsdb_set $n outbound $noutbound
+ set msg "OK, I'll tell $ctarget"
+ if {$found} { append msg " that too" }
+ append msg ", "
+ if {"$sec" != "secure"} {
+ switch -exact $rel {
+ unreliable { append msg "neither reliably nor securely" }
+ remind { append msg "pretty reliably, but not securely" }
+ pester { append msg "reliably but not securely" }
+ }
+ } else {
+ switch -exact $rel {
+ unreliable { append msg "securely but not reliably" }
+ remind { append msg "securely and pretty reliably" }
+ pester { append msg "reliably and securely" }
+ }
+ }
+ append msg .
+ tell_event $ltarget msgsarrive
+ ucmdr $msg {}
+ }
+ mailto {
+ set fmtmsg [exec fmt << " $text"]
+ exec /usr/sbin/sendmail -odb -oi -t -oee -f $mailwhy \
+ > /dev/null << \
+ "From: $ownmailaddr ($ownfullname)
+To: $mailtoint
+Subject: IRC tell from $n
+
+$n asked me[expr {[ischan $dest] ? " on $dest" : ""}] to tell you:
+[exec fmt << " $text"]
+
+(This message was for your nick $ctarget; your account $mailwhy
+ arranged for it to be forwarded to $mailtoint.)
+"
+ ucmdr \
+ "I've mailed $ctarget, which is what they prefer." \
+ {}
+ }
+ reject {
+ usererror "Sorry, $ctarget does not want me to take messages."
+ }
+ default {
+ error "bad tellsec $sec"
+ }
+ }
+}
+