]> code.citadel.org Git - citadel.git/blob - citadel/serv_calendar.c
* List UID and SUMMARY properties of conflicting events
[citadel.git] / citadel / serv_calendar.c
1 /* 
2  * $Id$ 
3  *
4  * This module implements iCalendar object processing and the Calendar>
5  * room on a Citadel/UX server.  It handles iCalendar objects using the
6  * iTIP protocol.  See RFCs 2445 and 2446.
7  *
8  */
9
10 #include "sysdep.h"
11 #include <unistd.h>
12 #include <sys/types.h>
13 #include <limits.h>
14 #include <stdio.h>
15 #include <string.h>
16 #ifdef HAVE_STRINGS_H
17 #include <strings.h>
18 #endif
19 #include "serv_calendar.h"
20 #include "citadel.h"
21 #include "server.h"
22 #include "citserver.h"
23 #include "sysdep_decls.h"
24 #include "support.h"
25 #include "config.h"
26 #include "dynloader.h"
27 #include "user_ops.h"
28 #include "room_ops.h"
29 #include "tools.h"
30 #include "msgbase.h"
31 #include "mime_parser.h"
32 #ifdef HAVE_ICAL_H
33 #include <ical.h>
34 #endif
35
36
37 #ifdef HAVE_ICAL_H
38
39 struct ical_respond_data {
40         char desired_partnum[SIZ];
41         icalcomponent *cal;
42 };
43
44
45
46
47
48 /*
49  * Write our config to disk
50  */
51 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
52         char temp[PATH_MAX];
53         FILE *fp;
54         char *ser;
55
56         strcpy(temp, tmpnam(NULL));
57         ser = icalcomponent_as_ical_string(cal);
58         if (ser == NULL) return;
59
60         /* Make a temp file out of it */
61         fp = fopen(temp, "w");
62         if (fp == NULL) return;
63         fwrite(ser, strlen(ser), 1, fp);
64         fclose(fp);
65
66         /* This handy API function does all the work for us.
67          * NOTE: normally we would want to set that last argument to 1, to
68          * force the system to delete the user's old vCard.  But it doesn't
69          * have to, because the vcard_upload_beforesave() hook above
70          * is going to notice what we're trying to do, and delete the old vCard.
71          */
72         CtdlWriteObject(USERCALENDARROOM,       /* which room */
73                         "text/calendar",        /* MIME type */
74                         temp,                   /* temp file */
75                         u,                      /* which user */
76                         0,                      /* not binary */
77                         0,              /* don't delete others of this type */
78                         0);                     /* no flags */
79
80         unlink(temp);
81 }
82
83
84 /*
85  * Add a calendar object to the user's calendar
86  */
87 void ical_add(icalcomponent *cal, int recursion_level) {
88         icalcomponent *c;
89
90         /*
91          * The VEVENT subcomponent is the one we're interested in saving.
92          */
93         if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
94         
95                 ical_write_to_cal(&CC->usersupp, cal);
96
97         }
98
99         /* If the component has subcomponents, recurse through them. */
100         for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
101             (c != 0);
102             c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
103                 /* Recursively process subcomponent */
104                 ical_add(c, recursion_level+1);
105         }
106
107 }
108
109
110
111 /*
112  * Callback function for mime parser that hunts for calendar content types
113  * and turns them into calendar objects
114  */
115 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
116                 void *content, char *cbtype, size_t length, char *encoding,
117                 void *cbuserdata) {
118
119         struct ical_respond_data *ird = NULL;
120
121         ird = (struct ical_respond_data *) cbuserdata;
122         if (ird->cal != NULL) {
123                 icalcomponent_free(ird->cal);
124                 ird->cal = NULL;
125         }
126         if (strcasecmp(partnum, ird->desired_partnum)) return;
127         ird->cal = icalcomponent_new_from_string(content);
128 }
129
130
131 /*
132  * Respond to a meeting request.
133  */
134 void ical_respond(long msgnum, char *partnum, char *action) {
135         struct CtdlMessage *msg;
136         struct ical_respond_data ird;
137
138         if (
139            (strcasecmp(action, "accept"))
140            && (strcasecmp(action, "decline"))
141         ) {
142                 cprintf("%d Action must be 'accept' or 'decline'\n",
143                         ERROR + ILLEGAL_VALUE
144                 );
145                 return;
146         }
147
148         msg = CtdlFetchMessage(msgnum);
149         if (msg == NULL) {
150                 cprintf("%d Message %ld not found.\n",
151                         ERROR+ILLEGAL_VALUE,
152                         (long)msgnum
153                 );
154                 return;
155         }
156
157         memset(&ird, 0, sizeof ird);
158         strcpy(ird.desired_partnum, partnum);
159         mime_parser(msg->cm_fields['M'],
160                 NULL,
161                 *ical_locate_part,              /* callback function */
162                 NULL, NULL,
163                 (void *) &ird,                  /* user data */
164                 0
165         );
166
167         CtdlFreeMessage(msg);
168
169         if (ird.cal != NULL) {
170                 /* Save this in the user's calendar if necessary */
171                 if (!strcasecmp(action, "accept")) {
172                         ical_add(ird.cal, 0);
173                 }
174
175                 /* Send a reply if necessary */
176                 /* FIXME ... do this */
177
178                 /* Delete the message from the inbox */
179                 /* FIXME ... do this */
180
181                 icalcomponent_free(ird.cal);
182                 ird.cal = NULL;
183                 cprintf("%d ok\n", CIT_OK);
184                 return;
185         }
186         else {
187                 cprintf("%d No calendar object found\n", ERROR);
188                 return;
189         }
190
191         /* should never get here */
192 }
193
194
195 /*
196  * Search for a property in both the top level and in a VEVENT subcomponent
197  */
198 icalproperty *ical_ctdl_get_subprop(
199                 icalcomponent *cal,
200                 icalproperty_kind which_prop
201 ) {
202         icalproperty *p;
203         icalcomponent *c;
204
205         p = icalcomponent_get_first_property(cal, which_prop);
206         if (p == NULL) {
207                 c = icalcomponent_get_first_component(cal,
208                                                         ICAL_VEVENT_COMPONENT);
209                 if (c != NULL) {
210                         p = icalcomponent_get_first_property(c, which_prop);
211                 }
212         }
213         return p;
214 }
215
216
217 /*
218  * Check to see if two events overlap.  Returns nonzero if they do.
219  */
220 int ical_ctdl_is_overlap(
221                         struct icaltimetype t1start,
222                         struct icaltimetype t1end,
223                         struct icaltimetype t2start,
224                         struct icaltimetype t2end
225 ) {
226
227         if (icaltime_is_null_time(t1start)) return(0);
228         if (icaltime_is_null_time(t2start)) return(0);
229
230         /* First, check for all-day events */
231         if (t1start.is_date) {
232                 if (!icaltime_compare_date_only(t1start, t2start)) {
233                         return(1);
234                 }
235                 if (!icaltime_is_null_time(t2end)) {
236                         if (!icaltime_compare_date_only(t1start, t2end)) {
237                                 return(1);
238                         }
239                 }
240         }
241
242         if (t2start.is_date) {
243                 if (!icaltime_compare_date_only(t2start, t1start)) {
244                         return(1);
245                 }
246                 if (!icaltime_is_null_time(t1end)) {
247                         if (!icaltime_compare_date_only(t2start, t1end)) {
248                                 return(1);
249                         }
250                 }
251         }
252
253         /* Now check for overlaps using date *and* time. */
254
255         /* First, bail out if either event 1 or event 2 is missing end time. */
256         if (icaltime_is_null_time(t1end)) return(0);
257         if (icaltime_is_null_time(t2end)) return(0);
258
259         /* If event 1 ends before event 2 starts, we're in the clear. */
260         if (icaltime_compare(t1end, t2start) <= 0) return(0);
261
262         /* If event 2 ends before event 1 starts, we're also ok. */
263         if (icaltime_compare(t2end, t1start) <= 0) return(0);
264
265         /* Otherwise, they overlap. */
266         return(1);
267 }
268
269
270
271 /*
272  * Backend for ical_hunt_for_conflicts()
273  */
274 void vcard_hunt_for_conflicts_backend(long msgnum, void *data) {
275         icalcomponent *cal;
276         struct CtdlMessage *msg;
277         struct ical_respond_data ird;
278         struct icaltimetype t1start, t1end, t2start, t2end;
279         icalproperty *p;
280         char conflict_event_uid[SIZ];
281         char conflict_event_summary[SIZ];
282
283         cal = (icalcomponent *)data;
284         strcpy(conflict_event_uid, "");
285         strcpy(conflict_event_summary, "");
286
287         msg = CtdlFetchMessage(msgnum);
288         if (msg == NULL) return;
289         memset(&ird, 0, sizeof ird);
290         strcpy(ird.desired_partnum, "1");       /* hopefully it's always 1 */
291         mime_parser(msg->cm_fields['M'],
292                 NULL,
293                 *ical_locate_part,              /* callback function */
294                 NULL, NULL,
295                 (void *) &ird,                  /* user data */
296                 0
297         );
298         CtdlFreeMessage(msg);
299
300         if (ird.cal == NULL) return;
301
302         t1start = icaltime_null_time();
303         t1end = icaltime_null_time();
304         t2start = icaltime_null_time();
305         t1end = icaltime_null_time();
306
307         /* Now compare cal to ird.cal */
308         p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
309         if (p == NULL) return;
310         if (p != NULL) t2start = icalproperty_get_dtstart(p);
311         
312         p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
313         if (p != NULL) t2end = icalproperty_get_dtend(p);
314
315         p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
316         if (p == NULL) return;
317         if (p != NULL) t1start = icalproperty_get_dtstart(p);
318         
319         p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
320         if (p != NULL) t1end = icalproperty_get_dtend(p);
321         
322         p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
323         if (p != NULL) {
324                 strcpy(conflict_event_uid, icalproperty_get_comment(p));
325         }
326         p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
327         if (p != NULL) {
328                 strcpy(conflict_event_summary, icalproperty_get_comment(p));
329         }
330
331
332         icalcomponent_free(ird.cal);
333
334         if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
335                 cprintf("%ld||%s|%s|\n",
336                         msgnum,
337                         conflict_event_uid,
338                         conflict_event_summary
339                 );
340         }
341 }
342
343
344
345 /* 
346  * Phase 2 of "hunt for conflicts" operation.
347  * At this point we have a calendar object which represents the VEVENT that
348  * we're considering adding to the calendar.  Now hunt through the user's
349  * calendar room, and output zero or more existing VEVENTs which conflict
350  * with this one.
351  */
352 void ical_hunt_for_conflicts(icalcomponent *cal) {
353         char hold_rm[ROOMNAMELEN];
354
355         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
356
357         if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
358                 getroom(&CC->quickroom, hold_rm);
359                 cprintf("%d You do not have a calendar.\n", ERROR);
360                 return;
361         }
362
363         cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
364
365         CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
366                 NULL,
367                 vcard_hunt_for_conflicts_backend,
368                 (void *) cal
369         );
370
371         cprintf("000\n");
372         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
373
374 }
375
376
377
378 /*
379  * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
380  */
381 void ical_conflicts(long msgnum, char *partnum) {
382         struct CtdlMessage *msg;
383         struct ical_respond_data ird;
384
385         msg = CtdlFetchMessage(msgnum);
386         if (msg == NULL) {
387                 cprintf("%d Message %ld not found.\n",
388                         ERROR+ILLEGAL_VALUE,
389                         (long)msgnum
390                 );
391                 return;
392         }
393
394         memset(&ird, 0, sizeof ird);
395         strcpy(ird.desired_partnum, partnum);
396         mime_parser(msg->cm_fields['M'],
397                 NULL,
398                 *ical_locate_part,              /* callback function */
399                 NULL, NULL,
400                 (void *) &ird,                  /* user data */
401                 0
402         );
403
404         CtdlFreeMessage(msg);
405
406         if (ird.cal != NULL) {
407                 ical_hunt_for_conflicts(ird.cal);
408                 icalcomponent_free(ird.cal);
409                 return;
410         }
411         else {
412                 cprintf("%d No calendar object found\n", ERROR);
413                 return;
414         }
415
416         /* should never get here */
417 }
418
419
420
421
422 /*
423  * All Citadel calendar commands from the client come through here.
424  */
425 void cmd_ical(char *argbuf)
426 {
427         char subcmd[SIZ];
428         long msgnum;
429         char partnum[SIZ];
430         char action[SIZ];
431
432         if (CtdlAccessCheck(ac_logged_in)) return;
433
434         extract(subcmd, argbuf, 0);
435
436         if (!strcmp(subcmd, "test")) {
437                 cprintf("%d This server supports calendaring\n", CIT_OK);
438                 return;
439         }
440
441         else if (!strcmp(subcmd, "respond")) {
442                 msgnum = extract_long(argbuf, 1);
443                 extract(partnum, argbuf, 2);
444                 extract(action, argbuf, 3);
445                 ical_respond(msgnum, partnum, action);
446         }
447
448         else if (!strcmp(subcmd, "conflicts")) {
449                 msgnum = extract_long(argbuf, 1);
450                 extract(partnum, argbuf, 2);
451                 ical_conflicts(msgnum, partnum);
452         }
453
454         else {
455                 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
456                 return;
457         }
458
459         /* should never get here */
460 }
461
462 #endif /* HAVE_ICAL_H */
463
464
465 /*
466  * We don't know if the calendar room exists so we just create it at login
467  */
468 void ical_create_room(void)
469 {
470         struct quickroom qr;
471         struct visit vbuf;
472
473         /* Create the calendar room if it doesn't already exist */
474         create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
475
476         /* Set expiration policy to manual; otherwise objects will be lost! */
477         if (lgetroom(&qr, USERCALENDARROOM)) {
478                 lprintf(3, "Couldn't get the user calendar room!\n");
479                 return;
480         }
481         qr.QRep.expire_mode = EXPIRE_MANUAL;
482         lputroom(&qr);
483
484         /* Set the view to a calendar view */
485         CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
486         vbuf.v_view = 3;        /* 3 = calendar */
487         CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
488
489         /* Create the tasks list room if it doesn't already exist */
490         create_room(USERTASKSROOM, 4, "", 0, 1, 0);
491
492         /* Set expiration policy to manual; otherwise objects will be lost! */
493         if (lgetroom(&qr, USERTASKSROOM)) {
494                 lprintf(3, "Couldn't get the user calendar room!\n");
495                 return;
496         }
497         qr.QRep.expire_mode = EXPIRE_MANUAL;
498         lputroom(&qr);
499
500         /* Set the view to a task list view */
501         CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
502         vbuf.v_view = 4;        /* 4 = tasks */
503         CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
504
505         return;
506 }
507
508
509 /* See if we need to prevent the object from being saved */
510 int ical_obj_beforesave(struct CtdlMessage *msg)
511 {
512         char roomname[ROOMNAMELEN];
513         char *p;
514         int a;
515         
516         /*
517          * Only messages with content-type text/calendar or text/x-calendar
518          * may be saved to Calendar>.  If the message is bound for
519          * Calendar> but doesn't have this content-type, throw an error
520          * so that the message may not be posted.
521          */
522
523         /* First determine if this is our room */
524         MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
525         if (strncmp(roomname, msg->cm_fields['O'], ROOMNAMELEN))
526                 return 0;       /* It's not us... */
527
528         /* Then determine content-type of the message */
529         
530         /* It must be an RFC822 message! */
531         /* FIXME: Not handling MIME multipart messages; implement with IMIP */
532         if (msg->cm_format_type != 4)
533                 return 1;       /* You tried to save a non-RFC822 message! */
534         
535         /* Find the Content-Type: header */
536         p = msg->cm_fields['M'];
537         a = strlen(p);
538         while (--a > 0) {
539                 if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
540                         if (!strncasecmp(p + 14, "text/x-calendar", 15) ||
541                             !strncasecmp(p + 14, "text/calendar", 13))
542                                 return 0;
543                         else
544                                 return 1;
545                 }
546                 p++;
547         }
548         
549         /* Oops!  No Content-Type in this message!  How'd that happen? */
550         lprintf(7, "RFC822 message with no Content-Type header!\n");
551         return 1;
552 }
553
554
555
556 /* Register this module with the Citadel server. */
557 char *Dynamic_Module_Init(void)
558 {
559         CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
560 #ifdef HAVE_ICAL_H
561         CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
562         CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
563 #endif
564         return "$Id$";
565 }