Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 15 additions & 0 deletions bridge/discord/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package bdiscord
import (
"bytes"
"fmt"
"net/http"
"net/url"
"strings"
"sync"

Expand Down Expand Up @@ -86,6 +88,19 @@ func (b *Bdiscord) Connect() error {
return err
}
b.Log.Info("Connection succeeded")

http_proxy := b.GetString("http_proxy")
if http_proxy != "" {
b.Log.Infof("Using HTTP proxy %s to connect Discord API", http_proxy)
// This cannot fail because when the URL is invalid, `NewHttpClient` (bridge.go)
// produces an error which is caught by `AddBridge` (gateway.go), and this
// point in code is never reached.
proxyURL, _ := url.Parse(http_proxy)
// Implemented the use of proxy hinted here: https://github.com/bwmarrin/discordgo/issues/852
b.c.Client.Transport = &http.Transport{Proxy: http.ProxyURL(proxyURL)}
b.c.Dialer.Proxy = http.ProxyURL(proxyURL)
}

// Add privileged intent for guild member tracking. This is needed to track nicks
// for display names and @mention translation
b.c.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAllWithoutPrivileged |
Expand Down
28 changes: 11 additions & 17 deletions bridge/discord/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/bwmarrin/discordgo"
"github.com/davecgh/go-spew/spew"
"github.com/matterbridge-org/matterbridge/bridge/config"
"github.com/matterbridge-org/matterbridge/bridge/helper"
)

func (b *Bdiscord) messageDelete(s *discordgo.Session, m *discordgo.MessageDelete) { //nolint:unparam
Expand Down Expand Up @@ -106,32 +105,27 @@ func (b *Bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat
first := true
for _, attach := range m.Attachments {
if b.alwaysDownloadFiles {
var url, name, caption string
var data *[]byte

url = attach.URL
name = attach.Filename

err = helper.HandleDownloadSize(b.Log, &rmsg, name, int64(attach.Size), b.General)
if err != nil {
return
}
data, err = helper.DownloadFile(url)
if err != nil {
return
}
var caption string

if first {
caption = m.Content
if caption == "" {
caption = name
caption = attach.Filename
}
first = false
} else {
// There's only one caption for many attachments, see
// https://github.com/matterbridge-org/matterbridge/pull/44#discussion_r2590140958
caption = ""
}

helper.HandleDownloadData(b.Log, &rmsg, name, caption, "", data, b.General)
// TODO: does the discord API have a unique ID for the file?
// in the meantime, let's reuse the filename
err = b.AddAttachmentFromURL(&rmsg, attach.Filename, attach.Filename, caption, attach.URL)
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be processed in the background instead of blocking the main thread. This is wrong and we should merge #114 first and not edit file download at all here.

if err != nil {
b.Log.WithError(err).Warn("Failed to download one attachment. Skipping to the next.")
continue
}
} else {
m.Content = m.Content + "\n" + attach.URL
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't the URL added as an attachment to rmsg? Why is it hardcoded in the content? That's unrelated to this PR, but it looks rather wrong.

}
Expand Down
9 changes: 8 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
- Supports attachments
- xmpp
- New and revised advanced authentication settings `UseDirectTLS`, `NoStartTls`, `NoPlain`, and `Mechanism` ([#77](https://github.com/matterbridge-org/matterbridge/pull/77))
- discord
- `http_proxy` support ([#113](https://github.com/matterbridge-org/matterbridge/pull/113))
- `AlwaysDownloadFiles` setting enables upload of discord media to 3rd party
media server and protocols, to work around Discord providing short-lived
URLs for attachments ([#44](https://github.com/matterbridge-org/matterbridge/pull/44))
(this setting may be deprecated and become the default in the future, see [this discussion](https://github.com/matterbridge-org/matterbridge/issues/37#issuecomment-3694667703))

## Bugfixes

Expand All @@ -38,7 +44,8 @@
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))
- image attachments are now send as images with more metadata ([#61](https://github.com/matterbridge-org/matterbridge/pull/61))
- image attachments are now sent as images with more metadata ([#61](https://github.com/matterbridge-org/matterbridge/pull/61))
- audio attachments are now sent as documents, not as a voice messages, otherwise they won't be delivered ([#45](https://github.com/matterbridge-org/matterbridge/pull/45))
- xmpp
- various upstream go-xmpp changes fix connection on SASL2 with PLAIN auth
- telegram
Expand Down
16 changes: 15 additions & 1 deletion docs/protocols/discord/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,21 @@ This feature requires the "Manage Webhooks" permission (either globally or as pe
Setting: OPTIONAL \
Format: boolean \
Example:

```toml
AutoWebhooks=true
```

## http_proxy

> [!INFO]
> This setting can also be set application-wide, see [../../settings.md#http_proxy].

Specify the HTTP proxy to connect to the Discord API, and to download HTTP
attachments received from Discord.

- Setting: **OPTIONAL**
- Format: *string*
- Example:
```toml
http_proxy="http://login:password@server.example.org:1234"
```
17 changes: 17 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,20 @@ Example:

## RemoteNickFormat
See [RemoteNickFormat](#RemoteNickFormat)

## http_proxy

Defines a matterbridge-wide HTTP proxy setting used when making HTTP requests. This is used when:

- downloading remote HTTP attachments
- connecting to HTTP APIs for protocols that support it:
- discord

This setting is not used when connecting to the media server to upload attachments.

- Setting: **OPTIONAL**
- Format: *string*
- Example:
```toml
http_proxy="http://login:password@server.example.org:1234"
```
Loading