We need to be able to get at ChanPrivs easily.

This commit is contained in:
Alex Bramley 2011-10-27 17:03:01 +01:00
parent 2603e0984c
commit 47dd5b3430
4 changed files with 28 additions and 28 deletions

View File

@ -102,14 +102,14 @@ func NewChannel(name string, l logging.Logger) *Channel {
} }
// Returns true if the Nick is associated with the Channel // Returns true if the Nick is associated with the Channel
func (ch *Channel) IsOn(nk *Nick) bool { func (ch *Channel) IsOn(nk *Nick) (*ChanPrivs, bool) {
_, ok := ch.nicks[nk] cp, ok := ch.nicks[nk]
return ok return cp, ok
} }
func (ch *Channel) IsOnStr(n string) bool { func (ch *Channel) IsOnStr(n string) (*Nick, bool) {
_, ok := ch.lookup[n] nk, ok := ch.lookup[n]
return ok return nk, ok
} }
// Associates a Nick with a Channel // Associates a Nick with a Channel

View File

@ -55,14 +55,14 @@ func NewNick(n string, l logging.Logger) *Nick {
} }
// Returns true if the Nick is associated with the Channel. // Returns true if the Nick is associated with the Channel.
func (nk *Nick) IsOn(ch *Channel) bool { func (nk *Nick) IsOn(ch *Channel) (*ChanPrivs, bool) {
_, ok := nk.chans[ch] cp, ok := nk.chans[ch]
return ok return cp, ok
} }
func (nk *Nick) IsOnStr(c string) bool { func (nk *Nick) IsOnStr(c string) (*Channel, bool) {
_, ok := nk.lookup[c] ch, ok := nk.lookup[c]
return ok return ch, ok
} }
// Associates a Channel with a Nick. // Associates a Channel with a Nick.

View File

@ -18,8 +18,8 @@ type StateTracker interface {
// Information about ME! // Information about ME!
Me() *Nick Me() *Nick
// And the tracking operations // And the tracking operations
IsOn(channel, nick string) bool IsOn(channel, nick string) (*ChanPrivs, bool)
Associate(channel *Channel, nick *Nick) Associate(channel *Channel, nick *Nick) *ChanPrivs
Dissociate(channel *Channel, nick *Nick) 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 // Returns true if both the channel c and the nick n are tracked
// and the nick is associated with the channel. // 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) nk := st.GetNick(n)
ch := st.GetChannel(c) ch := st.GetChannel(c)
if nk != nil && ch != nil { if nk != nil && ch != nil {
return nk.IsOn(ch) return nk.IsOn(ch)
} }
return false return nil, false
} }
// Associates an already known nick with an already known channel. // 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 { if ch == nil || nk == nil {
st.l.Error("StateTracker.Associate(): passed nil values :-(") 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.", st.l.Warn("StateTracker.Associate(): %s already on %s.",
nk.Nick, ch.Name) nk.Nick, ch.Name)
return return nil
} }
cp := new(ChanPrivs) cp := new(ChanPrivs)
ch.addNick(nk, cp) ch.addNick(nk, cp)
nk.addChannel(ch, cp) nk.addChannel(ch, cp)
return cp
} }
// Dissociates an already known nick from an already known channel. // Dissociates an already known nick from an already known channel.
// Does some tidying up to stop tracking nicks we're no longer on // Does some tidying up to stop tracking nicks we're no longer on
// any common channels with, and channels we're no longer on. // any common channels with, and channels we're no longer on.
func (st *stateTracker) Dissociate(ch *Channel, nk *Nick) { func (st *stateTracker) Dissociate(ch *Channel, nk *Nick) {
switch { if ch == nil || nk == nil {
case ch == nil || nk == nil:
st.l.Error("StateTracker.Dissociate(): passed nil values :-(") 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.", st.l.Warn("StateTracker.Dissociate(): %s not on %s.",
nk.Nick, ch.Name) 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. // I'm leaving the channel for some reason, so it won't be tracked.
st.delChannel(ch) st.delChannel(ch)
default: } else {
// Remove the nick from the channel and the channel from the nick. // Remove the nick from the channel and the channel from the nick.
ch.delNick(nk) ch.delNick(nk)
nk.delChannel(ch) nk.delChannel(ch)

View File

@ -346,13 +346,13 @@ func TestSTIsOn(t *testing.T) {
chan1 := NewChannel("#test1", l) chan1 := NewChannel("#test1", l)
st.chans["#test1"] = chan1 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)") t.Errorf("test1 is not on #test1 (yet)")
} }
cp := new(ChanPrivs) cp := new(ChanPrivs)
chan1.addNick(nick1, cp) chan1.addNick(nick1, cp)
nick1.addChannel(chan1, 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)") t.Errorf("test1 is on #test1 (now)")
} }
m.CheckNothingWritten(t) m.CheckNothingWritten(t)
@ -368,7 +368,7 @@ func TestSTAssociate(t *testing.T) {
st.Associate(chan1, nick1) st.Associate(chan1, nick1)
m.CheckNothingWritten(t) m.CheckNothingWritten(t)
if !st.IsOn("#test1", "test1") { if _, ok := st.IsOn("#test1", "test1"); !ok {
t.Errorf("test1 was not associated with #test1.") t.Errorf("test1 was not associated with #test1.")
} }