aboutsummaryrefslogtreecommitdiff
path: root/xmpp/handlers.go
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-12-10 21:34:55 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-12-10 21:34:55 +0300
commit416c70890950f76ce6f5ce902ea9a70165239f56 (patch)
tree2f9889d6d9448e3f7226cfa51d9bbe2e5fcde752 /xmpp/handlers.go
parentae2470dcb70c302bd3fbcd53ffa7384f526435bd (diff)
vCard support
Diffstat (limited to 'xmpp/handlers.go')
-rw-r--r--xmpp/handlers.go84
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