flokati/matrix.go

66 lines
1.3 KiB
Go
Raw Normal View History

2019-08-01 08:50:13 +00:00
// vi:ts=4:sts=4:sw=4:noet:tw=72
//
// flokatimx
//
// Copyright (c) 2019 Andreas Neue <an@dnix.de>
package main
import (
"flokati/modules"
"fmt"
"strings"
"time"
"git.dnix.de/an/xlog"
"github.com/matrix-org/gomatrix"
)
func Matrix() {
cl, err := gomatrix.NewClient(*server, *name, *token)
fmt.Println(err)
syncer := cl.Syncer.(*gomatrix.DefaultSyncer)
syncer.OnEventType("m.room.message", func(ev *gomatrix.Event) {
fmt.Println("Message: ", ev)
if msgtype, ok := ev.MessageType(); ok && msgtype == "m.text" {
if body, ok := ev.Body(); ok {
fmt.Println(ev.Sender, ev.RoomID, body)
modules.HandleMessage(&modules.Message{From: ev.Sender, Channel: ev.RoomID, Text: body})
}
}
})
go func() {
for {
if err := cl.Sync(); err != nil {
fmt.Println("Sync() returned ", err)
}
time.Sleep(1 * time.Second)
}
}()
xlog.Info("%s started", SoftwareInfo())
sayCh <- fmt.Sprintf("%s\n%s", "*", SoftwareInfo())
for {
var targets string
line := strings.Split(<-sayCh, "\n")
if len(line) < 2 {
continue
}
if line[0] != "*" {
targets = line[0]
} else {
targets = *channels
}
xlog.Debug("Say: \"%s\" to [%s]", line[1], targets)
for _, tar := range strings.Split(targets, ",") {
cl.SendText(tar, line[1])
time.Sleep(1 * time.Second)
}
}
time.Sleep(1 * time.Millisecond)
}