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
/*
 * meli - nntp module.
 *
 * Copyright 2017 - 2019 Manos Pitsidianakis
 *
 * This file is part of meli.
 *
 * meli is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * meli is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with meli. If not, see <http://www.gnu.org/licenses/>.
 */

use super::*;

use crate::backends::*;
use crate::email::*;
use crate::error::MeliError;
use std::sync::Arc;

/// `BackendOp` implementor for Nntp
#[derive(Debug, Clone)]
pub struct NntpOp {
    uid: usize,
    mailbox_hash: MailboxHash,
    connection: Arc<FutureMutex<NntpConnection>>,
    uid_store: Arc<UIDStore>,
}

impl NntpOp {
    pub fn new(
        uid: usize,
        mailbox_hash: MailboxHash,
        connection: Arc<FutureMutex<NntpConnection>>,
        uid_store: Arc<UIDStore>,
    ) -> Self {
        NntpOp {
            uid,
            connection,
            mailbox_hash,
            uid_store,
        }
    }
}

impl BackendOp for NntpOp {
    fn as_bytes(&mut self) -> ResultFuture<Vec<u8>> {
        let mailbox_hash = self.mailbox_hash;
        let uid = self.uid;
        let uid_store = self.uid_store.clone();
        let connection = self.connection.clone();
        Ok(Box::pin(async move {
            let mut res = String::with_capacity(8 * 1024);
            let mut conn = connection.lock().await;
            let path = uid_store.mailboxes.lock().await[&mailbox_hash]
                .name()
                .to_string();
            conn.send_command(format!("GROUP {}", path).as_bytes())
                .await?;
            conn.read_response(&mut res, false, &["211 "]).await?;
            if !res.starts_with("211 ") {
                return Err(MeliError::new(format!(
                    "{} Could not select newsgroup {}: expected GROUP response but got: {}",
                    &uid_store.account_name, path, res
                )));
            }
            conn.send_command(format!("ARTICLE {}", uid).as_bytes())
                .await?;
            conn.read_response(&mut res, true, &["220 "]).await?;
            if !res.starts_with("220 ") {
                return Err(MeliError::new(format!(
                    "{} Could not select article {}: expected ARTICLE response but got: {}",
                    &uid_store.account_name, path, res
                )));
            }
            let pos = res.find("\r\n").unwrap_or(0) + 2;

            Ok(res.as_bytes()[pos..].to_vec())
        }))
    }

    fn fetch_flags(&self) -> ResultFuture<Flag> {
        Ok(Box::pin(async move { Ok(Flag::default()) }))
    }
}