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"
31 #include "mime_parser.h"
32 #include "internet_addressing.h"
33 #include "serv_calendar.h"
34 #include "euidindex.h"
35 #include "ctdl_module.h"
37 #ifdef CITADEL_WITH_CALENDAR_SERVICE
40 #include "ical_dezonify.h"
44 struct ical_respond_data {
45 char desired_partnum[SIZ];
51 * Utility function to create a new VCALENDAR component with some of the
52 * required fields already set the way we like them.
54 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
55 icalcomponent *encaps;
57 encaps = icalcomponent_new_vcalendar();
59 lprintf(CTDL_CRIT, "Error at %s:%d - could not allocate component!\n",
64 /* Set the Product ID */
65 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
67 /* Set the Version Number */
68 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
75 * Utility function to encapsulate a subcomponent into a full VCALENDAR
77 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
78 icalcomponent *encaps;
80 /* If we're already looking at a full VCALENDAR component,
81 * don't bother ... just return itself.
83 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
87 /* Encapsulate the VEVENT component into a complete VCALENDAR */
88 encaps = icalcomponent_new_citadel_vcalendar();
89 if (encaps == NULL) return NULL;
91 /* Encapsulate the subcomponent inside */
92 icalcomponent_add_component(encaps, subcomp);
94 /* Convert all timestamps to UTC so we don't have to deal with
95 * stupid VTIMEZONE crap.
97 ical_dezonify(encaps);
99 /* Return the object we just created. */
107 * 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" or "tentative".
189 void ical_send_a_reply(icalcomponent *request, char *action) {
190 icalcomponent *the_reply = NULL;
191 icalcomponent *vevent = NULL;
192 icalproperty *attendee = NULL;
193 char attendee_string[SIZ];
194 icalproperty *organizer = NULL;
195 char organizer_string[SIZ];
196 icalproperty *summary = NULL;
197 char summary_string[SIZ];
198 icalproperty *me_attend = NULL;
199 struct recptypes *recp = NULL;
200 icalparameter *partstat = NULL;
201 char *serialized_reply = NULL;
202 char *reply_message_text = NULL;
203 struct CtdlMessage *msg = NULL;
204 struct recptypes *valid = NULL;
206 strcpy(organizer_string, "");
207 strcpy(summary_string, "Calendar item");
209 if (request == NULL) {
210 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
214 the_reply = icalcomponent_new_clone(request);
215 if (the_reply == NULL) {
216 lprintf(CTDL_ERR, "ERROR: cannot clone request\n");
220 /* Change the method from REQUEST to REPLY */
221 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
223 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
224 if (vevent != NULL) {
225 /* Hunt for attendees, removing ones that aren't us.
226 * (Actually, remove them all, cloning our own one so we can
227 * re-insert it later)
229 while (attendee = icalcomponent_get_first_property(vevent,
230 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
232 if (icalproperty_get_attendee(attendee)) {
233 strcpy(attendee_string,
234 icalproperty_get_attendee(attendee) );
235 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
236 strcpy(attendee_string, &attendee_string[7]);
237 striplt(attendee_string);
238 recp = validate_recipients(attendee_string);
240 if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
241 if (me_attend) icalproperty_free(me_attend);
242 me_attend = icalproperty_new_clone(attendee);
244 free_recipients(recp);
249 icalcomponent_remove_property(vevent, attendee);
250 icalproperty_free(attendee);
253 /* We found our own address in the attendee list. */
255 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
256 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
258 if (!strcasecmp(action, "accept")) {
259 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
261 else if (!strcasecmp(action, "decline")) {
262 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
264 else if (!strcasecmp(action, "tentative")) {
265 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
268 if (partstat) icalproperty_add_parameter(me_attend, partstat);
270 /* Now insert it back into the vevent. */
271 icalcomponent_add_property(vevent, me_attend);
274 /* Figure out who to send this thing to */
275 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
276 if (organizer != NULL) {
277 if (icalproperty_get_organizer(organizer)) {
278 strcpy(organizer_string,
279 icalproperty_get_organizer(organizer) );
282 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
283 strcpy(organizer_string, &organizer_string[7]);
284 striplt(organizer_string);
286 strcpy(organizer_string, "");
289 /* Extract the summary string -- we'll use it as the
290 * message subject for the reply
292 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
293 if (summary != NULL) {
294 if (icalproperty_get_summary(summary)) {
295 strcpy(summary_string,
296 icalproperty_get_summary(summary) );
301 /* Now generate the reply message and send it out. */
302 serialized_reply = strdup(icalcomponent_as_ical_string(the_reply));
303 icalcomponent_free(the_reply); /* don't need this anymore */
304 if (serialized_reply == NULL) return;
306 reply_message_text = malloc(strlen(serialized_reply) + SIZ);
307 if (reply_message_text != NULL) {
308 sprintf(reply_message_text,
309 "Content-type: text/calendar charset=\"utf-8\"\r\n\r\n%s\r\n",
313 msg = CtdlMakeMessage(&CC->user,
314 organizer_string, /* to */
316 CC->room.QRname, 0, FMT_RFC822,
319 summary_string, /* Use summary for subject */
324 valid = validate_recipients(organizer_string);
325 CtdlSubmitMsg(msg, valid, "");
326 CtdlFreeMessage(msg);
327 free_recipients(valid);
330 free(serialized_reply);
336 * Callback function for mime parser that hunts for calendar content types
337 * and turns them into calendar objects
339 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
340 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
343 struct ical_respond_data *ird = NULL;
345 ird = (struct ical_respond_data *) cbuserdata;
347 /* desired_partnum can be set to "_HUNT_" to have it just look for
348 * the first part with a content type of text/calendar. Otherwise
349 * we have to only process the right one.
351 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
352 if (strcasecmp(partnum, ird->desired_partnum)) {
357 if (strcasecmp(cbtype, "text/calendar")) {
361 if (ird->cal != NULL) {
362 icalcomponent_free(ird->cal);
366 ird->cal = icalcomponent_new_from_string(content);
367 if (ird->cal != NULL) {
368 ical_dezonify(ird->cal);
374 * Respond to a meeting request.
376 void ical_respond(long msgnum, char *partnum, char *action) {
377 struct CtdlMessage *msg = NULL;
378 struct ical_respond_data ird;
381 (strcasecmp(action, "accept"))
382 && (strcasecmp(action, "decline"))
384 cprintf("%d Action must be 'accept' or 'decline'\n",
385 ERROR + ILLEGAL_VALUE
390 msg = CtdlFetchMessage(msgnum, 1);
392 cprintf("%d Message %ld not found.\n",
393 ERROR + ILLEGAL_VALUE,
399 memset(&ird, 0, sizeof ird);
400 strcpy(ird.desired_partnum, partnum);
401 mime_parser(msg->cm_fields['M'],
403 *ical_locate_part, /* callback function */
405 (void *) &ird, /* user data */
409 /* We're done with the incoming message, because we now have a
410 * calendar object in memory.
412 CtdlFreeMessage(msg);
415 * Here is the real meat of this function. Handle the event.
417 if (ird.cal != NULL) {
418 /* Save this in the user's calendar if necessary */
419 if (!strcasecmp(action, "accept")) {
420 ical_add(ird.cal, 0);
423 /* Send a reply if necessary */
424 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
425 ical_send_a_reply(ird.cal, action);
428 /* Now that we've processed this message, we don't need it
429 * anymore. So delete it. (NOTE we don't do this anymore.)
430 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
433 /* Free the memory we allocated and return a response. */
434 icalcomponent_free(ird.cal);
436 cprintf("%d ok\n", CIT_OK);
440 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
444 /* should never get here */
449 * Figure out the UID of the calendar event being referred to in a
450 * REPLY object. This function is recursive.
452 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
453 icalcomponent *subcomponent;
456 /* If this object is a REPLY, then extract the UID. */
457 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
458 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
460 strcpy(uidbuf, icalproperty_get_comment(p));
464 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
465 * UID of the reply; we want the UID of the invitation being replied to.
467 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
468 subcomponent != NULL;
469 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
470 ical_learn_uid_of_reply(uidbuf, subcomponent);
476 * ical_update_my_calendar_with_reply() refers to this callback function; when we
477 * locate the message containing the calendar event we're replying to, this function
478 * gets called. It basically just sticks the message number in a supplied buffer.
480 void ical_hunt_for_event_to_update(long msgnum, void *data) {
483 msgnumptr = (long *) data;
488 struct original_event_container {
493 * Callback function for mime parser that hunts for calendar content types
494 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
495 * to fetch the object being updated)
497 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
498 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
501 struct original_event_container *oec = NULL;
503 if (strcasecmp(cbtype, "text/calendar")) {
506 oec = (struct original_event_container *) cbuserdata;
507 if (oec->c != NULL) {
508 icalcomponent_free(oec->c);
510 oec->c = icalcomponent_new_from_string(content);
515 * Merge updated attendee information from a REPLY into an existing event.
517 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
519 icalproperty *e_attendee, *r_attendee;
521 /* First things first. If we're not looking at a VEVENT component,
522 * recurse through subcomponents until we find one.
524 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
525 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
527 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
528 ical_merge_attendee_reply(c, reply);
533 /* Now do the same thing with the reply.
535 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
536 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
538 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
539 ical_merge_attendee_reply(event, c);
544 /* Clone the reply, because we're going to rip its guts out. */
545 reply = icalcomponent_new_clone(reply);
547 /* At this point we're looking at the correct subcomponents.
548 * Iterate through the attendees looking for a match.
551 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
553 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
555 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
557 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
559 /* Check to see if these two attendees match...
562 icalproperty_get_attendee(e_attendee),
563 icalproperty_get_attendee(r_attendee)
565 /* ...and if they do, remove the attendee from the event
566 * and replace it with the attendee from the reply. (The
567 * reply's copy will have the same address, but an updated
570 icalcomponent_remove_property(event, e_attendee);
571 icalproperty_free(e_attendee);
572 icalcomponent_remove_property(reply, r_attendee);
573 icalcomponent_add_property(event, r_attendee);
575 /* Since we diddled both sets of attendees, we have to start
576 * the iteration over again. This will not create an infinite
577 * loop because we removed the attendee from the reply. (That's
578 * why we cloned the reply, and that's what we mean by "ripping
587 /* Free the *clone* of the reply. */
588 icalcomponent_free(reply);
595 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
596 * calendar event. The object has already been deserialized for us; all
597 * we have to do here is hunt for the event in our calendar, merge in the
598 * updated attendee status, and save it again.
600 * This function returns 0 on success, 1 if the event was not found in the
601 * user's calendar, or 2 if an internal error occurred.
603 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
605 char hold_rm[ROOMNAMELEN];
606 long msgnum_being_replaced = 0;
607 struct CtdlMessage *msg = NULL;
608 struct original_event_container oec;
609 icalcomponent *original_event;
610 char *serialized_event = NULL;
611 char roomname[ROOMNAMELEN];
612 char *message_text = NULL;
614 /* Figure out just what event it is we're dealing with */
615 strcpy(uid, "--==<< InVaLiD uId >>==--");
616 ical_learn_uid_of_reply(uid, cal);
617 lprintf(CTDL_DEBUG, "UID of event being replied to is <%s>\n", uid);
619 strcpy(hold_rm, CC->room.QRname); /* save current room */
621 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
622 getroom(&CC->room, hold_rm);
623 lprintf(CTDL_CRIT, "cannot get user calendar room\n");
628 * Look in the EUID index for a message with
629 * the Citadel EUID set to the value we're looking for. Since
630 * Citadel always sets the message EUID to the vCalendar UID of
631 * the event, this will work.
633 msgnum_being_replaced = locate_message_by_euid(uid, &CC->room);
635 getroom(&CC->room, hold_rm); /* return to saved room */
637 lprintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
638 if (msgnum_being_replaced == 0) {
639 return(1); /* no calendar event found */
642 /* Now we know the ID of the message containing the event being updated.
643 * We don't actually have to delete it; that'll get taken care of by the
644 * server when we save another event with the same UID. This just gives
645 * us the ability to load the event into memory so we can diddle the
648 msg = CtdlFetchMessage(msgnum_being_replaced, 1);
650 return(2); /* internal error */
653 mime_parser(msg->cm_fields['M'],
655 *ical_locate_original_event, /* callback function */
657 &oec, /* user data */
660 CtdlFreeMessage(msg);
662 original_event = oec.c;
663 if (original_event == NULL) {
664 lprintf(CTDL_ERR, "ERROR: Original_component is NULL.\n");
668 /* Merge the attendee's updated status into the event */
669 ical_merge_attendee_reply(original_event, cal);
672 serialized_event = strdup(icalcomponent_as_ical_string(original_event));
673 icalcomponent_free(original_event); /* Don't need this anymore. */
674 if (serialized_event == NULL) return(2);
676 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
678 message_text = malloc(strlen(serialized_event) + SIZ);
679 if (message_text != NULL) {
680 sprintf(message_text,
681 "Content-type: text/calendar charset=\"utf-8\"\r\n\r\n%s\r\n",
685 msg = CtdlMakeMessage(&CC->user,
686 "", /* No recipient */
687 "", /* No recipient */
697 CIT_ICAL->avoid_sending_invitations = 1;
698 CtdlSubmitMsg(msg, NULL, roomname);
699 CtdlFreeMessage(msg);
700 CIT_ICAL->avoid_sending_invitations = 0;
703 free(serialized_event);
709 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
710 * simply extracts the calendar object from the message, deserializes it, and
711 * passes it up to ical_update_my_calendar_with_reply() for processing.
713 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
714 struct CtdlMessage *msg = NULL;
715 struct ical_respond_data ird;
719 (strcasecmp(action, "update"))
720 && (strcasecmp(action, "ignore"))
722 cprintf("%d Action must be 'update' or 'ignore'\n",
723 ERROR + ILLEGAL_VALUE
728 msg = CtdlFetchMessage(msgnum, 1);
730 cprintf("%d Message %ld not found.\n",
731 ERROR + ILLEGAL_VALUE,
737 memset(&ird, 0, sizeof ird);
738 strcpy(ird.desired_partnum, partnum);
739 mime_parser(msg->cm_fields['M'],
741 *ical_locate_part, /* callback function */
743 (void *) &ird, /* user data */
747 /* We're done with the incoming message, because we now have a
748 * calendar object in memory.
750 CtdlFreeMessage(msg);
753 * Here is the real meat of this function. Handle the event.
755 if (ird.cal != NULL) {
756 /* Update the user's calendar if necessary */
757 if (!strcasecmp(action, "update")) {
758 ret = ical_update_my_calendar_with_reply(ird.cal);
760 cprintf("%d Your calendar has been updated with this reply.\n",
764 cprintf("%d This event does not exist in your calendar.\n",
765 ERROR + FILE_NOT_FOUND);
768 cprintf("%d An internal error occurred.\n",
769 ERROR + INTERNAL_ERROR);
773 cprintf("%d This reply has been ignored.\n", CIT_OK);
776 /* Now that we've processed this message, we don't need it
777 * anymore. So delete it. (Don't do this anymore.)
778 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
781 /* Free the memory we allocated and return a response. */
782 icalcomponent_free(ird.cal);
787 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
791 /* should never get here */
796 * Search for a property in both the top level and in a VEVENT subcomponent
798 icalproperty *ical_ctdl_get_subprop(
800 icalproperty_kind which_prop
805 p = icalcomponent_get_first_property(cal, which_prop);
807 c = icalcomponent_get_first_component(cal,
808 ICAL_VEVENT_COMPONENT);
810 p = icalcomponent_get_first_property(c, which_prop);
818 * Check to see if two events overlap. Returns nonzero if they do.
819 * (This function is used in both Citadel and WebCit. If you change it in
820 * one place, change it in the other. Better yet, put it in a library.)
822 int ical_ctdl_is_overlap(
823 struct icaltimetype t1start,
824 struct icaltimetype t1end,
825 struct icaltimetype t2start,
826 struct icaltimetype t2end
829 if (icaltime_is_null_time(t1start)) return(0);
830 if (icaltime_is_null_time(t2start)) return(0);
832 /* First, check for all-day events */
833 if (t1start.is_date) {
834 if (!icaltime_compare_date_only(t1start, t2start)) {
837 if (!icaltime_is_null_time(t2end)) {
838 if (!icaltime_compare_date_only(t1start, t2end)) {
844 if (t2start.is_date) {
845 if (!icaltime_compare_date_only(t2start, t1start)) {
848 if (!icaltime_is_null_time(t1end)) {
849 if (!icaltime_compare_date_only(t2start, t1end)) {
855 /* Now check for overlaps using date *and* time. */
857 /* First, bail out if either event 1 or event 2 is missing end time. */
858 if (icaltime_is_null_time(t1end)) return(0);
859 if (icaltime_is_null_time(t2end)) return(0);
861 /* If event 1 ends before event 2 starts, we're in the clear. */
862 if (icaltime_compare(t1end, t2start) <= 0) return(0);
864 /* If event 2 ends before event 1 starts, we're also ok. */
865 if (icaltime_compare(t2end, t1start) <= 0) return(0);
867 /* Otherwise, they overlap. */
874 * Backend for ical_hunt_for_conflicts()
876 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
878 struct CtdlMessage *msg = NULL;
879 struct ical_respond_data ird;
880 struct icaltimetype t1start, t1end, t2start, t2end;
882 char conflict_event_uid[SIZ];
883 char conflict_event_summary[SIZ];
884 char compare_uid[SIZ];
886 cal = (icalcomponent *)data;
887 strcpy(compare_uid, "");
888 strcpy(conflict_event_uid, "");
889 strcpy(conflict_event_summary, "");
891 msg = CtdlFetchMessage(msgnum, 1);
892 if (msg == NULL) return;
893 memset(&ird, 0, sizeof ird);
894 strcpy(ird.desired_partnum, "_HUNT_");
895 mime_parser(msg->cm_fields['M'],
897 *ical_locate_part, /* callback function */
899 (void *) &ird, /* user data */
902 CtdlFreeMessage(msg);
904 if (ird.cal == NULL) return;
906 t1start = icaltime_null_time();
907 t1end = icaltime_null_time();
908 t2start = icaltime_null_time();
909 t1end = icaltime_null_time();
911 /* Now compare cal to ird.cal */
912 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
913 if (p == NULL) return;
914 if (p != NULL) t2start = icalproperty_get_dtstart(p);
916 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
917 if (p != NULL) t2end = icalproperty_get_dtend(p);
919 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
920 if (p == NULL) return;
921 if (p != NULL) t1start = icalproperty_get_dtstart(p);
923 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
924 if (p != NULL) t1end = icalproperty_get_dtend(p);
926 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
928 strcpy(compare_uid, icalproperty_get_comment(p));
931 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
933 strcpy(conflict_event_uid, icalproperty_get_comment(p));
936 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
938 strcpy(conflict_event_summary, icalproperty_get_comment(p));
941 icalcomponent_free(ird.cal);
943 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
944 cprintf("%ld||%s|%s|%d|\n",
947 conflict_event_summary,
948 ( ((strlen(compare_uid)>0)
949 &&(!strcasecmp(compare_uid,
950 conflict_event_uid))) ? 1 : 0
959 * Phase 2 of "hunt for conflicts" operation.
960 * At this point we have a calendar object which represents the VEVENT that
961 * we're considering adding to the calendar. Now hunt through the user's
962 * calendar room, and output zero or more existing VEVENTs which conflict
965 void ical_hunt_for_conflicts(icalcomponent *cal) {
966 char hold_rm[ROOMNAMELEN];
968 strcpy(hold_rm, CC->room.QRname); /* save current room */
970 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
971 getroom(&CC->room, hold_rm);
972 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
976 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
978 CtdlForEachMessage(MSGS_ALL, 0, NULL,
981 ical_hunt_for_conflicts_backend,
986 getroom(&CC->room, hold_rm); /* return to saved room */
993 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
995 void ical_conflicts(long msgnum, char *partnum) {
996 struct CtdlMessage *msg = NULL;
997 struct ical_respond_data ird;
999 msg = CtdlFetchMessage(msgnum, 1);
1001 cprintf("%d Message %ld not found.\n",
1002 ERROR + ILLEGAL_VALUE,
1008 memset(&ird, 0, sizeof ird);
1009 strcpy(ird.desired_partnum, partnum);
1010 mime_parser(msg->cm_fields['M'],
1012 *ical_locate_part, /* callback function */
1014 (void *) &ird, /* user data */
1018 CtdlFreeMessage(msg);
1020 if (ird.cal != NULL) {
1021 ical_hunt_for_conflicts(ird.cal);
1022 icalcomponent_free(ird.cal);
1026 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1030 /* should never get here */
1036 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1038 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1041 struct icalperiodtype my_period;
1043 if (cal == NULL) return;
1044 my_period = icalperiodtype_null_period();
1046 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1047 ical_add_to_freebusy(fb,
1048 icalcomponent_get_first_component(
1049 cal, ICAL_VEVENT_COMPONENT
1057 /* If this event is not opaque, the user isn't publishing it as
1058 * busy time, so don't bother doing anything else.
1060 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1062 v = icalproperty_get_value(p);
1064 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1070 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1071 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1073 my_period.start = icalproperty_get_dtstart(p);
1076 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1078 my_period.end = icalproperty_get_dtstart(p);
1082 icalcomponent_add_property(fb,
1083 icalproperty_new_freebusy(my_period)
1086 /* Make sure the DTSTART property of the freebusy *list* is set to
1087 * the DTSTART property of the *earliest event*.
1089 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1091 icalcomponent_set_dtstart(fb,
1092 icalcomponent_get_dtstart(cal) );
1095 if (icaltime_compare(
1096 icalcomponent_get_dtstart(cal),
1097 icalcomponent_get_dtstart(fb)
1099 icalcomponent_set_dtstart(fb,
1100 icalcomponent_get_dtstart(cal) );
1104 /* Make sure the DTEND property of the freebusy *list* is set to
1105 * the DTEND property of the *latest event*.
1107 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1109 icalcomponent_set_dtend(fb,
1110 icalcomponent_get_dtend(cal) );
1113 if (icaltime_compare(
1114 icalcomponent_get_dtend(cal),
1115 icalcomponent_get_dtend(fb)
1117 icalcomponent_set_dtend(fb,
1118 icalcomponent_get_dtend(cal) );
1127 * Backend for ical_freebusy()
1129 * This function simply loads the messages in the user's calendar room,
1130 * which contain VEVENTs, then strips them of all non-freebusy data, and
1131 * adds them to the supplied VCALENDAR.
1134 void ical_freebusy_backend(long msgnum, void *data) {
1136 struct CtdlMessage *msg = NULL;
1137 struct ical_respond_data ird;
1139 cal = (icalcomponent *)data;
1141 msg = CtdlFetchMessage(msgnum, 1);
1142 if (msg == NULL) return;
1143 memset(&ird, 0, sizeof ird);
1144 strcpy(ird.desired_partnum, "_HUNT_");
1145 mime_parser(msg->cm_fields['M'],
1147 *ical_locate_part, /* callback function */
1149 (void *) &ird, /* user data */
1152 CtdlFreeMessage(msg);
1154 if (ird.cal == NULL) return;
1156 ical_add_to_freebusy(cal, ird.cal);
1158 /* Now free the memory. */
1159 icalcomponent_free(ird.cal);
1165 * Grab another user's free/busy times
1167 void ical_freebusy(char *who) {
1168 struct ctdluser usbuf;
1169 char calendar_room_name[ROOMNAMELEN];
1170 char hold_rm[ROOMNAMELEN];
1171 char *serialized_request = NULL;
1172 icalcomponent *encaps = NULL;
1173 icalcomponent *fb = NULL;
1174 int found_user = (-1);
1175 struct recptypes *recp = NULL;
1180 int config_lines = 0;
1182 /* First try an exact match. */
1183 found_user = getuser(&usbuf, who);
1185 /* If not found, try it as an unqualified email address. */
1186 if (found_user != 0) {
1188 recp = validate_recipients(buf);
1189 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1191 if (recp->num_local == 1) {
1192 found_user = getuser(&usbuf, recp->recp_local);
1194 free_recipients(recp);
1198 /* If still not found, try it as an address qualified with the
1199 * primary FQDN of this Citadel node.
1201 if (found_user != 0) {
1202 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1203 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1204 recp = validate_recipients(buf);
1206 if (recp->num_local == 1) {
1207 found_user = getuser(&usbuf, recp->recp_local);
1209 free_recipients(recp);
1213 /* Still not found? Try qualifying it with every domain we
1214 * might have addresses in.
1216 if (found_user != 0) {
1217 config_lines = num_tokens(inetcfg, '\n');
1218 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1219 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1220 extract_token(host, buf, 0, '|', sizeof host);
1221 extract_token(type, buf, 1, '|', sizeof type);
1223 if ( (!strcasecmp(type, "localhost"))
1224 || (!strcasecmp(type, "directory")) ) {
1225 snprintf(buf, sizeof buf, "%s@%s", who, host);
1226 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1227 recp = validate_recipients(buf);
1229 if (recp->num_local == 1) {
1230 found_user = getuser(&usbuf, recp->recp_local);
1232 free_recipients(recp);
1238 if (found_user != 0) {
1239 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1243 MailboxName(calendar_room_name, sizeof calendar_room_name,
1244 &usbuf, USERCALENDARROOM);
1246 strcpy(hold_rm, CC->room.QRname); /* save current room */
1248 if (getroom(&CC->room, calendar_room_name) != 0) {
1249 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1250 getroom(&CC->room, hold_rm);
1254 /* Create a VFREEBUSY subcomponent */
1255 lprintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
1256 fb = icalcomponent_new_vfreebusy();
1258 cprintf("%d Internal error: cannot allocate memory.\n",
1259 ERROR + INTERNAL_ERROR);
1260 icalcomponent_free(encaps);
1261 getroom(&CC->room, hold_rm);
1265 /* Set the method to PUBLISH */
1266 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1268 /* Set the DTSTAMP to right now. */
1269 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1271 /* Add the user's email address as ORGANIZER */
1272 sprintf(buf, "MAILTO:%s", who);
1273 if (strchr(buf, '@') == NULL) {
1275 strcat(buf, config.c_fqdn);
1277 for (i=0; buf[i]; ++i) {
1278 if (buf[i]==' ') buf[i] = '_';
1280 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1282 /* Add busy time from events */
1283 lprintf(CTDL_DEBUG, "Adding busy time from events\n");
1284 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
1286 /* If values for DTSTART and DTEND are still not present, set them
1287 * to yesterday and tomorrow as default values.
1289 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1290 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1292 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1293 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1296 /* Put the freebusy component into the calendar component */
1297 lprintf(CTDL_DEBUG, "Encapsulating\n");
1298 encaps = ical_encapsulate_subcomponent(fb);
1299 if (encaps == NULL) {
1300 icalcomponent_free(fb);
1301 cprintf("%d Internal error: cannot allocate memory.\n",
1302 ERROR + INTERNAL_ERROR);
1303 getroom(&CC->room, hold_rm);
1307 /* Set the method to PUBLISH */
1308 lprintf(CTDL_DEBUG, "Setting method\n");
1309 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1312 lprintf(CTDL_DEBUG, "Serializing\n");
1313 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1314 icalcomponent_free(encaps); /* Don't need this anymore. */
1316 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1317 if (serialized_request != NULL) {
1318 client_write(serialized_request, strlen(serialized_request));
1319 free(serialized_request);
1323 /* Go back to the room from which we came... */
1324 getroom(&CC->room, hold_rm);
1330 * Backend for ical_getics()
1332 * This is a ForEachMessage() callback function that searches the current room
1333 * for calendar events and adds them each into one big calendar component.
1335 void ical_getics_backend(long msgnum, void *data) {
1336 icalcomponent *encaps, *c;
1337 struct CtdlMessage *msg = NULL;
1338 struct ical_respond_data ird;
1340 encaps = (icalcomponent *)data;
1341 if (encaps == NULL) return;
1343 /* Look for the calendar event... */
1345 msg = CtdlFetchMessage(msgnum, 1);
1346 if (msg == NULL) return;
1347 memset(&ird, 0, sizeof ird);
1348 strcpy(ird.desired_partnum, "_HUNT_");
1349 mime_parser(msg->cm_fields['M'],
1351 *ical_locate_part, /* callback function */
1353 (void *) &ird, /* user data */
1356 CtdlFreeMessage(msg);
1358 if (ird.cal == NULL) return;
1360 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1361 * are responsible for "the_request"'s memory -- it will be freed
1362 * when we free "encaps".
1365 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1366 * in. This will almost never happen.
1368 if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
1369 icalcomponent_add_component(encaps, ird.cal);
1372 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1373 * and other components encapsulated inside, we have to extract them.
1376 for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
1378 c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
1379 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1381 icalcomponent_free(ird.cal);
1388 * Retrieve all of the calendar items in the current room, and output them
1389 * as a single icalendar object.
1391 void ical_getics(void)
1393 icalcomponent *encaps = NULL;
1396 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1397 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1398 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1399 return; /* Not a vCalendar-centric room */
1402 encaps = icalcomponent_new_vcalendar();
1403 if (encaps == NULL) {
1404 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1405 __FILE__, __LINE__);
1406 cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
1410 cprintf("%d one big calendar\n", LISTING_FOLLOWS);
1412 /* Set the Product ID */
1413 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1415 /* Set the Version Number */
1416 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1418 /* Set the method to PUBLISH */
1419 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1421 /* Now go through the room encapsulating all calendar items. */
1422 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1425 ical_getics_backend,
1429 ser = strdup(icalcomponent_as_ical_string(encaps));
1430 client_write(ser, strlen(ser));
1433 icalcomponent_free(encaps); /* Don't need this anymore. */
1439 * Delete all of the calendar items in the current room, and replace them
1440 * with calendar items from a client-supplied data stream.
1442 void ical_putics(void)
1444 char *calstream = NULL;
1448 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1449 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1450 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1451 return; /* Not a vCalendar-centric room */
1454 if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
1455 cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
1459 cprintf("%d Transmit data now\n", SEND_LISTING);
1460 calstream = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
1461 if (calstream == NULL) {
1465 cal = icalcomponent_new_from_string(calstream);
1469 /* We got our data stream -- now do something with it. */
1471 /* Delete the existing messages in the room, because we are replacing
1472 * the entire calendar with an entire new (or updated) calendar.
1473 * (Careful: this opens an S_ROOMS critical section!)
1475 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
1477 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1478 * in. This will almost never happen.
1480 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
1481 ical_write_to_cal(&CC->user, cal);
1484 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1485 * and other components encapsulated inside, we have to extract them.
1488 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1490 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1491 ical_write_to_cal(&CC->user, c);
1495 icalcomponent_free(cal);
1500 * All Citadel calendar commands from the client come through here.
1502 void cmd_ical(char *argbuf)
1510 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1512 /* Allow "test" and "freebusy" subcommands without logging in. */
1514 if (!strcasecmp(subcmd, "test")) {
1515 cprintf("%d This server supports calendaring\n", CIT_OK);
1519 if (!strcasecmp(subcmd, "freebusy")) {
1520 extract_token(who, argbuf, 1, '|', sizeof who);
1525 if (!strcasecmp(subcmd, "sgi")) {
1526 CIT_ICAL->server_generated_invitations =
1527 (extract_int(argbuf, 1) ? 1 : 0) ;
1529 CIT_OK, CIT_ICAL->server_generated_invitations);
1533 if (CtdlAccessCheck(ac_logged_in)) return;
1535 if (!strcasecmp(subcmd, "respond")) {
1536 msgnum = extract_long(argbuf, 1);
1537 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1538 extract_token(action, argbuf, 3, '|', sizeof action);
1539 ical_respond(msgnum, partnum, action);
1543 if (!strcasecmp(subcmd, "handle_rsvp")) {
1544 msgnum = extract_long(argbuf, 1);
1545 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1546 extract_token(action, argbuf, 3, '|', sizeof action);
1547 ical_handle_rsvp(msgnum, partnum, action);
1551 if (!strcasecmp(subcmd, "conflicts")) {
1552 msgnum = extract_long(argbuf, 1);
1553 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1554 ical_conflicts(msgnum, partnum);
1558 if (!strcasecmp(subcmd, "getics")) {
1563 if (!strcasecmp(subcmd, "putics")) {
1568 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1574 * We don't know if the calendar room exists so we just create it at login
1576 void ical_create_room(void)
1581 /* Create the calendar room if it doesn't already exist */
1582 create_room(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1584 /* Set expiration policy to manual; otherwise objects will be lost! */
1585 if (lgetroom(&qr, USERCALENDARROOM)) {
1586 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1589 qr.QRep.expire_mode = EXPIRE_MANUAL;
1590 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1593 /* Set the view to a calendar view */
1594 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1595 vbuf.v_view = VIEW_CALENDAR;
1596 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1598 /* Create the tasks list room if it doesn't already exist */
1599 create_room(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1601 /* Set expiration policy to manual; otherwise objects will be lost! */
1602 if (lgetroom(&qr, USERTASKSROOM)) {
1603 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1606 qr.QRep.expire_mode = EXPIRE_MANUAL;
1607 qr.QRdefaultview = VIEW_TASKS;
1610 /* Set the view to a task list view */
1611 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1612 vbuf.v_view = VIEW_TASKS;
1613 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1615 /* Create the notes room if it doesn't already exist */
1616 create_room(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1618 /* Set expiration policy to manual; otherwise objects will be lost! */
1619 if (lgetroom(&qr, USERNOTESROOM)) {
1620 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1623 qr.QRep.expire_mode = EXPIRE_MANUAL;
1624 qr.QRdefaultview = VIEW_NOTES;
1627 /* Set the view to a notes view */
1628 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1629 vbuf.v_view = VIEW_NOTES;
1630 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1637 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1640 void ical_send_out_invitations(icalcomponent *cal) {
1641 icalcomponent *the_request = NULL;
1642 char *serialized_request = NULL;
1643 icalcomponent *encaps = NULL;
1644 char *request_message_text = NULL;
1645 struct CtdlMessage *msg = NULL;
1646 struct recptypes *valid = NULL;
1647 char attendees_string[SIZ];
1648 int num_attendees = 0;
1649 char this_attendee[256];
1650 icalproperty *attendee = NULL;
1651 char summary_string[SIZ];
1652 icalproperty *summary = NULL;
1655 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
1660 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1661 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1662 ical_send_out_invitations(
1663 icalcomponent_get_first_component(
1664 cal, ICAL_VEVENT_COMPONENT
1670 /* Clone the event */
1671 the_request = icalcomponent_new_clone(cal);
1672 if (the_request == NULL) {
1673 lprintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
1677 /* Extract the summary string -- we'll use it as the
1678 * message subject for the request
1680 strcpy(summary_string, "Meeting request");
1681 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1682 if (summary != NULL) {
1683 if (icalproperty_get_summary(summary)) {
1684 strcpy(summary_string,
1685 icalproperty_get_summary(summary) );
1689 /* Determine who the recipients of this message are (the attendees) */
1690 strcpy(attendees_string, "");
1691 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1692 if (icalproperty_get_attendee(attendee)) {
1693 safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
1694 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1695 strcpy(this_attendee, &this_attendee[7]);
1697 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
1698 snprintf(&attendees_string[strlen(attendees_string)],
1699 sizeof(attendees_string) - strlen(attendees_string),
1709 lprintf(CTDL_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1711 /* If there are no attendees, there are no invitations to send, so...
1712 * don't bother putting one together! Punch out, Maverick!
1714 if (num_attendees == 0) {
1715 icalcomponent_free(the_request);
1719 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1720 encaps = icalcomponent_new_vcalendar();
1721 if (encaps == NULL) {
1722 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1723 __FILE__, __LINE__);
1724 icalcomponent_free(the_request);
1728 /* Set the Product ID */
1729 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1731 /* Set the Version Number */
1732 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1734 /* Set the method to REQUEST */
1735 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1737 /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1738 ical_dezonify(the_request);
1740 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1741 * are responsible for "the_request"'s memory -- it will be freed
1742 * when we free "encaps".
1744 icalcomponent_add_component(encaps, the_request);
1747 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1748 icalcomponent_free(encaps); /* Don't need this anymore. */
1749 if (serialized_request == NULL) return;
1751 request_message_text = malloc(strlen(serialized_request) + SIZ);
1752 if (request_message_text != NULL) {
1753 sprintf(request_message_text,
1754 "Content-type: text/calendar\r\n\r\n%s\r\n",
1758 msg = CtdlMakeMessage(&CC->user,
1759 "", /* No single recipient here */
1760 "", /* No single recipient here */
1761 CC->room.QRname, 0, FMT_RFC822,
1764 summary_string, /* Use summary for subject */
1766 request_message_text);
1769 valid = validate_recipients(attendees_string);
1770 CtdlSubmitMsg(msg, valid, "");
1771 CtdlFreeMessage(msg);
1772 free_recipients(valid);
1775 free(serialized_request);
1780 * When a calendar object is being saved, determine whether it's a VEVENT
1781 * and the user saving it is the organizer. If so, send out invitations
1782 * to any listed attendees.
1785 void ical_saving_vevent(icalcomponent *cal) {
1787 icalproperty *organizer = NULL;
1788 char organizer_string[SIZ];
1790 lprintf(CTDL_DEBUG, "ical_saving_vevent() has been called!\n");
1792 /* Don't send out invitations unless the client wants us to. */
1793 if (CIT_ICAL->server_generated_invitations == 0) {
1797 /* Don't send out invitations if we've been asked not to. */
1798 if (CIT_ICAL->avoid_sending_invitations > 0) {
1802 strcpy(organizer_string, "");
1804 * The VEVENT subcomponent is the one we're interested in.
1805 * Send out invitations if, and only if, this user is the Organizer.
1807 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1808 organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
1809 if (organizer != NULL) {
1810 if (icalproperty_get_organizer(organizer)) {
1811 strcpy(organizer_string,
1812 icalproperty_get_organizer(organizer));
1815 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1816 strcpy(organizer_string, &organizer_string[7]);
1817 striplt(organizer_string);
1819 * If the user saving the event is listed as the
1820 * organizer, then send out invitations.
1822 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
1823 ical_send_out_invitations(cal);
1828 /* If the component has subcomponents, recurse through them. */
1829 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1831 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1832 /* Recursively process subcomponent */
1833 ical_saving_vevent(c);
1841 * Back end for ical_obj_beforesave()
1842 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1843 * the summary of the event (becomes message subject),
1844 * and the start time (becomes message date/time).
1846 void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
1847 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1848 char *encoding, void *cbuserdata)
1850 icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
1852 struct icalmessagemod *imm;
1855 imm = (struct icalmessagemod *)cbuserdata;
1857 /* We're only interested in calendar data. */
1858 if (strcasecmp(cbtype, "text/calendar")) {
1862 /* Hunt for the UID and drop it in
1863 * the "user data" pointer for the MIME parser. When
1864 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1867 whole_cal = icalcomponent_new_from_string(content);
1870 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1871 nested_event = icalcomponent_get_first_component(
1872 cal, ICAL_VEVENT_COMPONENT);
1873 if (nested_event != NULL) {
1877 nested_todo = icalcomponent_get_first_component(
1878 cal, ICAL_VTODO_COMPONENT);
1879 if (nested_todo != NULL) {
1886 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1888 /* If there's no uid we must generate one */
1889 generate_uuid(new_uid);
1890 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
1891 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1894 strcpy(imm->uid, icalproperty_get_comment(p));
1896 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
1898 strcpy(imm->subject, icalproperty_get_comment(p));
1900 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
1902 imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
1905 icalcomponent_free(cal);
1906 if (whole_cal != cal) {
1907 icalcomponent_free(whole_cal);
1916 * See if we need to prevent the object from being saved (we don't allow
1917 * MIME types other than text/calendar in "calendar" or "tasks" rooms). Also,
1918 * when saving an event to the calendar, set the message's Citadel exclusive
1919 * message ID to the UID of the object. This causes our replication checker to
1920 * automatically delete any existing instances of the same object. (Isn't
1923 * We also set the message's Subject to the event summary, and the Date/time to
1924 * the event start time.
1926 int ical_obj_beforesave(struct CtdlMessage *msg)
1928 struct icalmessagemod imm;
1930 /* First determine if this is a calendar or tasks room */
1931 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1932 && (CC->room.QRdefaultview != VIEW_TASKS)
1934 return(0); /* Not a vCalendar-centric room */
1937 /* It must be an RFC822 message! */
1938 if (msg->cm_format_type != 4) {
1939 lprintf(CTDL_DEBUG, "Rejecting non-RFC822 message\n");
1940 return(1); /* You tried to save a non-RFC822 message! */
1943 if (msg->cm_fields['M'] == NULL) {
1944 return(1); /* You tried to save a null message! */
1947 memset(&imm, 0, sizeof(struct icalmessagemod));
1949 /* Do all of our lovely back-end parsing */
1950 mime_parser(msg->cm_fields['M'],
1952 *ical_ctdl_set_exclusive_msgid,
1958 if (!IsEmptyStr(imm.uid)) {
1959 if (msg->cm_fields['E'] != NULL) {
1960 free(msg->cm_fields['E']);
1962 msg->cm_fields['E'] = strdup(imm.uid);
1963 lprintf(CTDL_DEBUG, "Saving calendar UID <%s>\n", msg->cm_fields['E']);
1965 if (!IsEmptyStr(imm.subject)) {
1966 if (msg->cm_fields['U'] != NULL) {
1967 free(msg->cm_fields['U']);
1969 msg->cm_fields['U'] = strdup(imm.subject);
1971 if (imm.dtstart > 0) {
1972 if (msg->cm_fields['T'] != NULL) {
1973 free(msg->cm_fields['T']);
1975 msg->cm_fields['T'] = strdup("000000000000000000");
1976 sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
1984 * Things we need to do after saving a calendar event.
1986 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
1987 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1988 char *encoding, void *cbuserdata)
1992 /* We're only interested in calendar items here. */
1993 if (strcasecmp(cbtype, "text/calendar")) {
1997 /* Hunt for the UID and drop it in
1998 * the "user data" pointer for the MIME parser. When
1999 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2002 if (!strcasecmp(cbtype, "text/calendar")) {
2003 cal = icalcomponent_new_from_string(content);
2005 ical_saving_vevent(cal);
2006 icalcomponent_free(cal);
2013 * Things we need to do after saving a calendar event.
2014 * (This will start back end tasks such as automatic generation of invitations,
2015 * if such actions are appropriate.)
2017 int ical_obj_aftersave(struct CtdlMessage *msg)
2019 char roomname[ROOMNAMELEN];
2022 * If this isn't the Calendar> room, no further action is necessary.
2025 /* First determine if this is our room */
2026 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
2027 if (strcasecmp(roomname, CC->room.QRname)) {
2028 return(0); /* Not the Calendar room -- don't do anything. */
2031 /* It must be an RFC822 message! */
2032 if (msg->cm_format_type != 4) return(1);
2034 /* Reject null messages */
2035 if (msg->cm_fields['M'] == NULL) return(1);
2037 /* Now recurse through it looking for our icalendar data */
2038 mime_parser(msg->cm_fields['M'],
2040 *ical_obj_aftersave_backend,
2050 void ical_session_startup(void) {
2051 CIT_ICAL = malloc(sizeof(struct cit_ical));
2052 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
2055 void ical_session_shutdown(void) {
2061 * Back end for ical_fixed_output()
2063 void ical_fixed_output_backend(icalcomponent *cal,
2070 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
2072 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2075 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
2077 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2080 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
2082 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2085 /* If the component has attendees, iterate through them. */
2086 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
2087 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
2088 if (!strncasecmp(buf, "MAILTO:", 7)) {
2090 /* screen name or email address */
2091 strcpy(buf, &buf[7]);
2093 cprintf("%s ", buf);
2098 /* If the component has subcomponents, recurse through them. */
2099 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2101 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2102 /* Recursively process subcomponent */
2103 ical_fixed_output_backend(c, recursion_level+1);
2110 * Function to output vcalendar data as plain text. Nobody uses MSG0
2111 * anymore, so really this is just so we expose the vCard data to the full
2114 void ical_fixed_output(char *ptr, int len) {
2118 stringy_cal = malloc(len + 1);
2119 safestrncpy(stringy_cal, ptr, len + 1);
2120 cal = icalcomponent_new_from_string(stringy_cal);
2128 ical_fixed_output_backend(cal, 0);
2130 /* Free the memory we obtained from libical's constructor */
2131 icalcomponent_free(cal);
2135 #endif /* CITADEL_WITH_CALENDAR_SERVICE */
2138 * Register this module with the Citadel server.
2140 CTDL_MODULE_INIT(calendar)
2142 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2143 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2144 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2145 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
2146 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2147 CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2148 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2149 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2152 /* return our Subversion id for the Log */
2158 void serv_calendar_destroy(void)
2160 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2161 icaltimezone_free_builtin_timezones();