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.
10 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
14 #include <sys/types.h>
23 #include "citserver.h"
24 #include "sysdep_decls.h"
27 #include "serv_extensions.h"
32 #include "mime_parser.h"
33 #include "serv_calendar.h"
35 #ifdef CITADEL_WITH_CALENDAR_SERVICE
38 #include "ical_dezonify.h"
40 struct ical_respond_data {
41 char desired_partnum[SIZ];
45 /* Session-local data for calendaring. */
50 * Utility function to encapsulate a subcomponent into a full VCALENDAR
52 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
53 icalcomponent *encaps;
55 /* If we're already looking at a full VCALENDAR component,
56 * don't bother ... just return itself.
58 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
62 /* Encapsulate the VEVENT component into a complete VCALENDAR */
63 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
65 lprintf(3, "Error at %s:%d - could not allocate component!\n",
70 /* Set the Product ID */
71 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
73 /* Set the Version Number */
74 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
76 /* Encapsulate the subcomponent inside */
77 icalcomponent_add_component(encaps, subcomp);
79 /* Convert all timestamps to UTC so we don't have to deal with
80 * stupid VTIMEZONE crap.
82 ical_dezonify(encaps);
84 /* Return the object we just created. */
92 * Write a calendar object into the specified user's calendar room.
96 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
100 icalcomponent *encaps;
102 if (cal == NULL) return;
104 /* If the supplied object is a subcomponent, encapsulate it in
105 * a full VCALENDAR component, and save that instead.
107 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
108 encaps = ical_encapsulate_subcomponent(
109 icalcomponent_new_clone(cal)
111 ical_write_to_cal(u, encaps);
112 icalcomponent_free(encaps);
116 strcpy(temp, tmpnam(NULL));
117 ser = icalcomponent_as_ical_string(cal);
118 if (ser == NULL) return;
120 /* Make a temp file out of it */
121 fp = fopen(temp, "w");
122 if (fp == NULL) return;
123 fwrite(ser, strlen(ser), 1, fp);
126 /* This handy API function does all the work for us.
128 CtdlWriteObject(USERCALENDARROOM, /* which room */
129 "text/calendar", /* MIME type */
130 temp, /* temp file */
133 0, /* don't delete others of this type */
141 * Add a calendar object to the user's calendar
143 * ok because it uses ical_write_to_cal()
145 void ical_add(icalcomponent *cal, int recursion_level) {
149 * The VEVENT subcomponent is the one we're interested in saving.
151 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
153 ical_write_to_cal(&CC->usersupp, cal);
157 /* If the component has subcomponents, recurse through them. */
158 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
160 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
161 /* Recursively process subcomponent */
162 ical_add(c, recursion_level+1);
170 * Send a reply to a meeting invitation.
172 * 'request' is the invitation to reply to.
173 * 'action' is the string "accept" or "decline".
175 * (Sorry about this being more than 80 columns ... there was just
176 * no easy way to break it down sensibly.)
180 void ical_send_a_reply(icalcomponent *request, char *action) {
181 icalcomponent *the_reply = NULL;
182 icalcomponent *vevent = NULL;
183 icalproperty *attendee = NULL;
184 char attendee_string[SIZ];
185 icalproperty *organizer = NULL;
186 char organizer_string[SIZ];
187 icalproperty *summary = NULL;
188 char summary_string[SIZ];
189 icalproperty *me_attend = NULL;
190 struct recptypes *recp = NULL;
191 icalparameter *partstat = NULL;
192 char *serialized_reply = NULL;
193 char *reply_message_text = NULL;
194 struct CtdlMessage *msg = NULL;
195 struct recptypes *valid = NULL;
197 strcpy(organizer_string, "");
198 strcpy(summary_string, "Calendar item");
200 if (request == NULL) {
201 lprintf(3, "ERROR: trying to reply to NULL event?\n");
205 the_reply = icalcomponent_new_clone(request);
206 if (the_reply == NULL) {
207 lprintf(3, "ERROR: cannot clone request\n");
211 /* Change the method from REQUEST to REPLY */
212 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
214 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
215 if (vevent != NULL) {
216 /* Hunt for attendees, removing ones that aren't us.
217 * (Actually, remove them all, cloning our own one so we can
218 * re-insert it later)
220 while (attendee = icalcomponent_get_first_property(vevent,
221 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
223 if (icalproperty_get_attendee(attendee)) {
224 strcpy(attendee_string,
225 icalproperty_get_attendee(attendee) );
226 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
227 strcpy(attendee_string, &attendee_string[7]);
228 striplt(attendee_string);
229 recp = validate_recipients(attendee_string);
231 if (!strcasecmp(recp->recp_local, CC->usersupp.fullname)) {
232 if (me_attend) icalproperty_free(me_attend);
233 me_attend = icalproperty_new_clone(attendee);
240 icalcomponent_remove_property(vevent, attendee);
241 icalproperty_free(attendee);
244 /* We found our own address in the attendee list. */
246 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
247 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
249 if (!strcasecmp(action, "accept")) {
250 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
252 else if (!strcasecmp(action, "decline")) {
253 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
255 else if (!strcasecmp(action, "tentative")) {
256 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
259 if (partstat) icalproperty_add_parameter(me_attend, partstat);
261 /* Now insert it back into the vevent. */
262 icalcomponent_add_property(vevent, me_attend);
265 /* Figure out who to send this thing to */
266 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
267 if (organizer != NULL) {
268 if (icalproperty_get_organizer(organizer)) {
269 strcpy(organizer_string,
270 icalproperty_get_organizer(organizer) );
273 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
274 strcpy(organizer_string, &organizer_string[7]);
275 striplt(organizer_string);
277 strcpy(organizer_string, "");
280 /* Extract the summary string -- we'll use it as the
281 * message subject for the reply
283 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
284 if (summary != NULL) {
285 if (icalproperty_get_summary(summary)) {
286 strcpy(summary_string,
287 icalproperty_get_summary(summary) );
293 /* Now generate the reply message and send it out. */
294 serialized_reply = strdoop(icalcomponent_as_ical_string(the_reply));
295 icalcomponent_free(the_reply); /* don't need this anymore */
296 if (serialized_reply == NULL) return;
298 reply_message_text = mallok(strlen(serialized_reply) + SIZ);
299 if (reply_message_text != NULL) {
300 sprintf(reply_message_text,
301 "Content-type: text/calendar\r\n\r\n%s\r\n",
305 msg = CtdlMakeMessage(&CC->usersupp, organizer_string,
306 CC->quickroom.QRname, 0, FMT_RFC822,
308 summary_string, /* Use summary for subject */
312 valid = validate_recipients(organizer_string);
313 CtdlSubmitMsg(msg, valid, "");
314 CtdlFreeMessage(msg);
317 phree(serialized_reply);
323 * Callback function for mime parser that hunts for calendar content types
324 * and turns them into calendar objects
326 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
327 void *content, char *cbtype, size_t length, char *encoding,
330 struct ical_respond_data *ird = NULL;
332 ird = (struct ical_respond_data *) cbuserdata;
333 if (ird->cal != NULL) {
334 icalcomponent_free(ird->cal);
337 if (strcasecmp(partnum, ird->desired_partnum)) return;
338 ird->cal = icalcomponent_new_from_string(content);
339 if (ird->cal != NULL) {
340 ical_dezonify(ird->cal);
346 * Respond to a meeting request.
348 void ical_respond(long msgnum, char *partnum, char *action) {
349 struct CtdlMessage *msg;
350 struct ical_respond_data ird;
353 (strcasecmp(action, "accept"))
354 && (strcasecmp(action, "decline"))
356 cprintf("%d Action must be 'accept' or 'decline'\n",
357 ERROR + ILLEGAL_VALUE
362 msg = CtdlFetchMessage(msgnum);
364 cprintf("%d Message %ld not found.\n",
371 memset(&ird, 0, sizeof ird);
372 strcpy(ird.desired_partnum, partnum);
373 mime_parser(msg->cm_fields['M'],
375 *ical_locate_part, /* callback function */
377 (void *) &ird, /* user data */
381 /* We're done with the incoming message, because we now have a
382 * calendar object in memory.
384 CtdlFreeMessage(msg);
387 * Here is the real meat of this function. Handle the event.
389 if (ird.cal != NULL) {
390 /* Save this in the user's calendar if necessary */
391 if (!strcasecmp(action, "accept")) {
392 ical_add(ird.cal, 0);
395 /* Send a reply if necessary */
396 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
397 ical_send_a_reply(ird.cal, action);
400 /* Now that we've processed this message, we don't need it
401 * anymore. So delete it.
403 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
405 /* Free the memory we allocated and return a response. */
406 icalcomponent_free(ird.cal);
408 cprintf("%d ok\n", CIT_OK);
412 cprintf("%d No calendar object found\n", ERROR);
416 /* should never get here */
421 * Figure out the UID of the calendar event being referred to in a
422 * REPLY object. This function is recursive.
424 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
425 icalcomponent *subcomponent;
428 /* If this object is a REPLY, then extract the UID. */
429 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
430 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
432 strcpy(uidbuf, icalproperty_get_comment(p));
436 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
437 * UID of the reply; we want the UID of the invitation being replied to.
439 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
440 subcomponent != NULL;
441 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
442 ical_learn_uid_of_reply(uidbuf, subcomponent);
448 * ical_update_my_calendar_with_reply() refers to this callback function; when we
449 * locate the message containing the calendar event we're replying to, this function
450 * gets called. It basically just sticks the message number in a supplied buffer.
452 void ical_hunt_for_event_to_update(long msgnum, void *data) {
455 msgnumptr = (long *) data;
460 struct original_event_container {
465 * Callback function for mime parser that hunts for calendar content types
466 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
467 * to fetch the object being updated)
469 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
470 void *content, char *cbtype, size_t length, char *encoding,
473 struct original_event_container *oec = NULL;
475 if (strcasecmp(cbtype, "text/calendar")) {
478 oec = (struct original_event_container *) cbuserdata;
479 if (oec->c != NULL) {
480 icalcomponent_free(oec->c);
482 oec->c = icalcomponent_new_from_string(content);
487 * Merge updated attendee information from a REPLY into an existing event.
489 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
491 icalproperty *e_attendee, *r_attendee;
493 /* First things first. If we're not looking at a VEVENT component,
494 * recurse through subcomponents until we find one.
496 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
497 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
499 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
500 ical_merge_attendee_reply(c, reply);
505 /* Now do the same thing with the reply.
507 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
508 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
510 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
511 ical_merge_attendee_reply(event, c);
516 /* Clone the reply, because we're going to rip its guts out. */
517 reply = icalcomponent_new_clone(reply);
519 /* At this point we're looking at the correct subcomponents.
520 * Iterate through the attendees looking for a match.
523 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
525 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
527 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
529 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
531 /* Check to see if these two attendees match...
534 icalproperty_get_attendee(e_attendee),
535 icalproperty_get_attendee(r_attendee)
537 /* ...and if they do, remove the attendee from the event
538 * and replace it with the attendee from the reply. (The
539 * reply's copy will have the same address, but an updated
543 icalcomponent_remove_property(event, e_attendee);
545 icalproperty_free(e_attendee);
547 icalcomponent_remove_property(reply, r_attendee);
549 icalcomponent_add_property(event, r_attendee);
552 /* Since we diddled both sets of attendees, we have to start
553 * the iteration over again. This will not create an infinite
554 * loop because we removed the attendee from the reply. (That's
555 * why we cloned the reply, and that's what we mean by "ripping
564 /* Free the *clone* of the reply. */
565 icalcomponent_free(reply);
572 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
573 * calendar event. The object has already been deserialized for us; all
574 * we have to do here is hunt for the event in our calendar, merge in the
575 * updated attendee status, and save it again.
577 * This function returns 0 on success, 1 if the event was not found in the
578 * user's calendar, or 2 if an internal error occurred.
580 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
582 char hold_rm[ROOMNAMELEN];
583 long msgnum_being_replaced = 0;
584 struct CtdlMessage *template = NULL;
585 struct CtdlMessage *msg;
586 struct original_event_container oec;
587 icalcomponent *original_event;
588 char *serialized_event = NULL;
589 char roomname[ROOMNAMELEN];
590 char *message_text = NULL;
592 /* Figure out just what event it is we're dealing with */
593 strcpy(uid, "--==<< InVaLiD uId >>==--");
594 ical_learn_uid_of_reply(uid, cal);
595 lprintf(9, "UID of event being replied to is <%s>\n", uid);
597 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
599 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
600 getroom(&CC->quickroom, hold_rm);
601 lprintf(3, "cannot get user calendar room\n");
606 * Pound through the user's calendar looking for a message with
607 * the Citadel EUID set to the value we're looking for. Since
608 * Citadel always sets the message EUID to the vCalendar UID of
609 * the event, this will work.
611 template = (struct CtdlMessage *)
612 mallok(sizeof(struct CtdlMessage));
613 memset(template, 0, sizeof(struct CtdlMessage));
614 template->cm_fields['E'] = strdoop(uid);
615 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
616 template, ical_hunt_for_event_to_update, &msgnum_being_replaced);
617 CtdlFreeMessage(template);
618 getroom(&CC->quickroom, hold_rm); /* return to saved room */
620 lprintf(9, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
621 if (msgnum_being_replaced == 0) {
622 return(1); /* no calendar event found */
625 /* Now we know the ID of the message containing the event being updated.
626 * We don't actually have to delete it; that'll get taken care of by the
627 * server when we save another event with the same UID. This just gives
628 * us the ability to load the event into memory so we can diddle the
631 msg = CtdlFetchMessage(msgnum_being_replaced);
633 return(2); /* internal error */
636 mime_parser(msg->cm_fields['M'],
638 *ical_locate_original_event, /* callback function */
640 &oec, /* user data */
643 CtdlFreeMessage(msg);
645 original_event = oec.c;
646 if (original_event == NULL) {
647 lprintf(3, "ERROR: Original_component is NULL.\n");
651 /* Merge the attendee's updated status into the event */
652 ical_merge_attendee_reply(original_event, cal);
655 serialized_event = strdoop(icalcomponent_as_ical_string(original_event));
656 icalcomponent_free(original_event); /* Don't need this anymore. */
657 if (serialized_event == NULL) return(2);
659 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
661 message_text = mallok(strlen(serialized_event) + SIZ);
662 if (message_text != NULL) {
663 sprintf(message_text,
664 "Content-type: text/calendar\r\n\r\n%s\r\n",
668 msg = CtdlMakeMessage(&CC->usersupp,
669 "", /* No recipient */
677 CIT_ICAL->avoid_sending_invitations = 1;
678 CtdlSubmitMsg(msg, NULL, roomname);
679 CtdlFreeMessage(msg);
680 CIT_ICAL->avoid_sending_invitations = 0;
683 phree(serialized_event);
689 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
690 * simply extracts the calendar object from the message, deserializes it, and
691 * passes it up to ical_update_my_calendar_with_reply() for processing.
693 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
694 struct CtdlMessage *msg;
695 struct ical_respond_data ird;
699 (strcasecmp(action, "update"))
700 && (strcasecmp(action, "ignore"))
702 cprintf("%d Action must be 'update' or 'ignore'\n",
703 ERROR + ILLEGAL_VALUE
708 msg = CtdlFetchMessage(msgnum);
710 cprintf("%d Message %ld not found.\n",
717 memset(&ird, 0, sizeof ird);
718 strcpy(ird.desired_partnum, partnum);
719 mime_parser(msg->cm_fields['M'],
721 *ical_locate_part, /* callback function */
723 (void *) &ird, /* user data */
727 /* We're done with the incoming message, because we now have a
728 * calendar object in memory.
730 CtdlFreeMessage(msg);
733 * Here is the real meat of this function. Handle the event.
735 if (ird.cal != NULL) {
736 /* Update the user's calendar if necessary */
737 if (!strcasecmp(action, "update")) {
738 ret = ical_update_my_calendar_with_reply(ird.cal);
740 cprintf("%d Your calendar has been updated with this reply.\n",
744 cprintf("%d This event does not exist in your calendar.\n",
745 ERROR + FILE_NOT_FOUND);
748 cprintf("%d An internal error occurred.\n",
749 ERROR + INTERNAL_ERROR);
753 cprintf("%d This reply has been ignored.\n", CIT_OK);
756 /* Now that we've processed this message, we don't need it
757 * anymore. So delete it. FIXME uncomment this when ready!
759 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
761 /* Free the memory we allocated and return a response. */
762 icalcomponent_free(ird.cal);
767 cprintf("%d No calendar object found\n", ERROR);
771 /* should never get here */
776 * Search for a property in both the top level and in a VEVENT subcomponent
778 icalproperty *ical_ctdl_get_subprop(
780 icalproperty_kind which_prop
785 p = icalcomponent_get_first_property(cal, which_prop);
787 c = icalcomponent_get_first_component(cal,
788 ICAL_VEVENT_COMPONENT);
790 p = icalcomponent_get_first_property(c, which_prop);
798 * Check to see if two events overlap. Returns nonzero if they do.
800 int ical_ctdl_is_overlap(
801 struct icaltimetype t1start,
802 struct icaltimetype t1end,
803 struct icaltimetype t2start,
804 struct icaltimetype t2end
807 if (icaltime_is_null_time(t1start)) return(0);
808 if (icaltime_is_null_time(t2start)) return(0);
810 /* First, check for all-day events */
811 if (t1start.is_date) {
812 if (!icaltime_compare_date_only(t1start, t2start)) {
815 if (!icaltime_is_null_time(t2end)) {
816 if (!icaltime_compare_date_only(t1start, t2end)) {
822 if (t2start.is_date) {
823 if (!icaltime_compare_date_only(t2start, t1start)) {
826 if (!icaltime_is_null_time(t1end)) {
827 if (!icaltime_compare_date_only(t2start, t1end)) {
833 /* Now check for overlaps using date *and* time. */
835 /* First, bail out if either event 1 or event 2 is missing end time. */
836 if (icaltime_is_null_time(t1end)) return(0);
837 if (icaltime_is_null_time(t2end)) return(0);
839 /* If event 1 ends before event 2 starts, we're in the clear. */
840 if (icaltime_compare(t1end, t2start) <= 0) return(0);
842 /* If event 2 ends before event 1 starts, we're also ok. */
843 if (icaltime_compare(t2end, t1start) <= 0) return(0);
845 /* Otherwise, they overlap. */
852 * Backend for ical_hunt_for_conflicts()
854 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
856 struct CtdlMessage *msg;
857 struct ical_respond_data ird;
858 struct icaltimetype t1start, t1end, t2start, t2end;
860 char conflict_event_uid[SIZ];
861 char conflict_event_summary[SIZ];
862 char compare_uid[SIZ];
864 cal = (icalcomponent *)data;
865 strcpy(compare_uid, "");
866 strcpy(conflict_event_uid, "");
867 strcpy(conflict_event_summary, "");
869 msg = CtdlFetchMessage(msgnum);
870 if (msg == NULL) return;
871 memset(&ird, 0, sizeof ird);
872 strcpy(ird.desired_partnum, "1"); /* hopefully it's always 1 */
873 mime_parser(msg->cm_fields['M'],
875 *ical_locate_part, /* callback function */
877 (void *) &ird, /* user data */
880 CtdlFreeMessage(msg);
882 if (ird.cal == NULL) return;
884 t1start = icaltime_null_time();
885 t1end = icaltime_null_time();
886 t2start = icaltime_null_time();
887 t1end = icaltime_null_time();
889 /* Now compare cal to ird.cal */
890 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
891 if (p == NULL) return;
892 if (p != NULL) t2start = icalproperty_get_dtstart(p);
894 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
895 if (p != NULL) t2end = icalproperty_get_dtend(p);
897 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
898 if (p == NULL) return;
899 if (p != NULL) t1start = icalproperty_get_dtstart(p);
901 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
902 if (p != NULL) t1end = icalproperty_get_dtend(p);
904 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
906 strcpy(compare_uid, icalproperty_get_comment(p));
909 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
911 strcpy(conflict_event_uid, icalproperty_get_comment(p));
914 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
916 strcpy(conflict_event_summary, icalproperty_get_comment(p));
920 icalcomponent_free(ird.cal);
922 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
923 cprintf("%ld||%s|%s|%d|\n",
926 conflict_event_summary,
927 ( ((strlen(compare_uid)>0)
928 &&(!strcasecmp(compare_uid,
929 conflict_event_uid))) ? 1 : 0
938 * Phase 2 of "hunt for conflicts" operation.
939 * At this point we have a calendar object which represents the VEVENT that
940 * we're considering adding to the calendar. Now hunt through the user's
941 * calendar room, and output zero or more existing VEVENTs which conflict
944 void ical_hunt_for_conflicts(icalcomponent *cal) {
945 char hold_rm[ROOMNAMELEN];
947 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
949 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
950 getroom(&CC->quickroom, hold_rm);
951 cprintf("%d You do not have a calendar.\n", ERROR);
955 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
957 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
959 ical_hunt_for_conflicts_backend,
964 getroom(&CC->quickroom, hold_rm); /* return to saved room */
971 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
973 void ical_conflicts(long msgnum, char *partnum) {
974 struct CtdlMessage *msg;
975 struct ical_respond_data ird;
977 msg = CtdlFetchMessage(msgnum);
979 cprintf("%d Message %ld not found.\n",
986 memset(&ird, 0, sizeof ird);
987 strcpy(ird.desired_partnum, partnum);
988 mime_parser(msg->cm_fields['M'],
990 *ical_locate_part, /* callback function */
992 (void *) &ird, /* user data */
996 CtdlFreeMessage(msg);
998 if (ird.cal != NULL) {
999 ical_hunt_for_conflicts(ird.cal);
1000 icalcomponent_free(ird.cal);
1004 cprintf("%d No calendar object found\n", ERROR);
1008 /* should never get here */
1014 * Remove all properties from a VEVENT that are not supplying the
1015 * bare minimum for free/busy data.
1017 void ical_freebusy_strip(icalcomponent *cal) {
1020 int did_something = 1;
1022 if (cal == NULL) return;
1024 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1025 ical_freebusy_strip(
1026 icalcomponent_get_first_component(
1027 cal, ICAL_VEVENT_COMPONENT
1035 while (did_something) {
1037 for ( p=icalcomponent_get_first_property
1038 (cal, ICAL_ANY_PROPERTY);
1040 p = icalcomponent_get_next_property
1041 (cal, ICAL_ANY_PROPERTY)
1045 (icalproperty_isa(p)==ICAL_DTSTART_PROPERTY)
1046 || (icalproperty_isa(p)==ICAL_DTEND_PROPERTY)
1047 || (icalproperty_isa(p)==ICAL_DURATION_PROPERTY)
1048 || (icalproperty_isa(p)==ICAL_FREEBUSY_PROPERTY)
1049 || (icalproperty_isa(p)==ICAL_TRANSP_PROPERTY)
1055 icalcomponent_remove_property(cal, p);
1056 icalproperty_free(p);
1068 * Backend for ical_freebusy()
1070 * This function simply loads the messages in the user's calendar room,
1071 * which contain VEVENTs, then strips them of all non-freebusy data, and
1072 * adds them to the supplied VCALENDAR.
1075 void ical_freebusy_backend(long msgnum, void *data) {
1077 struct CtdlMessage *msg;
1078 struct ical_respond_data ird;
1080 cal = (icalcomponent *)data;
1082 msg = CtdlFetchMessage(msgnum);
1083 if (msg == NULL) return;
1084 memset(&ird, 0, sizeof ird);
1085 strcpy(ird.desired_partnum, "1"); /* hopefully it's always 1 */
1086 mime_parser(msg->cm_fields['M'],
1088 *ical_locate_part, /* callback function */
1090 (void *) &ird, /* user data */
1093 CtdlFreeMessage(msg);
1095 if (ird.cal == NULL) return;
1097 /* Strip it! Strip it good! */
1098 ical_freebusy_strip(ird.cal);
1100 /* Encapsulate ird.cal inside cal (thereby also transferring
1101 * ownership of the memory it consumes).
1103 icalcomponent_add_component(cal, ird.cal);
1109 * Grab another user's free/busy times
1111 void ical_freebusy(char *who) {
1112 struct usersupp usbuf;
1113 char calendar_room_name[ROOMNAMELEN];
1114 char hold_rm[ROOMNAMELEN];
1115 char *serialized_request = NULL;
1116 icalcomponent *encaps = NULL;
1118 if (getuser(&usbuf, who) != 0) {
1119 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1123 MailboxName(calendar_room_name, sizeof calendar_room_name,
1124 &usbuf, USERCALENDARROOM);
1126 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
1128 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
1129 cprintf("%d Cannot open calendar\n", ERROR+ROOM_NOT_FOUND);
1130 getroom(&CC->quickroom, hold_rm);
1135 /* Create a VCALENDAR in which we will encapsulate all the VEVENTs */
1136 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
1137 if (encaps == NULL) {
1138 cprintf("%d Internal error: cannot allocate memory.\n",
1139 ERROR+INTERNAL_ERROR);
1140 getroom(&CC->quickroom, hold_rm);
1144 /* Set the Product ID */
1145 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1147 /* Set the Version Number */
1148 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1150 /* Set the method to ???? FIXME
1151 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST); */
1153 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
1154 NULL, ical_freebusy_backend, (void *)encaps
1158 serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
1159 icalcomponent_free(encaps); /* Don't need this anymore. */
1161 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1162 if (serialized_request != NULL) {
1163 client_write(serialized_request, strlen(serialized_request));
1167 /* Go back to the room from which we came... */
1168 getroom(&CC->quickroom, hold_rm);
1170 cprintf("%d not implemented yet\n", ERROR);
1177 * All Citadel calendar commands from the client come through here.
1179 void cmd_ical(char *argbuf)
1187 if (CtdlAccessCheck(ac_logged_in)) return;
1189 extract(subcmd, argbuf, 0);
1191 if (!strcmp(subcmd, "test")) {
1192 cprintf("%d This server supports calendaring\n", CIT_OK);
1196 else if (!strcmp(subcmd, "respond")) {
1197 msgnum = extract_long(argbuf, 1);
1198 extract(partnum, argbuf, 2);
1199 extract(action, argbuf, 3);
1200 ical_respond(msgnum, partnum, action);
1203 else if (!strcmp(subcmd, "handle_rsvp")) {
1204 msgnum = extract_long(argbuf, 1);
1205 extract(partnum, argbuf, 2);
1206 extract(action, argbuf, 3);
1207 ical_handle_rsvp(msgnum, partnum, action);
1210 else if (!strcmp(subcmd, "conflicts")) {
1211 msgnum = extract_long(argbuf, 1);
1212 extract(partnum, argbuf, 2);
1213 ical_conflicts(msgnum, partnum);
1216 else if (!strcmp(subcmd, "freebusy")) {
1217 extract(who, argbuf, 1);
1222 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
1226 /* should never get here */
1232 * We don't know if the calendar room exists so we just create it at login
1234 void ical_create_room(void)
1236 struct quickroom qr;
1239 /* Create the calendar room if it doesn't already exist */
1240 create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
1242 /* Set expiration policy to manual; otherwise objects will be lost! */
1243 if (lgetroom(&qr, USERCALENDARROOM)) {
1244 lprintf(3, "Couldn't get the user calendar room!\n");
1247 qr.QRep.expire_mode = EXPIRE_MANUAL;
1248 qr.QRdefaultview = 3; /* 3 = calendar view */
1251 /* Set the view to a calendar view */
1252 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
1253 vbuf.v_view = 3; /* 3 = calendar */
1254 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
1256 /* Create the tasks list room if it doesn't already exist */
1257 create_room(USERTASKSROOM, 4, "", 0, 1, 0);
1259 /* Set expiration policy to manual; otherwise objects will be lost! */
1260 if (lgetroom(&qr, USERTASKSROOM)) {
1261 lprintf(3, "Couldn't get the user calendar room!\n");
1264 qr.QRep.expire_mode = EXPIRE_MANUAL;
1265 qr.QRdefaultview = 4; /* 4 = tasks view */
1268 /* Set the view to a task list view */
1269 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
1270 vbuf.v_view = 4; /* 4 = tasks */
1271 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
1278 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1281 void ical_send_out_invitations(icalcomponent *cal) {
1282 icalcomponent *the_request = NULL;
1283 char *serialized_request = NULL;
1284 icalcomponent *encaps = NULL;
1285 char *request_message_text = NULL;
1286 struct CtdlMessage *msg = NULL;
1287 struct recptypes *valid = NULL;
1288 char attendees_string[SIZ];
1289 int num_attendees = 0;
1290 char this_attendee[SIZ];
1291 icalproperty *attendee = NULL;
1292 char summary_string[SIZ];
1293 icalproperty *summary = NULL;
1296 lprintf(3, "ERROR: trying to reply to NULL event?\n");
1301 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1302 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1303 ical_send_out_invitations(
1304 icalcomponent_get_first_component(
1305 cal, ICAL_VEVENT_COMPONENT
1311 /* Clone the event */
1312 the_request = icalcomponent_new_clone(cal);
1313 if (the_request == NULL) {
1314 lprintf(3, "ERROR: cannot clone calendar object\n");
1318 /* Extract the summary string -- we'll use it as the
1319 * message subject for the request
1321 strcpy(summary_string, "Meeting request");
1322 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1323 if (summary != NULL) {
1324 if (icalproperty_get_summary(summary)) {
1325 strcpy(summary_string,
1326 icalproperty_get_summary(summary) );
1330 /* Determine who the recipients of this message are (the attendees) */
1331 strcpy(attendees_string, "");
1332 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1333 if (icalproperty_get_attendee(attendee)) {
1334 strcpy(this_attendee, icalproperty_get_attendee(attendee) );
1335 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1336 strcpy(this_attendee, &this_attendee[7]);
1337 snprintf(&attendees_string[strlen(attendees_string)],
1338 sizeof(attendees_string) - strlen(attendees_string),
1347 lprintf(9, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1349 /* If there are no attendees, there are no invitations to send, so...
1350 * don't bother putting one together! Punch out, Maverick!
1352 if (num_attendees == 0) {
1353 icalcomponent_free(the_request);
1357 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1358 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
1359 if (encaps == NULL) {
1360 lprintf(3, "Error at %s:%d - could not allocate component!\n",
1361 __FILE__, __LINE__);
1362 icalcomponent_free(the_request);
1366 /* Set the Product ID */
1367 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1369 /* Set the Version Number */
1370 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1372 /* Set the method to REQUEST */
1373 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1375 /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1376 ical_dezonify(the_request);
1378 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1379 * are responsible for "the_request"'s memory -- it will be freed
1380 * when we free "encaps".
1382 icalcomponent_add_component(encaps, the_request);
1385 serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
1386 icalcomponent_free(encaps); /* Don't need this anymore. */
1387 if (serialized_request == NULL) return;
1389 request_message_text = mallok(strlen(serialized_request) + SIZ);
1390 if (request_message_text != NULL) {
1391 sprintf(request_message_text,
1392 "Content-type: text/calendar\r\n\r\n%s\r\n",
1396 msg = CtdlMakeMessage(&CC->usersupp,
1397 "", /* No single recipient here */
1398 CC->quickroom.QRname, 0, FMT_RFC822,
1400 summary_string, /* Use summary for subject */
1401 request_message_text);
1404 valid = validate_recipients(attendees_string);
1405 CtdlSubmitMsg(msg, valid, "");
1406 CtdlFreeMessage(msg);
1409 phree(serialized_request);
1414 * When a calendar object is being saved, determine whether it's a VEVENT
1415 * and the user saving it is the organizer. If so, send out invitations
1416 * to any listed attendees.
1419 void ical_saving_vevent(icalcomponent *cal) {
1421 icalproperty *organizer = NULL;
1422 char organizer_string[SIZ];
1424 /* Don't send out invitations if we've been asked not to. */
1425 lprintf(9, "CIT_ICAL->avoid_sending_invitations = %d\n",
1426 CIT_ICAL->avoid_sending_invitations);
1427 if (CIT_ICAL->avoid_sending_invitations > 0) {
1431 strcpy(organizer_string, "");
1433 * The VEVENT subcomponent is the one we're interested in.
1434 * Send out invitations if, and only if, this user is the Organizer.
1436 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1437 organizer = icalcomponent_get_first_property(cal,
1438 ICAL_ORGANIZER_PROPERTY);
1439 if (organizer != NULL) {
1440 if (icalproperty_get_organizer(organizer)) {
1441 strcpy(organizer_string,
1442 icalproperty_get_organizer(organizer));
1445 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1446 strcpy(organizer_string, &organizer_string[7]);
1447 striplt(organizer_string);
1449 * If the user saving the event is listed as the
1450 * organizer, then send out invitations.
1452 if (CtdlIsMe(organizer_string)) {
1453 ical_send_out_invitations(cal);
1458 /* If the component has subcomponents, recurse through them. */
1459 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1461 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1462 /* Recursively process subcomponent */
1463 ical_saving_vevent(c);
1471 * Back end for ical_obj_beforesave()
1472 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1473 * the summary of the event (becomes message subject),
1474 * and the start time (becomes message date/time).
1476 void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
1477 char *disp, void *content, char *cbtype, size_t length,
1478 char *encoding, void *cbuserdata)
1482 struct icalmessagemod *imm;
1484 imm = (struct icalmessagemod *)cbuserdata;
1486 /* If this is a text/calendar object, hunt for the UID and drop it in
1487 * the "user data" pointer for the MIME parser. When
1488 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
1491 if (!strcasecmp(cbtype, "text/calendar")) {
1492 cal = icalcomponent_new_from_string(content);
1494 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1495 cal = icalcomponent_get_first_component(
1496 cal, ICAL_VEVENT_COMPONENT
1501 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1503 strcpy(imm->uid, icalproperty_get_comment(p));
1505 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
1507 strcpy(imm->subject,
1508 icalproperty_get_comment(p));
1510 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
1512 imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
1514 icalcomponent_free(cal);
1524 * See if we need to prevent the object from being saved (we don't allow
1525 * MIME types other than text/calendar in the Calendar> room). Also, when
1526 * saving an event to the calendar, set the message's Citadel extended message
1527 * ID to the UID of the object. This causes our replication checker to
1528 * automatically delete any existing instances of the same object. (Isn't
1531 * We also set the message's Subject to the event summary, and the Date/time to
1532 * the event start time.
1534 int ical_obj_beforesave(struct CtdlMessage *msg)
1536 char roomname[ROOMNAMELEN];
1539 struct icalmessagemod imm;
1542 * Only messages with content-type text/calendar
1543 * may be saved to Calendar>. If the message is bound for
1544 * Calendar> but doesn't have this content-type, throw an error
1545 * so that the message may not be posted.
1548 /* First determine if this is our room */
1549 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
1550 if (strcasecmp(roomname, CC->quickroom.QRname)) {
1551 return 0; /* It's not the Calendar room. */
1554 /* Then determine content-type of the message */
1556 /* It must be an RFC822 message! */
1557 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1558 if (msg->cm_format_type != 4)
1559 return 1; /* You tried to save a non-RFC822 message! */
1561 /* Find the Content-Type: header */
1562 p = msg->cm_fields['M'];
1565 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1566 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1567 memset(&imm, 0, sizeof(struct icalmessagemod));
1568 mime_parser(msg->cm_fields['M'],
1570 *ical_ctdl_set_extended_msgid,
1575 if (strlen(imm.uid) > 0) {
1576 if (msg->cm_fields['E'] != NULL) {
1577 phree(msg->cm_fields['E']);
1579 msg->cm_fields['E'] = strdoop(imm.uid);
1581 if (strlen(imm.subject) > 0) {
1582 if (msg->cm_fields['U'] != NULL) {
1583 phree(msg->cm_fields['U']);
1585 msg->cm_fields['U'] = strdoop(imm.subject);
1587 if (imm.dtstart > 0) {
1588 if (msg->cm_fields['T'] != NULL) {
1589 phree(msg->cm_fields['T']);
1591 msg->cm_fields['T'] = strdoop("000000000000000000");
1592 sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
1603 /* Oops! No Content-Type in this message! How'd that happen? */
1604 lprintf(7, "RFC822 message with no Content-Type header!\n");
1610 * Things we need to do after saving a calendar event.
1612 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
1613 char *disp, void *content, char *cbtype, size_t length,
1614 char *encoding, void *cbuserdata)
1618 /* If this is a text/calendar object, hunt for the UID and drop it in
1619 * the "user data" pointer for the MIME parser. When
1620 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
1623 if (!strcasecmp(cbtype, "text/calendar")) {
1624 cal = icalcomponent_new_from_string(content);
1626 ical_saving_vevent(cal);
1627 icalcomponent_free(cal);
1634 * Things we need to do after saving a calendar event.
1636 int ical_obj_aftersave(struct CtdlMessage *msg)
1638 char roomname[ROOMNAMELEN];
1643 * If this isn't the Calendar> room, no further action is necessary.
1646 /* First determine if this is our room */
1647 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
1648 if (strcasecmp(roomname, CC->quickroom.QRname)) {
1649 return 0; /* It's not the Calendar room. */
1652 /* Then determine content-type of the message */
1654 /* It must be an RFC822 message! */
1655 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1656 if (msg->cm_format_type != 4) return(1);
1658 /* Find the Content-Type: header */
1659 p = msg->cm_fields['M'];
1662 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1663 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1664 mime_parser(msg->cm_fields['M'],
1666 *ical_obj_aftersave_backend,
1680 /* Oops! No Content-Type in this message! How'd that happen? */
1681 lprintf(7, "RFC822 message with no Content-Type header!\n");
1686 void ical_session_startup(void) {
1687 CtdlAllocUserData(SYM_CIT_ICAL, sizeof(struct cit_ical));
1688 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
1692 #endif /* CITADEL_WITH_CALENDAR_SERVICE */
1695 * Register this module with the Citadel server.
1697 char *serv_calendar_init(void)
1699 #ifdef CITADEL_WITH_CALENDAR_SERVICE
1700 SYM_CIT_ICAL = CtdlGetDynamicSymbol();
1701 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
1702 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
1703 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
1704 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
1705 CtdlRegisterSessionHook(ical_session_startup, EVT_START);