Files
3f5b3cdd98 feat: add configurable SMTP HELO hostname (#2146)
* feat: add configurable SMTP HELO hostname

Allow the SMTP HELO/EHLO hostname to be configured separately from
the SMTP server hostname.

This is useful when the SMTP server requires clients to identify
themselves with a fully qualified hostname different from the server
address.

* chore: remove vendored dependency changes

* Bump go-pkgz/notify to v1.4.0 and document SMTP_HELO_HOST

The HELOHost field lands in go-pkgz/notify v1.4.0, so the branch needs the
bump to compile; v1.3.0 in master has no such field. The example module is
tidied alongside, as any change to backend/go.mod requires.

Documents the parameter in the parameters table and, separately, in the email
setup page: what it does, that leaving it unset keeps the previous `localhost`
greeting, and the case it exists for, a relay refusing the greeting under
Postfix `reject_non_fqdn_helo_hostname`.

Also records the current limit: verification emails for email authentication
go through go-pkgz/auth's own sender, which has no equivalent setting, so the
greeting there is unchanged.

* Bump go-pkgz/auth to v2.2.0 and apply SMTP_HELO_HOST to verification email

The verification email sender had no way to set the greeting, so a relay that
refuses the HELO would accept notifications and still reject sign-in emails.
EmailParams gains HELOHost in go-pkgz/auth v2.2.0, so the same SMTP_HELO_HOST
now drives both paths.

The example module is tidied alongside, as any change to backend/go.mod
requires.

---------

Co-authored-by: oli <someone@somewhere.tld>
Co-authored-by: Dmitry Verkhoturov <paskal.07@gmail.com>
2026-08-19 02:52:39 -05:00

110 lines
2.9 KiB
Go

package notify
import (
"context"
"errors"
"fmt"
"net/url"
"strings"
"github.com/slack-go/slack"
)
// Slack notifications client
type Slack struct {
client *slack.Client
}
// NewSlack makes Slack client for notifications
func NewSlack(token string, opts ...slack.Option) *Slack {
return &Slack{client: slack.New(token, opts...)}
}
// Send sends the message over Slack, with "title", "titleLink" and "attachmentText" parsed from destination field
// with "slack:" schema same way "mailto:" schema is constructed.
//
// Example:
//
// - slack:channelName
// - slack:channelID
// - slack:userID
// - slack:channel?title=title&attachmentText=test%20text&titleLink=https://example.org
func (s *Slack) Send(ctx context.Context, destination, text string) error {
channelID, attachment, err := s.parseDestination(destination)
if err != nil {
return fmt.Errorf("problem parsing destination: %w", err)
}
options := []slack.MsgOption{slack.MsgOptionText(text, false)}
// titleLink alone carries nothing, slack renders it as a link on the title and drops it without one
if attachment.Title != "" || attachment.Text != "" {
options = append(options, slack.MsgOptionAttachments(attachment))
}
select {
case <-ctx.Done():
return ctx.Err()
default:
_, _, err = s.client.PostMessageContext(ctx, channelID, options...)
return err
}
}
// Schema returns schema prefix supported by this client
func (s *Slack) Schema() string {
return "slack"
}
func (s *Slack) String() string {
return "slack notifications destination"
}
// parses "slack:" in a manner "mailto:" URL is parsed url and returns channelID and attachment.
// if channelID is channel name and not ID (starting with C for channel and with U for user),
// then it will be resolved to ID.
func (s *Slack) parseDestination(destination string) (string, slack.Attachment, error) {
// parse URL
u, err := url.Parse(destination)
if err != nil {
return "", slack.Attachment{}, err
}
if u.Scheme != "slack" {
return "", slack.Attachment{}, fmt.Errorf("unsupported scheme %s, should be slack", u.Scheme)
}
channelID := u.Opaque
if !strings.HasPrefix(u.Opaque, "C") && !strings.HasPrefix(u.Opaque, "U") {
channelID, err = s.findChannelIDByName(u.Opaque)
if err != nil {
return "", slack.Attachment{}, fmt.Errorf("problem retrieving channel ID for #%s: %w", u.Opaque, err)
}
}
return channelID,
slack.Attachment{
Title: u.Query().Get("title"),
TitleLink: u.Query().Get("titleLink"),
Text: u.Query().Get("attachmentText"),
}, nil
}
func (s *Slack) findChannelIDByName(name string) (string, error) {
params := slack.GetConversationsParameters{}
for {
channels, next, err := s.client.GetConversations(&params)
if err != nil {
return "", err
}
for i := range channels {
if channels[i].Name == name {
return channels[i].ID, nil
}
}
if next == "" {
break
}
params.Cursor = next
}
return "", errors.New("no such channel")
}