telegabber/telegabber.go
2019-11-19 22:25:14 +02:00

66 lines
1.1 KiB
Go

package main
import (
"os"
"os/signal"
"dev.narayana.im/narayana/telegabber/config"
"dev.narayana.im/narayana/telegabber/xmpp"
log "github.com/sirupsen/logrus"
goxmpp "gosrc.io/xmpp"
)
// YAML config, compatible with the format of Zhabogram 2.0.0
const configPath string = "config.yml"
// JSON schema (not for editing by a user)
const schemaPath string = "./config_schema.json"
var sm *goxmpp.StreamManager
var component *goxmpp.Component
var err error
var cleanupDone chan struct{}
var sigintChannel chan os.Signal
func main() {
cleanupDone = make(chan struct{})
sigintChannel = make(chan os.Signal, 1)
signal.Notify(sigintChannel, os.Interrupt)
config, err := config.ReadConfig(configPath, schemaPath)
if err != nil {
log.Fatal(err)
}
SetLogrusLevel(config.XMPP.Loglevel)
sm, component, err = xmpp.NewComponent(config.XMPP, config.Telegram)
if err != nil {
log.Fatal(err)
}
go func() {
<-sigintChannel
log.Error("Interrupting...")
exit()
os.Exit(0)
}()
// reconnect automatically
err = sm.Run()
exit()
if err != nil {
<-cleanupDone
log.Fatal(err)
}
}
func exit() {
xmpp.Close(component)
close(cleanupDone)
}