dust-mail 0.4.3

A multi protocol email client
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
mod constants;

use std::{collections::HashMap, fmt::Display};

use async_native_tls::{TlsConnector, TlsStream};
use async_pop::{
    response::{
        types::DataType,
        uidl::{UidlResponse, UniqueId},
    },
    sasl::OAuth2Authenticator,
};
use async_trait::async_trait;

use crate::{
    client::{
        builder::MessageBuilder,
        connection::ConnectionSecurity,
        protocol::{Credentials, IncomingProtocol, PopCredentials, ServerCredentials},
    },
    error::{err, ErrorKind, Result},
    runtime::{
        io::{Read, Write},
        net::TcpStream,
    },
    tree::Node,
};

use self::constants::ACTIVITY_TIMEOUT;

use super::types::{
    flag::Flag,
    mailbox::{Mailbox, MailboxStats},
    message::{Message, Preview},
};

pub struct PopClient<S: Read + Write + Unpin + Send> {
    session: async_pop::Client<S>,
}

impl<S: Read + Write + Unpin + Send> PopClient<S> {
    pub async fn login<U: AsRef<str>, P: AsRef<str>>(
        mut self,
        username: U,
        password: P,
    ) -> Result<PopSession<S>> {
        self.session.login(username, password).await?;

        let session = PopSession::new(self.session);

        Ok(session)
    }

    pub async fn oauth_login<U: AsRef<str>, T: AsRef<str>>(
        mut self,
        username: U,
        token: T,
    ) -> Result<PopSession<S>> {
        let oauth_authenticator = OAuth2Authenticator::new(username.as_ref(), token.as_ref());

        self.session.auth(oauth_authenticator).await?;

        let session = PopSession::new(self.session);

        Ok(session)
    }
}

struct UniqueIdMap {
    map: HashMap<String, usize>,
}

impl UniqueIdMap {
    fn new() -> Self {
        Self {
            map: HashMap::new(),
        }
    }

    fn reset(&mut self) {
        self.map.clear();
    }

    fn get_id(&self, index: usize) -> Option<&str> {
        for (curr_id, curr_index) in &self.map {
            if &index == curr_index {
                return Some(curr_id);
            }
        }

        None
    }

    fn get<I: AsRef<str>>(&self, id: I) -> Option<usize> {
        self.map.get(id.as_ref()).map(|index| *index)
    }

    fn set<I: Display>(&mut self, id: I, index: usize) {
        self.map.insert(id.to_string(), index);
    }

    fn extend<'a, L: IntoIterator<Item = &'a UniqueId>>(&mut self, list: L) -> Result<()> {
        for unique_id in list {
            self.set(unique_id.id(), unique_id.index().value()?)
        }

        Ok(())
    }
}

pub struct PopSession<S: Read + Write + Unpin + Send> {
    session: async_pop::Client<S>,
    unique_id_map: UniqueIdMap,
}

pub async fn connect<S: AsRef<str>, P: Into<u16>>(
    server: S,
    port: P,
) -> Result<PopClient<TlsStream<TcpStream>>> {
    let tls = TlsConnector::new();

    let tcp_stream = TcpStream::connect((server.as_ref(), port.into())).await?;

    let tls_stream = tls.connect(server.as_ref(), tcp_stream).await?;

    let session = async_pop::new(tls_stream).await?;

    Ok(PopClient { session })
}

pub async fn connect_plain<S: AsRef<str>, P: Into<u16>>(
    server: S,
    port: P,
) -> Result<PopClient<TcpStream>> {
    let tcp_stream = TcpStream::connect((server.as_ref(), port.into())).await?;

    let session = async_pop::new(tcp_stream).await?;

    Ok(PopClient { session })
}

async fn login<S: Read + Write + Unpin + Send>(
    client: PopClient<S>,
    credentials: &Credentials,
) -> Result<PopSession<S>> {
    match credentials {
        Credentials::Password { username, password } => {
            let session = client.login(username, password).await?;

            Ok(session)
        }
        Credentials::OAuth { username, token } => {
            let session = client.oauth_login(username, token).await?;

            Ok(session)
        }
    }
}

pub async fn create(
    credentials: &PopCredentials,
) -> Result<Box<dyn IncomingProtocol + Sync + Send>> {
    match credentials.server().security() {
        ConnectionSecurity::Tls => {
            let client =
                connect(credentials.server().domain(), credentials.server().port()).await?;

            let session = login(client, credentials.credentials()).await?;

            Ok(Box::new(session))
        }
        _ => {
            let client =
                connect_plain(credentials.server().domain(), credentials.server().port()).await?;

            let session = login(client, credentials.credentials()).await?;

            Ok(Box::new(session))
        }
    }
}

impl<S: Read + Write + Unpin + Send> PopSession<S> {
    pub fn new(session: async_pop::Client<S>) -> Self {
        Self {
            session,

            unique_id_map: UniqueIdMap::new(),
        }
    }

    async fn get_stats(&mut self) -> Result<MailboxStats> {
        let stats = self.session.stat().await?;

        let message_count = stats.counter();

        let stats = MailboxStats::new(0, message_count.value()?);

        Ok(stats)
    }

    /// Fetches the message count from the pop server and creates a new 'fake' mailbox.
    ///
    /// We do this because Pop does not support mailboxes.
    async fn get_inbox(&mut self) -> Result<Mailbox> {
        let stats = self.get_stats().await?;

        // Create default inbox with stats
        let mailbox: Mailbox = stats.into();

        Ok(mailbox)
    }

