Add database models and improve config/main
This commit is contained in:
123
main.go
123
main.go
@ -1,4 +1,4 @@
|
||||
// mautrix-whatsapp - A Matrix-Whatsapp puppeting bridge.
|
||||
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
||||
// Copyright (C) 2018 Tulir Asokan
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
@ -24,9 +24,130 @@ import (
|
||||
"bufio"
|
||||
"encoding/gob"
|
||||
"github.com/mdp/qrterminal"
|
||||
"maunium.net/go/mautrix-whatsapp/config"
|
||||
flag "maunium.net/go/mauflag"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"maunium.net/go/mautrix-appservice"
|
||||
log "maunium.net/go/maulogger"
|
||||
"maunium.net/go/mautrix-whatsapp/database"
|
||||
)
|
||||
|
||||
var configPath = flag.MakeFull("c", "config", "The path to your config file.", "config.yaml").String()
|
||||
var registrationPath = flag.MakeFull("r", "registration", "The path where to save the appservice registration.", "registration.yaml").String()
|
||||
var generateRegistration = flag.MakeFull("g", "generate-registration", "Generate registration and quit.", "false").Bool()
|
||||
var wantHelp, _ = flag.MakeHelpFlag()
|
||||
|
||||
func (bridge *Bridge) GenerateRegistration() {
|
||||
reg, err := bridge.Config.NewRegistration()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Failed to generate registration:", err)
|
||||
os.Exit(20)
|
||||
}
|
||||
|
||||
err = reg.Save(*registrationPath)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Failed to save registration:", err)
|
||||
os.Exit(21)
|
||||
}
|
||||
|
||||
err = bridge.Config.Save(*configPath)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Failed to save config:", err)
|
||||
os.Exit(22)
|
||||
}
|
||||
fmt.Println("Registration generated. Add the path to the registration to your Synapse config restart it, then start the bridge.")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
type Bridge struct {
|
||||
AppService *appservice.AppService
|
||||
Config *config.Config
|
||||
DB *database.Database
|
||||
Log *log.Logger
|
||||
|
||||
MatrixListener *MatrixListener
|
||||
}
|
||||
|
||||
func NewBridge() *Bridge {
|
||||
bridge := &Bridge{}
|
||||
var err error
|
||||
bridge.Config, err = config.Load(*configPath)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Failed to load config:", err)
|
||||
os.Exit(10)
|
||||
}
|
||||
return bridge
|
||||
}
|
||||
|
||||
func (bridge *Bridge) Init() {
|
||||
var err error
|
||||
bridge.AppService, err = bridge.Config.MakeAppService()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Failed to initialize AppService:", err)
|
||||
os.Exit(11)
|
||||
}
|
||||
bridge.AppService.Init()
|
||||
bridge.Log = bridge.AppService.Log.Parent
|
||||
log.DefaultLogger = bridge.Log
|
||||
bridge.AppService.Log = log.CreateSublogger("Matrix", log.LevelDebug)
|
||||
|
||||
bridge.DB, err = database.New(bridge.Config.AppService.Database.URI)
|
||||
if err != nil {
|
||||
bridge.Log.Fatalln("Failed to initialize database:", err)
|
||||
os.Exit(12)
|
||||
}
|
||||
|
||||
bridge.MatrixListener = NewMatrixListener(bridge)
|
||||
}
|
||||
|
||||
func (bridge *Bridge) Start() {
|
||||
bridge.AppService.Start()
|
||||
bridge.MatrixListener.Start()
|
||||
}
|
||||
|
||||
func (bridge *Bridge) Stop() {
|
||||
bridge.AppService.Stop()
|
||||
bridge.MatrixListener.Stop()
|
||||
}
|
||||
|
||||
func (bridge *Bridge) Main() {
|
||||
if *generateRegistration {
|
||||
bridge.GenerateRegistration()
|
||||
return
|
||||
}
|
||||
|
||||
bridge.Init()
|
||||
bridge.Log.Infoln("Bridge initialization complete, starting...")
|
||||
bridge.Start()
|
||||
bridge.Log.Infoln("Bridge started!")
|
||||
|
||||
c := make(chan os.Signal)
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
<-c
|
||||
|
||||
bridge.Log.Infoln("Interrupt received, stopping...")
|
||||
bridge.Stop()
|
||||
bridge.Log.Infoln("Bridge stopped.")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
func main() {
|
||||
flag.SetHelpTitles("mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.", "[-h] [-c <path>] [-r <path>] [-g]")
|
||||
err := flag.Parse()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
flag.PrintHelp()
|
||||
os.Exit(1)
|
||||
} else if *wantHelp {
|
||||
flag.PrintHelp()
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
NewBridge().Main()
|
||||
}
|
||||
|
||||
func temp() {
|
||||
wac, err := whatsapp.NewConn(20 * time.Second)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
Reference in New Issue
Block a user