diff --git a/util/config.go b/util/config.go index fca0893..b468653 100644 --- a/util/config.go +++ b/util/config.go @@ -30,7 +30,8 @@ func LoadConfig(mm *MessageManager) { o, _ := j.GetObjectArray("options") // Add the "Stop" listener - mm.listeners = append(mm.listeners, func(i *Message) bool { + AddListener(func(i *Message) bool { + //listeners = append(mm.listeners, func(i *Message) bool { if i.User.IsOp && i.Text == "!stop\n" { mm.Output("stop") return true @@ -45,9 +46,11 @@ func LoadConfig(mm *MessageManager) { c.FeatureTPHome = opt_enabled // Add the listener for "!set home" // Add !set home listener - mm.listeners = append(mm.listeners, func(i *Message) bool { + AddListener(func(i *Message) bool { + // mm.listeners = append(mm.listeners, func(i *Message) bool { if i.User.Name != "" && i.Text == "!set home\n" { - mm.tempListeners = append(mm.tempListeners, func(inp *Message) bool { + AddTempListener(func(inp *Message) bool { + //mm.tempListeners = append(mm.tempListeners, func(inp *Message) bool { listen_for := "Teleported " + i.User.Name + " to " if inp.User.Name == "" && strings.Contains(inp.Text, listen_for) { // Found the text @@ -68,7 +71,8 @@ func LoadConfig(mm *MessageManager) { return false }) // Add !home listener - mm.listeners = append(mm.listeners, func(i *Message) bool { + AddListener(func(i *Message) bool { + //mm.listeners = append(mm.listeners, func(i *Message) bool { if i.User.Name != "" && i.Text == "!home\n" { home_str, found := GetHome(i.User.Name) if found { diff --git a/util/message_manager.go b/util/message_manager.go index 7520f41..5f2bd99 100644 --- a/util/message_manager.go +++ b/util/message_manager.go @@ -31,6 +31,9 @@ type MessageManager struct { output io.WriteCloser } +var Listeners []func(inp *Message) bool +var TempListeners []func(inp *Message) bool + func (mm MessageManager) Output(o string) { if !strings.HasSuffix("\n", o) { o = o + "\n" @@ -41,25 +44,23 @@ func (mm MessageManager) Output(o string) { func (mm MessageManager) ProcessMessage(inp string) bool { // First of all, create the message from inp m := NewMessage(inp) - tempListeners := mm.tempListeners - listeners := mm.listeners - fmt.Printf("[%d]l, [%d]tmpl: Processing Message: >>> %s\n", len(listeners), len(tempListeners), strings.Replace(inp, "\n", "", -1)) + fmt.Printf("[%d]l, [%d]tmpl: Processing Message: >>> %s\n", len(Listeners), len(TempListeners), strings.Replace(inp, "\n", "", -1)) // Now run the message through all of mm's tempListeners - for i := range tempListeners { + for i := range TempListeners { fmt.Printf("Processing message in tempListener %d\n", i) // Pop the listener off of the stack - consumed := tempListeners[i](m) + consumed := TempListeners[i](m) if consumed { // When a temp listener is consumed, we delete it fmt.Println("Consumed Event from Temporary Listener.") - mm.RemoveTempListener(i) + RemoveTempListener(i) return true } } // and run the message through all of mm's listeners - for i := range listeners { + for i := range Listeners { fmt.Printf("Processing message in listener %d\n", i) - consumed := listeners[i](m) + consumed := Listeners[i](m) if consumed { return true } @@ -68,25 +69,26 @@ func (mm MessageManager) ProcessMessage(inp string) bool { return false } -func (mm MessageManager) AddListener(l func(*Message) bool) { - mm.listeners = append(mm.listeners, l) +func AddListener(l func(*Message) bool) { + Listeners = append(Listeners, l) } -func (mm MessageManager) AddTempListener(l func(*Message) bool) { - mm.tempListeners = append(mm.tempListeners, l) +func AddTempListener(l func(*Message) bool) { + TempListeners = append(TempListeners, l) } -func (mm MessageManager) RemoveListener(i int) { - if i > -1 && i < len(mm.listeners) { - mm.listeners = append(mm.listeners[:i], mm.listeners[i+1:]...) +func RemoveListener(i int) { + if i > -1 && i < len(Listeners) { + t := append(Listeners[:i], Listeners[i+1:]...) + Listeners = make(([]func(*Message) bool), len(t)) + copy(LIsteners, t) } } -func (mm MessageManager) RemoveTempListener(i int) { - if i > -1 && i < len(mm.tempListeners) { - fmt.Printf("Removing Temp Listener %d\n", i) - fmt.Printf("Before: %d\n", len(mm.tempListeners)) - mm.tempListeners = append(mm.tempListeners[:i], mm.tempListeners[i+1:]...) - fmt.Printf("After: %d\n", len(mm.tempListeners)) +func RemoveTempListener(i int) { + if i > -1 && i < len(TempListeners) { + t := append(TempListeners[:i], TempListeners[i+1:]...) + TempListeners = make(([]func(*Message) bool), len(t)) + copy(TempListeners, t) } } @@ -94,8 +96,7 @@ func NewManager(o io.WriteCloser) MessageManager { mm := MessageManager{ // listeners: make(([]func(*Message) bool), 0, 10), // tempListeners: make(([]func(*Message) bool), 0, 10), - finishedTempListener: -1, - output: o, + output: o, } return mm }