Fix presence handling and lockup on TDlib instance creation
This commit is contained in:
parent
29da6ac0a0
commit
7185d4ac9b
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
|||
config.yml
|
||||
telegabber
|
||||
sessions/
|
||||
|
|
|
@ -13,6 +13,20 @@ func (c *Client) Connect() error {
|
|||
}
|
||||
|
||||
authorizer := client.ClientAuthorizer()
|
||||
go func() {
|
||||
for {
|
||||
state, ok := <-authorizer.State
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
ok = authorizationStateHandler(state)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
authorizer.TdlibParameters <- c.parameters
|
||||
|
||||
tdlibClient, err := client.NewClient(authorizer, c.logVerbosity)
|
||||
|
@ -33,4 +47,15 @@ func (c *Client) Disconnect() {
|
|||
if !c.online {
|
||||
return
|
||||
}
|
||||
|
||||
c.client.Stop()
|
||||
c.online = false
|
||||
}
|
||||
|
||||
func authorizationStateHandler(state client.AuthorizationState) bool {
|
||||
switch state.AuthorizationStateType() {
|
||||
// TODO
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
|
@ -2,12 +2,14 @@ package xmpp
|
|||
|
||||
import (
|
||||
"dev.narayana.im/narayana/telegabber/config"
|
||||
"dev.narayana.im/narayana/telegabber/telegram"
|
||||
|
||||
"gosrc.io/xmpp"
|
||||
)
|
||||
|
||||
var jid *xmpp.Jid
|
||||
var tgConf config.TelegramConfig
|
||||
var sessions map[string]telegram.Client
|
||||
|
||||
// NewComponent starts a new component and wraps it in
|
||||
// a stream manager that you should start yourself
|
||||
|
@ -19,6 +21,7 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea
|
|||
}
|
||||
|
||||
tgConf = tc
|
||||
sessions = make(map[string]telegram.Client)
|
||||
|
||||
options := xmpp.ComponentOptions{
|
||||
Address: conf.Host + ":" + conf.Port,
|
||||
|
|
|
@ -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 "":
|
||||
// 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"))
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue