Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions bridge/xmpp/xmpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sync"
"time"

lru "github.com/hashicorp/golang-lru"
"github.com/jpillora/backoff"
"github.com/matterbridge-org/matterbridge/bridge"
"github.com/matterbridge-org/matterbridge/bridge/config"
Expand All @@ -26,18 +27,25 @@ type Bxmpp struct {
xc *xmpp.Client
xmppMap map[string]string
connected bool
cache *lru.Cache
sync.RWMutex

avatarAvailability map[string]bool
avatarMap map[string]string
}

func New(cfg *bridge.Config) bridge.Bridger {
newCache, err := lru.New(5000)
if err != nil {
cfg.Log.Fatalf("Could not create LRU cache: %v", err)
}

return &Bxmpp{
Config: cfg,
xmppMap: make(map[string]string),
avatarAvailability: make(map[string]bool),
avatarMap: make(map[string]string),
cache: newCache,
}
}

Expand Down Expand Up @@ -124,19 +132,30 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {
return "", nil
}

// XEP-0461: populate reply fields if this message is a reply.
var replyID, replyTo string
if msg.ParentValid() {
if info, ok := b.cache.Get(msg.ParentID); ok {
si := info.(stanzaInfo)
replyID = si.stanzaID
replyTo = si.from
}
}

// Post normal message.
b.Log.Debugf("=> Sending message %#v", msg)
msgID := xid.New().String()
if _, err := b.xc.Send(xmpp.Chat{
Type: "groupchat",
Remote: msg.Channel + "@" + b.GetString("Muc"),
Text: msg.Username + msg.Text,
Type: "groupchat",
Remote: msg.Channel + "@" + b.GetString("Muc"),
Text: msg.Username + msg.Text,
ID: msgID,
ReplyID: replyID,
ReplyTo: replyTo,
}); err != nil {
return "", err
}

// Generate a dummy ID because to avoid collision with other internal messages
// However this does not provide proper Edits/Replies integration on XMPP side.
msgID := xid.New().String()
return msgID, nil
}

Expand Down Expand Up @@ -277,6 +296,11 @@ func (b *Bxmpp) xmppKeepAlive() chan bool {
return done
}

type stanzaInfo struct {
stanzaID string
from string
}

func (b *Bxmpp) handleXMPP() error {
b.startTime = time.Now()

Expand All @@ -300,6 +324,11 @@ func (b *Bxmpp) handleXMPP() error {
if v.Type == "groupchat" {
b.Log.Debugf("== Receiving %#v", v)

// XEP-0461: Cache StanzaID to use for Message Replies
if v.StanzaID.ID != "" {
b.cache.Add(v.ID, stanzaInfo{stanzaID: v.StanzaID.ID, from: v.Remote})
}

// Skip invalid messages.
if b.skipMessage(v) {
continue
Expand Down