diff options
Diffstat (limited to 'xmpp/handlers.go')
-rw-r--r-- | xmpp/handlers.go | 84 |
1 files changed, 83 insertions, 1 deletions
diff --git a/xmpp/handlers.go b/xmpp/handlers.go index 5635f10..31c2b27 100644 --- a/xmpp/handlers.go +++ b/xmpp/handlers.go @@ -1,11 +1,16 @@ package xmpp import ( + "bytes" + "encoding/base64" "github.com/pkg/errors" + "io" + "os" "strconv" "strings" "dev.narayana.im/narayana/telegabber/persistence" + "dev.narayana.im/narayana/telegabber/xmpp/extensions" "dev.narayana.im/narayana/telegabber/xmpp/gateway" log "github.com/sirupsen/logrus" @@ -25,7 +30,84 @@ func HandleIq(s xmpp.Sender, p stanza.Packet) { return } - log.Printf("Iq: %#v\n", iq) + log.Debugf("%#v", iq) + if iq.Type == "get" { + _, ok := iq.Payload.(*extensions.IqVcardTemp) + if ok { + log.WithFields(log.Fields{ + "from": iq.From, + "to": iq.To, + }).Warn("VCard request") + + fromJid, err := xmpp.NewJid(iq.From) + if err != nil { + log.Error("Invalid from JID!") + return + } + + session, ok := sessions[fromJid.Bare()] + if !ok { + log.Error("IQ from stranger") + return + } + + toParts := strings.Split(iq.To, "@") + toID, err := strconv.ParseInt(toParts[0], 10, 64) + if err != nil { + log.Error("Invalid IQ to") + return + } + chat, user, err := session.GetContactByID(toID, nil) + if err != nil { + log.Error(err) + return + } + + vcard := extensions.IqVcardTemp{} + if chat != nil { + vcard.Fn.Text = chat.Title + + if chat.Photo != nil { + path := chat.Photo.Small.Local.Path + file, err := os.Open(path) + if err == nil { + defer file.Close() + + buf := new(bytes.Buffer) + binval := base64.NewEncoder(base64.StdEncoding, buf) + _, err = io.Copy(binval, file) + if err == nil { + vcard.Photo.Type.Text = "image/jpeg" + vcard.Photo.Binval.Text = buf.String() + } else { + log.Errorf("Error calculating hash: %v", path) + } + } else if path != "" { + log.Errorf("Photo does not exist: %v", path) + } + } + } + if user != nil { + vcard.Nickname.Text = user.Username + vcard.N.Given.Text = user.FirstName + vcard.N.Family.Text = user.LastName + vcard.Tel.Number.Text = user.PhoneNumber + } + + answer := stanza.IQ{ + Attrs: stanza.Attrs{ + From: iq.To, + To: iq.From, + Id: iq.Id, + Type: "result", + }, + Payload: vcard, + } + log.Debugf("%#v", answer) + + _ = s.Send(answer) + } + } } // HandleMessage processes an incoming XMPP message |