rspamd_client/
config.rs

1//!
2//! ## Configuration for rspamd-client
3//!
4//! The `Config` struct allows you to customize various aspects of the client, including the base URL, proxy settings, and TLS settings.
5//!
6
7use std::collections::HashMap;
8use std::iter::IntoIterator;
9use typed_builder::TypedBuilder;
10
11/// Custom TLS settings for the Rspamd client
12#[derive(Debug, Clone, PartialEq)]
13pub struct TlsSettings {
14    /// Path to the TLS certificate file
15    pub cert_path: String,
16
17    /// Path to the TLS key file
18    pub key_path: String,
19
20    /// Optional path to the TLS CA file
21    pub ca_path: Option<String>,
22}
23
24/// Proxy configuration for the Rspamd client
25#[derive(Debug, Clone, PartialEq)]
26pub struct ProxyConfig {
27    /// Proxy server URL
28    pub proxy_url: String,
29
30    /// Optional username for proxy authentication
31    pub username: Option<String>,
32
33    /// Optional password for proxy authentication
34    pub password: Option<String>,
35}
36
37#[derive(TypedBuilder, Debug, PartialEq, Default)]
38pub struct EnvelopeData {
39    /// Sender email address
40    #[builder(default, setter(strip_option))]
41    pub from: Option<String>,
42
43    /// Recipients email addresses
44    #[builder(default)]
45    pub rcpt: Vec<String>,
46
47    /// Optional IP address of the sender
48    #[builder(default, setter(strip_option))]
49    pub ip: Option<String>,
50
51    /// Optional IP of the sender
52    #[builder(default, setter(strip_option))]
53    pub user: Option<String>,
54
55    /// Optional HELO string
56    #[builder(default, setter(strip_option))]
57    pub helo: Option<String>,
58
59    /// Optional hostname
60    #[builder(default, setter(strip_option))]
61    pub hostname: Option<String>,
62
63    /// Optional file path for local file scanning (File header)
64    /// When set, the message body is not transmitted and Rspamd reads the file directly from disk
65    /// This is a significant optimization when client and server are on the same host
66    #[builder(default, setter(strip_option))]
67    pub file_path: Option<String>,
68
69    /// Request rewritten body in response (body_block flag)
70    /// When enabled, if Rspamd rewrites the message body, it will be returned
71    /// as a separate part of the reply after the JSON, with Message-Offset header
72    /// indicating where the body starts
73    #[builder(default)]
74    pub body_block: bool,
75
76    /// Optional additional headers
77    #[builder(default)]
78    pub additional_headers: HashMap<String, String>,
79}
80
81impl IntoIterator for EnvelopeData {
82    type Item = (String, String);
83    type IntoIter = std::collections::hash_map::IntoIter<String, String>;
84
85    /// Convert the EnvelopeData struct into an iterator
86    fn into_iter(mut self) -> Self::IntoIter {
87        // We add all options to the additional headers
88        if let Some(from) = self.from {
89            self.additional_headers.insert("From".to_string(), from);
90        }
91        if let Some(ip) = self.ip {
92            self.additional_headers.insert("IP".to_string(), ip);
93        }
94        if let Some(user) = self.user {
95            self.additional_headers.insert("User".to_string(), user);
96        }
97        if let Some(helo) = self.helo {
98            self.additional_headers.insert("Helo".to_string(), helo);
99        }
100        if let Some(hostname) = self.hostname {
101            self.additional_headers
102                .insert("Hostname".to_string(), hostname);
103        }
104        if let Some(file_path) = self.file_path {
105            self.additional_headers
106                .insert("File".to_string(), file_path);
107        }
108        if self.body_block {
109            self.additional_headers
110                .insert("Flags".to_string(), "body_block".to_string());
111        }
112        for rcpt in self.rcpt {
113            self.additional_headers.insert("Rcpt".to_string(), rcpt);
114        }
115        self.additional_headers.into_iter()
116    }
117}
118
119/// Configuration for Rspamd client
120#[derive(TypedBuilder, Debug, PartialEq)]
121pub struct Config {
122    /// Base URL of Rspamd server
123    pub base_url: String,
124
125    /// Optional API key for authentication
126    #[builder(default, setter(strip_option))]
127    pub password: Option<String>,
128
129    /// Timeout duration for requests
130    #[builder(default = 30.0)]
131    pub timeout: f64,
132
133    /// Number of retries for requests
134    #[builder(default = 1)]
135    pub retries: u32,
136
137    /// Custom TLS settings for the asynchronous client
138    #[builder(default, setter(strip_option))]
139    pub tls_settings: Option<TlsSettings>,
140
141    /// Proxy configuration for the asynchronous client
142    #[builder(default, setter(strip_option))]
143    pub proxy_config: Option<ProxyConfig>,
144
145    /// Use zstd compression
146    #[builder(default = true)]
147    pub zstd: bool,
148
149    /// Encryption key if using native HTTPCrypt encryption (must be in Rspamd base32 format)
150    #[builder(default, setter(strip_option))]
151    pub encryption_key: Option<String>,
152}