summaryrefslogtreecommitdiff
path: root/crates/managesieve/src/op/capability.rs
blob: 45d7273a2e3448421208486f36e69417473988dd (plain)
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
/*
 * SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <hello@stalw.art>
 *
 * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
 */

use common::listener::SessionStream;
use jmap_proto::request::capability::Capabilities;

use crate::core::{Session, StatusResponse};

impl<T: SessionStream> Session<T> {
    pub async fn handle_capability(&self, message: &'static str) -> super::OpResult {
        let mut response = Vec::with_capacity(128);
        response.extend_from_slice(b"\"IMPLEMENTATION\" \"Stalwart ManageSieve\"\r\n");
        response.extend_from_slice(b"\"VERSION\" \"1.0\"\r\n");
        if !self.stream.is_tls() {
            response.extend_from_slice(b"\"STARTTLS\"\r\n");
        }
        if self.stream.is_tls() || self.jmap.core.imap.allow_plain_auth {
            response.extend_from_slice(b"\"SASL\" \"PLAIN OAUTHBEARER\"\r\n");
        } else {
            response.extend_from_slice(b"\"SASL\" \"OAUTHBEARER\"\r\n");
        };
        if let Some(sieve) =
            self.jmap
                .core
                .jmap
                .capabilities
                .account
                .iter()
                .find_map(|(_, item)| {
                    if let Capabilities::SieveAccount(sieve) = item {
                        Some(sieve)
                    } else {
                        None
                    }
                })
        {
            response.extend_from_slice(b"\"SIEVE\" \"");
            response.extend_from_slice(sieve.extensions.join(" ").as_bytes());
            response.extend_from_slice(b"\"\r\n");
            if let Some(notification_methods) = &sieve.notification_methods {
                response.extend_from_slice(b"\"NOTIFY\" \"");
                response.extend_from_slice(notification_methods.join(" ").as_bytes());
                response.extend_from_slice(b"\"\r\n");
            }
            if sieve.max_redirects > 0 {
                response.extend_from_slice(b"\"MAXREDIRECTS\" \"");
                response.extend_from_slice(sieve.max_redirects.to_string().as_bytes());
                response.extend_from_slice(b"\"\r\n");
            }
        } else {
            response.extend_from_slice(b"\"SIEVE\" \"\"\r\n");
        }

        Ok(StatusResponse::ok(message).serialize(response))
    }
}