2020-04-11 16:26:59 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (t *TestBot) QueueMessage(e *mautrix.Event) {
|
|
|
|
t.Jobs <- e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestBot) Message(roomid, message string) error {
|
|
|
|
_, err := t.Bot.Client.SendText(roomid, message)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-15 16:11:58 -04:00
|
|
|
func (t *TestBot) TestMessage() {
|
|
|
|
_, err := t.Bot.Client.SendText(t.Bot.ManagementRoomID, "TestBot Online!")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 16:26:59 -04:00
|
|
|
func (t *TestBot) HandleMessage(e *mautrix.Event) {
|
|
|
|
if e.Sender == t.Conf.Userid {
|
|
|
|
return //we don't care about our own messages
|
|
|
|
}
|
2020-04-15 16:11:58 -04:00
|
|
|
log.Printf("Handling room %v message:'%v'\n", e.RoomID, e.Content.Body)
|
2020-04-11 16:26:59 -04:00
|
|
|
body := e.Content.Body
|
|
|
|
body_s := strings.Split(body, " ")
|
|
|
|
|
|
|
|
if body_s[0] != t.Conf.Prefix {
|
|
|
|
return //disregard non commands
|
|
|
|
}
|
|
|
|
if len(body_s) < 2 {
|
|
|
|
return //nothing to parse
|
|
|
|
}
|
2020-04-14 16:54:31 -04:00
|
|
|
if body_s[1] == "stop" {
|
2020-04-11 16:26:59 -04:00
|
|
|
err := t.Message(e.RoomID, "Shutting down...")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
t.Shutdown()
|
|
|
|
return
|
|
|
|
}
|
2020-04-14 16:54:31 -04:00
|
|
|
response, found := ActionList[body_s[1]]
|
|
|
|
if !found {
|
|
|
|
response = func(inputs ...string) string { return "Command not found" }
|
|
|
|
}
|
|
|
|
msg := response(body_s[1:]...)
|
|
|
|
err := t.Message(e.RoomID, msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return
|
2020-04-11 16:26:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TestBot) MessageHandler(cancelChan <-chan bool) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-cancelChan:
|
|
|
|
return
|
|
|
|
case event := <-t.Jobs:
|
|
|
|
t.HandleMessage(event)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|