aboutsummaryrefslogtreecommitdiff
path: root/telegabber.go
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-11-19 23:25:14 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-11-19 23:25:14 +0300
commitf0c0d0ba94c923ca06b49211c70db47616e5a8e1 (patch)
tree23c097b6cb8249d4f978f7b22bd1b9877b0a5bba /telegabber.go
parenta09817976ee5cdb7d1bca25c80cdef3d1b7867f7 (diff)
Save sessions on exit
Diffstat (limited to 'telegabber.go')
-rw-r--r--telegabber.go38
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)
}