Thumbnail

rani/matterbridge.git

Clone URL: https://git.buni.party/rani/matterbridge.git

commit 2c3983e2dfcc3f2d0166c545900377a14ec7058c Author: Alexander <papatutuwawa@polynom.me> Date: Thu Apr 16 22:16:25 2020 +0000 Implement User Avatar spoofing of XMPP users (#1090) * Implement User Avatar spoofing of XMPP users diff --git a/bridge/xmpp/handler.go b/bridge/xmpp/handler.go new file mode 100644 index 0000000..731998d --- /dev/null +++ b/bridge/xmpp/handler.go @@ -00 +134 @@ +package bxmpp + +import ( + "github.com/42wim/matterbridge/bridge/config" + "github.com/42wim/matterbridge/bridge/helper" + "github.com/matterbridge/go-xmpp" +) + +// handleDownloadAvatar downloads the avatar of userid from channel +// sends a EVENT_AVATAR_DOWNLOAD message to the gateway if successful. +// logs an error message if it fails +func (b *Bxmpp) handleDownloadAvatar(avatar xmpp.AvatarData) { + rmsg := config.Message{ + Username: "system", + Text: "avatar", + Channel: b.parseChannel(avatar.From), + Account: b.Account, + UserID: avatar.From, + Event: config.EventAvatarDownload, + Extra: make(map[string][]interface{}), + } + if _, ok := b.avatarMap[avatar.From]; !ok { + b.Log.Debugf("Avatar.From: %s", avatar.From) + + err := helper.HandleDownloadSize(b.Log, &rmsg, avatar.From+".png", int64(len(avatar.Data)), b.General) + if err != nil { + b.Log.Error(err) + return + } + helper.HandleDownloadData(b.Log, &rmsg, avatar.From+".png", rmsg.Text, "", &avatar.Data, b.General) + b.Log.Debugf("Avatar download complete") + b.Remote <- rmsg + } +} diff --git a/bridge/xmpp/helpers.go b/bridge/xmpp/helpers.go new file mode 100644 index 0000000..eb6a536 --- /dev/null +++ b/bridge/xmpp/helpers.go @@ -00 +130 @@ +package bxmpp + +import ( + "regexp" + + "github.com/42wim/matterbridge/bridge/config" +) + +var pathRegex = regexp.MustCompile("[^a-zA-Z0-9]+") + +// GetAvatar constructs a URL for a given user-avatar if it is available in the cache. +func getAvatar(av map[string]string, userid string, general *config.Protocol) string { + if hash, ok := av[userid]; ok { + // NOTE: This does not happen in bridge/helper/helper.go but messes up XMPP + id := pathRegex.ReplaceAllString(userid, "_") + return general.MediaServerDownload + "/" + hash + "/" + id + ".png" + } + return "" +} + +func (b *Bxmpp) cacheAvatar(msg *config.Message) string { + fi := msg.Extra["file"][0].(config.FileInfo) + /* if we have a sha we have successfully uploaded the file to the media server, + so we can now cache the sha */ + if fi.SHA != "" { + b.Log.Debugf("Added %s to %s in avatarMap", fi.SHA, msg.UserID) + b.avatarMap[msg.UserID] = fi.SHA + } + return "" +} diff --git a/bridge/xmpp/xmpp.go b/bridge/xmpp/xmpp.go index b526301..7122534 100644 --- a/bridge/xmpp/xmpp.go +++ b/bridge/xmpp/xmpp.go @@ -2312 +2315 @@ type Bxmpp struct {   xmppMap map[string]string   connected bool   sync.RWMutex + + avatarMap map[string]string  }    func New(cfg *bridge.Config) bridge.Bridger {   return &Bxmpp{ - Config: cfg, - xmppMap: make(map[string]string), + Config: cfg, + xmppMap: make(map[string]string), + avatarMap: make(map[string]string),   }  }   @@ -696 +7210 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {   }   b.Log.Debugf("=> Receiving %#v", msg)   + if msg.Event == config.EventAvatarDownload { + return b.cacheAvatar(&msg), nil + } +   // Upload a file (in XMPP case send the upload URL because XMPP has no native upload support).   if msg.Extra != nil {   for _, rmsg := range helper.HandleExtra(&msg, b.General) { @@ -2306 +23712 @@ func (b *Bxmpp) handleXMPP() error {   event = config.EventTopicChange   }   + avatar := getAvatar(b.avatarMap, v.Remote, b.General) + if avatar == "" { + b.Log.Debugf("Requesting avatar data") + b.xc.AvatarRequestData(v.Remote) + } +   msgID := v.ID   if v.ReplaceID != "" {   msgID = v.ReplaceID @@ -2396 +2527 @@ func (b *Bxmpp) handleXMPP() error {   Text: v.Text,   Channel: b.parseChannel(v.Remote),   Account: b.Account, + Avatar: avatar,   UserID: v.Remote,   ID: msgID,   Event: event, @@ -2556 +2698 @@ func (b *Bxmpp) handleXMPP() error {   b.Log.Debugf("<= Message is %#v", rmsg)   b.Remote <- rmsg   } + case xmpp.AvatarData: + b.handleDownloadAvatar(v)   case xmpp.Presence:   // Do nothing.   } diff --git a/gateway/handlers.go b/gateway/handlers.go index 26d3f18..edc2bf4 100644 --- a/gateway/handlers.go +++ b/gateway/handlers.go @@ -1697 +1697 @@ func (gw *Gateway) ignoreEvent(event string, dest *bridge.Bridge) bool {   switch event {   case config.EventAvatarDownload:   // Avatar downloads are only relevant for telegram and mattermost for now - if dest.Protocol != "mattermost" && dest.Protocol != "telegram" { + if dest.Protocol != "mattermost" && dest.Protocol != "telegram" && dest.Protocol != "xmpp" {   return true   }   case config.EventJoinLeave: