rspamd

package
v0.1.0-rc.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 8, 2025 License: AGPL-3.0 Imports: 9 Imported by: 0

README

Rspamd HTTP Client

A Go HTTP client for interacting with the Rspamd spam filtering service. This client supports the /checkv2 endpoint for spam checking and is configurable via environment variables.

Features

  • HTTP client for Rspamd spam checking
  • Configurable via environment variables
  • Support for all Rspamd HTTP headers
  • Comprehensive response parsing
  • Connection health checking via ping
  • Helper methods for spam detection

Installation

The client is part of the mail utilities package and can be imported as:

import "your-project/mail/utils/rspamd"

Configuration

The client can be configured using environment variables or programmatically.

Environment Variables
Variable Description Default
RSPAMD_BASE_URL Rspamd server URL http://localhost:11333
RSPAMD_PASSWORD Password for authentication (empty)
RSPAMD_TIMEOUT_SECONDS HTTP timeout in seconds 30
RSPAMD_MAX_RETRIES Maximum retry attempts 3
Programmatic Configuration
config := &rspamd.Config{
    BaseURL:    "http://rspamd.internal:11333",
    Password:   "mysecretpassword",
    Timeout:    60 * time.Second,
    MaxRetries: 5,
}
client := rspamd.NewClient(config)

Usage

Basic Usage
// Create client with default configuration
client := rspamd.NewClient(nil)

// Create check request
req := &rspamd.CheckRequest{
    From:    "sender@example.com",
    Rcpt:    []string{"recipient@example.com"},
    IP:      "192.168.1.1",
    Helo:    "mail.example.com",
    Hostname: "mail.example.com",
    Message: emailMessage, // []byte containing RFC 2822 email
}

// Check message for spam
resp, err := client.CheckMessage(req)
if err != nil {
    log.Fatalf("Failed to check message: %v", err)
}

// Process results
fmt.Printf("Spam Score: %.2f\n", resp.GetScore())
fmt.Printf("Action: %s\n", resp.GetAction())
fmt.Printf("Is Spam: %t\n", resp.IsSpam())
Advanced Usage with All Headers
req := &rspamd.CheckRequest{
    DeliverTo: "user@company.com",
    IP:        "1.2.3.4",
    Helo:      "mail.example.com",
    Hostname:  "mail.example.com",
    Flags:     []string{"pass_all", "groups"},
    From:      "sender@example.com",
    QueueID:   "queue123",
    Raw:       false,
    Rcpt:      []string{"user@company.com", "admin@company.com"},
    Pass:      "all",
    Subject:   "Important Message",
    User:      "authenticated_user",
    Message:   emailMessage,
}
Health Checking
// Test connection to Rspamd
if err := client.Ping(); err != nil {
    log.Printf("Rspamd is not available: %v", err)
} else {
    log.Println("Rspamd is available")
}

Response Structure

The CheckResponse contains the following fields:

  • Action: The action taken by Rspamd (e.g., "reject", "soft reject", "add header", "no action")
  • Score: The spam score
  • RequiredScore: The threshold score for actions
  • Symbols: Map of triggered symbols and their details
  • Messages: Optional messages from Rspamd filters
  • Subject: Modified subject (if action is "rewrite subject")
  • URLs: List of URLs found in the message
  • Emails: List of email addresses found in the message
  • MessageID: Message ID for logging
  • Milter: Milter-specific response data
Helper Methods
// Check if message is classified as spam
if resp.IsSpam() {
    // Handle spam
}

// Get spam score
score := resp.GetScore()

// Get action taken
action := resp.GetAction()

Supported Rspamd Headers

The client supports all standard Rspamd HTTP headers:

  • Deliver-To: Actual delivery recipient
  • IP: Source IP address
  • Helo: SMTP HELO command
  • Hostname: Resolved hostname
  • Flags: Output flags (comma-separated)
  • From: SMTP MAIL FROM command
  • Queue-Id: SMTP queue ID
  • Raw: Treat content as raw data
  • Rcpt: SMTP recipient(s)
  • Pass: Pass all filters
  • Subject: Message subject
  • User: Authenticated SMTP client username
  • Password: Authentication password

