diff options
author | cos <cos> | 2024-06-07 08:35:58 +0200 |
---|---|---|
committer | cos <cos> | 2024-06-07 08:36:04 +0200 |
commit | 55772f9b596e5cafd328695f0dc7f5741543c187 (patch) | |
tree | 7c1f7ad239e32bf618433e6385df43a50b74b70b | |
parent | 89052221c847f8f4e24ae1f5bd37759a60d0e173 (diff) | |
download | ccc-topic/caldav-crates.zip |
explore: Unsuccessful attempt at using kaldav cratetopic/caldav-crates
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/bin/caldav-crates.rs | 87 | ||||
-rw-r--r-- | src/bin/ccc.rs (renamed from src/main.rs) | 0 |
3 files changed, 89 insertions, 0 deletions
@@ -4,5 +4,7 @@ version = "0.0.1" edition = "2021" [dependencies] +anyhow = "1.0.86" crossbeam-channel = "0.5.13" cursive = "0.20.0" +kaldav = "0.2.0" diff --git a/src/bin/caldav-crates.rs b/src/bin/caldav-crates.rs new file mode 100644 index 0000000..258b248 --- /dev/null +++ b/src/bin/caldav-crates.rs @@ -0,0 +1,87 @@ +use { + anyhow::{ + Result, + + anyhow, + }, + std::env::args, +}; + +struct CrateKaldav { + client: kaldav::Client, +} + +impl CrateKaldav { + fn new(url: &str, username: String, password: Option<String>) -> Self { + println!("kaldav"); + + let mut client = kaldav::Client::new(url); + client.set_auth(Some(kaldav::Authorization { username, password, })); + + Self { + client, + } + } + + fn show_content(&self) -> Result<()> { + // This function does not do what we want, because Principal::home() does not appear to + // work in our environment. + // Reading https://datatracker.ietf.org/doc/html/rfc4791#section-6.2.1 reveals that the + // CALDAV:calendar-home-set property merely SHOULD, rather than MUST, be implemented. + + let principals = self.client.principals()?; + // eprintln!("Principals: {principals:?}"); + + let mut home = None; + println!("Attempting to find home."); + for p in principals { + home = p.home().ok(); + if home.is_some() { + break; + } + } + if home.is_some() { + println!("Found home: {home:?}"); + } else { + return Err(anyhow!("No home found in any Principal. :(")); + } + + // This function is heavily based on: + // https://github.com/sanpii/kaldav/blob/main/examples/list_events.rs + eprintln!("Retrieving list of calendars."); + let calendars = self.client.calendars()?; + + for (name, calendar) in calendars { + println!("Calendar '{}'", name); + + let objects = calendar.events()?; + + if objects.is_empty() { + println!(" no events"); + continue; + } + + for object in objects { + for event in object.events { + println!( + " {} - {}", + event.dtstart, + event.summary.unwrap_or_default() + ); + } + } + } + Ok(()) + } +} + +fn main() -> Result<()> { + let caldav_url = args().nth(1).expect("Missing caldav url argument."); + let username = args().nth(2).expect("Missing username argument."); + let password = args().nth(3).expect("Missing password argument."); + + let kaldav = CrateKaldav::new(&caldav_url, username.clone(), Some(password.clone())); + kaldav.show_content()?; + + Ok(()) +} diff --git a/src/main.rs b/src/bin/ccc.rs index 13d24d6..13d24d6 100644 --- a/src/main.rs +++ b/src/bin/ccc.rs |