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.
12 #include <sys/types.h>
19 #include "serv_calendar.h"
22 #include "citserver.h"
23 #include "sysdep_decls.h"
26 #include "dynloader.h"
31 #include "mime_parser.h"
39 struct ical_respond_data {
40 char desired_partnum[SIZ];
49 * Write our config to disk
51 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
56 strcpy(temp, tmpnam(NULL));
57 ser = icalcomponent_as_ical_string(cal);
58 if (ser == NULL) return;
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);
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.
72 CtdlWriteObject(USERCALENDARROOM, /* which room */
73 "text/calendar", /* MIME type */
77 0, /* don't delete others of this type */
85 * Add a calendar object to the user's calendar
87 void ical_add(icalcomponent *cal, int recursion_level) {
91 * The VEVENT subcomponent is the one we're interested in saving.
93 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
95 ical_write_to_cal(&CC->usersupp, cal);
99 /* If the component has subcomponents, recurse through them. */
100 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
102 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
103 /* Recursively process subcomponent */
104 ical_add(c, recursion_level+1);
112 * Callback function for mime parser that hunts for calendar content types
113 * and turns them into calendar objects
115 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
116 void *content, char *cbtype, size_t length, char *encoding,
119 struct ical_respond_data *ird = NULL;
121 ird = (struct ical_respond_data *) cbuserdata;
122 if (ird->cal != NULL) {
123 icalcomponent_free(ird->cal);
126 if (strcasecmp(partnum, ird->desired_partnum)) return;
127 ird->cal = icalcomponent_new_from_string(content);
132 * Respond to a meeting request.
134 void ical_respond(long msgnum, char *partnum, char *action) {
135 struct CtdlMessage *msg;
136 struct ical_respond_data ird;
139 (strcasecmp(action, "accept"))
140 && (strcasecmp(action, "decline"))
142 cprintf("%d Action must be 'accept' or 'decline'\n",
143 ERROR + ILLEGAL_VALUE
148 msg = CtdlFetchMessage(msgnum);
150 cprintf("%d Message %ld not found.\n",
157 memset(&ird, 0, sizeof ird);
158 strcpy(ird.desired_partnum, partnum);
159 mime_parser(msg->cm_fields['M'],
161 *ical_locate_part, /* callback function */
163 (void *) &ird, /* user data */
167 CtdlFreeMessage(msg);
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);
175 /* Send a reply if necessary */
176 /* FIXME ... do this */
178 /* Delete the message from the inbox */
179 /* FIXME ... do this */
181 icalcomponent_free(ird.cal);
183 cprintf("%d ok\n", CIT_OK);
187 cprintf("%d No calendar object found\n", ERROR);
191 /* should never get here */
196 * Search for a property in both the top level and in a VEVENT subcomponent
198 icalproperty *ical_ctdl_get_subprop(
200 icalproperty_kind which_prop
205 p = icalcomponent_get_first_property(cal, which_prop);
207 c = icalcomponent_get_first_component(cal,
208 ICAL_VEVENT_COMPONENT);
210 p = icalcomponent_get_first_property(c, which_prop);
218 * Helper function for ical_ctdl_is_overlap() to simplify the code when
219 * comparing year/month/day. The number doesn't have to be meaningful, only
220 * consistent and unique for the supplied y/m/d combination.
222 inline int itymd(struct icaltimetype t) {
231 * Check to see if two events overlap. Returns nonzero if they do.
233 int ical_ctdl_is_overlap(
234 struct icaltimetype t1start,
235 struct icaltimetype t1end,
236 struct icaltimetype t2start,
237 struct icaltimetype t2end
240 if (icaltime_is_null_time(t1start)) return(0);
241 if (icaltime_is_null_time(t2start)) return(0);
243 /* First, check for all-day events */
244 if (t1start.is_date) {
245 if (!icaltime_compare_date_only(t1start, t2start)) {
248 if (!icaltime_is_null_time(t2end)) {
249 if (!icaltime_compare_date_only(t1start, t2end)) {
255 if (t2start.is_date) {
256 if (!icaltime_compare_date_only(t2start, t1start)) {
259 if (!icaltime_is_null_time(t1end)) {
260 if (!icaltime_compare_date_only(t2start, t1end)) {
266 /* Now check for overlaps using date *and* time. */
268 /* First, bail out if either event 1 or event 2 is missing end time. */
269 if (icaltime_is_null_time(t1end)) return(0);
270 if (icaltime_is_null_time(t2end)) return(0);
272 /* If event 1 ends before event 2 starts, we're in the clear. */
273 if (icaltime_compare(t1end, t2start) <= 0) return(0);
275 /* If event 2 ends before event 1 starts, we're also ok. */
276 if (icaltime_compare(t2end, t1start) <= 0) return(0);
278 /* Otherwise, they overlap. */
285 * Backend for ical_hunt_for_conflicts()
287 void vcard_hunt_for_conflicts_backend(long msgnum, void *data) {
289 struct CtdlMessage *msg;
290 struct ical_respond_data ird;
291 struct icaltimetype t1start, t1end, t2start, t2end;
294 cal = (icalcomponent *)data;
296 msg = CtdlFetchMessage(msgnum);
297 if (msg == NULL) return;
298 memset(&ird, 0, sizeof ird);
299 strcpy(ird.desired_partnum, "1"); /* hopefully it's always 1 */
300 mime_parser(msg->cm_fields['M'],
302 *ical_locate_part, /* callback function */
304 (void *) &ird, /* user data */
307 CtdlFreeMessage(msg);
309 if (ird.cal == NULL) return;
311 t1start = icaltime_null_time();
312 t1end = icaltime_null_time();
313 t2start = icaltime_null_time();
314 t1end = icaltime_null_time();
316 /* Now compare cal to ird.cal */
317 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
318 if (p == NULL) return;
319 if (p != NULL) t2start = icalproperty_get_dtstart(p);
321 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
322 if (p != NULL) t2end = icalproperty_get_dtend(p);
324 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
325 if (p == NULL) return;
326 if (p != NULL) t1start = icalproperty_get_dtstart(p);
328 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
329 if (p != NULL) t1end = icalproperty_get_dtend(p);
331 icalcomponent_free(ird.cal);
333 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
334 cprintf("%ld|FIXME put more here\n",
343 * Phase 2 of "hunt for conflicts" operation.
344 * At this point we have a calendar object which represents the VEVENT that
345 * we're considering adding to the calendar. Now hunt through the user's
346 * calendar room, and output zero or more existing VEVENTs which conflict
349 void ical_hunt_for_conflicts(icalcomponent *cal) {
350 char hold_rm[ROOMNAMELEN];
352 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
354 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
355 getroom(&CC->quickroom, hold_rm);
356 cprintf("%d You do not have a calendar.\n", ERROR);
360 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
362 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
364 vcard_hunt_for_conflicts_backend,
369 getroom(&CC->quickroom, hold_rm); /* return to saved room */
376 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
378 void ical_conflicts(long msgnum, char *partnum) {
379 struct CtdlMessage *msg;
380 struct ical_respond_data ird;
382 msg = CtdlFetchMessage(msgnum);
384 cprintf("%d Message %ld not found.\n",
391 memset(&ird, 0, sizeof ird);
392 strcpy(ird.desired_partnum, partnum);
393 mime_parser(msg->cm_fields['M'],
395 *ical_locate_part, /* callback function */
397 (void *) &ird, /* user data */
401 CtdlFreeMessage(msg);
403 if (ird.cal != NULL) {
404 ical_hunt_for_conflicts(ird.cal);
405 icalcomponent_free(ird.cal);
409 cprintf("%d No calendar object found\n", ERROR);
413 /* should never get here */
420 * All Citadel calendar commands from the client come through here.
422 void cmd_ical(char *argbuf)
429 if (CtdlAccessCheck(ac_logged_in)) return;
431 extract(subcmd, argbuf, 0);
433 if (!strcmp(subcmd, "test")) {
434 cprintf("%d This server supports calendaring\n", CIT_OK);
438 else if (!strcmp(subcmd, "respond")) {
439 msgnum = extract_long(argbuf, 1);
440 extract(partnum, argbuf, 2);
441 extract(action, argbuf, 3);
442 ical_respond(msgnum, partnum, action);
445 else if (!strcmp(subcmd, "conflicts")) {
446 msgnum = extract_long(argbuf, 1);
447 extract(partnum, argbuf, 2);
448 ical_conflicts(msgnum, partnum);
452 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
456 /* should never get here */
459 #endif /* HAVE_ICAL_H */
463 * We don't know if the calendar room exists so we just create it at login
465 void ical_create_room(void)
470 /* Create the calendar room if it doesn't already exist */
471 create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
473 /* Set expiration policy to manual; otherwise objects will be lost! */
474 if (lgetroom(&qr, USERCALENDARROOM)) {
475 lprintf(3, "Couldn't get the user calendar room!\n");
478 qr.QRep.expire_mode = EXPIRE_MANUAL;
481 /* Set the view to a calendar view */
482 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
483 vbuf.v_view = 3; /* 3 = calendar */
484 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
486 /* Create the tasks list room if it doesn't already exist */
487 create_room(USERTASKSROOM, 4, "", 0, 1, 0);
489 /* Set expiration policy to manual; otherwise objects will be lost! */
490 if (lgetroom(&qr, USERTASKSROOM)) {
491 lprintf(3, "Couldn't get the user calendar room!\n");
494 qr.QRep.expire_mode = EXPIRE_MANUAL;
497 /* Set the view to a task list view */
498 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
499 vbuf.v_view = 4; /* 4 = tasks */
500 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
506 /* See if we need to prevent the object from being saved */
507 int ical_obj_beforesave(struct CtdlMessage *msg)
509 char roomname[ROOMNAMELEN];
514 * Only messages with content-type text/calendar or text/x-calendar
515 * may be saved to Calendar>. If the message is bound for
516 * Calendar> but doesn't have this content-type, throw an error
517 * so that the message may not be posted.
520 /* First determine if this is our room */
521 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
522 if (strncmp(roomname, msg->cm_fields['O'], ROOMNAMELEN))
523 return 0; /* It's not us... */
525 /* Then determine content-type of the message */
527 /* It must be an RFC822 message! */
528 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
529 if (msg->cm_format_type != 4)
530 return 1; /* You tried to save a non-RFC822 message! */
532 /* Find the Content-Type: header */
533 p = msg->cm_fields['M'];
536 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
537 if (!strncasecmp(p + 14, "text/x-calendar", 15) ||
538 !strncasecmp(p + 14, "text/calendar", 13))
546 /* Oops! No Content-Type in this message! How'd that happen? */
547 lprintf(7, "RFC822 message with no Content-Type header!\n");
553 /* Register this module with the Citadel server. */
554 char *Dynamic_Module_Init(void)
556 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
558 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
559 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");