From 111a23d87ca247ad4ed8f99c5c27034f49f184a3 Mon Sep 17 00:00:00 2001 From: Alex Bramley Date: Thu, 20 Oct 2011 00:10:33 +0100 Subject: [PATCH] Epic refactor of the refactor. --- state/channel.go | 77 ++++++++++------------- state/nick.go | 94 ++++++++++------------------ state/tracker.go | 141 +++++++++++++++++++++++++++++++++++------- state/tracker_test.go | 42 +++++++------ 4 files changed, 204 insertions(+), 150 deletions(-) diff --git a/state/channel.go b/state/channel.go index d01116a..9a96dbf 100644 --- a/state/channel.go +++ b/state/channel.go @@ -11,8 +11,8 @@ import ( type Channel struct { Name, Topic string Modes *ChanMode + lookup map[string]*Nick nicks map[*Nick]*ChanPrivs - st StateTracker } // A struct representing the modes of an IRC Channel @@ -95,50 +95,35 @@ func NewChannel(name string) *Channel { Name: name, Modes: new(ChanMode), nicks: make(map[*Nick]*ChanPrivs), - } -} - -// Associates a Nick with a Channel using a shared set of ChanPrivs -func (ch *Channel) AddNick(n *Nick) { - if _, ok := ch.nicks[n]; !ok { - ch.nicks[n] = new(ChanPrivs) - n.chans[ch] = ch.nicks[n] - } else { - logging.Warn("Channel.AddNick(): trying to add already-present "+ - "nick %s to channel %s", n.Nick, ch.Name) + lookup: make(map[string]*Nick), } } // Returns true if the Nick is associated with the Channel -func (ch *Channel) IsOn(n *Nick) bool { - _, ok := ch.nicks[n] +func (ch *Channel) IsOn(nk *Nick) bool { + _, ok := ch.nicks[nk] return ok } -// Disassociates a Nick from a Channel. Will call ch.Delete() if the Nick being -// removed is the connection's nick. Will also call n.DelChannel(ch) to remove -// the association from the perspective of the Nick. -func (ch *Channel) DelNick(n *Nick) { - if _, ok := ch.nicks[n]; ok { - if n.me { - // we're leaving the channel, so remove all state we have about it - ch.Delete() - } else { - ch.nicks[n] = nil, false - n.DelChannel(ch) - } - } - // we call Channel.DelNick() and Nick.DelChannel() from each other to ensure - // consistency, and this would mean spewing an error message every delete +func (ch *Channel) IsOnStr(n string) bool { + _, ok := ch.lookup[n] + return ok } -// Stops the Channel from being tracked by state tracking handlers. Also calls -// n.DelChannel(ch) for all nicks that are associated with the channel. -func (ch *Channel) Delete() { - for n, _ := range ch.nicks { - n.DelChannel(ch) +// Associates a Nick with a Channel +func (ch *Channel) addNick(nk *Nick, cp *ChanPrivs) { + if _, ok := ch.nicks[nk]; !ok { + ch.nicks[nk] = cp + ch.lookup[nk.Nick] = nk + } +} + +// Disassociates a Nick from a Channel. +func (ch *Channel) delNick(nk *Nick) { + if _, ok := ch.nicks[nk]; ok { + ch.nicks[nk] = nil, false + ch.lookup[nk.Nick] = nil, false } - ch.st.DelChannel(ch.Name) } // Parses mode strings for a channel. @@ -190,19 +175,19 @@ func (ch *Channel) ParseModes(modes string, modeargs []string) { } case 'q', 'a', 'o', 'h', 'v': if len(modeargs) != 0 { - n := ch.st.GetNick(modeargs[0]) - if p, ok := ch.nicks[n]; ok { + if nk, ok := ch.lookup[modeargs[0]]; ok { + cp := ch.nicks[nk] switch m { case 'q': - p.Owner = modeop + cp.Owner = modeop case 'a': - p.Admin = modeop + cp.Admin = modeop case 'o': - p.Op = modeop + cp.Op = modeop case 'h': - p.HalfOp = modeop + cp.HalfOp = modeop case 'v': - p.Voice = modeop + cp.Voice = modeop } modeargs = modeargs[1:] } else { @@ -229,8 +214,8 @@ func (ch *Channel) String() string { str += "Topic: " + ch.Topic + "\n\t" str += "Modes: " + ch.Modes.String() + "\n\t" str += "Nicks: \n" - for n, p := range ch.nicks { - str += "\t\t" + n.Nick + ": " + p.String() + "\n" + for nk, cp := range ch.nicks { + str += "\t\t" + nk.Nick + ": " + cp.String() + "\n" } return str } @@ -273,9 +258,9 @@ func (cm *ChanMode) String() string { // Returns a string representing the channel privileges. Looks like: // +o -func (p *ChanPrivs) String() string { +func (cp *ChanPrivs) String() string { str := "+" - v := reflect.Indirect(reflect.ValueOf(p)) + v := reflect.Indirect(reflect.ValueOf(cp)) t := v.Type() for i := 0; i < v.NumField(); i++ { switch f := v.Field(i); f.Kind() { diff --git a/state/nick.go b/state/nick.go index 25890e3..006c5ea 100644 --- a/state/nick.go +++ b/state/nick.go @@ -9,9 +9,8 @@ import ( type Nick struct { Nick, Ident, Host, Name string Modes *NickMode + lookup map[string]*Channel chans map[*Channel]*ChanPrivs - me bool - st StateTracker } // A struct representing the modes of an IRC Nick (User Modes) @@ -44,68 +43,44 @@ func init() { * Nick methods for state management \******************************************************************************/ -func NewNick(nick string) *Nick { +func NewNick(n string) *Nick { return &Nick{ - Nick: nick, + Nick: n, Modes: new(NickMode), chans: make(map[*Channel]*ChanPrivs), - } -} - -// Associates a Channel with a Nick using a shared ChanPrivs -// -// Very slightly different to Channel.AddNick() in that it tests for a -// pre-existing association within the Nick object rather than the -// Channel object before associating the two. -func (n *Nick) AddChannel(ch *Channel) { - if _, ok := n.chans[ch]; !ok { - ch.nicks[n] = new(ChanPrivs) - n.chans[ch] = ch.nicks[n] - } else { - logging.Warn("Nick.AddChannel(): trying to add already-present "+ - "channel %s to nick %s", ch.Name, n.Nick) + lookup: make(map[string]*Channel), } } // Returns true if the Nick is associated with the Channel. -func (n *Nick) IsOn(ch *Channel) bool { - _, ok := n.chans[ch] +func (nk *Nick) IsOn(ch *Channel) bool { + _, ok := nk.chans[ch] return ok } -// Returns true if the Nick is Me! -func (n *Nick) IsMe() bool { - return n.me +func (nk *Nick) IsOnStr(c string) bool { + _, ok := nk.lookup[c] + return ok } -// Disassociates a Channel from a Nick. Will call n.Delete() if the Nick is no -// longer on any channels we are tracking. Will also call ch.DelNick(n) to -// remove the association from the perspective of the Channel. -func (n *Nick) DelChannel(ch *Channel) { - if _, ok := n.chans[ch]; ok { - n.chans[ch] = nil, false - ch.DelNick(n) - if len(n.chans) == 0 { - // nick is no longer in any channels we inhabit, stop tracking it - n.Delete() - } +// Associates a Channel with a Nick. +func (nk *Nick) addChannel(ch *Channel, cp *ChanPrivs) { + if _, ok := nk.chans[ch]; !ok { + nk.chans[ch] = cp + nk.lookup[ch.Name] = ch } } -// Stops the Nick from being tracked by state tracking handlers. Also calls -// ch.DelNick(n) for all Nicks that are associated with the Channel. -func (n *Nick) Delete() { - // we don't ever want to remove *our* nick from st.nicks... - if !n.me { - for ch, _ := range n.chans { - ch.DelNick(n) - } - n.st.DelNick(n.Nick) +// Disassociates a Channel from a Nick. +func (nk *Nick) delChannel(ch *Channel) { + if _, ok := nk.chans[ch]; ok { + nk.chans[ch] = nil, false + nk.lookup[ch.Name] = nil, false } } // Parse mode strings for a Nick. -func (n *Nick) ParseModes(modes string) { +func (nk *Nick) ParseModes(modes string) { var modeop bool // true => add mode, false => remove mode for i := 0; i < len(modes); i++ { switch m := modes[i]; m { @@ -114,15 +89,17 @@ func (n *Nick) ParseModes(modes string) { case '-': modeop = false case 'i': - n.Modes.Invisible = modeop + nk.Modes.Invisible = modeop case 'o': - n.Modes.Oper = modeop + nk.Modes.Oper = modeop case 'w': - n.Modes.WallOps = modeop + nk.Modes.WallOps = modeop case 'x': - n.Modes.HiddenHost = modeop + nk.Modes.HiddenHost = modeop case 'z': - n.Modes.SSL = modeop + nk.Modes.SSL = modeop + default: + logging.Info("Nick.ParseModes(): unknown mode char %c", m) } } } @@ -135,17 +112,14 @@ func (n *Nick) ParseModes(modes string) { // Channels: // : e.g. #moo: +o // ... -func (n *Nick) String() string { - str := "Nick: " + n.Nick + "\n\t" - str += "Hostmask: " + n.Ident + "@" + n.Host + "\n\t" - str += "Real Name: " + n.Name + "\n\t" - str += "Modes: " + n.Modes.String() + "\n\t" - if n.me { - str += "I think this is ME!\n\t" - } +func (nk *Nick) String() string { + str := "Nick: " + nk.Nick + "\n\t" + str += "Hostmask: " + nk.Ident + "@" + nk.Host + "\n\t" + str += "Real Name: " + nk.Name + "\n\t" + str += "Modes: " + nk.Modes.String() + "\n\t" str += "Channels: \n" - for ch, p := range n.chans { - str += "\t\t" + ch.Name + ": " + p.String() + "\n" + for ch, cp := range nk.chans { + str += "\t\t" + ch.Name + ": " + cp.String() + "\n" } return str } diff --git a/state/tracker.go b/state/tracker.go index 5bff676..77f9c55 100644 --- a/state/tracker.go +++ b/state/tracker.go @@ -6,51 +6,63 @@ import ( // The state manager interface type StateTracker interface { + // Nick methods NewNick(nick string) *Nick GetNick(nick string) *Nick ReNick(old, neu string) DelNick(nick string) + // Channel methods NewChannel(channel string) *Channel GetChannel(channel string) *Channel DelChannel(channel string) + // Information about ME! + Me() *Nick + // And the tracking operations IsOn(channel, nick string) bool + Associate(channel *Channel, nick *Nick) + Dissociate(channel *Channel, nick *Nick) } -// ... and a struct to implement it +// ... and a struct to implement it ... type stateTracker struct { // Map of channels we're on chans map[string]*Channel // Map of nicks we know about nicks map[string]*Nick + + // We need to keep state on who we are :-) + me *Nick +} + +// ... and finally a constructor to make it. +func NewTracker(mynick string) *stateTracker { + st := &stateTracker{ + chans: make(map[string]*Channel), + nicks: make(map[string]*Nick), + } + st.me = st.NewNick(mynick) + return st } /******************************************************************************\ * tracker methods to create/look up nicks/channels \******************************************************************************/ -func NewTracker() *stateTracker { - return &stateTracker{ - chans: make(map[string]*Channel), - nicks: make(map[string]*Nick), - } -} - // Creates a new Nick, initialises it, and stores it so it // can be properly tracked for state management purposes. -func (st *stateTracker) NewNick(nick string) *Nick { - if _, ok := st.nicks[nick]; ok { - logging.Warn("StateTracker.NewNick(): %s already tracked.", nick) +func (st *stateTracker) NewNick(n string) *Nick { + if _, ok := st.nicks[n]; ok { + logging.Warn("StateTracker.NewNick(): %s already tracked.", n) return nil } - st.nicks[nick] = NewNick(nick) - st.nicks[nick].st = st - return st.nicks[nick] + st.nicks[n] = NewNick(n) + return st.nicks[n] } // Returns a Nick for the nick n, if we're tracking it. func (st *stateTracker) GetNick(n string) *Nick { - if nick, ok := st.nicks[n]; ok { - return nick + if nk, ok := st.nicks[n]; ok { + return nk } return nil } @@ -58,11 +70,11 @@ func (st *stateTracker) GetNick(n string) *Nick { // Signals to the tracker that a Nick should be tracked // under a "neu" nick rather than the old one. func (st *stateTracker) ReNick(old, neu string) { - if n, ok := st.nicks[old]; ok { + if nk, ok := st.nicks[old]; ok { if _, ok := st.nicks[neu]; !ok { st.nicks[old] = nil, false - n.Nick = neu - st.nicks[neu] = n + nk.Nick = neu + st.nicks[neu] = nk } else { logging.Warn("StateTracker.ReNick(): %s already exists.", neu) } @@ -73,13 +85,36 @@ func (st *stateTracker) ReNick(old, neu string) { // Removes a Nick from being tracked. func (st *stateTracker) DelNick(n string) { - if _, ok := st.nicks[n]; ok { - st.nicks[n] = nil, false + if nk, ok := st.nicks[n]; ok { + if nk != st.me { + st.delNick(nk) + } else { + logging.Warn("StateTracker.DelNick(): won't delete myself.") + } } else { logging.Warn("StateTracker.DelNick(): %s not tracked.", n) } } +func (st *stateTracker) delNick(nk *Nick) { + if nk == st.me { + // Shouldn't get here => internal state tracking code is fubar. + logging.Error("StateTracker.DelNick(): TRYING TO DELETE ME :-(") + return + } + st.nicks[nk.Nick] = nil, false + for ch, _ := range nk.chans { + nk.delChannel(ch) + ch.delNick(nk) + if len(ch.nicks) == 0 { + // Deleting a nick from tracking shouldn't empty any channels as + // *we* should be on the channel with them to be tracking them. + logging.Error("StateTracker.delNick(): deleting nick %s emptied "+ + "channel %s, this shouldn't happen", nk.Nick, ch.Name) + } + } +} + // Creates a new Channel, initialises it, and stores it so it // can be properly tracked for state management purposes. func (st *stateTracker) NewChannel(c string) *Channel { @@ -88,7 +123,6 @@ func (st *stateTracker) NewChannel(c string) *Channel { return nil } st.chans[c] = NewChannel(c) - st.chans[c].st = st return st.chans[c] } @@ -102,11 +136,30 @@ func (st *stateTracker) GetChannel(c string) *Channel { // Removes a Channel from being tracked. func (st *stateTracker) DelChannel(c string) { - if _, ok := st.chans[c]; ok { - st.chans[c] = nil, false + if ch, ok := st.chans[c]; ok { + st.delChannel(ch) + } else { + logging.Warn("StateTracker.DelChannel(): %s not tracked.", c) } } +func (st *stateTracker) delChannel(ch *Channel) { + st.chans[ch.Name] = nil, false + for nk, _ := range ch.nicks { + ch.delNick(nk) + nk.delChannel(ch) + if len(nk.chans) == 0 && nk != st.me { + // We're no longer in any channels with this nick. + st.delNick(nk) + } + } +} + +// Returns the Nick the state tracker thinks is Me. +func (st *stateTracker) Me() *Nick { + return st.me +} + // 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 { @@ -117,3 +170,43 @@ func (st *stateTracker) IsOn(c, n string) bool { } return false } + +// Associates an already known nick with an already known channel. +func (st *stateTracker) Associate(ch *Channel, nk *Nick) { + if ch == nil || nk == nil { + logging.Error("StateTracker.Associate(): passed nil values :-(") + return + } + if nk.IsOn(ch) { + logging.Warn("StateTracker.Associate(): %s already on %s.", + nk.Nick, ch.Name) + return + } + cp := new(ChanPrivs) + ch.addNick(nk, cp) + nk.addChannel(ch, 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: + logging.Error("StateTracker.Dissociate(): passed nil values :-(") + case !nk.IsOn(ch): + logging.Warn("StateTracker.Dissociate(): %s not on %s.", + nk.Nick, ch.Name) + case nk == st.me: + // I'm leaving the channel for some reason, so it won't be tracked. + st.delChannel(ch) + default: + // Remove the nick from the channel and the channel from the nick. + ch.delNick(nk) + nk.delChannel(ch) + if len(nk.chans) == 0 { + // We're no longer in any channels with this nick. + st.delNick(nk) + } + } +} diff --git a/state/tracker_test.go b/state/tracker_test.go index 4658785..09b04da 100644 --- a/state/tracker_test.go +++ b/state/tracker_test.go @@ -5,18 +5,18 @@ import ( ) func TestNewNick(t *testing.T) { - st := NewTracker() + st := NewTracker("mynick") - if len(st.nicks) != 0 { - t.Errorf("Nick list of new tracker is non-zero length.") + if len(st.nicks) != 1 { + t.Errorf("Nick list of new tracker is not 1 (me!).") } - nick := st.NewNick("test1") + test1 := st.NewNick("test1") - if nick == nil || nick.Nick != "test1" || nick.st != st { + if test1 == nil || test1.Nick != "test1" { t.Errorf("Nick object created incorrectly by NewNick.") } - if n, ok := st.nicks["test1"]; !ok || n != nick || len(st.nicks) != 1 { + if n, ok := st.nicks["test1"]; !ok || n != test1 || len(st.nicks) != 2 { t.Errorf("Nick object stored incorrectly by NewNick.") } @@ -26,7 +26,7 @@ func TestNewNick(t *testing.T) { } func TestGetNick(t *testing.T) { - st := NewTracker() + st := NewTracker("mynick") test1 := NewNick("test1") st.nicks["test1"] = test1 @@ -37,13 +37,13 @@ func TestGetNick(t *testing.T) { if n := st.GetNick("test2"); n != nil { t.Errorf("Nick unexpectedly returned by GetNick.") } - if len(st.nicks) != 1 { + if len(st.nicks) != 2 { t.Errorf("Nick list changed size during GetNick.") } } func TestReNick(t *testing.T) { - st := NewTracker() + st := NewTracker("mynick") test1 := NewNick("test1") st.nicks["test1"] = test1 @@ -59,7 +59,7 @@ func TestReNick(t *testing.T) { if test1.Nick != "test2" { t.Errorf("Nick test1 not changed correctly.") } - if len(st.nicks) != 1 { + if len(st.nicks) != 2 { t.Errorf("Nick list changed size during ReNick.") } @@ -73,14 +73,14 @@ func TestReNick(t *testing.T) { if n, ok := st.nicks["test1"]; !ok || n != test2 { t.Errorf("Nick test1 overwritten/deleted by ReNick.") } - if len(st.nicks) != 2 { + if len(st.nicks) != 3 { t.Errorf("Nick list changed size during ReNick.") } } func TestDelNick(t *testing.T) { - st := NewTracker() + st := NewTracker("mynick") test1 := NewNick("test1") st.nicks["test1"] = test1 @@ -90,7 +90,7 @@ func TestDelNick(t *testing.T) { if _, ok := st.nicks["test1"]; ok { t.Errorf("Nick test1 still exists after DelNick.") } - if len(st.nicks) != 0 { + if len(st.nicks) != 1 { t.Errorf("Nick list still contains nicks after DelNick.") } @@ -98,13 +98,13 @@ func TestDelNick(t *testing.T) { st.DelNick("test2") - if len(st.nicks) != 1 { + if len(st.nicks) != 2 { t.Errorf("DelNick had unexpected side-effects.") } } func TestNewChannel(t *testing.T) { - st := NewTracker() + st := NewTracker("mynick") if len(st.chans) != 0 { t.Errorf("Channel list of new tracker is non-zero length.") @@ -112,7 +112,7 @@ func TestNewChannel(t *testing.T) { test1 := st.NewChannel("#test1") - if test1 == nil || test1.Name != "#test1" || test1.st != st { + if test1 == nil || test1.Name != "#test1" { t.Errorf("Channel object created incorrectly by NewChannel.") } if c, ok := st.chans["#test1"]; !ok || c != test1 || len(st.chans) != 1 { @@ -125,7 +125,7 @@ func TestNewChannel(t *testing.T) { } func TestGetChannel(t *testing.T) { - st := NewTracker() + st := NewTracker("mynick") test1 := NewChannel("#test1") st.chans["#test1"] = test1 @@ -142,7 +142,7 @@ func TestGetChannel(t *testing.T) { } func TestDelChannel(t *testing.T) { - st := NewTracker() + st := NewTracker("mynick") test1 := NewChannel("#test1") st.chans["#test1"] = test1 @@ -166,7 +166,7 @@ func TestDelChannel(t *testing.T) { } func TestIsOn(t *testing.T) { - st := NewTracker() + st := NewTracker("mynick") nick1 := NewNick("test1") st.nicks["test1"] = nick1 @@ -176,7 +176,9 @@ func TestIsOn(t *testing.T) { if st.IsOn("#test1", "test1") { t.Errorf("test1 is not on #test1 (yet)") } - chan1.AddNick(nick1) + cp := new(ChanPrivs) + chan1.addNick(nick1, cp) + nick1.addChannel(chan1, cp) if !st.IsOn("#test1", "test1") { t.Errorf("test1 is on #test1 (now)") }