From 9d04b8dc9f168139da39e431b7576074cdb3d8e6 Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Sun, 4 Aug 2024 00:06:28 +0100 Subject: [PATCH] better meta management --- irc.tcl | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/irc.tcl b/irc.tcl index f7e1c6d..c4eb004 100755 --- a/irc.tcl +++ b/irc.tcl @@ -140,38 +140,40 @@ namespace eval ::irc { } } + proc ::irc::int-dictsub args { + if [catch { dict {*}$args } result options] { + return -options options [regsub {dictionary$} $result "channel meta"] + } else { + return -options options $result + } + } + proc ::irc::meta {subcommand chan args} { switch -- $subcommand { exists { - if {[llength $args] != 1} { return -code error "wrong # args: should be \"irc::meta exists chan key\"" } + if ![llength $args] { return -code error "wrong # args: should be \"irc::meta exists chan key ?key ...?\"" } - dict exists [set chan.handlers($chan)] {*}$args + ::irc::int-dictsub exists [set chan.handlers($chan)] {*}$args } - remove { - if {[llength $args] != 1} { return -code error "wrong # args: should be \"irc::meta remove chan key\"" } + unset { + if ![llength $args] { return -code error "wrong # args: should be \"irc::meta unset chan key ?key ...?\"" } - dict remove chan.handlers($chan) {*}$args + ::irc::int-dictsub unset chan.handlers($chan) {*}$args } get { - if {[llength $args] ni {0 1}} { return -code error "wrong # args: should be \"irc::meta get chan ?key?\"" } - - if [catch { dict get [set chan.handlers($chan)] {*}$args } result options] { - return -options options [regsub {dictionary$} $result "channel meta"] - } else { - return -options options $result - } + ::irc::int-dictsub get [set chan.handlers($chan)] {*}$args } set { - if {[llength $args] != 2} { return -code error "wrong # args: should be \"irc::meta set chan key value\"" } + if {[llength $args] < 2} { return -code error "wrong # args: should be \"irc::meta set chan key ?key ...? value\"" } - dict set chan.handlers($chan) {*}$args + ::irc::int-dictsub set chan.handlers($chan) {*}$args } read { if [llength $args] { return -code error "wrong # args: should be \"irc::meta read chan\"" } set chan.handlers($chan) } - default { return -code error "unknown subcommand \"$subcommand\": must be exists, get, remove, or set" } + default { return -code error "unknown subcommand \"$subcommand\": must be exists, get, set, or unset" } } } -- 2.43.4