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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use std::collections::HashMap;

use anyhow::{anyhow, Result};
use chrono::{format::ParseErrorKind, prelude::*};

use serde::{Deserialize, Serialize};
use ureq::Agent;
use url::Url;

use crate::tasks::Task;

use super::CALDAV_ICON;

#[derive(Serialize, Deserialize, Clone)]
#[serde(default)]
pub struct CalDavSource {
    #[serde(skip)]
    agent: ureq::Agent,
    pub calendar_name: String,
    pub username: String,
    pub password: String,
    pub base_url: String,
}

impl Default for CalDavSource {
    fn default() -> Self {
        Self {
            agent: Agent::new(),
            calendar_name: String::default(),
            username: String::default(),
            password: String::default(),
            base_url: String::default(),
        }
    }
}

const DATE_TIME_FORMAT: &str = "%Y%m%dT%H%M%S";
const DATE_ONLY_FORMAT: &str = "%Y%m%d";
const DATE_TIME_FORMAT_WITH_TZ: &str = "%Y%m%dT%H%M%S%#z";

fn parse_caldav_date(data: &str) -> Result<DateTime<Utc>> {
    match DateTime::parse_from_str(data, DATE_TIME_FORMAT_WITH_TZ) {
        Ok(result) => {
            let result_utc: DateTime<Utc> = DateTime::from(result);
            Ok(result_utc)
        }
        Err(e) => {
            if e.kind() == ParseErrorKind::TooShort {
                // Try without a timezone and intepret it as local
                let result_local =
                    NaiveDateTime::parse_from_str(data, DATE_TIME_FORMAT).or_else(|e| {
                        // This could be only a date without a time
                        if e.kind() == ParseErrorKind::TooShort {
                            let date = NaiveDate::parse_from_str(data, DATE_ONLY_FORMAT)?;
                            let end_of_day =
                                NaiveTime::from_hms_opt(23, 59, 59).unwrap_or_default();
                            Ok(date.and_time(end_of_day))
                        } else {
                            Err(e)
                        }
                    })?;
                match result_local.and_local_timezone(Local) {
                    chrono::offset::LocalResult::Single(result_local) => Ok(result_local.into()),

                    chrono::offset::LocalResult::Ambiguous(earliest, _) => Ok(earliest.into()),
                    chrono::offset::LocalResult::None => {
                        Err(anyhow!("The local time {:#?} does not exist", result_local))
                    }
                }
            } else {
                Err(anyhow::Error::from(e).context(format!("Could not parse CalDAV date '{data}'")))
            }
        }
    }
}

impl CalDavSource {
    pub fn query_tasks(&self) -> Result<Vec<Task>> {
        let base_url = Url::parse(&self.base_url)?;
        let credentials =
            minicaldav::Credentials::Basic(self.username.clone(), self.password.clone());
        let calendars = minicaldav::get_calendars(self.agent.clone(), &credentials, &base_url)?;
        let mut result = Vec::default();
        for c in calendars {
            if c.name().as_str() == self.calendar_name {
                let (todos, _errors) = minicaldav::get_todos(self.agent.clone(), &credentials, &c)?;
                for t in todos {
                    let props: HashMap<String, String> = t
                        .properties_todo()
                        .into_iter()
                        .map(|(k, v)| (k.to_string(), v.to_string()))
                        .collect();
                    let completed = props
                        .get("STATUS")
                        .filter(|s| s.as_str() == "COMPLETED")
                        .is_some()
                        || props.contains_key("COMPLETED");
                    // Check start due date if this task is ready to be started on
                    let start_due = props
                        .get("DTSTART")
                        .map(|raw| parse_caldav_date(raw))
                        .transpose()?;
                    let can_start = if let Some(start_due) = start_due {
                        let start_due: DateTime<Local> = DateTime::from(start_due);
                        Local::now().cmp(&start_due).is_ge()
                    } else {
                        true
                    };
                    if !completed && can_start {
                        if let Some(title) = props.get("SUMMARY") {
                            let title = unescape(title);
                            let description: String = props
                                .get("DESCRIPTION")
                                .map(|s| unescape(s))
                                .unwrap_or_default();
                            let due = props
                                .get("DUE")
                                .map(|raw| parse_caldav_date(raw))
                                .transpose()?;

                            let created = props
                                .get("CREATED")
                                .map(|raw| parse_caldav_date(raw))
                                .transpose()?;

                            let task = Task {
                                project: format!("{} {}", CALDAV_ICON, c.name()),
                                title: title.clone(),
                                description,
                                due: due.map(DateTime::<Utc>::from),
                                created: created.map(DateTime::<Utc>::from),
                                id: props.get("UID").cloned(),
                            };
                            result.push(task);
                        }
                    }
                }
            }
        }
        Ok(result)
    }
}

/// Unescape some known escaped characters in CalDAV.
/// This always allocates a new string.
fn unescape(val: &str) -> String {
    let mut chars = val.chars().peekable();
    let mut unescaped = String::new();

    loop {
        match chars.next() {
            None => break,
            Some(c) => {
                let escaped_char = if c == '\\' {
                    if let Some(escaped_char) = chars.peek() {
                        let escaped_char = *escaped_char;
                        match escaped_char {
                            _ if escaped_char == '\\'
                                || escaped_char == '"'
                                || escaped_char == '\''
                                || escaped_char == '`'
                                || escaped_char == '$'
                                || escaped_char == ',' =>
                            {
                                Some(escaped_char)
                            }
                            'n' => Some('\n'),
                            'r' => Some('\r'),
                            't' => Some('\t'),
                            _ => None,
                        }
                    } else {
                        None
                    }
                } else {
                    None
                };
                if let Some(escaped_char) = escaped_char {
                    unescaped.push(escaped_char);
                    // skip the escaped character instead of outputting it again
                    chars.next();
                } else {
                    unescaped.push(c);
                };
            }
        }
    }

    unescaped
}

#[cfg(test)]
mod tests;