|
| 1 | +//! The IMAP NAMESPACE Extension |
| 2 | +
|
| 3 | +use std::io::Write; |
| 4 | + |
| 5 | +use abnf_core::streaming::{dquote, sp}; |
| 6 | +use imap_types::{ |
| 7 | + command::CommandBody, |
| 8 | + core::Vec1, |
| 9 | + extensions::namespace::{Namespace, NamespaceResponseExtension, Namespaces}, |
| 10 | + response::Data, |
| 11 | +}; |
| 12 | +use nom::{ |
| 13 | + branch::alt, |
| 14 | + bytes::streaming::tag_no_case, |
| 15 | + character::streaming::char, |
| 16 | + combinator::{map, value}, |
| 17 | + multi::{many0, many1, separated_list1}, |
| 18 | + sequence::{delimited, preceded, tuple}, |
| 19 | +}; |
| 20 | + |
| 21 | +use crate::{ |
| 22 | + core::{nil, quoted_char, string}, |
| 23 | + decode::IMAPResult, |
| 24 | + encode::{EncodeContext, EncodeIntoContext}, |
| 25 | +}; |
| 26 | + |
| 27 | +/// ```abnf |
| 28 | +/// namespace = "NAMESPACE" |
| 29 | +/// ``` |
| 30 | +pub(crate) fn namespace_command(input: &[u8]) -> IMAPResult<&[u8], CommandBody> { |
| 31 | + value(CommandBody::Namespace, tag_no_case(b"NAMESPACE"))(input) |
| 32 | +} |
| 33 | + |
| 34 | +/// ```abnf |
| 35 | +/// ;; The first Namespace is the Personal Namespace(s) |
| 36 | +/// ;; The second Namespace is the Other Users' Namespace(s) |
| 37 | +/// ;; The third Namespace is the Shared Namespace(s) |
| 38 | +/// Namespace-Response = "NAMESPACE" SP Namespace SP Namespace SP Namespace |
| 39 | +/// ``` |
| 40 | +pub(crate) fn namespace_response(input: &[u8]) -> IMAPResult<&[u8], Data> { |
| 41 | + let mut parser = tuple(( |
| 42 | + tag_no_case("NAMESPACE "), |
| 43 | + namespace, |
| 44 | + preceded(sp, namespace), |
| 45 | + preceded(sp, namespace), |
| 46 | + )); |
| 47 | + |
| 48 | + let (remaining, (_, personal, other, shared)) = parser(input)?; |
| 49 | + |
| 50 | + Ok(( |
| 51 | + remaining, |
| 52 | + Data::Namespace { |
| 53 | + personal, |
| 54 | + other, |
| 55 | + shared, |
| 56 | + }, |
| 57 | + )) |
| 58 | +} |
| 59 | + |
| 60 | +/// ```abnf |
| 61 | +/// Namespace = nil / "(" 1*Namespace-Descr ")" |
| 62 | +/// ``` |
| 63 | +fn namespace(input: &[u8]) -> IMAPResult<&[u8], Namespaces> { |
| 64 | + alt(( |
| 65 | + map(nil, |_| Vec::new()), |
| 66 | + delimited(char('('), many1(namespace_descr), char(')')), |
| 67 | + ))(input) |
| 68 | +} |
| 69 | + |
| 70 | +/// ```abnf |
| 71 | +/// Namespace-Descr = "(" |
| 72 | +/// string |
| 73 | +/// SP |
| 74 | +/// (DQUOTE QUOTED-CHAR DQUOTE / nil) |
| 75 | +/// *(Namespace-Response-Extension) |
| 76 | +/// ")" |
| 77 | +/// ``` |
| 78 | +fn namespace_descr(input: &[u8]) -> IMAPResult<&[u8], Namespace> { |
| 79 | + map( |
| 80 | + delimited( |
| 81 | + char('('), |
| 82 | + tuple(( |
| 83 | + string, |
| 84 | + sp, |
| 85 | + alt(( |
| 86 | + map(delimited(dquote, quoted_char, dquote), Some), |
| 87 | + value(None, nil), |
| 88 | + )), |
| 89 | + many0(namespace_response_extension), |
| 90 | + )), |
| 91 | + char(')'), |
| 92 | + ), |
| 93 | + |(prefix, _, delimiter, extensions)| Namespace { |
| 94 | + prefix, |
| 95 | + delimiter, |
| 96 | + extensions, |
| 97 | + }, |
| 98 | + )(input) |
| 99 | +} |
| 100 | + |
| 101 | +/// ```abnf |
| 102 | +/// Namespace-Response-Extension = SP string SP "(" string *(SP string) ")" |
| 103 | +/// ``` |
| 104 | +fn namespace_response_extension(input: &[u8]) -> IMAPResult<&[u8], NamespaceResponseExtension> { |
| 105 | + map( |
| 106 | + tuple(( |
| 107 | + preceded(sp, string), |
| 108 | + preceded( |
| 109 | + sp, |
| 110 | + delimited(char('('), separated_list1(sp, string), char(')')), |
| 111 | + ), |
| 112 | + )), |
| 113 | + |(key, values)| NamespaceResponseExtension { |
| 114 | + key, |
| 115 | + // We can use `unvalidated` because we know the vector has at least one element due to the `separated_list1` call above. |
| 116 | + values: Vec1::unvalidated(values), |
| 117 | + }, |
| 118 | + )(input) |
| 119 | +} |
| 120 | + |
| 121 | +impl EncodeIntoContext for Namespace<'_> { |
| 122 | + fn encode_ctx(&self, ctx: &mut EncodeContext) -> std::io::Result<()> { |
| 123 | + write!(ctx, "(")?; |
| 124 | + self.prefix.encode_ctx(ctx)?; |
| 125 | + write!(ctx, " ")?; |
| 126 | + |
| 127 | + match &self.delimiter { |
| 128 | + Some(delimiter_char) => { |
| 129 | + write!(ctx, "\"")?; |
| 130 | + delimiter_char.encode_ctx(ctx)?; |
| 131 | + write!(ctx, "\"")?; |
| 132 | + } |
| 133 | + None => { |
| 134 | + ctx.write_all(b"NIL")?; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + for ext in &self.extensions { |
| 139 | + ext.encode_ctx(ctx)?; |
| 140 | + } |
| 141 | + |
| 142 | + write!(ctx, ")") |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +impl EncodeIntoContext for NamespaceResponseExtension<'_> { |
| 147 | + fn encode_ctx(&self, ctx: &mut EncodeContext) -> std::io::Result<()> { |
| 148 | + write!(ctx, " ")?; |
| 149 | + self.key.encode_ctx(ctx)?; |
| 150 | + |
| 151 | + write!(ctx, " (")?; |
| 152 | + if let Some((last, head)) = self.values.as_ref().split_last() { |
| 153 | + for value in head { |
| 154 | + value.encode_ctx(ctx)?; |
| 155 | + write!(ctx, " ")?; |
| 156 | + } |
| 157 | + last.encode_ctx(ctx)?; |
| 158 | + } |
| 159 | + write!(ctx, ")") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +pub fn encode_namespaces(ctx: &mut EncodeContext, list: &Namespaces<'_>) -> std::io::Result<()> { |
| 164 | + if list.is_empty() { |
| 165 | + ctx.write_all(b"NIL") |
| 166 | + } else { |
| 167 | + ctx.write_all(b"(")?; |
| 168 | + for desc in list { |
| 169 | + desc.encode_ctx(ctx)?; |
| 170 | + } |
| 171 | + ctx.write_all(b")") |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +#[cfg(test)] |
| 176 | +mod tests { |
| 177 | + use super::namespace_response; |
| 178 | + |
| 179 | + #[test] |
| 180 | + fn parse_namespace_response() { |
| 181 | + let tests = [ |
| 182 | + b"NAMESPACE ((\"0\" \"\\\"\")) NIL NIL\r\n".as_ref(), |
| 183 | + #[cfg(feature = "ext_utf8")] |
| 184 | + b"NAMESPACE ((\"^^\x00\" \"\x07\")) NIL NIL\r\n", |
| 185 | + ]; |
| 186 | + |
| 187 | + for test in tests.into_iter() { |
| 188 | + namespace_response(test).unwrap(); |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments