4 * This module implements iCalendar object processing and the Calendar>
5 * room on a Citadel server. It handles iCalendar objects using the
6 * iTIP protocol. See RFCs 2445 and 2446.
10 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
15 #include <sys/types.h>
24 #include "citserver.h"
25 #include "sysdep_decls.h"
28 #include "serv_extensions.h"
33 #include "mime_parser.h"
34 #include "internet_addressing.h"
35 #include "serv_calendar.h"
37 #ifdef CITADEL_WITH_CALENDAR_SERVICE
40 #include "ical_dezonify.h"
42 struct ical_respond_data {
43 char desired_partnum[SIZ];
49 * Utility function to create a new VCALENDAR component with some of the
50 * required fields already set the way we like them.
52 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
53 icalcomponent *encaps;
55 encaps = icalcomponent_new_vcalendar();
57 lprintf(CTDL_CRIT, "Error at %s:%d - could not allocate component!\n",
62 /* Set the Product ID */
63 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
65 /* Set the Version Number */
66 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
73 * Utility function to encapsulate a subcomponent into a full VCALENDAR
75 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
76 icalcomponent *encaps;
78 /* If we're already looking at a full VCALENDAR component,
79 * don't bother ... just return itself.
81 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
85 /* Encapsulate the VEVENT component into a complete VCALENDAR */
86 encaps = icalcomponent_new_citadel_vcalendar();
87 if (encaps == NULL) return NULL;
89 /* Encapsulate the subcomponent inside */
90 icalcomponent_add_component(encaps, subcomp);
92 /* Convert all timestamps to UTC so we don't have to deal with
93 * stupid VTIMEZONE crap.
95 ical_dezonify(encaps);
97 /* Return the object we just created. */
105 * Write a calendar object into the specified user's calendar room.
109 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
113 icalcomponent *encaps;
115 if (cal == NULL) return;
117 /* If the supplied object is a subcomponent, encapsulate it in
118 * a full VCALENDAR component, and save that instead.
120 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
121 encaps = ical_encapsulate_subcomponent(
122 icalcomponent_new_clone(cal)
124 ical_write_to_cal(u, encaps);
125 icalcomponent_free(encaps);
129 strcpy(temp, tmpnam(NULL));
130 ser = icalcomponent_as_ical_string(cal);
131 if (ser == NULL) return;
133 /* Make a temp file out of it */
134 fp = fopen(temp, "w");
135 if (fp == NULL) return;
136 fwrite(ser, strlen(ser), 1, fp);
139 /* This handy API function does all the work for us.
141 CtdlWriteObject(USERCALENDARROOM, /* which room */
142 "text/calendar", /* MIME type */
143 temp, /* temp file */
146 0, /* don't delete others of this type */
154 * Add a calendar object to the user's calendar
156 * ok because it uses ical_write_to_cal()
158 void ical_add(icalcomponent *cal, int recursion_level) {
162 * The VEVENT subcomponent is the one we're interested in saving.
164 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
166 ical_write_to_cal(&CC->user, cal);
170 /* If the component has subcomponents, recurse through them. */
171 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
173 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
174 /* Recursively process subcomponent */
175 ical_add(c, recursion_level+1);
183 * Send a reply to a meeting invitation.
185 * 'request' is the invitation to reply to.
186 * 'action' is the string "accept" or "decline".
188 * (Sorry about this being more than 80 columns ... there was just
189 * no easy way to break it down sensibly.)
193 void ical_send_a_reply(icalcomponent *request, char *action) {
194 icalcomponent *the_reply = NULL;
195 icalcomponent *vevent = NULL;
196 icalproperty *attendee = NULL;
197 char attendee_string[SIZ];
198 icalproperty *organizer = NULL;
199 char organizer_string[SIZ];
200 icalproperty *summary = NULL;
201 char summary_string[SIZ];
202 icalproperty *me_attend = NULL;
203 struct recptypes *recp = NULL;
204 icalparameter *partstat = NULL;
205 char *serialized_reply = NULL;
206 char *reply_message_text = NULL;
207 struct CtdlMessage *msg = NULL;
208 struct recptypes *valid = NULL;
210 strcpy(organizer_string, "");
211 strcpy(summary_string, "Calendar item");
213 if (request == NULL) {
214 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
218 the_reply = icalcomponent_new_clone(request);
219 if (the_reply == NULL) {
220 lprintf(CTDL_ERR, "ERROR: cannot clone request\n");
224 /* Change the method from REQUEST to REPLY */
225 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
227 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
228 if (vevent != NULL) {
229 /* Hunt for attendees, removing ones that aren't us.
230 * (Actually, remove them all, cloning our own one so we can
231 * re-insert it later)
233 while (attendee = icalcomponent_get_first_property(vevent,
234 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
236 if (icalproperty_get_attendee(attendee)) {
237 strcpy(attendee_string,
238 icalproperty_get_attendee(attendee) );
239 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
240 strcpy(attendee_string, &attendee_string[7]);
241 striplt(attendee_string);
242 recp = validate_recipients(attendee_string);
244 if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
245 if (me_attend) icalproperty_free(me_attend);
246 me_attend = icalproperty_new_clone(attendee);
253 icalcomponent_remove_property(vevent, attendee);
254 icalproperty_free(attendee);
257 /* We found our own address in the attendee list. */
259 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
260 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
262 if (!strcasecmp(action, "accept")) {
263 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
265 else if (!strcasecmp(action, "decline")) {
266 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
268 else if (!strcasecmp(action, "tentative")) {
269 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
272 if (partstat) icalproperty_add_parameter(me_attend, partstat);
274 /* Now insert it back into the vevent. */
275 icalcomponent_add_property(vevent, me_attend);
278 /* Figure out who to send this thing to */
279 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
280 if (organizer != NULL) {
281 if (icalproperty_get_organizer(organizer)) {
282 strcpy(organizer_string,
283 icalproperty_get_organizer(organizer) );
286 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
287 strcpy(organizer_string, &organizer_string[7]);
288 striplt(organizer_string);
290 strcpy(organizer_string, "");
293 /* Extract the summary string -- we'll use it as the
294 * message subject for the reply
296 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
297 if (summary != NULL) {
298 if (icalproperty_get_summary(summary)) {
299 strcpy(summary_string,
300 icalproperty_get_summary(summary) );
306 /* Now generate the reply message and send it out. */
307 serialized_reply = strdup(icalcomponent_as_ical_string(the_reply));
308 icalcomponent_free(the_reply); /* don't need this anymore */
309 if (serialized_reply == NULL) return;
311 reply_message_text = malloc(strlen(serialized_reply) + SIZ);
312 if (reply_message_text != NULL) {
313 sprintf(reply_message_text,
314 "Content-type: text/calendar\r\n\r\n%s\r\n",
318 msg = CtdlMakeMessage(&CC->user,
319 organizer_string, /* to */
321 CC->room.QRname, 0, FMT_RFC822,
323 summary_string, /* Use summary for subject */
327 valid = validate_recipients(organizer_string);
328 CtdlSubmitMsg(msg, valid, "");
329 CtdlFreeMessage(msg);
332 free(serialized_reply);
338 * Callback function for mime parser that hunts for calendar content types
339 * and turns them into calendar objects
341 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
342 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
345 struct ical_respond_data *ird = NULL;
347 ird = (struct ical_respond_data *) cbuserdata;
349 /* desired_partnum can be set to "_HUNT_" to have it just look for
350 * the first part with a content type of text/calendar. Otherwise
351 * we have to only process the right one.
353 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
354 if (strcasecmp(partnum, ird->desired_partnum)) {
359 if (strcasecmp(cbtype, "text/calendar")) {
363 if (ird->cal != NULL) {
364 icalcomponent_free(ird->cal);
368 ird->cal = icalcomponent_new_from_string(content);
369 if (ird->cal != NULL) {
370 ical_dezonify(ird->cal);
376 * Respond to a meeting request.
378 void ical_respond(long msgnum, char *partnum, char *action) {
379 struct CtdlMessage *msg;
380 struct ical_respond_data ird;
383 (strcasecmp(action, "accept"))
384 && (strcasecmp(action, "decline"))
386 cprintf("%d Action must be 'accept' or 'decline'\n",
387 ERROR + ILLEGAL_VALUE
392 msg = CtdlFetchMessage(msgnum, 1);
394 cprintf("%d Message %ld not found.\n",
395 ERROR + ILLEGAL_VALUE,
401 memset(&ird, 0, sizeof ird);
402 strcpy(ird.desired_partnum, partnum);
403 mime_parser(msg->cm_fields['M'],
405 *ical_locate_part, /* callback function */
407 (void *) &ird, /* user data */
411 /* We're done with the incoming message, because we now have a
412 * calendar object in memory.
414 CtdlFreeMessage(msg);
417 * Here is the real meat of this function. Handle the event.
419 if (ird.cal != NULL) {
420 /* Save this in the user's calendar if necessary */
421 if (!strcasecmp(action, "accept")) {
422 ical_add(ird.cal, 0);
425 /* Send a reply if necessary */
426 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
427 ical_send_a_reply(ird.cal, action);
430 /* Now that we've processed this message, we don't need it
431 * anymore. So delete it.
433 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 1);
435 /* Free the memory we allocated and return a response. */
436 icalcomponent_free(ird.cal);
438 cprintf("%d ok\n", CIT_OK);
442 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
446 /* should never get here */
451 * Figure out the UID of the calendar event being referred to in a
452 * REPLY object. This function is recursive.
454 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
455 icalcomponent *subcomponent;
458 /* If this object is a REPLY, then extract the UID. */
459 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
460 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
462 strcpy(uidbuf, icalproperty_get_comment(p));
466 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
467 * UID of the reply; we want the UID of the invitation being replied to.
469 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
470 subcomponent != NULL;
471 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
472 ical_learn_uid_of_reply(uidbuf, subcomponent);
478 * ical_update_my_calendar_with_reply() refers to this callback function; when we
479 * locate the message containing the calendar event we're replying to, this function
480 * gets called. It basically just sticks the message number in a supplied buffer.
482 void ical_hunt_for_event_to_update(long msgnum, void *data) {
485 msgnumptr = (long *) data;
490 struct original_event_container {
495 * Callback function for mime parser that hunts for calendar content types
496 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
497 * to fetch the object being updated)
499 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
500 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
503 struct original_event_container *oec = NULL;
505 if (strcasecmp(cbtype, "text/calendar")) {
508 oec = (struct original_event_container *) cbuserdata;
509 if (oec->c != NULL) {
510 icalcomponent_free(oec->c);
512 oec->c = icalcomponent_new_from_string(content);
517 * Merge updated attendee information from a REPLY into an existing event.
519 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
521 icalproperty *e_attendee, *r_attendee;
523 /* First things first. If we're not looking at a VEVENT component,
524 * recurse through subcomponents until we find one.
526 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
527 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
529 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
530 ical_merge_attendee_reply(c, reply);
535 /* Now do the same thing with the reply.
537 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
538 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
540 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
541 ical_merge_attendee_reply(event, c);
546 /* Clone the reply, because we're going to rip its guts out. */
547 reply = icalcomponent_new_clone(reply);
549 /* At this point we're looking at the correct subcomponents.
550 * Iterate through the attendees looking for a match.
553 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
555 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
557 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
559 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
561 /* Check to see if these two attendees match...
564 icalproperty_get_attendee(e_attendee),
565 icalproperty_get_attendee(r_attendee)
567 /* ...and if they do, remove the attendee from the event
568 * and replace it with the attendee from the reply. (The
569 * reply's copy will have the same address, but an updated
572 icalcomponent_remove_property(event, e_attendee);
573 icalproperty_free(e_attendee);
574 icalcomponent_remove_property(reply, r_attendee);
575 icalcomponent_add_property(event, r_attendee);
577 /* Since we diddled both sets of attendees, we have to start
578 * the iteration over again. This will not create an infinite
579 * loop because we removed the attendee from the reply. (That's
580 * why we cloned the reply, and that's what we mean by "ripping
589 /* Free the *clone* of the reply. */
590 icalcomponent_free(reply);
597 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
598 * calendar event. The object has already been deserialized for us; all
599 * we have to do here is hunt for the event in our calendar, merge in the
600 * updated attendee status, and save it again.
602 * This function returns 0 on success, 1 if the event was not found in the
603 * user's calendar, or 2 if an internal error occurred.
605 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
607 char hold_rm[ROOMNAMELEN];
608 long msgnum_being_replaced = 0;
609 struct CtdlMessage *template = NULL;
610 struct CtdlMessage *msg;
611 struct original_event_container oec;
612 icalcomponent *original_event;
613 char *serialized_event = NULL;
614 char roomname[ROOMNAMELEN];
615 char *message_text = NULL;
617 /* Figure out just what event it is we're dealing with */
618 strcpy(uid, "--==<< InVaLiD uId >>==--");
619 ical_learn_uid_of_reply(uid, cal);
620 lprintf(CTDL_DEBUG, "UID of event being replied to is <%s>\n", uid);
622 strcpy(hold_rm, CC->room.QRname); /* save current room */
624 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
625 getroom(&CC->room, hold_rm);
626 lprintf(CTDL_CRIT, "cannot get user calendar room\n");
631 * Pound through the user's calendar looking for a message with
632 * the Citadel EUID set to the value we're looking for. Since
633 * Citadel always sets the message EUID to the vCalendar UID of
634 * the event, this will work.
636 template = (struct CtdlMessage *)
637 malloc(sizeof(struct CtdlMessage));
638 memset(template, 0, sizeof(struct CtdlMessage));
639 template->cm_fields['E'] = strdup(uid);
640 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
641 template, ical_hunt_for_event_to_update, &msgnum_being_replaced);
642 CtdlFreeMessage(template);
643 getroom(&CC->room, hold_rm); /* return to saved room */
645 lprintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
646 if (msgnum_being_replaced == 0) {
647 return(1); /* no calendar event found */
650 /* Now we know the ID of the message containing the event being updated.
651 * We don't actually have to delete it; that'll get taken care of by the
652 * server when we save another event with the same UID. This just gives
653 * us the ability to load the event into memory so we can diddle the
656 msg = CtdlFetchMessage(msgnum_being_replaced, 1);
658 return(2); /* internal error */
661 mime_parser(msg->cm_fields['M'],
663 *ical_locate_original_event, /* callback function */
665 &oec, /* user data */
668 CtdlFreeMessage(msg);
670 original_event = oec.c;
671 if (original_event == NULL) {
672 lprintf(CTDL_ERR, "ERROR: Original_component is NULL.\n");
676 /* Merge the attendee's updated status into the event */
677 ical_merge_attendee_reply(original_event, cal);
680 serialized_event = strdup(icalcomponent_as_ical_string(original_event));
681 icalcomponent_free(original_event); /* Don't need this anymore. */
682 if (serialized_event == NULL) return(2);
684 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
686 message_text = malloc(strlen(serialized_event) + SIZ);
687 if (message_text != NULL) {
688 sprintf(message_text,
689 "Content-type: text/calendar\r\n\r\n%s\r\n",
693 msg = CtdlMakeMessage(&CC->user,
694 "", /* No recipient */
695 "", /* No recipient */
703 CIT_ICAL->avoid_sending_invitations = 1;
704 CtdlSubmitMsg(msg, NULL, roomname);
705 CtdlFreeMessage(msg);
706 CIT_ICAL->avoid_sending_invitations = 0;
709 free(serialized_event);
715 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
716 * simply extracts the calendar object from the message, deserializes it, and
717 * passes it up to ical_update_my_calendar_with_reply() for processing.
719 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
720 struct CtdlMessage *msg;
721 struct ical_respond_data ird;
725 (strcasecmp(action, "update"))
726 && (strcasecmp(action, "ignore"))
728 cprintf("%d Action must be 'update' or 'ignore'\n",
729 ERROR + ILLEGAL_VALUE
734 msg = CtdlFetchMessage(msgnum, 1);
736 cprintf("%d Message %ld not found.\n",
737 ERROR + ILLEGAL_VALUE,
743 memset(&ird, 0, sizeof ird);
744 strcpy(ird.desired_partnum, partnum);
745 mime_parser(msg->cm_fields['M'],
747 *ical_locate_part, /* callback function */
749 (void *) &ird, /* user data */
753 /* We're done with the incoming message, because we now have a
754 * calendar object in memory.
756 CtdlFreeMessage(msg);
759 * Here is the real meat of this function. Handle the event.
761 if (ird.cal != NULL) {
762 /* Update the user's calendar if necessary */
763 if (!strcasecmp(action, "update")) {
764 ret = ical_update_my_calendar_with_reply(ird.cal);
766 cprintf("%d Your calendar has been updated with this reply.\n",
770 cprintf("%d This event does not exist in your calendar.\n",
771 ERROR + FILE_NOT_FOUND);
774 cprintf("%d An internal error occurred.\n",
775 ERROR + INTERNAL_ERROR);
779 cprintf("%d This reply has been ignored.\n", CIT_OK);
782 /* Now that we've processed this message, we don't need it
783 * anymore. So delete it. (Maybe make this optional?)
785 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 1);
787 /* Free the memory we allocated and return a response. */
788 icalcomponent_free(ird.cal);
793 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
797 /* should never get here */
802 * Search for a property in both the top level and in a VEVENT subcomponent
804 icalproperty *ical_ctdl_get_subprop(
806 icalproperty_kind which_prop
811 p = icalcomponent_get_first_property(cal, which_prop);
813 c = icalcomponent_get_first_component(cal,
814 ICAL_VEVENT_COMPONENT);
816 p = icalcomponent_get_first_property(c, which_prop);
824 * Check to see if two events overlap. Returns nonzero if they do.
825 * (This function is used in both Citadel and WebCit. If you change it in
826 * one place, change it in the other. Better yet, put it in a library.)
828 int ical_ctdl_is_overlap(
829 struct icaltimetype t1start,
830 struct icaltimetype t1end,
831 struct icaltimetype t2start,
832 struct icaltimetype t2end
835 if (icaltime_is_null_time(t1start)) return(0);
836 if (icaltime_is_null_time(t2start)) return(0);
838 /* First, check for all-day events */
839 if (t1start.is_date) {
840 if (!icaltime_compare_date_only(t1start, t2start)) {
843 if (!icaltime_is_null_time(t2end)) {
844 if (!icaltime_compare_date_only(t1start, t2end)) {
850 if (t2start.is_date) {
851 if (!icaltime_compare_date_only(t2start, t1start)) {
854 if (!icaltime_is_null_time(t1end)) {
855 if (!icaltime_compare_date_only(t2start, t1end)) {
861 /* Now check for overlaps using date *and* time. */
863 /* First, bail out if either event 1 or event 2 is missing end time. */
864 if (icaltime_is_null_time(t1end)) return(0);
865 if (icaltime_is_null_time(t2end)) return(0);
867 /* If event 1 ends before event 2 starts, we're in the clear. */
868 if (icaltime_compare(t1end, t2start) <= 0) return(0);
870 /* If event 2 ends before event 1 starts, we're also ok. */
871 if (icaltime_compare(t2end, t1start) <= 0) return(0);
873 /* Otherwise, they overlap. */
880 * Backend for ical_hunt_for_conflicts()
882 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
884 struct CtdlMessage *msg;
885 struct ical_respond_data ird;
886 struct icaltimetype t1start, t1end, t2start, t2end;
888 char conflict_event_uid[SIZ];
889 char conflict_event_summary[SIZ];
890 char compare_uid[SIZ];
892 cal = (icalcomponent *)data;
893 strcpy(compare_uid, "");
894 strcpy(conflict_event_uid, "");
895 strcpy(conflict_event_summary, "");
897 msg = CtdlFetchMessage(msgnum, 1);
898 if (msg == NULL) return;
899 memset(&ird, 0, sizeof ird);
900 strcpy(ird.desired_partnum, "_HUNT_");
901 mime_parser(msg->cm_fields['M'],
903 *ical_locate_part, /* callback function */
905 (void *) &ird, /* user data */
908 CtdlFreeMessage(msg);
910 if (ird.cal == NULL) return;
912 t1start = icaltime_null_time();
913 t1end = icaltime_null_time();
914 t2start = icaltime_null_time();
915 t1end = icaltime_null_time();
917 /* Now compare cal to ird.cal */
918 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
919 if (p == NULL) return;
920 if (p != NULL) t2start = icalproperty_get_dtstart(p);
922 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
923 if (p != NULL) t2end = icalproperty_get_dtend(p);
925 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
926 if (p == NULL) return;
927 if (p != NULL) t1start = icalproperty_get_dtstart(p);
929 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
930 if (p != NULL) t1end = icalproperty_get_dtend(p);
932 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
934 strcpy(compare_uid, icalproperty_get_comment(p));
937 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
939 strcpy(conflict_event_uid, icalproperty_get_comment(p));
942 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
944 strcpy(conflict_event_summary, icalproperty_get_comment(p));
947 icalcomponent_free(ird.cal);
949 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
950 cprintf("%ld||%s|%s|%d|\n",
953 conflict_event_summary,
954 ( ((strlen(compare_uid)>0)
955 &&(!strcasecmp(compare_uid,
956 conflict_event_uid))) ? 1 : 0
965 * Phase 2 of "hunt for conflicts" operation.
966 * At this point we have a calendar object which represents the VEVENT that
967 * we're considering adding to the calendar. Now hunt through the user's
968 * calendar room, and output zero or more existing VEVENTs which conflict
971 void ical_hunt_for_conflicts(icalcomponent *cal) {
972 char hold_rm[ROOMNAMELEN];
974 strcpy(hold_rm, CC->room.QRname); /* save current room */
976 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
977 getroom(&CC->room, hold_rm);
978 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
982 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
984 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
986 ical_hunt_for_conflicts_backend,
991 getroom(&CC->room, hold_rm); /* return to saved room */
998 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
1000 void ical_conflicts(long msgnum, char *partnum) {
1001 struct CtdlMessage *msg;
1002 struct ical_respond_data ird;
1004 msg = CtdlFetchMessage(msgnum, 1);
1006 cprintf("%d Message %ld not found.\n",
1007 ERROR + ILLEGAL_VALUE,
1013 memset(&ird, 0, sizeof ird);
1014 strcpy(ird.desired_partnum, partnum);
1015 mime_parser(msg->cm_fields['M'],
1017 *ical_locate_part, /* callback function */
1019 (void *) &ird, /* user data */
1023 CtdlFreeMessage(msg);
1025 if (ird.cal != NULL) {
1026 ical_hunt_for_conflicts(ird.cal);
1027 icalcomponent_free(ird.cal);
1031 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1035 /* should never get here */
1041 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1043 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1046 struct icalperiodtype my_period;
1048 if (cal == NULL) return;
1049 my_period = icalperiodtype_null_period();
1051 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1052 ical_add_to_freebusy(fb,
1053 icalcomponent_get_first_component(
1054 cal, ICAL_VEVENT_COMPONENT
1062 /* If this event is not opaque, the user isn't publishing it as
1063 * busy time, so don't bother doing anything else.
1065 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1067 v = icalproperty_get_value(p);
1069 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1075 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1076 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1078 my_period.start = icalproperty_get_dtstart(p);
1081 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1083 my_period.end = icalproperty_get_dtstart(p);
1087 icalcomponent_add_property(fb,
1088 icalproperty_new_freebusy(my_period)
1091 /* Make sure the DTSTART property of the freebusy *list* is set to
1092 * the DTSTART property of the *earliest event*.
1094 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1096 icalcomponent_set_dtstart(fb,
1097 icalcomponent_get_dtstart(cal) );
1100 if (icaltime_compare(
1101 icalcomponent_get_dtstart(cal),
1102 icalcomponent_get_dtstart(fb)
1104 icalcomponent_set_dtstart(fb,
1105 icalcomponent_get_dtstart(cal) );
1109 /* Make sure the DTEND property of the freebusy *list* is set to
1110 * the DTEND property of the *latest event*.
1112 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1114 icalcomponent_set_dtend(fb,
1115 icalcomponent_get_dtend(cal) );
1118 if (icaltime_compare(
1119 icalcomponent_get_dtend(cal),
1120 icalcomponent_get_dtend(fb)
1122 icalcomponent_set_dtend(fb,
1123 icalcomponent_get_dtend(cal) );
1132 * Backend for ical_freebusy()
1134 * This function simply loads the messages in the user's calendar room,
1135 * which contain VEVENTs, then strips them of all non-freebusy data, and
1136 * adds them to the supplied VCALENDAR.
1139 void ical_freebusy_backend(long msgnum, void *data) {
1141 struct CtdlMessage *msg;
1142 struct ical_respond_data ird;
1144 cal = (icalcomponent *)data;
1146 msg = CtdlFetchMessage(msgnum, 1);
1147 if (msg == NULL) return;
1148 memset(&ird, 0, sizeof ird);
1149 strcpy(ird.desired_partnum, "_HUNT_");
1150 mime_parser(msg->cm_fields['M'],
1152 *ical_locate_part, /* callback function */
1154 (void *) &ird, /* user data */
1157 CtdlFreeMessage(msg);
1159 if (ird.cal == NULL) return;
1161 ical_add_to_freebusy(cal, ird.cal);
1163 /* Now free the memory. */
1164 icalcomponent_free(ird.cal);
1170 * Grab another user's free/busy times
1172 void ical_freebusy(char *who) {
1173 struct ctdluser usbuf;
1174 char calendar_room_name[ROOMNAMELEN];
1175 char hold_rm[ROOMNAMELEN];
1176 char *serialized_request = NULL;
1177 icalcomponent *encaps = NULL;
1178 icalcomponent *fb = NULL;
1179 int found_user = (-1);
1180 struct recptypes *recp = NULL;
1185 int config_lines = 0;
1187 /* First try an exact match. */
1188 found_user = getuser(&usbuf, who);
1190 /* If not found, try it as an unqualified email address. */
1191 if (found_user != 0) {
1193 recp = validate_recipients(buf);
1194 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1196 if (recp->num_local == 1) {
1197 found_user = getuser(&usbuf, recp->recp_local);
1203 /* If still not found, try it as an address qualified with the
1204 * primary FQDN of this Citadel node.
1206 if (found_user != 0) {
1207 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1208 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1209 recp = validate_recipients(buf);
1211 if (recp->num_local == 1) {
1212 found_user = getuser(&usbuf, recp->recp_local);
1218 /* Still not found? Try qualifying it with every domain we
1219 * might have addresses in.
1221 if (found_user != 0) {
1222 config_lines = num_tokens(inetcfg, '\n');
1223 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1224 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1225 extract_token(host, buf, 0, '|', sizeof host);
1226 extract_token(type, buf, 1, '|', sizeof type);
1228 if ( (!strcasecmp(type, "localhost"))
1229 || (!strcasecmp(type, "directory")) ) {
1230 snprintf(buf, sizeof buf, "%s@%s", who, host);
1231 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1232 recp = validate_recipients(buf);
1234 if (recp->num_local == 1) {
1235 found_user = getuser(&usbuf, recp->recp_local);
1243 if (found_user != 0) {
1244 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1248 MailboxName(calendar_room_name, sizeof calendar_room_name,
1249 &usbuf, USERCALENDARROOM);
1251 strcpy(hold_rm, CC->room.QRname); /* save current room */
1253 if (getroom(&CC->room, calendar_room_name) != 0) {
1254 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1255 getroom(&CC->room, hold_rm);
1259 /* Create a VFREEBUSY subcomponent */
1260 lprintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
1261 fb = icalcomponent_new_vfreebusy();
1263 cprintf("%d Internal error: cannot allocate memory.\n",
1264 ERROR + INTERNAL_ERROR);
1265 icalcomponent_free(encaps);
1266 getroom(&CC->room, hold_rm);
1270 /* Set the method to PUBLISH */
1271 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1273 /* Set the DTSTAMP to right now. */
1274 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1276 /* Add the user's email address as ORGANIZER */
1277 sprintf(buf, "MAILTO:%s", who);
1278 if (strchr(buf, '@') == NULL) {
1280 strcat(buf, config.c_fqdn);
1282 for (i=0; i<strlen(buf); ++i) {
1283 if (buf[i]==' ') buf[i] = '_';
1285 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1287 /* Add busy time from events */
1288 lprintf(CTDL_DEBUG, "Adding busy time from events\n");
1289 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
1290 NULL, ical_freebusy_backend, (void *)fb
1293 /* If values for DTSTART and DTEND are still not present, set them
1294 * to yesterday and tomorrow as default values.
1296 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1297 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1299 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1300 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1303 /* Put the freebusy component into the calendar component */
1304 lprintf(CTDL_DEBUG, "Encapsulating\n");
1305 encaps = ical_encapsulate_subcomponent(fb);
1306 if (encaps == NULL) {
1307 icalcomponent_free(fb);
1308 cprintf("%d Internal error: cannot allocate memory.\n",
1309 ERROR + INTERNAL_ERROR);
1310 getroom(&CC->room, hold_rm);
1314 /* Set the method to PUBLISH */
1315 lprintf(CTDL_DEBUG, "Setting method\n");
1316 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1319 lprintf(CTDL_DEBUG, "Serializing\n");
1320 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1321 icalcomponent_free(encaps); /* Don't need this anymore. */
1323 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1324 if (serialized_request != NULL) {
1325 client_write(serialized_request, strlen(serialized_request));
1326 free(serialized_request);
1330 /* Go back to the room from which we came... */
1331 getroom(&CC->room, hold_rm);
1338 * All Citadel calendar commands from the client come through here.
1340 void cmd_ical(char *argbuf)
1348 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1350 /* Allow "test" and "freebusy" subcommands without logging in. */
1352 if (!strcasecmp(subcmd, "test")) {
1353 cprintf("%d This server supports calendaring\n", CIT_OK);
1357 if (!strcasecmp(subcmd, "freebusy")) {
1358 extract_token(who, argbuf, 1, '|', sizeof who);
1363 if (!strcasecmp(subcmd, "sgi")) {
1364 CIT_ICAL->server_generated_invitations =
1365 (extract_int(argbuf, 1) ? 1 : 0) ;
1367 CIT_OK, CIT_ICAL->server_generated_invitations);
1371 if (CtdlAccessCheck(ac_logged_in)) return;
1373 if (!strcasecmp(subcmd, "respond")) {
1374 msgnum = extract_long(argbuf, 1);
1375 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1376 extract_token(action, argbuf, 3, '|', sizeof action);
1377 ical_respond(msgnum, partnum, action);
1381 if (!strcasecmp(subcmd, "handle_rsvp")) {
1382 msgnum = extract_long(argbuf, 1);
1383 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1384 extract_token(action, argbuf, 3, '|', sizeof action);
1385 ical_handle_rsvp(msgnum, partnum, action);
1389 if (!strcasecmp(subcmd, "conflicts")) {
1390 msgnum = extract_long(argbuf, 1);
1391 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1392 ical_conflicts(msgnum, partnum);
1396 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1402 * We don't know if the calendar room exists so we just create it at login
1404 void ical_create_room(void)
1409 /* Create the calendar room if it doesn't already exist */
1410 create_room(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1412 /* Set expiration policy to manual; otherwise objects will be lost! */
1413 if (lgetroom(&qr, USERCALENDARROOM)) {
1414 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1417 qr.QRep.expire_mode = EXPIRE_MANUAL;
1418 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1421 /* Set the view to a calendar view */
1422 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1423 vbuf.v_view = VIEW_CALENDAR;
1424 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1426 /* Create the tasks list room if it doesn't already exist */
1427 create_room(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1429 /* Set expiration policy to manual; otherwise objects will be lost! */
1430 if (lgetroom(&qr, USERTASKSROOM)) {
1431 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1434 qr.QRep.expire_mode = EXPIRE_MANUAL;
1435 qr.QRdefaultview = VIEW_TASKS;
1438 /* Set the view to a task list view */
1439 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1440 vbuf.v_view = VIEW_TASKS;
1441 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1443 /* Create the notes room if it doesn't already exist */
1444 create_room(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1446 /* Set expiration policy to manual; otherwise objects will be lost! */
1447 if (lgetroom(&qr, USERNOTESROOM)) {
1448 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1451 qr.QRep.expire_mode = EXPIRE_MANUAL;
1452 qr.QRdefaultview = VIEW_NOTES;
1455 /* Set the view to a notes view */
1456 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1457 vbuf.v_view = VIEW_NOTES;
1458 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1465 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1468 void ical_send_out_invitations(icalcomponent *cal) {
1469 icalcomponent *the_request = NULL;
1470 char *serialized_request = NULL;
1471 icalcomponent *encaps = NULL;
1472 char *request_message_text = NULL;
1473 struct CtdlMessage *msg = NULL;
1474 struct recptypes *valid = NULL;
1475 char attendees_string[SIZ];
1476 int num_attendees = 0;
1477 char this_attendee[256];
1478 icalproperty *attendee = NULL;
1479 char summary_string[SIZ];
1480 icalproperty *summary = NULL;
1483 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
1488 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1489 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1490 ical_send_out_invitations(
1491 icalcomponent_get_first_component(
1492 cal, ICAL_VEVENT_COMPONENT
1498 /* Clone the event */
1499 the_request = icalcomponent_new_clone(cal);
1500 if (the_request == NULL) {
1501 lprintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
1505 /* Extract the summary string -- we'll use it as the
1506 * message subject for the request
1508 strcpy(summary_string, "Meeting request");
1509 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1510 if (summary != NULL) {
1511 if (icalproperty_get_summary(summary)) {
1512 strcpy(summary_string,
1513 icalproperty_get_summary(summary) );
1517 /* Determine who the recipients of this message are (the attendees) */
1518 strcpy(attendees_string, "");
1519 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1520 if (icalproperty_get_attendee(attendee)) {
1521 safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
1522 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1523 strcpy(this_attendee, &this_attendee[7]);
1525 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
1526 snprintf(&attendees_string[strlen(attendees_string)],
1527 sizeof(attendees_string) - strlen(attendees_string),
1537 lprintf(CTDL_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1539 /* If there are no attendees, there are no invitations to send, so...
1540 * don't bother putting one together! Punch out, Maverick!
1542 if (num_attendees == 0) {
1543 icalcomponent_free(the_request);
1547 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1548 encaps = icalcomponent_new_vcalendar();
1549 if (encaps == NULL) {
1550 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1551 __FILE__, __LINE__);
1552 icalcomponent_free(the_request);
1556 /* Set the Product ID */
1557 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1559 /* Set the Version Number */
1560 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1562 /* Set the method to REQUEST */
1563 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1565 /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1566 ical_dezonify(the_request);
1568 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1569 * are responsible for "the_request"'s memory -- it will be freed
1570 * when we free "encaps".
1572 icalcomponent_add_component(encaps, the_request);
1575 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1576 icalcomponent_free(encaps); /* Don't need this anymore. */
1577 if (serialized_request == NULL) return;
1579 request_message_text = malloc(strlen(serialized_request) + SIZ);
1580 if (request_message_text != NULL) {
1581 sprintf(request_message_text,
1582 "Content-type: text/calendar\r\n\r\n%s\r\n",
1586 msg = CtdlMakeMessage(&CC->user,
1587 "", /* No single recipient here */
1588 "", /* No single recipient here */
1589 CC->room.QRname, 0, FMT_RFC822,
1591 summary_string, /* Use summary for subject */
1592 request_message_text);
1595 valid = validate_recipients(attendees_string);
1596 CtdlSubmitMsg(msg, valid, "");
1597 CtdlFreeMessage(msg);
1600 free(serialized_request);
1605 * When a calendar object is being saved, determine whether it's a VEVENT
1606 * and the user saving it is the organizer. If so, send out invitations
1607 * to any listed attendees.
1610 void ical_saving_vevent(icalcomponent *cal) {
1612 icalproperty *organizer = NULL;
1613 char organizer_string[SIZ];
1615 /* Don't send out invitations unless the client wants us to. */
1616 if (CIT_ICAL->server_generated_invitations == 0) {
1620 /* Don't send out invitations if we've been asked not to. */
1621 if (CIT_ICAL->avoid_sending_invitations > 0) {
1625 strcpy(organizer_string, "");
1627 * The VEVENT subcomponent is the one we're interested in.
1628 * Send out invitations if, and only if, this user is the Organizer.
1630 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1631 organizer = icalcomponent_get_first_property(cal,
1632 ICAL_ORGANIZER_PROPERTY);
1633 if (organizer != NULL) {
1634 if (icalproperty_get_organizer(organizer)) {
1635 strcpy(organizer_string,
1636 icalproperty_get_organizer(organizer));
1639 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1640 strcpy(organizer_string, &organizer_string[7]);
1641 striplt(organizer_string);
1643 * If the user saving the event is listed as the
1644 * organizer, then send out invitations.
1646 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
1647 ical_send_out_invitations(cal);
1652 /* If the component has subcomponents, recurse through them. */
1653 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1655 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1656 /* Recursively process subcomponent */
1657 ical_saving_vevent(c);
1665 * Back end for ical_obj_beforesave()
1666 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1667 * the summary of the event (becomes message subject),
1668 * and the start time (becomes message date/time).
1670 void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
1671 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1672 char *encoding, void *cbuserdata)
1674 icalcomponent *cal, *nested_event, *nested_todo;
1676 struct icalmessagemod *imm;
1679 imm = (struct icalmessagemod *)cbuserdata;
1681 /* If this is a text/calendar object, hunt for the UID and drop it in
1682 * the "user data" pointer for the MIME parser. When
1683 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1686 if (!strcasecmp(cbtype, "text/calendar")) {
1687 cal = icalcomponent_new_from_string(content);
1689 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1690 nested_event = icalcomponent_get_first_component(
1691 cal, ICAL_VEVENT_COMPONENT
1693 nested_todo = icalcomponent_get_first_component(
1694 cal, ICAL_VTODO_COMPONENT
1696 if (nested_event != NULL) {
1699 else if (nested_todo != NULL) {
1705 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1707 /* If there's no uid we must generate one */
1708 generate_uuid(new_uid);
1709 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
1710 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1713 strcpy(imm->uid, icalproperty_get_comment(p));
1714 /* strcpy(imm->subject, icalproperty_get_comment(p)); old aethera hack */
1716 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
1718 strcpy(imm->subject, icalproperty_get_comment(p));
1720 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
1722 imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
1724 icalcomponent_free(cal);
1734 * See if we need to prevent the object from being saved (we don't allow
1735 * MIME types other than text/calendar in "calendar" or "tasks" rooms). Also,
1736 * when saving an event to the calendar, set the message's Citadel exclusive
1737 * message ID to the UID of the object. This causes our replication checker to
1738 * automatically delete any existing instances of the same object. (Isn't
1741 * We also set the message's Subject to the event summary, and the Date/time to
1742 * the event start time.
1744 int ical_obj_beforesave(struct CtdlMessage *msg)
1748 struct icalmessagemod imm;
1750 /* First determine if this is a calendar or tasks room */
1751 if ( (CC->curr_view != VIEW_CALENDAR)
1752 &&(CC->curr_view != VIEW_TASKS) ) {
1753 return(0); /* Not a vCalendar-centric room */
1756 /* It must be an RFC822 message! */
1757 if (msg->cm_format_type != 4) {
1758 return 1; /* You tried to save a non-RFC822 message! */
1761 /* Find the Content-Type: header */
1762 p = msg->cm_fields['M'];
1765 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1766 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1767 memset(&imm, 0, sizeof(struct icalmessagemod));
1768 mime_parser(msg->cm_fields['M'],
1770 *ical_ctdl_set_exclusive_msgid,
1775 if (strlen(imm.uid) > 0) {
1776 if (msg->cm_fields['E'] != NULL) {
1777 free(msg->cm_fields['E']);
1779 msg->cm_fields['E'] = strdup(imm.uid);
1781 if (strlen(imm.subject) > 0) {
1782 if (msg->cm_fields['U'] != NULL) {
1783 free(msg->cm_fields['U']);
1785 msg->cm_fields['U'] = strdup(imm.subject);
1787 if (imm.dtstart > 0) {
1788 if (msg->cm_fields['T'] != NULL) {
1789 free(msg->cm_fields['T']);
1791 msg->cm_fields['T'] = strdup("000000000000000000");
1792 sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
1803 /* Oops! No Content-Type in this message! How'd that happen? */
1804 lprintf(CTDL_ERR, "RFC822 message with no Content-Type header!\n");
1810 * Things we need to do after saving a calendar event.
1812 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
1813 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1814 char *encoding, void *cbuserdata)
1818 /* If this is a text/calendar object, hunt for the UID and drop it in
1819 * the "user data" pointer for the MIME parser. When
1820 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1823 if (!strcasecmp(cbtype, "text/calendar")) {
1824 cal = icalcomponent_new_from_string(content);
1826 ical_saving_vevent(cal);
1827 icalcomponent_free(cal);
1834 * Things we need to do after saving a calendar event.
1836 int ical_obj_aftersave(struct CtdlMessage *msg)
1838 char roomname[ROOMNAMELEN];
1843 * If this isn't the Calendar> room, no further action is necessary.
1846 /* First determine if this is our room */
1847 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
1848 if (strcasecmp(roomname, CC->room.QRname)) {
1849 return 0; /* It's not the Calendar room. */
1852 /* Then determine content-type of the message */
1854 /* It must be an RFC822 message! */
1855 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1856 if (msg->cm_format_type != 4) return(1);
1858 /* Find the Content-Type: header */
1859 p = msg->cm_fields['M'];
1862 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1863 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1864 mime_parser(msg->cm_fields['M'],
1866 *ical_obj_aftersave_backend,
1880 /* Oops! No Content-Type in this message! How'd that happen? */
1881 lprintf(CTDL_ERR, "RFC822 message with no Content-Type header!\n");
1886 void ical_session_startup(void) {
1887 CIT_ICAL = malloc(sizeof(struct cit_ical));
1888 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
1891 void ical_session_shutdown(void) {
1897 * Back end for ical_fixed_output()
1899 void ical_fixed_output_backend(icalcomponent *cal,
1903 icalproperty *method = NULL;
1904 icalproperty_method the_method = ICAL_METHOD_NONE;
1906 struct icaltimetype t;
1910 /* Look for a method */
1911 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
1913 /* See what we need to do with this */
1914 if (method != NULL) {
1915 the_method = icalproperty_get_method(method);
1916 switch(the_method) {
1917 case ICAL_METHOD_REQUEST:
1918 cprintf("Meeting invitation\n");
1920 case ICAL_METHOD_REPLY:
1921 cprintf("Attendee's reply to your invitation\n");
1923 case ICAL_METHOD_PUBLISH:
1924 cprintf("Published event\n");
1927 cprintf("This is an unknown type of calendar item.\n");
1932 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
1934 cprintf("Summary: %s\n", (const char *)icalproperty_get_comment(p));
1937 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
1939 cprintf("Location: %s\n", (const char *)icalproperty_get_comment(p));
1943 * Only show start/end times if we're actually looking at the VEVENT
1944 * component. Otherwise it shows bogus dates for things like timezone.
1946 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1948 p = icalcomponent_get_first_property(cal,
1949 ICAL_DTSTART_PROPERTY);
1951 t = icalproperty_get_dtstart(p);
1954 cprintf("Date: %s %d, %d\n",
1955 ascmonths[t.month - 1],
1960 tt = icaltime_as_timet(t);
1961 fmt_date(buf, sizeof buf, tt, 0);
1962 cprintf("Starting date/time: %s\n", buf);
1966 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1968 t = icalproperty_get_dtend(p);
1969 tt = icaltime_as_timet(t);
1970 fmt_date(buf, sizeof buf, tt, 0);
1971 cprintf("Ending date/time: %s\n", buf);
1976 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
1978 cprintf("Description: %s\n", (const char *)icalproperty_get_comment(p));
1981 /* If the component has attendees, iterate through them. */
1982 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
1983 cprintf("Attendee: ");
1984 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
1985 if (!strncasecmp(buf, "MAILTO:", 7)) {
1987 /* screen name or email address */
1988 strcpy(buf, &buf[7]);
1990 cprintf("%s ", buf);
1995 /* If the component has subcomponents, recurse through them. */
1996 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1998 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1999 /* Recursively process subcomponent */
2000 ical_fixed_output_backend(c, recursion_level+1);
2007 * Function to output a calendar item as plain text. Nobody uses MSG0
2008 * anymore, so really this is just so we expose the vCard data to the full
2011 void ical_fixed_output(char *ptr, int len) {
2015 stringy_cal = malloc(len + 1);
2016 safestrncpy(stringy_cal, ptr, len + 1);
2017 cal = icalcomponent_new_from_string(stringy_cal);
2021 cprintf("There was an error parsing this calendar item.\n");
2026 ical_fixed_output_backend(cal, 0);
2028 /* Free the memory we obtained from libical's constructor */
2029 icalcomponent_free(cal);
2033 #endif /* CITADEL_WITH_CALENDAR_SERVICE */
2036 * Register this module with the Citadel server.
2038 char *serv_calendar_init(void)
2040 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2041 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2042 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2043 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
2044 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2045 CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2046 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2047 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);