diff options
Diffstat (limited to 'telegabber.go')
-rw-r--r-- | telegabber.go | 38 |
1 files changed, 36 insertions, 2 deletions
diff --git a/telegabber.go b/telegabber.go index 8c098e1..a196cc1 100644 --- a/telegabber.go +++ b/telegabber.go @@ -1,10 +1,14 @@ 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 @@ -13,7 +17,18 @@ 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) @@ -21,11 +36,30 @@ func main() { SetLogrusLevel(config.XMPP.Loglevel) - cm, err := xmpp.NewComponent(config.XMPP, config.Telegram) + 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 - log.Fatal(cm.Run()) + err = sm.Run() + exit() + + if err != nil { + <-cleanupDone + log.Fatal(err) + } +} + +func exit() { + xmpp.Close(component) + close(cleanupDone) } |