mmflokati/main.go

99 lines
2.0 KiB
Go
Raw Normal View History

2017-06-28 10:23:37 +00:00
// vi:ts=4:sts=4:sw=4:noet:tw=72
//
2017-07-01 22:50:47 +00:00
// mmflokati
2017-06-28 10:23:37 +00:00
//
2017-07-01 22:50:47 +00:00
// Copyright (c) 2015,2016,2017 Andreas Neue <an@dnix.de>
2017-06-28 10:23:37 +00:00
package main
import (
"flag"
2017-06-28 11:07:36 +00:00
"fmt"
"runtime"
2017-06-28 10:23:37 +00:00
"strings"
"time"
2017-06-28 11:07:36 +00:00
"code.dnix.de/an/flokatilib/modules"
2017-06-28 10:23:37 +00:00
"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")
2017-06-28 10:23:37 +00:00
)
func init() {
flag.Parse()
}
var (
sayCh chan string
)
func main() {
sayCh = make(chan string, 1024)
xlog.Init(xlog.DEBUG)
2017-06-28 11:07:36 +00:00
xlog.Info("%s started", SoftwareInfo())
2017-06-28 10:23:37 +00:00
xlog.Info("Connecting ...")
bot := matterclient.New(*name, *pass, *team, *server)
2017-07-01 22:50:47 +00:00
//bot.SetLogLevel("debug")
2017-06-28 10:23:37 +00:00
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))
}
2017-06-28 10:23:37 +00:00
xlog.Info("Connected")
2017-06-28 11:07:36 +00:00
modules.Init(sayCh, *mods)
modules.BotNick = *name
2017-06-28 10:23:37 +00:00
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)
2017-06-28 10:23:37 +00:00
}
}()
go func() {
for {
mm := <-bot.MessageChan
2017-07-01 22:50:47 +00:00
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})
2017-06-28 10:23:37 +00:00
}
}()
bot.StatusLoop()
2017-06-28 10:23:37 +00:00
}
2017-06-28 11:07:36 +00:00
func SoftwareInfo() string {
2017-12-06 18:31:55 +00:00
return fmt.Sprintf("mmflokati %s-%s (built %s [%s])", FlokatiVersion, FlokatiBuild, FlokatiBuilddate, runtime.Version())
2017-06-28 11:07:36 +00:00
}