2011-10-08 09:57:10 +00:00
|
|
|
package state
|
2009-12-17 17:22:31 +00:00
|
|
|
|
|
|
|
import (
|
2013-09-27 21:19:40 +00:00
|
|
|
"github.com/fluffle/goirc/logging"
|
2014-12-31 13:17:46 +00:00
|
|
|
|
|
|
|
"sync"
|
2009-12-17 17:22:31 +00:00
|
|
|
)
|
|
|
|
|
2011-09-28 19:48:58 +00:00
|
|
|
// The state manager interface
|
2013-02-16 11:29:56 +00:00
|
|
|
type Tracker interface {
|
2011-10-19 23:10:33 +00:00
|
|
|
// Nick methods
|
2011-09-28 19:48:58 +00:00
|
|
|
NewNick(nick string) *Nick
|
|
|
|
GetNick(nick string) *Nick
|
2014-12-31 13:17:46 +00:00
|
|
|
ReNick(old, neu string) *Nick
|
|
|
|
DelNick(nick string) *Nick
|
|
|
|
NickInfo(nick, ident, host, name string) *Nick
|
|
|
|
NickModes(nick, modestr string) *Nick
|
2011-10-19 23:10:33 +00:00
|
|
|
// Channel methods
|
2011-09-28 19:48:58 +00:00
|
|
|
NewChannel(channel string) *Channel
|
|
|
|
GetChannel(channel string) *Channel
|
2014-12-31 13:17:46 +00:00
|
|
|
DelChannel(channel string) *Channel
|
|
|
|
Topic(channel, topic string) *Channel
|
|
|
|
ChannelModes(channel, modestr string, modeargs ...string) *Channel
|
2011-10-19 23:10:33 +00:00
|
|
|
// Information about ME!
|
|
|
|
Me() *Nick
|
|
|
|
// And the tracking operations
|
2011-10-27 16:03:01 +00:00
|
|
|
IsOn(channel, nick string) (*ChanPrivs, bool)
|
2014-12-31 13:17:46 +00:00
|
|
|
Associate(channel, nick string) *ChanPrivs
|
|
|
|
Dissociate(channel, nick string)
|
2011-11-05 02:25:25 +00:00
|
|
|
Wipe()
|
2011-11-05 09:30:32 +00:00
|
|
|
// The state tracker can output a debugging string
|
|
|
|
String() string
|
2011-09-28 19:48:58 +00:00
|
|
|
}
|
|
|
|
|
2011-10-19 23:10:33 +00:00
|
|
|
// ... and a struct to implement it ...
|
2011-09-28 19:48:58 +00:00
|
|
|
type stateTracker struct {
|
|
|
|
// Map of channels we're on
|
2014-12-31 13:17:46 +00:00
|
|
|
chans map[string]*channel
|
2011-09-28 19:48:58 +00:00
|
|
|
// Map of nicks we know about
|
2014-12-31 13:17:46 +00:00
|
|
|
nicks map[string]*nick
|
2011-09-28 19:48:58 +00:00
|
|
|
|
2011-10-19 23:10:33 +00:00
|
|
|
// We need to keep state on who we are :-)
|
2014-12-31 13:17:46 +00:00
|
|
|
me *nick
|
|
|
|
|
|
|
|
// And we need to protect against data races *cough*.
|
|
|
|
mu sync.Mutex
|
2011-10-19 23:10:33 +00:00
|
|
|
}
|
2009-12-18 22:39:22 +00:00
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
var _ Tracker = (*stateTracker)(nil)
|
|
|
|
|
2011-11-05 02:25:25 +00:00
|
|
|
// ... and a constructor to make it ...
|
2013-01-23 22:33:01 +00:00
|
|
|
func NewTracker(mynick string) *stateTracker {
|
2011-10-19 23:10:33 +00:00
|
|
|
st := &stateTracker{
|
2014-12-31 13:17:46 +00:00
|
|
|
chans: make(map[string]*channel),
|
|
|
|
nicks: make(map[string]*nick),
|
2011-10-13 21:48:04 +00:00
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
st.me = newNick(mynick)
|
|
|
|
st.nicks[mynick] = st.me
|
2011-10-19 23:10:33 +00:00
|
|
|
return st
|
2011-10-08 09:40:58 +00:00
|
|
|
}
|
|
|
|
|
2011-11-05 02:25:25 +00:00
|
|
|
// ... and a method to wipe the state clean.
|
|
|
|
func (st *stateTracker) Wipe() {
|
2014-12-31 13:17:46 +00:00
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
2011-11-05 02:25:25 +00:00
|
|
|
// Deleting all the channels implicitly deletes every nick but me.
|
|
|
|
for _, ch := range st.chans {
|
|
|
|
st.delChannel(ch)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-19 23:10:33 +00:00
|
|
|
/******************************************************************************\
|
|
|
|
* tracker methods to create/look up nicks/channels
|
|
|
|
\******************************************************************************/
|
|
|
|
|
2014-12-31 13:17:46 +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-10-19 23:10:33 +00:00
|
|
|
func (st *stateTracker) NewNick(n string) *Nick {
|
2014-12-31 13:17:46 +00:00
|
|
|
if n == "" {
|
|
|
|
logging.Warn("Tracker.NewNick(): Not tracking empty nick.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
2011-10-19 23:10:33 +00:00
|
|
|
if _, ok := st.nicks[n]; ok {
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Warn("Tracker.NewNick(): %s already tracked.", n)
|
2011-10-12 21:59:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
st.nicks[n] = newNick(n)
|
|
|
|
return st.nicks[n].Nick()
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:46 +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 {
|
2014-12-31 13:17:46 +00:00
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
2011-10-19 23:10:33 +00:00
|
|
|
if nk, ok := st.nicks[n]; ok {
|
2014-12-31 13:17:46 +00:00
|
|
|
return nk.Nick()
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:46 +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.
|
2014-12-31 13:17:46 +00:00
|
|
|
func (st *stateTracker) ReNick(old, neu string) *Nick {
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
|
|
|
nk, ok := st.nicks[old]
|
|
|
|
if !ok {
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Warn("Tracker.ReNick(): %s not tracked.", old)
|
2014-12-31 13:17:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, ok := st.nicks[neu]; ok {
|
|
|
|
logging.Warn("Tracker.ReNick(): %s already exists.", neu)
|
|
|
|
return nil
|
2011-10-06 22:54:34 +00:00
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
|
|
|
|
nk.nick = neu
|
|
|
|
delete(st.nicks, old)
|
|
|
|
st.nicks[neu] = nk
|
|
|
|
for ch, _ := range nk.chans {
|
|
|
|
// We also need to update the lookup maps of all the channels
|
|
|
|
// the nick is on, to keep things in sync.
|
|
|
|
delete(ch.lookup, old)
|
|
|
|
ch.lookup[neu] = nk
|
|
|
|
}
|
|
|
|
return nk.Nick()
|
2011-10-06 22:54:34 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
// Removes a nick from being tracked.
|
|
|
|
func (st *stateTracker) DelNick(n string) *Nick {
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
2011-10-19 23:10:33 +00:00
|
|
|
if nk, ok := st.nicks[n]; ok {
|
2014-12-31 13:17:46 +00:00
|
|
|
if nk == st.me {
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Warn("Tracker.DelNick(): won't delete myself.")
|
2014-12-31 13:17:46 +00:00
|
|
|
return nil
|
2011-10-19 23:10:33 +00:00
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
st.delNick(nk)
|
|
|
|
return nk.Nick()
|
2011-10-06 22:54:34 +00:00
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
logging.Warn("Tracker.DelNick(): %s not tracked.", n)
|
|
|
|
return nil
|
2011-10-06 22:54:34 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
func (st *stateTracker) delNick(nk *nick) {
|
|
|
|
// st.mu lock held by DelNick, DelChannel or Wipe
|
2011-10-19 23:10:33 +00:00
|
|
|
if nk == st.me {
|
|
|
|
// Shouldn't get here => internal state tracking code is fubar.
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Error("Tracker.DelNick(): TRYING TO DELETE ME :-(")
|
2011-10-19 23:10:33 +00:00
|
|
|
return
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
delete(st.nicks, nk.nick)
|
2011-10-19 23:10:33 +00:00
|
|
|
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.
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Error("Tracker.delNick(): deleting nick %s emptied "+
|
2014-12-31 13:17:46 +00:00
|
|
|
"channel %s, this shouldn't happen!", nk.nick, ch.name)
|
2011-10-19 23:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
// Sets ident, host and "real" name for the nick.
|
|
|
|
func (st *stateTracker) NickInfo(n, ident, host, name string) *Nick {
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
|
|
|
nk, ok := st.nicks[n]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
nk.ident = ident
|
|
|
|
nk.host = host
|
|
|
|
nk.name = name
|
|
|
|
return nk.Nick()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets user modes for the nick.
|
|
|
|
func (st *stateTracker) NickModes(n, modes string) *Nick {
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
|
|
|
nk, ok := st.nicks[n]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
nk.parseModes(modes)
|
|
|
|
return nk.Nick()
|
|
|
|
}
|
|
|
|
|
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 {
|
2014-12-31 13:17:46 +00:00
|
|
|
if c == "" {
|
|
|
|
logging.Warn("Tracker.NewChannel(): Not tracking empty channel.")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
2011-10-13 21:31:09 +00:00
|
|
|
if _, ok := st.chans[c]; ok {
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Warn("Tracker.NewChannel(): %s already tracked.", c)
|
2011-10-13 21:31:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
st.chans[c] = newChannel(c)
|
|
|
|
return st.chans[c].Channel()
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2014-12-31 13:17:46 +00:00
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
2011-09-28 19:48:58 +00:00
|
|
|
if ch, ok := st.chans[c]; ok {
|
2014-12-31 13:17:46 +00:00
|
|
|
return ch.Channel()
|
2009-12-17 17:22:31 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2011-10-08 09:57:10 +00:00
|
|
|
// Removes a Channel from being tracked.
|
2014-12-31 13:17:46 +00:00
|
|
|
func (st *stateTracker) DelChannel(c string) *Channel {
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
2011-10-19 23:10:33 +00:00
|
|
|
if ch, ok := st.chans[c]; ok {
|
|
|
|
st.delChannel(ch)
|
2014-12-31 13:17:46 +00:00
|
|
|
return ch.Channel()
|
2011-10-06 22:54:34 +00:00
|
|
|
}
|
2014-12-31 13:17:46 +00:00
|
|
|
logging.Warn("Tracker.DelChannel(): %s not tracked.", c)
|
|
|
|
return nil
|
2011-10-06 22:54:34 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
func (st *stateTracker) delChannel(ch *channel) {
|
|
|
|
// st.mu lock held by DelChannel or Wipe
|
|
|
|
delete(st.chans, ch.name)
|
2011-10-19 23:10:33 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-31 13:17:46 +00:00
|
|
|
// Sets the topic of a channel.
|
|
|
|
func (st *stateTracker) Topic(c, topic string) *Channel {
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
|
|
|
ch, ok := st.chans[c]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ch.topic = topic
|
|
|
|
return ch.Channel()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets modes for a channel, including privileges like +o.
|
|
|
|
func (st *stateTracker) ChannelModes(c, modes string, args ...string) *Channel {
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
|
|
|
ch, ok := st.chans[c]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ch.parseModes(modes, args...)
|
|
|
|
return ch.Channel()
|
|
|
|
}
|
|
|
|
|
2011-10-19 23:10:33 +00:00
|
|
|
// Returns the Nick the state tracker thinks is Me.
|
2017-10-31 21:38:46 +00:00
|
|
|
// NOTE: Nick() requires the mutex to be held.
|
2011-10-19 23:10:33 +00:00
|
|
|
func (st *stateTracker) Me() *Nick {
|
2017-10-31 21:38:46 +00:00
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
2014-12-31 13:17:46 +00:00
|
|
|
return st.me.Nick()
|
2011-10-19 23:10:33 +00:00
|
|
|
}
|
|
|
|
|
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-27 16:03:01 +00:00
|
|
|
func (st *stateTracker) IsOn(c, n string) (*ChanPrivs, bool) {
|
2014-12-31 13:17:46 +00:00
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
|
|
|
nk, nok := st.nicks[n]
|
|
|
|
ch, cok := st.chans[c]
|
|
|
|
if nok && cok {
|
|
|
|
return nk.isOn(ch)
|
2011-10-06 22:54:34 +00:00
|
|
|
}
|
2011-10-27 16:03:01 +00:00
|
|
|
return nil, false
|
2011-10-06 22:54:34 +00:00
|
|
|
}
|
2011-10-19 23:10:33 +00:00
|
|
|
|
|
|
|
// Associates an already known nick with an already known channel.
|
2014-12-31 13:17:46 +00:00
|
|
|
func (st *stateTracker) Associate(c, n string) *ChanPrivs {
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
|
|
|
nk, nok := st.nicks[n]
|
|
|
|
ch, cok := st.chans[c]
|
|
|
|
|
|
|
|
if !cok {
|
2011-10-27 16:54:23 +00:00
|
|
|
// As we can implicitly delete both nicks and channels from being
|
|
|
|
// tracked by dissociating one from the other, we should verify that
|
|
|
|
// we're not being passed an old Nick or Channel.
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Error("Tracker.Associate(): channel %s not found in "+
|
2014-12-31 13:17:46 +00:00
|
|
|
"internal state.", c)
|
2013-01-23 22:33:01 +00:00
|
|
|
return nil
|
2014-12-31 13:17:46 +00:00
|
|
|
} else if !nok {
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Error("Tracker.Associate(): nick %s not found in "+
|
2014-12-31 13:17:46 +00:00
|
|
|
"internal state.", n)
|
2013-01-23 22:33:01 +00:00
|
|
|
return nil
|
2014-12-31 13:17:46 +00:00
|
|
|
} else if _, ok := nk.isOn(ch); ok {
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Warn("Tracker.Associate(): %s already on %s.",
|
2014-12-31 13:17:46 +00:00
|
|
|
nk, ch)
|
2011-10-27 16:03:01 +00:00
|
|
|
return nil
|
2011-10-19 23:10:33 +00:00
|
|
|
}
|
|
|
|
cp := new(ChanPrivs)
|
|
|
|
ch.addNick(nk, cp)
|
|
|
|
nk.addChannel(ch, cp)
|
2014-12-31 13:17:46 +00:00
|
|
|
return cp.Copy()
|
2011-10-19 23:10:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2014-12-31 13:17:46 +00:00
|
|
|
func (st *stateTracker) Dissociate(c, n string) {
|
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
|
|
|
nk, nok := st.nicks[n]
|
|
|
|
ch, cok := st.chans[c]
|
|
|
|
|
|
|
|
if !cok {
|
2011-10-27 16:54:23 +00:00
|
|
|
// As we can implicitly delete both nicks and channels from being
|
|
|
|
// tracked by dissociating one from the other, we should verify that
|
|
|
|
// we're not being passed an old Nick or Channel.
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Error("Tracker.Dissociate(): channel %s not found in "+
|
2014-12-31 13:17:46 +00:00
|
|
|
"internal state.", c)
|
|
|
|
} else if !nok {
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Error("Tracker.Dissociate(): nick %s not found in "+
|
2014-12-31 13:17:46 +00:00
|
|
|
"internal state.", n)
|
|
|
|
} else if _, ok := nk.isOn(ch); !ok {
|
2013-02-16 11:29:56 +00:00
|
|
|
logging.Warn("Tracker.Dissociate(): %s not on %s.",
|
2014-12-31 13:17:46 +00:00
|
|
|
nk.nick, ch.name)
|
2011-10-27 16:03:01 +00:00
|
|
|
} else if nk == st.me {
|
2011-10-19 23:10:33 +00:00
|
|
|
// I'm leaving the channel for some reason, so it won't be tracked.
|
|
|
|
st.delChannel(ch)
|
2011-10-27 16:03:01 +00:00
|
|
|
} else {
|
2011-10-19 23:10:33 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-11-05 06:06:59 +00:00
|
|
|
|
|
|
|
func (st *stateTracker) String() string {
|
2014-12-31 13:17:46 +00:00
|
|
|
st.mu.Lock()
|
|
|
|
defer st.mu.Unlock()
|
2011-11-05 06:06:59 +00:00
|
|
|
str := "GoIRC Channels\n"
|
|
|
|
str += "--------------\n\n"
|
|
|
|
for _, ch := range st.chans {
|
|
|
|
str += ch.String() + "\n"
|
|
|
|
}
|
|
|
|
str += "GoIRC NickNames\n"
|
|
|
|
str += "---------------\n\n"
|
|
|
|
for _, n := range st.nicks {
|
|
|
|
if n != st.me {
|
|
|
|
str += n.String() + "\n"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
}
|