2020-04-11 14:39:19 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2020-04-11 16:26:59 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
2020-04-11 14:39:19 -04:00
|
|
|
|
2020-04-11 16:26:59 -04:00
|
|
|
"git.saintnet.tech/stryan/vega/botlib"
|
2020-04-11 14:39:19 -04:00
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
c := &botlib.Config{
|
|
|
|
Userid: "@testbot:saintnet.tech",
|
|
|
|
Server: "matrix.saintnet.tech",
|
|
|
|
Token: "MDAxYmxvY2F0aW9uIHNhaW50bmV0LnRlY2gKMDAxM2lkZW50aWZpZXIga2V5CjAwMTBjaWQgZ2VuID0gMQowMDI5Y2lkIHVzZXJfaWQgPSBAdGVzdGJvdDpzYWludG5ldC50ZWNoCjAwMTZjaWQgdHlwZSA9IGFjY2VzcwowMDIxY2lkIG5vbmNlID0gemlKZGs9bmJhcjp1eThIXgowMDJmc2lnbmF0dXJlIE4x0sbUS9lN-fi0KjJmEmpx6_wpYzgvk4k2Eugtkva7Cg",
|
|
|
|
Name: "TestBot",
|
|
|
|
Owner: "@stryan:saintnet.tech",
|
2020-04-11 16:26:59 -04:00
|
|
|
Prefix: "!tb",
|
2020-04-11 14:39:19 -04:00
|
|
|
}
|
|
|
|
b := botlib.NewBot(c)
|
2020-04-11 16:26:59 -04:00
|
|
|
tb := &TestBot{
|
|
|
|
Bot: b,
|
|
|
|
Conf: c,
|
|
|
|
Jobs: make(chan *mautrix.Event, 100),
|
|
|
|
}
|
2020-04-14 16:54:31 -04:00
|
|
|
tb.InitActions()
|
2020-04-11 14:39:19 -04:00
|
|
|
syncer := botlib.NewCustomSyncer("@testbot:saintnet.tech", b.Client.Store)
|
2020-04-11 16:26:59 -04:00
|
|
|
syncer.OnEventType(mautrix.EventMessage, tb.QueueMessage)
|
|
|
|
syncer.OnEventType(mautrix.StateMember, b.DefaultHandleMember)
|
2020-04-11 14:39:19 -04:00
|
|
|
|
|
|
|
b.Client.Syncer = syncer
|
2020-04-11 16:26:59 -04:00
|
|
|
|
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
|
2020-04-11 14:39:19 -04:00
|
|
|
go func() {
|
2020-04-11 16:26:59 -04:00
|
|
|
wg.Add(1)
|
2020-04-11 14:39:19 -04:00
|
|
|
err := b.Client.Sync()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-11 16:26:59 -04:00
|
|
|
wg.Done()
|
2020-04-11 14:39:19 -04:00
|
|
|
}()
|
|
|
|
log.Println("Syncing enabled")
|
|
|
|
err := b.LeaveEmptyRooms()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-11 16:26:59 -04:00
|
|
|
tb.LoadState()
|
|
|
|
_, err = b.CreateManagementRoom()
|
2020-04-11 14:39:19 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-04-11 16:26:59 -04:00
|
|
|
log.Println("Begining main phase")
|
|
|
|
cancelChan := make(chan bool)
|
2020-04-11 17:24:47 -04:00
|
|
|
for i := 0; i < 4; i++ {
|
2020-04-11 16:26:59 -04:00
|
|
|
go func() {
|
|
|
|
wg.Add(1)
|
|
|
|
tb.MessageHandler(cancelChan)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2020-04-11 14:39:19 -04:00
|
|
|
}
|
2020-04-11 16:26:59 -04:00
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
sig := <-sigs
|
|
|
|
log.Printf("Received sig %v", sig)
|
|
|
|
cancelChan <- true
|
|
|
|
tb.Shutdown()
|
|
|
|
}()
|
2020-04-11 14:39:19 -04:00
|
|
|
|
2020-04-11 16:26:59 -04:00
|
|
|
wg.Wait()
|
2020-04-11 14:39:19 -04:00
|
|
|
}
|