98 lines
		
	
	
	
		
			2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			98 lines
		
	
	
	
		
			2 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
// vi:ts=4:sts=4:sw=4:noet:tw=72
 | 
						|
//
 | 
						|
// mmflokati
 | 
						|
//
 | 
						|
// Copyright (c) 2015,2016,2017 Andreas Neue <an@dnix.de>
 | 
						|
 | 
						|
package main
 | 
						|
 | 
						|
import (
 | 
						|
	"flag"
 | 
						|
	"fmt"
 | 
						|
	"runtime"
 | 
						|
	"strings"
 | 
						|
	"time"
 | 
						|
 | 
						|
	"code.dnix.de/an/flokatilib/modules"
 | 
						|
	"code.dnix.de/an/xlog"
 | 
						|
 | 
						|
	"github.com/42wim/matterbridge/matterclient"
 | 
						|
)
 | 
						|
 | 
						|
var (
 | 
						|
	name     = flag.String("name", "matterbot", "Bot name")
 | 
						|
	pass     = flag.String("pass", "", "Password")
 | 
						|
	team     = flag.String("team", "", "Team name")
 | 
						|
	server   = flag.String("server", "mm.example.com", "Server address")
 | 
						|
	channels = flag.String("channels", "", "Channels to join")
 | 
						|
	mods     = flag.String("mods", "", "Modules to load")
 | 
						|
)
 | 
						|
 | 
						|
func init() {
 | 
						|
	flag.Parse()
 | 
						|
}
 | 
						|
 | 
						|
var (
 | 
						|
	sayCh chan string
 | 
						|
)
 | 
						|
 | 
						|
func main() {
 | 
						|
	sayCh = make(chan string, 1024)
 | 
						|
 | 
						|
	xlog.Init(xlog.DEBUG)
 | 
						|
 | 
						|
	xlog.Info("%s started", SoftwareInfo())
 | 
						|
	xlog.Info("Connecting ...")
 | 
						|
 | 
						|
	bot := matterclient.New(*name, *pass, *team, *server)
 | 
						|
	//bot.SetLogLevel("debug")
 | 
						|
	err := bot.Login()
 | 
						|
	if err != nil {
 | 
						|
		xlog.Error(err.Error())
 | 
						|
	}
 | 
						|
	teamId := bot.GetTeamId()
 | 
						|
	for _, ch := range strings.Split(*channels, ",") {
 | 
						|
		xlog.Debug("Joining %s", ch)
 | 
						|
		bot.JoinChannel(bot.GetChannelId(ch, teamId))
 | 
						|
	}
 | 
						|
 | 
						|
	xlog.Info("Connected")
 | 
						|
 | 
						|
	modules.Init(sayCh, *mods)
 | 
						|
	modules.BotNick = *name
 | 
						|
 | 
						|
	sayCh <- "town-square\n" + SoftwareInfo()
 | 
						|
 | 
						|
	go func() {
 | 
						|
		for {
 | 
						|
			var targets string
 | 
						|
			line := strings.Split(<-sayCh, "\n")
 | 
						|
			if len(line) < 2 {
 | 
						|
				continue
 | 
						|
			}
 | 
						|
			targets = line[0]
 | 
						|
			for _, tar := range strings.Split(targets, ",") {
 | 
						|
				chId := bot.GetChannelId(tar, teamId)
 | 
						|
				bot.PostMessage(chId, line[1])
 | 
						|
			}
 | 
						|
			time.Sleep(1 * time.Second)
 | 
						|
		}
 | 
						|
	}()
 | 
						|
 | 
						|
	go func() {
 | 
						|
		for {
 | 
						|
			mm := <-bot.MessageChan
 | 
						|
			if mm.Username == *name {
 | 
						|
				continue
 | 
						|
			}
 | 
						|
			xlog.Debug("Got msg: User: %v, Channel: %v, Text: \"%v\"", mm.Username, mm.Channel, mm.Text)
 | 
						|
			modules.HandleMessage(&modules.Message{From: mm.Username, Channel: mm.Channel, Text: mm.Text})
 | 
						|
		}
 | 
						|
	}()
 | 
						|
 | 
						|
	bot.StatusLoop()
 | 
						|
}
 | 
						|
 | 
						|
func SoftwareInfo() string {
 | 
						|
	return fmt.Sprintf("mmflokati %s-%s (built %s [%s])", FlokatiVersion, FlokatiBuild, FlokatiBuilddate, runtime.Version())
 | 
						|
}
 |