Error Handling

The client returns descriptive errors for various failure scenarios:

  • Network connectivity issues
  • Invalid HTTP responses
  • JSON parsing errors
  • Configuration errors

Testing

Run the tests with:

go test ./mail/utils/rspamd

Examples

See example.go for complete usage examples including:

  • Basic spam checking
  • Custom configuration
  • Environment variable usage
  • Response processing

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Example

func Example()

Example usage of the Rspamd client

func ExampleWithCustomConfig

func ExampleWithCustomConfig()

ExampleWithCustomConfig demonstrates using custom configuration

func ExampleWithEnvironmentVariables

func ExampleWithEnvironmentVariables()

ExampleWithEnvironmentVariables demonstrates using environment variables

Types

type CheckRequest

type CheckRequest struct {
	// DeliverTo defines the actual delivery recipient of the message.
	// Can be used for personalized statistics and user-specific options.
	DeliverTo string `json:"deliver_to,omitempty"`

	// IP defines the IP address from which this message was received.
	// Used for IP-based filtering and reputation checks.
	IP string `json:"ip,omitempty"`

	// Helo defines the SMTP HELO command value.
	// Used for HELO-based filtering and validation.
	Helo string `json:"helo,omitempty"`

	// Hostname defines the resolved hostname of the sending server.
	// Used for hostname-based filtering and reverse DNS checks.
	Hostname string `json:"hostname,omitempty"`

	// Flags defines output flags as a comma-separated list.
	// Supported flags: pass_all, groups, zstd, no_log, milter, profile, body_block, ext_urls, skip, skip_process
	Flags []string `json:"flags,omitempty"`

	// From defines the SMTP MAIL FROM command data (sender email address).
	// Used for sender-based filtering and reputation checks.
	From string `json:"from,omitempty"`

	// QueueID defines the SMTP queue ID for the message.
	// Can be used instead of message ID in logging for better traceability.
	QueueID string `json:"queue_id,omitempty"`

	// Raw indicates whether the content should be treated as raw data instead of MIME.
	// If set to true, Rspamd assumes the content is not MIME and treats it as raw data.
	Raw bool `json:"raw,omitempty"`

	// Rcpt defines the SMTP recipient(s) of the message.
	// Multiple recipients can be specified for multi-recipient messages.
	Rcpt []string `json:"rcpt,omitempty"`

	// Pass controls which filters should be checked for this message.
	// If set to "all", all filters will be checked regardless of settings.
	Pass string `json:"pass,omitempty"`

	// Subject defines the subject of the message.
	// Used for non-MIME messages or when subject needs to be explicitly specified.
	Subject string `json:"subject,omitempty"`

	// User defines the username for authenticated SMTP client.
	// Used for user-based filtering and authentication checks.
	User string `json:"user,omitempty"`

	// Message contains the actual email message content in RFC 2822 format.
	// This is the raw email data that will be analyzed by Rspamd.
	Message []byte `json:"message"`
}

CheckRequest represents the request structure for Rspamd spam checking

type CheckResponse

type CheckResponse struct {
	// Action indicates the action taken by Rspamd based on the spam score.
	// Possible values: "reject", "soft reject", "add header", "rewrite subject", "greylist", "no action"
	Action string `json:"action"`

	// Score is the calculated spam score for the message.
	// Higher scores indicate higher likelihood of spam.
	Score float64 `json:"score"`

	// RequiredScore is the threshold score required to trigger the action.
	// This is the configured threshold for the action that was taken.
	RequiredScore float64 `json:"required_score"`

	// Symbols contains the triggered spam detection symbols and their details.
	// Each symbol represents a specific spam detection rule that was matched.
	Symbols map[string]interface{} `json:"symbols"`

	// Messages contains optional messages added by Rspamd filters.
	// The "smtp_message" key contains text intended to be returned as SMTP response.
	Messages map[string]string `json:"messages"`

	// Subject contains the modified subject if the action was "rewrite subject".
	// This is the new subject that should be used for the message.
	Subject string `json:"subject,omitempty"`

	// URLs contains a list of URLs found in the message (only hostnames).
	// Useful for URL-based filtering and analysis.
	URLs []string `json:"urls,omitempty"`

	// Emails contains a list of email addresses found in the message.
	// Useful for email address extraction and analysis.
	Emails []string `json:"emails,omitempty"`

	// MessageID is the ID of the message, useful for logging and tracking.
	// This helps correlate Rspamd results with message processing.
	MessageID string `json:"message-id,omitempty"`

	// Milter contains milter-specific response data for header manipulation.
	// Used when integrating with MTA milter interfaces.
	Milter *MilterResponse `json:"milter,omitempty"`
}

