email-lib 0.27.0

Cross-platform, asynchronous Rust library to manage emails
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! # Account configuration discovery
//!
//! This module contains the [`serde`] representation of the Mozilla
//! [Autoconfiguration].
//!
//! [Autoconfiguration]: https://wiki.mozilla.org/Thunderbird:Autoconfiguration:ConfigFileFormat

use std::time::Duration;

use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
/// The root level of the Mozilla Autoconfiguration.
pub struct AutoConfig {
    pub version: String,
    pub email_provider: EmailProvider,
    #[serde(rename = "oAuth2")]
    pub oauth2: Option<OAuth2Config>,
}

impl AutoConfig {
    pub fn is_gmail(&self) -> bool {
        self.email_provider.id == "googlemail.com"
    }

    /// The config version
    pub fn version(&self) -> &str {
        &self.version
    }

    /// Information about the email provider for the given email
    /// address, e.g. Google or Microsoft
    pub fn email_provider(&self) -> &EmailProvider {
        &self.email_provider
    }

    /// If the provider supports oAuth2, it SHOULD be specified here,
    /// but some providers don't.
    pub fn oauth2(&self) -> Option<&OAuth2Config> {
        self.oauth2.as_ref()
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct OAuth2Config {
    issuer: String,
    scope: String,
    #[serde(rename = "authURL")]
    auth_url: String,
    #[serde(rename = "tokenURL")]
    token_url: String,
}

impl OAuth2Config {
    /// The implementer of the oAuth2 protocol for this email
    /// provider, which is usually the email provider itself.
    pub fn issuer(&self) -> &str {
        &self.issuer
    }

    /// The scopes needed from the oAuth2 API to be able to login
    /// using an oAuth2 generated token.
    pub fn scope(&self) -> Vec<&str> {
        self.scope.split(' ').collect()
    }

    /// The url where the initial oAuth2 login takes place.
    pub fn auth_url(&self) -> &str {
        &self.auth_url
    }

    /// The url used to refresh the oAuth2 token.
    pub fn token_url(&self) -> &str {
        &self.token_url
    }
}

#[derive(Debug, Deserialize)]
pub struct EmailProvider {
    pub id: String,
    #[serde(rename = "$value")]
    pub properties: Vec<EmailProviderProperty>,
}

impl EmailProvider {
    /// Just an array containing all of the email providers
    /// properties, usefull if you want to get multiple properties in
    /// 1 for loop.
    pub fn properties(&self) -> &Vec<EmailProviderProperty> {
        &self.properties
    }

    /// The email providers unique id.
    pub fn id(&self) -> &str {
        &self.id
    }

    /// The domain name that the email provider uses in their email
    /// addresses.
    pub fn domain(&self) -> Vec<&str> {
        let mut domains: Vec<&str> = Vec::new();

        for property in &self.properties {
            if let EmailProviderProperty::Domain(domain) = property {
                domains.push(domain)
            }
        }

        domains
    }

    /// The email providers display name. e.g. Google Mail
    pub fn display_name(&self) -> Option<&str> {
        for property in &self.properties {
            if let EmailProviderProperty::DisplayName(display_name) = property {
                return Some(display_name);
            }
        }

        None
    }

    /// The email providers short display name. e.g. GMail
    pub fn display_short_name(&self) -> Option<&str> {
        for property in &self.properties {
            if let EmailProviderProperty::DisplayShortName(short_name) = property {
                return Some(short_name);
            }
        }

        None
    }

    /// An array containing info about all of an email providers
    /// incoming mail servers
    pub fn incoming_servers(&self) -> Vec<&Server> {
        let mut servers: Vec<&Server> = Vec::new();

        for property in &self.properties {
            if let EmailProviderProperty::IncomingServer(server) = property {
                servers.push(server)
            }
        }

        servers
    }

    /// An array containing info about all of an email providers
    /// outgoing mail servers
    pub fn outgoing_servers(&self) -> Vec<&Server> {
        let mut servers: Vec<&Server> = Vec::new();

        for property in &self.properties {
            if let EmailProviderProperty::OutgoingServer(server) = property {
                servers.push(server)
            }
        }

        servers
    }

    /// An array containing info about all of an email providers mail
    /// servers
    pub fn servers(&self) -> Vec<&Server> {
        let mut servers: Vec<&Server> = Vec::new();

        for property in &self.properties {
            match property {
                EmailProviderProperty::IncomingServer(server) => servers.push(server),
                EmailProviderProperty::OutgoingServer(server) => servers.push(server),
                _ => {}
            }
        }

        servers
    }

    /// Documentation on how to setup the email client, provided by
    /// the email provider.
    pub fn documentation(&self) -> Option<&Documentation> {
        for property in &self.properties {
            if let EmailProviderProperty::Documentation(documentation) = property {
                return Some(documentation);
            }
        }

        None
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum EmailProviderProperty {
    Domain(String),
    DisplayName(String),
    DisplayShortName(String),
    IncomingServer(Server),
    OutgoingServer(Server),
    Documentation(Documentation),
}

#[derive(Debug, Deserialize)]
pub struct Server {
    pub r#type: ServerType,
    #[serde(rename = "$value")]
    pub properties: Vec<ServerProperty>,
}

impl Server {
    /// Just an array containing all of a mail servers properties,
    /// usefull if you want to get multiple properties in 1 for loop.
    pub fn properties(&self) -> &Vec<ServerProperty> {
        &self.properties
    }

    /// What type of mail server this server is.
    pub fn server_type(&self) -> &ServerType {
        &self.r#type
    }

    /// The mail servers domain/ip
    pub fn hostname(&self) -> Option<&str> {
        for property in &self.properties {
            if let ServerProperty::Hostname(hostname) = property {
                return Some(hostname);
            }
        }

        None
    }

    /// The mail servers port
    pub fn port(&self) -> Option<&u16> {
        for property in &self.properties {
            if let ServerProperty::Port(port) = property {
                return Some(port);
            }
        }

        None
    }

    /// The kind of security the mail server prefers
    pub fn security_type(&self) -> Option<&SecurityType> {
        for property in &self.properties {
            if let ServerProperty::SocketType(socket_type) = property {
                return Some(socket_type);
            }
        }

        None
    }

    /// The kind of authentication is needed to login to this mail
    /// server
    pub fn authentication_type(&self) -> Vec<&AuthenticationType> {
        let mut types: Vec<&AuthenticationType> = Vec::new();

        for property in &self.properties {
            if let ServerProperty::Authentication(authentication_type) = property {
                types.push(authentication_type)
            }
        }

        types
    }

    /// The users username
    pub fn username(&self) -> Option<&str> {
        for property in &self.properties {
            if let ServerProperty::Username(username) = property {
                return Some(username);
            }
        }

        None
    }

    /// The users password
    pub fn password(&self) -> Option<&str> {
        for property in &self.properties {
            if let ServerProperty::Password(password) = property {
                return Some(password);
            }
        }

        None
    }
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ServerProperty {
    Hostname(String),
    Port(u16),
    SocketType(SecurityType),
    Authentication(AuthenticationType),
    OwaURL(String),
    EwsURL(String),
    UseGlobalPreferredServer(bool),
    Pop3(Pop3Config),
    Username(String),
    Password(String),
}

#[derive(Debug, Deserialize)]
pub enum SecurityType {
    #[serde(rename = "plain")]
    Plain,
    #[serde(rename = "STARTTLS")]
    Starttls,
    #[serde(rename = "SSL")]
    Tls,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum ServerType {
    Exchange,
    #[cfg(feature = "imap")]
    Imap,
    #[cfg(feature = "smtp")]
    Smtp,
    #[serde(other)]
    Unknown,
}

#[derive(Debug, Deserialize)]
pub enum AuthenticationType {
    #[serde(rename = "password-cleartext")]
    PasswordCleartext,
    #[serde(rename = "password-encrypted")]
    PasswordEncrypted,
    #[serde(rename = "NTLM")]
    Ntlm,
    #[serde(rename = "GSAPI")]
    GsApi,
    #[serde(rename = "client-IP-address")]
    ClientIPAddress,
    #[serde(rename = "TLS-client-cert")]
    TlsClientCert,
    OAuth2,
    #[serde(rename = "None")]
    None,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Pop3Config {
    leave_messages_on_server: bool,
    download_on_biff: Option<bool>,
    days_to_leave_messages_on_server: Option<u64>,
    check_interval: Option<CheckInterval>,
}

impl Pop3Config {
    /// If the server should leave all of the read messages on the
    /// server after the client quits the connection.
    pub fn leave_messages_on_server(&self) -> &bool {
        &self.leave_messages_on_server
    }

    pub fn download_on_biff(&self) -> Option<&bool> {
        self.download_on_biff.as_ref()
    }

    /// How long the Pop messages will be stored on the server.
    pub fn time_to_leave_messages_on_server(&self) -> Option<Duration> {
        self.days_to_leave_messages_on_server
            .as_ref()
            .map(|days| Duration::from_secs(days * 24 * 60 * 60))
    }

    /// The interval in which the server will allow a check for new
    /// messages. Not supported on all servers.
    pub fn check_interval(&self) -> Option<Duration> {
        match self.check_interval.as_ref() {
            Some(interval) => {
                if let Some(minutes) = interval.minutes {
                    return Some(Duration::from_secs(minutes * 60));
                };

                None
            }
            None => None,
        }
    }
}

#[derive(Debug, Deserialize)]
struct CheckInterval {
    minutes: Option<u64>,
}

#[derive(Debug, Deserialize)]
pub struct Documentation {
    url: String,
    #[serde(rename = "$value")]
    properties: Vec<DocumentationDescription>,
}

impl Documentation {
    /// Where the documentation can be found.
    pub fn url(&self) -> &str {
        &self.url
    }

    /// The documentation in different languages.
    pub fn properties(&self) -> &Vec<DocumentationDescription> {
        &self.properties
    }
}

#[derive(Debug, Deserialize)]
pub struct DocumentationDescription {
    lang: Option<String>,
    #[serde(rename = "$value")]
    description: String,
}

impl DocumentationDescription {
    /// What language the documentation is in.
    pub fn language(&self) -> Option<&str> {
        self.lang.as_deref()
    }

    /// The documentation.
    pub fn description(&self) -> &str {
        &self.description
    }
}