aboutsummaryrefslogtreecommitdiff
path: root/src/imap/command/selected.rs
blob: 40e75e272240ea46ea98b5914627bf87a23a3876 (plain) (blame)
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
use std::sync::Arc;

use anyhow::{bail, Result};
use boitalettres::proto::Request;
use boitalettres::proto::Response;
use imap_codec::types::command::CommandBody;
use imap_codec::types::flag::{Flag, StoreResponse, StoreType};
use imap_codec::types::mailbox::Mailbox as MailboxCodec;
use imap_codec::types::response::Code;
use imap_codec::types::sequence::SequenceSet;

use crate::imap::command::examined;
use crate::imap::flow;
use crate::imap::mailbox_view::MailboxView;

use crate::mail::user::User;

pub struct SelectedContext<'a> {
    pub req: &'a Request,
    pub user: &'a Arc<User>,
    pub mailbox: &'a mut MailboxView,
}

pub async fn dispatch<'a>(ctx: SelectedContext<'a>) -> Result<(Response, flow::Transition)> {
    match &ctx.req.command.body {
        // Only write commands here, read commands are handled in
        // `examined.rs`
        CommandBody::Close => ctx.close().await,
        CommandBody::Expunge => ctx.expunge().await,
        CommandBody::Store {
            sequence_set,
            kind,
            response,
            flags,
            uid,
        } => ctx.store(sequence_set, kind, response, flags, uid).await,
        CommandBody::Copy {
            sequence_set,
            mailbox,
            uid,
        } => ctx.copy(sequence_set, mailbox, uid).await,
        _ => {
            let ctx = examined::ExaminedContext {
                req: ctx.req,
                user: ctx.user,
                mailbox: ctx.mailbox,
            };
            examined::dispatch(ctx).await
        }
    }
}

// --- PRIVATE ---

impl<'a> SelectedContext<'a> {
    async fn close(self) -> Result<(Response, flow::Transition)> {
        // We expunge messages,
        // but we don't send the untagged EXPUNGE responses
        self.expunge().await?;
        Ok((Response::ok("CLOSE completed")?, flow::Transition::Unselect))
    }

    async fn expunge(self) -> Result<(Response, flow::Transition)> {
        let data = self.mailbox.expunge().await?;

        Ok((
            Response::ok("EXPUNGE completed")?.with_body(data),
            flow::Transition::None,
        ))
    }

    async fn store(
        self,
        sequence_set: &SequenceSet,
        kind: &StoreType,
        response: &StoreResponse,
        flags: &[Flag],
        uid: &bool,
    ) -> Result<(Response, flow::Transition)> {
        let data = self
            .mailbox
            .store(sequence_set, kind, response, flags, uid)
            .await?;

        Ok((
            Response::ok("STORE completed")?.with_body(data),
            flow::Transition::None,
        ))
    }

    async fn copy(
        self,
        sequence_set: &SequenceSet,
        mailbox: &MailboxCodec,
        uid: &bool,
    ) -> Result<(Response, flow::Transition)> {
        let name = String::try_from(mailbox.clone())?;

        let mb_opt = self.user.open_mailbox(&name).await?;
        let mb = match mb_opt {
            Some(mb) => mb,
            None => bail!("Mailbox does not exist"),
        };

        let (uidval, uid_map) = self.mailbox.copy(sequence_set, mb, uid).await?;

        let copyuid_str = format!(
            "{} {} {}",
            uidval,
            uid_map
                .iter()
                .map(|(sid, _)| format!("{}", sid))
                .collect::<Vec<_>>()
                .join(","),
            uid_map
                .iter()
                .map(|(_, tuid)| format!("{}", tuid))
                .collect::<Vec<_>>()
                .join(",")
        );

        Ok((
            Response::ok("COPY completed")?.with_extra_code(Code::Other(
                "COPYUID".try_into().unwrap(),
                Some(copyuid_str),
            )),
            flow::Transition::None,
        ))
    }
}