diff --git a/state/channel.go b/state/channel.go index 9a96dbf..8631189 100644 --- a/state/channel.go +++ b/state/channel.go @@ -13,6 +13,7 @@ type Channel struct { Modes *ChanMode lookup map[string]*Nick nicks map[*Nick]*ChanPrivs + l logging.Logger } // A struct representing the modes of an IRC Channel @@ -90,12 +91,13 @@ func init() { * Channel methods for state management \******************************************************************************/ -func NewChannel(name string) *Channel { +func NewChannel(name string, l logging.Logger) *Channel { return &Channel{ Name: name, Modes: new(ChanMode), nicks: make(map[*Nick]*ChanPrivs), lookup: make(map[string]*Nick), + l: l, } } @@ -115,6 +117,8 @@ func (ch *Channel) addNick(nk *Nick, cp *ChanPrivs) { if _, ok := ch.nicks[nk]; !ok { ch.nicks[nk] = cp ch.lookup[nk.Nick] = nk + } else { + ch.l.Warn("Channel.addNick(): %s already on %s.", nk.Nick, ch.Name) } } @@ -123,6 +127,8 @@ func (ch *Channel) delNick(nk *Nick) { if _, ok := ch.nicks[nk]; ok { ch.nicks[nk] = nil, false ch.lookup[nk.Nick] = nil, false + } else { + ch.l.Warn("Channel.delNick(): %s not on %s.", nk.Nick, ch.Name) } } @@ -160,7 +166,7 @@ func (ch *Channel) ParseModes(modes string, modeargs []string) { } else if !modeop { ch.Modes.Key = "" } else { - logging.Warn("Channel.ParseModes(): not enough arguments to "+ + ch.l.Warn("Channel.ParseModes(): not enough arguments to "+ "process MODE %s %s%s", ch.Name, modestr, m) } case 'l': @@ -170,7 +176,7 @@ func (ch *Channel) ParseModes(modes string, modeargs []string) { } else if !modeop { ch.Modes.Limit = 0 } else { - logging.Warn("Channel.ParseModes(): not enough arguments to "+ + ch.l.Warn("Channel.ParseModes(): not enough arguments to "+ "process MODE %s %s%s", ch.Name, modestr, m) } case 'q', 'a', 'o', 'h', 'v': @@ -191,13 +197,15 @@ func (ch *Channel) ParseModes(modes string, modeargs []string) { } modeargs = modeargs[1:] } else { - logging.Warn("Channel.ParseModes(): untracked nick %s "+ + ch.l.Warn("Channel.ParseModes(): untracked nick %s "+ "recieved MODE on channel %s", modeargs[0], ch.Name) } } else { - logging.Warn("Channel.ParseModes(): not enough arguments to "+ + ch.l.Warn("Channel.ParseModes(): not enough arguments to "+ "process MODE %s %s%s", ch.Name, modestr, m) } + default: + ch.l.Info("Channel.ParseModes(): unknown mode char %c", m) } } } diff --git a/state/nick.go b/state/nick.go index 006c5ea..64ccf5f 100644 --- a/state/nick.go +++ b/state/nick.go @@ -11,6 +11,7 @@ type Nick struct { Modes *NickMode lookup map[string]*Channel chans map[*Channel]*ChanPrivs + l logging.Logger } // A struct representing the modes of an IRC Nick (User Modes) @@ -43,12 +44,13 @@ func init() { * Nick methods for state management \******************************************************************************/ -func NewNick(n string) *Nick { +func NewNick(n string, l logging.Logger) *Nick { return &Nick{ Nick: n, Modes: new(NickMode), chans: make(map[*Channel]*ChanPrivs), lookup: make(map[string]*Channel), + l: l, } } @@ -68,6 +70,8 @@ func (nk *Nick) addChannel(ch *Channel, cp *ChanPrivs) { if _, ok := nk.chans[ch]; !ok { nk.chans[ch] = cp nk.lookup[ch.Name] = ch + } else { + nk.l.Warn("Nick.addChannel(): %s already on %s.", nk.Nick, ch.Name) } } @@ -76,6 +80,8 @@ func (nk *Nick) delChannel(ch *Channel) { if _, ok := nk.chans[ch]; ok { nk.chans[ch] = nil, false nk.lookup[ch.Name] = nil, false + } else { + nk.l.Warn("Nick.delChannel(): %s not on %s.", nk.Nick, ch.Name) } } @@ -99,7 +105,7 @@ func (nk *Nick) ParseModes(modes string) { case 'z': nk.Modes.SSL = modeop default: - logging.Info("Nick.ParseModes(): unknown mode char %c", m) + nk.l.Info("Nick.ParseModes(): unknown mode char %c", m) } } } diff --git a/state/tracker.go b/state/tracker.go index 8c8062f..d1b5fe9 100644 --- a/state/tracker.go +++ b/state/tracker.go @@ -32,13 +32,17 @@ type stateTracker struct { // We need to keep state on who we are :-) me *Nick + + // For great logging justice. + l logging.Logger } // ... and finally a constructor to make it. -func NewTracker(mynick string) *stateTracker { +func NewTracker(mynick string, l logging.Logger) *stateTracker { st := &stateTracker{ chans: make(map[string]*Channel), nicks: make(map[string]*Nick), + l: l, } st.me = st.NewNick(mynick) return st @@ -52,10 +56,10 @@ func NewTracker(mynick string) *stateTracker { // can be properly tracked for state management purposes. func (st *stateTracker) NewNick(n string) *Nick { if _, ok := st.nicks[n]; ok { - logging.Warn("StateTracker.NewNick(): %s already tracked.", n) + st.l.Warn("StateTracker.NewNick(): %s already tracked.", n) return nil } - st.nicks[n] = NewNick(n) + st.nicks[n] = NewNick(n, st.l) return st.nicks[n] } @@ -82,10 +86,10 @@ func (st *stateTracker) ReNick(old, neu string) { ch.lookup[neu] = nk } } else { - logging.Warn("StateTracker.ReNick(): %s already exists.", neu) + st.l.Warn("StateTracker.ReNick(): %s already exists.", neu) } } else { - logging.Warn("StateTracker.ReNick(): %s not tracked.", old) + st.l.Warn("StateTracker.ReNick(): %s not tracked.", old) } } @@ -95,17 +99,17 @@ func (st *stateTracker) DelNick(n string) { if nk != st.me { st.delNick(nk) } else { - logging.Warn("StateTracker.DelNick(): won't delete myself.") + st.l.Warn("StateTracker.DelNick(): won't delete myself.") } } else { - logging.Warn("StateTracker.DelNick(): %s not tracked.", n) + st.l.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 :-(") + st.l.Error("StateTracker.DelNick(): TRYING TO DELETE ME :-(") return } st.nicks[nk.Nick] = nil, false @@ -115,8 +119,8 @@ func (st *stateTracker) delNick(nk *Nick) { 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) + st.l.Error("StateTracker.delNick(): deleting nick %s emptied "+ + "channel %s, this shouldn't happen!", nk.Nick, ch.Name) } } } @@ -125,10 +129,10 @@ func (st *stateTracker) delNick(nk *Nick) { // can be properly tracked for state management purposes. func (st *stateTracker) NewChannel(c string) *Channel { if _, ok := st.chans[c]; ok { - logging.Warn("StateTracker.NewChannel(): %s already tracked", c) + st.l.Warn("StateTracker.NewChannel(): %s already tracked.", c) return nil } - st.chans[c] = NewChannel(c) + st.chans[c] = NewChannel(c, st.l) return st.chans[c] } @@ -145,7 +149,7 @@ func (st *stateTracker) DelChannel(c string) { if ch, ok := st.chans[c]; ok { st.delChannel(ch) } else { - logging.Warn("StateTracker.DelChannel(): %s not tracked.", c) + st.l.Warn("StateTracker.DelChannel(): %s not tracked.", c) } } @@ -180,11 +184,11 @@ func (st *stateTracker) IsOn(c, n string) bool { // 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 :-(") + st.l.Error("StateTracker.Associate(): passed nil values :-(") return } if nk.IsOn(ch) { - logging.Warn("StateTracker.Associate(): %s already on %s.", + st.l.Warn("StateTracker.Associate(): %s already on %s.", nk.Nick, ch.Name) return } @@ -199,9 +203,9 @@ func (st *stateTracker) Associate(ch *Channel, nk *Nick) { func (st *stateTracker) Dissociate(ch *Channel, nk *Nick) { switch { case ch == nil || nk == nil: - logging.Error("StateTracker.Dissociate(): passed nil values :-(") + st.l.Error("StateTracker.Dissociate(): passed nil values :-(") case !nk.IsOn(ch): - logging.Warn("StateTracker.Dissociate(): %s not on %s.", + st.l.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.