2019-10-22 16:36:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-11-19 20:25:14 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2019-10-22 16:36:54 +00:00
|
|
|
"dev.narayana.im/narayana/telegabber/config"
|
|
|
|
"dev.narayana.im/narayana/telegabber/xmpp"
|
2019-11-03 22:15:43 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
2019-11-19 20:25:14 +00:00
|
|
|
goxmpp "gosrc.io/xmpp"
|
2019-10-22 16:36:54 +00:00
|
|
|
)
|
|
|
|
|
2019-10-29 01:23:57 +00:00
|
|
|
// YAML config, compatible with the format of Zhabogram 2.0.0
|
|
|
|
const configPath string = "config.yml"
|
2019-11-03 22:15:43 +00:00
|
|
|
|
2019-10-29 01:23:57 +00:00
|
|
|
// JSON schema (not for editing by a user)
|
|
|
|
const schemaPath string = "./config_schema.json"
|
2019-10-22 16:36:54 +00:00
|
|
|
|
2019-11-19 20:25:14 +00:00
|
|
|
var sm *goxmpp.StreamManager
|
|
|
|
var component *goxmpp.Component
|
|
|
|
var err error
|
|
|
|
|
|
|
|
var cleanupDone chan struct{}
|
|
|
|
var sigintChannel chan os.Signal
|
|
|
|
|
2019-10-22 16:36:54 +00:00
|
|
|
func main() {
|
2019-11-19 20:25:14 +00:00
|
|
|
cleanupDone = make(chan struct{})
|
|
|
|
sigintChannel = make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigintChannel, os.Interrupt)
|
|
|
|
|
2019-10-29 01:23:57 +00:00
|
|
|
config, err := config.ReadConfig(configPath, schemaPath)
|
2019-10-22 19:55:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-11-05 00:09:07 +00:00
|
|
|
SetLogrusLevel(config.XMPP.Loglevel)
|
|
|
|
|
2019-11-19 20:25:14 +00:00
|
|
|
sm, component, err = xmpp.NewComponent(config.XMPP, config.Telegram)
|
2019-11-03 22:15:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2019-10-22 16:36:54 +00:00
|
|
|
|
2019-11-19 20:25:14 +00:00
|
|
|
go func() {
|
|
|
|
<-sigintChannel
|
|
|
|
log.Error("Interrupting...")
|
|
|
|
exit()
|
|
|
|
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
|
2019-10-22 16:36:54 +00:00
|
|
|
// reconnect automatically
|
2019-11-19 20:25:14 +00:00
|
|
|
err = sm.Run()
|
|
|
|
exit()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
<-cleanupDone
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func exit() {
|
|
|
|
xmpp.Close(component)
|
|
|
|
close(cleanupDone)
|
2019-10-22 16:36:54 +00:00
|
|
|
}
|