~aleteoryx/tclircc

682156f9acef1c9f0f8251c7c34567084e6bb281 — Aleteoryx 1 year, 4 months ago 9b25c0a
better meta management
1 files changed, 15 insertions(+), 13 deletions(-)

M irc.tcl
M irc.tcl => irc.tcl +15 -13
@@ 140,31 140,33 @@ 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\"" }

        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\"" }
        if ![llength $args] { return -code error "wrong # args: should be \"irc::meta remove chan key\"" }

        dict remove chan.handlers($chan) {*}$args
        ::irc::int-dictsub remove 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\"" }