| 1 | use quick_xml::Reader; |
| 2 | use quick_xml::events::Event; |
| 3 | |
| 4 | #[derive(Debug, Clone, Default)] |
| 5 | pub struct PropfindResponse { |
| 6 | pub href: String, |
| 7 | pub displayname: Option<String>, |
| 8 | pub calendar_color: Option<String>, |
| 9 | pub resource_type_calendar: bool, |
| 10 | pub supports_vevent: bool, |
| 11 | pub writable: bool, |
| 12 | pub current_user_principal: Option<String>, |
| 13 | pub calendar_home_set: Option<String>, |
| 14 | pub calendar_user_addresses: Vec<String>, |
| 15 | pub schedule_outbox: Option<String>, |
| 16 | pub getctag: Option<String>, |
| 17 | } |
| 18 | |
| 19 | pub fn parse_multistatus(xml: &str) -> Vec<PropfindResponse> { |
| 20 | let mut reader = Reader::from_str(xml); |
| 21 | reader.config_mut().trim_text(true); |
| 22 | let mut responses = Vec::new(); |
| 23 | let mut current = PropfindResponse::default(); |
| 24 | let mut in_response = false; |
| 25 | let mut buf = Vec::new(); |
| 26 | let mut path: Vec<String> = Vec::new(); |
| 27 | loop { |
| 28 | match reader.read_event_into(&mut buf) { |
| 29 | Ok(Event::Start(e)) => { |
| 30 | let local = local_name(&e.name().as_ref()); |
| 31 | path.push(local.clone()); |
| 32 | if local == "response" { |
| 33 | in_response = true; |
| 34 | current = PropfindResponse::default(); |
| 35 | } |
| 36 | if local == "calendar" && path.iter().any(|p| p == "resourcetype") { |
| 37 | current.resource_type_calendar = true; |
| 38 | } |
| 39 | if local == "comp" { |
| 40 | for attr in e.attributes().flatten() { |
| 41 | if local_name(attr.key.as_ref()) == "name" |
| 42 | && attr |
| 43 | .unescape_value() |
| 44 | .map(|v| v.eq_ignore_ascii_case("VEVENT")) |
| 45 | .unwrap_or(false) |
| 46 | { |
| 47 | current.supports_vevent = true; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | Ok(Event::Empty(e)) => { |
| 53 | let local = local_name(&e.name().as_ref()); |
| 54 | if local == "calendar" && path.iter().any(|p| p == "resourcetype") { |
| 55 | current.resource_type_calendar = true; |
| 56 | } |
| 57 | if local == "comp" { |
| 58 | for attr in e.attributes().flatten() { |
| 59 | if local_name(attr.key.as_ref()) == "name" |
| 60 | && attr |
| 61 | .unescape_value() |
| 62 | .map(|v| v.eq_ignore_ascii_case("VEVENT")) |
| 63 | .unwrap_or(false) |
| 64 | { |
| 65 | current.supports_vevent = true; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | if local == "write" || local == "write-content" || local == "all" { |
| 70 | if path.iter().any(|p| p == "current-user-privilege-set") { |
| 71 | current.writable = true; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | Ok(Event::Text(t)) => { |
| 76 | let text = t.unescape().unwrap_or_default().to_string(); |
| 77 | let leaf = path.last().cloned().unwrap_or_default(); |
| 78 | if !in_response { |
| 79 | continue; |
| 80 | } |
| 81 | match leaf.as_str() { |
| 82 | "href" => { |
| 83 | if path.iter().any(|p| p == "current-user-principal") { |
| 84 | current.current_user_principal = Some(text); |
| 85 | } else if path.iter().any(|p| p == "calendar-home-set") { |
| 86 | current.calendar_home_set = Some(text); |
| 87 | } else if path.iter().any(|p| p == "calendar-user-address-set") { |
| 88 | current.calendar_user_addresses.push(text); |
| 89 | } else if path.iter().any(|p| p == "schedule-outbox-URL") { |
| 90 | current.schedule_outbox = Some(text); |
| 91 | } else if current.href.is_empty() { |
| 92 | current.href = text; |
| 93 | } |
| 94 | } |
| 95 | "displayname" => current.displayname = Some(text), |
| 96 | "calendar-color" | "color" => current.calendar_color = Some(text), |
| 97 | "getctag" | "getetag" => { |
| 98 | current.getctag = Some(text.trim_matches('"').to_owned()) |
| 99 | } |
| 100 | _ => {} |
| 101 | } |
| 102 | } |
| 103 | Ok(Event::End(e)) => { |
| 104 | let local = local_name(&e.name().as_ref()); |
| 105 | if local == "response" && in_response { |
| 106 | responses.push(std::mem::take(&mut current)); |
| 107 | in_response = false; |
| 108 | } |
| 109 | path.pop(); |
| 110 | } |
| 111 | Ok(Event::Eof) => break, |
| 112 | Err(_) => break, |
| 113 | _ => {} |
| 114 | } |
| 115 | buf.clear(); |
| 116 | } |
| 117 | responses |
| 118 | } |
| 119 | |
| 120 | pub fn parse_calendar_query(xml: &str) -> Vec<(String, Option<String>, String)> { |
| 121 | // href, etag, calendar-data |
| 122 | let mut reader = Reader::from_str(xml); |
| 123 | reader.config_mut().trim_text(true); |
| 124 | let mut out = Vec::new(); |
| 125 | let mut href = String::new(); |
| 126 | let mut etag = None; |
| 127 | let mut data = String::new(); |
| 128 | let mut in_response = false; |
| 129 | let mut path: Vec<String> = Vec::new(); |
| 130 | let mut buf = Vec::new(); |
| 131 | loop { |
| 132 | match reader.read_event_into(&mut buf) { |
| 133 | Ok(Event::Start(e)) => { |
| 134 | let local = local_name(&e.name().as_ref()); |
| 135 | path.push(local.clone()); |
| 136 | if local == "response" { |
| 137 | in_response = true; |
| 138 | href.clear(); |
| 139 | etag = None; |
| 140 | data.clear(); |
| 141 | } |
| 142 | } |
| 143 | Ok(Event::Text(t)) => { |
| 144 | if !in_response { |
| 145 | continue; |
| 146 | } |
| 147 | let text = t.unescape().unwrap_or_default().to_string(); |
| 148 | let leaf = path.last().cloned().unwrap_or_default(); |
| 149 | match leaf.as_str() { |
| 150 | "href" if href.is_empty() => href = text, |
| 151 | "getetag" => etag = Some(text.trim_matches('"').to_owned()), |
| 152 | "calendar-data" => data.push_str(&text), |
| 153 | _ => {} |
| 154 | } |
| 155 | } |
| 156 | Ok(Event::CData(t)) => { |
| 157 | if path.last().map(|s| s.as_str()) == Some("calendar-data") { |
| 158 | data.push_str(&String::from_utf8_lossy(&t)); |
| 159 | } |
| 160 | } |
| 161 | Ok(Event::End(e)) => { |
| 162 | let local = local_name(&e.name().as_ref()); |
| 163 | if local == "response" && in_response { |
| 164 | if !data.is_empty() { |
| 165 | out.push((href.clone(), etag.clone(), data.clone())); |
| 166 | } |
| 167 | in_response = false; |
| 168 | } |
| 169 | path.pop(); |
| 170 | } |
| 171 | Ok(Event::Eof) => break, |
| 172 | Err(_) => break, |
| 173 | _ => {} |
| 174 | } |
| 175 | buf.clear(); |
| 176 | } |
| 177 | out |
| 178 | } |
| 179 | |
| 180 | fn local_name(name: &[u8]) -> String { |
| 181 | let full = String::from_utf8_lossy(name); |
| 182 | let after_ns = full.rsplit('}').next().unwrap_or(&full); |
| 183 | after_ns |
| 184 | .rsplit(':') |
| 185 | .next() |
| 186 | .unwrap_or(after_ns) |
| 187 | .to_ascii_lowercase() |
| 188 | } |
| 189 | |
| 190 | pub fn propfind_body(props: &[&str]) -> String { |
| 191 | let mut body = String::from( |
| 192 | r#"<?xml version="1.0" encoding="utf-8"?> |
| 193 | <d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:cs="http://calendarserver.org/ns/" xmlns:x="http://apple.com/ns/ical/"> |
| 194 | <d:prop> |
| 195 | "#, |
| 196 | ); |
| 197 | for prop in props { |
| 198 | body.push_str(" "); |
| 199 | body.push_str(prop); |
| 200 | body.push('\n'); |
| 201 | } |
| 202 | body.push_str(" </d:prop>\n</d:propfind>"); |
| 203 | body |
| 204 | } |
| 205 | |
| 206 | pub fn calendar_query_body(start: &str, end: &str, expand: bool) -> String { |
| 207 | let expand_xml = if expand { |
| 208 | format!( |
| 209 | r#" <c:expand start="{start}" end="{end}"/> |
| 210 | "# |
| 211 | ) |
| 212 | } else { |
| 213 | String::new() |
| 214 | }; |
| 215 | format!( |
| 216 | r#"<?xml version="1.0" encoding="utf-8"?> |
| 217 | <c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"> |
| 218 | <d:prop> |
| 219 | <d:getetag/> |
| 220 | <c:calendar-data> |
| 221 | {expand_xml} </c:calendar-data> |
| 222 | </d:prop> |
| 223 | <c:filter> |
| 224 | <c:comp-filter name="VCALENDAR"> |
| 225 | <c:comp-filter name="VEVENT"> |
| 226 | <c:time-range start="{start}" end="{end}"/> |
| 227 | </c:comp-filter> |
| 228 | </c:comp-filter> |
| 229 | </c:filter> |
| 230 | </c:calendar-query>"# |
| 231 | ) |
| 232 | } |