blob: 14b3b3d1c4e38fd5611a60bba675f2d5e992ef57 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package main
import (
"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"
goxmpp "gosrc.io/xmpp"
)
// YAML config, compatible with the format of Zhabogram 2.0.0
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() {
var profilingPort = flag.Int("profiling-port", 0, "The port for pprof server")
flag.Parse()
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)
}
SetLogrusLevel(config.XMPP.Loglevel)
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
err = sm.Run()
exit()
if err != nil {
<-cleanupDone
log.Fatal(err)
// bye
os.Exit(-1)
}
}
func exit() {
xmpp.Close(component)
close(cleanupDone)
}
|