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-15 16:11:58 -04:00
|
|
|
"github.com/spf13/viper"
|
2020-04-11 14:39:19 -04:00
|
|
|
"maunium.net/go/mautrix"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2020-04-15 16:11:58 -04:00
|
|
|
viper.SetConfigName("config")
|
|
|
|
viper.AddConfigPath("/etc/testbot")
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Fatal error config file: %v \n", err)
|
|
|
|
}
|
|
|
|
viper.SetConfigType("yaml")
|
2020-04-11 14:39:19 -04:00
|
|
|
c := &botlib.Config{
|
2020-04-15 16:11:58 -04:00
|
|
|
Userid: viper.GetString("userid"),
|
|
|
|
Server: viper.GetString("server"),
|
|
|
|
Token: viper.GetString("token"),
|
|
|
|
Name: viper.GetString("name"),
|
|
|
|
Owner: viper.GetString("owner"),
|
|
|
|
Prefix: viper.GetString("prefix"),
|
2020-04-11 14:39:19 -04:00
|
|
|
}
|
|
|
|
b := botlib.NewBot(c)
|
2020-04-11 16:26:59 -04:00
|
|
|
tb := &TestBot{
|
2020-04-21 16:18:09 -04:00
|
|
|
Bot: b,
|
|
|
|
Conf: c,
|
|
|
|
Jobs: make(chan *mautrix.Event, 100),
|
|
|
|
StateFile: viper.GetString("statefile"),
|
2020-04-11 16:26:59 -04:00
|
|
|
}
|
2020-04-14 16:54:31 -04:00
|
|
|
tb.InitActions()
|
2020-04-15 16:11:58 -04:00
|
|
|
tb.InitScheduler()
|
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")
|
2020-04-15 16:11:58 -04:00
|
|
|
err = b.LeaveEmptyRooms()
|
2020-04-11 14:39:19 -04:00
|
|
|
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
|
|
|
}
|