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"
38 struct ical_respond_data {
39 char desired_partnum[SIZ];
45 * Write our config to disk
47 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
52 strcpy(temp, tmpnam(NULL));
53 ser = icalcomponent_as_ical_string(cal);
54 if (ser == NULL) return;
56 /* Make a temp file out of it */
57 fp = fopen(temp, "w");
58 if (fp == NULL) return;
59 fwrite(ser, strlen(ser), 1, fp);
62 /* This handy API function does all the work for us.
64 CtdlWriteObject(USERCALENDARROOM, /* which room */
65 "text/calendar", /* MIME type */
69 0, /* don't delete others of this type */
77 * Add a calendar object to the user's calendar
79 void ical_add(icalcomponent *cal, int recursion_level) {
83 * The VEVENT subcomponent is the one we're interested in saving.
85 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
87 ical_write_to_cal(&CC->usersupp, cal);
91 /* If the component has subcomponents, recurse through them. */
92 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
94 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
95 /* Recursively process subcomponent */
96 ical_add(c, recursion_level+1);
104 * Callback function for mime parser that hunts for calendar content types
105 * and turns them into calendar objects
107 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
108 void *content, char *cbtype, size_t length, char *encoding,
111 struct ical_respond_data *ird = NULL;
113 ird = (struct ical_respond_data *) cbuserdata;
114 if (ird->cal != NULL) {
115 icalcomponent_free(ird->cal);
118 if (strcasecmp(partnum, ird->desired_partnum)) return;
119 ird->cal = icalcomponent_new_from_string(content);
124 * Respond to a meeting request.
126 void ical_respond(long msgnum, char *partnum, char *action) {
127 struct CtdlMessage *msg;
128 struct ical_respond_data ird;
131 (strcasecmp(action, "accept"))
132 && (strcasecmp(action, "decline"))
134 cprintf("%d Action must be 'accept' or 'decline'\n",
135 ERROR + ILLEGAL_VALUE
140 msg = CtdlFetchMessage(msgnum);
142 cprintf("%d Message %ld not found.\n",
149 memset(&ird, 0, sizeof ird);
150 strcpy(ird.desired_partnum, partnum);
151 mime_parser(msg->cm_fields['M'],
153 *ical_locate_part, /* callback function */
155 (void *) &ird, /* user data */
159 CtdlFreeMessage(msg);
161 if (ird.cal != NULL) {
162 /* Save this in the user's calendar if necessary */
163 if (!strcasecmp(action, "accept")) {
164 ical_add(ird.cal, 0);
167 /* Send a reply if necessary */
168 /* FIXME ... do this */
170 /* Delete the message from the inbox */
171 /* FIXME ... do this */
173 icalcomponent_free(ird.cal);
175 cprintf("%d ok\n", CIT_OK);
179 cprintf("%d No calendar object found\n", ERROR);
183 /* should never get here */
188 * Search for a property in both the top level and in a VEVENT subcomponent
190 icalproperty *ical_ctdl_get_subprop(
192 icalproperty_kind which_prop
197 p = icalcomponent_get_first_property(cal, which_prop);
199 c = icalcomponent_get_first_component(cal,
200 ICAL_VEVENT_COMPONENT);
202 p = icalcomponent_get_first_property(c, which_prop);
210 * Check to see if two events overlap. Returns nonzero if they do.
212 int ical_ctdl_is_overlap(
213 struct icaltimetype t1start,
214 struct icaltimetype t1end,
215 struct icaltimetype t2start,
216 struct icaltimetype t2end
219 if (icaltime_is_null_time(t1start)) return(0);
220 if (icaltime_is_null_time(t2start)) return(0);
222 /* First, check for all-day events */
223 if (t1start.is_date) {
224 if (!icaltime_compare_date_only(t1start, t2start)) {
227 if (!icaltime_is_null_time(t2end)) {
228 if (!icaltime_compare_date_only(t1start, t2end)) {
234 if (t2start.is_date) {
235 if (!icaltime_compare_date_only(t2start, t1start)) {
238 if (!icaltime_is_null_time(t1end)) {
239 if (!icaltime_compare_date_only(t2start, t1end)) {
245 /* Now check for overlaps using date *and* time. */
247 /* First, bail out if either event 1 or event 2 is missing end time. */
248 if (icaltime_is_null_time(t1end)) return(0);
249 if (icaltime_is_null_time(t2end)) return(0);
251 /* If event 1 ends before event 2 starts, we're in the clear. */
252 if (icaltime_compare(t1end, t2start) <= 0) return(0);
254 /* If event 2 ends before event 1 starts, we're also ok. */
255 if (icaltime_compare(t2end, t1start) <= 0) return(0);
257 /* Otherwise, they overlap. */
264 * Backend for ical_hunt_for_conflicts()
266 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
268 struct CtdlMessage *msg;
269 struct ical_respond_data ird;
270 struct icaltimetype t1start, t1end, t2start, t2end;
272 char conflict_event_uid[SIZ];
273 char conflict_event_summary[SIZ];
275 cal = (icalcomponent *)data;
276 strcpy(conflict_event_uid, "");
277 strcpy(conflict_event_summary, "");
279 msg = CtdlFetchMessage(msgnum);
280 if (msg == NULL) return;
281 memset(&ird, 0, sizeof ird);
282 strcpy(ird.desired_partnum, "1"); /* hopefully it's always 1 */
283 mime_parser(msg->cm_fields['M'],
285 *ical_locate_part, /* callback function */
287 (void *) &ird, /* user data */
290 CtdlFreeMessage(msg);
292 if (ird.cal == NULL) return;
294 t1start = icaltime_null_time();
295 t1end = icaltime_null_time();
296 t2start = icaltime_null_time();
297 t1end = icaltime_null_time();
299 /* Now compare cal to ird.cal */
300 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
301 if (p == NULL) return;
302 if (p != NULL) t2start = icalproperty_get_dtstart(p);
304 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
305 if (p != NULL) t2end = icalproperty_get_dtend(p);
307 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
308 if (p == NULL) return;
309 if (p != NULL) t1start = icalproperty_get_dtstart(p);
311 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
312 if (p != NULL) t1end = icalproperty_get_dtend(p);
314 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
316 strcpy(conflict_event_uid, icalproperty_get_comment(p));
318 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
320 strcpy(conflict_event_summary, icalproperty_get_comment(p));
324 icalcomponent_free(ird.cal);
326 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
327 cprintf("%ld||%s|%s|\n",
330 conflict_event_summary
338 * Phase 2 of "hunt for conflicts" operation.
339 * At this point we have a calendar object which represents the VEVENT that
340 * we're considering adding to the calendar. Now hunt through the user's
341 * calendar room, and output zero or more existing VEVENTs which conflict
344 void ical_hunt_for_conflicts(icalcomponent *cal) {
345 char hold_rm[ROOMNAMELEN];
347 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
349 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
350 getroom(&CC->quickroom, hold_rm);
351 cprintf("%d You do not have a calendar.\n", ERROR);
355 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
357 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
359 ical_hunt_for_conflicts_backend,
364 getroom(&CC->quickroom, hold_rm); /* return to saved room */
371 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
373 void ical_conflicts(long msgnum, char *partnum) {
374 struct CtdlMessage *msg;
375 struct ical_respond_data ird;
377 msg = CtdlFetchMessage(msgnum);
379 cprintf("%d Message %ld not found.\n",
386 memset(&ird, 0, sizeof ird);
387 strcpy(ird.desired_partnum, partnum);
388 mime_parser(msg->cm_fields['M'],
390 *ical_locate_part, /* callback function */
392 (void *) &ird, /* user data */
396 CtdlFreeMessage(msg);
398 if (ird.cal != NULL) {
399 ical_hunt_for_conflicts(ird.cal);
400 icalcomponent_free(ird.cal);
404 cprintf("%d No calendar object found\n", ERROR);
408 /* should never get here */
415 * All Citadel calendar commands from the client come through here.
417 void cmd_ical(char *argbuf)
424 if (CtdlAccessCheck(ac_logged_in)) return;
426 extract(subcmd, argbuf, 0);
428 if (!strcmp(subcmd, "test")) {
429 cprintf("%d This server supports calendaring\n", CIT_OK);
433 else if (!strcmp(subcmd, "respond")) {
434 msgnum = extract_long(argbuf, 1);
435 extract(partnum, argbuf, 2);
436 extract(action, argbuf, 3);
437 ical_respond(msgnum, partnum, action);
440 else if (!strcmp(subcmd, "conflicts")) {
441 msgnum = extract_long(argbuf, 1);
442 extract(partnum, argbuf, 2);
443 ical_conflicts(msgnum, partnum);
447 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
451 /* should never get here */
457 * We don't know if the calendar room exists so we just create it at login
459 void ical_create_room(void)
464 /* Create the calendar room if it doesn't already exist */
465 create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
467 /* Set expiration policy to manual; otherwise objects will be lost! */
468 if (lgetroom(&qr, USERCALENDARROOM)) {
469 lprintf(3, "Couldn't get the user calendar room!\n");
472 qr.QRep.expire_mode = EXPIRE_MANUAL;
475 /* Set the view to a calendar view */
476 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
477 vbuf.v_view = 3; /* 3 = calendar */
478 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
480 /* Create the tasks list room if it doesn't already exist */
481 create_room(USERTASKSROOM, 4, "", 0, 1, 0);
483 /* Set expiration policy to manual; otherwise objects will be lost! */
484 if (lgetroom(&qr, USERTASKSROOM)) {
485 lprintf(3, "Couldn't get the user calendar room!\n");
488 qr.QRep.expire_mode = EXPIRE_MANUAL;
491 /* Set the view to a task list view */
492 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
493 vbuf.v_view = 4; /* 4 = tasks */
494 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
502 * Back end for ical_obj_beforesave()
503 * This hunts for the UID of the calendar event.
505 void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
506 char *disp, void *content, char *cbtype, size_t length,
507 char *encoding, void *cbuserdata)
512 /* If this is a text/calendar object, hunt for the UID and drop it in
513 * the "user data" pointer for the MIME parser. When
514 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
517 if (!strcasecmp(cbtype, "text/calendar")) {
518 cal = icalcomponent_new_from_string(content);
520 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
522 strcpy((char *)cbuserdata,
523 icalproperty_get_comment(p)
526 icalcomponent_free(cal);
536 * See if we need to prevent the object from being saved (we don't allow
537 * MIME types other than text/calendar in the Calendar> room). Also, when
538 * saving an event to the calendar, set the message's Citadel extended message
539 * ID to the UID of the object. This causes our replication checker to
540 * automatically delete any existing instances of the same object. (Isn't
543 int ical_obj_beforesave(struct CtdlMessage *msg)
545 char roomname[ROOMNAMELEN];
551 * Only messages with content-type text/calendar
552 * may be saved to Calendar>. If the message is bound for
553 * Calendar> but doesn't have this content-type, throw an error
554 * so that the message may not be posted.
557 /* First determine if this is our room */
558 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
559 if (strncmp(roomname, msg->cm_fields['O'], ROOMNAMELEN))
560 return 0; /* It's not us... */
562 /* Then determine content-type of the message */
564 /* It must be an RFC822 message! */
565 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
566 if (msg->cm_format_type != 4)
567 return 1; /* You tried to save a non-RFC822 message! */
569 /* Find the Content-Type: header */
570 p = msg->cm_fields['M'];
573 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
574 if (!strncasecmp(p + 14, "text/calendar", 15)) {
576 mime_parser(msg->cm_fields['M'],
578 *ical_ctdl_set_extended_msgid,
583 if (strlen(eidbuf) > 0) {
584 if (msg->cm_fields['E'] != NULL) {
585 phree(msg->cm_fields['E']);
587 msg->cm_fields['E'] = strdoop(eidbuf);
598 /* Oops! No Content-Type in this message! How'd that happen? */
599 lprintf(7, "RFC822 message with no Content-Type header!\n");
604 #endif /* HAVE_ICAL_H */
607 * Register this module with the Citadel server.
609 char *Dynamic_Module_Init(void)
612 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
613 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
614 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");