    async fn update_uidl_map(&mut self) -> Result<()> {
        let uidl = match self.session.uidl(None).await? {
            UidlResponse::Multiple(list) => list,
            _ => unreachable!(),
        };

        self.unique_id_map.extend(uidl.items())?;

        Ok(())
    }

    async fn get_index<T: AsRef<str>>(&mut self, unique_id: T) -> Result<usize> {
        if let Some(index) = self.unique_id_map.get(&unique_id) {
            return Ok(index);
        };

        self.update_uidl_map().await?;

        match self.unique_id_map.get(&unique_id) {
            Some(msg_number) => Ok(msg_number),
            None => err!(
                ErrorKind::MessageNotFound,
                "Could not find a message with id {}",
                unique_id.as_ref()
            ),
        }
    }
}

#[async_trait]
impl<S: Read + Write + Unpin + Send> IncomingProtocol for PopSession<S> {
    async fn send_keep_alive(&mut self) -> Result<()> {
        self.session.noop().await?;

        Ok(())
    }

    fn should_keep_alive(&self) -> bool {
        match self.session.last_activity() {
            Some(last_activity) => last_activity.elapsed() > ACTIVITY_TIMEOUT,
            None => false,
        }
    }

    async fn get_mailbox_list(&mut self) -> Result<Node<Mailbox>> {
        Ok(self.get_inbox().await?.into())
    }

    async fn get_mailbox(&mut self, _mailbox_id: &str) -> Result<Node<Mailbox>> {
        Ok(self.get_inbox().await?.into())
    }

    async fn logout(&mut self) -> Result<()> {
        self.unique_id_map.reset();

        self.session.quit().await?;

        Ok(())
    }

    async fn delete_mailbox(&mut self, _: &str) -> Result<()> {
        err!(
            ErrorKind::Unsupported,
            "Pop does not support deleting mailboxes",
        )
    }

    async fn rename_mailbox(&mut self, _: &str, _: &str) -> Result<()> {
        err!(
            ErrorKind::Unsupported,
            "Pop does not support renaming mailboxes",
        )
    }

    async fn create_mailbox(&mut self, _: &str) -> Result<()> {
        err!(
            ErrorKind::Unsupported,
            "Pop does not support creating mailboxes",
        )
    }

    async fn get_messages(&mut self, _: &str, start: usize, end: usize) -> Result<Vec<Preview>> {
        let total_messages = self.get_stats().await?.total();

        let sequence_start = if total_messages < end {
            1
        } else {
            total_messages.saturating_sub(end).saturating_add(1)
        };

        let sequence_end = if total_messages < start {
            1
        } else {
            total_messages.saturating_sub(start).saturating_add(1)
        };

        let msg_count = end.saturating_sub(start) as usize;

        let mut previews: Vec<Preview> = Vec::with_capacity(msg_count);

        for msg_number in sequence_start..sequence_end {
            let unique_id = match self.unique_id_map.get_id(msg_number) {
                Some(id) => id.to_string(),
                None => {
                    let uidl_response = self.session.uidl(Some(msg_number)).await?;

                    let unique_id = match &uidl_response {
                        UidlResponse::Multiple(_) => unreachable!(),
                        UidlResponse::Single(item) => item.id(),
                    };

                    unique_id.value()?
                }
            };

            let body = self.session.top(msg_number, 0).await?;

            let mut flags = vec![Flag::Read];

            // If we have marked a message as deleted, we will add the corresponding flag
            if self.session.is_deleted(&msg_number) {
                flags.push(Flag::Deleted)
            }

            let builder: MessageBuilder = body.as_ref().try_into()?;

            let preview: Preview = builder.flags(flags).id(&unique_id).build()?;

            previews.push(preview)
        }

        Ok(previews)
    }

    async fn get_message(&mut self, _box_id: &str, message_id: &str) -> Result<Message> {
        let msg_number = self.get_index(message_id).await?;

        let body = self.session.retr(msg_number).await?;

        let mut flags = vec![Flag::Read];

        // If we have marked a message as deleted, we will add the corresponding flag
        if self.session.is_deleted(&msg_number) {
            flags.push(Flag::Deleted)
        }

        let builder: MessageBuilder = body.as_ref().try_into()?;

        let message: Message = builder.flags(flags).id(message_id).build()?;

        Ok(message)
    }
}

#[cfg(test)]
mod test {

    use super::*;

    use dotenv::dotenv;
    use std::env;

    async fn create_test_session() -> PopSession<TlsStream<TcpStream>> {
        dotenv().ok();

        let username = env::var("POP_USERNAME").unwrap();
        let password = env::var("POP_PASSWORD").unwrap();

        let server = env::var("POP_SERVER").unwrap();
        let port: u16 = 995;

        let client = super::connect(server, port).await.unwrap();

        let session = client.login(&username, &password).await.unwrap();

        session
    }

    #[cfg_attr(feature = "runtime-async-std", async_std::test)]
    #[cfg_attr(feature = "runtime-tokio", tokio::test)]
    async fn get_messages() {
        let mut session = create_test_session().await;

        let previews = session.get_messages("Inbox", 0, 10).await.unwrap();

        for preview in previews.iter() {
            println!("{:?}", preview);
        }
    }

    #[cfg_attr(feature = "runtime-async-std", async_std::test)]
    #[cfg_attr(feature = "runtime-tokio", tokio::test)]
    async fn get_message() {
        let mut session = create_test_session().await;

        let message = session.get_message("Inbox", "1").await.unwrap();

        println!("{:?}", message.to());
    }
}