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    /// Optional additional headers
70    #[builder(default)]
71    pub additional_headers: HashMap<String, String>,
72}
73
74impl IntoIterator for EnvelopeData {
75    type Item = (String, String);
76    type IntoIter = std::collections::hash_map::IntoIter<String, String>;
77
78    /// Convert the EnvelopeData struct into an iterator
79    fn into_iter(mut self) -> Self::IntoIter {
80        // We add all options to the additional headers
81        if let Some(from) = self.from {
82            self.additional_headers.insert("From".to_string(), from);
83        }
84        if let Some(ip) = self.ip {
85            self.additional_headers.insert("IP".to_string(), ip);
86        }
87        if let Some(user) = self.user {
88            self.additional_headers.insert("User".to_string(), user);
89        }
90        if let Some(helo) = self.helo {
91            self.additional_headers.insert("Helo".to_string(), helo);
92        }
93        if let Some(hostname) = self.hostname {
94            self.additional_headers.insert("Hostname".to_string(), hostname);
95        }
96        if let Some(file_path) = self.file_path {
97            self.additional_headers.insert("File".to_string(), file_path);
98        }
99        for rcpt in self.rcpt {
100            self.additional_headers.insert("Rcpt".to_string(), rcpt);
101        }
102        self.additional_headers.into_iter()
103    }
104}
105
106/// Configuration for Rspamd client
107#[derive(TypedBuilder,Debug, PartialEq)]
108pub struct Config {
109    /// Base URL of Rspamd server
110    pub base_url: String,
111
112    /// Optional API key for authentication
113    #[builder(default, setter(strip_option))]
114    pub password: Option<String>,
115
116    /// Timeout duration for requests
117    #[builder(default=30.0)]
118    pub timeout: f64,
119
120    /// Number of retries for requests
121    #[builder(default=1)]
122    pub retries: u32,
123
124    /// Custom TLS settings for the asynchronous client
125    #[builder(default, setter(strip_option))]
126    pub tls_settings: Option<TlsSettings>,
127
128    /// Proxy configuration for the asynchronous client
129    #[builder(default, setter(strip_option))]
130    pub proxy_config: Option<ProxyConfig>,
131
132    /// Use zstd compression
133    #[builder(default=true)]
134    pub zstd: bool,
135
136    /// Encryption key if using native HTTPCrypt encryption (must be in Rspamd base32 format)
137    #[builder(default, setter(strip_option))]
138    pub encryption_key: Option<String>,
139}