diff --git a/state/tracker.go b/state/tracker.go index 686fedc..3ae5d36 100644 --- a/state/tracker.go +++ b/state/tracker.go @@ -1,6 +1,8 @@ package state import ( + "fmt" + "github.com/fluffle/goirc/logging" "sync" @@ -11,6 +13,7 @@ type Tracker interface { // Nick methods NewNick(nick string) *Nick GetNick(nick string) *Nick + GetNickByHostmask(host string) *Nick ReNick(old, neu string) *Nick DelNick(nick string) *Nick NickInfo(nick, ident, host, name string) *Nick @@ -100,6 +103,18 @@ func (st *stateTracker) GetNick(n string) *Nick { return nil } +// Returns a nick for the hostmask h, if we're tracking it. +func (st *stateTracker) GetNickByHostmask(h string) *Nick { + st.mu.Lock() + defer st.mu.Unlock() + for n := range st.nicks { + if fmt.Sprintf("%s@%s", st.nicks[n].ident, st.nicks[n].host) == h { + return st.nicks[n].Nick() + } + } + return nil +} + // 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) *Nick { diff --git a/state/tracker_test.go b/state/tracker_test.go index 1cfe8fc..71dd88c 100644 --- a/state/tracker_test.go +++ b/state/tracker_test.go @@ -58,6 +58,22 @@ func TestSTGetNick(t *testing.T) { } } +func TestSTGetNickByHostmask(t *testing.T) { + st := NewTracker("mynick") + test1 := st.NewNick("test1") + test1 = st.NickInfo("test1", "test", "test", "test") + + if n := st.GetNickByHostmask("test@test"); !test1.Equals(n) { + t.Errorf("Incorrect nick returned by GetNickByHostmask.") + } + if n := st.GetNickByHostmask("test2@test"); n != nil { + t.Errorf("Nick unexpectedly returned by GetNickByHostmask.") + } + if len(st.nicks) != 2 { + t.Errorf("Nick list changed size during GetNickByHostmask.") + } +} + func TestSTReNick(t *testing.T) { st := NewTracker("mynick") test1 := st.NewNick("test1")