From 6fc0ac3c9d6a919b2c6308f0f7b204ec7f45ff99 Mon Sep 17 00:00:00 2001 From: Daniel Aberger Date: Fri, 26 Feb 2016 19:00:00 +0100 Subject: [PATCH 1/3] removed case sensitivity of module commands --- modules/coffee.go | 2 +- modules/twitch.go | 2 +- modules/weather.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/coffee.go b/modules/coffee.go index 03c51c9..2f604c0 100644 --- a/modules/coffee.go +++ b/modules/coffee.go @@ -24,7 +24,7 @@ func coffeeHandleMessage(m *irc.Message) { if len(tok) < 1 { return } - switch tok[0] { + switch strings.ToLower(tok[0]) { case "!kaffee": switch len(tok) { case 1: diff --git a/modules/twitch.go b/modules/twitch.go index 185d3d1..2ab6e57 100644 --- a/modules/twitch.go +++ b/modules/twitch.go @@ -146,7 +146,7 @@ func twitchHandleMessage(m *irc.Message) { if len(tok) < 1 { return } - switch tok[0] { + switch strings.ToLower(tok[0]) { case "!twitch": switch len(tok) { case 1: diff --git a/modules/weather.go b/modules/weather.go index 6815b9b..2c749ea 100644 --- a/modules/weather.go +++ b/modules/weather.go @@ -85,7 +85,7 @@ func weatherHandleMessage(m *irc.Message) { if len(tok) < 1 { return } - switch tok[0] { + switch strings.ToLower(tok[0]) { case "!weather", "!wetter": switch len(tok) { case 1: From 813939cddc5bc95b20a269b3494f032e03320240 Mon Sep 17 00:00:00 2001 From: Daniel Aberger Date: Tue, 15 Mar 2016 17:31:31 +0100 Subject: [PATCH 2/3] Improved coffee module --- modules/coffee.go | 97 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 86 insertions(+), 11 deletions(-) diff --git a/modules/coffee.go b/modules/coffee.go index ade3de3..9b22122 100644 --- a/modules/coffee.go +++ b/modules/coffee.go @@ -7,12 +7,26 @@ package modules import ( + "fmt" + "log" "strings" + "sync" "time" "code.dnix.de/an/xlog" "github.com/sorcix/irc" + "github.com/sorcix/irc/ctcp" +) + +type Receivers struct { + names map[string]bool + running bool + mux sync.Mutex +} + +var ( + r Receivers ) func init() { @@ -29,30 +43,91 @@ func coffeeHandleMessage(m *irc.Message) { case "!kaffee": switch len(tok) { case 1: - go coffeeMake(nil) + go r.addReceivers(nil) default: - go coffeeMake(tok[1:]) + go r.addReceivers(tok[1:]) } default: } } -func coffeeMake(rec []string) { - SayCh <- "*\nsetzt Kaffee auf." - time.Sleep(30 * time.Second) - SayCh <- "*\nstellt eine frische Kanne Kaffee in den Raum." - if rec != nil { +func (r *Receivers) addReceivers(newNames []string) { + r.mux.Lock() + if r.running { + SayCh <- "*\nEs wird gerade Kaffee zubereitet." + if newNames != nil { + r.addNames(newNames) + } + } else { + if newNames != nil { + r.addNames(newNames) + } + go makeCoffee() + } + r.mux.Unlock() +} + +func (r *Receivers) makeCoffee() { + printAction("setzt Kaffee auf.") + time.Sleep(time.Second * 30) + printAction("stellt eine frische Kanne Kaffee in den Raum.") + r.mux.Lock() + if len(r.names) != 0 { var users string - for i, v := range rec { - users += v - if i < len(rec)-2 { + for i, _ := range r.names { + users += i + /*if i < len(rec)-2 { users += ", " } else if i == len(rec)-2 { users += " und " - } + }*/ } SayCh <- "*\ngibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee." } time.Sleep(10 * time.Second) SayCh <- "*\nProst! (c)" + + r.names = make(map[string]bool) + r.running = false + + r.mux.Unlock() } + +func (r *Receivers) addNames(newNames []string) { + for _, v := range newNames { + r.names[v] = true + } +} + +func printAction(s string) { + msg := ctcp.Encode(ctcp.ACTION, fmt.Sprintf(s)) + SayCh <- fmt.Sprintf("%s\n%s", "*", msg) +} + +//func (r *Receivers) coffeeMake(rec []string) { +// if *running { +// // TODO: add new recipients to map +// // no if rec == nil add the sender, else add the sender and rec +// SayCh <- "*\nEs wird gerade Kaffee zubereitet." +// } else { +// printAction("setzt Kaffee auf.") +// time.Sleep(30 * time.Second) +// printAction("stellt eine frische Kanne Kaffee in den Raum.") + +// if rec != nil { +// var users string +// for i, v := range rec { +// users += v +// if i < len(rec)-2 { +// users += ", " +// } else if i == len(rec)-2 { +// users += " und " +// } +// } +// SayCh <- "*\ngibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee." +// } + +// time.Sleep(10 * time.Second) +// SayCh <- "*\nProst! (c)" +// } +//} From 3fcf3137993bbbf494c2929d878d871b85f10334 Mon Sep 17 00:00:00 2001 From: Gogs Date: Tue, 15 Mar 2016 18:05:55 +0100 Subject: [PATCH 3/3] Improved coffee module --- modules/coffee.go | 48 +++++++++++++---------------------------------- 1 file changed, 13 insertions(+), 35 deletions(-) diff --git a/modules/coffee.go b/modules/coffee.go index 9b22122..51698e7 100644 --- a/modules/coffee.go +++ b/modules/coffee.go @@ -8,7 +8,6 @@ package modules import ( "fmt" - "log" "strings" "sync" "time" @@ -31,6 +30,7 @@ var ( func init() { MsgHandlers["coffee"] = coffeeHandleMessage + r.names = make(map[string]bool) xlog.Info("Coffee module initialized") } @@ -54,7 +54,6 @@ func coffeeHandleMessage(m *irc.Message) { func (r *Receivers) addReceivers(newNames []string) { r.mux.Lock() if r.running { - SayCh <- "*\nEs wird gerade Kaffee zubereitet." if newNames != nil { r.addNames(newNames) } @@ -62,34 +61,40 @@ func (r *Receivers) addReceivers(newNames []string) { if newNames != nil { r.addNames(newNames) } - go makeCoffee() + go r.makeCoffee() } r.mux.Unlock() } func (r *Receivers) makeCoffee() { + r.mux.Lock() + r.running = true + r.mux.Unlock() printAction("setzt Kaffee auf.") time.Sleep(time.Second * 30) printAction("stellt eine frische Kanne Kaffee in den Raum.") r.mux.Lock() if len(r.names) != 0 { var users string + var count int = 0 for i, _ := range r.names { + count++ users += i - /*if i < len(rec)-2 { + if count < len(r.names)-1 { users += ", " - } else if i == len(rec)-2 { + } else if count == len(r.names)-1 { users += " und " - }*/ + } } - SayCh <- "*\ngibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee." + printAction("gibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee.") } + r.mux.Unlock() time.Sleep(10 * time.Second) SayCh <- "*\nProst! (c)" + r.mux.Lock() r.names = make(map[string]bool) r.running = false - r.mux.Unlock() } @@ -104,30 +109,3 @@ func printAction(s string) { SayCh <- fmt.Sprintf("%s\n%s", "*", msg) } -//func (r *Receivers) coffeeMake(rec []string) { -// if *running { -// // TODO: add new recipients to map -// // no if rec == nil add the sender, else add the sender and rec -// SayCh <- "*\nEs wird gerade Kaffee zubereitet." -// } else { -// printAction("setzt Kaffee auf.") -// time.Sleep(30 * time.Second) -// printAction("stellt eine frische Kanne Kaffee in den Raum.") - -// if rec != nil { -// var users string -// for i, v := range rec { -// users += v -// if i < len(rec)-2 { -// users += ", " -// } else if i == len(rec)-2 { -// users += " und " -// } -// } -// SayCh <- "*\ngibt " + users + " einen frischen, richtig schwarzen, richtig leckeren Kaffee." -// } - -// time.Sleep(10 * time.Second) -// SayCh <- "*\nProst! (c)" -// } -//}