CheckResponse represents the response from Rspamd spam checking

func (*CheckResponse) GetAction

func (r *CheckResponse) GetAction() string

GetAction returns the action taken by Rspamd

func (*CheckResponse) GetScore

func (r *CheckResponse) GetScore() float64

GetScore returns the spam score

func (*CheckResponse) IsSpam

func (r *CheckResponse) IsSpam() bool

IsSpam checks if the response indicates spam

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents an Rspamd HTTP client for spam checking

func NewClient

func NewClient(config *Config) *Client

NewClient creates a new Rspamd client with the given configuration

func (*Client) CheckMessage

func (c *Client) CheckMessage(req *CheckRequest) (*CheckResponse, error)

CheckMessage sends a message to Rspamd for spam checking

func (*Client) Ping

func (c *Client) Ping() error

Ping sends a ping request to check if Rspamd is available

type Config

type Config struct {
	// BaseURL is the base URL of the Rspamd server.
	// Should include protocol, hostname, and port (e.g., "http://localhost:11333").
	BaseURL string

	// Password is the authentication password for the Rspamd server.
	// Required if Rspamd is configured with password authentication.
	Password string

	// Timeout is the HTTP timeout for requests to the Rspamd server.
	// Should be set appropriately for your network conditions and message sizes.
	Timeout time.Duration

	// MaxRetries is the maximum number of retry attempts for failed requests.
	// Used to handle temporary network issues or server unavailability.
	MaxRetries int
}

Config holds the configuration for the Rspamd client

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default configuration with environment variable support

type HeaderValue

type HeaderValue struct {
	// Value is the actual header value content.
	// This is the text that will be set as the header value.
	Value string `json:"value"`

	// Order specifies the insertion order of the header.
	// Lower numbers are inserted first (e.g., 0 = first header).
	Order int `json:"order"`
}

HeaderValue represents a header value with insertion order

type MilterResponse

type MilterResponse struct {
	// AddHeaders specifies headers to add to the message.
	// The key is the header name, and the value contains the header value and insertion order.
	AddHeaders map[string]HeaderValue `json:"add_headers,omitempty"`

	// RemoveHeaders specifies headers to remove from the message.
	// The key is the header name, and the value is the order of the header to remove.
	// Special values: 0 = remove all headers with this name, negative = remove Nth header from end.
	RemoveHeaders map[string]int `json:"remove_headers,omitempty"`

	// ChangeFrom specifies a new SMTP MAIL FROM value to replace the original.
	// Used to modify the sender address during message processing.
	ChangeFrom string `json:"change_from,omitempty"`

	// Reject specifies a custom rejection message.
	// Values like "discard" or "quarantine" can be used for custom rejection handling.
	Reject string `json:"reject,omitempty"`

	// SpamHeader specifies a custom spam header name to add.
	// The header will contain spam detection information.
	SpamHeader string `json:"spam_header,omitempty"`

	// NoAction indicates that instead of performing any action, just add an X-Rspamd-Action header.
	// The message will be accepted with the action information in the header.
	NoAction bool `json:"no_action,omitempty"`

	// AddRcpt specifies new recipients to add to the message.
	// Used to add additional recipients during message processing.
	AddRcpt []string `json:"add_rcpt,omitempty"`

	// DelRcpt specifies recipients to remove from the message.
	// Used to remove recipients during message processing.
	DelRcpt []string `json:"del_rcpt,omitempty"`
}

MilterResponse represents milter-specific response data for header manipulation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL