From 47dd5b34306a7ad6bc234136e665316ad100e741 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Thu, 27 Oct 2011 17:03:01 +0100 Subject: [PATCH] We need to be able to get at ChanPrivs easily. --- state/channel.go | 12 ++++++------ state/nick.go | 12 ++++++------ state/tracker.go | 26 +++++++++++++------------- state/tracker_test.go | 6 +++--- 4 files changed, 28 insertions(+), 28 deletions(-) diff --git a/state/channel.go b/state/channel.go index 8631189..5b96d14 100644 --- a/state/channel.go +++ b/state/channel.go @@ -102,14 +102,14 @@ func NewChannel(name string, l logging.Logger) *Channel { } // Returns true if the Nick is associated with the Channel -func (ch *Channel) IsOn(nk *Nick) bool { - _, ok := ch.nicks[nk] - return ok +func (ch *Channel) IsOn(nk *Nick) (*ChanPrivs, bool) { + cp, ok := ch.nicks[nk] + return cp, ok } -func (ch *Channel) IsOnStr(n string) bool { - _, ok := ch.lookup[n] - return ok +func (ch *Channel) IsOnStr(n string) (*Nick, bool) { + nk, ok := ch.lookup[n] + return nk, ok } // Associates a Nick with a Channel diff --git a/state/nick.go b/state/nick.go index 64ccf5f..6dc4089 100644 --- a/state/nick.go +++ b/state/nick.go @@ -55,14 +55,14 @@ func NewNick(n string, l logging.Logger) *Nick { } // Returns true if the Nick is associated with the Channel. -func (nk *Nick) IsOn(ch *Channel) bool { - _, ok := nk.chans[ch] - return ok +func (nk *Nick) IsOn(ch *Channel) (*ChanPrivs, bool) { + cp, ok := nk.chans[ch] + return cp, ok } -func (nk *Nick) IsOnStr(c string) bool { - _, ok := nk.lookup[c] - return ok +func (nk *Nick) IsOnStr(c string) (*Channel, bool) { + ch, ok := nk.lookup[c] + return ch, ok } // Associates a Channel with a Nick. diff --git a/state/tracker.go b/state/tracker.go index d1b5fe9..ee02b30 100644 --- a/state/tracker.go +++ b/state/tracker.go @@ -18,8 +18,8 @@ type StateTracker interface { // Information about ME! Me() *Nick // And the tracking operations - IsOn(channel, nick string) bool - Associate(channel *Channel, nick *Nick) + IsOn(channel, nick string) (*ChanPrivs, bool) + Associate(channel *Channel, nick *Nick) *ChanPrivs Dissociate(channel *Channel, nick *Nick) } @@ -172,45 +172,45 @@ func (st *stateTracker) Me() *Nick { // Returns true if both the channel c and the nick n are tracked // and the nick is associated with the channel. -func (st *stateTracker) IsOn(c, n string) bool { +func (st *stateTracker) IsOn(c, n string) (*ChanPrivs, bool) { nk := st.GetNick(n) ch := st.GetChannel(c) if nk != nil && ch != nil { return nk.IsOn(ch) } - return false + return nil, false } // Associates an already known nick with an already known channel. -func (st *stateTracker) Associate(ch *Channel, nk *Nick) { +func (st *stateTracker) Associate(ch *Channel, nk *Nick) *ChanPrivs { if ch == nil || nk == nil { st.l.Error("StateTracker.Associate(): passed nil values :-(") - return + return nil } - if nk.IsOn(ch) { + if _, ok := nk.IsOn(ch); ok { st.l.Warn("StateTracker.Associate(): %s already on %s.", nk.Nick, ch.Name) - return + return nil } cp := new(ChanPrivs) ch.addNick(nk, cp) nk.addChannel(ch, cp) + return cp } // Dissociates an already known nick from an already known channel. // Does some tidying up to stop tracking nicks we're no longer on // any common channels with, and channels we're no longer on. func (st *stateTracker) Dissociate(ch *Channel, nk *Nick) { - switch { - case ch == nil || nk == nil: + if ch == nil || nk == nil { st.l.Error("StateTracker.Dissociate(): passed nil values :-(") - case !nk.IsOn(ch): + } else if _, ok := nk.IsOn(ch); !ok { st.l.Warn("StateTracker.Dissociate(): %s not on %s.", nk.Nick, ch.Name) - case nk == st.me: + } else if nk == st.me { // I'm leaving the channel for some reason, so it won't be tracked. st.delChannel(ch) - default: + } else { // Remove the nick from the channel and the channel from the nick. ch.delNick(nk) nk.delChannel(ch) diff --git a/state/tracker_test.go b/state/tracker_test.go index 818f5d1..1079ee9 100644 --- a/state/tracker_test.go +++ b/state/tracker_test.go @@ -346,13 +346,13 @@ func TestSTIsOn(t *testing.T) { chan1 := NewChannel("#test1", l) st.chans["#test1"] = chan1 - if st.IsOn("#test1", "test1") { + if priv, ok := st.IsOn("#test1", "test1"); ok || priv != nil { t.Errorf("test1 is not on #test1 (yet)") } cp := new(ChanPrivs) chan1.addNick(nick1, cp) nick1.addChannel(chan1, cp) - if !st.IsOn("#test1", "test1") { + if priv, ok := st.IsOn("#test1", "test1"); !ok || priv != cp { t.Errorf("test1 is on #test1 (now)") } m.CheckNothingWritten(t) @@ -368,7 +368,7 @@ func TestSTAssociate(t *testing.T) { st.Associate(chan1, nick1) m.CheckNothingWritten(t) - if !st.IsOn("#test1", "test1") { + if _, ok := st.IsOn("#test1", "test1"); !ok { t.Errorf("test1 was not associated with #test1.") }