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 CtdlMakeTempFileName(temp, sizeof temp);
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 */
328 valid = validate_recipients(organizer_string);
329 CtdlSubmitMsg(msg, valid, "");
330 CtdlFreeMessage(msg);
333 free(serialized_reply);
339 * Callback function for mime parser that hunts for calendar content types
340 * and turns them into calendar objects
342 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
343 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
346 struct ical_respond_data *ird = NULL;
348 ird = (struct ical_respond_data *) cbuserdata;
350 /* desired_partnum can be set to "_HUNT_" to have it just look for
351 * the first part with a content type of text/calendar. Otherwise
352 * we have to only process the right one.
354 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
355 if (strcasecmp(partnum, ird->desired_partnum)) {
360 if (strcasecmp(cbtype, "text/calendar")) {
364 if (ird->cal != NULL) {
365 icalcomponent_free(ird->cal);
369 ird->cal = icalcomponent_new_from_string(content);
370 if (ird->cal != NULL) {
371 ical_dezonify(ird->cal);
377 * Respond to a meeting request.
379 void ical_respond(long msgnum, char *partnum, char *action) {
380 struct CtdlMessage *msg;
381 struct ical_respond_data ird;
384 (strcasecmp(action, "accept"))
385 && (strcasecmp(action, "decline"))
387 cprintf("%d Action must be 'accept' or 'decline'\n",
388 ERROR + ILLEGAL_VALUE
393 msg = CtdlFetchMessage(msgnum, 1);
395 cprintf("%d Message %ld not found.\n",
396 ERROR + ILLEGAL_VALUE,
402 memset(&ird, 0, sizeof ird);
403 strcpy(ird.desired_partnum, partnum);
404 mime_parser(msg->cm_fields['M'],
406 *ical_locate_part, /* callback function */
408 (void *) &ird, /* user data */
412 /* We're done with the incoming message, because we now have a
413 * calendar object in memory.
415 CtdlFreeMessage(msg);
418 * Here is the real meat of this function. Handle the event.
420 if (ird.cal != NULL) {
421 /* Save this in the user's calendar if necessary */
422 if (!strcasecmp(action, "accept")) {
423 ical_add(ird.cal, 0);
426 /* Send a reply if necessary */
427 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
428 ical_send_a_reply(ird.cal, action);
431 /* Now that we've processed this message, we don't need it
432 * anymore. So delete it.
434 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 1);
436 /* Free the memory we allocated and return a response. */
437 icalcomponent_free(ird.cal);
439 cprintf("%d ok\n", CIT_OK);
443 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
447 /* should never get here */
452 * Figure out the UID of the calendar event being referred to in a
453 * REPLY object. This function is recursive.
455 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
456 icalcomponent *subcomponent;
459 /* If this object is a REPLY, then extract the UID. */
460 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
461 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
463 strcpy(uidbuf, icalproperty_get_comment(p));
467 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
468 * UID of the reply; we want the UID of the invitation being replied to.
470 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
471 subcomponent != NULL;
472 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
473 ical_learn_uid_of_reply(uidbuf, subcomponent);
479 * ical_update_my_calendar_with_reply() refers to this callback function; when we
480 * locate the message containing the calendar event we're replying to, this function
481 * gets called. It basically just sticks the message number in a supplied buffer.
483 void ical_hunt_for_event_to_update(long msgnum, void *data) {
486 msgnumptr = (long *) data;
491 struct original_event_container {
496 * Callback function for mime parser that hunts for calendar content types
497 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
498 * to fetch the object being updated)
500 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
501 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
504 struct original_event_container *oec = NULL;
506 if (strcasecmp(cbtype, "text/calendar")) {
509 oec = (struct original_event_container *) cbuserdata;
510 if (oec->c != NULL) {
511 icalcomponent_free(oec->c);
513 oec->c = icalcomponent_new_from_string(content);
518 * Merge updated attendee information from a REPLY into an existing event.
520 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
522 icalproperty *e_attendee, *r_attendee;
524 /* First things first. If we're not looking at a VEVENT component,
525 * recurse through subcomponents until we find one.
527 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
528 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
530 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
531 ical_merge_attendee_reply(c, reply);
536 /* Now do the same thing with the reply.
538 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
539 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
541 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
542 ical_merge_attendee_reply(event, c);
547 /* Clone the reply, because we're going to rip its guts out. */
548 reply = icalcomponent_new_clone(reply);
550 /* At this point we're looking at the correct subcomponents.
551 * Iterate through the attendees looking for a match.
554 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
556 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
558 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
560 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
562 /* Check to see if these two attendees match...
565 icalproperty_get_attendee(e_attendee),
566 icalproperty_get_attendee(r_attendee)
568 /* ...and if they do, remove the attendee from the event
569 * and replace it with the attendee from the reply. (The
570 * reply's copy will have the same address, but an updated
573 icalcomponent_remove_property(event, e_attendee);
574 icalproperty_free(e_attendee);
575 icalcomponent_remove_property(reply, r_attendee);
576 icalcomponent_add_property(event, r_attendee);
578 /* Since we diddled both sets of attendees, we have to start
579 * the iteration over again. This will not create an infinite
580 * loop because we removed the attendee from the reply. (That's
581 * why we cloned the reply, and that's what we mean by "ripping
590 /* Free the *clone* of the reply. */
591 icalcomponent_free(reply);
598 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
599 * calendar event. The object has already been deserialized for us; all
600 * we have to do here is hunt for the event in our calendar, merge in the
601 * updated attendee status, and save it again.
603 * This function returns 0 on success, 1 if the event was not found in the
604 * user's calendar, or 2 if an internal error occurred.
606 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
608 char hold_rm[ROOMNAMELEN];
609 long msgnum_being_replaced = 0;
610 struct CtdlMessage *template = NULL;
611 struct CtdlMessage *msg;
612 struct original_event_container oec;
613 icalcomponent *original_event;
614 char *serialized_event = NULL;
615 char roomname[ROOMNAMELEN];
616 char *message_text = NULL;
618 /* Figure out just what event it is we're dealing with */
619 strcpy(uid, "--==<< InVaLiD uId >>==--");
620 ical_learn_uid_of_reply(uid, cal);
621 lprintf(CTDL_DEBUG, "UID of event being replied to is <%s>\n", uid);
623 strcpy(hold_rm, CC->room.QRname); /* save current room */
625 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
626 getroom(&CC->room, hold_rm);
627 lprintf(CTDL_CRIT, "cannot get user calendar room\n");
632 * Pound through the user's calendar looking for a message with
633 * the Citadel EUID set to the value we're looking for. Since
634 * Citadel always sets the message EUID to the vCalendar UID of
635 * the event, this will work.
637 template = (struct CtdlMessage *)
638 malloc(sizeof(struct CtdlMessage));
639 memset(template, 0, sizeof(struct CtdlMessage));
640 template->cm_fields['E'] = strdup(uid);
641 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
642 template, ical_hunt_for_event_to_update, &msgnum_being_replaced);
643 CtdlFreeMessage(template);
644 getroom(&CC->room, hold_rm); /* return to saved room */
646 lprintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
647 if (msgnum_being_replaced == 0) {
648 return(1); /* no calendar event found */
651 /* Now we know the ID of the message containing the event being updated.
652 * We don't actually have to delete it; that'll get taken care of by the
653 * server when we save another event with the same UID. This just gives
654 * us the ability to load the event into memory so we can diddle the
657 msg = CtdlFetchMessage(msgnum_being_replaced, 1);
659 return(2); /* internal error */
662 mime_parser(msg->cm_fields['M'],
664 *ical_locate_original_event, /* callback function */
666 &oec, /* user data */
669 CtdlFreeMessage(msg);
671 original_event = oec.c;
672 if (original_event == NULL) {
673 lprintf(CTDL_ERR, "ERROR: Original_component is NULL.\n");
677 /* Merge the attendee's updated status into the event */
678 ical_merge_attendee_reply(original_event, cal);
681 serialized_event = strdup(icalcomponent_as_ical_string(original_event));
682 icalcomponent_free(original_event); /* Don't need this anymore. */
683 if (serialized_event == NULL) return(2);
685 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
687 message_text = malloc(strlen(serialized_event) + SIZ);
688 if (message_text != NULL) {
689 sprintf(message_text,
690 "Content-type: text/calendar\r\n\r\n%s\r\n",
694 msg = CtdlMakeMessage(&CC->user,
695 "", /* No recipient */
696 "", /* No recipient */
705 CIT_ICAL->avoid_sending_invitations = 1;
706 CtdlSubmitMsg(msg, NULL, roomname);
707 CtdlFreeMessage(msg);
708 CIT_ICAL->avoid_sending_invitations = 0;
711 free(serialized_event);
717 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
718 * simply extracts the calendar object from the message, deserializes it, and
719 * passes it up to ical_update_my_calendar_with_reply() for processing.
721 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
722 struct CtdlMessage *msg;
723 struct ical_respond_data ird;
727 (strcasecmp(action, "update"))
728 && (strcasecmp(action, "ignore"))
730 cprintf("%d Action must be 'update' or 'ignore'\n",
731 ERROR + ILLEGAL_VALUE
736 msg = CtdlFetchMessage(msgnum, 1);
738 cprintf("%d Message %ld not found.\n",
739 ERROR + ILLEGAL_VALUE,
745 memset(&ird, 0, sizeof ird);
746 strcpy(ird.desired_partnum, partnum);
747 mime_parser(msg->cm_fields['M'],
749 *ical_locate_part, /* callback function */
751 (void *) &ird, /* user data */
755 /* We're done with the incoming message, because we now have a
756 * calendar object in memory.
758 CtdlFreeMessage(msg);
761 * Here is the real meat of this function. Handle the event.
763 if (ird.cal != NULL) {
764 /* Update the user's calendar if necessary */
765 if (!strcasecmp(action, "update")) {
766 ret = ical_update_my_calendar_with_reply(ird.cal);
768 cprintf("%d Your calendar has been updated with this reply.\n",
772 cprintf("%d This event does not exist in your calendar.\n",
773 ERROR + FILE_NOT_FOUND);
776 cprintf("%d An internal error occurred.\n",
777 ERROR + INTERNAL_ERROR);
781 cprintf("%d This reply has been ignored.\n", CIT_OK);
784 /* Now that we've processed this message, we don't need it
785 * anymore. So delete it. (Maybe make this optional?)
787 CtdlDeleteMessages(CC->room.QRname, msgnum, "", 1);
789 /* Free the memory we allocated and return a response. */
790 icalcomponent_free(ird.cal);
795 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
799 /* should never get here */
804 * Search for a property in both the top level and in a VEVENT subcomponent
806 icalproperty *ical_ctdl_get_subprop(
808 icalproperty_kind which_prop
813 p = icalcomponent_get_first_property(cal, which_prop);
815 c = icalcomponent_get_first_component(cal,
816 ICAL_VEVENT_COMPONENT);
818 p = icalcomponent_get_first_property(c, which_prop);
826 * Check to see if two events overlap. Returns nonzero if they do.
827 * (This function is used in both Citadel and WebCit. If you change it in
828 * one place, change it in the other. Better yet, put it in a library.)
830 int ical_ctdl_is_overlap(
831 struct icaltimetype t1start,
832 struct icaltimetype t1end,
833 struct icaltimetype t2start,
834 struct icaltimetype t2end
837 if (icaltime_is_null_time(t1start)) return(0);
838 if (icaltime_is_null_time(t2start)) return(0);
840 /* First, check for all-day events */
841 if (t1start.is_date) {
842 if (!icaltime_compare_date_only(t1start, t2start)) {
845 if (!icaltime_is_null_time(t2end)) {
846 if (!icaltime_compare_date_only(t1start, t2end)) {
852 if (t2start.is_date) {
853 if (!icaltime_compare_date_only(t2start, t1start)) {
856 if (!icaltime_is_null_time(t1end)) {
857 if (!icaltime_compare_date_only(t2start, t1end)) {
863 /* Now check for overlaps using date *and* time. */
865 /* First, bail out if either event 1 or event 2 is missing end time. */
866 if (icaltime_is_null_time(t1end)) return(0);
867 if (icaltime_is_null_time(t2end)) return(0);
869 /* If event 1 ends before event 2 starts, we're in the clear. */
870 if (icaltime_compare(t1end, t2start) <= 0) return(0);
872 /* If event 2 ends before event 1 starts, we're also ok. */
873 if (icaltime_compare(t2end, t1start) <= 0) return(0);
875 /* Otherwise, they overlap. */
882 * Backend for ical_hunt_for_conflicts()
884 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
886 struct CtdlMessage *msg;
887 struct ical_respond_data ird;
888 struct icaltimetype t1start, t1end, t2start, t2end;
890 char conflict_event_uid[SIZ];
891 char conflict_event_summary[SIZ];
892 char compare_uid[SIZ];
894 cal = (icalcomponent *)data;
895 strcpy(compare_uid, "");
896 strcpy(conflict_event_uid, "");
897 strcpy(conflict_event_summary, "");
899 msg = CtdlFetchMessage(msgnum, 1);
900 if (msg == NULL) return;
901 memset(&ird, 0, sizeof ird);
902 strcpy(ird.desired_partnum, "_HUNT_");
903 mime_parser(msg->cm_fields['M'],
905 *ical_locate_part, /* callback function */
907 (void *) &ird, /* user data */
910 CtdlFreeMessage(msg);
912 if (ird.cal == NULL) return;
914 t1start = icaltime_null_time();
915 t1end = icaltime_null_time();
916 t2start = icaltime_null_time();
917 t1end = icaltime_null_time();
919 /* Now compare cal to ird.cal */
920 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
921 if (p == NULL) return;
922 if (p != NULL) t2start = icalproperty_get_dtstart(p);
924 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
925 if (p != NULL) t2end = icalproperty_get_dtend(p);
927 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
928 if (p == NULL) return;
929 if (p != NULL) t1start = icalproperty_get_dtstart(p);
931 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
932 if (p != NULL) t1end = icalproperty_get_dtend(p);
934 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
936 strcpy(compare_uid, icalproperty_get_comment(p));
939 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
941 strcpy(conflict_event_uid, icalproperty_get_comment(p));
944 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
946 strcpy(conflict_event_summary, icalproperty_get_comment(p));
949 icalcomponent_free(ird.cal);
951 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
952 cprintf("%ld||%s|%s|%d|\n",
955 conflict_event_summary,
956 ( ((strlen(compare_uid)>0)
957 &&(!strcasecmp(compare_uid,
958 conflict_event_uid))) ? 1 : 0
967 * Phase 2 of "hunt for conflicts" operation.
968 * At this point we have a calendar object which represents the VEVENT that
969 * we're considering adding to the calendar. Now hunt through the user's
970 * calendar room, and output zero or more existing VEVENTs which conflict
973 void ical_hunt_for_conflicts(icalcomponent *cal) {
974 char hold_rm[ROOMNAMELEN];
976 strcpy(hold_rm, CC->room.QRname); /* save current room */
978 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
979 getroom(&CC->room, hold_rm);
980 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
984 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
986 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
988 ical_hunt_for_conflicts_backend,
993 getroom(&CC->room, hold_rm); /* return to saved room */
1000 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
1002 void ical_conflicts(long msgnum, char *partnum) {
1003 struct CtdlMessage *msg;
1004 struct ical_respond_data ird;
1006 msg = CtdlFetchMessage(msgnum, 1);
1008 cprintf("%d Message %ld not found.\n",
1009 ERROR + ILLEGAL_VALUE,
1015 memset(&ird, 0, sizeof ird);
1016 strcpy(ird.desired_partnum, partnum);
1017 mime_parser(msg->cm_fields['M'],
1019 *ical_locate_part, /* callback function */
1021 (void *) &ird, /* user data */
1025 CtdlFreeMessage(msg);
1027 if (ird.cal != NULL) {
1028 ical_hunt_for_conflicts(ird.cal);
1029 icalcomponent_free(ird.cal);
1033 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1037 /* should never get here */
1043 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1045 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1048 struct icalperiodtype my_period;
1050 if (cal == NULL) return;
1051 my_period = icalperiodtype_null_period();
1053 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1054 ical_add_to_freebusy(fb,
1055 icalcomponent_get_first_component(
1056 cal, ICAL_VEVENT_COMPONENT
1064 /* If this event is not opaque, the user isn't publishing it as
1065 * busy time, so don't bother doing anything else.
1067 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1069 v = icalproperty_get_value(p);
1071 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1077 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1078 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1080 my_period.start = icalproperty_get_dtstart(p);
1083 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1085 my_period.end = icalproperty_get_dtstart(p);
1089 icalcomponent_add_property(fb,
1090 icalproperty_new_freebusy(my_period)
1093 /* Make sure the DTSTART property of the freebusy *list* is set to
1094 * the DTSTART property of the *earliest event*.
1096 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1098 icalcomponent_set_dtstart(fb,
1099 icalcomponent_get_dtstart(cal) );
1102 if (icaltime_compare(
1103 icalcomponent_get_dtstart(cal),
1104 icalcomponent_get_dtstart(fb)
1106 icalcomponent_set_dtstart(fb,
1107 icalcomponent_get_dtstart(cal) );
1111 /* Make sure the DTEND property of the freebusy *list* is set to
1112 * the DTEND property of the *latest event*.
1114 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1116 icalcomponent_set_dtend(fb,
1117 icalcomponent_get_dtend(cal) );
1120 if (icaltime_compare(
1121 icalcomponent_get_dtend(cal),
1122 icalcomponent_get_dtend(fb)
1124 icalcomponent_set_dtend(fb,
1125 icalcomponent_get_dtend(cal) );
1134 * Backend for ical_freebusy()
1136 * This function simply loads the messages in the user's calendar room,
1137 * which contain VEVENTs, then strips them of all non-freebusy data, and
1138 * adds them to the supplied VCALENDAR.
1141 void ical_freebusy_backend(long msgnum, void *data) {
1143 struct CtdlMessage *msg;
1144 struct ical_respond_data ird;
1146 cal = (icalcomponent *)data;
1148 msg = CtdlFetchMessage(msgnum, 1);
1149 if (msg == NULL) return;
1150 memset(&ird, 0, sizeof ird);
1151 strcpy(ird.desired_partnum, "_HUNT_");
1152 mime_parser(msg->cm_fields['M'],
1154 *ical_locate_part, /* callback function */
1156 (void *) &ird, /* user data */
1159 CtdlFreeMessage(msg);
1161 if (ird.cal == NULL) return;
1163 ical_add_to_freebusy(cal, ird.cal);
1165 /* Now free the memory. */
1166 icalcomponent_free(ird.cal);
1172 * Grab another user's free/busy times
1174 void ical_freebusy(char *who) {
1175 struct ctdluser usbuf;
1176 char calendar_room_name[ROOMNAMELEN];
1177 char hold_rm[ROOMNAMELEN];
1178 char *serialized_request = NULL;
1179 icalcomponent *encaps = NULL;
1180 icalcomponent *fb = NULL;
1181 int found_user = (-1);
1182 struct recptypes *recp = NULL;
1187 int config_lines = 0;
1189 /* First try an exact match. */
1190 found_user = getuser(&usbuf, who);
1192 /* If not found, try it as an unqualified email address. */
1193 if (found_user != 0) {
1195 recp = validate_recipients(buf);
1196 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1198 if (recp->num_local == 1) {
1199 found_user = getuser(&usbuf, recp->recp_local);
1205 /* If still not found, try it as an address qualified with the
1206 * primary FQDN of this Citadel node.
1208 if (found_user != 0) {
1209 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1210 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1211 recp = validate_recipients(buf);
1213 if (recp->num_local == 1) {
1214 found_user = getuser(&usbuf, recp->recp_local);
1220 /* Still not found? Try qualifying it with every domain we
1221 * might have addresses in.
1223 if (found_user != 0) {
1224 config_lines = num_tokens(inetcfg, '\n');
1225 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1226 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1227 extract_token(host, buf, 0, '|', sizeof host);
1228 extract_token(type, buf, 1, '|', sizeof type);
1230 if ( (!strcasecmp(type, "localhost"))
1231 || (!strcasecmp(type, "directory")) ) {
1232 snprintf(buf, sizeof buf, "%s@%s", who, host);
1233 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1234 recp = validate_recipients(buf);
1236 if (recp->num_local == 1) {
1237 found_user = getuser(&usbuf, recp->recp_local);
1245 if (found_user != 0) {
1246 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1250 MailboxName(calendar_room_name, sizeof calendar_room_name,
1251 &usbuf, USERCALENDARROOM);
1253 strcpy(hold_rm, CC->room.QRname); /* save current room */
1255 if (getroom(&CC->room, calendar_room_name) != 0) {
1256 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1257 getroom(&CC->room, hold_rm);
1261 /* Create a VFREEBUSY subcomponent */
1262 lprintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
1263 fb = icalcomponent_new_vfreebusy();
1265 cprintf("%d Internal error: cannot allocate memory.\n",
1266 ERROR + INTERNAL_ERROR);
1267 icalcomponent_free(encaps);
1268 getroom(&CC->room, hold_rm);
1272 /* Set the method to PUBLISH */
1273 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1275 /* Set the DTSTAMP to right now. */
1276 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1278 /* Add the user's email address as ORGANIZER */
1279 sprintf(buf, "MAILTO:%s", who);
1280 if (strchr(buf, '@') == NULL) {
1282 strcat(buf, config.c_fqdn);
1284 for (i=0; i<strlen(buf); ++i) {
1285 if (buf[i]==' ') buf[i] = '_';
1287 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1289 /* Add busy time from events */
1290 lprintf(CTDL_DEBUG, "Adding busy time from events\n");
1291 CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
1292 NULL, ical_freebusy_backend, (void *)fb
1295 /* If values for DTSTART and DTEND are still not present, set them
1296 * to yesterday and tomorrow as default values.
1298 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1299 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1301 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1302 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1305 /* Put the freebusy component into the calendar component */
1306 lprintf(CTDL_DEBUG, "Encapsulating\n");
1307 encaps = ical_encapsulate_subcomponent(fb);
1308 if (encaps == NULL) {
1309 icalcomponent_free(fb);
1310 cprintf("%d Internal error: cannot allocate memory.\n",
1311 ERROR + INTERNAL_ERROR);
1312 getroom(&CC->room, hold_rm);
1316 /* Set the method to PUBLISH */
1317 lprintf(CTDL_DEBUG, "Setting method\n");
1318 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1321 lprintf(CTDL_DEBUG, "Serializing\n");
1322 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1323 icalcomponent_free(encaps); /* Don't need this anymore. */
1325 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1326 if (serialized_request != NULL) {
1327 client_write(serialized_request, strlen(serialized_request));
1328 free(serialized_request);
1332 /* Go back to the room from which we came... */
1333 getroom(&CC->room, hold_rm);
1340 * All Citadel calendar commands from the client come through here.
1342 void cmd_ical(char *argbuf)
1350 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1352 /* Allow "test" and "freebusy" subcommands without logging in. */
1354 if (!strcasecmp(subcmd, "test")) {
1355 cprintf("%d This server supports calendaring\n", CIT_OK);
1359 if (!strcasecmp(subcmd, "freebusy")) {
1360 extract_token(who, argbuf, 1, '|', sizeof who);
1365 if (!strcasecmp(subcmd, "sgi")) {
1366 CIT_ICAL->server_generated_invitations =
1367 (extract_int(argbuf, 1) ? 1 : 0) ;
1369 CIT_OK, CIT_ICAL->server_generated_invitations);
1373 if (CtdlAccessCheck(ac_logged_in)) return;
1375 if (!strcasecmp(subcmd, "respond")) {
1376 msgnum = extract_long(argbuf, 1);
1377 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1378 extract_token(action, argbuf, 3, '|', sizeof action);
1379 ical_respond(msgnum, partnum, action);
1383 if (!strcasecmp(subcmd, "handle_rsvp")) {
1384 msgnum = extract_long(argbuf, 1);
1385 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1386 extract_token(action, argbuf, 3, '|', sizeof action);
1387 ical_handle_rsvp(msgnum, partnum, action);
1391 if (!strcasecmp(subcmd, "conflicts")) {
1392 msgnum = extract_long(argbuf, 1);
1393 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1394 ical_conflicts(msgnum, partnum);
1398 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1404 * We don't know if the calendar room exists so we just create it at login
1406 void ical_create_room(void)
1411 /* Create the calendar room if it doesn't already exist */
1412 create_room(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1414 /* Set expiration policy to manual; otherwise objects will be lost! */
1415 if (lgetroom(&qr, USERCALENDARROOM)) {
1416 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1419 qr.QRep.expire_mode = EXPIRE_MANUAL;
1420 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1423 /* Set the view to a calendar view */
1424 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1425 vbuf.v_view = VIEW_CALENDAR;
1426 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1428 /* Create the tasks list room if it doesn't already exist */
1429 create_room(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1431 /* Set expiration policy to manual; otherwise objects will be lost! */
1432 if (lgetroom(&qr, USERTASKSROOM)) {
1433 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1436 qr.QRep.expire_mode = EXPIRE_MANUAL;
1437 qr.QRdefaultview = VIEW_TASKS;
1440 /* Set the view to a task list view */
1441 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1442 vbuf.v_view = VIEW_TASKS;
1443 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1445 /* Create the notes room if it doesn't already exist */
1446 create_room(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1448 /* Set expiration policy to manual; otherwise objects will be lost! */
1449 if (lgetroom(&qr, USERNOTESROOM)) {
1450 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1453 qr.QRep.expire_mode = EXPIRE_MANUAL;
1454 qr.QRdefaultview = VIEW_NOTES;
1457 /* Set the view to a notes view */
1458 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1459 vbuf.v_view = VIEW_NOTES;
1460 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1467 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1470 void ical_send_out_invitations(icalcomponent *cal) {
1471 icalcomponent *the_request = NULL;
1472 char *serialized_request = NULL;
1473 icalcomponent *encaps = NULL;
1474 char *request_message_text = NULL;
1475 struct CtdlMessage *msg = NULL;
1476 struct recptypes *valid = NULL;
1477 char attendees_string[SIZ];
1478 int num_attendees = 0;
1479 char this_attendee[256];
1480 icalproperty *attendee = NULL;
1481 char summary_string[SIZ];
1482 icalproperty *summary = NULL;
1485 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
1490 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1491 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1492 ical_send_out_invitations(
1493 icalcomponent_get_first_component(
1494 cal, ICAL_VEVENT_COMPONENT
1500 /* Clone the event */
1501 the_request = icalcomponent_new_clone(cal);
1502 if (the_request == NULL) {
1503 lprintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
1507 /* Extract the summary string -- we'll use it as the
1508 * message subject for the request
1510 strcpy(summary_string, "Meeting request");
1511 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1512 if (summary != NULL) {
1513 if (icalproperty_get_summary(summary)) {
1514 strcpy(summary_string,
1515 icalproperty_get_summary(summary) );
1519 /* Determine who the recipients of this message are (the attendees) */
1520 strcpy(attendees_string, "");
1521 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1522 if (icalproperty_get_attendee(attendee)) {
1523 safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
1524 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1525 strcpy(this_attendee, &this_attendee[7]);
1527 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
1528 snprintf(&attendees_string[strlen(attendees_string)],
1529 sizeof(attendees_string) - strlen(attendees_string),
1539 lprintf(CTDL_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1541 /* If there are no attendees, there are no invitations to send, so...
1542 * don't bother putting one together! Punch out, Maverick!
1544 if (num_attendees == 0) {
1545 icalcomponent_free(the_request);
1549 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1550 encaps = icalcomponent_new_vcalendar();
1551 if (encaps == NULL) {
1552 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1553 __FILE__, __LINE__);
1554 icalcomponent_free(the_request);
1558 /* Set the Product ID */
1559 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1561 /* Set the Version Number */
1562 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1564 /* Set the method to REQUEST */
1565 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1567 /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1568 ical_dezonify(the_request);
1570 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1571 * are responsible for "the_request"'s memory -- it will be freed
1572 * when we free "encaps".
1574 icalcomponent_add_component(encaps, the_request);
1577 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1578 icalcomponent_free(encaps); /* Don't need this anymore. */
1579 if (serialized_request == NULL) return;
1581 request_message_text = malloc(strlen(serialized_request) + SIZ);
1582 if (request_message_text != NULL) {
1583 sprintf(request_message_text,
1584 "Content-type: text/calendar\r\n\r\n%s\r\n",
1588 msg = CtdlMakeMessage(&CC->user,
1589 "", /* No single recipient here */
1590 "", /* No single recipient here */
1591 CC->room.QRname, 0, FMT_RFC822,
1593 summary_string, /* Use summary for subject */
1595 request_message_text);
1598 valid = validate_recipients(attendees_string);
1599 CtdlSubmitMsg(msg, valid, "");
1600 CtdlFreeMessage(msg);
1603 free(serialized_request);
1608 * When a calendar object is being saved, determine whether it's a VEVENT
1609 * and the user saving it is the organizer. If so, send out invitations
1610 * to any listed attendees.
1613 void ical_saving_vevent(icalcomponent *cal) {
1615 icalproperty *organizer = NULL;
1616 char organizer_string[SIZ];
1618 /* Don't send out invitations unless the client wants us to. */
1619 if (CIT_ICAL->server_generated_invitations == 0) {
1623 /* Don't send out invitations if we've been asked not to. */
1624 if (CIT_ICAL->avoid_sending_invitations > 0) {
1628 strcpy(organizer_string, "");
1630 * The VEVENT subcomponent is the one we're interested in.
1631 * Send out invitations if, and only if, this user is the Organizer.
1633 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1634 organizer = icalcomponent_get_first_property(cal,
1635 ICAL_ORGANIZER_PROPERTY);
1636 if (organizer != NULL) {
1637 if (icalproperty_get_organizer(organizer)) {
1638 strcpy(organizer_string,
1639 icalproperty_get_organizer(organizer));
1642 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1643 strcpy(organizer_string, &organizer_string[7]);
1644 striplt(organizer_string);
1646 * If the user saving the event is listed as the
1647 * organizer, then send out invitations.
1649 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
1650 ical_send_out_invitations(cal);
1655 /* If the component has subcomponents, recurse through them. */
1656 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1658 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1659 /* Recursively process subcomponent */
1660 ical_saving_vevent(c);
1668 * Back end for ical_obj_beforesave()
1669 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1670 * the summary of the event (becomes message subject),
1671 * and the start time (becomes message date/time).
1673 void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
1674 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1675 char *encoding, void *cbuserdata)
1677 icalcomponent *cal, *nested_event, *nested_todo;
1679 struct icalmessagemod *imm;
1682 imm = (struct icalmessagemod *)cbuserdata;
1684 /* If this is a text/calendar object, hunt for the UID and drop it in
1685 * the "user data" pointer for the MIME parser. When
1686 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1689 if (!strcasecmp(cbtype, "text/calendar")) {
1690 cal = icalcomponent_new_from_string(content);
1692 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1693 nested_event = icalcomponent_get_first_component(
1694 cal, ICAL_VEVENT_COMPONENT
1696 nested_todo = icalcomponent_get_first_component(
1697 cal, ICAL_VTODO_COMPONENT
1699 if (nested_event != NULL) {
1702 else if (nested_todo != NULL) {
1708 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1710 /* If there's no uid we must generate one */
1711 generate_uuid(new_uid);
1712 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
1713 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1716 strcpy(imm->uid, icalproperty_get_comment(p));
1717 /* strcpy(imm->subject, icalproperty_get_comment(p)); old aethera hack */
1719 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
1721 strcpy(imm->subject, icalproperty_get_comment(p));
1723 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
1725 imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
1727 icalcomponent_free(cal);
1737 * See if we need to prevent the object from being saved (we don't allow
1738 * MIME types other than text/calendar in "calendar" or "tasks" rooms). Also,
1739 * when saving an event to the calendar, set the message's Citadel exclusive
1740 * message ID to the UID of the object. This causes our replication checker to
1741 * automatically delete any existing instances of the same object. (Isn't
1744 * We also set the message's Subject to the event summary, and the Date/time to
1745 * the event start time.
1747 int ical_obj_beforesave(struct CtdlMessage *msg)
1751 struct icalmessagemod imm;
1753 /* First determine if this is a calendar or tasks room */
1754 if ( (CC->curr_view != VIEW_CALENDAR)
1755 &&(CC->curr_view != VIEW_TASKS) ) {
1756 return(0); /* Not a vCalendar-centric room */
1759 /* It must be an RFC822 message! */
1760 if (msg->cm_format_type != 4) {
1761 return 1; /* You tried to save a non-RFC822 message! */
1764 /* Find the Content-Type: header */
1765 p = msg->cm_fields['M'];
1768 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1769 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1770 memset(&imm, 0, sizeof(struct icalmessagemod));
1771 mime_parser(msg->cm_fields['M'],
1773 *ical_ctdl_set_exclusive_msgid,
1778 if (strlen(imm.uid) > 0) {
1779 if (msg->cm_fields['E'] != NULL) {
1780 free(msg->cm_fields['E']);
1782 msg->cm_fields['E'] = strdup(imm.uid);
1784 if (strlen(imm.subject) > 0) {
1785 if (msg->cm_fields['U'] != NULL) {
1786 free(msg->cm_fields['U']);
1788 msg->cm_fields['U'] = strdup(imm.subject);
1790 if (imm.dtstart > 0) {
1791 if (msg->cm_fields['T'] != NULL) {
1792 free(msg->cm_fields['T']);
1794 msg->cm_fields['T'] = strdup("000000000000000000");
1795 sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
1806 /* Oops! No Content-Type in this message! How'd that happen? */
1807 lprintf(CTDL_ERR, "RFC822 message with no Content-Type header!\n");
1813 * Things we need to do after saving a calendar event.
1815 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
1816 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1817 char *encoding, void *cbuserdata)
1821 /* If this is a text/calendar object, hunt for the UID and drop it in
1822 * the "user data" pointer for the MIME parser. When
1823 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1826 if (!strcasecmp(cbtype, "text/calendar")) {
1827 cal = icalcomponent_new_from_string(content);
1829 ical_saving_vevent(cal);
1830 icalcomponent_free(cal);
1837 * Things we need to do after saving a calendar event.
1839 int ical_obj_aftersave(struct CtdlMessage *msg)
1841 char roomname[ROOMNAMELEN];
1846 * If this isn't the Calendar> room, no further action is necessary.
1849 /* First determine if this is our room */
1850 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
1851 if (strcasecmp(roomname, CC->room.QRname)) {
1852 return 0; /* It's not the Calendar room. */
1855 /* Then determine content-type of the message */
1857 /* It must be an RFC822 message! */
1858 /* FIXME: Not handling MIME multipart messages; implement with IMIP */
1859 if (msg->cm_format_type != 4) return(1);
1861 /* Find the Content-Type: header */
1862 p = msg->cm_fields['M'];
1865 if (!strncasecmp(p, "Content-Type: ", 14)) { /* Found it */
1866 if (!strncasecmp(p + 14, "text/calendar", 13)) {
1867 mime_parser(msg->cm_fields['M'],
1869 *ical_obj_aftersave_backend,
1883 /* Oops! No Content-Type in this message! How'd that happen? */
1884 lprintf(CTDL_ERR, "RFC822 message with no Content-Type header!\n");
1889 void ical_session_startup(void) {
1890 CIT_ICAL = malloc(sizeof(struct cit_ical));
1891 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
1894 void ical_session_shutdown(void) {
1900 * Back end for ical_fixed_output()
1902 void ical_fixed_output_backend(icalcomponent *cal,
1906 icalproperty *method = NULL;
1907 icalproperty_method the_method = ICAL_METHOD_NONE;
1909 struct icaltimetype t;
1913 /* Look for a method */
1914 method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
1916 /* See what we need to do with this */
1917 if (method != NULL) {
1918 the_method = icalproperty_get_method(method);
1919 switch(the_method) {
1920 case ICAL_METHOD_REQUEST:
1921 cprintf("Meeting invitation\n");
1923 case ICAL_METHOD_REPLY:
1924 cprintf("Attendee's reply to your invitation\n");
1926 case ICAL_METHOD_PUBLISH:
1927 cprintf("Published event\n");
1930 cprintf("This is an unknown type of calendar item.\n");
1935 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
1937 cprintf("Summary: %s\n", (const char *)icalproperty_get_comment(p));
1940 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
1942 cprintf("Location: %s\n", (const char *)icalproperty_get_comment(p));
1946 * Only show start/end times if we're actually looking at the VEVENT
1947 * component. Otherwise it shows bogus dates for things like timezone.
1949 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1951 p = icalcomponent_get_first_property(cal,
1952 ICAL_DTSTART_PROPERTY);
1954 t = icalproperty_get_dtstart(p);
1957 cprintf("Date: %s %d, %d\n",
1958 ascmonths[t.month - 1],
1963 tt = icaltime_as_timet(t);
1964 fmt_date(buf, sizeof buf, tt, 0);
1965 cprintf("Starting date/time: %s\n", buf);
1969 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1971 t = icalproperty_get_dtend(p);
1972 tt = icaltime_as_timet(t);
1973 fmt_date(buf, sizeof buf, tt, 0);
1974 cprintf("Ending date/time: %s\n", buf);
1979 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
1981 cprintf("Description: %s\n", (const char *)icalproperty_get_comment(p));
1984 /* If the component has attendees, iterate through them. */
1985 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
1986 cprintf("Attendee: ");
1987 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
1988 if (!strncasecmp(buf, "MAILTO:", 7)) {
1990 /* screen name or email address */
1991 strcpy(buf, &buf[7]);
1993 cprintf("%s ", buf);
1998 /* If the component has subcomponents, recurse through them. */
1999 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2001 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2002 /* Recursively process subcomponent */
2003 ical_fixed_output_backend(c, recursion_level+1);
2010 * Function to output a calendar item as plain text. Nobody uses MSG0
2011 * anymore, so really this is just so we expose the vCard data to the full
2014 void ical_fixed_output(char *ptr, int len) {
2018 stringy_cal = malloc(len + 1);
2019 safestrncpy(stringy_cal, ptr, len + 1);
2020 cal = icalcomponent_new_from_string(stringy_cal);
2024 cprintf("There was an error parsing this calendar item.\n");
2029 ical_fixed_output_backend(cal, 0);
2031 /* Free the memory we obtained from libical's constructor */
2032 icalcomponent_free(cal);
2036 #endif /* CITADEL_WITH_CALENDAR_SERVICE */
2039 * Register this module with the Citadel server.
2041 char *serv_calendar_init(void)
2043 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2044 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2045 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2046 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
2047 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2048 CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2049 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2050 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);