|
|
@ -25,19 +25,19 @@ var db persistence.SessionsYamlDB
|
|
|
|
|
|
|
|
|
|
|
|
// NewComponent starts a new component and wraps it in
|
|
|
|
// NewComponent starts a new component and wraps it in
|
|
|
|
// a stream manager that you should start yourself
|
|
|
|
// a stream manager that you should start yourself
|
|
|
|
func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.StreamManager, error) {
|
|
|
|
func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.StreamManager, *xmpp.Component, error) {
|
|
|
|
var err error
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
|
|
jid, err = xmpp.NewJid(conf.Jid)
|
|
|
|
jid, err = xmpp.NewJid(conf.Jid)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tgConf = tc
|
|
|
|
tgConf = tc
|
|
|
|
|
|
|
|
|
|
|
|
err = loadSessions(conf.Db)
|
|
|
|
err = loadSessions(conf.Db)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
options := xmpp.ComponentOptions{
|
|
|
|
options := xmpp.ComponentOptions{
|
|
|
@ -54,14 +54,14 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
|
|
|
|
|
|
|
|
|
|
|
|
component, err := xmpp.NewComponent(options, router)
|
|
|
|
component, err := xmpp.NewComponent(options, router)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cm := xmpp.NewStreamManager(component, nil)
|
|
|
|
sm := xmpp.NewStreamManager(component, nil)
|
|
|
|
|
|
|
|
|
|
|
|
go heartbeat(component)
|
|
|
|
go heartbeat(component)
|
|
|
|
|
|
|
|
|
|
|
|
return cm, nil
|
|
|
|
return sm, component, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func logPresence(err error, presence *stanza.Presence) {
|
|
|
|
func logPresence(err error, presence *stanza.Presence) {
|
|
|
@ -228,3 +228,22 @@ func sendPresence(component *xmpp.Component, to string, args ...args.V) error {
|
|
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Close gracefully terminates the component and saves active sessions
|
|
|
|
|
|
|
|
func Close(component *xmpp.Component) {
|
|
|
|
|
|
|
|
log.Error("Disconnecting...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for _, session := range sessions {
|
|
|
|
|
|
|
|
session.Disconnect()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.Transaction(func() bool {
|
|
|
|
|
|
|
|
for jid, session := range sessions {
|
|
|
|
|
|
|
|
db.Data.Sessions[jid] = *session.Session
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
|
|
|
}, persistence.SessionMarshaller)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
component.Disconnect()
|
|
|
|
|
|
|
|
}
|
|
|
|