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
24 changes: 24 additions & 0 deletions bridge/xmpp/xmpp.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
"github.com/42wim/matterbridge/bridge/helper"
lru "github.com/hashicorp/golang-lru"
"github.com/jpillora/backoff"
"github.com/matterbridge/go-xmpp"
"github.com/rs/xid"
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 @@ -130,13 +138,25 @@ func (b *Bxmpp) Send(msg config.Message) (string, error) {
if msg.ID != "" {
msgReplaceID = msg.ID
}

// XEP-0461: populate reply fields if this message is a reply.
var replyID, replyTo string
if msg.ParentValid() {
if stanzaID, ok := b.cache.Get(msg.ParentID); ok {
replyID = stanzaID.(string)
}
replyTo = msg.Channel + "@" + b.GetString("Muc") + "/" + b.GetString("Nick")
}

b.Log.Debugf("=> Sending message %#v", msg)
if _, err := b.xc.Send(xmpp.Chat{
Type: "groupchat",
Remote: msg.Channel + "@" + b.GetString("Muc"),
Text: msg.Username + msg.Text,
ID: msgID,
ReplaceID: msgReplaceID,
ReplyID: replyID,
ReplyTo: replyTo,
}); err != nil {
return "", err
}
Expand Down Expand Up @@ -297,6 +317,10 @@ func (b *Bxmpp) handleXMPP() error {
if v.Type == "groupchat" {
b.Log.Debugf("== Receiving %#v", v)

if v.StanzaID != "" {
b.cache.Add(v.ID, v.StanzaID)
}

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