2011-10-08 09:57:10 +00:00
|
|
|
package state
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
import (
|
2011-09-29 21:54:54 +00:00
|
|
|
"github.com/fluffle/goirc/logging"
|
2009-12-17 17:22:31 +00:00
|
|
|
)
|
|
|
|
|
2011-09-28 19:48:58 +00:00
|
|
|
// The state manager interface
|
|
|
|
type StateTracker interface {
|
|
|
|
NewNick(nick string) *Nick
|
|
|
|
GetNick(nick string) *Nick
|
2011-10-06 22:54:34 +00:00
|
|
|
ReNick(old, neu string)
|
|
|
|
DelNick(nick string)
|
2011-09-28 19:48:58 +00:00
|
|
|
NewChannel(channel string) *Channel
|
|
|
|
GetChannel(channel string) *Channel
|
2011-10-06 22:54:34 +00:00
|
|
|
DelChannel(channel string)
|
2011-09-28 19:48:58 +00:00
|
|
|
IsOn(channel, nick string) bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... 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
|
|
|
|
}
|
|
|
|
|
2009-12-17 17:22:31 +00:00
|
|
|
/******************************************************************************\
|
2011-09-28 19:48:58 +00:00
|
|
|
* tracker methods to create/look up nicks/channels
|
2009-12-17 17:22:31 +00:00
|
|
|
\******************************************************************************/
|
2009-12-18 22:39:22 +00:00
|
|
|
|
2011-10-12 21:59:52 +00:00
|
|
|
func NewTracker() *stateTracker {
|
2011-10-08 09:57:10 +00:00
|
|
|
st := &stateTracker{}
|
|
|
|
st.initialise()
|
2011-10-12 21:59:52 +00:00
|
|
|
return st
|
2011-10-08 09:57:10 +00:00
|
|
|
}
|
|
|
|
|
2011-10-08 09:40:58 +00:00
|
|
|
func (st *stateTracker) initialise() {
|
|
|
|
st.nicks = make(map[string]*Nick)
|
|
|
|
st.chans = make(map[string]*Channel)
|
|
|
|
}
|
|
|
|
|
2011-10-08 09:57:10 +00:00
|
|
|
// Creates a new Nick, initialises it, and stores it so it
|
2009-12-18 22:39:22 +00:00
|
|
|
// can be properly tracked for state management purposes.
|
2011-09-28 19:48:58 +00:00
|
|
|
func (st *stateTracker) NewNick(nick string) *Nick {
|
2011-10-12 21:59:52 +00:00
|
|
|
if _, ok := st.nicks[nick]; ok {
|
|
|
|
logging.Warn("StateTracker.NewNick(): %s already tracked.", nick)
|
|
|
|
return nil
|
|
|
|
}
|
2011-09-28 19:48:58 +00:00
|
|
|
n := &Nick{Nick: nick, st: st}
|
2009-12-17 21:30:18 +00:00
|
|
|
n.initialise()
|
2011-09-28 19:48:58 +00:00
|
|
|
st.nicks[nick] = n
|
2009-12-17 21:30:18 +00:00
|
|
|
return n
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2011-10-08 09:57:10 +00:00
|
|
|
// Returns a Nick for the nick n, if we're tracking it.
|
2011-09-28 19:48:58 +00:00
|
|
|
func (st *stateTracker) GetNick(n string) *Nick {
|
|
|
|
if nick, ok := st.nicks[n]; ok {
|
2009-12-17 17:22:31 +00:00
|
|
|
return nick
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-10-08 09:57:10 +00:00
|
|
|
// Signals to the tracker that a Nick should be tracked
|
2011-10-06 22:54:34 +00:00
|
|
|
// under a "neu" nick rather than the old one.
|
|
|
|
func (st *stateTracker) ReNick(old, neu string) {
|
|
|
|
if n, ok := st.nicks[old]; ok {
|
|
|
|
st.nicks[old] = nil, false
|
|
|
|
n.Nick = neu
|
|
|
|
st.nicks[neu] = n
|
2011-10-12 21:59:52 +00:00
|
|
|
} else {
|
|
|
|
logging.Warn("StateTracker.ReNick(): %s not tracked.", old)
|
2011-10-06 22:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 09:57:10 +00:00
|
|
|
// Removes a Nick from being tracked.
|
2011-10-06 22:54:34 +00:00
|
|
|
func (st *stateTracker) DelNick(n string) {
|
|
|
|
if _, ok := st.nicks[n]; ok {
|
|
|
|
st.nicks[n] = nil, false
|
2011-10-12 21:59:52 +00:00
|
|
|
} else {
|
|
|
|
logging.Warn("StateTracker.DelNick(): %s not tracked.", n)
|
2011-10-06 22:54:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 09:57:10 +00:00
|
|
|
// Creates a new Channel, initialises it, and stores it so it
|
2009-12-18 22:39:22 +00:00
|
|
|
// can be properly tracked for state management purposes.
|
2011-09-28 19:48:58 +00:00
|
|
|
func (st *stateTracker) NewChannel(c string) *Channel {
|
|
|
|
ch := &Channel{Name: c, st: st}
|
2009-12-17 21:30:18 +00:00
|
|
|
ch.initialise()
|
2011-09-28 19:48:58 +00:00
|
|
|
st.chans[c] = ch
|
2009-12-17 17:22:31 +00:00
|
|
|
return ch
|
|
|
|
}
|
|
|
|
|
2011-10-08 09:57:10 +00:00
|
|
|
// Returns a Channel for the channel c, if we're tracking it.
|
2011-09-28 19:48:58 +00:00
|
|
|
func (st *stateTracker) GetChannel(c string) *Channel {
|
|
|
|
if ch, ok := st.chans[c]; ok {
|
2009-12-17 17:22:31 +00:00
|
|
|
return ch
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-10-08 09:57:10 +00:00
|
|
|
// Removes a Channel from being tracked.
|
2011-10-06 22:54:34 +00:00
|
|
|
func (st *stateTracker) DelChannel(c string) {
|
|
|
|
if _, ok := st.chans[c]; ok {
|
|
|
|
st.chans[c] = nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-08 09:57:10 +00:00
|
|
|
// Returns true if both the channel c and the nick n are tracked
|
|
|
|
// and the nick is associated with the channel.
|
2011-10-06 22:54:34 +00:00
|
|
|
func (st *stateTracker) IsOn(c, n string) bool {
|
|
|
|
nk := st.GetNick(n)
|
|
|
|
ch := st.GetChannel(c)
|
|
|
|
if nk != nil && ch != nil {
|
|
|
|
return nk.IsOn(ch)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|