jmap-client

crates.io build docs.rs crates.io

jmap-client is a JSON Meta Application Protocol (JMAP) library written in Rust. The library is a full implementation of the JMAP RFCs including:

Features:

Usage Example

```rust // Connect to the JMAP server using Basic authentication. // (just for demonstration purposes, Bearer tokens should be used instead) let client = Client::new() .credentials(("john@example.org", "secret")) .connect("https://jmap.example.org") .await .unwrap();

// Create a mailbox. let mailboxid = client .mailboxcreate("My Mailbox", None::, Role::None) .await .unwrap() .take_id();

// Import a message into the mailbox. client .emailimport( b"From: john@example.org\nSubject: test\n\n test".tovec(), [&mailbox_id], ["$draft"].into(), None, ) .await .unwrap();

// Obtain all e-mail ids matching a filter. let emailid = client .emailquery( Filter::and([ email::query::Filter::subject("test"), email::query::Filter::inmailbox(&mailboxid), email::query::Filter::haskeyword("$draft"), ]) .into(), [email::query::Comparator::from()].into(), ) .await .unwrap() .takeids() .pop() .unwrap();

// Fetch an e-mail message. let email = client .emailget( &emailid, [Property::Subject, Property::Preview, Property::Keywords].into(), ) .await .unwrap() .unwrap(); asserteq!(email.preview().unwrap(), "test"); asserteq!(email.subject().unwrap(), "test"); assert_eq!(email.keywords(), ["$draft"]);

// Fetch only the updated properties of all mailboxes that changed // since a state. let mut request = client.build(); let changesrequest = request.changesmailbox("n").maxchanges(0); let propertiesref = changesrequest.updatedpropertiesreference(); let updatedref = changesrequest.updatedreference(); request .getmailbox() .idsref(updatedref) .propertiesref(propertiesref); for mailbox in request .send() .await .unwrap() .unwrapmethodresponses() .pop() .unwrap() .unwrapgetmailbox() .unwrap() .takelist() { println!("Changed mailbox: {:#?}", mailbox); }

// Delete the mailbox including any messages client.mailboxdestroy(&mailboxid, true).await.unwrap();

// Open an EventSource connection with the JMAP server. let mut stream = client .event_source( [ TypeState::Email, TypeState::EmailDelivery, TypeState::Mailbox, TypeState::EmailSubmission, TypeState::Identity, ] .into(), false, 60.into(), None, ) .await .unwrap();

// Consume events received over EventSource. while let Some(event) = stream.next().await { let changes = event.unwrap(); println!("-> Change id: {:?}", changes.id()); for accountid in changes.changedaccounts() { println!(" Account {} has changes:", accountid); if let Some(accountchanges) = changes.changes(accountid) { for (typestate, stateid) in accountchanges { println!(" Type {:?} has a new state {}.", typestate, stateid); } } } } ```

More examples available under the examples directory.

Testing

To run the testsuite:

bash $ cargo test --all-features

Conformed RFCs

License

Licensed under either of

at your option.

Copyright

Copyright (C) 2022, Stalwart Labs Ltd.