]> Pileus Git - lackey/blob - cals/ical.c
3ee3c68b073352239701dc97354066e0c7e48b9c
[lackey] / cals / ical.c
1 /*
2  * Copyright (C) 2012-2013 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #define _XOPEN_SOURCE
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <wordexp.h>
23 #include <libical/ical.h>
24
25 #include "util.h"
26 #include "conf.h"
27 #include "date.h"
28 #include "cal.h"
29
30 /* Local types */
31 typedef struct {
32         cal_t *cal;
33         icalcomponent *comp;
34         struct icaltimetype start;
35         struct icaltimetype end;
36 } ical_inst;
37
38 typedef struct ical_t {
39         cal_t          cal;
40         char          *location;
41         char          *username;
42         char          *password;
43         icalcomponent *comp;
44         struct ical_t *next;
45 } ical_t;
46
47 /* Static data */
48 static ical_t *calendars;
49
50 /* Helper functions */
51 static int ical_compare(const void *_a, const void *_b)
52 {
53         const ical_inst *a = _a;
54         const ical_inst *b = _b;
55         int scomp = icaltime_compare(a->start, b->start);
56         int ecomp = icaltime_compare(a->end,   b->end);
57         return scomp != 0 ? scomp :
58                ecomp != 0 ? ecomp : 0 ;
59 }
60
61 static date_t to_date(struct icaltimetype time)
62 {
63         return (date_t){
64                 .year  = time.year,
65                 .month = time.month ? time.month-1 : 0,
66                 .day   = time.day   ? time.day  -1 : 0,
67                 .hour  = time.hour,
68                 .min   = time.minute,
69         };
70 }
71
72 static icaltimetype to_itime(date_t time)
73 {
74         return (struct icaltimetype){
75                 .year   = time.year,
76                 .month  = time.month + 1,
77                 .day    = time.day   + 1,
78                 .hour   = time.hour,
79                 .minute = time.min
80         };
81 }
82
83 static void add_recur(cal_t *cal,
84                 icalarray *array, icalcomponent *comp,
85                 icaltimetype start, icaltimetype end,
86                 icalcomponent_kind which)
87 {
88         icalcomponent_kind kind = icalcomponent_isa(comp);
89
90         if (kind == which) {
91                 /* Get recurrence data */
92                 struct icaltimetype cstart, cend; // Component times
93                 struct icaltimetype istart, iend; // Instance times
94                 struct icaldurationtype length;   // Duration
95
96                 icalproperty             *rrule;
97                 struct icalrecurrencetype recur;
98                 icalrecur_iterator       *iter;
99
100                 cstart = icalcomponent_get_dtstart(comp);
101                 cend   = icalcomponent_get_dtend(comp);
102                 length = icaltime_subtract(cend, cstart);
103
104                 /* Full day event */
105                 if (icaltime_is_null_time(cstart) ||
106                     which == ICAL_VTODO_COMPONENT) {
107                         icalarray_append(array, &(ical_inst){
108                                 .cal   = cal,
109                                 .comp  = comp,
110                                 .start = cstart,
111                                 .end   = cend,
112                         });
113                 }
114
115                 /* Add all recurrences */
116                 rrule = icalcomponent_get_first_property(comp, ICAL_RRULE_PROPERTY);
117
118                 /* One-time event */
119                 if (!rrule) {
120                         icalarray_append(array, &(ical_inst){
121                                 .cal   = cal,
122                                 .comp  = comp,
123                                 .start = cstart,
124                                 .end   = cend,
125                         });
126                 }
127
128                 /* Recurring events */
129                 while (rrule) {
130                         recur = icalproperty_get_rrule(rrule);
131                         iter  = icalrecur_iterator_new(recur, cstart);
132
133                         /* Add recurrence for this rrule */
134                         while (1) {
135                                 istart = iend = icalrecur_iterator_next(iter);
136                                 if (icaltime_is_null_time(istart))
137                                         break;    // no more instances
138                                 if (!icaltime_is_null_time(cend))
139                                         iend = icaltime_add(iend, length);
140
141                                 if (icaltime_compare(iend, start) <= 0)
142                                         continue; // instance ends before start time
143                                 if (icaltime_compare(istart, end) >= 0)
144                                         break;    // instance begins after stop time
145
146                                 icalarray_append(array, &(ical_inst){
147                                         .cal   = cal,
148                                         .comp  = comp,
149                                         .start = istart,
150                                         .end   = iend,
151                                 });
152                         }
153
154                         icalrecur_iterator_free(iter);
155                         rrule = icalcomponent_get_next_property(comp, ICAL_RRULE_PROPERTY);
156                 }
157         }
158
159         /* Add children */
160         icalcomponent_kind find = ICAL_ANY_COMPONENT;
161         icalcomponent *child = icalcomponent_get_first_component(comp, find);
162         while (child) {
163                 add_recur(cal, array, child, start, end, which);
164                 child = icalcomponent_get_next_component(comp, find);
165         }
166 }
167
168 static void read_icals(void)
169 {
170         for (ical_t *cal = calendars; cal; cal = cal->next) {
171                 if (cal->comp == NULL && cal->location) {
172                         wordexp_t wexp;
173                         wordexp(cal->location, &wexp, WRDE_NOCMD);
174                         icalparser *parser = icalparser_new();
175                         for (int i = 0; i < wexp.we_wordc; i++) {
176                                 FILE *file = fopen(wexp.we_wordv[i], "r");
177                                 if (!file)
178                                         continue;
179                                 icalparser_set_gen_data(parser, file);
180                         }
181                         cal->comp = icalparser_parse(parser, (void*)fgets);
182                         icalparser_free(parser);
183                         wordfree(&wexp);
184                 }
185         }
186 }
187
188 /* Event functions */
189 static event_t *to_event(ical_inst *inst)
190 {
191         icalproperty *prop = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
192
193         event_t *event = new0(event_t);
194         event->name  = strcopy(icalcomponent_get_summary(inst->comp));
195         event->desc  = strcopy(icalcomponent_get_description(inst->comp));
196         event->loc   = strcopy(icalcomponent_get_location(inst->comp));
197         event->cat   = icalproperty_get_value_as_string_r(prop);
198         event->start = to_date(inst->start);
199         event->end   = to_date(inst->end);
200         event->cal   = inst->cal;
201         return event;
202 }
203
204 static event_t *to_events(icalarray *array)
205 {
206         event_t  list = {};
207         event_t *tail = &list;
208         for (int i = 0; i < array->num_elements; i++) {
209                  ical_inst *inst = icalarray_element_at(array, i);
210                  tail->next = to_event(inst);
211                  tail = tail->next;
212         }
213         return list.next;
214 }
215
216 static void print_events(event_t *start)
217 {
218         for (event_t *cur = start; cur; cur = cur->next)
219                 printf("%04d-%02d-%02d %02d:%02d - %s\n",
220                         cur->start.year, cur->start.month, cur->start.day,
221                         cur->start.hour, cur->start.min,
222                         cur->name ?: cur->desc ?: "[no summary]");
223 }
224
225 /* Todo functions */
226 static todo_t *to_todo(ical_inst *inst)
227 {
228         icalproperty *cat  = icalcomponent_get_first_property(inst->comp, ICAL_CATEGORIES_PROPERTY);
229         icalproperty *perc = icalcomponent_get_first_property(inst->comp, ICAL_PERCENTCOMPLETE_PROPERTY);
230
231         todo_t *todo = new0(todo_t);
232         todo->name   = strcopy(icalcomponent_get_summary(inst->comp));
233         todo->desc   = strcopy(icalcomponent_get_description(inst->comp));
234         todo->cat    = strcopy(icalproperty_get_value_as_string(cat));
235         todo->status = icalcomponent_get_status(inst->comp) == ICAL_STATUS_COMPLETED ? 100 :
236                        perc ? icalproperty_get_percentcomplete(perc) : 0;
237         todo->start  = to_date(inst->start);
238         todo->due    = to_date(icalcomponent_get_due(inst->comp));
239         todo->cal    = inst->cal;
240         return todo;
241 }
242
243 static todo_t *to_todos(icalarray *array)
244 {
245         todo_t  list = {};
246         todo_t *tail = &list;
247         for (int i = 0; i < array->num_elements; i++) {
248                  ical_inst *inst = icalarray_element_at(array, i);
249                  tail->next = to_todo(inst);
250                  tail = tail->next;
251         }
252         return list.next;
253 }
254
255 static void print_todos(todo_t *start)
256 {
257         for (todo_t *cur = start; cur; cur = cur->next)
258                 printf("%04d-%02d-%02d %02d:%02d - %d%% - %s\n",
259                         cur->due.year, cur->due.month, cur->due.day,
260                         cur->due.hour, cur->due.min,   cur->status,
261                         cur->name ?: cur->desc ?: "[no summary]");
262 }
263
264 /* Config parser */
265 void ical_config(const char *group, const char *name, const char *key, const char *value)
266 {
267         ical_t *cal = NULL, *last = NULL;
268
269         /* Make sure it's valid */
270         if (!match(group, "ical") || !name)
271                 return;
272
273         /* Find existing calendar */
274         for (cal = calendars; cal; last = cal, cal = cal->next)
275                 if (match(cal->cal.name, name))
276                         break;
277
278         /* Create new calendar */
279         if (!cal) {
280                 cal = new0(ical_t);
281                 cal->cal.type = "ical";
282                 cal->cal.name = get_name(name);
283                 if (last)
284                         last->next = cal;
285                 else
286                         calendars = cal;
287                 return;
288         }
289
290         /* Set calendar values */
291         if (match(key, "location"))
292                 cal->location = get_string(value);
293         else if (match(key, "username"))
294                 cal->username = get_string(value);
295         else if (match(key, "password"))
296                 cal->password = get_string(value);
297 }
298
299 /* Cal functions */
300 cal_t *ical_cals(void)
301 {
302         read_icals();
303
304         for (ical_t *cal = calendars; cal; cal = cal->next)
305                 cal->cal.next = &cal->next->cal;
306
307         return &calendars->cal;
308 }
309
310 /* Event functions */
311 event_t *ical_events(date_t _start, date_t _end)
312 {
313         read_icals();
314
315         icaltimetype start = to_itime(_start);
316         icaltimetype end   = to_itime(_end);
317         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
318         for (ical_t *cal = calendars; cal; cal = cal->next)
319                 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VEVENT_COMPONENT);
320         icalarray_sort(array, ical_compare);
321         event_t *events = to_events(array);
322         icalarray_free(array);
323
324         return events;
325 }
326
327 /* Todo functions */
328 todo_t *ical_todos(date_t _start, date_t _end)
329 {
330         read_icals();
331
332         icaltimetype start = to_itime(_start);
333         icaltimetype end   = to_itime(_end);
334         icalarray *array = icalarray_new(sizeof(ical_inst), 1);
335         for (ical_t *cal = calendars; cal; cal = cal->next)
336                 add_recur(&cal->cal, array, cal->comp, start, end, ICAL_VTODO_COMPONENT);
337         icalarray_sort(array, ical_compare);
338         todo_t *todos = to_todos(array);
339         icalarray_free(array);
340
341         return todos;
342 }
343
344 /* Test functions */
345 void ical_printr(icalcomponent *comp, int depth)
346 {
347         /* Print component */
348         icalcomponent_kind kind = icalcomponent_isa(comp);
349         printf("%*s", depth, "");
350         printf("%s",  icalcomponent_kind_to_string(kind));
351         if (kind == ICAL_VEVENT_COMPONENT ||
352             kind == ICAL_VTODO_COMPONENT)
353                 printf(" - %s", icalcomponent_get_summary(comp) ?: "[no summary]");
354         printf("\n");
355
356         /* Print children */
357         icalcomponent_kind find = ICAL_ANY_COMPONENT;
358         icalcomponent *child = icalcomponent_get_first_component(comp, find);
359         while (child) {
360                 ical_printr(child, depth+2);
361                 child = icalcomponent_get_next_component(comp, find);
362         }
363 }
364
365 void ical_test(char *path)
366 {
367         /* Load ical */
368         FILE *file = fopen(path, "r");
369         icalparser *parser = icalparser_new();
370         icalparser_set_gen_data(parser, file);
371         icalcomponent *comp = icalparser_parse(parser, (void*)fgets);
372
373         /* Misc */
374         icalarray *array;
375         icaltimetype start = {.year = 2000};
376         icaltimetype end   = {.year = 2020};
377
378         /* Find events */
379         array = icalarray_new(sizeof(ical_inst), 1);
380         add_recur(NULL, array, comp, start, end, ICAL_VEVENT_COMPONENT);
381         icalarray_sort(array, ical_compare);
382         event_t *events = to_events(array);
383         icalarray_free(array);
384
385         /* Find Todos */
386         array = icalarray_new(sizeof(ical_inst), 1);
387         add_recur(NULL, array, comp, start, end, ICAL_VTODO_COMPONENT);
388         icalarray_sort(array, ical_compare);
389         todo_t *todos = to_todos(array);
390         icalarray_free(array);
391
392         /* Print */
393         ical_printr(comp, 0);
394         print_events(events);
395         print_todos(todos);
396
397         (void)print_events;
398         (void)print_todos;
399         (void)events;
400         (void)todos;
401
402         /* Cleanup */
403         icalparser_free(parser);
404 }