flokati/matrix.go

64 lines
1.3 KiB
Go

// vi:ts=4:sts=4:sw=4:noet:tw=72
//
// flokati
//
// Copyright (c) 2019 Andreas Neue <an@dnix.de>
package main
import (
"fmt"
"strings"
"time"
modules "git.dnix.de/an/flokati/modules"
"git.dnix.de/an/xlog"
"github.com/matrix-org/gomatrix"
)
func Matrix(say chan string) {
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)
}
}()
say <- fmt.Sprintf("%s\n%s", "*", SoftwareInfo())
for {
var targets string
line := strings.Split(<-say, "\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.Second)
}
}