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>
22 #include <libcitadel.h>
25 #include "citserver.h"
31 #include "internet_addressing.h"
32 #include "serv_calendar.h"
33 #include "euidindex.h"
34 #include "ctdl_module.h"
36 #ifdef CITADEL_WITH_CALENDAR_SERVICE
39 #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.
107 * If the supplied user is NULL, this function writes the calendar object
108 * to the currently selected room.
110 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
114 icalcomponent *encaps = NULL;
115 struct CtdlMessage *msg = NULL;
116 icalcomponent *tmp=NULL;
118 if (cal == NULL) return;
120 /* If the supplied object is a subcomponent, encapsulate it in
121 * a full VCALENDAR component, and save that instead.
123 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
124 tmp = icalcomponent_new_clone(cal);
125 encaps = ical_encapsulate_subcomponent(tmp);
126 ical_write_to_cal(u, encaps);
127 icalcomponent_free(tmp);
128 icalcomponent_free(encaps);
132 ser = icalcomponent_as_ical_string(cal);
133 if (ser == NULL) return;
135 /* If the caller supplied a user, write to that user's default calendar room */
137 /* Make a temp file out of it */
138 CtdlMakeTempFileName(temp, sizeof temp);
139 fp = fopen(temp, "w");
141 fwrite(ser, strlen(ser), 1, fp);
144 /* This handy API function does all the work for us. */
145 CtdlWriteObject(USERCALENDARROOM, /* which room */
146 "text/calendar", /* MIME type */
147 temp, /* temp file */
150 0, /* don't delete others of this type */
157 /* If the caller did not supply a user, write to the currently selected room */
159 msg = malloc(sizeof(struct CtdlMessage));
160 memset(msg, 0, sizeof(struct CtdlMessage));
161 msg->cm_magic = CTDLMESSAGE_MAGIC;
162 msg->cm_anon_type = MES_NORMAL;
163 msg->cm_format_type = 4;
164 msg->cm_fields['A'] = strdup(CC->user.fullname);
165 msg->cm_fields['O'] = strdup(CC->room.QRname);
166 msg->cm_fields['N'] = strdup(config.c_nodename);
167 msg->cm_fields['H'] = strdup(config.c_humannode);
168 msg->cm_fields['M'] = malloc(strlen(ser) + 40);
169 strcpy(msg->cm_fields['M'], "Content-type: text/calendar\r\n\r\n");
170 strcat(msg->cm_fields['M'], ser);
172 /* Now write the data */
173 CtdlSubmitMsg(msg, NULL, "");
174 CtdlFreeMessage(msg);
177 /* In either case, now we can free the serialized calendar object */
183 * Add a calendar object to the user's calendar
185 * ok because it uses ical_write_to_cal()
187 void ical_add(icalcomponent *cal, int recursion_level) {
191 * The VEVENT subcomponent is the one we're interested in saving.
193 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
195 ical_write_to_cal(&CC->user, cal);
199 /* If the component has subcomponents, recurse through them. */
200 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
202 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
203 /* Recursively process subcomponent */
204 ical_add(c, recursion_level+1);
212 * Send a reply to a meeting invitation.
214 * 'request' is the invitation to reply to.
215 * 'action' is the string "accept" or "decline" or "tentative".
218 void ical_send_a_reply(icalcomponent *request, char *action) {
219 icalcomponent *the_reply = NULL;
220 icalcomponent *vevent = NULL;
221 icalproperty *attendee = NULL;
222 char attendee_string[SIZ];
223 icalproperty *organizer = NULL;
224 char organizer_string[SIZ];
225 icalproperty *summary = NULL;
226 char summary_string[SIZ];
227 icalproperty *me_attend = NULL;
228 struct recptypes *recp = NULL;
229 icalparameter *partstat = NULL;
230 char *serialized_reply = NULL;
231 char *reply_message_text = NULL;
232 struct CtdlMessage *msg = NULL;
233 struct recptypes *valid = NULL;
235 strcpy(organizer_string, "");
236 strcpy(summary_string, "Calendar item");
238 if (request == NULL) {
239 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
243 the_reply = icalcomponent_new_clone(request);
244 if (the_reply == NULL) {
245 lprintf(CTDL_ERR, "ERROR: cannot clone request\n");
249 /* Change the method from REQUEST to REPLY */
250 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
252 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
253 if (vevent != NULL) {
254 /* Hunt for attendees, removing ones that aren't us.
255 * (Actually, remove them all, cloning our own one so we can
256 * re-insert it later)
258 while (attendee = icalcomponent_get_first_property(vevent,
259 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
261 if (icalproperty_get_attendee(attendee)) {
262 strcpy(attendee_string,
263 icalproperty_get_attendee(attendee) );
264 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
265 strcpy(attendee_string, &attendee_string[7]);
266 striplt(attendee_string);
267 recp = validate_recipients(attendee_string);
269 if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
270 if (me_attend) icalproperty_free(me_attend);
271 me_attend = icalproperty_new_clone(attendee);
273 free_recipients(recp);
278 icalcomponent_remove_property(vevent, attendee);
279 icalproperty_free(attendee);
282 /* We found our own address in the attendee list. */
284 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
285 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
287 if (!strcasecmp(action, "accept")) {
288 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
290 else if (!strcasecmp(action, "decline")) {
291 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
293 else if (!strcasecmp(action, "tentative")) {
294 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
297 if (partstat) icalproperty_add_parameter(me_attend, partstat);
299 /* Now insert it back into the vevent. */
300 icalcomponent_add_property(vevent, me_attend);
303 /* Figure out who to send this thing to */
304 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
305 if (organizer != NULL) {
306 if (icalproperty_get_organizer(organizer)) {
307 strcpy(organizer_string,
308 icalproperty_get_organizer(organizer) );
311 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
312 strcpy(organizer_string, &organizer_string[7]);
313 striplt(organizer_string);
315 strcpy(organizer_string, "");
318 /* Extract the summary string -- we'll use it as the
319 * message subject for the reply
321 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
322 if (summary != NULL) {
323 if (icalproperty_get_summary(summary)) {
324 strcpy(summary_string,
325 icalproperty_get_summary(summary) );
330 /* Now generate the reply message and send it out. */
331 serialized_reply = strdup(icalcomponent_as_ical_string(the_reply));
332 icalcomponent_free(the_reply); /* don't need this anymore */
333 if (serialized_reply == NULL) return;
335 reply_message_text = malloc(strlen(serialized_reply) + SIZ);
336 if (reply_message_text != NULL) {
337 sprintf(reply_message_text,
338 "Content-type: text/calendar charset=\"utf-8\"\r\n\r\n%s\r\n",
342 msg = CtdlMakeMessage(&CC->user,
343 organizer_string, /* to */
345 CC->room.QRname, 0, FMT_RFC822,
348 summary_string, /* Use summary for subject */
353 valid = validate_recipients(organizer_string);
354 CtdlSubmitMsg(msg, valid, "");
355 CtdlFreeMessage(msg);
356 free_recipients(valid);
359 free(serialized_reply);
365 * Callback function for mime parser that hunts for calendar content types
366 * and turns them into calendar objects
368 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
369 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
372 struct ical_respond_data *ird = NULL;
374 ird = (struct ical_respond_data *) cbuserdata;
376 /* desired_partnum can be set to "_HUNT_" to have it just look for
377 * the first part with a content type of text/calendar. Otherwise
378 * we have to only process the right one.
380 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
381 if (strcasecmp(partnum, ird->desired_partnum)) {
386 if (strcasecmp(cbtype, "text/calendar")) {
390 if (ird->cal != NULL) {
391 icalcomponent_free(ird->cal);
395 ird->cal = icalcomponent_new_from_string(content);
396 if (ird->cal != NULL) {
397 ical_dezonify(ird->cal);
403 * Respond to a meeting request.
405 void ical_respond(long msgnum, char *partnum, char *action) {
406 struct CtdlMessage *msg = NULL;
407 struct ical_respond_data ird;
410 (strcasecmp(action, "accept"))
411 && (strcasecmp(action, "decline"))
413 cprintf("%d Action must be 'accept' or 'decline'\n",
414 ERROR + ILLEGAL_VALUE
419 msg = CtdlFetchMessage(msgnum, 1);
421 cprintf("%d Message %ld not found.\n",
422 ERROR + ILLEGAL_VALUE,
428 memset(&ird, 0, sizeof ird);
429 strcpy(ird.desired_partnum, partnum);
430 mime_parser(msg->cm_fields['M'],
432 *ical_locate_part, /* callback function */
434 (void *) &ird, /* user data */
438 /* We're done with the incoming message, because we now have a
439 * calendar object in memory.
441 CtdlFreeMessage(msg);
444 * Here is the real meat of this function. Handle the event.
446 if (ird.cal != NULL) {
447 /* Save this in the user's calendar if necessary */
448 if (!strcasecmp(action, "accept")) {
449 ical_add(ird.cal, 0);
452 /* Send a reply if necessary */
453 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
454 ical_send_a_reply(ird.cal, action);
457 /* Now that we've processed this message, we don't need it
458 * anymore. So delete it. (NOTE we don't do this anymore.)
459 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
462 /* Free the memory we allocated and return a response. */
463 icalcomponent_free(ird.cal);
465 cprintf("%d ok\n", CIT_OK);
469 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
473 /* should never get here */
478 * Figure out the UID of the calendar event being referred to in a
479 * REPLY object. This function is recursive.
481 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
482 icalcomponent *subcomponent;
485 /* If this object is a REPLY, then extract the UID. */
486 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
487 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
489 strcpy(uidbuf, icalproperty_get_comment(p));
493 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
494 * UID of the reply; we want the UID of the invitation being replied to.
496 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
497 subcomponent != NULL;
498 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
499 ical_learn_uid_of_reply(uidbuf, subcomponent);
505 * ical_update_my_calendar_with_reply() refers to this callback function; when we
506 * locate the message containing the calendar event we're replying to, this function
507 * gets called. It basically just sticks the message number in a supplied buffer.
509 void ical_hunt_for_event_to_update(long msgnum, void *data) {
512 msgnumptr = (long *) data;
517 struct original_event_container {
522 * Callback function for mime parser that hunts for calendar content types
523 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
524 * to fetch the object being updated)
526 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
527 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
530 struct original_event_container *oec = NULL;
532 if (strcasecmp(cbtype, "text/calendar")) {
535 oec = (struct original_event_container *) cbuserdata;
536 if (oec->c != NULL) {
537 icalcomponent_free(oec->c);
539 oec->c = icalcomponent_new_from_string(content);
544 * Merge updated attendee information from a REPLY into an existing event.
546 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
548 icalproperty *e_attendee, *r_attendee;
550 /* First things first. If we're not looking at a VEVENT component,
551 * recurse through subcomponents until we find one.
553 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
554 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
556 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
557 ical_merge_attendee_reply(c, reply);
562 /* Now do the same thing with the reply.
564 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
565 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
567 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
568 ical_merge_attendee_reply(event, c);
573 /* Clone the reply, because we're going to rip its guts out. */
574 reply = icalcomponent_new_clone(reply);
576 /* At this point we're looking at the correct subcomponents.
577 * Iterate through the attendees looking for a match.
580 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
582 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
584 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
586 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
588 /* Check to see if these two attendees match...
591 icalproperty_get_attendee(e_attendee),
592 icalproperty_get_attendee(r_attendee)
594 /* ...and if they do, remove the attendee from the event
595 * and replace it with the attendee from the reply. (The
596 * reply's copy will have the same address, but an updated
599 icalcomponent_remove_property(event, e_attendee);
600 icalproperty_free(e_attendee);
601 icalcomponent_remove_property(reply, r_attendee);
602 icalcomponent_add_property(event, r_attendee);
604 /* Since we diddled both sets of attendees, we have to start
605 * the iteration over again. This will not create an infinite
606 * loop because we removed the attendee from the reply. (That's
607 * why we cloned the reply, and that's what we mean by "ripping
616 /* Free the *clone* of the reply. */
617 icalcomponent_free(reply);
624 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
625 * calendar event. The object has already been deserialized for us; all
626 * we have to do here is hunt for the event in our calendar, merge in the
627 * updated attendee status, and save it again.
629 * This function returns 0 on success, 1 if the event was not found in the
630 * user's calendar, or 2 if an internal error occurred.
632 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
634 char hold_rm[ROOMNAMELEN];
635 long msgnum_being_replaced = 0;
636 struct CtdlMessage *msg = NULL;
637 struct original_event_container oec;
638 icalcomponent *original_event;
639 char *serialized_event = NULL;
640 char roomname[ROOMNAMELEN];
641 char *message_text = NULL;
643 /* Figure out just what event it is we're dealing with */
644 strcpy(uid, "--==<< InVaLiD uId >>==--");
645 ical_learn_uid_of_reply(uid, cal);
646 lprintf(CTDL_DEBUG, "UID of event being replied to is <%s>\n", uid);
648 strcpy(hold_rm, CC->room.QRname); /* save current room */
650 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
651 getroom(&CC->room, hold_rm);
652 lprintf(CTDL_CRIT, "cannot get user calendar room\n");
657 * Look in the EUID index for a message with
658 * the Citadel EUID set to the value we're looking for. Since
659 * Citadel always sets the message EUID to the vCalendar UID of
660 * the event, this will work.
662 msgnum_being_replaced = locate_message_by_euid(uid, &CC->room);
664 getroom(&CC->room, hold_rm); /* return to saved room */
666 lprintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
667 if (msgnum_being_replaced == 0) {
668 return(1); /* no calendar event found */
671 /* Now we know the ID of the message containing the event being updated.
672 * We don't actually have to delete it; that'll get taken care of by the
673 * server when we save another event with the same UID. This just gives
674 * us the ability to load the event into memory so we can diddle the
677 msg = CtdlFetchMessage(msgnum_being_replaced, 1);
679 return(2); /* internal error */
682 mime_parser(msg->cm_fields['M'],
684 *ical_locate_original_event, /* callback function */
686 &oec, /* user data */
689 CtdlFreeMessage(msg);
691 original_event = oec.c;
692 if (original_event == NULL) {
693 lprintf(CTDL_ERR, "ERROR: Original_component is NULL.\n");
697 /* Merge the attendee's updated status into the event */
698 ical_merge_attendee_reply(original_event, cal);
701 serialized_event = strdup(icalcomponent_as_ical_string(original_event));
702 icalcomponent_free(original_event); /* Don't need this anymore. */
703 if (serialized_event == NULL) return(2);
705 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
707 message_text = malloc(strlen(serialized_event) + SIZ);
708 if (message_text != NULL) {
709 sprintf(message_text,
710 "Content-type: text/calendar charset=\"utf-8\"\r\n\r\n%s\r\n",
714 msg = CtdlMakeMessage(&CC->user,
715 "", /* No recipient */
716 "", /* No recipient */
726 CIT_ICAL->avoid_sending_invitations = 1;
727 CtdlSubmitMsg(msg, NULL, roomname);
728 CtdlFreeMessage(msg);
729 CIT_ICAL->avoid_sending_invitations = 0;
732 free(serialized_event);
738 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
739 * simply extracts the calendar object from the message, deserializes it, and
740 * passes it up to ical_update_my_calendar_with_reply() for processing.
742 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
743 struct CtdlMessage *msg = NULL;
744 struct ical_respond_data ird;
748 (strcasecmp(action, "update"))
749 && (strcasecmp(action, "ignore"))
751 cprintf("%d Action must be 'update' or 'ignore'\n",
752 ERROR + ILLEGAL_VALUE
757 msg = CtdlFetchMessage(msgnum, 1);
759 cprintf("%d Message %ld not found.\n",
760 ERROR + ILLEGAL_VALUE,
766 memset(&ird, 0, sizeof ird);
767 strcpy(ird.desired_partnum, partnum);
768 mime_parser(msg->cm_fields['M'],
770 *ical_locate_part, /* callback function */
772 (void *) &ird, /* user data */
776 /* We're done with the incoming message, because we now have a
777 * calendar object in memory.
779 CtdlFreeMessage(msg);
782 * Here is the real meat of this function. Handle the event.
784 if (ird.cal != NULL) {
785 /* Update the user's calendar if necessary */
786 if (!strcasecmp(action, "update")) {
787 ret = ical_update_my_calendar_with_reply(ird.cal);
789 cprintf("%d Your calendar has been updated with this reply.\n",
793 cprintf("%d This event does not exist in your calendar.\n",
794 ERROR + FILE_NOT_FOUND);
797 cprintf("%d An internal error occurred.\n",
798 ERROR + INTERNAL_ERROR);
802 cprintf("%d This reply has been ignored.\n", CIT_OK);
805 /* Now that we've processed this message, we don't need it
806 * anymore. So delete it. (Don't do this anymore.)
807 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
810 /* Free the memory we allocated and return a response. */
811 icalcomponent_free(ird.cal);
816 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
820 /* should never get here */
825 * Search for a property in both the top level and in a VEVENT subcomponent
827 icalproperty *ical_ctdl_get_subprop(
829 icalproperty_kind which_prop
834 p = icalcomponent_get_first_property(cal, which_prop);
836 c = icalcomponent_get_first_component(cal,
837 ICAL_VEVENT_COMPONENT);
839 p = icalcomponent_get_first_property(c, which_prop);
847 * Check to see if two events overlap. Returns nonzero if they do.
848 * (This function is used in both Citadel and WebCit. If you change it in
849 * one place, change it in the other. Better yet, put it in a library.)
851 int ical_ctdl_is_overlap(
852 struct icaltimetype t1start,
853 struct icaltimetype t1end,
854 struct icaltimetype t2start,
855 struct icaltimetype t2end
858 if (icaltime_is_null_time(t1start)) return(0);
859 if (icaltime_is_null_time(t2start)) return(0);
861 /* First, check for all-day events */
862 if (t1start.is_date) {
863 if (!icaltime_compare_date_only(t1start, t2start)) {
866 if (!icaltime_is_null_time(t2end)) {
867 if (!icaltime_compare_date_only(t1start, t2end)) {
873 if (t2start.is_date) {
874 if (!icaltime_compare_date_only(t2start, t1start)) {
877 if (!icaltime_is_null_time(t1end)) {
878 if (!icaltime_compare_date_only(t2start, t1end)) {
884 /* Now check for overlaps using date *and* time. */
886 /* First, bail out if either event 1 or event 2 is missing end time. */
887 if (icaltime_is_null_time(t1end)) return(0);
888 if (icaltime_is_null_time(t2end)) return(0);
890 /* If event 1 ends before event 2 starts, we're in the clear. */
891 if (icaltime_compare(t1end, t2start) <= 0) return(0);
893 /* If event 2 ends before event 1 starts, we're also ok. */
894 if (icaltime_compare(t2end, t1start) <= 0) return(0);
896 /* Otherwise, they overlap. */
903 * Backend for ical_hunt_for_conflicts()
905 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
907 struct CtdlMessage *msg = NULL;
908 struct ical_respond_data ird;
909 struct icaltimetype t1start, t1end, t2start, t2end;
911 char conflict_event_uid[SIZ];
912 char conflict_event_summary[SIZ];
913 char compare_uid[SIZ];
915 cal = (icalcomponent *)data;
916 strcpy(compare_uid, "");
917 strcpy(conflict_event_uid, "");
918 strcpy(conflict_event_summary, "");
920 msg = CtdlFetchMessage(msgnum, 1);
921 if (msg == NULL) return;
922 memset(&ird, 0, sizeof ird);
923 strcpy(ird.desired_partnum, "_HUNT_");
924 mime_parser(msg->cm_fields['M'],
926 *ical_locate_part, /* callback function */
928 (void *) &ird, /* user data */
931 CtdlFreeMessage(msg);
933 if (ird.cal == NULL) return;
935 t1start = icaltime_null_time();
936 t1end = icaltime_null_time();
937 t2start = icaltime_null_time();
938 t1end = icaltime_null_time();
940 /* Now compare cal to ird.cal */
941 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTSTART_PROPERTY);
942 if (p == NULL) return;
943 if (p != NULL) t2start = icalproperty_get_dtstart(p);
945 p = ical_ctdl_get_subprop(ird.cal, ICAL_DTEND_PROPERTY);
946 if (p != NULL) t2end = icalproperty_get_dtend(p);
948 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
949 if (p == NULL) return;
950 if (p != NULL) t1start = icalproperty_get_dtstart(p);
952 p = ical_ctdl_get_subprop(cal, ICAL_DTEND_PROPERTY);
953 if (p != NULL) t1end = icalproperty_get_dtend(p);
955 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
957 strcpy(compare_uid, icalproperty_get_comment(p));
960 p = ical_ctdl_get_subprop(ird.cal, ICAL_UID_PROPERTY);
962 strcpy(conflict_event_uid, icalproperty_get_comment(p));
965 p = ical_ctdl_get_subprop(ird.cal, ICAL_SUMMARY_PROPERTY);
967 strcpy(conflict_event_summary, icalproperty_get_comment(p));
970 icalcomponent_free(ird.cal);
972 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
973 cprintf("%ld||%s|%s|%d|\n",
976 conflict_event_summary,
977 ( ((strlen(compare_uid)>0)
978 &&(!strcasecmp(compare_uid,
979 conflict_event_uid))) ? 1 : 0
988 * Phase 2 of "hunt for conflicts" operation.
989 * At this point we have a calendar object which represents the VEVENT that
990 * we're considering adding to the calendar. Now hunt through the user's
991 * calendar room, and output zero or more existing VEVENTs which conflict
994 void ical_hunt_for_conflicts(icalcomponent *cal) {
995 char hold_rm[ROOMNAMELEN];
997 strcpy(hold_rm, CC->room.QRname); /* save current room */
999 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
1000 getroom(&CC->room, hold_rm);
1001 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
1005 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
1007 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1010 ical_hunt_for_conflicts_backend,
1015 getroom(&CC->room, hold_rm); /* return to saved room */
1022 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
1024 void ical_conflicts(long msgnum, char *partnum) {
1025 struct CtdlMessage *msg = NULL;
1026 struct ical_respond_data ird;
1028 msg = CtdlFetchMessage(msgnum, 1);
1030 cprintf("%d Message %ld not found.\n",
1031 ERROR + ILLEGAL_VALUE,
1037 memset(&ird, 0, sizeof ird);
1038 strcpy(ird.desired_partnum, partnum);
1039 mime_parser(msg->cm_fields['M'],
1041 *ical_locate_part, /* callback function */
1043 (void *) &ird, /* user data */
1047 CtdlFreeMessage(msg);
1049 if (ird.cal != NULL) {
1050 ical_hunt_for_conflicts(ird.cal);
1051 icalcomponent_free(ird.cal);
1055 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1059 /* should never get here */
1065 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1067 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1070 struct icalperiodtype my_period;
1072 if (cal == NULL) return;
1073 my_period = icalperiodtype_null_period();
1075 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1076 ical_add_to_freebusy(fb,
1077 icalcomponent_get_first_component(
1078 cal, ICAL_VEVENT_COMPONENT
1086 /* If this event is not opaque, the user isn't publishing it as
1087 * busy time, so don't bother doing anything else.
1089 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1091 v = icalproperty_get_value(p);
1093 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1099 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1100 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1102 my_period.start = icalproperty_get_dtstart(p);
1105 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1107 my_period.end = icalproperty_get_dtstart(p);
1111 icalcomponent_add_property(fb,
1112 icalproperty_new_freebusy(my_period)
1115 /* Make sure the DTSTART property of the freebusy *list* is set to
1116 * the DTSTART property of the *earliest event*.
1118 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1120 icalcomponent_set_dtstart(fb,
1121 icalcomponent_get_dtstart(cal) );
1124 if (icaltime_compare(
1125 icalcomponent_get_dtstart(cal),
1126 icalcomponent_get_dtstart(fb)
1128 icalcomponent_set_dtstart(fb,
1129 icalcomponent_get_dtstart(cal) );
1133 /* Make sure the DTEND property of the freebusy *list* is set to
1134 * the DTEND property of the *latest event*.
1136 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1138 icalcomponent_set_dtend(fb,
1139 icalcomponent_get_dtend(cal) );
1142 if (icaltime_compare(
1143 icalcomponent_get_dtend(cal),
1144 icalcomponent_get_dtend(fb)
1146 icalcomponent_set_dtend(fb,
1147 icalcomponent_get_dtend(cal) );
1156 * Backend for ical_freebusy()
1158 * This function simply loads the messages in the user's calendar room,
1159 * which contain VEVENTs, then strips them of all non-freebusy data, and
1160 * adds them to the supplied VCALENDAR.
1163 void ical_freebusy_backend(long msgnum, void *data) {
1165 struct CtdlMessage *msg = NULL;
1166 struct ical_respond_data ird;
1168 cal = (icalcomponent *)data;
1170 msg = CtdlFetchMessage(msgnum, 1);
1171 if (msg == NULL) return;
1172 memset(&ird, 0, sizeof ird);
1173 strcpy(ird.desired_partnum, "_HUNT_");
1174 mime_parser(msg->cm_fields['M'],
1176 *ical_locate_part, /* callback function */
1178 (void *) &ird, /* user data */
1181 CtdlFreeMessage(msg);
1183 if (ird.cal == NULL) return;
1185 ical_add_to_freebusy(cal, ird.cal);
1187 /* Now free the memory. */
1188 icalcomponent_free(ird.cal);
1194 * Grab another user's free/busy times
1196 void ical_freebusy(char *who) {
1197 struct ctdluser usbuf;
1198 char calendar_room_name[ROOMNAMELEN];
1199 char hold_rm[ROOMNAMELEN];
1200 char *serialized_request = NULL;
1201 icalcomponent *encaps = NULL;
1202 icalcomponent *fb = NULL;
1203 int found_user = (-1);
1204 struct recptypes *recp = NULL;
1209 int config_lines = 0;
1211 /* First try an exact match. */
1212 found_user = getuser(&usbuf, who);
1214 /* If not found, try it as an unqualified email address. */
1215 if (found_user != 0) {
1217 recp = validate_recipients(buf);
1218 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1220 if (recp->num_local == 1) {
1221 found_user = getuser(&usbuf, recp->recp_local);
1223 free_recipients(recp);
1227 /* If still not found, try it as an address qualified with the
1228 * primary FQDN of this Citadel node.
1230 if (found_user != 0) {
1231 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1232 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1233 recp = validate_recipients(buf);
1235 if (recp->num_local == 1) {
1236 found_user = getuser(&usbuf, recp->recp_local);
1238 free_recipients(recp);
1242 /* Still not found? Try qualifying it with every domain we
1243 * might have addresses in.
1245 if (found_user != 0) {
1246 config_lines = num_tokens(inetcfg, '\n');
1247 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1248 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1249 extract_token(host, buf, 0, '|', sizeof host);
1250 extract_token(type, buf, 1, '|', sizeof type);
1252 if ( (!strcasecmp(type, "localhost"))
1253 || (!strcasecmp(type, "directory")) ) {
1254 snprintf(buf, sizeof buf, "%s@%s", who, host);
1255 lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1256 recp = validate_recipients(buf);
1258 if (recp->num_local == 1) {
1259 found_user = getuser(&usbuf, recp->recp_local);
1261 free_recipients(recp);
1267 if (found_user != 0) {
1268 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1272 MailboxName(calendar_room_name, sizeof calendar_room_name,
1273 &usbuf, USERCALENDARROOM);
1275 strcpy(hold_rm, CC->room.QRname); /* save current room */
1277 if (getroom(&CC->room, calendar_room_name) != 0) {
1278 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1279 getroom(&CC->room, hold_rm);
1283 /* Create a VFREEBUSY subcomponent */
1284 lprintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
1285 fb = icalcomponent_new_vfreebusy();
1287 cprintf("%d Internal error: cannot allocate memory.\n",
1288 ERROR + INTERNAL_ERROR);
1289 getroom(&CC->room, hold_rm);
1293 /* Set the method to PUBLISH */
1294 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1296 /* Set the DTSTAMP to right now. */
1297 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1299 /* Add the user's email address as ORGANIZER */
1300 sprintf(buf, "MAILTO:%s", who);
1301 if (strchr(buf, '@') == NULL) {
1303 strcat(buf, config.c_fqdn);
1305 for (i=0; buf[i]; ++i) {
1306 if (buf[i]==' ') buf[i] = '_';
1308 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1310 /* Add busy time from events */
1311 lprintf(CTDL_DEBUG, "Adding busy time from events\n");
1312 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
1314 /* If values for DTSTART and DTEND are still not present, set them
1315 * to yesterday and tomorrow as default values.
1317 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1318 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1320 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1321 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1324 /* Put the freebusy component into the calendar component */
1325 lprintf(CTDL_DEBUG, "Encapsulating\n");
1326 encaps = ical_encapsulate_subcomponent(fb);
1327 if (encaps == NULL) {
1328 icalcomponent_free(fb);
1329 cprintf("%d Internal error: cannot allocate memory.\n",
1330 ERROR + INTERNAL_ERROR);
1331 getroom(&CC->room, hold_rm);
1335 /* Set the method to PUBLISH */
1336 lprintf(CTDL_DEBUG, "Setting method\n");
1337 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1340 lprintf(CTDL_DEBUG, "Serializing\n");
1341 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1342 icalcomponent_free(encaps); /* Don't need this anymore. */
1344 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1345 if (serialized_request != NULL) {
1346 client_write(serialized_request, strlen(serialized_request));
1347 free(serialized_request);
1351 /* Go back to the room from which we came... */
1352 getroom(&CC->room, hold_rm);
1358 * Backend for ical_getics()
1360 * This is a ForEachMessage() callback function that searches the current room
1361 * for calendar events and adds them each into one big calendar component.
1363 void ical_getics_backend(long msgnum, void *data) {
1364 icalcomponent *encaps, *c;
1365 struct CtdlMessage *msg = NULL;
1366 struct ical_respond_data ird;
1368 encaps = (icalcomponent *)data;
1369 if (encaps == NULL) return;
1371 /* Look for the calendar event... */
1373 msg = CtdlFetchMessage(msgnum, 1);
1374 if (msg == NULL) return;
1375 memset(&ird, 0, sizeof ird);
1376 strcpy(ird.desired_partnum, "_HUNT_");
1377 mime_parser(msg->cm_fields['M'],
1379 *ical_locate_part, /* callback function */
1381 (void *) &ird, /* user data */
1384 CtdlFreeMessage(msg);
1386 if (ird.cal == NULL) return;
1388 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1389 * are responsible for "the_request"'s memory -- it will be freed
1390 * when we free "encaps".
1393 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1394 * in. This will almost never happen.
1396 if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
1397 icalcomponent_add_component(encaps, ird.cal);
1400 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1401 * and other components encapsulated inside, we have to extract them.
1404 for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
1406 c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
1407 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1409 icalcomponent_free(ird.cal);
1416 * Retrieve all of the calendar items in the current room, and output them
1417 * as a single icalendar object.
1419 void ical_getics(void)
1421 icalcomponent *encaps = NULL;
1424 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1425 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1426 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1427 return; /* Not a vCalendar-centric room */
1430 encaps = icalcomponent_new_vcalendar();
1431 if (encaps == NULL) {
1432 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1433 __FILE__, __LINE__);
1434 cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
1438 cprintf("%d one big calendar\n", LISTING_FOLLOWS);
1440 /* Set the Product ID */
1441 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1443 /* Set the Version Number */
1444 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1446 /* Set the method to PUBLISH */
1447 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1449 /* Now go through the room encapsulating all calendar items. */
1450 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1453 ical_getics_backend,
1457 ser = strdup(icalcomponent_as_ical_string(encaps));
1458 client_write(ser, strlen(ser));
1461 icalcomponent_free(encaps); /* Don't need this anymore. */
1467 * Delete all of the calendar items in the current room, and replace them
1468 * with calendar items from a client-supplied data stream.
1470 void ical_putics(void)
1472 char *calstream = NULL;
1476 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1477 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1478 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1479 return; /* Not a vCalendar-centric room */
1482 if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
1483 cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
1487 cprintf("%d Transmit data now\n", SEND_LISTING);
1488 calstream = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
1489 if (calstream == NULL) {
1493 cal = icalcomponent_new_from_string(calstream);
1497 /* We got our data stream -- now do something with it. */
1499 /* Delete the existing messages in the room, because we are replacing
1500 * the entire calendar with an entire new (or updated) calendar.
1501 * (Careful: this opens an S_ROOMS critical section!)
1503 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
1505 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1506 * in. This will almost never happen.
1508 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
1509 ical_write_to_cal(NULL, cal);
1512 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1513 * and other components encapsulated inside, we have to extract them.
1516 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1518 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1519 ical_write_to_cal(NULL, c);
1523 icalcomponent_free(cal);
1528 * All Citadel calendar commands from the client come through here.
1530 void cmd_ical(char *argbuf)
1538 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1540 /* Allow "test" and "freebusy" subcommands without logging in. */
1542 if (!strcasecmp(subcmd, "test")) {
1543 cprintf("%d This server supports calendaring\n", CIT_OK);
1547 if (!strcasecmp(subcmd, "freebusy")) {
1548 extract_token(who, argbuf, 1, '|', sizeof who);
1553 if (!strcasecmp(subcmd, "sgi")) {
1554 CIT_ICAL->server_generated_invitations =
1555 (extract_int(argbuf, 1) ? 1 : 0) ;
1557 CIT_OK, CIT_ICAL->server_generated_invitations);
1561 if (CtdlAccessCheck(ac_logged_in)) return;
1563 if (!strcasecmp(subcmd, "respond")) {
1564 msgnum = extract_long(argbuf, 1);
1565 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1566 extract_token(action, argbuf, 3, '|', sizeof action);
1567 ical_respond(msgnum, partnum, action);
1571 if (!strcasecmp(subcmd, "handle_rsvp")) {
1572 msgnum = extract_long(argbuf, 1);
1573 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1574 extract_token(action, argbuf, 3, '|', sizeof action);
1575 ical_handle_rsvp(msgnum, partnum, action);
1579 if (!strcasecmp(subcmd, "conflicts")) {
1580 msgnum = extract_long(argbuf, 1);
1581 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1582 ical_conflicts(msgnum, partnum);
1586 if (!strcasecmp(subcmd, "getics")) {
1591 if (!strcasecmp(subcmd, "putics")) {
1596 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1602 * We don't know if the calendar room exists so we just create it at login
1604 void ical_create_room(void)
1609 /* Create the calendar room if it doesn't already exist */
1610 create_room(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1612 /* Set expiration policy to manual; otherwise objects will be lost! */
1613 if (lgetroom(&qr, USERCALENDARROOM)) {
1614 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1617 qr.QRep.expire_mode = EXPIRE_MANUAL;
1618 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1621 /* Set the view to a calendar view */
1622 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1623 vbuf.v_view = VIEW_CALENDAR;
1624 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1626 /* Create the tasks list room if it doesn't already exist */
1627 create_room(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1629 /* Set expiration policy to manual; otherwise objects will be lost! */
1630 if (lgetroom(&qr, USERTASKSROOM)) {
1631 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1634 qr.QRep.expire_mode = EXPIRE_MANUAL;
1635 qr.QRdefaultview = VIEW_TASKS;
1638 /* Set the view to a task list view */
1639 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1640 vbuf.v_view = VIEW_TASKS;
1641 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1643 /* Create the notes room if it doesn't already exist */
1644 create_room(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1646 /* Set expiration policy to manual; otherwise objects will be lost! */
1647 if (lgetroom(&qr, USERNOTESROOM)) {
1648 lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1651 qr.QRep.expire_mode = EXPIRE_MANUAL;
1652 qr.QRdefaultview = VIEW_NOTES;
1655 /* Set the view to a notes view */
1656 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1657 vbuf.v_view = VIEW_NOTES;
1658 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1665 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1668 void ical_send_out_invitations(icalcomponent *cal) {
1669 icalcomponent *the_request = NULL;
1670 char *serialized_request = NULL;
1671 icalcomponent *encaps = NULL;
1672 char *request_message_text = NULL;
1673 struct CtdlMessage *msg = NULL;
1674 struct recptypes *valid = NULL;
1675 char attendees_string[SIZ];
1676 int num_attendees = 0;
1677 char this_attendee[256];
1678 icalproperty *attendee = NULL;
1679 char summary_string[SIZ];
1680 icalproperty *summary = NULL;
1683 lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
1688 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1689 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1690 ical_send_out_invitations(
1691 icalcomponent_get_first_component(
1692 cal, ICAL_VEVENT_COMPONENT
1698 /* Clone the event */
1699 the_request = icalcomponent_new_clone(cal);
1700 if (the_request == NULL) {
1701 lprintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
1705 /* Extract the summary string -- we'll use it as the
1706 * message subject for the request
1708 strcpy(summary_string, "Meeting request");
1709 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1710 if (summary != NULL) {
1711 if (icalproperty_get_summary(summary)) {
1712 strcpy(summary_string,
1713 icalproperty_get_summary(summary) );
1717 /* Determine who the recipients of this message are (the attendees) */
1718 strcpy(attendees_string, "");
1719 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1720 if (icalproperty_get_attendee(attendee)) {
1721 safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
1722 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1723 strcpy(this_attendee, &this_attendee[7]);
1725 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
1726 snprintf(&attendees_string[strlen(attendees_string)],
1727 sizeof(attendees_string) - strlen(attendees_string),
1737 lprintf(CTDL_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1739 /* If there are no attendees, there are no invitations to send, so...
1740 * don't bother putting one together! Punch out, Maverick!
1742 if (num_attendees == 0) {
1743 icalcomponent_free(the_request);
1747 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1748 encaps = icalcomponent_new_vcalendar();
1749 if (encaps == NULL) {
1750 lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1751 __FILE__, __LINE__);
1752 icalcomponent_free(the_request);
1756 /* Set the Product ID */
1757 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1759 /* Set the Version Number */
1760 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1762 /* Set the method to REQUEST */
1763 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1765 /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1766 ical_dezonify(the_request);
1768 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1769 * are responsible for "the_request"'s memory -- it will be freed
1770 * when we free "encaps".
1772 icalcomponent_add_component(encaps, the_request);
1775 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1776 icalcomponent_free(encaps); /* Don't need this anymore. */
1777 if (serialized_request == NULL) return;
1779 request_message_text = malloc(strlen(serialized_request) + SIZ);
1780 if (request_message_text != NULL) {
1781 sprintf(request_message_text,
1782 "Content-type: text/calendar\r\n\r\n%s\r\n",
1786 msg = CtdlMakeMessage(&CC->user,
1787 "", /* No single recipient here */
1788 "", /* No single recipient here */
1789 CC->room.QRname, 0, FMT_RFC822,
1792 summary_string, /* Use summary for subject */
1794 request_message_text);
1797 valid = validate_recipients(attendees_string);
1798 CtdlSubmitMsg(msg, valid, "");
1799 CtdlFreeMessage(msg);
1800 free_recipients(valid);
1803 free(serialized_request);
1808 * When a calendar object is being saved, determine whether it's a VEVENT
1809 * and the user saving it is the organizer. If so, send out invitations
1810 * to any listed attendees.
1813 void ical_saving_vevent(icalcomponent *cal) {
1815 icalproperty *organizer = NULL;
1816 char organizer_string[SIZ];
1818 lprintf(CTDL_DEBUG, "ical_saving_vevent() has been called!\n");
1820 /* Don't send out invitations unless the client wants us to. */
1821 if (CIT_ICAL->server_generated_invitations == 0) {
1825 /* Don't send out invitations if we've been asked not to. */
1826 if (CIT_ICAL->avoid_sending_invitations > 0) {
1830 strcpy(organizer_string, "");
1832 * The VEVENT subcomponent is the one we're interested in.
1833 * Send out invitations if, and only if, this user is the Organizer.
1835 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1836 organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
1837 if (organizer != NULL) {
1838 if (icalproperty_get_organizer(organizer)) {
1839 strcpy(organizer_string,
1840 icalproperty_get_organizer(organizer));
1843 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1844 strcpy(organizer_string, &organizer_string[7]);
1845 striplt(organizer_string);
1847 * If the user saving the event is listed as the
1848 * organizer, then send out invitations.
1850 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
1851 ical_send_out_invitations(cal);
1856 /* If the component has subcomponents, recurse through them. */
1857 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1859 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1860 /* Recursively process subcomponent */
1861 ical_saving_vevent(c);
1869 * Back end for ical_obj_beforesave()
1870 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1871 * the summary of the event (becomes message subject),
1872 * and the start time (becomes message date/time).
1874 void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
1875 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1876 char *encoding, void *cbuserdata)
1878 icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
1880 struct icalmessagemod *imm;
1883 imm = (struct icalmessagemod *)cbuserdata;
1885 /* We're only interested in calendar data. */
1886 if (strcasecmp(cbtype, "text/calendar")) {
1890 /* Hunt for the UID and drop it in
1891 * the "user data" pointer for the MIME parser. When
1892 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1895 whole_cal = icalcomponent_new_from_string(content);
1898 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1899 nested_event = icalcomponent_get_first_component(
1900 cal, ICAL_VEVENT_COMPONENT);
1901 if (nested_event != NULL) {
1905 nested_todo = icalcomponent_get_first_component(
1906 cal, ICAL_VTODO_COMPONENT);
1907 if (nested_todo != NULL) {
1914 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1916 /* If there's no uid we must generate one */
1917 generate_uuid(new_uid);
1918 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
1919 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1922 strcpy(imm->uid, icalproperty_get_comment(p));
1924 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
1926 strcpy(imm->subject, icalproperty_get_comment(p));
1928 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
1930 imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
1933 icalcomponent_free(cal);
1934 if (whole_cal != cal) {
1935 icalcomponent_free(whole_cal);
1944 * See if we need to prevent the object from being saved (we don't allow
1945 * MIME types other than text/calendar in "calendar" or "tasks" rooms). Also,
1946 * when saving an event to the calendar, set the message's Citadel exclusive
1947 * message ID to the UID of the object. This causes our replication checker to
1948 * automatically delete any existing instances of the same object. (Isn't
1951 * We also set the message's Subject to the event summary, and the Date/time to
1952 * the event start time.
1954 int ical_obj_beforesave(struct CtdlMessage *msg)
1956 struct icalmessagemod imm;
1958 /* First determine if this is a calendar or tasks room */
1959 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1960 && (CC->room.QRdefaultview != VIEW_TASKS)
1962 return(0); /* Not a vCalendar-centric room */
1965 /* It must be an RFC822 message! */
1966 if (msg->cm_format_type != 4) {
1967 lprintf(CTDL_DEBUG, "Rejecting non-RFC822 message\n");
1968 return(1); /* You tried to save a non-RFC822 message! */
1971 if (msg->cm_fields['M'] == NULL) {
1972 return(1); /* You tried to save a null message! */
1975 memset(&imm, 0, sizeof(struct icalmessagemod));
1977 /* Do all of our lovely back-end parsing */
1978 mime_parser(msg->cm_fields['M'],
1980 *ical_ctdl_set_exclusive_msgid,
1986 if (!IsEmptyStr(imm.uid)) {
1987 if (msg->cm_fields['E'] != NULL) {
1988 free(msg->cm_fields['E']);
1990 msg->cm_fields['E'] = strdup(imm.uid);
1991 lprintf(CTDL_DEBUG, "Saving calendar UID <%s>\n", msg->cm_fields['E']);
1993 if (!IsEmptyStr(imm.subject)) {
1994 if (msg->cm_fields['U'] != NULL) {
1995 free(msg->cm_fields['U']);
1997 msg->cm_fields['U'] = strdup(imm.subject);
1999 if (imm.dtstart > 0) {
2000 if (msg->cm_fields['T'] != NULL) {
2001 free(msg->cm_fields['T']);
2003 msg->cm_fields['T'] = strdup("000000000000000000");
2004 sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
2012 * Things we need to do after saving a calendar event.
2014 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
2015 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2016 char *encoding, void *cbuserdata)
2020 /* We're only interested in calendar items here. */
2021 if (strcasecmp(cbtype, "text/calendar")) {
2025 /* Hunt for the UID and drop it in
2026 * the "user data" pointer for the MIME parser. When
2027 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2030 if (!strcasecmp(cbtype, "text/calendar")) {
2031 cal = icalcomponent_new_from_string(content);
2033 ical_saving_vevent(cal);
2034 icalcomponent_free(cal);
2041 * Things we need to do after saving a calendar event.
2042 * (This will start back end tasks such as automatic generation of invitations,
2043 * if such actions are appropriate.)
2045 int ical_obj_aftersave(struct CtdlMessage *msg)
2047 char roomname[ROOMNAMELEN];
2050 * If this isn't the Calendar> room, no further action is necessary.
2053 /* First determine if this is our room */
2054 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
2055 if (strcasecmp(roomname, CC->room.QRname)) {
2056 return(0); /* Not the Calendar room -- don't do anything. */
2059 /* It must be an RFC822 message! */
2060 if (msg->cm_format_type != 4) return(1);
2062 /* Reject null messages */
2063 if (msg->cm_fields['M'] == NULL) return(1);
2065 /* Now recurse through it looking for our icalendar data */
2066 mime_parser(msg->cm_fields['M'],
2068 *ical_obj_aftersave_backend,
2078 void ical_session_startup(void) {
2079 CIT_ICAL = malloc(sizeof(struct cit_ical));
2080 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
2083 void ical_session_shutdown(void) {
2089 * Back end for ical_fixed_output()
2091 void ical_fixed_output_backend(icalcomponent *cal,
2098 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
2100 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2103 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
2105 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2108 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
2110 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2113 /* If the component has attendees, iterate through them. */
2114 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
2115 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
2116 if (!strncasecmp(buf, "MAILTO:", 7)) {
2118 /* screen name or email address */
2119 strcpy(buf, &buf[7]);
2121 cprintf("%s ", buf);
2126 /* If the component has subcomponents, recurse through them. */
2127 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2129 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2130 /* Recursively process subcomponent */
2131 ical_fixed_output_backend(c, recursion_level+1);
2138 * Function to output vcalendar data as plain text. Nobody uses MSG0
2139 * anymore, so really this is just so we expose the vCard data to the full
2142 void ical_fixed_output(char *ptr, int len) {
2146 stringy_cal = malloc(len + 1);
2147 safestrncpy(stringy_cal, ptr, len + 1);
2148 cal = icalcomponent_new_from_string(stringy_cal);
2156 ical_fixed_output_backend(cal, 0);
2158 /* Free the memory we obtained from libical's constructor */
2159 icalcomponent_free(cal);
2163 #endif /* CITADEL_WITH_CALENDAR_SERVICE */
2166 * Register this module with the Citadel server.
2168 CTDL_MODULE_INIT(calendar)
2172 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2173 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2174 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2175 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
2176 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2177 CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2178 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2179 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2183 /* return our Subversion id for the Log */
2189 void serv_calendar_destroy(void)
2191 #ifdef CITADEL_WITH_CALENDAR_SERVICE
2192 icaltimezone_free_builtin_timezones();