aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 461cb11e8d6c59bb6cb7a9385bb830674f1ebe41 (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
mod bayou;
mod cryptoblob;
mod time;
mod uidindex;

use anyhow::Result;

use rand::prelude::*;
use rusoto_credential::{EnvironmentProvider, ProvideAwsCredentials};
use rusoto_signature::Region;

use bayou::*;
use cryptoblob::Key;
use uidindex::*;

#[tokio::main]
async fn main() {
    do_stuff().await.expect("Something failed");
}

async fn do_stuff() -> Result<()> {
    let creds = EnvironmentProvider::default().credentials().await.unwrap();

    let k2v_region = Region::Custom {
        name: "garage-staging".to_owned(),
        endpoint: "https://k2v-staging.home.adnab.me".to_owned(),
    };

    let s3_region = Region::Custom {
        name: "garage-staging".to_owned(),
        endpoint: "https://garage-staging.home.adnab.me".to_owned(),
    };

    let key = Key::from_slice(&[0u8; 32]).unwrap();

    let mut uid_index = Bayou::<UidIndex>::new(
        creds,
        k2v_region,
        s3_region,
        "mail".into(),
        "TestMailbox".into(),
        key,
    )?;

    uid_index.sync().await?;

    dump(&uid_index);

    let mut rand_id = [0u8; 24];
    rand_id[..8].copy_from_slice(&u64::to_be_bytes(thread_rng().gen()));
    let add_mail_op = uid_index
        .state()
        .op_mail_add(MailUuid(rand_id), vec!["\\Unseen".into()]);
    uid_index.push(add_mail_op).await?;

    dump(&uid_index);

    Ok(())
}

fn dump(uid_index: &Bayou<UidIndex>) {
    let s = uid_index.state();
    println!("---- MAILBOX STATE ----");
    println!("UIDVALIDITY {}", s.uidvalidity);
    println!("UIDNEXT {}", s.uidnext);
    println!("INTERNALSEQ {}", s.internalseq);
    for (uid, uuid) in s.mails_by_uid.iter() {
        println!(
            "{} {} {}",
            uid,
            hex::encode(uuid.0),
            s.mail_flags
                .get(uuid)
                .cloned()
                .unwrap_or_default()
                .join(", ")
        );
    }
    println!("");
}