Compare commits

...

5 Commits
master ... jmap

Author SHA1 Message Date
Manos Pitsidianakis a548f7509f
JMAP WIP #5 2019-12-05 00:04:03 +02:00
Manos Pitsidianakis bfa5bab15d
JMAP WIP #4 2019-12-04 19:45:30 +02:00
Manos Pitsidianakis 138c14f730
JMAP WIP #3 2019-12-04 01:04:38 +02:00
Manos Pitsidianakis 994e64d8a6
JMAP WIP #2 2019-12-03 21:29:26 +02:00
Manos Pitsidianakis 2a573af016
JMAP WIP 2019-12-03 13:30:42 +02:00
18 changed files with 3081 additions and 46 deletions

842
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -26,9 +26,11 @@ bincode = "1.2.0"
uuid = { version = "0.7.4", features = ["serde", "v4"] } uuid = { version = "0.7.4", features = ["serde", "v4"] }
text_processing = { path = "../text_processing", version = "*", optional= true } text_processing = { path = "../text_processing", version = "*", optional= true }
libc = {version = "0.2.59", features = ["extra_traits",]} libc = {version = "0.2.59", features = ["extra_traits",]}
reqwest = { version ="0.10.0-alpha.2", optional=true, features = ["json", "blocking" ]}
serde_json = { version = "1.0", optional = true, features = ["raw_value",] }
[features] [features]
default = ["unicode_algorithms", "imap_backend", "maildir_backend", "mbox_backend", "vcard"] default = ["unicode_algorithms", "imap_backend", "maildir_backend", "mbox_backend", "jmap_backend", "vcard"]
debug-tracing = [] debug-tracing = []
unicode_algorithms = ["text_processing"] unicode_algorithms = ["text_processing"]
@ -36,4 +38,5 @@ imap_backend = ["native-tls"]
maildir_backend = ["notify", "notify-rust", "memmap"] maildir_backend = ["notify", "notify-rust", "memmap"]
mbox_backend = ["notify", "notify-rust", "memmap"] mbox_backend = ["notify", "notify-rust", "memmap"]
notmuch_backend = [] notmuch_backend = []
jmap_backend = ["reqwest", "serde_json" ]
vcard = [] vcard = []

View File

@ -28,6 +28,10 @@ pub mod mbox;
pub mod notmuch; pub mod notmuch;
#[cfg(feature = "notmuch_backend")] #[cfg(feature = "notmuch_backend")]
pub use self::notmuch::NotmuchDb; pub use self::notmuch::NotmuchDb;
#[cfg(feature = "jmap_backend")]
pub mod jmap;
#[cfg(feature = "jmap_backend")]
pub use self::jmap::JmapType;
#[cfg(feature = "imap_backend")] #[cfg(feature = "imap_backend")]
pub use self::imap::ImapType; pub use self::imap::ImapType;
@ -119,6 +123,16 @@ impl Backends {
}, },
); );
} }
#[cfg(feature = "jmap_backend")]
{
b.register(
"jmap".to_string(),
Backend {
create_fn: Box::new(|| Box::new(|f, i| JmapType::new(f, i))),
validate_conf_fn: Box::new(JmapType::validate_config),
},
);
}
b b
} }

View File

