From bffe946388532115d5f480ecaf9296d303043a6e Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Wed, 31 Dec 2014 13:17:11 +0000 Subject: [PATCH] The great state tracker privatisation 1/3: channels. --- state/channel.go | 163 +++++++++++++++++++++++++----------------- state/channel_test.go | 66 +++++++++++------ 2 files changed, 141 insertions(+), 88 deletions(-) diff --git a/state/channel.go b/state/channel.go index 9bc1e5f..35379d2 100644 --- a/state/channel.go +++ b/state/channel.go @@ -3,18 +3,24 @@ package state import ( "github.com/fluffle/goirc/logging" - "fmt" "reflect" - "sort" "strconv" ) -// A struct representing an IRC channel +// A Channel is returned from the state tracker and contains +// a copy of the channel state at a particular time. type Channel struct { Name, Topic string Modes *ChanMode - lookup map[string]*Nick - nicks map[*Nick]*ChanPrivs + Nicks map[string]*ChanPrivs +} + +// Internal bookkeeping struct for channels. +type channel struct { + name, topic string + modes *ChanMode + lookup map[string]*nick + nicks map[*nick]*ChanPrivs } // A struct representing the modes of an IRC Channel @@ -98,48 +104,57 @@ func init() { * Channel methods for state management \******************************************************************************/ -func NewChannel(name string) *Channel { - return &Channel{ - Name: name, - Modes: new(ChanMode), - nicks: make(map[*Nick]*ChanPrivs), - lookup: make(map[string]*Nick), +func newChannel(name string) *channel { + return &channel{ + name: name, + modes: new(ChanMode), + nicks: make(map[*nick]*ChanPrivs), + lookup: make(map[string]*nick), } } -// Returns true if the Nick is associated with the Channel -func (ch *Channel) IsOn(nk *Nick) (*ChanPrivs, bool) { - cp, ok := ch.nicks[nk] - return cp, ok +// Returns a copy of the internal tracker channel state at this time. +// Relies on tracker-level locking for concurrent access. +func (ch *channel) Channel() *Channel { + c := &Channel{ + Name: ch.name, + Topic: ch.topic, + Modes: ch.modes.Copy(), + Nicks: make(map[string]*ChanPrivs), + } + for n, cp := range ch.nicks { + c.Nicks[n.nick] = cp.Copy() + } + return c } -func (ch *Channel) IsOnStr(n string) (*Nick, bool) { - nk, ok := ch.lookup[n] - return nk, ok +func (ch *channel) isOn(nk *nick) (*ChanPrivs, bool) { + cp, ok := ch.nicks[nk] + return cp.Copy(), ok } // Associates a Nick with a Channel -func (ch *Channel) addNick(nk *Nick, cp *ChanPrivs) { +func (ch *channel) addNick(nk *nick, cp *ChanPrivs) { if _, ok := ch.nicks[nk]; !ok { ch.nicks[nk] = cp - ch.lookup[nk.Nick] = nk + ch.lookup[nk.nick] = nk } else { - logging.Warn("Channel.addNick(): %s already on %s.", nk.Nick, ch.Name) + logging.Warn("Channel.addNick(): %s already on %s.", nk.nick, ch.name) } } // Disassociates a Nick from a Channel. -func (ch *Channel) delNick(nk *Nick) { +func (ch *channel) delNick(nk *nick) { if _, ok := ch.nicks[nk]; ok { delete(ch.nicks, nk) - delete(ch.lookup, nk.Nick) + delete(ch.lookup, nk.nick) } else { - logging.Warn("Channel.delNick(): %s not on %s.", nk.Nick, ch.Name) + logging.Warn("Channel.delNick(): %s not on %s.", nk.nick, ch.name) } } // Parses mode strings for a channel. -func (ch *Channel) ParseModes(modes string, modeargs ...string) { +func (ch *channel) parseModes(modes string, modeargs ...string) { var modeop bool // true => add mode, false => remove mode var modestr string for i := 0; i < len(modes); i++ { @@ -151,43 +166,43 @@ func (ch *Channel) ParseModes(modes string, modeargs ...string) { modeop = false modestr = string(m) case 'i': - ch.Modes.InviteOnly = modeop + ch.modes.InviteOnly = modeop case 'm': - ch.Modes.Moderated = modeop + ch.modes.Moderated = modeop case 'n': - ch.Modes.NoExternalMsg = modeop + ch.modes.NoExternalMsg = modeop case 'p': - ch.Modes.Private = modeop + ch.modes.Private = modeop case 'r': - ch.Modes.Registered = modeop + ch.modes.Registered = modeop case 's': - ch.Modes.Secret = modeop + ch.modes.Secret = modeop case 't': - ch.Modes.ProtectedTopic = modeop + ch.modes.ProtectedTopic = modeop case 'z': - ch.Modes.SSLOnly = modeop + ch.modes.SSLOnly = modeop case 'Z': - ch.Modes.AllSSL = modeop + ch.modes.AllSSL = modeop case 'O': - ch.Modes.OperOnly = modeop + ch.modes.OperOnly = modeop case 'k': if modeop && len(modeargs) != 0 { - ch.Modes.Key, modeargs = modeargs[0], modeargs[1:] + ch.modes.Key, modeargs = modeargs[0], modeargs[1:] } else if !modeop { - ch.Modes.Key = "" + ch.modes.Key = "" } else { logging.Warn("Channel.ParseModes(): not enough arguments to "+ - "process MODE %s %s%c", ch.Name, modestr, m) + "process MODE %s %s%c", ch.name, modestr, m) } case 'l': if modeop && len(modeargs) != 0 { - ch.Modes.Limit, _ = strconv.Atoi(modeargs[0]) + ch.modes.Limit, _ = strconv.Atoi(modeargs[0]) modeargs = modeargs[1:] } else if !modeop { - ch.Modes.Limit = 0 + ch.modes.Limit = 0 } else { logging.Warn("Channel.ParseModes(): not enough arguments to "+ - "process MODE %s %s%c", ch.Name, modestr, m) + "process MODE %s %s%c", ch.name, modestr, m) } case 'q', 'a', 'o', 'h', 'v': if len(modeargs) != 0 { @@ -208,11 +223,11 @@ func (ch *Channel) ParseModes(modes string, modeargs ...string) { modeargs = modeargs[1:] } else { logging.Warn("Channel.ParseModes(): untracked nick %s "+ - "received MODE on channel %s", modeargs[0], ch.Name) + "received MODE on channel %s", modeargs[0], ch.name) } } else { logging.Warn("Channel.ParseModes(): not enough arguments to "+ - "process MODE %s %s%c", ch.Name, modestr, m) + "process MODE %s %s%c", ch.name, modestr, m) } default: logging.Info("Channel.ParseModes(): unknown mode char %c", m) @@ -220,29 +235,39 @@ func (ch *Channel) ParseModes(modes string, modeargs ...string) { } } -type byNick []*Nick - -func (b byNick) Len() int { return len(b) } -func (b byNick) Less(i, j int) bool { return b[i].Nick < b[j].Nick } -func (b byNick) Swap(i, j int) { b[i], b[j] = b[j], b[i] } - -// Nicks returns a list of *Nick that are on the channel, sorted by nick. -func (ch *Channel) Nicks() []*Nick { - nicks := make([]*Nick, 0, len(ch.lookup)) - for _, nick := range ch.lookup { - nicks = append(nicks, nick) - } - sort.Sort(byNick(nicks)) - return nicks +// Returns true if the Nick is associated with the Channel +func (ch *Channel) IsOn(nk string) (*ChanPrivs, bool) { + cp, ok := ch.Nicks[nk] + return cp, ok } -// NicksStr returns a list of nick strings that are on the channel, sorted by nick. -func (ch *Channel) NicksStr() []string { - var nicks []string - for _, nick := range ch.Nicks() { - nicks = append(nicks, nick.Nick) - } - return nicks +// Test Channel equality. +func (ch *Channel) Equals(other *Channel) bool { + return reflect.DeepEqual(ch, other) +} + +// Duplicates a ChanMode struct. +func (cm *ChanMode) Copy() *ChanMode { + if cm == nil { return nil } + c := *cm + return &c +} + +// Test ChanMode equality. +func (cm *ChanMode) Equals(other *ChanMode) bool { + return reflect.DeepEqual(cm, other) +} + +// Duplicates a ChanPrivs struct. +func (cp *ChanPrivs) Copy() *ChanPrivs { + if cp == nil { return nil } + c := *cp + return &c +} + +// Test ChanPrivs equality. +func (cp *ChanPrivs) Equals(other *ChanPrivs) bool { + return reflect.DeepEqual(cp, other) } // Returns a string representing the channel. Looks like: @@ -257,12 +282,16 @@ func (ch *Channel) String() string { str += "Topic: " + ch.Topic + "\n\t" str += "Modes: " + ch.Modes.String() + "\n\t" str += "Nicks: \n" - for nk, cp := range ch.nicks { - str += "\t\t" + nk.Nick + ": " + cp.String() + "\n" + for nk, cp := range ch.Nicks { + str += "\t\t" + nk + ": " + cp.String() + "\n" } return str } +func (ch *channel) String() string { + return ch.Channel().String() +} + // Returns a string representing the channel modes. Looks like: // +npk key func (cm *ChanMode) String() string { @@ -284,7 +313,7 @@ func (cm *ChanMode) String() string { case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: if f.Int() != 0 { str += ChanModeToString[t.Field(i).Name] - a = append(a, fmt.Sprintf("%d", f.Int())) + a = append(a, strconv.FormatInt(f.Int(), 10)) } } } diff --git a/state/channel_test.go b/state/channel_test.go index f714044..d5c804d 100644 --- a/state/channel_test.go +++ b/state/channel_test.go @@ -1,23 +1,35 @@ package state -import ( - "testing" -) +import "testing" + +func compareChannel(t *testing.T, ch *channel) { + c := ch.Channel() + if c.Name != ch.name || c.Topic != ch.topic || + !c.Modes.Equals(ch.modes) || len(c.Nicks) != len(ch.nicks) { + t.Errorf("Channel not duped correctly from internal state.") + } + for nk, cp := range ch.nicks { + if other, ok := c.Nicks[nk.nick]; !ok || !cp.Equals(other) { + t.Errorf("Nick not duped correctly from internal state.") + } + } +} func TestNewChannel(t *testing.T) { - ch := NewChannel("#test1") + ch := newChannel("#test1") - if ch.Name != "#test1" { + if ch.name != "#test1" { t.Errorf("Channel not created correctly by NewChannel()") } if len(ch.nicks) != 0 || len(ch.lookup) != 0 { t.Errorf("Channel maps contain data after NewChannel()") } + compareChannel(t, ch) } func TestAddNick(t *testing.T) { - ch := NewChannel("#test1") - nk := NewNick("test1") + ch := newChannel("#test1") + nk := newNick("test1") cp := new(ChanPrivs) ch.addNick(nk, cp) @@ -31,11 +43,12 @@ func TestAddNick(t *testing.T) { if n, ok := ch.lookup["test1"]; !ok || n != nk { t.Errorf("Nick test1 not properly stored in lookup map.") } + compareChannel(t, ch) } func TestDelNick(t *testing.T) { - ch := NewChannel("#test1") - nk := NewNick("test1") + ch := newChannel("#test1") + nk := newNick("test1") cp := new(ChanPrivs) ch.addNick(nk, cp) @@ -49,18 +62,20 @@ func TestDelNick(t *testing.T) { if n, ok := ch.lookup["#test1"]; ok || n != nil { t.Errorf("Nick test1 not properly removed from lookup map.") } + compareChannel(t, ch) } func TestChannelParseModes(t *testing.T) { - ch := NewChannel("#test1") - md := ch.Modes + ch := newChannel("#test1") + md := ch.modes // Channel modes can adjust channel privs too, so we need a Nick - nk := NewNick("test1") + nk := newNick("test1") cp := new(ChanPrivs) ch.addNick(nk, cp) // Test bools first. + compareChannel(t, ch) if md.Private || md.Secret || md.ProtectedTopic || md.NoExternalMsg || md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly { t.Errorf("Modes for new channel set to true.") @@ -72,8 +87,9 @@ func TestChannelParseModes(t *testing.T) { md.InviteOnly = true // Flip some MOAR bits. - ch.ParseModes("+s-p+tm-i") + ch.parseModes("+s-p+tm-i") + compareChannel(t, ch) if md.Private || !md.Secret || !md.ProtectedTopic || !md.NoExternalMsg || !md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly { t.Errorf("Modes not flipped correctly by ParseModes.") @@ -85,19 +101,22 @@ func TestChannelParseModes(t *testing.T) { } // enable limit correctly - ch.ParseModes("+l", "256") + ch.parseModes("+l", "256") + compareChannel(t, ch) if md.Limit != 256 { t.Errorf("Limit for channel not set correctly") } // enable limit incorrectly - ch.ParseModes("+l") + ch.parseModes("+l") + compareChannel(t, ch) if md.Limit != 256 { t.Errorf("Bad limit value caused limit to be unset.") } // disable limit correctly - ch.ParseModes("-l") + ch.parseModes("-l") + compareChannel(t, ch) if md.Limit != 0 { t.Errorf("Limit for channel not unset correctly") } @@ -108,19 +127,22 @@ func TestChannelParseModes(t *testing.T) { } // enable key correctly - ch.ParseModes("+k", "foobar") + ch.parseModes("+k", "foobar") + compareChannel(t, ch) if md.Key != "foobar" { t.Errorf("Key for channel not set correctly") } // enable key incorrectly - ch.ParseModes("+k") + ch.parseModes("+k") + compareChannel(t, ch) if md.Key != "foobar" { t.Errorf("Bad key value caused key to be unset.") } // disable key correctly - ch.ParseModes("-k") + ch.parseModes("-k") + compareChannel(t, ch) if md.Key != "" { t.Errorf("Key for channel not unset correctly") } @@ -128,16 +150,18 @@ func TestChannelParseModes(t *testing.T) { // Test chan privs parsing. cp.Op = true cp.HalfOp = true - ch.ParseModes("+aq-o", "test1", "test1", "test1") + ch.parseModes("+aq-o", "test1", "test1", "test1") + compareChannel(t, ch) if !cp.Owner || !cp.Admin || cp.Op || !cp.HalfOp || cp.Voice { t.Errorf("Channel privileges not flipped correctly by ParseModes.") } // Test a random mix of modes, just to be sure md.Limit = 256 - ch.ParseModes("+zpt-qsl+kv-h", "test1", "foobar", "test1") + ch.parseModes("+zpt-qsl+kv-h", "test1", "foobar", "test1") + compareChannel(t, ch) if !md.Private || md.Secret || !md.ProtectedTopic || !md.NoExternalMsg || !md.Moderated || md.InviteOnly || md.OperOnly || !md.SSLOnly { t.Errorf("Modes not flipped correctly by ParseModes (2).")