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>
23 #include <libcitadel.h>
26 #include "citserver.h"
32 #include "internet_addressing.h"
33 #include "serv_calendar.h"
34 #include "euidindex.h"
35 #include "ctdl_module.h"
36 #include "ical_dezonify.h"
40 struct ical_respond_data {
41 char desired_partnum[SIZ];
47 * Utility function to create a new VCALENDAR component with some of the
48 * required fields already set the way we like them.
50 icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
51 icalcomponent *encaps;
53 encaps = icalcomponent_new_vcalendar();
55 CtdlLogPrintf(CTDL_CRIT, "Error at %s:%d - could not allocate component!\n",
60 /* Set the Product ID */
61 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
63 /* Set the Version Number */
64 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
71 * Utility function to encapsulate a subcomponent into a full VCALENDAR
73 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
74 icalcomponent *encaps;
76 /* If we're already looking at a full VCALENDAR component,
77 * don't bother ... just return itself.
79 if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
83 /* Encapsulate the VEVENT component into a complete VCALENDAR */
84 encaps = icalcomponent_new_citadel_vcalendar();
85 if (encaps == NULL) return NULL;
87 /* Encapsulate the subcomponent inside */
88 icalcomponent_add_component(encaps, subcomp);
90 /* Convert all timestamps to UTC so we don't have to deal with
91 * stupid VTIMEZONE crap.
93 ical_dezonify(encaps);
95 /* Return the object we just created. */
103 * Write a calendar object into the specified user's calendar room.
104 * If the supplied user is NULL, this function writes the calendar object
105 * to the currently selected room.
107 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
109 icalcomponent *encaps = NULL;
110 struct CtdlMessage *msg = NULL;
111 icalcomponent *tmp=NULL;
113 if (cal == NULL) return;
115 /* If the supplied object is a subcomponent, encapsulate it in
116 * a full VCALENDAR component, and save that instead.
118 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
119 tmp = icalcomponent_new_clone(cal);
120 encaps = ical_encapsulate_subcomponent(tmp);
121 ical_write_to_cal(u, encaps);
122 icalcomponent_free(tmp);
123 icalcomponent_free(encaps);
127 ser = icalcomponent_as_ical_string(cal);
128 if (ser == NULL) return;
130 /* If the caller supplied a user, write to that user's default calendar room */
132 /* This handy API function does all the work for us. */
133 CtdlWriteObject(USERCALENDARROOM, /* which room */
134 "text/calendar", /* MIME type */
136 strlen(ser)+1, /* length */
139 0, /* don't delete others of this type */
144 /* If the caller did not supply a user, write to the currently selected room */
146 msg = malloc(sizeof(struct CtdlMessage));
147 memset(msg, 0, sizeof(struct CtdlMessage));
148 msg->cm_magic = CTDLMESSAGE_MAGIC;
149 msg->cm_anon_type = MES_NORMAL;
150 msg->cm_format_type = 4;
151 msg->cm_fields['A'] = strdup(CC->user.fullname);
152 msg->cm_fields['O'] = strdup(CC->room.QRname);
153 msg->cm_fields['N'] = strdup(config.c_nodename);
154 msg->cm_fields['H'] = strdup(config.c_humannode);
155 msg->cm_fields['M'] = malloc(strlen(ser) + 40);
156 strcpy(msg->cm_fields['M'], "Content-type: text/calendar\r\n\r\n");
157 strcat(msg->cm_fields['M'], ser);
159 /* Now write the data */
160 CtdlSubmitMsg(msg, NULL, "", QP_EADDR);
161 CtdlFreeMessage(msg);
164 /* In either case, now we can free the serialized calendar object */
170 * Add a calendar object to the user's calendar
172 * ok because it uses ical_write_to_cal()
174 void ical_add(icalcomponent *cal, int recursion_level) {
178 * The VEVENT subcomponent is the one we're interested in saving.
180 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
182 ical_write_to_cal(&CC->user, cal);
186 /* If the component has subcomponents, recurse through them. */
187 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
189 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
190 /* Recursively process subcomponent */
191 ical_add(c, recursion_level+1);
199 * Send a reply to a meeting invitation.
201 * 'request' is the invitation to reply to.
202 * 'action' is the string "accept" or "decline" or "tentative".
205 void ical_send_a_reply(icalcomponent *request, char *action) {
206 icalcomponent *the_reply = NULL;
207 icalcomponent *vevent = NULL;
208 icalproperty *attendee = NULL;
209 char attendee_string[SIZ];
210 icalproperty *organizer = NULL;
211 char organizer_string[SIZ];
212 icalproperty *summary = NULL;
213 char summary_string[SIZ];
214 icalproperty *me_attend = NULL;
215 struct recptypes *recp = NULL;
216 icalparameter *partstat = NULL;
217 char *serialized_reply = NULL;
218 char *reply_message_text = NULL;
219 struct CtdlMessage *msg = NULL;
220 struct recptypes *valid = NULL;
222 strcpy(organizer_string, "");
223 strcpy(summary_string, "Calendar item");
225 if (request == NULL) {
226 CtdlLogPrintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
230 the_reply = icalcomponent_new_clone(request);
231 if (the_reply == NULL) {
232 CtdlLogPrintf(CTDL_ERR, "ERROR: cannot clone request\n");
236 /* Change the method from REQUEST to REPLY */
237 icalcomponent_set_method(the_reply, ICAL_METHOD_REPLY);
239 vevent = icalcomponent_get_first_component(the_reply, ICAL_VEVENT_COMPONENT);
240 if (vevent != NULL) {
241 /* Hunt for attendees, removing ones that aren't us.
242 * (Actually, remove them all, cloning our own one so we can
243 * re-insert it later)
245 while (attendee = icalcomponent_get_first_property(vevent,
246 ICAL_ATTENDEE_PROPERTY), (attendee != NULL)
248 if (icalproperty_get_attendee(attendee)) {
249 strcpy(attendee_string,
250 icalproperty_get_attendee(attendee) );
251 if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
252 strcpy(attendee_string, &attendee_string[7]);
253 striplt(attendee_string);
254 recp = validate_recipients(attendee_string, NULL, 0);
256 if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
257 if (me_attend) icalproperty_free(me_attend);
258 me_attend = icalproperty_new_clone(attendee);
260 free_recipients(recp);
265 icalcomponent_remove_property(vevent, attendee);
266 icalproperty_free(attendee);
269 /* We found our own address in the attendee list. */
271 /* Change the partstat from NEEDS-ACTION to ACCEPT or DECLINE */
272 icalproperty_remove_parameter(me_attend, ICAL_PARTSTAT_PARAMETER);
274 if (!strcasecmp(action, "accept")) {
275 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_ACCEPTED);
277 else if (!strcasecmp(action, "decline")) {
278 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_DECLINED);
280 else if (!strcasecmp(action, "tentative")) {
281 partstat = icalparameter_new_partstat(ICAL_PARTSTAT_TENTATIVE);
284 if (partstat) icalproperty_add_parameter(me_attend, partstat);
286 /* Now insert it back into the vevent. */
287 icalcomponent_add_property(vevent, me_attend);
290 /* Figure out who to send this thing to */
291 organizer = icalcomponent_get_first_property(vevent, ICAL_ORGANIZER_PROPERTY);
292 if (organizer != NULL) {
293 if (icalproperty_get_organizer(organizer)) {
294 strcpy(organizer_string,
295 icalproperty_get_organizer(organizer) );
298 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
299 strcpy(organizer_string, &organizer_string[7]);
300 striplt(organizer_string);
302 strcpy(organizer_string, "");
305 /* Extract the summary string -- we'll use it as the
306 * message subject for the reply
308 summary = icalcomponent_get_first_property(vevent, ICAL_SUMMARY_PROPERTY);
309 if (summary != NULL) {
310 if (icalproperty_get_summary(summary)) {
311 strcpy(summary_string,
312 icalproperty_get_summary(summary) );
317 /* Now generate the reply message and send it out. */
318 serialized_reply = strdup(icalcomponent_as_ical_string(the_reply));
319 icalcomponent_free(the_reply); /* don't need this anymore */
320 if (serialized_reply == NULL) return;
322 reply_message_text = malloc(strlen(serialized_reply) + SIZ);
323 if (reply_message_text != NULL) {
324 sprintf(reply_message_text,
325 "Content-type: text/calendar; charset=\"utf-8\"\r\n\r\n%s\r\n",
329 msg = CtdlMakeMessage(&CC->user,
330 organizer_string, /* to */
332 CC->room.QRname, 0, FMT_RFC822,
335 summary_string, /* Use summary for subject */
341 valid = validate_recipients(organizer_string, NULL, 0);
342 CtdlSubmitMsg(msg, valid, "", QP_EADDR);
343 CtdlFreeMessage(msg);
344 free_recipients(valid);
347 free(serialized_reply);
353 * Callback function for mime parser that hunts for calendar content types
354 * and turns them into calendar objects
356 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
357 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
360 struct ical_respond_data *ird = NULL;
362 ird = (struct ical_respond_data *) cbuserdata;
364 /* desired_partnum can be set to "_HUNT_" to have it just look for
365 * the first part with a content type of text/calendar. Otherwise
366 * we have to only process the right one.
368 if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
369 if (strcasecmp(partnum, ird->desired_partnum)) {
374 if ( (strcasecmp(cbtype, "text/calendar"))
375 && (strcasecmp(cbtype, "application/ics")) ) {
379 if (ird->cal != NULL) {
380 icalcomponent_free(ird->cal);
384 ird->cal = icalcomponent_new_from_string(content);
385 if (ird->cal != NULL) {
386 ical_dezonify(ird->cal);
392 * Respond to a meeting request.
394 void ical_respond(long msgnum, char *partnum, char *action) {
395 struct CtdlMessage *msg = NULL;
396 struct ical_respond_data ird;
399 (strcasecmp(action, "accept"))
400 && (strcasecmp(action, "decline"))
402 cprintf("%d Action must be 'accept' or 'decline'\n",
403 ERROR + ILLEGAL_VALUE
408 msg = CtdlFetchMessage(msgnum, 1);
410 cprintf("%d Message %ld not found.\n",
411 ERROR + ILLEGAL_VALUE,
417 memset(&ird, 0, sizeof ird);
418 strcpy(ird.desired_partnum, partnum);
419 mime_parser(msg->cm_fields['M'],
421 *ical_locate_part, /* callback function */
423 (void *) &ird, /* user data */
427 /* We're done with the incoming message, because we now have a
428 * calendar object in memory.
430 CtdlFreeMessage(msg);
433 * Here is the real meat of this function. Handle the event.
435 if (ird.cal != NULL) {
436 /* Save this in the user's calendar if necessary */
437 if (!strcasecmp(action, "accept")) {
438 ical_add(ird.cal, 0);
441 /* Send a reply if necessary */
442 if (icalcomponent_get_method(ird.cal) == ICAL_METHOD_REQUEST) {
443 ical_send_a_reply(ird.cal, action);
446 /* Now that we've processed this message, we don't need it
447 * anymore. So delete it. (NOTE we don't do this anymore.)
448 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
451 /* Free the memory we allocated and return a response. */
452 icalcomponent_free(ird.cal);
454 cprintf("%d ok\n", CIT_OK);
458 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
462 /* should never get here */
467 * Figure out the UID of the calendar event being referred to in a
468 * REPLY object. This function is recursive.
470 void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
471 icalcomponent *subcomponent;
474 /* If this object is a REPLY, then extract the UID. */
475 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
476 p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
478 strcpy(uidbuf, icalproperty_get_comment(p));
482 /* Otherwise, recurse through any VEVENT subcomponents. We do NOT want the
483 * UID of the reply; we want the UID of the invitation being replied to.
485 for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
486 subcomponent != NULL;
487 subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
488 ical_learn_uid_of_reply(uidbuf, subcomponent);
494 * ical_update_my_calendar_with_reply() refers to this callback function; when we
495 * locate the message containing the calendar event we're replying to, this function
496 * gets called. It basically just sticks the message number in a supplied buffer.
498 void ical_hunt_for_event_to_update(long msgnum, void *data) {
501 msgnumptr = (long *) data;
506 struct original_event_container {
511 * Callback function for mime parser that hunts for calendar content types
512 * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
513 * to fetch the object being updated)
515 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
516 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
519 struct original_event_container *oec = NULL;
521 if ( (strcasecmp(cbtype, "text/calendar"))
522 && (strcasecmp(cbtype, "application/ics")) ) {
525 oec = (struct original_event_container *) cbuserdata;
526 if (oec->c != NULL) {
527 icalcomponent_free(oec->c);
529 oec->c = icalcomponent_new_from_string(content);
534 * Merge updated attendee information from a REPLY into an existing event.
536 void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
538 icalproperty *e_attendee, *r_attendee;
540 /* First things first. If we're not looking at a VEVENT component,
541 * recurse through subcomponents until we find one.
543 if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
544 for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
546 c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
547 ical_merge_attendee_reply(c, reply);
552 /* Now do the same thing with the reply.
554 if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
555 for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
557 c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
558 ical_merge_attendee_reply(event, c);
563 /* Clone the reply, because we're going to rip its guts out. */
564 reply = icalcomponent_new_clone(reply);
566 /* At this point we're looking at the correct subcomponents.
567 * Iterate through the attendees looking for a match.
570 for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
572 e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
574 for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
576 r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
578 /* Check to see if these two attendees match...
581 icalproperty_get_attendee(e_attendee),
582 icalproperty_get_attendee(r_attendee)
584 /* ...and if they do, remove the attendee from the event
585 * and replace it with the attendee from the reply. (The
586 * reply's copy will have the same address, but an updated
589 icalcomponent_remove_property(event, e_attendee);
590 icalproperty_free(e_attendee);
591 icalcomponent_remove_property(reply, r_attendee);
592 icalcomponent_add_property(event, r_attendee);
594 /* Since we diddled both sets of attendees, we have to start
595 * the iteration over again. This will not create an infinite
596 * loop because we removed the attendee from the reply. (That's
597 * why we cloned the reply, and that's what we mean by "ripping
606 /* Free the *clone* of the reply. */
607 icalcomponent_free(reply);
614 * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
615 * calendar event. The object has already been deserialized for us; all
616 * we have to do here is hunt for the event in our calendar, merge in the
617 * updated attendee status, and save it again.
619 * This function returns 0 on success, 1 if the event was not found in the
620 * user's calendar, or 2 if an internal error occurred.
622 int ical_update_my_calendar_with_reply(icalcomponent *cal) {
624 char hold_rm[ROOMNAMELEN];
625 long msgnum_being_replaced = 0;
626 struct CtdlMessage *msg = NULL;
627 struct original_event_container oec;
628 icalcomponent *original_event;
629 char *serialized_event = NULL;
630 char roomname[ROOMNAMELEN];
631 char *message_text = NULL;
633 /* Figure out just what event it is we're dealing with */
634 strcpy(uid, "--==<< InVaLiD uId >>==--");
635 ical_learn_uid_of_reply(uid, cal);
636 CtdlLogPrintf(CTDL_DEBUG, "UID of event being replied to is <%s>\n", uid);
638 strcpy(hold_rm, CC->room.QRname); /* save current room */
640 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
641 getroom(&CC->room, hold_rm);
642 CtdlLogPrintf(CTDL_CRIT, "cannot get user calendar room\n");
647 * Look in the EUID index for a message with
648 * the Citadel EUID set to the value we're looking for. Since
649 * Citadel always sets the message EUID to the iCalendar UID of
650 * the event, this will work.
652 msgnum_being_replaced = locate_message_by_euid(uid, &CC->room);
654 getroom(&CC->room, hold_rm); /* return to saved room */
656 CtdlLogPrintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
657 if (msgnum_being_replaced == 0) {
658 return(1); /* no calendar event found */
661 /* Now we know the ID of the message containing the event being updated.
662 * We don't actually have to delete it; that'll get taken care of by the
663 * server when we save another event with the same UID. This just gives
664 * us the ability to load the event into memory so we can diddle the
667 msg = CtdlFetchMessage(msgnum_being_replaced, 1);
669 return(2); /* internal error */
672 mime_parser(msg->cm_fields['M'],
674 *ical_locate_original_event, /* callback function */
676 &oec, /* user data */
679 CtdlFreeMessage(msg);
681 original_event = oec.c;
682 if (original_event == NULL) {
683 CtdlLogPrintf(CTDL_ERR, "ERROR: Original_component is NULL.\n");
687 /* Merge the attendee's updated status into the event */
688 ical_merge_attendee_reply(original_event, cal);
691 serialized_event = strdup(icalcomponent_as_ical_string(original_event));
692 icalcomponent_free(original_event); /* Don't need this anymore. */
693 if (serialized_event == NULL) return(2);
695 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
697 message_text = malloc(strlen(serialized_event) + SIZ);
698 if (message_text != NULL) {
699 sprintf(message_text,
700 "Content-type: text/calendar; charset=\"utf-8\"\r\n\r\n%s\r\n",
704 msg = CtdlMakeMessage(&CC->user,
705 "", /* No recipient */
706 "", /* No recipient */
717 CIT_ICAL->avoid_sending_invitations = 1;
718 CtdlSubmitMsg(msg, NULL, roomname, QP_EADDR);
719 CtdlFreeMessage(msg);
720 CIT_ICAL->avoid_sending_invitations = 0;
723 free(serialized_event);
729 * Handle an incoming RSVP for an event. (This is the server subcommand part; it
730 * simply extracts the calendar object from the message, deserializes it, and
731 * passes it up to ical_update_my_calendar_with_reply() for processing.
733 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
734 struct CtdlMessage *msg = NULL;
735 struct ical_respond_data ird;
739 (strcasecmp(action, "update"))
740 && (strcasecmp(action, "ignore"))
742 cprintf("%d Action must be 'update' or 'ignore'\n",
743 ERROR + ILLEGAL_VALUE
748 msg = CtdlFetchMessage(msgnum, 1);
750 cprintf("%d Message %ld not found.\n",
751 ERROR + ILLEGAL_VALUE,
757 memset(&ird, 0, sizeof ird);
758 strcpy(ird.desired_partnum, partnum);
759 mime_parser(msg->cm_fields['M'],
761 *ical_locate_part, /* callback function */
763 (void *) &ird, /* user data */
767 /* We're done with the incoming message, because we now have a
768 * calendar object in memory.
770 CtdlFreeMessage(msg);
773 * Here is the real meat of this function. Handle the event.
775 if (ird.cal != NULL) {
776 /* Update the user's calendar if necessary */
777 if (!strcasecmp(action, "update")) {
778 ret = ical_update_my_calendar_with_reply(ird.cal);
780 cprintf("%d Your calendar has been updated with this reply.\n",
784 cprintf("%d This event does not exist in your calendar.\n",
785 ERROR + FILE_NOT_FOUND);
788 cprintf("%d An internal error occurred.\n",
789 ERROR + INTERNAL_ERROR);
793 cprintf("%d This reply has been ignored.\n", CIT_OK);
796 /* Now that we've processed this message, we don't need it
797 * anymore. So delete it. (Don't do this anymore.)
798 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
801 /* Free the memory we allocated and return a response. */
802 icalcomponent_free(ird.cal);
807 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
811 /* should never get here */
816 * Search for a property in both the top level and in a VEVENT subcomponent
818 icalproperty *ical_ctdl_get_subprop(
820 icalproperty_kind which_prop
825 p = icalcomponent_get_first_property(cal, which_prop);
827 c = icalcomponent_get_first_component(cal,
828 ICAL_VEVENT_COMPONENT);
830 p = icalcomponent_get_first_property(c, which_prop);
838 * Check to see if two events overlap. Returns nonzero if they do.
839 * (This function is used in both Citadel and WebCit. If you change it in
840 * one place, change it in the other. Better yet, put it in a library.)
842 int ical_ctdl_is_overlap(
843 struct icaltimetype t1start,
844 struct icaltimetype t1end,
845 struct icaltimetype t2start,
846 struct icaltimetype t2end
849 if (icaltime_is_null_time(t1start)) return(0);
850 if (icaltime_is_null_time(t2start)) return(0);
852 /* First, check for all-day events */
853 if (t1start.is_date) {
854 if (!icaltime_compare_date_only(t1start, t2start)) {
857 if (!icaltime_is_null_time(t2end)) {
858 if (!icaltime_compare_date_only(t1start, t2end)) {
864 if (t2start.is_date) {
865 if (!icaltime_compare_date_only(t2start, t1start)) {
868 if (!icaltime_is_null_time(t1end)) {
869 if (!icaltime_compare_date_only(t2start, t1end)) {
875 /* Now check for overlaps using date *and* time. */
877 /* First, bail out if either event 1 or event 2 is missing end time. */
878 if (icaltime_is_null_time(t1end)) return(0);
879 if (icaltime_is_null_time(t2end)) return(0);
881 /* If event 1 ends before event 2 starts, we're in the clear. */
882 if (icaltime_compare(t1end, t2start) <= 0) return(0);
884 /* If event 2 ends before event 1 starts, we're also ok. */
885 if (icaltime_compare(t2end, t1start) <= 0) return(0);
887 /* Otherwise, they overlap. */
894 * Phase 6 of "hunt for conflicts"
895 * called by ical_conflicts_phase5()
897 * Now both the proposed and existing events have been boiled down to start and end times.
898 * Check for overlap and output any conflicts.
900 void ical_conflicts_phase6(struct icaltimetype t1start,
901 struct icaltimetype t1end,
902 struct icaltimetype t2start,
903 struct icaltimetype t2end,
904 long existing_msgnum,
905 char *conflict_event_uid,
906 char *conflict_event_summary,
910 /* debugging cruft */
912 tt = icaltime_as_timet(t1start);
913 CtdlLogPrintf(CTDL_DEBUG, "PROPOSED START: %s", ctime(&tt));
914 tt = icaltime_as_timet(t1end);
915 CtdlLogPrintf(CTDL_DEBUG, " PROPOSED END: %s", ctime(&tt));
916 tt = icaltime_as_timet(t2start);
917 CtdlLogPrintf(CTDL_DEBUG, "EXISTING START: %s", ctime(&tt));
918 tt = icaltime_as_timet(t2end);
919 CtdlLogPrintf(CTDL_DEBUG, " EXISTING END: %s", ctime(&tt));
921 /* compare and output */
923 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
924 cprintf("%ld||%s|%s|%d|\n",
927 conflict_event_summary,
928 ( ((strlen(compare_uid)>0)
929 &&(!strcasecmp(compare_uid,
930 conflict_event_uid))) ? 1 : 0
933 CtdlLogPrintf(CTDL_DEBUG, " --- CONFLICT --- \n");
936 CtdlLogPrintf(CTDL_DEBUG, " --- no conflict --- \n");
944 * Phase 5 of "hunt for conflicts"
945 * Called by ical_conflicts_phase4()
947 * We have the proposed event boiled down to start and end times.
948 * Now check it against an existing event.
950 void ical_conflicts_phase5(struct icaltimetype t1start,
951 struct icaltimetype t1end,
952 icalcomponent *existing_event,
953 long existing_msgnum,
956 char conflict_event_uid[SIZ];
957 char conflict_event_summary[SIZ];
958 struct icaltimetype t2start, t2end;
962 icalproperty *rrule = NULL;
963 struct icalrecurrencetype recur;
964 icalrecur_iterator *ritr = NULL; */
967 strcpy(conflict_event_uid, "");
968 strcpy(conflict_event_summary, "");
969 t2start = icaltime_null_time();
970 t2end = icaltime_null_time();
973 /* existing event stuff */
975 p = ical_ctdl_get_subprop(existing_event, ICAL_DTSTART_PROPERTY);
976 if (p == NULL) return;
977 if (p != NULL) t2start = icalproperty_get_dtstart(p);
979 p = ical_ctdl_get_subprop(existing_event, ICAL_DTEND_PROPERTY);
980 if (p != NULL) t2end = icalproperty_get_dtend(p);
982 /*rrule = ical_ctdl_get_subprop(existing_event, ICAL_RRULE_PROPERTY);
984 recur = icalproperty_get_rrule(rrule);
985 ritr = icalrecur_iterator_new(recur, t1start);
986 CtdlLogPrintf(CTDL_DEBUG, "Recurrence found: %s\n", icalrecurrencetype_as_string(&recur));
990 p = ical_ctdl_get_subprop(existing_event, ICAL_UID_PROPERTY);
992 strcpy(conflict_event_uid, icalproperty_get_comment(p));
995 p = ical_ctdl_get_subprop(existing_event, ICAL_SUMMARY_PROPERTY);
997 strcpy(conflict_event_summary, icalproperty_get_comment(p));
1000 ical_conflicts_phase6(t1start, t1end, t2start, t2end,
1001 existing_msgnum, conflict_event_uid, conflict_event_summary, compare_uid
1005 t1start = icalrecur_iterator_next(ritr);
1008 } while ( (rrule) && (!icaltime_is_null_time(t1start)) );*/
1016 * Phase 4 of "hunt for conflicts"
1017 * Called by ical_hunt_for_conflicts_backend()
1019 * At this point we've got it boiled down to two icalcomponent events in memory.
1020 * If they conflict, output something to the client.
1022 void ical_conflicts_phase4(icalcomponent *proposed_event,
1023 icalcomponent *existing_event,
1024 long existing_msgnum)
1026 struct icaltimetype t1start, t1end;
1027 t1start = icaltime_null_time();
1028 t1end = icaltime_null_time();
1030 char compare_uid[SIZ];
1032 /* initialization */
1033 strcpy(compare_uid, "");
1035 /* proposed event stuff */
1037 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTSTART_PROPERTY);
1038 if (p == NULL) return;
1039 if (p != NULL) t1start = icalproperty_get_dtstart(p);
1041 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTEND_PROPERTY);
1042 if (p != NULL) t1end = icalproperty_get_dtend(p);
1044 p = ical_ctdl_get_subprop(proposed_event, ICAL_UID_PROPERTY);
1046 strcpy(compare_uid, icalproperty_get_comment(p));
1049 ical_conflicts_phase5(t1start, t1end, existing_event, existing_msgnum, compare_uid);
1055 * Phase 3 of "hunt for conflicts"
1056 * Called by ical_hunt_for_conflicts()
1058 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
1059 icalcomponent *proposed_event;
1060 struct CtdlMessage *msg = NULL;
1061 struct ical_respond_data ird;
1063 proposed_event = (icalcomponent *)data;
1065 msg = CtdlFetchMessage(msgnum, 1);
1066 if (msg == NULL) return;
1067 memset(&ird, 0, sizeof ird);
1068 strcpy(ird.desired_partnum, "_HUNT_");
1069 mime_parser(msg->cm_fields['M'],
1071 *ical_locate_part, /* callback function */
1073 (void *) &ird, /* user data */
1076 CtdlFreeMessage(msg);
1078 if (ird.cal == NULL) return;
1080 ical_conflicts_phase4(proposed_event, ird.cal, msgnum);
1081 icalcomponent_free(ird.cal);
1087 * Phase 2 of "hunt for conflicts" operation.
1088 * At this point we have a calendar object which represents the VEVENT that
1089 * is proposed for addition to the calendar. Now hunt through the user's
1090 * calendar room, and output zero or more existing VEVENTs which conflict
1093 void ical_hunt_for_conflicts(icalcomponent *cal) {
1094 char hold_rm[ROOMNAMELEN];
1096 strcpy(hold_rm, CC->room.QRname); /* save current room */
1098 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
1099 getroom(&CC->room, hold_rm);
1100 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
1104 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
1106 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1109 ical_hunt_for_conflicts_backend,
1114 getroom(&CC->room, hold_rm); /* return to saved room */
1121 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
1123 void ical_conflicts(long msgnum, char *partnum) {
1124 struct CtdlMessage *msg = NULL;
1125 struct ical_respond_data ird;
1127 msg = CtdlFetchMessage(msgnum, 1);
1129 cprintf("%d Message %ld not found.\n",
1130 ERROR + ILLEGAL_VALUE,
1136 memset(&ird, 0, sizeof ird);
1137 strcpy(ird.desired_partnum, partnum);
1138 mime_parser(msg->cm_fields['M'],
1140 *ical_locate_part, /* callback function */
1142 (void *) &ird, /* user data */
1146 CtdlFreeMessage(msg);
1148 if (ird.cal != NULL) {
1149 ical_hunt_for_conflicts(ird.cal);
1150 icalcomponent_free(ird.cal);
1154 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1158 /* should never get here */
1164 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1166 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1169 struct icalperiodtype my_period;
1171 if (cal == NULL) return;
1172 my_period = icalperiodtype_null_period();
1174 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1175 ical_add_to_freebusy(fb,
1176 icalcomponent_get_first_component(
1177 cal, ICAL_VEVENT_COMPONENT
1185 /* If this event is not opaque, the user isn't publishing it as
1186 * busy time, so don't bother doing anything else.
1188 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1190 v = icalproperty_get_value(p);
1192 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1198 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1199 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1201 my_period.start = icalproperty_get_dtstart(p);
1204 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1206 my_period.end = icalproperty_get_dtstart(p);
1210 icalcomponent_add_property(fb,
1211 icalproperty_new_freebusy(my_period)
1214 /* Make sure the DTSTART property of the freebusy *list* is set to
1215 * the DTSTART property of the *earliest event*.
1217 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1219 icalcomponent_set_dtstart(fb,
1220 icalcomponent_get_dtstart(cal) );
1223 if (icaltime_compare(
1224 icalcomponent_get_dtstart(cal),
1225 icalcomponent_get_dtstart(fb)
1227 icalcomponent_set_dtstart(fb,
1228 icalcomponent_get_dtstart(cal) );
1232 /* Make sure the DTEND property of the freebusy *list* is set to
1233 * the DTEND property of the *latest event*.
1235 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1237 icalcomponent_set_dtend(fb,
1238 icalcomponent_get_dtend(cal) );
1241 if (icaltime_compare(
1242 icalcomponent_get_dtend(cal),
1243 icalcomponent_get_dtend(fb)
1245 icalcomponent_set_dtend(fb,
1246 icalcomponent_get_dtend(cal) );
1255 * Backend for ical_freebusy()
1257 * This function simply loads the messages in the user's calendar room,
1258 * which contain VEVENTs, then strips them of all non-freebusy data, and
1259 * adds them to the supplied VCALENDAR.
1262 void ical_freebusy_backend(long msgnum, void *data) {
1264 struct CtdlMessage *msg = NULL;
1265 struct ical_respond_data ird;
1267 cal = (icalcomponent *)data;
1269 msg = CtdlFetchMessage(msgnum, 1);
1270 if (msg == NULL) return;
1271 memset(&ird, 0, sizeof ird);
1272 strcpy(ird.desired_partnum, "_HUNT_");
1273 mime_parser(msg->cm_fields['M'],
1275 *ical_locate_part, /* callback function */
1277 (void *) &ird, /* user data */
1280 CtdlFreeMessage(msg);
1282 if (ird.cal == NULL) return;
1284 ical_add_to_freebusy(cal, ird.cal);
1286 /* Now free the memory. */
1287 icalcomponent_free(ird.cal);
1293 * Grab another user's free/busy times
1295 void ical_freebusy(char *who) {
1296 struct ctdluser usbuf;
1297 char calendar_room_name[ROOMNAMELEN];
1298 char hold_rm[ROOMNAMELEN];
1299 char *serialized_request = NULL;
1300 icalcomponent *encaps = NULL;
1301 icalcomponent *fb = NULL;
1302 int found_user = (-1);
1303 struct recptypes *recp = NULL;
1308 int config_lines = 0;
1310 /* First try an exact match. */
1311 found_user = getuser(&usbuf, who);
1313 /* If not found, try it as an unqualified email address. */
1314 if (found_user != 0) {
1316 recp = validate_recipients(buf, NULL, 0);
1317 CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1319 if (recp->num_local == 1) {
1320 found_user = getuser(&usbuf, recp->recp_local);
1322 free_recipients(recp);
1326 /* If still not found, try it as an address qualified with the
1327 * primary FQDN of this Citadel node.
1329 if (found_user != 0) {
1330 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1331 CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1332 recp = validate_recipients(buf, NULL, 0);
1334 if (recp->num_local == 1) {
1335 found_user = getuser(&usbuf, recp->recp_local);
1337 free_recipients(recp);
1341 /* Still not found? Try qualifying it with every domain we
1342 * might have addresses in.
1344 if (found_user != 0) {
1345 config_lines = num_tokens(inetcfg, '\n');
1346 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1347 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1348 extract_token(host, buf, 0, '|', sizeof host);
1349 extract_token(type, buf, 1, '|', sizeof type);
1351 if ( (!strcasecmp(type, "localhost"))
1352 || (!strcasecmp(type, "directory")) ) {
1353 snprintf(buf, sizeof buf, "%s@%s", who, host);
1354 CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1355 recp = validate_recipients(buf, NULL, 0);
1357 if (recp->num_local == 1) {
1358 found_user = getuser(&usbuf, recp->recp_local);
1360 free_recipients(recp);
1366 if (found_user != 0) {
1367 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1371 MailboxName(calendar_room_name, sizeof calendar_room_name,
1372 &usbuf, USERCALENDARROOM);
1374 strcpy(hold_rm, CC->room.QRname); /* save current room */
1376 if (getroom(&CC->room, calendar_room_name) != 0) {
1377 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1378 getroom(&CC->room, hold_rm);
1382 /* Create a VFREEBUSY subcomponent */
1383 CtdlLogPrintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
1384 fb = icalcomponent_new_vfreebusy();
1386 cprintf("%d Internal error: cannot allocate memory.\n",
1387 ERROR + INTERNAL_ERROR);
1388 getroom(&CC->room, hold_rm);
1392 /* Set the method to PUBLISH */
1393 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1395 /* Set the DTSTAMP to right now. */
1396 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1398 /* Add the user's email address as ORGANIZER */
1399 sprintf(buf, "MAILTO:%s", who);
1400 if (strchr(buf, '@') == NULL) {
1402 strcat(buf, config.c_fqdn);
1404 for (i=0; buf[i]; ++i) {
1405 if (buf[i]==' ') buf[i] = '_';
1407 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1409 /* Add busy time from events */
1410 CtdlLogPrintf(CTDL_DEBUG, "Adding busy time from events\n");
1411 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
1413 /* If values for DTSTART and DTEND are still not present, set them
1414 * to yesterday and tomorrow as default values.
1416 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1417 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1419 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1420 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1423 /* Put the freebusy component into the calendar component */
1424 CtdlLogPrintf(CTDL_DEBUG, "Encapsulating\n");
1425 encaps = ical_encapsulate_subcomponent(fb);
1426 if (encaps == NULL) {
1427 icalcomponent_free(fb);
1428 cprintf("%d Internal error: cannot allocate memory.\n",
1429 ERROR + INTERNAL_ERROR);
1430 getroom(&CC->room, hold_rm);
1434 /* Set the method to PUBLISH */
1435 CtdlLogPrintf(CTDL_DEBUG, "Setting method\n");
1436 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1439 CtdlLogPrintf(CTDL_DEBUG, "Serializing\n");
1440 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1441 icalcomponent_free(encaps); /* Don't need this anymore. */
1443 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1444 if (serialized_request != NULL) {
1445 client_write(serialized_request, strlen(serialized_request));
1446 free(serialized_request);
1450 /* Go back to the room from which we came... */
1451 getroom(&CC->room, hold_rm);
1457 * Backend for ical_getics()
1459 * This is a ForEachMessage() callback function that searches the current room
1460 * for calendar events and adds them each into one big calendar component.
1462 void ical_getics_backend(long msgnum, void *data) {
1463 icalcomponent *encaps, *c;
1464 struct CtdlMessage *msg = NULL;
1465 struct ical_respond_data ird;
1467 encaps = (icalcomponent *)data;
1468 if (encaps == NULL) return;
1470 /* Look for the calendar event... */
1472 msg = CtdlFetchMessage(msgnum, 1);
1473 if (msg == NULL) return;
1474 memset(&ird, 0, sizeof ird);
1475 strcpy(ird.desired_partnum, "_HUNT_");
1476 mime_parser(msg->cm_fields['M'],
1478 *ical_locate_part, /* callback function */
1480 (void *) &ird, /* user data */
1483 CtdlFreeMessage(msg);
1485 if (ird.cal == NULL) return;
1487 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1488 * are responsible for "the_request"'s memory -- it will be freed
1489 * when we free "encaps".
1492 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1493 * in. This will almost never happen.
1495 if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
1496 icalcomponent_add_component(encaps, ird.cal);
1499 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1500 * and other components encapsulated inside, we have to extract them.
1503 for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
1505 c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
1506 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1508 icalcomponent_free(ird.cal);
1515 * Retrieve all of the calendar items in the current room, and output them
1516 * as a single icalendar object.
1518 void ical_getics(void)
1520 icalcomponent *encaps = NULL;
1523 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1524 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1525 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1526 return; /* Not an iCalendar-centric room */
1529 encaps = icalcomponent_new_vcalendar();
1530 if (encaps == NULL) {
1531 CtdlLogPrintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1532 __FILE__, __LINE__);
1533 cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
1537 cprintf("%d one big calendar\n", LISTING_FOLLOWS);
1539 /* Set the Product ID */
1540 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1542 /* Set the Version Number */
1543 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1545 /* Set the method to PUBLISH */
1546 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1548 /* Now go through the room encapsulating all calendar items. */
1549 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1552 ical_getics_backend,
1556 ser = strdup(icalcomponent_as_ical_string(encaps));
1557 client_write(ser, strlen(ser));
1560 icalcomponent_free(encaps); /* Don't need this anymore. */
1566 * Delete all of the calendar items in the current room, and replace them
1567 * with calendar items from a client-supplied data stream.
1569 void ical_putics(void)
1571 char *calstream = NULL;
1575 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1576 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1577 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1578 return; /* Not an iCalendar-centric room */
1581 if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
1582 cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
1586 cprintf("%d Transmit data now\n", SEND_LISTING);
1587 calstream = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
1588 if (calstream == NULL) {
1592 cal = icalcomponent_new_from_string(calstream);
1596 /* We got our data stream -- now do something with it. */
1598 /* Delete the existing messages in the room, because we are replacing
1599 * the entire calendar with an entire new (or updated) calendar.
1600 * (Careful: this opens an S_ROOMS critical section!)
1602 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
1604 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1605 * in. This will almost never happen.
1607 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
1608 ical_write_to_cal(NULL, cal);
1611 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1612 * and other components encapsulated inside, we have to extract them.
1615 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1617 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1618 ical_write_to_cal(NULL, c);
1622 icalcomponent_free(cal);
1627 * All Citadel calendar commands from the client come through here.
1629 void cmd_ical(char *argbuf)
1637 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1639 /* Allow "test" and "freebusy" subcommands without logging in. */
1641 if (!strcasecmp(subcmd, "test")) {
1642 cprintf("%d This server supports calendaring\n", CIT_OK);
1646 if (!strcasecmp(subcmd, "freebusy")) {
1647 extract_token(who, argbuf, 1, '|', sizeof who);
1652 if (!strcasecmp(subcmd, "sgi")) {
1653 CIT_ICAL->server_generated_invitations =
1654 (extract_int(argbuf, 1) ? 1 : 0) ;
1656 CIT_OK, CIT_ICAL->server_generated_invitations);
1660 if (CtdlAccessCheck(ac_logged_in)) return;
1662 if (!strcasecmp(subcmd, "respond")) {
1663 msgnum = extract_long(argbuf, 1);
1664 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1665 extract_token(action, argbuf, 3, '|', sizeof action);
1666 ical_respond(msgnum, partnum, action);
1670 if (!strcasecmp(subcmd, "handle_rsvp")) {
1671 msgnum = extract_long(argbuf, 1);
1672 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1673 extract_token(action, argbuf, 3, '|', sizeof action);
1674 ical_handle_rsvp(msgnum, partnum, action);
1678 if (!strcasecmp(subcmd, "conflicts")) {
1679 msgnum = extract_long(argbuf, 1);
1680 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1681 ical_conflicts(msgnum, partnum);
1685 if (!strcasecmp(subcmd, "getics")) {
1690 if (!strcasecmp(subcmd, "putics")) {
1695 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1701 * We don't know if the calendar room exists so we just create it at login
1703 void ical_create_room(void)
1708 /* Create the calendar room if it doesn't already exist */
1709 create_room(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1711 /* Set expiration policy to manual; otherwise objects will be lost! */
1712 if (lgetroom(&qr, USERCALENDARROOM)) {
1713 CtdlLogPrintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1716 qr.QRep.expire_mode = EXPIRE_MANUAL;
1717 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1720 /* Set the view to a calendar view */
1721 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1722 vbuf.v_view = VIEW_CALENDAR;
1723 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1725 /* Create the tasks list room if it doesn't already exist */
1726 create_room(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1728 /* Set expiration policy to manual; otherwise objects will be lost! */
1729 if (lgetroom(&qr, USERTASKSROOM)) {
1730 CtdlLogPrintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1733 qr.QRep.expire_mode = EXPIRE_MANUAL;
1734 qr.QRdefaultview = VIEW_TASKS;
1737 /* Set the view to a task list view */
1738 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1739 vbuf.v_view = VIEW_TASKS;
1740 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1742 /* Create the notes room if it doesn't already exist */
1743 create_room(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1745 /* Set expiration policy to manual; otherwise objects will be lost! */
1746 if (lgetroom(&qr, USERNOTESROOM)) {
1747 CtdlLogPrintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1750 qr.QRep.expire_mode = EXPIRE_MANUAL;
1751 qr.QRdefaultview = VIEW_NOTES;
1754 /* Set the view to a notes view */
1755 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1756 vbuf.v_view = VIEW_NOTES;
1757 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1764 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1767 void ical_send_out_invitations(icalcomponent *cal) {
1768 icalcomponent *the_request = NULL;
1769 char *serialized_request = NULL;
1770 icalcomponent *encaps = NULL;
1771 char *request_message_text = NULL;
1772 struct CtdlMessage *msg = NULL;
1773 struct recptypes *valid = NULL;
1774 char attendees_string[SIZ];
1775 int num_attendees = 0;
1776 char this_attendee[256];
1777 icalproperty *attendee = NULL;
1778 char summary_string[SIZ];
1779 icalproperty *summary = NULL;
1783 CtdlLogPrintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
1788 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1789 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1790 ical_send_out_invitations(
1791 icalcomponent_get_first_component(
1792 cal, ICAL_VEVENT_COMPONENT
1798 /* Clone the event */
1799 the_request = icalcomponent_new_clone(cal);
1800 if (the_request == NULL) {
1801 CtdlLogPrintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
1805 /* Extract the summary string -- we'll use it as the
1806 * message subject for the request
1808 strcpy(summary_string, "Meeting request");
1809 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1810 if (summary != NULL) {
1811 if (icalproperty_get_summary(summary)) {
1812 strcpy(summary_string,
1813 icalproperty_get_summary(summary) );
1817 /* Determine who the recipients of this message are (the attendees) */
1818 strcpy(attendees_string, "");
1819 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1820 if (icalproperty_get_attendee(attendee)) {
1821 safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
1822 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1823 strcpy(this_attendee, &this_attendee[7]);
1825 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
1826 snprintf(&attendees_string[strlen(attendees_string)],
1827 sizeof(attendees_string) - strlen(attendees_string),
1837 CtdlLogPrintf(CTDL_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1839 /* If there are no attendees, there are no invitations to send, so...
1840 * don't bother putting one together! Punch out, Maverick!
1842 if (num_attendees == 0) {
1843 icalcomponent_free(the_request);
1847 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1848 encaps = icalcomponent_new_vcalendar();
1849 if (encaps == NULL) {
1850 CtdlLogPrintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1851 __FILE__, __LINE__);
1852 icalcomponent_free(the_request);
1856 /* Set the Product ID */
1857 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1859 /* Set the Version Number */
1860 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1862 /* Set the method to REQUEST */
1863 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1865 /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1866 ical_dezonify(the_request);
1868 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1869 * are responsible for "the_request"'s memory -- it will be freed
1870 * when we free "encaps".
1872 icalcomponent_add_component(encaps, the_request);
1875 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1876 icalcomponent_free(encaps); /* Don't need this anymore. */
1877 if (serialized_request == NULL) return;
1879 reqsize = strlen(serialized_request) + SIZ;
1880 request_message_text = malloc(reqsize);
1881 if (request_message_text != NULL) {
1882 snprintf(request_message_text, reqsize,
1883 "Content-type: text/calendar\r\n\r\n%s\r\n",
1887 msg = CtdlMakeMessage(&CC->user,
1888 "", /* No single recipient here */
1889 "", /* No single recipient here */
1890 CC->room.QRname, 0, FMT_RFC822,
1893 summary_string, /* Use summary for subject */
1895 request_message_text,
1899 valid = validate_recipients(attendees_string, NULL, 0);
1900 CtdlSubmitMsg(msg, valid, "", QP_EADDR);
1901 CtdlFreeMessage(msg);
1902 free_recipients(valid);
1905 free(serialized_request);
1910 * When a calendar object is being saved, determine whether it's a VEVENT
1911 * and the user saving it is the organizer. If so, send out invitations
1912 * to any listed attendees.
1915 void ical_saving_vevent(icalcomponent *cal) {
1917 icalproperty *organizer = NULL;
1918 char organizer_string[SIZ];
1920 CtdlLogPrintf(CTDL_DEBUG, "ical_saving_vevent() has been called!\n");
1922 /* Don't send out invitations unless the client wants us to. */
1923 if (CIT_ICAL->server_generated_invitations == 0) {
1927 /* Don't send out invitations if we've been asked not to. */
1928 if (CIT_ICAL->avoid_sending_invitations > 0) {
1932 strcpy(organizer_string, "");
1934 * The VEVENT subcomponent is the one we're interested in.
1935 * Send out invitations if, and only if, this user is the Organizer.
1937 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1938 organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
1939 if (organizer != NULL) {
1940 if (icalproperty_get_organizer(organizer)) {
1941 strcpy(organizer_string,
1942 icalproperty_get_organizer(organizer));
1945 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1946 strcpy(organizer_string, &organizer_string[7]);
1947 striplt(organizer_string);
1949 * If the user saving the event is listed as the
1950 * organizer, then send out invitations.
1952 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
1953 ical_send_out_invitations(cal);
1958 /* If the component has subcomponents, recurse through them. */
1959 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1961 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1962 /* Recursively process subcomponent */
1963 ical_saving_vevent(c);
1971 * Back end for ical_obj_beforesave()
1972 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1973 * the summary of the event (becomes message subject),
1974 * and the start time (becomes message date/time).
1976 void ical_obj_beforesave_backend(char *name, char *filename, char *partnum,
1977 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1978 char *encoding, void *cbuserdata)
1980 icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
1982 char new_uid[256] = "";
1983 char buf[1024] = "";
1984 struct CtdlMessage *msg = (struct CtdlMessage *) cbuserdata;
1988 /* We're only interested in calendar data. */
1989 if ( (strcasecmp(cbtype, "text/calendar"))
1990 && (strcasecmp(cbtype, "application/ics")) ) {
1994 /* Hunt for the UID and drop it in
1995 * the "user data" pointer for the MIME parser. When
1996 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1999 whole_cal = icalcomponent_new_from_string(content);
2002 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
2003 nested_event = icalcomponent_get_first_component(
2004 cal, ICAL_VEVENT_COMPONENT);
2005 if (nested_event != NULL) {
2009 nested_todo = icalcomponent_get_first_component(
2010 cal, ICAL_VTODO_COMPONENT);
2011 if (nested_todo != NULL) {
2019 /* Set the message EUID to the iCalendar UID */
2021 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
2023 /* If there's no uid we must generate one */
2024 generate_uuid(new_uid);
2025 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
2026 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
2029 safestrncpy(buf, icalproperty_get_comment(p), sizeof buf);
2030 if (!IsEmptyStr(buf)) {
2031 if (msg->cm_fields['E'] != NULL) {
2032 free(msg->cm_fields['E']);
2034 msg->cm_fields['E'] = strdup(buf);
2035 CtdlLogPrintf(CTDL_DEBUG, "Saving calendar UID <%s>\n", buf);
2039 /* Set the message subject to the iCalendar summary */
2041 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
2043 safestrncpy(buf, icalproperty_get_comment(p), sizeof buf);
2044 if (!IsEmptyStr(buf)) {
2045 if (msg->cm_fields['U'] != NULL) {
2046 free(msg->cm_fields['U']);
2048 msg->cm_fields['U'] = strdup(buf);
2052 /* Set the message date/time to the iCalendar start time */
2054 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
2057 idtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
2059 if (msg->cm_fields['T'] != NULL) {
2060 free(msg->cm_fields['T']);
2062 msg->cm_fields['T'] = strdup("000000000000000000");
2063 sprintf(msg->cm_fields['T'], "%ld", idtstart);
2068 icalcomponent_free(cal);
2069 if (whole_cal != cal) {
2070 icalcomponent_free(whole_cal);
2079 * See if we need to prevent the object from being saved (we don't allow
2080 * MIME types other than text/calendar in "calendar" or "tasks" rooms).
2082 * If the message is being saved, we also set various message header fields
2083 * using data found in the iCalendar object.
2085 int ical_obj_beforesave(struct CtdlMessage *msg)
2087 /* First determine if this is a calendar or tasks room */
2088 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
2089 && (CC->room.QRdefaultview != VIEW_TASKS)
2091 return(0); /* Not an iCalendar-centric room */
2094 /* It must be an RFC822 message! */
2095 if (msg->cm_format_type != 4) {
2096 CtdlLogPrintf(CTDL_DEBUG, "Rejecting non-RFC822 message\n");
2097 return(1); /* You tried to save a non-RFC822 message! */
2100 if (msg->cm_fields['M'] == NULL) {
2101 return(1); /* You tried to save a null message! */
2104 /* Do all of our lovely back-end parsing */
2105 mime_parser(msg->cm_fields['M'],
2107 *ical_obj_beforesave_backend,
2118 * Things we need to do after saving a calendar event.
2120 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
2121 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2122 char *encoding, void *cbuserdata)
2126 /* We're only interested in calendar items here. */
2127 if ( (strcasecmp(cbtype, "text/calendar"))
2128 && (strcasecmp(cbtype, "application/ics")) ) {
2132 /* Hunt for the UID and drop it in
2133 * the "user data" pointer for the MIME parser. When
2134 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2137 if ( (!strcasecmp(cbtype, "text/calendar"))
2138 || (!strcasecmp(cbtype, "application/ics")) ) {
2139 cal = icalcomponent_new_from_string(content);
2141 ical_saving_vevent(cal);
2142 icalcomponent_free(cal);
2149 * Things we need to do after saving a calendar event.
2150 * (This will start back end tasks such as automatic generation of invitations,
2151 * if such actions are appropriate.)
2153 int ical_obj_aftersave(struct CtdlMessage *msg)
2155 char roomname[ROOMNAMELEN];
2158 * If this isn't the Calendar> room, no further action is necessary.
2161 /* First determine if this is our room */
2162 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
2163 if (strcasecmp(roomname, CC->room.QRname)) {
2164 return(0); /* Not the Calendar room -- don't do anything. */
2167 /* It must be an RFC822 message! */
2168 if (msg->cm_format_type != 4) return(1);
2170 /* Reject null messages */
2171 if (msg->cm_fields['M'] == NULL) return(1);
2173 /* Now recurse through it looking for our icalendar data */
2174 mime_parser(msg->cm_fields['M'],
2176 *ical_obj_aftersave_backend,
2186 void ical_session_startup(void) {
2187 CIT_ICAL = malloc(sizeof(struct cit_ical));
2188 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
2191 void ical_session_shutdown(void) {
2197 * Back end for ical_fixed_output()
2199 void ical_fixed_output_backend(icalcomponent *cal,
2206 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
2208 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2211 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
2213 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2216 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
2218 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2221 /* If the component has attendees, iterate through them. */
2222 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
2223 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
2224 if (!strncasecmp(buf, "MAILTO:", 7)) {
2226 /* screen name or email address */
2227 strcpy(buf, &buf[7]);
2229 cprintf("%s ", buf);
2234 /* If the component has subcomponents, recurse through them. */
2235 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2237 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2238 /* Recursively process subcomponent */
2239 ical_fixed_output_backend(c, recursion_level+1);
2246 * Function to output iCalendar data as plain text. Nobody uses MSG0
2247 * anymore, so really this is just so we expose the vCard data to the full
2250 void ical_fixed_output(char *ptr, int len) {
2254 stringy_cal = malloc(len + 1);
2255 safestrncpy(stringy_cal, ptr, len + 1);
2256 cal = icalcomponent_new_from_string(stringy_cal);
2264 ical_fixed_output_backend(cal, 0);
2266 /* Free the memory we obtained from libical's constructor */
2267 icalcomponent_free(cal);
2272 void serv_calendar_destroy(void)
2274 icaltimezone_free_builtin_timezones();
2278 * Register this module with the Citadel server.
2280 CTDL_MODULE_INIT(calendar)
2284 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2285 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2286 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
2287 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2288 CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2289 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2290 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2291 CtdlRegisterFixedOutputHook("application/ics", ical_fixed_output);
2292 CtdlRegisterCleanupHook(serv_calendar_destroy);
2295 /* return our Subversion id for the Log */