|
|
|
@ -1,6 +1,8 @@
|
|
|
|
|
package xmpp
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
|
|
|
|
"dev.narayana.im/narayana/telegabber/telegram"
|
|
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
@ -8,8 +10,6 @@ import (
|
|
|
|
|
"gosrc.io/xmpp/stanza"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var sessions map[string]telegram.Client
|
|
|
|
|
|
|
|
|
|
func logPacketType(p stanza.Packet) {
|
|
|
|
|
log.Warn("Ignoring packet: %T\n", p)
|
|
|
|
|
}
|
|
|
|
@ -48,7 +48,8 @@ func HandlePresence(s xmpp.Sender, p stanza.Packet) {
|
|
|
|
|
|
|
|
|
|
if prs.Type == "subscribe" {
|
|
|
|
|
handleSubscription(s, prs)
|
|
|
|
|
} else if prs.To == jid.Bare() {
|
|
|
|
|
}
|
|
|
|
|
if prs.To == jid.Bare() {
|
|
|
|
|
handlePresence(s, prs)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -93,17 +94,26 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) {
|
|
|
|
|
if !ok {
|
|
|
|
|
client, err := telegram.NewClient(tgConf, bareFromJid)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error("Invalid from JID!")
|
|
|
|
|
log.Error(errors.Wrap(err, "TDlib initialization failure"))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
sessions[bareFromJid] = client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch p.Type {
|
|
|
|
|
case "unsubscribed":
|
|
|
|
|
session.Disconnect()
|
|
|
|
|
delete(sessions, bareFromJid)
|
|
|
|
|
case "unavailable", "error":
|
|
|
|
|
session.Disconnect()
|
|
|
|
|
case "":
|
|
|
|
|
session.Connect()
|
|
|
|
|
// due to the weird implentation of go-tdlib wrapper, it won't
|
|
|
|
|
// return the client instance until successful authorization
|
|
|
|
|
go func() {
|
|
|
|
|
session.Connect()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Error(errors.Wrap(err, "TDlib connection failure"))
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|