diff options
author | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-24 20:10:29 +0300 |
---|---|---|
committer | bodqhrohro <bodqhrohro@gmail.com> | 2019-11-24 20:10:29 +0300 |
commit | 653b1bde94b89d91862007a18b8730ff58673f59 (patch) | |
tree | 5b0c8674f918eb49dd27e53918ff10529c0ff55c /xmpp/component.go | |
parent | c0c21a35a4cfd326423ad530926a0e96c1b07dcf (diff) |
Telegram authorization
Diffstat (limited to 'xmpp/component.go')
-rw-r--r-- | xmpp/component.go | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/xmpp/component.go b/xmpp/component.go index fa1a650..7e2d16e 100644 --- a/xmpp/component.go +++ b/xmpp/component.go @@ -8,6 +8,7 @@ import ( "dev.narayana.im/narayana/telegabber/config" "dev.narayana.im/narayana/telegabber/persistence" "dev.narayana.im/narayana/telegabber/telegram" + "dev.narayana.im/narayana/telegabber/xmpp/gateway" log "github.com/sirupsen/logrus" "github.com/soheilhy/args" @@ -17,9 +18,8 @@ import ( const pollingInterval time.Duration = 1e7 -var jid *xmpp.Jid var tgConf config.TelegramConfig -var sessions map[string]telegram.Client +var sessions map[string]*telegram.Client var queue map[string]*stanza.Presence var db persistence.SessionsYamlDB @@ -28,18 +28,13 @@ var db persistence.SessionsYamlDB func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.StreamManager, *xmpp.Component, error) { var err error - jid, err = xmpp.NewJid(conf.Jid) + gateway.Jid, err = xmpp.NewJid(conf.Jid) if err != nil { return nil, nil, err } tgConf = tc - err = loadSessions(conf.Db) - if err != nil { - return nil, nil, err - } - options := xmpp.ComponentOptions{ Address: conf.Host + ":" + conf.Port, Domain: conf.Jid, @@ -57,6 +52,11 @@ func NewComponent(conf config.XMPPConfig, tc config.TelegramConfig) (*xmpp.Strea return nil, nil, err } + err = loadSessions(conf.Db, component) + if err != nil { + return nil, nil, err + } + sm := xmpp.NewStreamManager(component, nil) go heartbeat(component) @@ -99,10 +99,10 @@ func heartbeat(component *xmpp.Component) { } } -func loadSessions(dbPath string) error { +func loadSessions(dbPath string, component *xmpp.Component) error { var err error - sessions = make(map[string]telegram.Client) + sessions = make(map[string]*telegram.Client) db, err = persistence.LoadSessions(dbPath) if err != nil { @@ -111,7 +111,7 @@ func loadSessions(dbPath string) error { db.Transaction(func() bool { for jid, session := range db.Data.Sessions { - getTelegramInstance(jid, &session) + getTelegramInstance(jid, &session, component) } return false @@ -120,10 +120,11 @@ func loadSessions(dbPath string) error { return nil } -func getTelegramInstance(jid string, savedSession *persistence.Session) (telegram.Client, bool) { +func getTelegramInstance(jid string, savedSession *persistence.Session, component *xmpp.Component) (*telegram.Client, bool) { + var err error session, ok := sessions[jid] if !ok { - session, err := telegram.NewClient(tgConf, jid, savedSession) + session, err = telegram.NewClient(tgConf, jid, component, savedSession) if err != nil { log.Error(errors.Wrap(err, "TDlib initialization failure")) return session, false @@ -195,7 +196,7 @@ func newPresence(bareJid string, to string, args ...args.V) stanza.Presence { func sendPresence(component *xmpp.Component, to string, args ...args.V) error { var logFrom string - bareJid := jid.Bare() + bareJid := gateway.Jid.Bare() if SPFrom.IsSet(args) { logFrom = SPFrom.Get(args) } else { @@ -212,7 +213,12 @@ func sendPresence(component *xmpp.Component, to string, args ...args.V) error { // explicit check, as marshalling is expensive if log.GetLevel() == log.DebugLevel { - log.Debug(xml.Marshal(presence)) + xmlPresence, err := xml.Marshal(presence) + if err == nil { + log.Debug(string(xmlPresence)) + } else { + log.Debugf("%#v", presence) + } } immed := SPImmed.Get(args) |