The great state tracker privatisation 1/3: channels.

This commit is contained in:
Alex Bramley 2014-12-31 13:17:11 +00:00
parent 8231942086
commit bffe946388
2 changed files with 141 additions and 88 deletions

View File

@ -3,18 +3,24 @@ package state
import ( import (
"github.com/fluffle/goirc/logging" "github.com/fluffle/goirc/logging"
"fmt"
"reflect" "reflect"
"sort"
"strconv" "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 { type Channel struct {
Name, Topic string Name, Topic string
Modes *ChanMode Modes *ChanMode
lookup map[string]*Nick Nicks map[string]*ChanPrivs
nicks map[*Nick]*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 // A struct representing the modes of an IRC Channel
@ -98,48 +104,57 @@ func init() {
* Channel methods for state management * Channel methods for state management
\******************************************************************************/ \******************************************************************************/
func NewChannel(name string) *Channel { func newChannel(name string) *channel {
return &Channel{ return &channel{
Name: name, name: name,
Modes: new(ChanMode), modes: new(ChanMode),
nicks: make(map[*Nick]*ChanPrivs), nicks: make(map[*nick]*ChanPrivs),
lookup: make(map[string]*Nick), lookup: make(map[string]*nick),
} }
} }
// Returns true if the Nick is associated with the Channel // Returns a copy of the internal tracker channel state at this time.
func (ch *Channel) IsOn(nk *Nick) (*ChanPrivs, bool) { // Relies on tracker-level locking for concurrent access.
cp, ok := ch.nicks[nk] func (ch *channel) Channel() *Channel {
return cp, ok 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) { func (ch *channel) isOn(nk *nick) (*ChanPrivs, bool) {
nk, ok := ch.lookup[n] cp, ok := ch.nicks[nk]
return nk, ok return cp.Copy(), ok
} }
// Associates a Nick with a Channel // 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 { if _, ok := ch.nicks[nk]; !ok {
ch.nicks[nk] = cp ch.nicks[nk] = cp
ch.lookup[nk.Nick] = nk ch.lookup[nk.nick] = nk
} else { } 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. // Disassociates a Nick from a Channel.
func (ch *Channel) delNick(nk *Nick) { func (ch *channel) delNick(nk *nick) {
if _, ok := ch.nicks[nk]; ok { if _, ok := ch.nicks[nk]; ok {
delete(ch.nicks, nk) delete(ch.nicks, nk)
delete(ch.lookup, nk.Nick) delete(ch.lookup, nk.nick)
} else { } 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. // 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 modeop bool // true => add mode, false => remove mode
var modestr string var modestr string
for i := 0; i < len(modes); i++ { for i := 0; i < len(modes); i++ {
@ -151,43 +166,43 @@ func (ch *Channel) ParseModes(modes string, modeargs ...string) {
modeop = false modeop = false
modestr = string(m) modestr = string(m)
case 'i': case 'i':
ch.Modes.InviteOnly = modeop ch.modes.InviteOnly = modeop
case 'm': case 'm':
ch.Modes.Moderated = modeop ch.modes.Moderated = modeop
case 'n': case 'n':
ch.Modes.NoExternalMsg = modeop ch.modes.NoExternalMsg = modeop
case 'p': case 'p':
ch.Modes.Private = modeop ch.modes.Private = modeop
case 'r': case 'r':
ch.Modes.Registered = modeop ch.modes.Registered = modeop
case 's': case 's':
ch.Modes.Secret = modeop ch.modes.Secret = modeop
case 't': case 't':
ch.Modes.ProtectedTopic = modeop ch.modes.ProtectedTopic = modeop
case 'z': case 'z':
ch.Modes.SSLOnly = modeop ch.modes.SSLOnly = modeop
case 'Z': case 'Z':
ch.Modes.AllSSL = modeop ch.modes.AllSSL = modeop
case 'O': case 'O':
ch.Modes.OperOnly = modeop ch.modes.OperOnly = modeop
case 'k': case 'k':
if modeop && len(modeargs) != 0 { if modeop && len(modeargs) != 0 {
ch.Modes.Key, modeargs = modeargs[0], modeargs[1:] ch.modes.Key, modeargs = modeargs[0], modeargs[1:]
} else if !modeop { } else if !modeop {
ch.Modes.Key = "" ch.modes.Key = ""
} else { } else {
logging.Warn("Channel.ParseModes(): not enough arguments to "+ 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': case 'l':
if modeop && len(modeargs) != 0 { if modeop && len(modeargs) != 0 {
ch.Modes.Limit, _ = strconv.Atoi(modeargs[0]) ch.modes.Limit, _ = strconv.Atoi(modeargs[0])
modeargs = modeargs[1:] modeargs = modeargs[1:]
} else if !modeop { } else if !modeop {
ch.Modes.Limit = 0 ch.modes.Limit = 0
} else { } else {
logging.Warn("Channel.ParseModes(): not enough arguments to "+ 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': case 'q', 'a', 'o', 'h', 'v':
if len(modeargs) != 0 { if len(modeargs) != 0 {
@ -208,11 +223,11 @@ func (ch *Channel) ParseModes(modes string, modeargs ...string) {
modeargs = modeargs[1:] modeargs = modeargs[1:]
} else { } else {
logging.Warn("Channel.ParseModes(): untracked nick %s "+ 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 { } else {
logging.Warn("Channel.ParseModes(): not enough arguments to "+ 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: default:
logging.Info("Channel.ParseModes(): unknown mode char %c", m) logging.Info("Channel.ParseModes(): unknown mode char %c", m)
@ -220,29 +235,39 @@ func (ch *Channel) ParseModes(modes string, modeargs ...string) {
} }
} }
type byNick []*Nick // Returns true if the Nick is associated with the Channel
func (ch *Channel) IsOn(nk string) (*ChanPrivs, bool) {
func (b byNick) Len() int { return len(b) } cp, ok := ch.Nicks[nk]
func (b byNick) Less(i, j int) bool { return b[i].Nick < b[j].Nick } return cp, ok
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
} }
// NicksStr returns a list of nick strings that are on the channel, sorted by nick. // Test Channel equality.
func (ch *Channel) NicksStr() []string { func (ch *Channel) Equals(other *Channel) bool {
var nicks []string return reflect.DeepEqual(ch, other)
for _, nick := range ch.Nicks() { }
nicks = append(nicks, nick.Nick)
} // Duplicates a ChanMode struct.
return nicks 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: // Returns a string representing the channel. Looks like:
@ -257,12 +282,16 @@ func (ch *Channel) String() string {
str += "Topic: " + ch.Topic + "\n\t" str += "Topic: " + ch.Topic + "\n\t"
str += "Modes: " + ch.Modes.String() + "\n\t" str += "Modes: " + ch.Modes.String() + "\n\t"
str += "Nicks: \n" str += "Nicks: \n"
for nk, cp := range ch.nicks { for nk, cp := range ch.Nicks {
str += "\t\t" + nk.Nick + ": " + cp.String() + "\n" str += "\t\t" + nk + ": " + cp.String() + "\n"
} }
return str return str
} }
func (ch *channel) String() string {
return ch.Channel().String()
}
// Returns a string representing the channel modes. Looks like: // Returns a string representing the channel modes. Looks like:
// +npk key // +npk key
func (cm *ChanMode) String() string { 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: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if f.Int() != 0 { if f.Int() != 0 {
str += ChanModeToString[t.Field(i).Name] str += ChanModeToString[t.Field(i).Name]
a = append(a, fmt.Sprintf("%d", f.Int())) a = append(a, strconv.FormatInt(f.Int(), 10))
} }
} }
} }

View File

@ -1,23 +1,35 @@
package state package state
import ( import "testing"
"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) { 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()") t.Errorf("Channel not created correctly by NewChannel()")
} }
if len(ch.nicks) != 0 || len(ch.lookup) != 0 { if len(ch.nicks) != 0 || len(ch.lookup) != 0 {
t.Errorf("Channel maps contain data after NewChannel()") t.Errorf("Channel maps contain data after NewChannel()")
} }
compareChannel(t, ch)
} }
func TestAddNick(t *testing.T) { func TestAddNick(t *testing.T) {
ch := NewChannel("#test1") ch := newChannel("#test1")
nk := NewNick("test1") nk := newNick("test1")
cp := new(ChanPrivs) cp := new(ChanPrivs)
ch.addNick(nk, cp) ch.addNick(nk, cp)
@ -31,11 +43,12 @@ func TestAddNick(t *testing.T) {
if n, ok := ch.lookup["test1"]; !ok || n != nk { if n, ok := ch.lookup["test1"]; !ok || n != nk {
t.Errorf("Nick test1 not properly stored in lookup map.") t.Errorf("Nick test1 not properly stored in lookup map.")
} }
compareChannel(t, ch)
} }
func TestDelNick(t *testing.T) { func TestDelNick(t *testing.T) {
ch := NewChannel("#test1") ch := newChannel("#test1")
nk := NewNick("test1") nk := newNick("test1")
cp := new(ChanPrivs) cp := new(ChanPrivs)
ch.addNick(nk, cp) ch.addNick(nk, cp)
@ -49,18 +62,20 @@ func TestDelNick(t *testing.T) {
if n, ok := ch.lookup["#test1"]; ok || n != nil { if n, ok := ch.lookup["#test1"]; ok || n != nil {
t.Errorf("Nick test1 not properly removed from lookup map.") t.Errorf("Nick test1 not properly removed from lookup map.")
} }
compareChannel(t, ch)
} }
func TestChannelParseModes(t *testing.T) { func TestChannelParseModes(t *testing.T) {
ch := NewChannel("#test1") ch := newChannel("#test1")
md := ch.Modes md := ch.modes
// Channel modes can adjust channel privs too, so we need a Nick // Channel modes can adjust channel privs too, so we need a Nick
nk := NewNick("test1") nk := newNick("test1")
cp := new(ChanPrivs) cp := new(ChanPrivs)
ch.addNick(nk, cp) ch.addNick(nk, cp)
// Test bools first. // Test bools first.
compareChannel(t, ch)
if md.Private || md.Secret || md.ProtectedTopic || md.NoExternalMsg || if md.Private || md.Secret || md.ProtectedTopic || md.NoExternalMsg ||
md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly { md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly {
t.Errorf("Modes for new channel set to true.") t.Errorf("Modes for new channel set to true.")
@ -72,8 +87,9 @@ func TestChannelParseModes(t *testing.T) {
md.InviteOnly = true md.InviteOnly = true
// Flip some MOAR bits. // 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 || if md.Private || !md.Secret || !md.ProtectedTopic || !md.NoExternalMsg ||
!md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly { !md.Moderated || md.InviteOnly || md.OperOnly || md.SSLOnly {
t.Errorf("Modes not flipped correctly by ParseModes.") t.Errorf("Modes not flipped correctly by ParseModes.")
@ -85,19 +101,22 @@ func TestChannelParseModes(t *testing.T) {
} }
// enable limit correctly // enable limit correctly
ch.ParseModes("+l", "256") ch.parseModes("+l", "256")
compareChannel(t, ch)
if md.Limit != 256 { if md.Limit != 256 {
t.Errorf("Limit for channel not set correctly") t.Errorf("Limit for channel not set correctly")
} }
// enable limit incorrectly // enable limit incorrectly
ch.ParseModes("+l") ch.parseModes("+l")
compareChannel(t, ch)
if md.Limit != 256 { if md.Limit != 256 {
t.Errorf("Bad limit value caused limit to be unset.") t.Errorf("Bad limit value caused limit to be unset.")
} }
// disable limit correctly // disable limit correctly
ch.ParseModes("-l") ch.parseModes("-l")
compareChannel(t, ch)
if md.Limit != 0 { if md.Limit != 0 {
t.Errorf("Limit for channel not unset correctly") t.Errorf("Limit for channel not unset correctly")
} }
@ -108,19 +127,22 @@ func TestChannelParseModes(t *testing.T) {
} }
// enable key correctly // enable key correctly
ch.ParseModes("+k", "foobar") ch.parseModes("+k", "foobar")
compareChannel(t, ch)
if md.Key != "foobar" { if md.Key != "foobar" {
t.Errorf("Key for channel not set correctly") t.Errorf("Key for channel not set correctly")
} }
// enable key incorrectly // enable key incorrectly
ch.ParseModes("+k") ch.parseModes("+k")
compareChannel(t, ch)
if md.Key != "foobar" { if md.Key != "foobar" {
t.Errorf("Bad key value caused key to be unset.") t.Errorf("Bad key value caused key to be unset.")
} }
// disable key correctly // disable key correctly
ch.ParseModes("-k") ch.parseModes("-k")
compareChannel(t, ch)
if md.Key != "" { if md.Key != "" {
t.Errorf("Key for channel not unset correctly") t.Errorf("Key for channel not unset correctly")
} }
@ -128,16 +150,18 @@ func TestChannelParseModes(t *testing.T) {
// Test chan privs parsing. // Test chan privs parsing.
cp.Op = true cp.Op = true
cp.HalfOp = 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 { if !cp.Owner || !cp.Admin || cp.Op || !cp.HalfOp || cp.Voice {
t.Errorf("Channel privileges not flipped correctly by ParseModes.") t.Errorf("Channel privileges not flipped correctly by ParseModes.")
} }
// Test a random mix of modes, just to be sure // Test a random mix of modes, just to be sure
md.Limit = 256 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 || if !md.Private || md.Secret || !md.ProtectedTopic || !md.NoExternalMsg ||
!md.Moderated || md.InviteOnly || md.OperOnly || !md.SSLOnly { !md.Moderated || md.InviteOnly || md.OperOnly || !md.SSLOnly {
t.Errorf("Modes not flipped correctly by ParseModes (2).") t.Errorf("Modes not flipped correctly by ParseModes (2).")