@ -0,0 +1,276 @@
/*
* meli - jmap module.
*
* Copyright 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 crate::async_workers::{Async, AsyncBuilder, AsyncStatus, WorkContext};
use crate::backends::BackendOp;
use crate::backends::FolderHash;
use crate::backends::RefreshEvent;
use crate::backends::RefreshEventKind::{self, *};
use crate::backends::{BackendFolder, Folder, FolderOperation, MailBackend, RefreshEventConsumer};
use crate::conf::AccountSettings;
use crate::email::*;
use crate::error::{MeliError, Result};
use fnv::{FnvHashMap, FnvHashSet};
use reqwest::blocking::Client;
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;
use std::str::FromStr;
use std::sync::{Arc, Mutex, RwLock};
#[macro_export]
macro_rules! _impl {
($field:ident : $t:ty) => {
pub fn $field(mut self, new_val: $t) -> Self {
self.$field = new_val;
self
}
}
}
#[macro_export]
macro_rules! _impl_get_mut {
($method:ident, $field:ident : $t:ty) => {
pub fn $method(&mut self) -> &mut $t {
&mut self.$field
}
}
}
pub mod connection;
use connection::*;
pub mod protocol;
use protocol::*;
pub mod rfc8620;
use rfc8620::*;
pub mod objects;
use objects::*;
pub mod folder;
use folder::*;
#[derive(Debug, Default)]
pub struct EnvelopeCache {
bytes: Option<String>,
headers: Option<String>,
body: Option<String>,
flags: Option<Flag>,
}
#[derive(Debug, Clone)]
pub struct JmapServerConf {
pub server_hostname: String,
pub server_username: String,
pub server_password: String,
pub server_port: u16,
pub danger_accept_invalid_certs: bool,
}
macro_rules! get_conf_val {
($s:ident[$var:literal]) => {
$s.extra.get($var).ok_or_else(|| {
MeliError::new(format!(
"Configuration error ({}): JMAP connection requires the field `{}` set",
$s.name.as_str(),
$var
))
})
};
($s:ident[$var:literal], $default:expr) => {
$s.extra
.get($var)
.map(|v| {
<_>::from_str(v).map_err(|e| {
MeliError::new(format!(
"Configuration error ({}): Invalid value for field `{}`: {}\n{}",
$s.name.as_str(),
$var,
v,
e
))
})
})
.unwrap_or_else(|| Ok($default))
};
}
impl JmapServerConf {
pub fn new(s: &AccountSettings) -> Result<Self> {
Ok(JmapServerConf {
server_hostname: get_conf_val!(s["server_hostname"])?.to_string(),
server_username: get_conf_val!(s["server_username"])?.to_string(),
server_password: get_conf_val!(s["server_password"])?.to_string(),
server_port: get_conf_val!(s["server_port"], 443)?,
danger_accept_invalid_certs: get_conf_val!(s["danger_accept_invalid_certs"], false)?,
})
}
}
struct IsSubscribedFn(Box<dyn Fn(&str) -> bool + Send + Sync>);
impl std::fmt::Debug for IsSubscribedFn {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "IsSubscribedFn Box")
}
}
impl std::ops::Deref for IsSubscribedFn {
type Target = Box<dyn Fn(&str) -> bool + Send + Sync>;
fn deref(&self) -> &Box<dyn Fn(&str) -> bool + Send + Sync> {
&self.0
}
}
macro_rules! get_conf_val {
($s:ident[$var:literal]) => {
$s.extra.get($var).ok_or_else(|| {
MeliError::new(format!(
"Configuration error ({}): JMAP connection requires the field `{}` set",
$s.name.as_str(),
$var
))
})
};
($s:ident[$var:literal], $default:expr) => {
$s.extra
.get($var)
.map(|v| {
<_>::from_str(v).map_err(|e| {
MeliError::new(format!(
"Configuration error ({}): Invalid value for field `{}`: {}\n{}",
$s.name.as_str(),
$var,
v,
e
))
})
})
.unwrap_or_else(|| Ok($default))
};
}
#[derive(Debug)]
pub struct JmapType {
account_name: String,
online: Arc<Mutex<bool>>,
is_subscribed: Arc<IsSubscribedFn>,
server_conf: JmapServerConf,
connection: Arc<JmapConnection>,
folders: Arc<RwLock<FnvHashMap<FolderHash, JmapFolder>>>,
}
impl MailBackend for JmapType {
fn is_online(&self) -> bool {
*self.online.lock().unwrap()
}
fn get(&mut self, folder: &Folder) -> Async<Result<Vec<Envelope>>> {
let mut w = AsyncBuilder::new();
let folders = self.folders.clone();
let connection = self.connection.clone();
let folder_hash = folder.hash();
let handle = {
let tx = w.tx();
let closure = move |_work_context| {
tx.send(AsyncStatus::Payload(protocol::get(
&connection,
&folders.read().unwrap()[&folder_hash],
)))
.unwrap();
tx.send(AsyncStatus::Finished).unwrap();
};
Box::new(closure)
};
w.build(handle)
}
fn watch(
&self,
sender: RefreshEventConsumer,
work_context: WorkContext,
) -> Result<std::thread::ThreadId> {
Err(MeliError::from("sadfsa"))
}
fn folders(&self) -> Result<FnvHashMap<FolderHash, Folder>> {
if self.folders.read().unwrap().is_empty() {
let folders = std::dbg!(protocol::get_mailboxes(&self.connection))?;
let ret = Ok(folders
.iter()
.map(|(&h, f)| (h, BackendFolder::clone(f) as Folder))
.collect());
*self.folders.write().unwrap() = folders;
ret
} else {
Ok(self
.folders
.read()
.unwrap()
.iter()
.map(|(&h, f)| (h, BackendFolder::clone(f) as Folder))
.collect())
}
}
fn operation(&self, hash: EnvelopeHash) -> Box<dyn BackendOp> {
unimplemented!()
}
fn save(&self, bytes: &[u8], folder: &str, flags: Option<Flag>) -> Result<()> {
Ok(())
}
fn folder_operation(&mut self, path: &str, op: FolderOperation) -> Result<()> {
Ok(())
}
fn as_any(&self) -> &dyn::std::any::Any {
self
}
}
impl JmapType {
pub fn new(
s: &AccountSettings,
is_subscribed: Box<dyn Fn(&str) -> bool + Send + Sync>,
) -> Result<Box<dyn MailBackend>> {
let online = Arc::new(Mutex::new(false));
let server_conf = JmapServerConf::new(s)?;
Ok(Box::new(JmapType {
connection: Arc::new(JmapConnection::new(&server_conf, online.clone())?),
folders: Arc::new(RwLock::new(FnvHashMap::default())),
account_name: s.name.clone(),
online,
is_subscribed: Arc::new(IsSubscribedFn(is_subscribed)),
server_conf,
}))
}
pub fn validate_config(s: &AccountSettings) -> Result<()> {
get_conf_val!(s["server_hostname"])?;
get_conf_val!(s["server_username"])?;
get_conf_val!(s["server_password"])?;
get_conf_val!(s["server_port"], 443)?;
get_conf_val!(s["danger_accept_invalid_certs"], false)?;
Ok(())
}
}

View File

@ -0,0 +1,66 @@
/*
* meli - jmap module.
*
* Copyright 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::*;
#[derive(Debug)]
pub struct JmapConnection {
pub request_no: Arc<Mutex<usize>>,
pub client: Arc<Mutex<Client>>,
pub online_status: Arc<Mutex<bool>>,
pub server_conf: JmapServerConf,
pub account_id: Arc<Mutex<String>>,
}
impl JmapConnection {
pub fn new(server_conf: &JmapServerConf, online_status: Arc<Mutex<bool>>) -> Result<Self> {
use reqwest::header;
let mut headers = header::HeaderMap::new();
headers.insert(
header::AUTHORIZATION,
header::HeaderValue::from_static("fc32dffe-14e7-11ea-a277-2477037a1804"),
);
headers.insert(
header::ACCEPT,
header::HeaderValue::from_static("application/json"),
);
headers.insert(
header::CONTENT_TYPE,
header::HeaderValue::from_static("application/json"),
);
let client = reqwest::blocking::ClientBuilder::new()
.danger_accept_invalid_certs(server_conf.danger_accept_invalid_certs)
.default_headers(headers)
.build()?;
let res_text = client.get(&server_conf.server_hostname).send()?.text()?;
debug!(&res_text);
let server_conf = server_conf.clone();
Ok(JmapConnection {
request_no: Arc::new(Mutex::new(0)),
client: Arc::new(Mutex::new(client)),
online_status,
server_conf,
account_id: Arc::new(Mutex::new(String::new())),
})
}
}

View File

@ -0,0 +1,73 @@
/*
* meli - jmap module.
*
* Copyright 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::FolderPermissions;
#[derive(Debug, Clone)]
pub struct JmapFolder {
pub name: String,
pub path: String,
pub hash: FolderHash,
pub v: Vec<FolderHash>,
pub id: String,
pub is_subscribed: bool,
pub my_rights: JmapRights,
pub parent_id: Option<String>,
pub role: Option<String>,
pub sort_order: u64,
pub total_emails: u64,
pub total_threads: u64,
pub unread_emails: u64,
pub unread_threads: u64,
}
impl BackendFolder for JmapFolder {
fn hash(&self) -> FolderHash {
self.hash
}
fn name(&self) -> &str {
&self.name
}
fn path(&self) -> &str {
&self.path
}
fn change_name(&mut self, _s: &str) {}
fn clone(&self) -> Folder {
Box::new(std::clone::Clone::clone(self))
}
fn children(&self) -> &[FolderHash] {
&self.v
}
fn parent(&self) -> Option<FolderHash> {
None
}
fn permissions(&self) -> FolderPermissions {
FolderPermissions::default()
}
}

View File

@ -0,0 +1,28 @@
/*
* meli - jmap module.
*
* Copyright 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::*;
mod email;
pub use email::*;
mod mailbox;
pub use mailbox::*;

View File

@ -0,0 +1,500 @@
/*
* meli - jmap module.
*
* Copyright 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::jmap::protocol::*;
use crate::backends::jmap::rfc8620::bool_false;
use serde::de::{Deserialize, Deserializer};
use serde_json::Value;
use std::collections::hash_map::DefaultHasher;
use std::collections::HashMap;
use std::hash::Hasher;
// 4.1.1.
// Metadata
// These properties represent metadata about the message in the mail
// store and are not derived from parsing the message itself.
//
// o id: "Id" (immutable; server-set)
//
// The id of the Email object. Note that this is the JMAP object id,
// NOT the Message-ID header field value of the message [RFC5322].
//
// o blobId: "Id" (immutable; server-set)
//
// The id representing the raw octets of the message [RFC5322] for
// this Email. This may be used to download the raw original message
// or to attach it directly to another Email, etc.
//
// o threadId: "Id" (immutable; server-set)
//
// The id of the Thread to which this Email belongs.
//
// o mailboxIds: "Id[Boolean]"
//
// The set of Mailbox ids this Email belongs to. An Email in the
// mail store MUST belong to one or more Mailboxes at all times
// (until it is destroyed). The set is represented as an object,
// with each key being a Mailbox id. The value for each key in the
// object MUST be true.
//
// o keywords: "String[Boolean]" (default: {})
//
// A set of keywords that apply to the Email. The set is represented
// as an object, with the keys being the keywords. The value for
// each key in the object MUST be true.
//
// Keywords are shared with IMAP. The six system keywords from IMAP
// get special treatment. The following four keywords have their
// first character changed from "\" in IMAP to "$" in JMAP and have
// particular semantic meaning:
//
// * "$draft": The Email is a draft the user is composing.
//
// * "$seen": The Email has been read.
//
// * "$flagged": The Email has been flagged for urgent/special
// attention.
//
// * "$answered": The Email has been replied to.
//
// The IMAP "\Recent" keyword is not exposed via JMAP. The IMAP
// "\Deleted" keyword is also not present: IMAP uses a delete+expunge
// model, which JMAP does not. Any message with the "\Deleted"
// keyword MUST NOT be visible via JMAP (and so are not counted in
// the "totalEmails", "unreadEmails", "totalThreads", and
// "unreadThreads" Mailbox properties).
//
// Users may add arbitrary keywords to an Email. For compatibility
// with IMAP, a keyword is a case-insensitive string of 1-255
// characters in the ASCII subset %x21-%x7e (excludes control chars
// and space), and it MUST NOT include any of these characters:
//
// ( ) { ] % * " \
//
// Because JSON is case sensitive, servers MUST return keywords in
// lowercase.
//
// The IANA "IMAP and JMAP Keywords" registry at
// <https://www.iana.org/assignments/imap-jmap-keywords/> as
// established in [RFC5788] assigns semantic meaning to some other
// keywords in common use. New keywords may be established here in
// the future. In particular, note:
//
// * "$forwarded": The Email has been forwarded.
//
// * "$phishing": The Email is highly likely to be phishing.
// Clients SHOULD warn users to take care when viewing this Email
// and disable links and attachments.
//
// * "$junk": The Email is definitely spam. Clients SHOULD set this
// flag when users report spam to help train automated spam-
// detection systems.
//
// * "$notjunk": The Email is definitely not spam. Clients SHOULD
// set this flag when users indicate an Email is legitimate, to
// help train automated spam-detection systems.
//
// o size: "UnsignedInt" (immutable; server-set)
//
// The size, in octets, of the raw data for the message [RFC5322] (as
// referenced by the "blobId", i.e., the number of octets in the file
// the user would download).
//
// o receivedAt: "UTCDate" (immutable; default: time of creation on
// server)
//
// The date the Email was received by the message store. This is the
// "internal date" in IMAP [RFC3501]./
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct EmailObject {
#[serde(default)]
id: Id,
#[serde(default)]
mailbox_ids: HashMap<Id, bool>,
#[serde(default)]
size: u64,
#[serde(default)]
received_at: String,
#[serde(default)]
to: Vec<EmailAddress>,
#[serde(default)]
bcc: Vec<EmailAddress>,
#[serde(default)]
reply_to: Option<EmailAddress>,
#[serde(default)]
cc: Vec<EmailAddress>,
#[serde(default)]
from: Vec<EmailAddress>,
#[serde(default)]
in_reply_to_email_id: Id,
#[serde(default)]
keywords: Value,
#[serde(default)]
attached_emails: Option<Id>,
#[serde(default)]
attachments: Vec<Value>,
#[serde(default)]
blob_id: String,
#[serde(default)]
has_attachment: bool,
#[serde(default)]
#[serde(deserialize_with = "deserialize_header")]
headers: HashMap<String, String>,
#[serde(default)]
html_body: Vec<HtmlBody>,
#[serde(default)]
preview: Option<String>,
#[serde(default)]
sent_at: String,
#[serde(default)]
subject: String,
#[serde(default)]
text_body: Vec<TextBody>,
#[serde(default)]
thread_id: Id,
}
#[derive(Deserialize, Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
struct Header {
name: String,
value: String,
}
fn deserialize_header<'de, D>(
deserializer: D,
) -> std::result::Result<HashMap<String, String>, D::Error>
where
D: Deserializer<'de>,
{
let v = <Vec<Header>>::deserialize(deserializer)?;
Ok(v.into_iter().map(|t| (t.name, t.value)).collect())
}
#[derive(Deserialize, Serialize, Debug, Default)]
#[serde(rename_all = "camelCase")]
struct EmailAddress {
email: String,
name: Option<String>,
}
impl Into<crate::email::Address> for EmailAddress {
fn into(self) -> crate::email::Address {
let Self { email, mut name } = self;
crate::make_address!((name.take().unwrap_or_default()), email)
}
}
impl std::fmt::Display for EmailAddress {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
if self.name.is_some() {
write!(f, "{} <{}>", self.name.as_ref().unwrap(), &self.email)
} else {
write!(f, "{}", &self.email)
}
}
}
impl std::convert::From<EmailObject> for crate::Envelope {
fn from(mut t: EmailObject) -> crate::Envelope {
let mut env = crate::Envelope::new(0);
env.set_date(std::mem::replace(&mut t.sent_at, String::new()).as_bytes());
if let Some(d) = crate::email::parser::date(env.date_as_str().as_bytes()) {
env.set_datetime(d);
}
if let Some(v) = t.headers.get("Message-ID").or(t.headers.get("Message-Id")) {
env.set_message_id(v.as_bytes());
}
if let Some(v) = t.headers.get("In-Reply-To") {
env.set_in_reply_to(v.as_bytes());
env.push_references(v.as_bytes());
}
if let Some(v) = t.headers.get("References") {
let parse_result = crate::email::parser::references(v.as_bytes());
if parse_result.is_done() {
for v in parse_result.to_full_result().unwrap() {
env.push_references(v);
}
}
env.set_references(v.as_bytes());
}
if let Some(v) = t.headers.get("Date") {
env.set_date(v.as_bytes());
if let Some(d) = crate::email::parser::date(v.as_bytes()) {
env.set_datetime(d);
}
}
env.set_has_attachments(t.has_attachment);
env.set_subject(std::mem::replace(&mut t.subject, String::new()).into_bytes());
env.set_from(
std::mem::replace(&mut t.from, Vec::new())
.into_iter()
.map(|addr| addr.into())
.collect::<Vec<crate::email::Address>>(),
);
env.set_to(
std::mem::replace(&mut t.to, Vec::new())
.into_iter()
.map(|addr| addr.into())
.collect::<Vec<crate::email::Address>>(),
);
env.set_cc(
std::mem::replace(&mut t.cc, Vec::new())
.into_iter()
.map(|addr| addr.into())
.collect::<Vec<crate::email::Address>>(),
);
env.set_bcc(
std::mem::replace(&mut t.bcc, Vec::new())
.into_iter()
.map(|addr| addr.into())
.collect::<Vec<crate::email::Address>>(),
);
if env.references.is_some() {
if let Some(pos) = env
.references
.as_ref()
.map(|r| &r.refs)
.unwrap()
.iter()
.position(|r| r == env.message_id())
{
env.references.as_mut().unwrap().refs.remove(pos);
}
}
let mut h = DefaultHasher::new();
h.write(t.id.as_bytes());
env.set_hash(h.finish());
env
}
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
struct HtmlBody {
blob_id: Id,
cid: Option<String>,
disposition: String,
headers: Value,
language: Option<Vec<String>>,
location: Option<String>,
name: Option<String>,
part_id: Option<String>,
size: u64,
#[serde(alias = "type")]
content_type: String,
#[serde(default)]
sub_parts: Vec<Value>,
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
struct TextBody {
blob_id: Id,
cid: Option<String>,
disposition: String,
headers: Value,
language: Option<Vec<String>>,
location: Option<String>,
name: Option<String>,
part_id: Option<String>,
size: u64,
#[serde(alias = "type")]
content_type: String,
#[serde(default)]
sub_parts: Vec<Value>,
}
impl Object for EmailObject {
const NAME: &'static str = "Email";
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct EmailQueryResponse {
pub account_id: Id,
pub can_calculate_changes: bool,
pub collapse_threads: bool,
// FIXME
pub filter: String,
pub ids: Vec<Id>,
pub position: u64,
pub query_state: String,
pub sort: Option<String>,
pub total: usize,
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct EmailQueryCall {
pub filter: EmailFilterCondition, /* "inMailboxes": [ folder.id ] },*/
pub collapse_threads: bool,
pub position: u64,
pub fetch_threads: bool,
pub fetch_messages: bool,
pub fetch_message_properties: Vec<MessageProperty>,
}
impl Method<EmailObject> for EmailQueryCall {
const NAME: &'static str = "Email/query";
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct EmailGet {
#[serde(flatten)]
pub get_call: Get<EmailObject>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub body_properties: Vec<String>,
#[serde(default = "bool_false")]
pub fetch_text_body_values: bool,
#[serde(default = "bool_false")]
pub fetch_html_body_values: bool,
#[serde(default = "bool_false")]
pub fetch_all_body_values: bool,
#[serde(default)]
pub max_body_value_bytes: u64,
}
impl Method<EmailObject> for EmailGet {
const NAME: &'static str = "Email/get";
}
impl EmailGet {
pub fn new(get_call: Get<EmailObject>) -> Self {
EmailGet {
get_call,
body_properties: Vec::new(),
fetch_text_body_values: false,
fetch_html_body_values: false,
fetch_all_body_values: false,
max_body_value_bytes: 0,
}
}
_impl!(body_properties: Vec<String>);
_impl!(fetch_text_body_values: bool);
_impl!(fetch_html_body_values: bool);
_impl!(fetch_all_body_values: bool);
_impl!(max_body_value_bytes: u64);
}
#[derive(Serialize, Deserialize, Default, Debug)]
#[serde(rename_all = "camelCase")]
pub struct EmailFilterCondition {
#[serde(skip_serializing_if = "Option::is_none")]
pub in_mailbox: Option<Id>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub in_mailbox_other_than: Vec<Id>,
#[serde(skip_serializing_if = "String::is_empty")]
pub before: UtcDate,
#[serde(skip_serializing_if = "String::is_empty")]
pub after: UtcDate,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub min_size: Option<u64>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub max_size: Option<u64>,
#[serde(skip_serializing_if = "String::is_empty")]
pub all_in_thread_have_keyword: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub some_in_thread_have_keyword: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub none_in_thread_have_keyword: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub has_keyword: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub not_keyword: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub has_attachment: Option<bool>,
#[serde(skip_serializing_if = "String::is_empty")]
pub text: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub from: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub to: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub cc: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub bcc: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub subject: String,
#[serde(skip_serializing_if = "String::is_empty")]
pub body: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub header: Vec<Value>,
}
impl EmailFilterCondition {
pub fn new() -> Self {
Self::default()
}
_impl!(in_mailbox: Option<Id>);
_impl!(in_mailbox_other_than: Vec<Id>);
_impl!(before: UtcDate);
_impl!(after: UtcDate);
_impl!(min_size: Option<u64>);
_impl!(max_size: Option<u64>);
_impl!(all_in_thread_have_keyword: String);
_impl!(some_in_thread_have_keyword: String);
_impl!(none_in_thread_have_keyword: String);
_impl!(has_keyword: String);
_impl!(not_keyword: String);
_impl!(has_attachment: Option<bool>);
_impl!(text: String);
_impl!(from: String);
_impl!(to: String);
_impl!(cc: String);
_impl!(bcc: String);
_impl!(subject: String);
_impl!(body: String);
_impl!(header: Vec<Value>);
}
impl FilterTrait<EmailObject> for EmailFilterCondition {}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum MessageProperty {
ThreadId,
MailboxId,
IsUnread,
IsFlagged,
IsAnswered,
IsDraft,
HasAttachment,
From,
To,
Subject,
Date,
Preview,
}

View File

@ -0,0 +1,71 @@
/*
* meli - jmap module.
*
* Copyright 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::*;
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MailboxObject {
pub id: String,
pub is_subscribed: bool,
pub my_rights: JmapRights,
pub name: String,
pub parent_id: Option<String>,
pub role: Option<String>,
pub sort_order: u64,
pub total_emails: u64,
pub total_threads: u64,
pub unread_emails: u64,
pub unread_threads: u64,
}
impl Object for MailboxObject {
const NAME: &'static str = "Mailbox";
}
#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct JmapRights {
pub may_add_items: bool,
pub may_create_child: bool,
pub may_delete: bool,
pub may_read_items: bool,
pub may_remove_items: bool,
pub may_rename: bool,
pub may_set_keywords: bool,
pub may_set_seen: bool,
pub may_submit: bool,
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MailboxGet {
#[serde(flatten)]
pub get_call: Get<MailboxObject>,
}
impl MailboxGet {
pub fn new(get_call: Get<MailboxObject>) -> Self {
MailboxGet { get_call }
}
}
impl Method<MailboxObject> for MailboxGet {
const NAME: &'static str = "Mailbox/get";
}

View File

@ -0,0 +1,388 @@
/*
* meli - jmap module.
*
* Copyright 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::folder::JmapFolder;
use super::*;
use serde::{de::DeserializeOwned, Serialize};
use serde_json::{json, Value};
use std::convert::TryFrom;
pub type Id = String;
pub type UtcDate = String;
use super::rfc8620::Object;
macro_rules! get_request_no {
($lock:expr) => {{
let mut lck = $lock.lock().unwrap();
let ret = *lck;
*lck += 1;
ret
}};
}
pub trait Response<OBJ: Object> {
const NAME: &'static str;
}
pub trait Method<OBJ: Object>: Serialize {
const NAME: &'static str;
}
macro_rules! get_path_hash {
($path:expr) => {{
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
let mut hasher = DefaultHasher::new();
$path.hash(&mut hasher);
hasher.finish()
}};
}
static USING: &'static [&'static str] = &["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"];
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Request {
using: &'static [&'static str],
/* Why is this Value instead of Box<dyn Method<_>>? The Method trait cannot be made into a
* Trait object because its serialize() will be generic. */
method_calls: Vec<Value>,
#[serde(skip)]
request_no: Arc<Mutex<usize>>,
}
impl Request {
pub fn new(request_no: Arc<Mutex<usize>>) -> Self {
Request {
using: USING,
method_calls: Vec::new(),
request_no,
}
}
pub fn add_call<M: Method<O>, O: Object>(&mut self, call: &M) -> usize {
let seq = get_request_no!(self.request_no);
self.method_calls
.push(serde_json::to_value((M::NAME, call, &format!("m{}", seq))).unwrap());
seq
}
}
#[derive(Serialize, Debug)]
#[serde(untagged)]
pub enum MethodCall {
#[serde(rename_all = "camelCase")]
EmailQuery {
filter: Vec<String>, /* "inMailboxes": [ folder.id ] },*/
collapse_threads: bool,
position: u64,
fetch_threads: bool,
fetch_messages: bool,
fetch_message_properties: Vec<MessageProperty>,
},
MailboxGet {},
Empty {},
}
pub fn get_mailboxes(conn: &JmapConnection) -> Result<FnvHashMap<FolderHash, JmapFolder>> {
let seq = get_request_no!(conn.request_no);
let res = conn
.client
.lock()
.unwrap()
.post("https://jmap-proxy.local/jmap/fc32dffe-14e7-11ea-a277-2477037a1804/")
.json(&json!({
"using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
"methodCalls": [["Mailbox/get", {},
format!("#m{}",seq).as_str()]],
}))
.send();
let res_text = res?.text()?;
let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap();
*conn.online_status.lock().unwrap() = true;
let m = GetResponse::<MailboxObject>::try_from(v.method_responses.remove(0))?;
let GetResponse::<MailboxObject> {
list, account_id, ..
} = m;
*conn.account_id.lock().unwrap() = account_id;
Ok(list
.into_iter()
.map(|r| {
let MailboxObject {
id,
is_subscribed,
my_rights,
name,
parent_id,
role,
sort_order,
total_emails,
total_threads,
unread_emails,
unread_threads,
} = r;
let hash = get_path_hash!(&name);
(
hash,
JmapFolder {
name: name.clone(),
hash,
path: name,
v: Vec::new(),
id,
is_subscribed,
my_rights,
parent_id,
role,
sort_order,
total_emails,
total_threads,
unread_emails,
unread_threads,
},
)
})
.collect())
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct JsonResponse<'a> {
#[serde(borrow)]
method_responses: Vec<MethodResponse<'a>>,
}
pub fn get_message_list(conn: &JmapConnection, folder: &JmapFolder) -> Result<Vec<String>> {
let seq = get_request_no!(conn.request_no);
let email_call: EmailQueryCall = EmailQueryCall {
filter: EmailFilterCondition::new().in_mailbox(Some(folder.id.clone())),
collapse_threads: false,
position: 0,
fetch_threads: true,
fetch_messages: true,
fetch_message_properties: vec![
MessageProperty::ThreadId,
MessageProperty::MailboxId,
MessageProperty::IsUnread,
MessageProperty::IsFlagged,
MessageProperty::IsAnswered,
MessageProperty::IsDraft,
MessageProperty::HasAttachment,
MessageProperty::From,
MessageProperty::To,
MessageProperty::Subject,
MessageProperty::Date,
MessageProperty::Preview,
],
};
let mut req = Request::new(conn.request_no.clone());
req.add_call(&email_call);
/*
{
"using": [
"urn:ietf:params:jmap:core",
"urn:ietf:params:jmap:mail"
],
"methodCalls": [[
"Email/query",
{
"collapseThreads": false,
"fetchMessageProperties": [
"threadId",
"mailboxId",
"isUnread",
"isFlagged",
"isAnswered",
"isDraft",
"hasAttachment",
"from",
"to",
"subject",
"date",
"preview"
],
"fetchMessages": true,
"fetchThreads": true,
"filter": [
{
"inMailboxes": [
"fde49e47-14e7-11ea-a277-2477037a1804"
]
}
],
"position": 0
},
"f"
]]
}
*/
/*
r#"
"using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
"methodCalls": [["Email/query", { "filter": {
"inMailboxes": [ folder.id ]
},
"collapseThreads": false,
"position": 0,
"fetchThreads": true,
"fetchMessages": true,
"fetchMessageProperties": [
"threadId",
"mailboxId",
"isUnread",
"isFlagged",
"isAnswered",
"isDraft",
"hasAttachment",
"from",
"to",
"subject",
"date",
"preview"
],
}, format!("m{}", seq).as_str()]],
});"
);*/
let res = conn
.client
.lock()
.unwrap()
.post("https://jmap-proxy.local/jmap/fc32dffe-14e7-11ea-a277-2477037a1804/")
.json(&req)
.send();
let res_text = res?.text()?;
let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap();
*conn.online_status.lock().unwrap() = true;
let m = QueryResponse::<EmailObject>::try_from(v.method_responses.remove(0))?;
let QueryResponse::<EmailObject> { ids, .. } = m;
Ok(ids)
}
pub fn get_message(conn: &JmapConnection, ids: &[String]) -> Result<Vec<Envelope>> {
let seq = get_request_no!(conn.request_no);
let email_call: EmailGet = EmailGet::new(
Get::new()
.ids(Some(JmapArgument::value(
ids.iter().cloned().collect::<Vec<String>>(),
)))
.account_id(conn.account_id.lock().unwrap().clone()),
);
let mut req = Request::new(conn.request_no.clone());
req.add_call(&email_call);
let res = conn
.client
.lock()
.unwrap()
.post("https://jmap-proxy.local/jmap/fc32dffe-14e7-11ea-a277-2477037a1804/")
.json(&req)
.send();
let res_text = res?.text()?;
let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap();
let e = GetResponse::<EmailObject>::try_from(v.method_responses.remove(0))?;
let GetResponse::<EmailObject> { list, .. } = e;
Ok(list
.into_iter()
.map(std::convert::Into::into)
.collect::<Vec<Envelope>>())
}
/*
*
*json!({
"using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"],
"methodCalls": [["Email/get", {
"ids": ids,
"properties": [ "threadId", "mailboxIds", "from", "subject",
"receivedAt",
"htmlBody", "bodyValues" ],
"bodyProperties": [ "partId", "blobId", "size", "type" ],
"fetchHTMLBodyValues": true,
"maxBodyValueBytes": 256
}, format!("m{}", seq).as_str()]],
}))
*/
pub fn get(conn: &JmapConnection, folder: &JmapFolder) -> Result<Vec<Envelope>> {
let email_query_call: EmailQueryCall = EmailQueryCall {
filter: EmailFilterCondition::new().in_mailbox(Some(folder.id.clone())),
collapse_threads: false,
position: 0,
fetch_threads: true,
fetch_messages: true,
fetch_message_properties: vec![
MessageProperty::ThreadId,
MessageProperty::MailboxId,
MessageProperty::IsUnread,
MessageProperty::IsFlagged,
MessageProperty::IsAnswered,
MessageProperty::IsDraft,
MessageProperty::HasAttachment,
MessageProperty::From,
MessageProperty::To,
MessageProperty::Subject,
MessageProperty::Date,
MessageProperty::Preview,
],
};
let mut req = Request::new(conn.request_no.clone());
let prev_seq = req.add_call(&email_query_call);
let email_call: EmailGet = EmailGet::new(
Get::new()
.ids(Some(JmapArgument::reference(
prev_seq,
&email_query_call,
"/ids",
)))
.account_id(conn.account_id.lock().unwrap().clone()),
);
req.add_call(&email_call);
let res = conn
.client
.lock()
.unwrap()
.post("https://jmap-proxy.local/jmap/fc32dffe-14e7-11ea-a277-2477037a1804/")
.json(&req)
.send();
let res_text = res?.text()?;
let mut v: MethodResponse = serde_json::from_str(&res_text).unwrap();
let e = GetResponse::<EmailObject>::try_from(v.method_responses.pop().unwrap())?;
let GetResponse::<EmailObject> { list, .. } = e;
Ok(list
.into_iter()
.map(std::convert::Into::into)
.collect::<Vec<Envelope>>())
}

