Thumbnail

rani/matterbridge.git

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

commit bc265e3dd1e425091cc81869cc80f76cff97360f Author: selfhoster1312 <selfhoster1312@kl.netlib.re> Date: Sat Dec 06 21:16:44 2025 +0000 matrix: Support authenticated media (MSC3916) diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go index 3fb2d61..fc71f6d 100644 --- a/bridge/matrix/matrix.go +++ b/bridge/matrix/matrix.go @@ -37 +39 @@ package bmatrix  import (   "bytes"   "fmt" + "io"   "mime" + "net/http"   "regexp"   "strings"   "sync" @@ -3936 +39517 @@ func (b *Bmatrix) Send(msg config.Message) (string, error) {   return resp.EventID, err  }   +func (b *Bmatrix) NewHttpRequest(method, uri string, body io.Reader) (*http.Request, error) { + req, err := http.NewRequest(method, uri, body) + if err != nil { + return nil, err + } + + req.Header.Add("Authorization", "Bearer "+b.mc.AccessToken) + + return req, nil +} +  func (b *Bmatrix) handlematrix() {   syncer := b.mc.Syncer.(*matrix.DefaultSyncer)   syncer.OnEventType("m.room.redaction", b.handleEvent) @@ -4726 +48525 @@ func (b *Bmatrix) handleReply(ev *matrix.Event, rmsg config.Message) bool {   return true  }   +func (b *Bmatrix) handleAttachment(ev *matrix.Event, rmsg config.Message) bool { + if !b.containsAttachment(ev.Content) { + return false + } + + go func() { + // File download is processed in the background to avoid stalling + err := b.handleDownloadFile(&rmsg, ev.Content) + if err != nil { + b.Log.Errorf("%#v", err) + return + } + + b.Remote <- rmsg + }() + + return true +} +  func (b *Bmatrix) handleMemberChange(ev *matrix.Event) {   // Update the displayname on join messages, according to https://matrix.org/docs/spec/client_server/r0.6.1#events-on-change-of-profile-information   if ev.Content["membership"] == "join" { @@ -53912 +57110 @@ func (b *Bmatrix) handleEvent(ev *matrix.Event) {   return   }   - // Do we have attachments - if b.containsAttachment(ev.Content) { - err := b.handleDownloadFile(&rmsg, ev.Content) - if err != nil { - b.Log.Errorf("download failed: %#v", err) - } + // Do we have an attachment + // TODO: does matrix support multiple attachments? + if b.handleAttachment(ev, rmsg) { + return   }     b.Log.Debugf("<= Sending message from %s on %s to gateway", ev.Sender, b.Account) @@ -5707 +60010 @@ func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content map[string]in   if url, ok = content["url"].(string); !ok {   return fmt.Errorf("url isn't a %T", url)   } - url = strings.ReplaceAll(url, "mxc://", b.GetString("Server")+"/_matrix/media/v1/download/") + // Matrix downloads now have to be authenticated with an access token + // See https://github.com/matrix-org/matrix-spec-proposals/blob/main/proposals/3916-authentication-for-media.md + // Also see: https://github.com/matterbridge-org/matterbridge/issues/36 + url = strings.ReplaceAll(url, "mxc://", b.GetString("Server")+"/_matrix/client/v1/media/download/")     if info, ok = content["info"].(map[string]interface{}); !ok {   return fmt.Errorf("info isn't a %T", info) @@ -60118 +63411 @@ func (b *Bmatrix) handleDownloadFile(rmsg *config.Message, content map[string]in   }   }   - // check if the size is ok - err := helper.HandleDownloadSize(b.Log, rmsg, name, int64(size), b.General) + // TODO: add attachment ID? + err := b.AddAttachmentFromURL(rmsg, name, "", "", url)   if err != nil {   return err   } - // actually download the file - data, err := helper.DownloadFile(url) - if err != nil { - return fmt.Errorf("download %s failed %#v", url, err) - } - // add the downloaded data to the message - helper.HandleDownloadData(b.Log, rmsg, name, "", url, data, b.General)   return nil  }   diff --git a/changelog.md b/changelog.md index 88dd0c4..c5bce57 100644 --- a/changelog.md +++ b/changelog.md @@ -316 +318 @@  - general   - when downloading a file attachment from a remote HTTP server, matterbridge will now error if the return code is not 200 to avoid saving trash data ([#20](https://github.com/matterbridge-org/matterbridge/pull/20)) +- matrix + - attachments received from matrix are working again, with authenticated media (MSC3916) implemented ([#61](https://github.com/matterbridge-org/matterbridge/pull/61))    ## Upstream