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/handlers.go | |
parent | c0c21a35a4cfd326423ad530926a0e96c1b07dcf (diff) |
Telegram authorization
Diffstat (limited to 'xmpp/handlers.go')
-rw-r--r-- | xmpp/handlers.go | 64 |
1 files changed, 58 insertions, 6 deletions
diff --git a/xmpp/handlers.go b/xmpp/handlers.go index ba93a11..d3abeab 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -2,8 +2,11 @@ package xmpp import ( "github.com/pkg/errors" + "strconv" + "strings" "dev.narayana.im/narayana/telegabber/persistence" + "dev.narayana.im/narayana/telegabber/xmpp/gateway" log "github.com/sirupsen/logrus" "gosrc.io/xmpp" @@ -33,9 +36,52 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) { return } - log.Printf("Message: %#v\n", msg) - reply := stanza.Message{Attrs: stanza.Attrs{To: msg.From}, Body: msg.Body} - _ = s.Send(reply) + component, ok := s.(*xmpp.Component) + if !ok { + log.Error("Not a component") + return + } + + if msg.Type != "error" && msg.Body != "" { + log.WithFields(log.Fields{ + "from": msg.From, + "to": msg.To, + }).Warn("Message") + log.Debugf("%#v", msg) + + fromJid, err := xmpp.NewJid(msg.From) + if err != nil { + log.Error("Invalid from JID!") + return + } + + session, ok := sessions[fromJid.Bare()] + if !ok { + log.Error("Message from stranger") + return + } + + toParts := strings.Split(msg.To, "@") + toID := toParts[0] + if len(toParts) > 1 { + toIDInt, err := strconv.Atoi(toID) + if err != nil { + session.ProcessOutgoingMessage(toIDInt, msg.Body, 0) + return + } + } else if toID == gateway.Jid.Bare() { + bodyFields := strings.Fields(msg.Body) + cmd := bodyFields[0] + if strings.HasPrefix(cmd, "/") { + response := session.ProcessTransportCommand(cmd[1:], bodyFields[1:]) + if response != "" { + gateway.SendMessage(msg.From, "", response, component) + } + return + } + } + log.Warn("Unknown purpose of the message, skipping") + } } // HandlePresence processes an incoming XMPP presence @@ -49,7 +95,7 @@ func HandlePresence(s xmpp.Sender, p stanza.Packet) { if prs.Type == "subscribe" { handleSubscription(s, prs) } - if prs.To == jid.Bare() { + if prs.To == gateway.Jid.Bare() { handlePresence(s, prs) } } @@ -77,6 +123,12 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) { presenceType = "online" } + component, ok := s.(*xmpp.Component) + if !ok { + log.Error("Not a component") + return + } + log.WithFields(log.Fields{ "type": presenceType, "from": p.From, @@ -90,7 +142,7 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) { return } bareFromJid := fromJid.Bare() - session, ok := getTelegramInstance(bareFromJid, &persistence.Session{}) + session, ok := getTelegramInstance(bareFromJid, &persistence.Session{}, component) if !ok { return } @@ -101,7 +153,7 @@ func handlePresence(s xmpp.Sender, p stanza.Presence) { delete(sessions, bareFromJid) case "unavailable", "error": session.Disconnect() - case "": + case "", "online": // due to the weird implentation of go-tdlib wrapper, it won't // return the client instance until successful authorization go func() { |