+ if {[ischan $dest]} {
+ recordlastseen_n $n "invoking me in $dest" 1
+ set output $dest
+ } else {
+ recordlastseen_n $n "talking to me" 1
+ set output $n
+ }
+ nick_case $n
+
+ if {[catch {
+ regsub {^! *} $text {} text
+ set ucmd [ta_word]
+ set procname ucmd/[string tolower $ucmd]
+ if {[catch { info body $procname }]} {
+ error "unknown command; try help for help"
+ }
+ $procname $p $dest
+ } rv]} {
+ sendprivmsg $n "error: $rv"
+ } else {
+ manyset $rv priv_msgs pub_msgs priv_acts pub_acts
+ foreach {td val} [list $n $priv_acts $output $pub_acts] {
+ foreach l [split $val "\n"] {
+ sendaction_priority 0 $td $l
+ }
+ }
+ foreach {td val} [list $n $priv_msgs $output $pub_msgs] {
+ foreach l [split $val "\n"] {
+ sendprivmsg $td $l
+ }
+ }
+ }
+}
+
+proc msg_INVITE {p c n chan} {
+ after 1000 [list sendout JOIN $chan]
+}
+
+proc grep {var predicate list} {
+ set o {}
+ upvar 1 $var v
+ foreach v $list {
+ if {[uplevel 1 [list expr $predicate]]} { lappend o $v }
+ }
+ return $o
+}
+
+proc msg_353 {p c dest type chan nicklist} {
+ global names_chans nick_onchans
+ if {![info exists names_chans]} { set names_chans {} }
+ set chan [irctolower $chan]
+ lappend names_chans $chan
+ channel_noone_seen $chan
+ foreach n [split $nicklist { }] {
+ regsub {^[@+]} $n {} n
+ if {![string length $n]} continue
+ check_nick $n
+ upvar #0 nick_onchans($n) oc
+ lappend oc $chan
+ nick_ishere $n
+ }
+}
+
+proc msg_366 {p c args} {
+ global names_chans nick_onchans
+ if {[llength names_chans] > 1} {
+ foreach n [array names nick_onchans] {
+ upvar #0 nick_onchans($n) oc
+ set oc [grep tc {[lsearch -exact $tc $names_chans] >= 0} $oc]
+ if {![llength $oc]} { nick_forget $n }
+ }
+ }
+ unset names_chans
+}
+
+proc ta_anymore {} {
+ upvar 1 text text
+ return [expr {!![string length $text]}]
+}
+
+proc ta_nomore {} {
+ upvar 1 text text
+ if {[string length $text]} { error "too many parameters" }
+}
+
+proc ta_word {} {
+ upvar 1 text text
+ if {![regexp {^([^ ]+) *(.*)} $text dummy firstword text]} {
+ error "too few parameters"
+ }
+ return $firstword
+}
+
+proc ta_nick {} {
+ upvar 1 text text
+ set v [ta_word]
+ check_nick $v
+ return $v
+}
+
+proc def_ucmd {cmdname body} {
+ proc ucmd/$cmdname {p dest} " upvar 1 text text\n$body"
+}
+
+proc ucmdr {priv pub args} {
+ return -code return [concat [list $priv $pub] $args]
+}
+
+proc loadhelp {} {
+ global help_topics
+
+ catch { unset help_topics }
+ set f [open helpinfos r]
+ try_except_finally {
+ set lno 0
+ while {[gets $f l] >= 0} {
+ incr lno
+ if {[regexp {^#.*} $l]} {
+ } elseif {[regexp {^ *$} $l]} {
+ if {[info exists topic]} {
+ set help_topics($topic) [join $lines "\n"]
+ unset topic
+ unset lines
+ }
+ } elseif {[regexp {^!([-+._0-9a-z]*)$} $l dummy newtopic]} {
+ if {[info exists topic]} {
+ error "help $newtopic while in $topic"
+ }
+ set topic $newtopic
+ set lines {}
+ } elseif {[regexp {^[^!#]} $l]} {
+ set topic
+ lappend lines [string trimright $l]
+ } else {
+ error "eh ? $lno: $l"
+ }
+ }
+ if {[info exists topic]} { error "unfinished topic $topic" }
+ } {} {
+ close $f
+ }
+}
+
+def_ucmd help {
+ if {[set lag [out_lagged]]} {
+ if {[ischan $dest]} { set replyto $dest } else { set replyto $n }
+ if {$lag > 1} {
+ sendaction_priority 1 $replyto \
+ "is very lagged. Please ask for help again later."
+ ucmdr {} {}
+ } else {
+ sendaction_priority 1 $replyto \
+ "is lagged. Your help will arrive shortly ..."
+ }
+ }
+
+ upvar #0 help_topics([irctolower [string trim $text]]) info
+ if {![info exists info]} { ucmdr "No help on $text, sorry." {} }
+ ucmdr $info {}
+}
+
+def_ucmd ? {
+ global help_topics
+ ucmdr $help_topics() {}
+}
+
+proc check_username {target} {
+ if {
+ [string length $target] > 8 ||
+ [regexp {[^-0-9a-z]} $target] ||
+ ![regexp {^[a-z]} $target]
+ } { error "invalid username" }
+}
+
+proc somedb__head {} {
+ uplevel 1 {
+ set idl [irctolower $id]
+ upvar #0 ${nickchan}db($idl) ndbe
+ binary scan $idl H* idh
+ set idfn $fprefix$idh
+ if {![info exists iddbe] && [file exists $idfn]} {
+ set f [open $idfn r]
+ try_except_finally { set newval [read $f] } {} { close $f }
+ if {[llength $newval] % 2} { error "invalid length" }
+ set iddbe $newval
+ }
+ }
+}
+
+proc def_somedb {name arglist body} {
+ foreach {nickchan fprefix} {nick users/n chan chans/c} {
+ proc ${nickchan}db_$name $arglist \
+ "set nickchan $nickchan; set fprefix $fprefix; somedb__head; $body"
+ }
+}
+
+def_somedb exists {id} {
+ return [info exists iddbe]
+}
+
+def_somedb delete {id} {
+ catch { unset iddbe }
+ file delete $idfn
+}
+
+set default_settings_nick {timeformat ks}
+set default_settings_chan {autojoin 1}
+
+def_somedb set {id args} {
+ upvar #0 default_settings_$nickchan def
+ if {![info exists iddbe]} { set iddbe $def }
+ foreach {key value} [concat $iddbe $args] { set a($key) $value }
+ set newval {}
+ foreach {key value} [array get a] { lappend newval $key $value }
+ set f [open $idfn.new w]
+ try_except_finally {
+ puts $f $newval
+ close $f
+ file rename -force $idfn.new $idfn
+ } {
+ } {
+ catch { close $f }
+ }
+ set iddbe $newval
+}
+
+def_somedb get {id key} {
+ upvar #0 default_settings_$nickchan def
+ if {[info exists iddbe]} {
+ set l $iddbe
+ } else {
+ set l $def
+ }
+ foreach {tkey value} $l {
+ if {"$tkey" == "$key"} { return $value }
+ }
+ error "unset setting $key"
+}
+
+proc opt {key} {
+ global calling_nick
+ if {[info exists calling_nick]} { set n $calling_nick } { set n {} }
+ return [nickdb_get $n $key]
+}
+
+proc check_notonchan {} {
+ upvar 1 dest dest
+ if {[ischan $dest]} { error "that command must be sent privately" }
+}
+
+proc nick_securitycheck {strict} {
+ upvar 1 n n
+ if {![nickdb_exists $n]} { error "you are unknown to me, use `register'." }
+ set wantu [nickdb_get $n username]
+ if {![string length $wantu]} {
+ if {$strict} {
+ error "that feature is only available to secure users, sorry."
+ } else {
+ return
+ }
+ }
+ upvar #0 nick_username($n) nu
+ if {![info exists nu]} {
+ error "nick $n is secure, you must identify yourself first."
+ }
+ if {"$wantu" != "$nu"} {
+ error "you are the wrong user - the nick $n belongs to $wantu, not $nu"
+ }
+}
+
+proc channel_securitycheck {channel n} {
+ # You must also call `nick_securitycheck 1'
+ set mgrs [chandb_get $channel managers]
+ if {[lsearch -exact [irctolower $mgrs] [irctolower $n]] < 0} {
+ error "you are not a manager of $channel"
+ }
+}
+
+proc def_chancmd {name body} {
+ proc channel/$name {} \
+ " upvar 1 target chan; upvar 1 n n; upvar 1 text text; $body"
+}
+
+def_chancmd manager {
+ set opcode [ta_word]
+ switch -exact _$opcode {
+ _= { set ml {} }
+ _+ - _- {
+ if {[chandb_exists $chan]} {
+ set ml [chandb_get $chan managers]
+ } else {
+ set ml [list [irctolower $n]]
+ }
+ }
+ default {
+ error "`channel manager' opcode must be one of + - ="
+ }
+ }
+ foreach nn [split $text " "] {
+ if {![string length $nn]} continue
+ check_nick $nn
+ set nn [irctolower $nn]
+ if {"$opcode" != "-"} {
+ lappend ml $nn
+ } else {
+ set ml [grep nq {"$nq" != "$nn"} $ml]
+ }
+ }
+ if {[llength $ml]} {
+ chandb_set $chan managers $ml
+ ucmdr "Managers of $chan: $ml" {}
+ } else {
+ chandb_delete $chan
+ ucmdr {} {} "forgets about managing $chan." {}
+ }
+}
+
+def_chancmd autojoin {
+ set yesno [ta_word]
+ switch -exact [string tolower $yesno] {
+ no { set nv 0 }
+ yes { set nv 1 }
+ default { error "channel autojoin must be `yes' or `no' }
+ }
+ chandb_set $chan autojoin $nv
+}
+
+def_chancmd show {
+ if {[chandb_exists $chan]} {
+ set l "Settings for $chan: autojoin "
+ append l [lindex {no yes} [chandb_get $chan autojoin]]
+ append l "\nManagers: "
+ append l [join [chandb_get $chan managers] " "]
+ ucmdr {} $l
+ } else {
+ ucmdr {} "The channel $chan is not managed."
+ }
+}
+
+def_ucmd op {
+ if {[ischan $dest]} { set target $dest }
+ if {[ta_anymore]} { set target [ta_word] }
+ ta_nomore
+ if {![info exists target]} { error "you must specify, or !... on, the channel" }
+ if {![ischan $target]} { error "not a valid channel" }
+ if {![chandb_exists $target]} { error "$target is not a managed channel." }
+ prefix_nick
+ nick_securitycheck 1
+ channel_securitycheck $target $n
+ sendout MODE $target +o $n
+}
+
+def_ucmd channel {
+ if {[ischan $dest]} { set target $dest }
+ if {![ta_anymore]} {
+ set subcmd show