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"
36 #include "euidindex.h"
38 #ifdef CITADEL_WITH_CALENDAR_SERVICE
41 #include "ical_dezonify.h"
43 struct ical_respond_data {
44 char desired_partnum[SIZ];
50 * Utility function to create a new VCALENDAR component with some of the
51 * required fields already set the way we like them.
53 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
54 icalcomponent *encaps;
56 encaps = icalcomponent_new_vcalendar();
58 lprintf(CTDL_CRIT, "Error at %s:%d - could not allocate component!\n",
63 /* Set the Product ID */
64 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
66 /* Set the Version Number */
67 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
74 * Utility function to encapsulate a subcomponent into a full VCALENDAR
76 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
77 icalcomponent *encaps;
79 /* If we're already looking at a full VCALENDAR component,
80 * don't bother ... just return itself.
82 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
86 /* Encapsulate the VEVENT component into a complete VCALENDAR */
87 encaps = icalcomponent_new_citadel_vcalendar();
88 if (encaps == NULL) return NULL;
90 /* Encapsulate the subcomponent inside */
91 icalcomponent_add_component(encaps, subcomp);
93 /* Convert all timestamps to UTC so we don't have to deal with
94 * stupid VTIMEZONE crap.
96 ical_dezonify(encaps);
98 /* Return the object we just created. */
106 * Write a calendar object into the specified user's calendar room.
108 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
112 icalcomponent *encaps;
114 if (cal == NULL) return;
116 /* If the supplied object is a subcomponent, encapsulate it in
117 * a full VCALENDAR component, and save that instead.
119 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
120 encaps = ical_encapsulate_subcomponent(
121 icalcomponent_new_clone(cal)
123 ical_write_to_cal(u, encaps);
124 icalcomponent_free(encaps);
128 CtdlMakeTempFileName(temp, sizeof temp);
129 ser = icalcomponent_as_ical_string(cal);
130 if (ser == NULL) return;
132 /* Make a temp file out of it */
133 fp = fopen(temp, "w");
134 if (fp == NULL) return;
135 fwrite(ser, strlen(ser), 1, fp);
138 /* This handy API function does all the work for us.
140 CtdlWriteObject(USERCALENDARROOM, /* which room */
141 "text/calendar", /* MIME type */
142 temp, /* temp file */
145 0, /* don't delete others of this type */
153 * Add a calendar object to the user's calendar
155 * ok because it uses ical_write_to_cal()
157 void ical_add(icalcomponent *cal, int recursion_level) {
161 * The VEVENT subcomponent is the one we're interested in saving.
163 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
165 ical_write_to_cal(&CC->user, cal);
169 /* If the component has subcomponents, recurse through them. */
170 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
172 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
173 /* Recursively process subcomponent */
174 ical_add(c, recursion_level+1);
182 * Send a reply to a meeting invitation.
184 * 'request' is the invitation to reply to.
185 * 'action' is the string "accept" or "decline" or "tentative".
188 void ical_send_a_reply(icalcomponent *request, char *action) {
189 icalcomponent *the_reply = NULL;
190 icalcomponent *vevent = NULL;
191 icalproperty *attendee = NULL;
192 char attendee_string[SIZ];
193 icalproperty *organizer = NULL;
194 char organizer_string[SIZ];
195 icalproperty *summary = NULL;
196 char summary_string[SIZ];
197 icalproperty *me_attend = NULL;
198 struct recptypes *recp = NULL;
199 icalparameter *partstat = NULL;
200 char *serialized_reply = NULL;
201 char *reply_message_text = NULL;
202 struct CtdlMessage *msg = NULL;
203 struct recptypes *valid = NULL;
205 strcpy(organizer_string, "");
206 strcpy(summary_string, "Calendar item");
208 if (request == NULL) {
209 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
213 the_reply = icalcomponent_new_clone(request);
214 if (the_reply == NULL) {
215 lprintf(CTDL_ERR, "ERROR: cannot clone request\n");
219 /* Change the method from REQUEST to REPLY */
220 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
222 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
223 if (vevent != NULL) {
224 /* Hunt for attendees, removing ones that aren't us.
225 * (Actually, remove them all, cloning our own one so we can
226 * re-insert it later)
228 while (attendee = icalcomponent_get_first_property(vevent,
229 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
231 if (icalproperty_get_attendee(attendee)) {
232 strcpy(attendee_string,
233 icalproperty_get_attendee(attendee) );
234 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
235 strcpy(attendee_string, &attendee_string[7]);
236 striplt(attendee_string);
237 recp = validate_recipients(attendee_string);
239 if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
240 if (me_attend) icalproperty_free(me_attend);
241 me_attend = icalproperty_new_clone(attendee);
243 free_recipients(recp);
248 icalcomponent_remove_property(vevent, attendee);
249 icalproperty_free(attendee);
252 /* We found our own address in the attendee list. */
254 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
255 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
257 if (!strcasecmp(action, "accept")) {
258 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
260 else if (!strcasecmp(action, "decline")) {
261 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
263 else if (!strcasecmp(action, "tentative")) {
264 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
267 if (partstat) icalproperty_add_parameter(me_attend, partstat);
269 /* Now insert it back into the vevent. */
270 icalcomponent_add_property(vevent, me_attend);
273 /* Figure out who to send this thing to */
274 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
275 if (organizer != NULL) {
276 if (icalproperty_get_organizer(organizer)) {
277 strcpy(organizer_string,
278 icalproperty_get_organizer(organizer) );
281 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
282 strcpy(organizer_string, &organizer_string[7]);
283 striplt(organizer_string);
285 strcpy(organizer_string, "");
288 /* Extract the summary string -- we'll use it as the
289 * message subject for the reply
291 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
292 if (summary != NULL) {
293 if (icalproperty_get_summary(summary)) {
294 strcpy(summary_string,
295 icalproperty_get_summary(summary) );
300 /* Now generate the reply message and send it out. */
301 serialized_reply = strdup(icalcomponent_as_ical_string(the_reply));
302 icalcomponent_free(the_reply); /* don't need this anymore */
303 if (serialized_reply == NULL) return;
305 reply_message_text = malloc(strlen(serialized_reply) + SIZ);
306 if (reply_message_text != NULL) {
307 sprintf(reply_message_text,
308 "Content-type: text/calendar charset=\"utf-8\"\r\n\r\n%s\r\n",
312 msg = CtdlMakeMessage(&CC->user,
313 organizer_string, /* to */
315 CC->room.QRname, 0, FMT_RFC822,
318 summary_string, /* Use summary for subject */
323 valid = validate_recipients(organizer_string);
324 CtdlSubmitMsg(msg, valid, "");
325 CtdlFreeMessage(msg);
326 free_recipients(valid);
329 free(serialized_reply);
335 * Callback function for mime parser that hunts for calendar content types
336 * and turns them into calendar objects
338 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
339 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
342 struct ical_respond_data *ird = NULL;
344 ird = (struct ical_respond_data *) cbuserdata;
346 /* desired_partnum can be set to "_HUNT_" to have it just look for
347 * the first part with a content type of text/calendar. Otherwise
348 * we have to only process the right one.
350 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
351 if (strcasecmp(partnum, ird->desired_partnum)) {
356 if (strcasecmp(cbtype, "text/calendar")) {
360 if (ird->cal != NULL) {
361 icalcomponent_free(ird->cal);
365 ird->cal = icalcomponent_new_from_string(content);
366 if (ird->cal != NULL) {
367 ical_dezonify(ird->cal);
373 * Respond to a meeting request.
375 void ical_respond(long msgnum, char *partnum, char *action) {
376 struct CtdlMessage *msg = NULL;
377 struct ical_respond_data ird;
380 (strcasecmp(action, "accept"))
381 && (strcasecmp(action, "decline"))
383 cprintf("%d Action must be 'accept' or 'decline'\n",
384 ERROR + ILLEGAL_VALUE
389 msg = CtdlFetchMessage(msgnum, 1);
391 cprintf("%d Message %ld not found.\n",
392 ERROR + ILLEGAL_VALUE,
398 memset(&ird, 0, sizeof ird);
399 strcpy(ird.desired_partnum, partnum);
400 mime_parser(msg->cm_fields['M'],
402 *ical_locate_part, /* callback function */
404 (void *) &ird, /* user data */
408 /* We're done with the incoming message, because we now have a
409 * calendar object in memory.
411 CtdlFreeMessage(msg);
414 * Here is the real meat of this function. Handle the event.
416 if (ird.cal != NULL) {
417 /* Save this in the user's calendar if necessary */
418 if (!strcasecmp(action, "accept")) {
419 ical_add(ird.cal, 0);
422 /* Send a reply if necessary */
423 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
424 ical_send_a_reply(ird.cal, action);
427 /* Now that we've processed this message, we don't need it
428 * anymore. So delete it.
430 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
432 /* Free the memory we allocated and return a response. */
433 icalcomponent_free(ird.cal);
435 cprintf("%d ok\n", CIT_OK);
439 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
443 /* should never get here */
448 * Figure out the UID of the calendar event being referred to in a
449 * REPLY object. This function is recursive.
451 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
452 icalcomponent *subcomponent;
455 /* If this object is a REPLY, then extract the UID. */
456 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
457 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
459 strcpy(uidbuf, icalproperty_get_comment(p));
463 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
464 * UID of the reply; we want the UID of the invitation being replied to.
466 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
467 subcomponent != NULL;
468 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
469 ical_learn_uid_of_reply(uidbuf, subcomponent);
475 * ical_update_my_calendar_with_reply() refers to this callback function; when we
476 * locate the message containing the calendar event we're replying to, this function
477 * gets called. It basically just sticks the message number in a supplied buffer.
479 void ical_hunt_for_event_to_update(long msgnum, void *data) {
482 msgnumptr = (long *) data;
487 struct original_event_container {
492 * Callback function for mime parser that hunts for calendar content types
493 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
494 * to fetch the object being updated)
496 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
497 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
500 struct original_event_container *oec = NULL;
502 if (strcasecmp(cbtype, "text/calendar")) {
505 oec = (struct original_event_container *) cbuserdata;
506 if (oec->c != NULL) {
507 icalcomponent_free(oec->c);
509 oec->c = icalcomponent_new_from_string(content);
514 * Merge updated attendee information from a REPLY into an existing event.
516 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
518 icalproperty *e_attendee, *r_attendee;
520 /* First things first. If we're not looking at a VEVENT component,
521 * recurse through subcomponents until we find one.
523 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
524 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
526 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
527 ical_merge_attendee_reply(c, reply);
532 /* Now do the same thing with the reply.
534 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
535 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
537 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
538 ical_merge_attendee_reply(event, c);
543 /* Clone the reply, because we're going to rip its guts out. */
544 reply = icalcomponent_new_clone(reply);
546 /* At this point we're looking at the correct subcomponents.
547 * Iterate through the attendees looking for a match.
550 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
552 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
554 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
556 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
558 /* Check to see if these two attendees match...
561 icalproperty_get_attendee(e_attendee),
562 icalproperty_get_attendee(r_attendee)
564 /* ...and if they do, remove the attendee from the event
565 * and replace it with the attendee from the reply. (The
566 * reply's copy will have the same address, but an updated
569 icalcomponent_remove_property(event, e_attendee);
570 icalproperty_free(e_attendee);
571 icalcomponent_remove_property(reply, r_attendee);
572 icalcomponent_add_property(event, r_attendee);
574 /* Since we diddled both sets of attendees, we have to start
575 * the iteration over again. This will not create an infinite
576 * loop because we removed the attendee from the reply. (That's
577 * why we cloned the reply, and that's what we mean by "ripping
586 /* Free the *clone* of the reply. */
587 icalcomponent_free(reply);
594 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
595 * calendar event. The object has already been deserialized for us; all
596 * we have to do here is hunt for the event in our calendar, merge in the
597 * updated attendee status, and save it again.
599 * This function returns 0 on success, 1 if the event was not found in the
600 * user's calendar, or 2 if an internal error occurred.
602 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
604 char hold_rm[ROOMNAMELEN];
605 long msgnum_being_replaced = 0;
606 struct CtdlMessage *msg = NULL;
607 struct original_event_container oec;
608 icalcomponent *original_event;
609 char *serialized_event = NULL;
610 char roomname[ROOMNAMELEN];
611 char *message_text = NULL;
613 /* Figure out just what event it is we're dealing with */
614 strcpy(uid, "--==<< InVaLiD uId >>==--");
615 ical_learn_uid_of_reply(uid, cal);
616 lprintf(CTDL_DEBUG, "UID of event being replied to is <%s>\n", uid);
618 strcpy(hold_rm, CC->room.QRname); /* save current room */
620 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
621 getroom(&CC->room, hold_rm);
622 lprintf(CTDL_CRIT, "cannot get user calendar room\n");
627 * Look in the EUID index for a message with
628 * the Citadel EUID set to the value we're looking for. Since
629 * Citadel always sets the message EUID to the vCalendar UID of
630 * the event, this will work.
632 msgnum_being_replaced = locate_message_by_euid(uid, &CC->room);
634 getroom(&CC->room, hold_rm); /* return to saved room */
636 lprintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
637 if (msgnum_being_replaced == 0) {
638 return(1); /* no calendar event found */
641 /* Now we know the ID of the message containing the event being updated.
642 * We don't actually have to delete it; that'll get taken care of by the
643 * server when we save another event with the same UID. This just gives
644 * us the ability to load the event into memory so we can diddle the
647 msg = CtdlFetchMessage(msgnum_being_replaced, 1);
649 return(2); /* internal error */
652 mime_parser(msg->cm_fields['M'],
654 *ical_locate_original_event, /* callback function */
656 &oec, /* user data */
659 CtdlFreeMessage(msg);
661 original_event = oec.c;
662 if (original_event == NULL) {
663 lprintf(CTDL_ERR, "ERROR: Original_component is NULL.\n");
667 /* Merge the attendee's updated status into the event */
668 ical_merge_attendee_reply(original_event, cal);
671 serialized_event = strdup(icalcomponent_as_ical_string(original_event));
672 icalcomponent_free(original_event); /* Don't need this anymore. */
673 if (serialized_event == NULL) return(2);
675 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
677 message_text = malloc(strlen(serialized_event) + SIZ);
678 if (message_text != NULL) {
679 sprintf(message_text,
680 "Content-type: text/calendar charset=\"utf-8\"\r\n\r\n%s\r\n",
684 msg = CtdlMakeMessage(&CC->user,
685 "", /* No recipient */
686 "", /* No recipient */
696 CIT_ICAL->avoid_sending_invitations = 1;
697 CtdlSubmitMsg(msg, NULL, roomname);
698 CtdlFreeMessage(msg);
699 CIT_ICAL->avoid_sending_invitations = 0;
702 free(serialized_event);
708 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
709 * simply extracts the calendar object from the message, deserializes it, and
710 * passes it up to ical_update_my_calendar_with_reply() for processing.
712 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
713 struct CtdlMessage *msg = NULL;
714 struct ical_respond_data ird;
718 (strcasecmp(action, "update"))
719 && (strcasecmp(action, "ignore"))
721 cprintf("%d Action must be 'update' or 'ignore'\n",
722 ERROR + ILLEGAL_VALUE
727 msg = CtdlFetchMessage(msgnum, 1);
729 cprintf("%d Message %ld not found.\n",
730 ERROR + ILLEGAL_VALUE,
736 memset(&ird, 0, sizeof ird);
737 strcpy(ird.desired_partnum, partnum);
738 mime_parser(msg->cm_fields['M'],
740 *ical_locate_part, /* callback function */
742 (void *) &ird, /* user data */
746 /* We're done with the incoming message, because we now have a
747 * calendar object in memory.
749 CtdlFreeMessage(msg);
752 * Here is the real meat of this function. Handle the event.
754 if (ird.cal != NULL) {
755 /* Update the user's calendar if necessary */
756 if (!strcasecmp(action, "update")) {
757 ret = ical_update_my_calendar_with_reply(ird.cal);
759 cprintf("%d Your calendar has been updated with this reply.\n",
763 cprintf("%d This event does not exist in your calendar.\n",
764 ERROR + FILE_NOT_FOUND);
767 cprintf("%d An internal error occurred.\n",
768 ERROR + INTERNAL_ERROR);
772 cprintf("%d This reply has been ignored.\n", CIT_OK);
775 /* Now that we've processed this message, we don't need it
776 * anymore. So delete it. (Maybe make this optional?)
778 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
780 /* Free the memory we allocated and return a response. */
781 icalcomponent_free(ird.cal);
786 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
790 /* should never get here */
795 * Search for a property in both the top level and in a VEVENT subcomponent
797 icalproperty *ical_ctdl_get_subprop(
799 icalproperty_kind which_prop
804 p = icalcomponent_get_first_property(cal, which_prop);
806 c = icalcomponent_get_first_component(cal,
807 ICAL_VEVENT_COMPONENT);
809 p = icalcomponent_get_first_property(c, which_prop);
817 * Check to see if two events overlap. Returns nonzero if they do.
818 * (This function is used in both Citadel and WebCit. If you change it in
819 * one place, change it in the other. Better yet, put it in a library.)
821 int ical_ctdl_is_overlap(
822 struct icaltimetype t1start,
823 struct icaltimetype t1end,
824 struct icaltimetype t2start,
825 struct icaltimetype t2end
828 if (icaltime_is_null_time(t1start)) return(0);
829 if (icaltime_is_null_time(t2start)) return(0);
831 /* First, check for all-day events */
832 if (t1start.is_date) {
833 if (!icaltime_compare_date_only(t1start, t2start)) {
836 if (!icaltime_is_null_time(t2end)) {
837 if (!icaltime_compare_date_only(t1start, t2end)) {
843 if (t2start.is_date) {
844 if (!icaltime_compare_date_only(t2start, t1start)) {
847 if (!icaltime_is_null_time(t1end)) {
848 if (!icaltime_compare_date_only(t2start, t1end)) {
854 /* Now check for overlaps using date *and* time. */
856 /* First, bail out if either event 1 or event 2 is missing end time. */
857 if (icaltime_is_null_time(t1end)) return(0);
858 if (icaltime_is_null_time(t2end)) return(0);
860 /* If event 1 ends before event 2 starts, we're in the clear. */
861 if (icaltime_compare(t1end, t2start) <= 0) return(0);
863 /* If event 2 ends before event 1 starts, we're also ok. */
864 if (icaltime_compare(t2end, t1start) <= 0) return(0);
866 /* Otherwise, they overlap. */
873 * Backend for ical_hunt_for_conflicts()
875 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
877 struct CtdlMessage *msg = NULL;
878 struct ical_respond_data ird;
879 struct icaltimetype t1start, t1end, t2start, t2end;
881 char conflict_event_uid[SIZ];
882 char conflict_event_summary[SIZ];
883 char compare_uid[SIZ];
885 cal = (icalcomponent *)data;
886 strcpy(compare_uid, "");
887 strcpy(conflict_event_uid, "");
888 strcpy(conflict_event_summary, "");
890 msg = CtdlFetchMessage(msgnum, 1);
891 if (msg == NULL) return;
892 memset(&ird, 0, sizeof ird);
893 strcpy(ird.desired_partnum, "_HUNT_");
894 mime_parser(msg->cm_fields['M'],
896 *ical_locate_part, /* callback function */
898 (void *) &ird, /* user data */
901 CtdlFreeMessage(msg);
903 if (ird.cal == NULL) return;
905 t1start = icaltime_null_time();
906 t1end = icaltime_null_time();
907 t2start = icaltime_null_time();
908 t1end = icaltime_null_time();
910 /* Now compare cal to ird.cal */
911 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
912 if (p == NULL) return;
913 if (p != NULL) t2start = icalproperty_get_dtstart(p);
915 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
916 if (p != NULL) t2end = icalproperty_get_dtend(p);
918 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
919 if (p == NULL) return;
920 if (p != NULL) t1start = icalproperty_get_dtstart(p);
922 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
923 if (p != NULL) t1end = icalproperty_get_dtend(p);
925 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
927 strcpy(compare_uid, icalproperty_get_comment(p));
930 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
932 strcpy(conflict_event_uid, icalproperty_get_comment(p));
935 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
937 strcpy(conflict_event_summary, icalproperty_get_comment(p));
940 icalcomponent_free(ird.cal);
942 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
943 cprintf("%ld||%s|%s|%d|\n",
946 conflict_event_summary,
947 ( ((strlen(compare_uid)>0)
948 &&(!strcasecmp(compare_uid,
949 conflict_event_uid))) ? 1 : 0
958 * Phase 2 of "hunt for conflicts" operation.
959 * At this point we have a calendar object which represents the VEVENT that
960 * we're considering adding to the calendar. Now hunt through the user's
961 * calendar room, and output zero or more existing VEVENTs which conflict
964 void ical_hunt_for_conflicts(icalcomponent *cal) {
965 char hold_rm[ROOMNAMELEN];
967 strcpy(hold_rm, CC->room.QRname); /* save current room */
969 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
970 getroom(&CC->room, hold_rm);
971 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
975 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
977 CtdlForEachMessage(MSGS_ALL, 0, NULL,
980 ical_hunt_for_conflicts_backend,
985 getroom(&CC->room, hold_rm); /* return to saved room */
992 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
994 void ical_conflicts(long msgnum, char *partnum) {
995 struct CtdlMessage *msg = NULL;
996 struct ical_respond_data ird;
998 msg = CtdlFetchMessage(msgnum, 1);
1000 cprintf("%d Message %ld not found.\n",
1001 ERROR + ILLEGAL_VALUE,
1007 memset(&ird, 0, sizeof ird);
1008 strcpy(ird.desired_partnum, partnum);
1009 mime_parser(msg->cm_fields['M'],
1011 *ical_locate_part, /* callback function */
1013 (void *) &ird, /* user data */
1017 CtdlFreeMessage(msg);
1019 if (ird.cal != NULL) {
1020 ical_hunt_for_conflicts(ird.cal);
1021 icalcomponent_free(ird.cal);
1025 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1029 /* should never get here */
1035 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1037 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1040 struct icalperiodtype my_period;
1042 if (cal == NULL) return;
1043 my_period = icalperiodtype_null_period();
1045 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1046 ical_add_to_freebusy(fb,
1047 icalcomponent_get_first_component(
1048 cal, ICAL_VEVENT_COMPONENT
1056 /* If this event is not opaque, the user isn't publishing it as
1057 * busy time, so don't bother doing anything else.
1059 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1061 v = icalproperty_get_value(p);
1063 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1069 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1070 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1072 my_period.start = icalproperty_get_dtstart(p);
1075 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1077 my_period.end = icalproperty_get_dtstart(p);
1081 icalcomponent_add_property(fb,
1082 icalproperty_new_freebusy(my_period)
1085 /* Make sure the DTSTART property of the freebusy *list* is set to
1086 * the DTSTART property of the *earliest event*.
1088 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1090 icalcomponent_set_dtstart(fb,
1091 icalcomponent_get_dtstart(cal) );
1094 if (icaltime_compare(
1095 icalcomponent_get_dtstart(cal),
1096 icalcomponent_get_dtstart(fb)
1098 icalcomponent_set_dtstart(fb,
1099 icalcomponent_get_dtstart(cal) );
1103 /* Make sure the DTEND property of the freebusy *list* is set to
1104 * the DTEND property of the *latest event*.
1106 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1108 icalcomponent_set_dtend(fb,
1109 icalcomponent_get_dtend(cal) );
1112 if (icaltime_compare(
1113 icalcomponent_get_dtend(cal),
1114 icalcomponent_get_dtend(fb)
1116 icalcomponent_set_dtend(fb,
1117 icalcomponent_get_dtend(cal) );
1126 * Backend for ical_freebusy()
1128 * This function simply loads the messages in the user's calendar room,
1129 * which contain VEVENTs, then strips them of all non-freebusy data, and
1130 * adds them to the supplied VCALENDAR.
1133 void ical_freebusy_backend(long msgnum, void *data) {
1135 struct CtdlMessage *msg = NULL;
1136 struct ical_respond_data ird;
1138 cal = (icalcomponent *)data;
1140 msg = CtdlFetchMessage(msgnum, 1);
1141 if (msg == NULL) return;
1142 memset(&ird, 0, sizeof ird);
1143 strcpy(ird.desired_partnum, "_HUNT_");
1144 mime_parser(msg->cm_fields['M'],
1146 *ical_locate_part, /* callback function */
1148 (void *) &ird, /* user data */
1151 CtdlFreeMessage(msg);
1153 if (ird.cal == NULL) return;
1155 ical_add_to_freebusy(cal, ird.cal);
1157 /* Now free the memory. */
1158 icalcomponent_free(ird.cal);
1164 * Grab another user's free/busy times
1166 void ical_freebusy(char *who) {
1167 struct ctdluser usbuf;
1168 char calendar_room_name[ROOMNAMELEN];
1169 char hold_rm[ROOMNAMELEN];
1170 char *serialized_request = NULL;
1171 icalcomponent *encaps = NULL;
1172 icalcomponent *fb = NULL;
1173 int found_user = (-1);
1174 struct recptypes *recp = NULL;
1179 int config_lines = 0;
1181 /* First try an exact match. */
1182 found_user = getuser(&usbuf, who);
1184 /* If not found, try it as an unqualified email address. */
1185 if (found_user != 0) {
1187 recp = validate_recipients(buf);
1188 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1190 if (recp->num_local == 1) {
1191 found_user = getuser(&usbuf, recp->recp_local);
1193 free_recipients(recp);
1197 /* If still not found, try it as an address qualified with the
1198 * primary FQDN of this Citadel node.
1200 if (found_user != 0) {
1201 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1202 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1203 recp = validate_recipients(buf);
1205 if (recp->num_local == 1) {
1206 found_user = getuser(&usbuf, recp->recp_local);
1208 free_recipients(recp);
1212 /* Still not found? Try qualifying it with every domain we
1213 * might have addresses in.
1215 if (found_user != 0) {
1216 config_lines = num_tokens(inetcfg, '\n');
1217 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1218 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1219 extract_token(host, buf, 0, '|', sizeof host);
1220 extract_token(type, buf, 1, '|', sizeof type);
1222 if ( (!strcasecmp(type, "localhost"))
1223 || (!strcasecmp(type, "directory")) ) {
1224 snprintf(buf, sizeof buf, "%s@%s", who, host);
1225 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1226 recp = validate_recipients(buf);
1228 if (recp->num_local == 1) {
1229 found_user = getuser(&usbuf, recp->recp_local);
1231 free_recipients(recp);
1237 if (found_user != 0) {
1238 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1242 MailboxName(calendar_room_name, sizeof calendar_room_name,
1243 &usbuf, USERCALENDARROOM);
1245 strcpy(hold_rm, CC->room.QRname); /* save current room */
1247 if (getroom(&CC->room, calendar_room_name) != 0) {
1248 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1249 getroom(&CC->room, hold_rm);
1253 /* Create a VFREEBUSY subcomponent */
1254 lprintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
1255 fb = icalcomponent_new_vfreebusy();
1257 cprintf("%d Internal error: cannot allocate memory.\n",
1258 ERROR + INTERNAL_ERROR);
1259 icalcomponent_free(encaps);
1260 getroom(&CC->room, hold_rm);
1264 /* Set the method to PUBLISH */
1265 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1267 /* Set the DTSTAMP to right now. */
1268 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1270 /* Add the user's email address as ORGANIZER */
1271 sprintf(buf, "MAILTO:%s", who);
1272 if (strchr(buf, '@') == NULL) {
1274 strcat(buf, config.c_fqdn);
1276 for (i=0; i<strlen(buf); ++i) {
1277 if (buf[i]==' ') buf[i] = '_';
1279 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1281 /* Add busy time from events */
1282 lprintf(CTDL_DEBUG, "Adding busy time from events\n");
1283 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
1285 /* If values for DTSTART and DTEND are still not present, set them
1286 * to yesterday and tomorrow as default values.
1288 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1289 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1291 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1292 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1295 /* Put the freebusy component into the calendar component */
1296 lprintf(CTDL_DEBUG, "Encapsulating\n");
1297 encaps = ical_encapsulate_subcomponent(fb);
1298 if (encaps == NULL) {
1299 icalcomponent_free(fb);
1300 cprintf("%d Internal error: cannot allocate memory.\n",
1301 ERROR + INTERNAL_ERROR);
1302 getroom(&CC->room, hold_rm);
1306 /* Set the method to PUBLISH */
1307 lprintf(CTDL_DEBUG, "Setting method\n");
1308 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1311 lprintf(CTDL_DEBUG, "Serializing\n");
1312 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1313 icalcomponent_free(encaps); /* Don't need this anymore. */
1315 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1316 if (serialized_request != NULL) {
1317 client_write(serialized_request, strlen(serialized_request));
1318 free(serialized_request);
1322 /* Go back to the room from which we came... */
1323 getroom(&CC->room, hold_rm);
1329 * Backend for ical_getics()
1331 * This is a ForEachMessage() callback function that searches the current room
1332 * for calendar events and adds them each into one big calendar component.
1334 void ical_getics_backend(long msgnum, void *data) {
1335 icalcomponent *encaps, *c;
1336 struct CtdlMessage *msg = NULL;
1337 struct ical_respond_data ird;
1339 encaps = (icalcomponent *)data;
1340 if (encaps == NULL) return;
1342 /* Look for the calendar event... */
1344 msg = CtdlFetchMessage(msgnum, 1);
1345 if (msg == NULL) return;
1346 memset(&ird, 0, sizeof ird);
1347 strcpy(ird.desired_partnum, "_HUNT_");
1348 mime_parser(msg->cm_fields['M'],
1350 *ical_locate_part, /* callback function */
1352 (void *) &ird, /* user data */
1355 CtdlFreeMessage(msg);
1357 if (ird.cal == NULL) return;
1359 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1360 * are responsible for "the_request"'s memory -- it will be freed
1361 * when we free "encaps".
1364 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1365 * in. This will almost never happen.
1367 if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
1368 icalcomponent_add_component(encaps, ird.cal);
1371 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1372 * and other components encapsulated inside, we have to extract them.
1375 for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
1377 c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
1378 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1380 icalcomponent_free(ird.cal);
1387 * Retrieve all of the calendar items in the current room, and output them
1388 * as a single icalendar object.
1390 void ical_getics(void)
1392 icalcomponent *encaps = NULL;
1395 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1396 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1397 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1398 return; /* Not a vCalendar-centric room */
1401 encaps = icalcomponent_new_vcalendar();
1402 if (encaps == NULL) {
1403 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1404 __FILE__, __LINE__);
1405 cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
1409 cprintf("%d one big calendar\n", LISTING_FOLLOWS);
1411 /* Set the Product ID */
1412 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1414 /* Set the Version Number */
1415 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1417 /* Set the method to PUBLISH */
1418 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1420 /* Now go through the room encapsulating all calendar items. */
1421 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1424 ical_getics_backend,
1428 ser = strdup(icalcomponent_as_ical_string(encaps));
1429 client_write(ser, strlen(ser));
1432 icalcomponent_free(encaps); /* Don't need this anymore. */
1438 * Delete all of the calendar items in the current room, and replace them
1439 * with calendar items from a client-supplied data stream.
1441 void ical_putics(void)
1443 char *calstream = NULL;
1447 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1448 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1449 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1450 return; /* Not a vCalendar-centric room */
1453 if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
1454 cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
1458 cprintf("%d Transmit data now\n", SEND_LISTING);
1459 calstream = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0);
1460 if (calstream == NULL) {
1464 cal = icalcomponent_new_from_string(calstream);
1468 /* We got our data stream -- now do something with it. */
1470 /* Delete the existing messages in the room, because we are replacing
1471 * the entire calendar with an entire new (or updated) calendar.
1472 * (Careful: this opens an S_ROOMS critical section!)
1474 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
1476 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1477 * in. This will almost never happen.
1479 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
1480 ical_write_to_cal(&CC->user, cal);
1483 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1484 * and other components encapsulated inside, we have to extract them.
1487 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1489 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1490 ical_write_to_cal(&CC->user, c);
1494 icalcomponent_free(cal);
1499 * All Citadel calendar commands from the client come through here.
1501 void cmd_ical(char *argbuf)
1509 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1511 /* Allow "test" and "freebusy" subcommands without logging in. */
1513 if (!strcasecmp(subcmd, "test")) {
1514 cprintf("%d This server supports calendaring\n", CIT_OK);
1518 if (!strcasecmp(subcmd, "freebusy")) {
1519 extract_token(who, argbuf, 1, '|', sizeof who);
1524 if (!strcasecmp(subcmd, "sgi")) {
1525 CIT_ICAL->server_generated_invitations =
1526 (extract_int(argbuf, 1) ? 1 : 0) ;
1528 CIT_OK, CIT_ICAL->server_generated_invitations);
1532 if (CtdlAccessCheck(ac_logged_in)) return;
1534 if (!strcasecmp(subcmd, "respond")) {
1535 msgnum = extract_long(argbuf, 1);
1536 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1537 extract_token(action, argbuf, 3, '|', sizeof action);
1538 ical_respond(msgnum, partnum, action);
1542 if (!strcasecmp(subcmd, "handle_rsvp")) {
1543 msgnum = extract_long(argbuf, 1);
1544 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1545 extract_token(action, argbuf, 3, '|', sizeof action);
1546 ical_handle_rsvp(msgnum, partnum, action);
1550 if (!strcasecmp(subcmd, "conflicts")) {
1551 msgnum = extract_long(argbuf, 1);
1552 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1553 ical_conflicts(msgnum, partnum);
1557 if (!strcasecmp(subcmd, "getics")) {
1562 if (!strcasecmp(subcmd, "putics")) {
1567 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1573 * We don't know if the calendar room exists so we just create it at login
1575 void ical_create_room(void)
1580 /* Create the calendar room if it doesn't already exist */
1581 create_room(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1583 /* Set expiration policy to manual; otherwise objects will be lost! */
1584 if (lgetroom(&qr, USERCALENDARROOM)) {
1585 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1588 qr.QRep.expire_mode = EXPIRE_MANUAL;
1589 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1592 /* Set the view to a calendar view */
1593 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1594 vbuf.v_view = VIEW_CALENDAR;
1595 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1597 /* Create the tasks list room if it doesn't already exist */
1598 create_room(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1600 /* Set expiration policy to manual; otherwise objects will be lost! */
1601 if (lgetroom(&qr, USERTASKSROOM)) {
1602 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1605 qr.QRep.expire_mode = EXPIRE_MANUAL;
1606 qr.QRdefaultview = VIEW_TASKS;
1609 /* Set the view to a task list view */
1610 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1611 vbuf.v_view = VIEW_TASKS;
1612 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1614 /* Create the notes room if it doesn't already exist */
1615 create_room(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1617 /* Set expiration policy to manual; otherwise objects will be lost! */
1618 if (lgetroom(&qr, USERNOTESROOM)) {
1619 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1622 qr.QRep.expire_mode = EXPIRE_MANUAL;
1623 qr.QRdefaultview = VIEW_NOTES;
1626 /* Set the view to a notes view */
1627 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1628 vbuf.v_view = VIEW_NOTES;
1629 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1636 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1639 void ical_send_out_invitations(icalcomponent *cal) {
1640 icalcomponent *the_request = NULL;
1641 char *serialized_request = NULL;
1642 icalcomponent *encaps = NULL;
1643 char *request_message_text = NULL;
1644 struct CtdlMessage *msg = NULL;
1645 struct recptypes *valid = NULL;
1646 char attendees_string[SIZ];
1647 int num_attendees = 0;
1648 char this_attendee[256];
1649 icalproperty *attendee = NULL;
1650 char summary_string[SIZ];
1651 icalproperty *summary = NULL;
1654 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
1659 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1660 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1661 ical_send_out_invitations(
1662 icalcomponent_get_first_component(
1663 cal, ICAL_VEVENT_COMPONENT
1669 /* Clone the event */
1670 the_request = icalcomponent_new_clone(cal);
1671 if (the_request == NULL) {
1672 lprintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
1676 /* Extract the summary string -- we'll use it as the
1677 * message subject for the request
1679 strcpy(summary_string, "Meeting request");
1680 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1681 if (summary != NULL) {
1682 if (icalproperty_get_summary(summary)) {
1683 strcpy(summary_string,
1684 icalproperty_get_summary(summary) );
1688 /* Determine who the recipients of this message are (the attendees) */
1689 strcpy(attendees_string, "");
1690 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1691 if (icalproperty_get_attendee(attendee)) {
1692 safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
1693 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1694 strcpy(this_attendee, &this_attendee[7]);
1696 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
1697 snprintf(&attendees_string[strlen(attendees_string)],
1698 sizeof(attendees_string) - strlen(attendees_string),
1708 lprintf(CTDL_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1710 /* If there are no attendees, there are no invitations to send, so...
1711 * don't bother putting one together! Punch out, Maverick!
1713 if (num_attendees == 0) {
1714 icalcomponent_free(the_request);
1718 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1719 encaps = icalcomponent_new_vcalendar();
1720 if (encaps == NULL) {
1721 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1722 __FILE__, __LINE__);
1723 icalcomponent_free(the_request);
1727 /* Set the Product ID */
1728 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1730 /* Set the Version Number */
1731 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1733 /* Set the method to REQUEST */
1734 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1736 /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1737 ical_dezonify(the_request);
1739 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1740 * are responsible for "the_request"'s memory -- it will be freed
1741 * when we free "encaps".
1743 icalcomponent_add_component(encaps, the_request);
1746 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1747 icalcomponent_free(encaps); /* Don't need this anymore. */
1748 if (serialized_request == NULL) return;
1750 request_message_text = malloc(strlen(serialized_request) + SIZ);
1751 if (request_message_text != NULL) {
1752 sprintf(request_message_text,
1753 "Content-type: text/calendar\r\n\r\n%s\r\n",
1757 msg = CtdlMakeMessage(&CC->user,
1758 "", /* No single recipient here */
1759 "", /* No single recipient here */
1760 CC->room.QRname, 0, FMT_RFC822,
1763 summary_string, /* Use summary for subject */
1765 request_message_text);
1768 valid = validate_recipients(attendees_string);
1769 CtdlSubmitMsg(msg, valid, "");
1770 CtdlFreeMessage(msg);
1771 free_recipients(valid);
1774 free(serialized_request);
1779 * When a calendar object is being saved, determine whether it's a VEVENT
1780 * and the user saving it is the organizer. If so, send out invitations
1781 * to any listed attendees.
1784 void ical_saving_vevent(icalcomponent *cal) {
1786 icalproperty *organizer = NULL;
1787 char organizer_string[SIZ];
1789 lprintf(CTDL_DEBUG, "ical_saving_vevent() has been called!\n");
1791 /* Don't send out invitations unless the client wants us to. */
1792 if (CIT_ICAL->server_generated_invitations == 0) {
1796 /* Don't send out invitations if we've been asked not to. */
1797 if (CIT_ICAL->avoid_sending_invitations > 0) {
1801 strcpy(organizer_string, "");
1803 * The VEVENT subcomponent is the one we're interested in.
1804 * Send out invitations if, and only if, this user is the Organizer.
1806 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1807 organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
1808 if (organizer != NULL) {
1809 if (icalproperty_get_organizer(organizer)) {
1810 strcpy(organizer_string,
1811 icalproperty_get_organizer(organizer));
1814 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1815 strcpy(organizer_string, &organizer_string[7]);
1816 striplt(organizer_string);
1818 * If the user saving the event is listed as the
1819 * organizer, then send out invitations.
1821 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
1822 ical_send_out_invitations(cal);
1827 /* If the component has subcomponents, recurse through them. */
1828 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1830 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1831 /* Recursively process subcomponent */
1832 ical_saving_vevent(c);
1840 * Back end for ical_obj_beforesave()
1841 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1842 * the summary of the event (becomes message subject),
1843 * and the start time (becomes message date/time).
1845 void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
1846 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1847 char *encoding, void *cbuserdata)
1849 icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
1851 struct icalmessagemod *imm;
1854 imm = (struct icalmessagemod *)cbuserdata;
1856 /* We're only interested in calendar data. */
1857 if (strcasecmp(cbtype, "text/calendar")) {
1861 /* Hunt for the UID and drop it in
1862 * the "user data" pointer for the MIME parser. When
1863 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1866 whole_cal = icalcomponent_new_from_string(content);
1869 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1870 nested_event = icalcomponent_get_first_component(
1871 cal, ICAL_VEVENT_COMPONENT);
1872 if (nested_event != NULL) {
1876 nested_todo = icalcomponent_get_first_component(
1877 cal, ICAL_VTODO_COMPONENT);
1878 if (nested_todo != NULL) {
1885 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1887 /* If there's no uid we must generate one */
1888 generate_uuid(new_uid);
1889 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
1890 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1893 strcpy(imm->uid, icalproperty_get_comment(p));
1895 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
1897 strcpy(imm->subject, icalproperty_get_comment(p));
1899 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
1901 imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
1904 icalcomponent_free(cal);
1905 if (whole_cal != cal) {
1906 icalcomponent_free(whole_cal);
1915 * See if we need to prevent the object from being saved (we don't allow
1916 * MIME types other than text/calendar in "calendar" or "tasks" rooms). Also,
1917 * when saving an event to the calendar, set the message's Citadel exclusive
1918 * message ID to the UID of the object. This causes our replication checker to
1919 * automatically delete any existing instances of the same object. (Isn't
1922 * We also set the message's Subject to the event summary, and the Date/time to
1923 * the event start time.
1925 int ical_obj_beforesave(struct CtdlMessage *msg)
1927 struct icalmessagemod imm;
1929 /* First determine if this is a calendar or tasks room */
1930 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1931 && (CC->room.QRdefaultview != VIEW_TASKS)
1933 return(0); /* Not a vCalendar-centric room */
1936 /* It must be an RFC822 message! */
1937 if (msg->cm_format_type != 4) {
1938 lprintf(CTDL_DEBUG, "Rejecting non-RFC822 message\n");
1939 return(1); /* You tried to save a non-RFC822 message! */
1942 if (msg->cm_fields['M'] == NULL) {
1943 return(1); /* You tried to save a null message! */
1946 memset(&imm, 0, sizeof(struct icalmessagemod));
1948 /* Do all of our lovely back-end parsing */
1949 mime_parser(msg->cm_fields['M'],
1951 *ical_ctdl_set_exclusive_msgid,
1957 if (strlen(imm.uid) > 0) {
1958 if (msg->cm_fields['E'] != NULL) {
1959 free(msg->cm_fields['E']);
1961 msg->cm_fields['E'] = strdup(imm.uid);
1962 lprintf(CTDL_DEBUG, "Saving calendar UID <%s>\n", msg->cm_fields['E']);
1964 if (strlen(imm.subject) > 0) {
1965 if (msg->cm_fields['U'] != NULL) {
1966 free(msg->cm_fields['U']);
1968 msg->cm_fields['U'] = strdup(imm.subject);
1970 if (imm.dtstart > 0) {
1971 if (msg->cm_fields['T'] != NULL) {
1972 free(msg->cm_fields['T']);
1974 msg->cm_fields['T'] = strdup("000000000000000000");
1975 sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
1983 * Things we need to do after saving a calendar event.
1985 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
1986 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1987 char *encoding, void *cbuserdata)
1991 /* We're only interested in calendar items here. */
1992 if (strcasecmp(cbtype, "text/calendar")) {
1996 /* Hunt for the UID and drop it in
1997 * the "user data" pointer for the MIME parser. When
1998 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2001 if (!strcasecmp(cbtype, "text/calendar")) {
2002 cal = icalcomponent_new_from_string(content);
2004 ical_saving_vevent(cal);
2005 icalcomponent_free(cal);
2012 * Things we need to do after saving a calendar event.
2013 * (This will start back end tasks such as automatic generation of invitations,
2014 * if such actions are appropriate.)
2016 int ical_obj_aftersave(struct CtdlMessage *msg)
2018 char roomname[ROOMNAMELEN];
2021 * If this isn't the Calendar> room, no further action is necessary.
2024 /* First determine if this is our room */
2025 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
2026 if (strcasecmp(roomname, CC->room.QRname)) {
2027 return(0); /* Not the Calendar room -- don't do anything. */
2030 /* It must be an RFC822 message! */
2031 if (msg->cm_format_type != 4) return(1);
2033 /* Reject null messages */
2034 if (msg->cm_fields['M'] == NULL) return(1);
2036 /* Now recurse through it looking for our icalendar data */
2037 mime_parser(msg->cm_fields['M'],
2039 *ical_obj_aftersave_backend,
2049 void ical_session_startup(void) {
2050 CIT_ICAL = malloc(sizeof(struct cit_ical));
2051 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
2054 void ical_session_shutdown(void) {
2060 * Back end for ical_fixed_output()
2062 void ical_fixed_output_backend(icalcomponent *cal,
2069 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
2071 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2074 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
2076 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2079 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
2081 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2084 /* If the component has attendees, iterate through them. */
2085 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
2086 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
2087 if (!strncasecmp(buf, "MAILTO:", 7)) {
2089 /* screen name or email address */
2090 strcpy(buf, &buf[7]);
2092 cprintf("%s ", buf);
2097 /* If the component has subcomponents, recurse through them. */
2098 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2100 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2101 /* Recursively process subcomponent */
2102 ical_fixed_output_backend(c, recursion_level+1);
2109 * Function to output vcalendar data as plain text. Nobody uses MSG0
2110 * anymore, so really this is just so we expose the vCard data to the full
2113 void ical_fixed_output(char *ptr, int len) {
2117 stringy_cal = malloc(len + 1);
2118 safestrncpy(stringy_cal, ptr, len + 1);
2119 cal = icalcomponent_new_from_string(stringy_cal);
2127 ical_fixed_output_backend(cal, 0);
2129 /* Free the memory we obtained from libical's constructor */
2130 icalcomponent_free(cal);
2134 #endif /* CITADEL_WITH_CALENDAR_SERVICE */
2137 * Register this module with the Citadel server.
2139 char *serv_calendar_init(void)
2141 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2142 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2143 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2144 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
2145 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2146 CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2147 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2148 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2155 void serv_calendar_destroy(void)
2157 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2158 icaltimezone_free_builtin_timezones();