forked from an/flokati
		
	Merge branch 'master' of sikk/flokatirc into master
This commit is contained in:
		
						commit
						f734fd9c37
					
				
					 3 changed files with 68 additions and 15 deletions
				
			
		| 
						 | 
					@ -7,16 +7,30 @@
 | 
				
			||||||
package modules
 | 
					package modules
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
						"fmt"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
						"sync"
 | 
				
			||||||
	"time"
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"code.dnix.de/an/xlog"
 | 
						"code.dnix.de/an/xlog"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/sorcix/irc"
 | 
						"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() {
 | 
					func init() {
 | 
				
			||||||
	MsgHandlers["coffee"] = coffeeHandleMessage
 | 
						MsgHandlers["coffee"] = coffeeHandleMessage
 | 
				
			||||||
 | 
						r.names = make(map[string]bool)
 | 
				
			||||||
	xlog.Info("Coffee module initialized")
 | 
						xlog.Info("Coffee module initialized")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,34 +39,73 @@ func coffeeHandleMessage(m *irc.Message) {
 | 
				
			||||||
	if len(tok) < 1 {
 | 
						if len(tok) < 1 {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	switch tok[0] {
 | 
						switch strings.ToLower(tok[0]) {
 | 
				
			||||||
	case "!kaffee":
 | 
						case "!kaffee":
 | 
				
			||||||
		switch len(tok) {
 | 
							switch len(tok) {
 | 
				
			||||||
		case 1:
 | 
							case 1:
 | 
				
			||||||
			go coffeeMake(nil)
 | 
								go r.addReceivers(nil)
 | 
				
			||||||
		default:
 | 
							default:
 | 
				
			||||||
			go coffeeMake(tok[1:])
 | 
								go r.addReceivers(tok[1:])
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	default:
 | 
						default:
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func coffeeMake(rec []string) {
 | 
					func (r *Receivers) addReceivers(newNames []string) {
 | 
				
			||||||
	SayCh <- "*\nsetzt Kaffee auf."
 | 
						r.mux.Lock()
 | 
				
			||||||
	time.Sleep(30 * time.Second)
 | 
						if r.running {
 | 
				
			||||||
	SayCh <- "*\nstellt eine frische Kanne Kaffee in den Raum."
 | 
							if newNames != nil {
 | 
				
			||||||
	if rec != nil {
 | 
								r.addNames(newNames)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							if newNames != nil {
 | 
				
			||||||
 | 
								r.addNames(newNames)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							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 users string
 | 
				
			||||||
		for i, v := range rec {
 | 
							var count int = 0
 | 
				
			||||||
			users += v
 | 
							for i, _ := range r.names {
 | 
				
			||||||
			if i < len(rec)-2 {
 | 
								count++
 | 
				
			||||||
 | 
								users += i
 | 
				
			||||||
 | 
								if count < len(r.names)-1 {
 | 
				
			||||||
				users += ", "
 | 
									users += ", "
 | 
				
			||||||
			} else if i == len(rec)-2 {
 | 
								} else if count == len(r.names)-1 {
 | 
				
			||||||
				users += " und "
 | 
									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)
 | 
						time.Sleep(10 * time.Second)
 | 
				
			||||||
	SayCh <- "*\nProst! (c)"
 | 
						SayCh <- "*\nProst! (c)"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						r.mux.Lock()
 | 
				
			||||||
 | 
						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)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -147,7 +147,7 @@ func twitchHandleMessage(m *irc.Message) {
 | 
				
			||||||
	if len(tok) < 1 {
 | 
						if len(tok) < 1 {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	switch tok[0] {
 | 
						switch strings.ToLower(tok[0]) {
 | 
				
			||||||
	case "!twitch":
 | 
						case "!twitch":
 | 
				
			||||||
		switch len(tok) {
 | 
							switch len(tok) {
 | 
				
			||||||
		case 1:
 | 
							case 1:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -86,7 +86,7 @@ func weatherHandleMessage(m *irc.Message) {
 | 
				
			||||||
	if len(tok) < 1 {
 | 
						if len(tok) < 1 {
 | 
				
			||||||
		return
 | 
							return
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	switch tok[0] {
 | 
						switch strings.ToLower(tok[0]) {
 | 
				
			||||||
	case "!weather", "!wetter":
 | 
						case "!weather", "!wetter":
 | 
				
			||||||
		switch len(tok) {
 | 
							switch len(tok) {
 | 
				
			||||||
		case 1:
 | 
							case 1:
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue