2019-10-22 16:36:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-13 15:31:28 +00:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
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"
|
2023-11-11 21:10:23 +00:00
|
|
|
"github.com/zelenin/go-tdlib/client"
|
2019-11-19 20:25:14 +00:00
|
|
|
goxmpp "gosrc.io/xmpp"
|
2019-10-22 16:36:54 +00:00
|
|
|
)
|
|
|
|
|
2024-05-05 17:16:38 +00:00
|
|
|
var version string = "1.9.4"
|
2023-01-18 04:42:10 +00:00
|
|
|
var commit string
|
2022-04-22 23:07:22 +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() {
|
2023-01-18 04:42:10 +00:00
|
|
|
if commit != "" {
|
|
|
|
version = fmt.Sprintf("%v-%v", version, commit)
|
|
|
|
}
|
|
|
|
|
2020-01-13 15:31:28 +00:00
|
|
|
var profilingPort = flag.Int("profiling-port", 0, "The port for pprof server")
|
2022-01-27 06:57:46 +00:00
|
|
|
// 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")
|
2023-06-03 04:20:03 +00:00
|
|
|
// Folder for Badger DB of message ids
|
|
|
|
var idsPath = flag.String("ids", "ids", "Ids folder path")
|
2022-04-22 23:07:22 +00:00
|
|
|
var versionFlag = flag.Bool("version", false, "Print the version and exit")
|
2020-01-13 15:31:28 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2022-04-22 23:07:22 +00:00
|
|
|
if *versionFlag {
|
|
|
|
fmt.Printf("%v\n", version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2020-01-13 15:31:28 +00:00
|
|
|
if *profilingPort > 0 {
|
|
|
|
go func() {
|
|
|
|
log.Println(http.ListenAndServe(fmt.Sprintf("localhost:%v", *profilingPort), nil))
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:25:14 +00:00
|
|
|
cleanupDone = make(chan struct{})
|
|
|
|
sigintChannel = make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigintChannel, os.Interrupt)
|
|
|
|
|
2022-01-27 06:57:46 +00:00
|
|
|
config, err := config.ReadConfig(*configPath, *schemaPath)
|
2019-10-22 19:55:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-11-11 21:10:23 +00:00
|
|
|
client.SetLogVerbosityLevel(&client.SetLogVerbosityLevelRequest{
|
|
|
|
NewVerbosityLevel: stringToTdlibLogConstant(config.Telegram.Loglevel),
|
|
|
|
})
|
2019-11-05 00:09:07 +00:00
|
|
|
SetLogrusLevel(config.XMPP.Loglevel)
|
|
|
|
|
2023-01-18 04:42:10 +00:00
|
|
|
log.Infof("Starting telegabber version %v", version)
|
|
|
|
|
2023-06-03 04:20:03 +00:00
|
|
|
sm, component, err = xmpp.NewComponent(config.XMPP, config.Telegram, *idsPath)
|
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)
|
2019-12-22 01:04:45 +00:00
|
|
|
// bye
|
|
|
|
os.Exit(-1)
|
2019-11-19 20:25:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-11 21:10:23 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-11-19 20:25:14 +00:00
|
|
|
func exit() {
|
|
|
|
xmpp.Close(component)
|
|
|
|
close(cleanupDone)
|
2019-10-22 16:36:54 +00:00
|
|
|
}
|