You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
telegabber/telegabber.go

119 lines
2.4 KiB

package main
import (
4 years ago
"flag"
"fmt"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"dev.narayana.im/narayana/telegabber/config"
"dev.narayana.im/narayana/telegabber/xmpp"
log "github.com/sirupsen/logrus"
"github.com/zelenin/go-tdlib/client"
goxmpp "gosrc.io/xmpp"
)
1 week ago
var version string = "1.9.2"
var commit string
var sm *goxmpp.StreamManager
var component *goxmpp.Component
var err error
var cleanupDone chan struct{}
var sigintChannel chan os.Signal
func main() {
if commit != "" {
version = fmt.Sprintf("%v-%v", version, commit)
}
4 years ago
var profilingPort = flag.Int("profiling-port", 0, "The port for pprof server")
// YAML config, compatible with the format of Zhabogram 2.0.0
var configPath = flag.String("config", "config.yml", "Config file path")
// JSON schema (not for editing by a user)
var schemaPath = flag.String("schema", "./config_schema.json", "Schema file path")
// Folder for Badger DB of message ids
var idsPath = flag.String("ids", "ids", "Ids folder path")
var versionFlag = flag.Bool("version", false, "Print the version and exit")
4 years ago
flag.Parse()
if *versionFlag {
fmt.Printf("%v\n", version)
os.Exit(0)
}
4 years ago
if *profilingPort > 0 {
go func() {
log.Println(http.ListenAndServe(fmt.Sprintf("localhost:%v", *profilingPort), nil))
}()
}
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)
}
client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
NewVerbosityLevel: stringToTdlibLogConstant(config.Telegram.Loglevel),
})
SetLogrusLevel(config.XMPP.Loglevel)
log.Infof("Starting telegabber version %v", version)
sm, component, err = xmpp.NewComponent(config.XMPP, config.Telegram, *idsPath)
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)
// bye
os.Exit(-1)
}
}
var tdlibLogConstants = map[string]int32{
":fatal": 0,
":error": 1,
":warn": 2,
":info": 3,
":debug": 4,
":verbose": 5,
":all": 1023,
}
func stringToTdlibLogConstant(c string) int32 {
level, ok := tdlibLogConstants[c]
if !ok {
level = 0
}
return level
}
func exit() {
xmpp.Close(component)
close(cleanupDone)
}