+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
+ } else {
+ set subcmd [ta_word]
+ }
+ if {[ischan $subcmd]} {
+ set target $subcmd
+ if {![ta_anymore]} {
+ set subcmd show
+ } else {
+ set subcmd [ta_word]
+ }
+ }
+ if {![info exists target]} { error "privately, you must specify a channel" }
+ set procname channel/$subcmd
+ if {"$subcmd" != "show"} {
+ if {[catch { info body $procname }]} { error "unknown channel setting $subcmd" }
+ prefix_nick
+ nick_securitycheck 1
+ if {[chandb_exists $target]} {
+ channel_securitycheck $target $n
+ } else {
+ upvar #0 chan_initialop([irctolower $target]) io
+ upvar #0 nick_unique($n) u
+ if {![info exists io]} { error "$target is not a managed channel" }
+ if {"$io" != "$u"} { error "you are not the interim manager of $target" }
+ if {"$subcmd" != "manager"} { error "use `channel manager' first" }
+ }
+ }
+ channel/$subcmd
+}
+
+def_ucmd who {
+ if {[ta_anymore]} {
+ set target [ta_word]; ta_nomore
+ set myself 1
+ } else {
+ prefix_nick
+ set target $n
+ set myself [expr {"$target" != "$n"}]
+ }
+ upvar #0 nick_case([irctolower $target]) nc
+ set nshow $target
+ if {[info exists nc]} {
+ upvar #0 nick_onchans($nc) oc
+ upvar #0 nick_username($nc) nu
+ if {[info exists oc]} { set nshow $nc }
+ }
+ if {![nickdb_exists $target]} {
+ set ol "$nshow is not a registered nick."
+ } elseif {[string length [set username [nickdb_get $target username]]]} {
+ set ol "The nick $nshow belongs to the user $username."
+ } else {
+ set ol "The nick $nshow is registered (but not to a username)."
+ }
+ if {![info exists nc] || ![info exists oc]} {
+ if {$myself} {
+ append ol "\nI can't see $nshow on anywhere."
+ } else {
+ append ol "\nYou aren't on any channels with me."
+ }
+ } elseif {![info exists nu]} {
+ append ol "\n$nshow has not identified themselves."
+ } elseif {![info exists username]} {
+ append ol "\n$nshow has identified themselves as the user $nu."
+ } elseif {"$nu" != "$username"} {
+ append ol "\nHowever, $nshow is being used by the user $nu."
+ } else {
+ append ol "\n$nshow has identified themselves to me."
+ }
+ ucmdr {} $ol
+}
+