aerogramme/src/imap/capability.rs

160 lines
4.1 KiB
Rust
Raw Normal View History

2024-01-18 17:03:21 +00:00
use imap_codec::imap_types::command::{FetchModifier, SelectExamineModifier, StoreModifier};
2024-02-22 16:30:40 +00:00
use imap_codec::imap_types::core::Vec1;
2024-01-03 19:53:07 +00:00
use imap_codec::imap_types::extensions::enable::{CapabilityEnable, Utf8Kind};
2024-01-03 19:53:25 +00:00
use imap_codec::imap_types::response::Capability;
2024-01-03 19:53:07 +00:00
use std::collections::HashSet;
2024-01-03 11:29:19 +00:00
use crate::imap::attributes::AttributesProxy;
2024-01-03 15:52:31 +00:00
fn capability_unselect() -> Capability<'static> {
Capability::try_from("UNSELECT").unwrap()
}
fn capability_condstore() -> Capability<'static> {
Capability::try_from("CONDSTORE").unwrap()
}
2024-01-20 10:45:32 +00:00
fn capability_uidplus() -> Capability<'static> {
Capability::try_from("UIDPLUS").unwrap()
}
2024-01-20 18:23:44 +00:00
fn capability_liststatus() -> Capability<'static> {
Capability::try_from("LIST-STATUS").unwrap()
}
2024-01-10 16:07:07 +00:00
/*
2024-01-03 15:52:31 +00:00
fn capability_qresync() -> Capability<'static> {
Capability::try_from("QRESYNC").unwrap()
}
2024-01-10 16:07:07 +00:00
*/
2024-01-03 15:52:31 +00:00
2024-01-03 11:29:19 +00:00
#[derive(Debug, Clone)]
2024-01-03 19:53:07 +00:00
pub struct ServerCapability(HashSet<Capability<'static>>);
2024-01-03 11:29:19 +00:00
impl Default for ServerCapability {
fn default() -> Self {
2024-01-03 19:53:07 +00:00
Self(HashSet::from([
Capability::Imap4Rev1,
Capability::Enable,
2024-01-03 19:53:07 +00:00
Capability::Move,
2024-01-03 20:29:36 +00:00
Capability::LiteralPlus,
2024-01-19 13:13:43 +00:00
Capability::Idle,
2024-01-03 19:53:07 +00:00
capability_unselect(),
capability_condstore(),
2024-01-20 10:45:32 +00:00
capability_uidplus(),
2024-01-20 18:23:44 +00:00
capability_liststatus(),
2024-01-03 19:53:07 +00:00
//capability_qresync(),
]))
2024-01-03 11:29:19 +00:00
}
}
impl ServerCapability {
2024-02-22 16:30:40 +00:00
pub fn to_vec(&self) -> Vec1<Capability<'static>> {
2024-01-03 19:53:25 +00:00
self.0
.iter()
.map(|v| v.clone())
.collect::<Vec<_>>()
.try_into()
.unwrap()
2024-01-03 11:29:19 +00:00
}
2024-01-03 15:52:31 +00:00
2024-01-03 19:53:07 +00:00
#[allow(dead_code)]
2024-01-03 15:52:31 +00:00
pub fn support(&self, cap: &Capability<'static>) -> bool {
2024-01-03 19:53:07 +00:00
self.0.contains(cap)
2024-01-03 15:52:31 +00:00
}
}
#[derive(Clone)]
2024-01-09 16:40:23 +00:00
pub enum ClientStatus {
2024-01-03 19:53:07 +00:00
NotSupportedByServer,
Disabled,
Enabled,
}
2024-01-09 16:40:23 +00:00
impl ClientStatus {
pub fn is_enabled(&self) -> bool {
matches!(self, Self::Enabled)
}
pub fn enable(&self) -> Self {
match self {
Self::Disabled => Self::Enabled,
other => other.clone(),
}
}
2024-01-09 16:40:23 +00:00
}
2024-01-03 15:52:31 +00:00
pub struct ClientCapability {
2024-01-09 16:40:23 +00:00
pub condstore: ClientStatus,
pub utf8kind: Option<Utf8Kind>,
2024-01-03 15:52:31 +00:00
}
2024-01-03 19:53:07 +00:00
impl ClientCapability {
pub fn new(sc: &ServerCapability) -> Self {
2024-01-03 15:52:31 +00:00
Self {
2024-01-03 19:53:07 +00:00
condstore: match sc.0.contains(&capability_condstore()) {
true => ClientStatus::Disabled,
_ => ClientStatus::NotSupportedByServer,
},
utf8kind: None,
2024-01-03 15:52:31 +00:00
}
}
2024-01-10 16:07:07 +00:00
pub fn enable_condstore(&mut self) {
self.condstore = self.condstore.enable();
}
pub fn attributes_enable(&mut self, ap: &AttributesProxy) {
if ap.is_enabling_condstore() {
self.enable_condstore()
}
}
pub fn fetch_modifiers_enable(&mut self, mods: &[FetchModifier]) {
2024-01-18 17:03:21 +00:00
if mods
.iter()
.any(|x| matches!(x, FetchModifier::ChangedSince(..)))
{
self.enable_condstore()
}
}
pub fn store_modifiers_enable(&mut self, mods: &[StoreModifier]) {
2024-01-18 17:03:21 +00:00
if mods
.iter()
.any(|x| matches!(x, StoreModifier::UnchangedSince(..)))
{
self.enable_condstore()
}
}
2024-01-11 15:55:37 +00:00
pub fn select_enable(&mut self, mods: &[SelectExamineModifier]) {
for m in mods.iter() {
match m {
SelectExamineModifier::Condstore => self.enable_condstore(),
}
}
}
2024-01-03 15:52:31 +00:00
pub fn try_enable(
&mut self,
2024-01-03 19:53:07 +00:00
caps: &[CapabilityEnable<'static>],
) -> Vec<CapabilityEnable<'static>> {
2024-01-03 15:52:31 +00:00
let mut enabled = vec![];
for cap in caps {
match cap {
2024-01-03 19:53:07 +00:00
CapabilityEnable::CondStore if matches!(self.condstore, ClientStatus::Disabled) => {
self.condstore = ClientStatus::Enabled;
enabled.push(cap.clone());
2024-01-03 15:52:31 +00:00
}
2024-01-03 19:53:07 +00:00
CapabilityEnable::Utf8(kind) if Some(kind) != self.utf8kind.as_ref() => {
self.utf8kind = Some(kind.clone());
enabled.push(cap.clone());
2024-01-03 15:52:31 +00:00
}
_ => (),
}
}
enabled
}
2024-01-03 11:29:19 +00:00
}