View File

@ -0,0 +1,619 @@
/*
* meli - jmap module.
*
* Copyright 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::Id;
use core::marker::PhantomData;
use serde::de::DeserializeOwned;
use serde::ser::{Serialize, SerializeStruct, Serializer};
use serde_json::{value::RawValue, Value};
mod filters;
pub use filters::*;
mod comparator;
pub use comparator::*;
mod argument;
pub use argument::*;
use super::protocol::{Method, Response};
use std::collections::HashMap;
pub trait Object {
const NAME: &'static str;
}
// 5.1. /get
//
// Objects of type Foo are fetched via a call to "Foo/get".
//
// It takes the following arguments:
//
// o accountId: "Id"
//
// The id of the account to use.
//
// o ids: "Id[]|null"
//
// The ids of the Foo objects to return. If null, then *all* records
// of the data type are returned, if this is supported for that data
// type and the number of records does not exceed the
// "maxObjectsInGet" limit.
//
// o properties: "String[]|null"
//
// If supplied, only the properties listed in the array are returned
// for each Foo object. If null, all properties of the object are
// returned. The id property of the object is *always* returned,
// even if not explicitly requested. If an invalid property is
// requested, the call MUST be rejected with an "invalidArguments"
// error.
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct JmapSession {
capabilities: HashMap<String, CapabilitiesObject>,
accounts: HashMap<Id, Account>,
primary_accounts: Vec<Id>,
username: String,
api_url: String,
download_url: String,
upload_url: String,
event_source_url: String,
state: String,
#[serde(flatten)]
extra_properties: HashMap<String, Value>,
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CapabilitiesObject {
max_size_upload: u64,
max_concurrent_upload: u64,
max_size_request: u64,
max_concurrent_requests: u64,
max_calls_in_request: u64,
max_objects_in_get: u64,
max_objects_in_set: u64,
collation_algorithms: Vec<String>,
}
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Account {
name: String,
is_personal: bool,
is_read_only: bool,
account_capabilities: HashMap<String, Value>,
#[serde(flatten)]
extra_properties: HashMap<String, Value>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Get<OBJ: Object>
where
OBJ: std::fmt::Debug + Serialize,
{
#[serde(skip_serializing_if = "String::is_empty")]
pub account_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(flatten)]
pub ids: Option<JmapArgument<Vec<String>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<Vec<String>>,
#[serde(skip)]
_ph: PhantomData<*const OBJ>,
}
impl<OBJ: Object> Get<OBJ>
where
OBJ: std::fmt::Debug + Serialize,
{
pub fn new() -> Self {
Self {
account_id: String::new(),
ids: None,
properties: None,
_ph: PhantomData,
}
}
_impl!(account_id: String);
_impl!(ids: Option<JmapArgument<Vec<String>>>);
_impl!(properties: Option<Vec<String>>);
}
impl<OBJ: Object + Serialize + std::fmt::Debug> Serialize for Get<OBJ> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut fields_no = 0;
if !self.account_id.is_empty() {
fields_no += 1;
}
if !self.ids.is_none() {
fields_no += 1;
}
if !self.properties.is_none() {
fields_no += 1;
}
let mut state = serializer.serialize_struct("Get", fields_no)?;
if !self.account_id.is_empty() {
state.serialize_field("accountId", &self.account_id)?;
}
match self.ids.as_ref() {
None => {}
Some(JmapArgument::Value(ref v)) => state.serialize_field("ids", v)?,
Some(JmapArgument::ResultReference {
ref result_of,
ref name,
ref path,
}) => {
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct A<'a> {
result_of: &'a str,
name: &'a str,
path: &'a str,
}
state.serialize_field(
"#ids",
&A {
result_of,
name,
path,
},
)?;
}
}
if !self.properties.is_none() {
state.serialize_field("properties", self.properties.as_ref().unwrap());
}
state.end()
}
}
// The response has the following arguments:
//
// o accountId: "Id"
//
// The id of the account used for the call.
//
// o state: "String"
//
// A (preferably short) string representing the state on the server
// for *all* the data of this type in the account (not just the
// objects returned in this call). If the data changes, this string
// MUST change. If the Foo data is unchanged, servers SHOULD return
// the same state string on subsequent requests for this data type.
// When a client receives a response with a different state string to
// a previous call, it MUST either throw away all currently cached
// objects for the type or call "Foo/changes" to get the exact
// changes.
//
// o list: "Foo[]"
//
// An array of the Foo objects requested. This is the *empty array*
// if no objects were found or if the "ids" argument passed in was
// also an empty array. The results MAY be in a different order to
// the "ids" in the request arguments. If an identical id is
// included more than once in the request, the server MUST only
// include it once in either the "list" or the "notFound" argument of
// the response.
//
// o notFound: "Id[]"
//
// This array contains the ids passed to the method for records that
// do not exist. The array is empty if all requested ids were found
// or if the "ids" argument passed in was either null or an empty
// array.
//
// The following additional error may be returned instead of the "Foo/
// get" response:
//
// "requestTooLarge": The number of ids requested by the client exceeds
// the maximum number the server is willing to process in a single
// method call.
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct MethodResponse<'a> {
#[serde(borrow)]
pub method_responses: Vec<&'a RawValue>,
#[serde(default)]
pub created_ids: HashMap<Id, Id>,
#[serde(default)]
pub session_state: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetResponse<OBJ: Object> {
#[serde(skip_serializing_if = "String::is_empty")]
pub account_id: String,
#[serde(default)]
pub state: String,
pub list: Vec<OBJ>,
pub not_found: Vec<String>,
}
impl<OBJ: Object + DeserializeOwned> std::convert::TryFrom<&RawValue> for GetResponse<OBJ> {
type Error = crate::error::MeliError;
fn try_from(t: &RawValue) -> Result<GetResponse<OBJ>, crate::error::MeliError> {
let res: (String, GetResponse<OBJ>, String) = serde_json::from_str(t.get())?;
assert_eq!(&res.0, &format!("{}/get", OBJ::NAME));
Ok(res.1)
}
}
impl<OBJ: Object> GetResponse<OBJ> {
_impl_get_mut!(account_id_mut, account_id: String);
_impl_get_mut!(state_mut, state: String);
_impl_get_mut!(list_mut, list: Vec<OBJ>);
_impl_get_mut!(not_found_mut, not_found: Vec<String>);
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
enum JmapError {
RequestTooLarge,
InvalidArguments,
InvalidResultReference,
}
// 5.5. /query
//
// For data sets where the total amount of data is expected to be very
// small, clients can just fetch the complete set of data and then do
// any sorting/filtering locally. However, for large data sets (e.g.,
// multi-gigabyte mailboxes), the client needs to be able to
// search/sort/window the data type on the server.
//
// A query on the set of Foos in an account is made by calling "Foo/
// query". This takes a number of arguments to determine which records
// to include, how they should be sorted, and which part of the result
// should be returned (the full list may be *very* long). The result is
// returned as a list of Foo ids.
//
// A call to "Foo/query" takes the following arguments:
//
// o accountId: "Id"
//
// The id of the account to use.
//
// o filter: "FilterOperator|FilterCondition|null"
//
// Determines the set of Foos returned in the results. If null, all
// objects in the account of this type are included in the results.
// A *FilterOperator* object has the following properties:
//
// * operator: "String"
//
// This MUST be one of the following strings:
//
// + "AND": All of the conditions must match for the filter to
// match.
//
// + "OR": At least one of the conditions must match for the
// filter to match.
//
// + "NOT": None of the conditions must match for the filter to
// match.
//
// * conditions: "(FilterOperator|FilterCondition)[]"
//
// The conditions to evaluate against each record.
//
// A *FilterCondition* is an "object" whose allowed properties and
// semantics depend on the data type and is defined in the /query
// method specification for that type. It MUST NOT have an
// "operator" property.
//
// o sort: "Comparator[]|null"
//
// Lists the names of properties to compare between two Foo records,
// and how to compare them, to determine which comes first in the
// sort. If two Foo records have an identical value for the first
// comparator, the next comparator will be considered, and so on. If
// all comparators are the same (this includes the case where an
// empty array or null is given as the "sort" argument), the sort
// order is server dependent, but it MUST be stable between calls to
// "Foo/query". A *Comparator* has the following properties:
//
// * property: "String"
//
// The name of the property on the Foo objects to compare.
//
// * isAscending: "Boolean" (optional; default: true)
//
// If true, sort in ascending order. If false, reverse the
// comparator's results to sort in descending order.
//
// * collation: "String" (optional; default is server-dependent)
//
// The identifier, as registered in the collation registry defined
// in [RFC4790], for the algorithm to use when comparing the order
// of strings. The algorithms the server supports are advertised
// in the capabilities object returned with the Session object
// (see Section 2).
//
// If omitted, the default algorithm is server dependent, but:
//
// 1. It MUST be unicode-aware.
//
// 2. It MAY be selected based on an Accept-Language header in
// the request (as defined in [RFC7231], Section 5.3.5) or
// out-of-band information about the user's language/locale.
//
// 3. It SHOULD be case insensitive where such a concept makes
// sense for a language/locale. Where the user's language is
// unknown, it is RECOMMENDED to follow the advice in
// Section 5.2.3 of [RFC8264].
//
// The "i;unicode-casemap" collation [RFC5051] and the Unicode
// Collation Algorithm (<http://www.unicode.org/reports/tr10/>)
// are two examples that fulfil these criterion and provide
// reasonable behaviour for a large number of languages.
//
// When the property being compared is not a string, the
// "collation" property is ignored, and the following comparison
// rules apply based on the type. In ascending order:
//
// + "Boolean": false comes before true.
//
// + "Number": A lower number comes before a higher number.
//
// + "Date"/"UTCDate": The earlier date comes first.
//
// The Comparator object may also have additional properties as
// required for specific sort operations defined in a type's /query
// method.
//
// o position: "Int" (default: 0)
//
// The zero-based index of the first id in the full list of results
// to return.
//
// If a negative value is given, it is an offset from the end of the
// list. Specifically, the negative value MUST be added to the total
// number of results given the filter, and if still negative, it's
// clamped to "0". This is now the zero-based index of the first id
// to return.
//
// If the index is greater than or equal to the total number of
// objects in the results list, then the "ids" array in the response
// will be empty, but this is not an error.
//
// o anchor: "Id|null"
//
// A Foo id. If supplied, the "position" argument is ignored. The
// index of this id in the results will be used in combination with
// the "anchorOffset" argument to determine the index of the first
// result to return (see below for more details).
//
// o anchorOffset: "Int" (default: 0)
//
// The index of the first result to return relative to the index of
// the anchor, if an anchor is given. This MAY be negative. For
// example, "-1" means the Foo immediately preceding the anchor is
// the first result in the list returned (see below for more
// details).
//
// o limit: "UnsignedInt|null"
//
// The maximum number of results to return. If null, no limit
// presumed. The server MAY choose to enforce a maximum "limit"
// argument. In this case, if a greater value is given (or if it is
// null), the limit is clamped to the maximum; the new limit is
// returned with the response so the client is aware. If a negative
// value is given, the call MUST be rejected with an
// "invalidArguments" error.
//
// o calculateTotal: "Boolean" (default: false)
//
// Does the client wish to know the total number of results in the
// query? This may be slow and expensive for servers to calculate,
// particularly with complex filters, so clients should take care to
// only request the total when needed.
//
// If an "anchor" argument is given, the anchor is looked for in the
// results after filtering and sorting. If found, the "anchorOffset" is
// then added to its index. If the resulting index is now negative, it
// is clamped to 0. This index is now used exactly as though it were
// supplied as the "position" argument. If the anchor is not found, the
// call is rejected with an "anchorNotFound" error.
//
// If an "anchor" is specified, any position argument supplied by the
// client MUST be ignored. If no "anchor" is supplied, any
// "anchorOffset" argument MUST be ignored.
//
// A client can use "anchor" instead of "position" to find the index of
// an id within a large set of results.
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QueryCall<F: FilterTrait<OBJ>, OBJ: Object>
where
OBJ: std::fmt::Debug + Serialize,
{
account_id: String,
filter: Option<Filter<F, OBJ>>,
sort: Option<Comparator<OBJ>>,
#[serde(default)]
position: u64,
#[serde(skip_serializing_if = "Option::is_none")]
anchor: Option<String>,
#[serde(default)]
anchor_offset: u64,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u64>,
#[serde(default = "bool_false")]
calculate_total: bool,
#[serde(skip)]
_ph: PhantomData<*const OBJ>,
}
impl<F: FilterTrait<OBJ>, OBJ: Object> QueryCall<F, OBJ>
where
OBJ: std::fmt::Debug + Serialize,
{
pub fn new() -> Self {
Self {
account_id: String::new(),
filter: None,
sort: None,
position: 0,
anchor: None,
anchor_offset: 0,
limit: None,
calculate_total: false,
_ph: PhantomData,
}
}
_impl!(account_id: String);
_impl!(filter: Option<Filter<F, OBJ>>);
_impl!(sort: Option<Comparator<OBJ>>);
_impl!(position: u64);
_impl!(anchor: Option<String>);
_impl!(anchor_offset: u64);
_impl!(limit: Option<u64>);
_impl!(calculate_total: bool);
}
pub fn bool_false() -> bool {
false
}
pub fn bool_true() -> bool {
true
}
// The response has the following arguments:
//
// o accountId: "Id"
//
// The id of the account used for the call.
//
// o queryState: "String"
//
// A string encoding the current state of the query on the server.
// This string MUST change if the results of the query (i.e., the
// matching ids and their sort order) have changed. The queryState
// string MAY change if something has changed on the server, which
// means the results may have changed but the server doesn't know for
// sure.
//
// The queryState string only represents the ordered list of ids that
// match the particular query (including its sort/filter). There is
// no requirement for it to change if a property on an object
// matching the query changes but the query results are unaffected
// (indeed, it is more efficient if the queryState string does not
// change in this case). The queryState string only has meaning when
// compared to future responses to a query with the same type/sort/
// filter or when used with /queryChanges to fetch changes.
//
// Should a client receive back a response with a different
// queryState string to a previous call, it MUST either throw away
// the currently cached query and fetch it again (note, this does not
// require fetching the records again, just the list of ids) or call
// "Foo/queryChanges" to get the difference.
//
// o canCalculateChanges: "Boolean"
//
// This is true if the server supports calling "Foo/queryChanges"
// with these "filter"/"sort" parameters. Note, this does not
// guarantee that the "Foo/queryChanges" call will succeed, as it may
// only be possible for a limited time afterwards due to server
// internal implementation details.
//
// o position: "UnsignedInt"
//
// The zero-based index of the first result in the "ids" array within
// the complete list of query results.
//
// o ids: "Id[]"
//
// The list of ids for each Foo in the query results, starting at the
// index given by the "position" argument of this response and
// continuing until it hits the end of the results or reaches the
// "limit" number of ids. If "position" is >= "total", this MUST be
// the empty list.
//
// o total: "UnsignedInt" (only if requested)
//
// The total number of Foos in the results (given the "filter").
// This argument MUST be omitted if the "calculateTotal" request
// argument is not true.
//
// o limit: "UnsignedInt" (if set by the server)
//
// The limit enforced by the server on the maximum number of results
// to return. This is only returned if the server set a limit or
// used a different limit than that given in the request.
//
// The following additional errors may be returned instead of the "Foo/
// query" response:
//
// "anchorNotFound": An anchor argument was supplied, but it cannot be
// found in the results of the query.
//
// "unsupportedSort": The "sort" is syntactically valid, but it includes
// a property the server does not support sorting on or a collation
// method it does not recognise.
//
// "unsupportedFilter": The "filter" is syntactically valid, but the
// server cannot process it. If the filter was the result of a user's
// search input, the client SHOULD suggest that the user simplify their
// search.
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct QueryResponse<OBJ: Object> {
#[serde(skip_serializing_if = "String::is_empty", default)]
pub account_id: String,
pub query_state: String,
pub can_calculate_changes: bool,
pub position: u64,
pub ids: Vec<Id>,
#[serde(default)]
pub total: u64,
#[serde(default)]
pub limit: u64,
#[serde(skip)]
_ph: PhantomData<*const OBJ>,
}
impl<OBJ: Object + DeserializeOwned> std::convert::TryFrom<&RawValue> for QueryResponse<OBJ> {
type Error = crate::error::MeliError;
fn try_from(t: &RawValue) -> Result<QueryResponse<OBJ>, crate::error::MeliError> {
let res: (String, QueryResponse<OBJ>, String) = serde_json::from_str(t.get())?;
assert_eq!(&res.0, &format!("{}/query", OBJ::NAME));
Ok(res.1)
}
}
impl<OBJ: Object> QueryResponse<OBJ> {
_impl_get_mut!(ids_mut, ids: Vec<Id>);
}

View File

@ -0,0 +1,54 @@
/*
* meli - jmap module.
*
* Copyright 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 crate::backends::jmap::protocol::Method;
use crate::backends::jmap::rfc8620::Object;
use serde::de::DeserializeOwned;
use serde::ser::{Serialize, SerializeStruct, Serializer};
#[derive(Deserialize, Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum JmapArgument<T> {
Value(T),
ResultReference {
result_of: String,
name: String,
path: String,
},
}
impl<T> JmapArgument<T> {
pub fn value(v: T) -> Self {
JmapArgument::Value(v)
}
pub fn reference<M, OBJ>(result_of: usize, method: &M, path: &str) -> Self
where
M: Method<OBJ>,
OBJ: Object,
{
JmapArgument::ResultReference {
result_of: format!("m{}", result_of),
name: M::NAME.to_string(),
path: path.to_string(),
}
}
}

View File

@ -0,0 +1,61 @@
/*
* meli - jmap module.
*
* Copyright 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::*;
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Comparator<OBJ: Object> {
property: String,
#[serde(default = "bool_true")]
is_ascending: bool,
//FIXME
collation: Option<String>,
//#[serde(flatten)]
additional_properties: Vec<String>,
_ph: PhantomData<*const OBJ>,
}
impl<OBJ: Object> Comparator<OBJ> {
pub fn new() -> Self {
Self {
property: String::new(),
is_ascending: true,
collation: None,
additional_properties: Vec::new(),
_ph: PhantomData,
}
}
_impl!(property: String);
_impl!(is_ascending: bool);
_impl!(collation: Option<String>);
_impl!(additional_properties: Vec<String>);
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "UPPERCASE")]
pub enum FilterOperator {
And,
Or,
Not,
}

View File

@ -0,0 +1,50 @@
/*
* meli - jmap module.
*
* Copyright 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::*;
pub trait FilterTrait<T> {}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
#[serde(untagged)]
pub enum Filter<F: FilterTrait<OBJ>, OBJ: Object> {
Operator {
operator: FilterOperator,
conditions: Vec<Filter<F, OBJ>>,
},
Condition(FilterCondition<F, OBJ>),
}
#[derive(Serialize, Debug)]
pub struct FilterCondition<F: FilterTrait<OBJ>, OBJ: Object> {
#[serde(flatten)]
cond: F,
#[serde(skip)]
_ph: PhantomData<*const OBJ>,
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "UPPERCASE")]
pub enum FilterOperator {
And,
Or,
Not,
}

View File

@ -125,7 +125,7 @@ pub struct Envelope {
subject: Option<Vec<u8>>, subject: Option<Vec<u8>>,
message_id: MessageID, message_id: MessageID,
in_reply_to: Option<MessageID>, in_reply_to: Option<MessageID>,
references: Option<References>, pub references: Option<References>,
other_headers: FnvHashMap<String, String>, other_headers: FnvHashMap<String, String>,
timestamp: UnixTimestamp, timestamp: UnixTimestamp,
@ -542,6 +542,7 @@ impl Envelope {
None => Vec::new(), None => Vec::new(),
} }
} }
pub fn other_headers(&self) -> &FnvHashMap<String, String> { pub fn other_headers(&self) -> &FnvHashMap<String, String> {
&self.other_headers &self.other_headers
} }

View File

@ -256,3 +256,34 @@ impl fmt::Debug for References {
write!(f, "{:#?}", self.refs) write!(f, "{:#?}", self.refs)
} }
} }
#[macro_export]
macro_rules! make_address {
($d:expr, $a:expr) => {
Address::Mailbox(if $d.is_empty() {
MailboxAddress {
raw: format!("{}", $a).into_bytes(),
display_name: StrBuilder {
offset: 0,
length: 0,
},
address_spec: StrBuilder {
offset: 0,
length: $a.len(),
},
}
} else {
MailboxAddress {
raw: format!("{} <{}>", $d, $a).into_bytes(),
display_name: StrBuilder {
offset: 0,
length: $d.len(),
},
address_spec: StrBuilder {
offset: $d.len() + 2,
length: $a.len(),
},
}
})
};
}

View File

@ -1083,36 +1083,6 @@ mod tests {
); );
} }
macro_rules! make_address {
($d:literal, $a:literal) => {
Address::Mailbox(if $d.is_empty() {
MailboxAddress {
raw: format!("<{}>", $a).into_bytes(),
display_name: StrBuilder {
offset: 0,
length: 0,
},
address_spec: StrBuilder {
offset: 1,
length: $a.len(),
},
}
} else {
MailboxAddress {
raw: format!("{} <{}>", $d, $a).into_bytes(),
display_name: StrBuilder {
offset: 0,
length: $d.len(),
},
address_spec: StrBuilder {
offset: $d.len() + 2,
length: $a.len(),
},
}
})
};
}
#[test] #[test]
fn test_address_list() { fn test_address_list() {
let s = b"Obit Oppidum <user@domain>, let s = b"Obit Oppidum <user@domain>,

View File

@ -144,6 +144,22 @@ impl From<native_tls::Error> for MeliError {
} }
} }
#[cfg(feature = "jmap_backend")]
impl From<reqwest::Error> for MeliError {
#[inline]
fn from(kind: reqwest::Error) -> MeliError {
MeliError::new(format!("{}", kind))
}
}
#[cfg(feature = "jmap_backend")]
impl From<serde_json::error::Error> for MeliError {
#[inline]
fn from(kind: serde_json::error::Error) -> MeliError {
MeliError::new(format!("{}", kind))
}
}
impl From<&str> for MeliError { impl From<&str> for MeliError {
#[inline] #[inline]
fn from(kind: &str) -> MeliError { fn from(kind: &str) -> MeliError {