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 create a new VCALENDAR component with some of the
51 * required fields already set the way we like them.
53 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
54 icalcomponent *encaps;
56 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
58 lprintf(3, "Error at %s:%d - could not allocate component!\n",
63 /* Set the Product ID */
64 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
66 /* Set the Version Number */
67 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
72 * Utility function to encapsulate a subcomponent into a full VCALENDAR
74 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
75 icalcomponent *encaps;
77 /* If we're already looking at a full VCALENDAR component,
78 * don't bother ... just return itself.
80 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
84 /* Encapsulate the VEVENT component into a complete VCALENDAR */
85 encaps = icalcomponent_new_citadel_vcalendar();
86 if (encaps == NULL) return NULL;
88 /* Encapsulate the subcomponent inside */
89 icalcomponent_add_component(encaps, subcomp);
91 /* Convert all timestamps to UTC so we don't have to deal with
92 * stupid VTIMEZONE crap.
94 ical_dezonify(encaps);
96 /* Return the object we just created. */
104 * Write a calendar object into the specified user's calendar room.
108 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
112 icalcomponent *encaps;
114 if (cal == NULL) return;
116 /* If the supplied object is a subcomponent, encapsulate it in
117 * a full VCALENDAR component, and save that instead.
119 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
120 encaps = ical_encapsulate_subcomponent(
121 icalcomponent_new_clone(cal)
123 ical_write_to_cal(u, encaps);
124 icalcomponent_free(encaps);
128 strcpy(temp, tmpnam(NULL));
129 ser = icalcomponent_as_ical_string(cal);
130 if (ser == NULL) return;
132 /* Make a temp file out of it */
133 fp = fopen(temp, "w");
134 if (fp == NULL) return;
135 fwrite(ser, strlen(ser), 1, fp);
138 /* This handy API function does all the work for us.
140 CtdlWriteObject(USERCALENDARROOM, /* which room */
141 "text/calendar", /* MIME type */
142 temp, /* temp file */
145 0, /* don't delete others of this type */
153 * Add a calendar object to the user's calendar
155 * ok because it uses ical_write_to_cal()
157 void ical_add(icalcomponent *cal, int recursion_level) {
161 * The VEVENT subcomponent is the one we're interested in saving.
163 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
165 ical_write_to_cal(&CC->usersupp, cal);
169 /* If the component has subcomponents, recurse through them. */
170 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
172 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
173 /* Recursively process subcomponent */
174 ical_add(c, recursion_level+1);
182 * Send a reply to a meeting invitation.
184 * 'request' is the invitation to reply to.
185 * 'action' is the string "accept" or "decline".
187 * (Sorry about this being more than 80 columns ... there was just
188 * no easy way to break it down sensibly.)
192 void ical_send_a_reply(icalcomponent *request, char *action) {
193 icalcomponent *the_reply = NULL;
194 icalcomponent *vevent = NULL;
195 icalproperty *attendee = NULL;
196 char attendee_string[SIZ];
197 icalproperty *organizer = NULL;
198 char organizer_string[SIZ];
199 icalproperty *summary = NULL;
200 char summary_string[SIZ];
201 icalproperty *me_attend = NULL;
202 struct recptypes *recp = NULL;
203 icalparameter *partstat = NULL;
204 char *serialized_reply = NULL;
205 char *reply_message_text = NULL;
206 struct CtdlMessage *msg = NULL;
207 struct recptypes *valid = NULL;
209 strcpy(organizer_string, "");
210 strcpy(summary_string, "Calendar item");
212 if (request == NULL) {
213 lprintf(3, "ERROR: trying to reply to NULL event?\n");
217 the_reply = icalcomponent_new_clone(request);
218 if (the_reply == NULL) {
219 lprintf(3, "ERROR: cannot clone request\n");
223 /* Change the method from REQUEST to REPLY */
224 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
226 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
227 if (vevent != NULL) {
228 /* Hunt for attendees, removing ones that aren't us.
229 * (Actually, remove them all, cloning our own one so we can
230 * re-insert it later)
232 while (attendee = icalcomponent_get_first_property(vevent,
233 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
235 if (icalproperty_get_attendee(attendee)) {
236 strcpy(attendee_string,
237 icalproperty_get_attendee(attendee) );
238 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
239 strcpy(attendee_string, &attendee_string[7]);
240 striplt(attendee_string);
241 recp = validate_recipients(attendee_string);
243 if (!strcasecmp(recp->recp_local, CC->usersupp.fullname)) {
244 if (me_attend) icalproperty_free(me_attend);
245 me_attend = icalproperty_new_clone(attendee);
252 icalcomponent_remove_property(vevent, attendee);
253 icalproperty_free(attendee);
256 /* We found our own address in the attendee list. */
258 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
259 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
261 if (!strcasecmp(action, "accept")) {
262 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
264 else if (!strcasecmp(action, "decline")) {
265 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
267 else if (!strcasecmp(action, "tentative")) {
268 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
271 if (partstat) icalproperty_add_parameter(me_attend, partstat);
273 /* Now insert it back into the vevent. */
274 icalcomponent_add_property(vevent, me_attend);
277 /* Figure out who to send this thing to */
278 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
279 if (organizer != NULL) {
280 if (icalproperty_get_organizer(organizer)) {
281 strcpy(organizer_string,
282 icalproperty_get_organizer(organizer) );
285 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
286 strcpy(organizer_string, &organizer_string[7]);
287 striplt(organizer_string);
289 strcpy(organizer_string, "");
292 /* Extract the summary string -- we'll use it as the
293 * message subject for the reply
295 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
296 if (summary != NULL) {
297 if (icalproperty_get_summary(summary)) {
298 strcpy(summary_string,
299 icalproperty_get_summary(summary) );
305 /* Now generate the reply message and send it out. */
306 serialized_reply = strdoop(icalcomponent_as_ical_string(the_reply));
307 icalcomponent_free(the_reply); /* don't need this anymore */
308 if (serialized_reply == NULL) return;
310 reply_message_text = mallok(strlen(serialized_reply) + SIZ);
311 if (reply_message_text != NULL) {
312 sprintf(reply_message_text,
313 "Content-type: text/calendar\r\n\r\n%s\r\n",
317 msg = CtdlMakeMessage(&CC->usersupp, organizer_string,
318 CC->quickroom.QRname, 0, FMT_RFC822,
320 summary_string, /* Use summary for subject */
324 valid = validate_recipients(organizer_string);
325 CtdlSubmitMsg(msg, valid, "");
326 CtdlFreeMessage(msg);
329 phree(serialized_reply);
335 * Callback function for mime parser that hunts for calendar content types
336 * and turns them into calendar objects
338 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
339 void *content, char *cbtype, size_t length, char *encoding,
342 struct ical_respond_data *ird = NULL;
344 ird = (struct ical_respond_data *) cbuserdata;
346 /* desired_partnum can be set to "_HUNT_" to have it just look for
347 * the first part with a content type of text/calendar. Otherwise
348 * we have to only process the right one.
350 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
351 if (strcasecmp(partnum, ird->desired_partnum)) {
356 if (strcasecmp(cbtype, "text/calendar")) {
360 if (ird->cal != NULL) {
361 icalcomponent_free(ird->cal);
365 ird->cal = icalcomponent_new_from_string(content);
366 if (ird->cal != NULL) {
367 ical_dezonify(ird->cal);
373 * Respond to a meeting request.
375 void ical_respond(long msgnum, char *partnum, char *action) {
376 struct CtdlMessage *msg;
377 struct ical_respond_data ird;
380 (strcasecmp(action, "accept"))
381 && (strcasecmp(action, "decline"))
383 cprintf("%d Action must be 'accept' or 'decline'\n",
384 ERROR + ILLEGAL_VALUE
389 msg = CtdlFetchMessage(msgnum);
391 cprintf("%d Message %ld not found.\n",
398 memset(&ird, 0, sizeof ird);
399 strcpy(ird.desired_partnum, partnum);
400 mime_parser(msg->cm_fields['M'],
402 *ical_locate_part, /* callback function */
404 (void *) &ird, /* user data */
408 /* We're done with the incoming message, because we now have a
409 * calendar object in memory.
411 CtdlFreeMessage(msg);
414 * Here is the real meat of this function. Handle the event.
416 if (ird.cal != NULL) {
417 /* Save this in the user's calendar if necessary */
418 if (!strcasecmp(action, "accept")) {
419 ical_add(ird.cal, 0);
422 /* Send a reply if necessary */
423 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
424 ical_send_a_reply(ird.cal, action);
427 /* Now that we've processed this message, we don't need it
428 * anymore. So delete it.
430 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
432 /* Free the memory we allocated and return a response. */
433 icalcomponent_free(ird.cal);
435 cprintf("%d ok\n", CIT_OK);
439 cprintf("%d No calendar object found\n", ERROR);
443 /* should never get here */
448 * Figure out the UID of the calendar event being referred to in a
449 * REPLY object. This function is recursive.
451 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
452 icalcomponent *subcomponent;
455 /* If this object is a REPLY, then extract the UID. */
456 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
457 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
459 strcpy(uidbuf, icalproperty_get_comment(p));
463 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
464 * UID of the reply; we want the UID of the invitation being replied to.
466 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
467 subcomponent != NULL;
468 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
469 ical_learn_uid_of_reply(uidbuf, subcomponent);
475 * ical_update_my_calendar_with_reply() refers to this callback function; when we
476 * locate the message containing the calendar event we're replying to, this function
477 * gets called. It basically just sticks the message number in a supplied buffer.
479 void ical_hunt_for_event_to_update(long msgnum, void *data) {
482 msgnumptr = (long *) data;
487 struct original_event_container {
492 * Callback function for mime parser that hunts for calendar content types
493 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
494 * to fetch the object being updated)
496 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
497 void *content, char *cbtype, size_t length, char *encoding,
500 struct original_event_container *oec = NULL;
502 if (strcasecmp(cbtype, "text/calendar")) {
505 oec = (struct original_event_container *) cbuserdata;
506 if (oec->c != NULL) {
507 icalcomponent_free(oec->c);
509 oec->c = icalcomponent_new_from_string(content);
514 * Merge updated attendee information from a REPLY into an existing event.
516 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
518 icalproperty *e_attendee, *r_attendee;
520 /* First things first. If we're not looking at a VEVENT component,
521 * recurse through subcomponents until we find one.
523 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
524 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
526 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
527 ical_merge_attendee_reply(c, reply);
532 /* Now do the same thing with the reply.
534 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
535 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
537 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
538 ical_merge_attendee_reply(event, c);
543 /* Clone the reply, because we're going to rip its guts out. */
544 reply = icalcomponent_new_clone(reply);
546 /* At this point we're looking at the correct subcomponents.
547 * Iterate through the attendees looking for a match.
550 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
552 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
554 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
556 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
558 /* Check to see if these two attendees match...
561 icalproperty_get_attendee(e_attendee),
562 icalproperty_get_attendee(r_attendee)
564 /* ...and if they do, remove the attendee from the event
565 * and replace it with the attendee from the reply. (The
566 * reply's copy will have the same address, but an updated
570 icalcomponent_remove_property(event, e_attendee);
572 icalproperty_free(e_attendee);
574 icalcomponent_remove_property(reply, r_attendee);
576 icalcomponent_add_property(event, r_attendee);
579 /* Since we diddled both sets of attendees, we have to start
580 * the iteration over again. This will not create an infinite
581 * loop because we removed the attendee from the reply. (That's
582 * why we cloned the reply, and that's what we mean by "ripping
591 /* Free the *clone* of the reply. */
592 icalcomponent_free(reply);
599 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
600 * calendar event. The object has already been deserialized for us; all
601 * we have to do here is hunt for the event in our calendar, merge in the
602 * updated attendee status, and save it again.
604 * This function returns 0 on success, 1 if the event was not found in the
605 * user's calendar, or 2 if an internal error occurred.
607 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
609 char hold_rm[ROOMNAMELEN];
610 long msgnum_being_replaced = 0;
611 struct CtdlMessage *template = NULL;
612 struct CtdlMessage *msg;
613 struct original_event_container oec;
614 icalcomponent *original_event;
615 char *serialized_event = NULL;
616 char roomname[ROOMNAMELEN];
617 char *message_text = NULL;
619 /* Figure out just what event it is we're dealing with */
620 strcpy(uid, "--==<< InVaLiD uId >>==--");
621 ical_learn_uid_of_reply(uid, cal);
622 lprintf(9, "UID of event being replied to is <%s>\n", uid);
624 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
626 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
627 getroom(&CC->quickroom, hold_rm);
628 lprintf(3, "cannot get user calendar room\n");
633 * Pound through the user's calendar looking for a message with
634 * the Citadel EUID set to the value we're looking for. Since
635 * Citadel always sets the message EUID to the vCalendar UID of
636 * the event, this will work.
638 template = (struct CtdlMessage *)
639 mallok(sizeof(struct CtdlMessage));
640 memset(template, 0, sizeof(struct CtdlMessage));
641 template->cm_fields['E'] = strdoop(uid);
642 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
643 template, ical_hunt_for_event_to_update, &msgnum_being_replaced);
644 CtdlFreeMessage(template);
645 getroom(&CC->quickroom, hold_rm); /* return to saved room */
647 lprintf(9, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
648 if (msgnum_being_replaced == 0) {
649 return(1); /* no calendar event found */
652 /* Now we know the ID of the message containing the event being updated.
653 * We don't actually have to delete it; that'll get taken care of by the
654 * server when we save another event with the same UID. This just gives
655 * us the ability to load the event into memory so we can diddle the
658 msg = CtdlFetchMessage(msgnum_being_replaced);
660 return(2); /* internal error */
663 mime_parser(msg->cm_fields['M'],
665 *ical_locate_original_event, /* callback function */
667 &oec, /* user data */
670 CtdlFreeMessage(msg);
672 original_event = oec.c;
673 if (original_event == NULL) {
674 lprintf(3, "ERROR: Original_component is NULL.\n");
678 /* Merge the attendee's updated status into the event */
679 ical_merge_attendee_reply(original_event, cal);
682 serialized_event = strdoop(icalcomponent_as_ical_string(original_event));
683 icalcomponent_free(original_event); /* Don't need this anymore. */
684 if (serialized_event == NULL) return(2);
686 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
688 message_text = mallok(strlen(serialized_event) + SIZ);
689 if (message_text != NULL) {
690 sprintf(message_text,
691 "Content-type: text/calendar\r\n\r\n%s\r\n",
695 msg = CtdlMakeMessage(&CC->usersupp,
696 "", /* No recipient */
704 CIT_ICAL->avoid_sending_invitations = 1;
705 CtdlSubmitMsg(msg, NULL, roomname);
706 CtdlFreeMessage(msg);
707 CIT_ICAL->avoid_sending_invitations = 0;
710 phree(serialized_event);
716 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
717 * simply extracts the calendar object from the message, deserializes it, and
718 * passes it up to ical_update_my_calendar_with_reply() for processing.
720 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
721 struct CtdlMessage *msg;
722 struct ical_respond_data ird;
726 (strcasecmp(action, "update"))
727 && (strcasecmp(action, "ignore"))
729 cprintf("%d Action must be 'update' or 'ignore'\n",
730 ERROR + ILLEGAL_VALUE
735 msg = CtdlFetchMessage(msgnum);
737 cprintf("%d Message %ld not found.\n",
744 memset(&ird, 0, sizeof ird);
745 strcpy(ird.desired_partnum, partnum);
746 mime_parser(msg->cm_fields['M'],
748 *ical_locate_part, /* callback function */
750 (void *) &ird, /* user data */
754 /* We're done with the incoming message, because we now have a
755 * calendar object in memory.
757 CtdlFreeMessage(msg);
760 * Here is the real meat of this function. Handle the event.
762 if (ird.cal != NULL) {
763 /* Update the user's calendar if necessary */
764 if (!strcasecmp(action, "update")) {
765 ret = ical_update_my_calendar_with_reply(ird.cal);
767 cprintf("%d Your calendar has been updated with this reply.\n",
771 cprintf("%d This event does not exist in your calendar.\n",
772 ERROR + FILE_NOT_FOUND);
775 cprintf("%d An internal error occurred.\n",
776 ERROR + INTERNAL_ERROR);
780 cprintf("%d This reply has been ignored.\n", CIT_OK);
783 /* Now that we've processed this message, we don't need it
784 * anymore. So delete it. (Maybe make this optional?)
786 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
788 /* Free the memory we allocated and return a response. */
789 icalcomponent_free(ird.cal);
794 cprintf("%d No calendar object found\n", ERROR);
798 /* should never get here */
803 * Search for a property in both the top level and in a VEVENT subcomponent
805 icalproperty *ical_ctdl_get_subprop(
807 icalproperty_kind which_prop
812 p = icalcomponent_get_first_property(cal, which_prop);
814 c = icalcomponent_get_first_component(cal,
815 ICAL_VEVENT_COMPONENT);
817 p = icalcomponent_get_first_property(c, which_prop);
825 * Check to see if two events overlap. Returns nonzero if they do.
827 int ical_ctdl_is_overlap(
828 struct icaltimetype t1start,
829 struct icaltimetype t1end,
830 struct icaltimetype t2start,
831 struct icaltimetype t2end
834 if (icaltime_is_null_time(t1start)) return(0);
835 if (icaltime_is_null_time(t2start)) return(0);
837 /* First, check for all-day events */
838 if (t1start.is_date) {
839 if (!icaltime_compare_date_only(t1start, t2start)) {
842 if (!icaltime_is_null_time(t2end)) {
843 if (!icaltime_compare_date_only(t1start, t2end)) {
849 if (t2start.is_date) {
850 if (!icaltime_compare_date_only(t2start, t1start)) {
853 if (!icaltime_is_null_time(t1end)) {
854 if (!icaltime_compare_date_only(t2start, t1end)) {
860 /* Now check for overlaps using date *and* time. */
862 /* First, bail out if either event 1 or event 2 is missing end time. */
863 if (icaltime_is_null_time(t1end)) return(0);
864 if (icaltime_is_null_time(t2end)) return(0);
866 /* If event 1 ends before event 2 starts, we're in the clear. */
867 if (icaltime_compare(t1end, t2start) <= 0) return(0);
869 /* If event 2 ends before event 1 starts, we're also ok. */
870 if (icaltime_compare(t2end, t1start) <= 0) return(0);
872 /* Otherwise, they overlap. */
879 * Backend for ical_hunt_for_conflicts()
881 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
883 struct CtdlMessage *msg;
884 struct ical_respond_data ird;
885 struct icaltimetype t1start, t1end, t2start, t2end;
887 char conflict_event_uid[SIZ];
888 char conflict_event_summary[SIZ];
889 char compare_uid[SIZ];
891 cal = (icalcomponent *)data;
892 strcpy(compare_uid, "");
893 strcpy(conflict_event_uid, "");
894 strcpy(conflict_event_summary, "");
896 msg = CtdlFetchMessage(msgnum);
897 if (msg == NULL) return;
898 memset(&ird, 0, sizeof ird);
899 strcpy(ird.desired_partnum, "_HUNT_");
900 mime_parser(msg->cm_fields['M'],
902 *ical_locate_part, /* callback function */
904 (void *) &ird, /* user data */
907 CtdlFreeMessage(msg);
909 if (ird.cal == NULL) return;
911 t1start = icaltime_null_time();
912 t1end = icaltime_null_time();
913 t2start = icaltime_null_time();
914 t1end = icaltime_null_time();
916 /* Now compare cal to ird.cal */
917 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
918 if (p == NULL) return;
919 if (p != NULL) t2start = icalproperty_get_dtstart(p);
921 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
922 if (p != NULL) t2end = icalproperty_get_dtend(p);
924 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
925 if (p == NULL) return;
926 if (p != NULL) t1start = icalproperty_get_dtstart(p);
928 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
929 if (p != NULL) t1end = icalproperty_get_dtend(p);
931 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
933 strcpy(compare_uid, icalproperty_get_comment(p));
936 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
938 strcpy(conflict_event_uid, icalproperty_get_comment(p));
941 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
943 strcpy(conflict_event_summary, icalproperty_get_comment(p));
946 icalcomponent_free(ird.cal);
948 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
949 cprintf("%ld||%s|%s|%d|\n",
952 conflict_event_summary,
953 ( ((strlen(compare_uid)>0)
954 &&(!strcasecmp(compare_uid,
955 conflict_event_uid))) ? 1 : 0
964 * Phase 2 of "hunt for conflicts" operation.
965 * At this point we have a calendar object which represents the VEVENT that
966 * we're considering adding to the calendar. Now hunt through the user's
967 * calendar room, and output zero or more existing VEVENTs which conflict
970 void ical_hunt_for_conflicts(icalcomponent *cal) {
971 char hold_rm[ROOMNAMELEN];
973 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
975 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
976 getroom(&CC->quickroom, hold_rm);
977 cprintf("%d You do not have a calendar.\n", ERROR);
981 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
983 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
985 ical_hunt_for_conflicts_backend,
990 getroom(&CC->quickroom, hold_rm); /* return to saved room */
997 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
999 void ical_conflicts(long msgnum, char *partnum) {
1000 struct CtdlMessage *msg;
1001 struct ical_respond_data ird;
1003 msg = CtdlFetchMessage(msgnum);
1005 cprintf("%d Message %ld not found.\n",
1006 ERROR+ILLEGAL_VALUE,
1012 memset(&ird, 0, sizeof ird);
1013 strcpy(ird.desired_partnum, partnum);
1014 mime_parser(msg->cm_fields['M'],
1016 *ical_locate_part, /* callback function */
1018 (void *) &ird, /* user data */
1022 CtdlFreeMessage(msg);
1024 if (ird.cal != NULL) {
1025 ical_hunt_for_conflicts(ird.cal);
1026 icalcomponent_free(ird.cal);
1030 cprintf("%d No calendar object found\n", ERROR);
1034 /* should never get here */
1040 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1042 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1045 struct icalperiodtype my_period;
1047 if (cal == NULL) return;
1048 my_period = icalperiodtype_null_period();
1050 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1051 ical_add_to_freebusy(fb,
1052 icalcomponent_get_first_component(
1053 cal, ICAL_VEVENT_COMPONENT
1061 /* If this event is not opaque, the user isn't publishing it as
1062 * busy time, so don't bother doing anything else.
1064 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1066 v = icalproperty_get_value(p);
1068 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1074 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1075 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1077 my_period.start = icalproperty_get_dtstart(p);
1080 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1082 my_period.end = icalproperty_get_dtstart(p);
1086 icalcomponent_add_property(fb,
1087 icalproperty_new_freebusy(my_period)
1095 * Backend for ical_freebusy()
1097 * This function simply loads the messages in the user's calendar room,
1098 * which contain VEVENTs, then strips them of all non-freebusy data, and
1099 * adds them to the supplied VCALENDAR.
1102 void ical_freebusy_backend(long msgnum, void *data) {
1104 struct CtdlMessage *msg;
1105 struct ical_respond_data ird;
1107 cal = (icalcomponent *)data;
1109 msg = CtdlFetchMessage(msgnum);
1110 if (msg == NULL) return;
1111 memset(&ird, 0, sizeof ird);
1112 strcpy(ird.desired_partnum, "_HUNT_");
1113 mime_parser(msg->cm_fields['M'],
1115 *ical_locate_part, /* callback function */
1117 (void *) &ird, /* user data */
1120 CtdlFreeMessage(msg);
1122 if (ird.cal == NULL) return;
1124 ical_add_to_freebusy(cal, ird.cal);
1126 /* Now free the memory. */
1127 icalcomponent_free(ird.cal);
1133 * Grab another user's free/busy times
1135 void ical_freebusy(char *who) {
1136 struct usersupp usbuf;
1137 char calendar_room_name[ROOMNAMELEN];
1138 char hold_rm[ROOMNAMELEN];
1139 char *serialized_request = NULL;
1140 icalcomponent *encaps = NULL;
1141 icalcomponent *fb = NULL;
1143 if (getuser(&usbuf, who) != 0) {
1144 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1148 MailboxName(calendar_room_name, sizeof calendar_room_name,
1149 &usbuf, USERCALENDARROOM);
1151 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
1153 if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
1154 cprintf("%d Cannot open calendar\n", ERROR+ROOM_NOT_FOUND);
1155 getroom(&CC->quickroom, hold_rm);
1160 /* Create a VCALENDAR in which we will encapsulate all the VFREEBUSYs */
1161 encaps = icalcomponent_new_citadel_vcalendar();
1162 if (encaps == NULL) {
1163 cprintf("%d Internal error: cannot allocate memory.\n",
1164 ERROR+INTERNAL_ERROR);
1165 getroom(&CC->quickroom, hold_rm);
1169 /* Create a VFREEBUSY subcomponent */
1170 fb = icalcomponent_new(ICAL_VFREEBUSY_COMPONENT);
1172 cprintf("%d Internal error: cannot allocate memory.\n",
1173 ERROR+INTERNAL_ERROR);
1174 icalcomponent_free(encaps);
1175 getroom(&CC->quickroom, hold_rm);
1179 /* Put the freebusy component into the calendar component */
1180 icalcomponent_add_component(encaps, fb);
1182 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
1183 NULL, ical_freebusy_backend, (void *)fb
1187 serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
1188 icalcomponent_free(encaps); /* Don't need this anymore. */
1190 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1191 if (serialized_request != NULL) {
1192 client_write(serialized_request, strlen(serialized_request));
1196 /* Go back to the room from which we came... */
1197 getroom(&CC->quickroom, hold_rm);
1199 cprintf("%d not implemented yet\n", ERROR);
1206 * All Citadel calendar commands from the client come through here.
1208 void cmd_ical(char *argbuf)
1216 if (CtdlAccessCheck(ac_logged_in)) return;
1218 extract(subcmd, argbuf, 0);
1220 if (!strcmp(subcmd, "test")) {
1221 cprintf("%d This server supports calendaring\n", CIT_OK);
1225 else if (!strcmp(subcmd, "respond")) {
1226 msgnum = extract_long(argbuf, 1);
1227 extract(partnum, argbuf, 2);
1228 extract(action, argbuf, 3);
1229 ical_respond(msgnum, partnum, action);
1232 else if (!strcmp(subcmd, "handle_rsvp")) {
1233 msgnum = extract_long(argbuf, 1);
1234 extract(partnum, argbuf, 2);
1235 extract(action, argbuf, 3);
1236 ical_handle_rsvp(msgnum, partnum, action);
1239 else if (!strcmp(subcmd, "conflicts")) {
1240 msgnum = extract_long(argbuf, 1);
1241 extract(partnum, argbuf, 2);
1242 ical_conflicts(msgnum, partnum);
1245 else if (!strcmp(subcmd, "freebusy")) {
1246 extract(who, argbuf, 1);
1251 cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
1255 /* should never get here */
1261 * We don't know if the calendar room exists so we just create it at login
1263 void ical_create_room(void)
1265 struct quickroom qr;
1268 /* Create the calendar room if it doesn't already exist */
1269 create_room(USERCALENDARROOM, 4, "", 0, 1, 0);
1271 /* Set expiration policy to manual; otherwise objects will be lost! */
1272 if (lgetroom(&qr, USERCALENDARROOM)) {
1273 lprintf(3, "Couldn't get the user calendar room!\n");
1276 qr.QRep.expire_mode = EXPIRE_MANUAL;
1277 qr.QRdefaultview = 3; /* 3 = calendar view */
1280 /* Set the view to a calendar view */
1281 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
1282 vbuf.v_view = 3; /* 3 = calendar */
1283 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
1285 /* Create the tasks list room if it doesn't already exist */
1286 create_room(USERTASKSROOM, 4, "", 0, 1, 0);
1288 /* Set expiration policy to manual; otherwise objects will be lost! */
1289 if (lgetroom(&qr, USERTASKSROOM)) {
1290 lprintf(3, "Couldn't get the user calendar room!\n");
1293 qr.QRep.expire_mode = EXPIRE_MANUAL;
1294 qr.QRdefaultview = 4; /* 4 = tasks view */
1297 /* Set the view to a task list view */
1298 CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
1299 vbuf.v_view = 4; /* 4 = tasks */
1300 CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
1307 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1310 void ical_send_out_invitations(icalcomponent *cal) {
1311 icalcomponent *the_request = NULL;
1312 char *serialized_request = NULL;
1313 icalcomponent *encaps = NULL;
1314 char *request_message_text = NULL;
1315 struct CtdlMessage *msg = NULL;
1316 struct recptypes *valid = NULL;
1317 char attendees_string[SIZ];
1318 int num_attendees = 0;
1319 char this_attendee[SIZ];
1320 icalproperty *attendee = NULL;
1321 char summary_string[SIZ];
1322 icalproperty *summary = NULL;
1325 lprintf(3, "ERROR: trying to reply to NULL event?\n");
1330 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1331 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1332 ical_send_out_invitations(
1333 icalcomponent_get_first_component(
1334 cal, ICAL_VEVENT_COMPONENT
1340 /* Clone the event */
1341 the_request = icalcomponent_new_clone(cal);
1342 if (the_request == NULL) {
1343 lprintf(3, "ERROR: cannot clone calendar object\n");
1347 /* Extract the summary string -- we'll use it as the
1348 * message subject for the request
1350 strcpy(summary_string, "Meeting request");
1351 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1352 if (summary != NULL) {
1353 if (icalproperty_get_summary(summary)) {
1354 strcpy(summary_string,
1355 icalproperty_get_summary(summary) );
1359 /* Determine who the recipients of this message are (the attendees) */
1360 strcpy(attendees_string, "");
1361 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1362 if (icalproperty_get_attendee(attendee)) {
1363 strcpy(this_attendee, icalproperty_get_attendee(attendee) );
1364 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1365 strcpy(this_attendee, &this_attendee[7]);
1366 snprintf(&attendees_string[strlen(attendees_string)],
1367 sizeof(attendees_string) - strlen(attendees_string),
1376 lprintf(9, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1378 /* If there are no attendees, there are no invitations to send, so...
1379 * don't bother putting one together! Punch out, Maverick!
1381 if (num_attendees == 0) {
1382 icalcomponent_free(the_request);
1386 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1387 encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
1388 if (encaps == NULL) {
1389 lprintf(3, "Error at %s:%d - could not allocate component!\n",
1390 __FILE__, __LINE__);
1391 icalcomponent_free(the_request);
1395 /* Set the Product ID */
1396 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1398 /* Set the Version Number */
1399 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1401 /* Set the method to REQUEST */
1402 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1404 /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1405 ical_dezonify(the_request);
1407 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1408 * are responsible for "the_request"'s memory -- it will be freed
1409 * when we free "encaps".
1411 icalcomponent_add_component(encaps, the_request);
1414 serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
1415 icalcomponent_free(encaps); /* Don't need this anymore. */
1416 if (serialized_request == NULL) return;
1418 request_message_text = mallok(strlen(serialized_request) + SIZ);
1419 if (request_message_text != NULL) {
1420 sprintf(request_message_text,
1421 "Content-type: text/calendar\r\n\r\n%s\r\n",
1425 msg = CtdlMakeMessage(&CC->usersupp,
1426 "", /* No single recipient here */
1427 CC->quickroom.QRname, 0, FMT_RFC822,
1429 summary_string, /* Use summary for subject */
1430 request_message_text);
1433 valid = validate_recipients(attendees_string);
1434 CtdlSubmitMsg(msg, valid, "");
1435 CtdlFreeMessage(msg);
1438 phree(serialized_request);
1443 * When a calendar object is being saved, determine whether it's a VEVENT
1444 * and the user saving it is the organizer. If so, send out invitations
1445 * to any listed attendees.
1448 void ical_saving_vevent(icalcomponent *cal) {
1450 icalproperty *organizer = NULL;
1451 char organizer_string[SIZ];
1453 /* Don't send out invitations if we've been asked not to. */
1454 lprintf(9, "CIT_ICAL->avoid_sending_invitations = %d\n",
1455 CIT_ICAL->avoid_sending_invitations);
1456 if (CIT_ICAL->avoid_sending_invitations > 0) {
1460 strcpy(organizer_string, "");
1462 * The VEVENT subcomponent is the one we're interested in.
1463 * Send out invitations if, and only if, this user is the Organizer.
1465 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1466 organizer = icalcomponent_get_first_property(cal,
1467 ICAL_ORGANIZER_PROPERTY);
1468 if (organizer != NULL) {
1469 if (icalproperty_get_organizer(organizer)) {
1470 strcpy(organizer_string,
1471 icalproperty_get_organizer(organizer));
1474 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1475 strcpy(organizer_string, &organizer_string[7]);
1476 striplt(organizer_string);
1478 * If the user saving the event is listed as the
1479 * organizer, then send out invitations.
1481 if (CtdlIsMe(organizer_string)) {
1482 ical_send_out_invitations(cal);
1487 /* If the component has subcomponents, recurse through them. */
1488 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1490 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1491 /* Recursively process subcomponent */
1492 ical_saving_vevent(c);
1500 * Back end for ical_obj_beforesave()
1501 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1502 * the summary of the event (becomes message subject),
1503 * and the start time (becomes message date/time).
1505 void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
1506 char *disp, void *content, char *cbtype, size_t length,
1507 char *encoding, void *cbuserdata)
1511 struct icalmessagemod *imm;
1513 imm = (struct icalmessagemod *)cbuserdata;
1515 /* If this is a text/calendar object, hunt for the UID and drop it in
1516 * the "user data" pointer for the MIME parser. When
1517 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
1520 if (!strcasecmp(cbtype, "text/calendar")) {
1521 cal = icalcomponent_new_from_string(content);
1523 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1524 cal = icalcomponent_get_first_component(
1525 cal, ICAL_VEVENT_COMPONENT
1530 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1532 strcpy(imm->uid, icalproperty_get_comment(p));
1534 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
1536 strcpy(imm->subject,
1537 icalproperty_get_comment(p));
1539 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
1541 imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
1543 icalcomponent_free(cal);
1553 * See if we need to prevent the object from being saved (we don't allow
1554 * MIME types other than text/calendar in the Calendar> room). Also, when
1555 * saving an event to the calendar, set the message's Citadel extended message
1556 * ID to the UID of the object. This causes our replication checker to
1557 * automatically delete any existing instances of the same object. (Isn't
1560 * We also set the message's Subject to the event summary, and the Date/time to
1561 * the event start time.
1563 int ical_obj_beforesave(struct CtdlMessage *msg)
1565 char roomname[ROOMNAMELEN];
1568 struct icalmessagemod imm;
1571 * Only messages with content-type text/calendar
1572 * may be saved to Calendar>. If the message is bound for
1573 * Calendar> but doesn't have this content-type, throw an error
1574 * so that the message may not be posted.
1577 /* First determine if this is our room */
1578 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
1579 if (strcasecmp(roomname, CC->quickroom.QRname)) {
1580 return 0; /* It's not the Calendar room. */
1583 /* Then determine content-type of the message */
1585 /* It must be an RFC822 message! */
1586 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1587 if (msg->cm_format_type != 4)
1588 return 1; /* You tried to save a non-RFC822 message! */
1590 /* Find the Content-Type: header */
1591 p = msg->cm_fields['M'];
1594 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1595 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1596 memset(&imm, 0, sizeof(struct icalmessagemod));
1597 mime_parser(msg->cm_fields['M'],
1599 *ical_ctdl_set_extended_msgid,
1604 if (strlen(imm.uid) > 0) {
1605 if (msg->cm_fields['E'] != NULL) {
1606 phree(msg->cm_fields['E']);
1608 msg->cm_fields['E'] = strdoop(imm.uid);
1610 if (strlen(imm.subject) > 0) {
1611 if (msg->cm_fields['U'] != NULL) {
1612 phree(msg->cm_fields['U']);
1614 msg->cm_fields['U'] = strdoop(imm.subject);
1616 if (imm.dtstart > 0) {
1617 if (msg->cm_fields['T'] != NULL) {
1618 phree(msg->cm_fields['T']);
1620 msg->cm_fields['T'] = strdoop("000000000000000000");
1621 sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
1632 /* Oops! No Content-Type in this message! How'd that happen? */
1633 lprintf(7, "RFC822 message with no Content-Type header!\n");
1639 * Things we need to do after saving a calendar event.
1641 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
1642 char *disp, void *content, char *cbtype, size_t length,
1643 char *encoding, void *cbuserdata)
1647 /* If this is a text/calendar object, hunt for the UID and drop it in
1648 * the "user data" pointer for the MIME parser. When
1649 * ical_obj_beforesave() sees it there, it'll set the Extended msgid
1652 if (!strcasecmp(cbtype, "text/calendar")) {
1653 cal = icalcomponent_new_from_string(content);
1655 ical_saving_vevent(cal);
1656 icalcomponent_free(cal);
1663 * Things we need to do after saving a calendar event.
1665 int ical_obj_aftersave(struct CtdlMessage *msg)
1667 char roomname[ROOMNAMELEN];
1672 * If this isn't the Calendar> room, no further action is necessary.
1675 /* First determine if this is our room */
1676 MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
1677 if (strcasecmp(roomname, CC->quickroom.QRname)) {
1678 return 0; /* It's not the Calendar room. */
1681 /* Then determine content-type of the message */
1683 /* It must be an RFC822 message! */
1684 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1685 if (msg->cm_format_type != 4) return(1);
1687 /* Find the Content-Type: header */
1688 p = msg->cm_fields['M'];
1691 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1692 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1693 mime_parser(msg->cm_fields['M'],
1695 *ical_obj_aftersave_backend,
1709 /* Oops! No Content-Type in this message! How'd that happen? */
1710 lprintf(7, "RFC822 message with no Content-Type header!\n");
1715 void ical_session_startup(void) {
1716 CtdlAllocUserData(SYM_CIT_ICAL, sizeof(struct cit_ical));
1717 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
1721 #endif /* CITADEL_WITH_CALENDAR_SERVICE */
1724 * Register this module with the Citadel server.
1726 char *serv_calendar_init(void)
1728 #ifdef CITADEL_WITH_CALENDAR_SERVICE
1729 SYM_CIT_ICAL = CtdlGetDynamicSymbol();
1730 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
1731 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
1732 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
1733 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
1734 CtdlRegisterSessionHook(ical_session_startup, EVT_START);