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. */
895 * Called by ical_hunt_for_conflicts_backend()
897 * At this point we've got it boiled down to two icalcomponent events in memory.
898 * If they conflict, output something to the client.
900 void ical_output_conflicts(icalcomponent *proposed_event,
901 icalcomponent *existing_event,
902 long existing_msgnum)
904 struct icaltimetype t1start, t1end, t2start, t2end;
905 t1start = icaltime_null_time();
906 t1end = icaltime_null_time();
907 t2start = icaltime_null_time();
908 t1end = icaltime_null_time();
910 char conflict_event_uid[SIZ];
911 char conflict_event_summary[SIZ];
912 char compare_uid[SIZ];
917 strcpy(compare_uid, "");
918 strcpy(conflict_event_uid, "");
919 strcpy(conflict_event_summary, "");
922 /* existing event stuff */
924 p = ical_ctdl_get_subprop(existing_event, ICAL_DTSTART_PROPERTY);
925 if (p == NULL) return;
926 if (p != NULL) t2start = icalproperty_get_dtstart(p);
928 p = ical_ctdl_get_subprop(existing_event, ICAL_DTEND_PROPERTY);
929 if (p != NULL) t2end = icalproperty_get_dtend(p);
931 p = ical_ctdl_get_subprop(existing_event, ICAL_UID_PROPERTY);
933 strcpy(conflict_event_uid, icalproperty_get_comment(p));
936 p = ical_ctdl_get_subprop(existing_event, ICAL_SUMMARY_PROPERTY);
938 strcpy(conflict_event_summary, icalproperty_get_comment(p));
942 /* proposed event stuff */
944 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTSTART_PROPERTY);
945 if (p == NULL) return;
946 if (p != NULL) t1start = icalproperty_get_dtstart(p);
948 p = ical_ctdl_get_subprop(proposed_event, ICAL_DTEND_PROPERTY);
949 if (p != NULL) t1end = icalproperty_get_dtend(p);
951 p = ical_ctdl_get_subprop(proposed_event, ICAL_UID_PROPERTY);
953 strcpy(compare_uid, icalproperty_get_comment(p));
957 /* compare and output */
959 if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
960 cprintf("%ld||%s|%s|%d|\n",
963 conflict_event_summary,
964 ( ((strlen(compare_uid)>0)
965 &&(!strcasecmp(compare_uid,
966 conflict_event_uid))) ? 1 : 0
976 * Called by ical_hunt_for_conflicts()
978 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
979 icalcomponent *proposed_event;
980 struct CtdlMessage *msg = NULL;
981 struct ical_respond_data ird;
983 proposed_event = (icalcomponent *)data;
985 msg = CtdlFetchMessage(msgnum, 1);
986 if (msg == NULL) return;
987 memset(&ird, 0, sizeof ird);
988 strcpy(ird.desired_partnum, "_HUNT_");
989 mime_parser(msg->cm_fields['M'],
991 *ical_locate_part, /* callback function */
993 (void *) &ird, /* user data */
996 CtdlFreeMessage(msg);
998 if (ird.cal == NULL) return;
1001 ical_output_conflicts(proposed_event, ird.cal, msgnum);
1002 icalcomponent_free(ird.cal);
1008 * Phase 2 of "hunt for conflicts" operation.
1009 * At this point we have a calendar object which represents the VEVENT that
1010 * is proposed for addition to the calendar. Now hunt through the user's
1011 * calendar room, and output zero or more existing VEVENTs which conflict
1014 void ical_hunt_for_conflicts(icalcomponent *cal) {
1015 char hold_rm[ROOMNAMELEN];
1017 strcpy(hold_rm, CC->room.QRname); /* save current room */
1019 if (getroom(&CC->room, USERCALENDARROOM) != 0) {
1020 getroom(&CC->room, hold_rm);
1021 cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
1025 cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
1027 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1030 ical_hunt_for_conflicts_backend,
1035 getroom(&CC->room, hold_rm); /* return to saved room */
1042 * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
1044 void ical_conflicts(long msgnum, char *partnum) {
1045 struct CtdlMessage *msg = NULL;
1046 struct ical_respond_data ird;
1048 msg = CtdlFetchMessage(msgnum, 1);
1050 cprintf("%d Message %ld not found.\n",
1051 ERROR + ILLEGAL_VALUE,
1057 memset(&ird, 0, sizeof ird);
1058 strcpy(ird.desired_partnum, partnum);
1059 mime_parser(msg->cm_fields['M'],
1061 *ical_locate_part, /* callback function */
1063 (void *) &ird, /* user data */
1067 CtdlFreeMessage(msg);
1069 if (ird.cal != NULL) {
1070 ical_hunt_for_conflicts(ird.cal);
1071 icalcomponent_free(ird.cal);
1075 cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
1079 /* should never get here */
1085 * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
1087 void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
1090 struct icalperiodtype my_period;
1092 if (cal == NULL) return;
1093 my_period = icalperiodtype_null_period();
1095 if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
1096 ical_add_to_freebusy(fb,
1097 icalcomponent_get_first_component(
1098 cal, ICAL_VEVENT_COMPONENT
1106 /* If this event is not opaque, the user isn't publishing it as
1107 * busy time, so don't bother doing anything else.
1109 p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
1111 v = icalproperty_get_value(p);
1113 if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
1119 /* Convert the DTSTART and DTEND properties to an icalperiod. */
1120 p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
1122 my_period.start = icalproperty_get_dtstart(p);
1125 p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
1127 my_period.end = icalproperty_get_dtstart(p);
1131 icalcomponent_add_property(fb,
1132 icalproperty_new_freebusy(my_period)
1135 /* Make sure the DTSTART property of the freebusy *list* is set to
1136 * the DTSTART property of the *earliest event*.
1138 p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
1140 icalcomponent_set_dtstart(fb,
1141 icalcomponent_get_dtstart(cal) );
1144 if (icaltime_compare(
1145 icalcomponent_get_dtstart(cal),
1146 icalcomponent_get_dtstart(fb)
1148 icalcomponent_set_dtstart(fb,
1149 icalcomponent_get_dtstart(cal) );
1153 /* Make sure the DTEND property of the freebusy *list* is set to
1154 * the DTEND property of the *latest event*.
1156 p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
1158 icalcomponent_set_dtend(fb,
1159 icalcomponent_get_dtend(cal) );
1162 if (icaltime_compare(
1163 icalcomponent_get_dtend(cal),
1164 icalcomponent_get_dtend(fb)
1166 icalcomponent_set_dtend(fb,
1167 icalcomponent_get_dtend(cal) );
1176 * Backend for ical_freebusy()
1178 * This function simply loads the messages in the user's calendar room,
1179 * which contain VEVENTs, then strips them of all non-freebusy data, and
1180 * adds them to the supplied VCALENDAR.
1183 void ical_freebusy_backend(long msgnum, void *data) {
1185 struct CtdlMessage *msg = NULL;
1186 struct ical_respond_data ird;
1188 cal = (icalcomponent *)data;
1190 msg = CtdlFetchMessage(msgnum, 1);
1191 if (msg == NULL) return;
1192 memset(&ird, 0, sizeof ird);
1193 strcpy(ird.desired_partnum, "_HUNT_");
1194 mime_parser(msg->cm_fields['M'],
1196 *ical_locate_part, /* callback function */
1198 (void *) &ird, /* user data */
1201 CtdlFreeMessage(msg);
1203 if (ird.cal == NULL) return;
1205 ical_add_to_freebusy(cal, ird.cal);
1207 /* Now free the memory. */
1208 icalcomponent_free(ird.cal);
1214 * Grab another user's free/busy times
1216 void ical_freebusy(char *who) {
1217 struct ctdluser usbuf;
1218 char calendar_room_name[ROOMNAMELEN];
1219 char hold_rm[ROOMNAMELEN];
1220 char *serialized_request = NULL;
1221 icalcomponent *encaps = NULL;
1222 icalcomponent *fb = NULL;
1223 int found_user = (-1);
1224 struct recptypes *recp = NULL;
1229 int config_lines = 0;
1231 /* First try an exact match. */
1232 found_user = getuser(&usbuf, who);
1234 /* If not found, try it as an unqualified email address. */
1235 if (found_user != 0) {
1237 recp = validate_recipients(buf, NULL, 0);
1238 CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1240 if (recp->num_local == 1) {
1241 found_user = getuser(&usbuf, recp->recp_local);
1243 free_recipients(recp);
1247 /* If still not found, try it as an address qualified with the
1248 * primary FQDN of this Citadel node.
1250 if (found_user != 0) {
1251 snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
1252 CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1253 recp = validate_recipients(buf, NULL, 0);
1255 if (recp->num_local == 1) {
1256 found_user = getuser(&usbuf, recp->recp_local);
1258 free_recipients(recp);
1262 /* Still not found? Try qualifying it with every domain we
1263 * might have addresses in.
1265 if (found_user != 0) {
1266 config_lines = num_tokens(inetcfg, '\n');
1267 for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
1268 extract_token(buf, inetcfg, i, '\n', sizeof buf);
1269 extract_token(host, buf, 0, '|', sizeof host);
1270 extract_token(type, buf, 1, '|', sizeof type);
1272 if ( (!strcasecmp(type, "localhost"))
1273 || (!strcasecmp(type, "directory")) ) {
1274 snprintf(buf, sizeof buf, "%s@%s", who, host);
1275 CtdlLogPrintf(CTDL_DEBUG, "Trying <%s>\n", buf);
1276 recp = validate_recipients(buf, NULL, 0);
1278 if (recp->num_local == 1) {
1279 found_user = getuser(&usbuf, recp->recp_local);
1281 free_recipients(recp);
1287 if (found_user != 0) {
1288 cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
1292 MailboxName(calendar_room_name, sizeof calendar_room_name,
1293 &usbuf, USERCALENDARROOM);
1295 strcpy(hold_rm, CC->room.QRname); /* save current room */
1297 if (getroom(&CC->room, calendar_room_name) != 0) {
1298 cprintf("%d Cannot open calendar\n", ERROR + ROOM_NOT_FOUND);
1299 getroom(&CC->room, hold_rm);
1303 /* Create a VFREEBUSY subcomponent */
1304 CtdlLogPrintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
1305 fb = icalcomponent_new_vfreebusy();
1307 cprintf("%d Internal error: cannot allocate memory.\n",
1308 ERROR + INTERNAL_ERROR);
1309 getroom(&CC->room, hold_rm);
1313 /* Set the method to PUBLISH */
1314 icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
1316 /* Set the DTSTAMP to right now. */
1317 icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
1319 /* Add the user's email address as ORGANIZER */
1320 sprintf(buf, "MAILTO:%s", who);
1321 if (strchr(buf, '@') == NULL) {
1323 strcat(buf, config.c_fqdn);
1325 for (i=0; buf[i]; ++i) {
1326 if (buf[i]==' ') buf[i] = '_';
1328 icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
1330 /* Add busy time from events */
1331 CtdlLogPrintf(CTDL_DEBUG, "Adding busy time from events\n");
1332 CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
1334 /* If values for DTSTART and DTEND are still not present, set them
1335 * to yesterday and tomorrow as default values.
1337 if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
1338 icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
1340 if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
1341 icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
1344 /* Put the freebusy component into the calendar component */
1345 CtdlLogPrintf(CTDL_DEBUG, "Encapsulating\n");
1346 encaps = ical_encapsulate_subcomponent(fb);
1347 if (encaps == NULL) {
1348 icalcomponent_free(fb);
1349 cprintf("%d Internal error: cannot allocate memory.\n",
1350 ERROR + INTERNAL_ERROR);
1351 getroom(&CC->room, hold_rm);
1355 /* Set the method to PUBLISH */
1356 CtdlLogPrintf(CTDL_DEBUG, "Setting method\n");
1357 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1360 CtdlLogPrintf(CTDL_DEBUG, "Serializing\n");
1361 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1362 icalcomponent_free(encaps); /* Don't need this anymore. */
1364 cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
1365 if (serialized_request != NULL) {
1366 client_write(serialized_request, strlen(serialized_request));
1367 free(serialized_request);
1371 /* Go back to the room from which we came... */
1372 getroom(&CC->room, hold_rm);
1378 * Backend for ical_getics()
1380 * This is a ForEachMessage() callback function that searches the current room
1381 * for calendar events and adds them each into one big calendar component.
1383 void ical_getics_backend(long msgnum, void *data) {
1384 icalcomponent *encaps, *c;
1385 struct CtdlMessage *msg = NULL;
1386 struct ical_respond_data ird;
1388 encaps = (icalcomponent *)data;
1389 if (encaps == NULL) return;
1391 /* Look for the calendar event... */
1393 msg = CtdlFetchMessage(msgnum, 1);
1394 if (msg == NULL) return;
1395 memset(&ird, 0, sizeof ird);
1396 strcpy(ird.desired_partnum, "_HUNT_");
1397 mime_parser(msg->cm_fields['M'],
1399 *ical_locate_part, /* callback function */
1401 (void *) &ird, /* user data */
1404 CtdlFreeMessage(msg);
1406 if (ird.cal == NULL) return;
1408 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1409 * are responsible for "the_request"'s memory -- it will be freed
1410 * when we free "encaps".
1413 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1414 * in. This will almost never happen.
1416 if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
1417 icalcomponent_add_component(encaps, ird.cal);
1420 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1421 * and other components encapsulated inside, we have to extract them.
1424 for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
1426 c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
1427 icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
1429 icalcomponent_free(ird.cal);
1436 * Retrieve all of the calendar items in the current room, and output them
1437 * as a single icalendar object.
1439 void ical_getics(void)
1441 icalcomponent *encaps = NULL;
1444 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1445 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1446 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1447 return; /* Not an iCalendar-centric room */
1450 encaps = icalcomponent_new_vcalendar();
1451 if (encaps == NULL) {
1452 CtdlLogPrintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1453 __FILE__, __LINE__);
1454 cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
1458 cprintf("%d one big calendar\n", LISTING_FOLLOWS);
1460 /* Set the Product ID */
1461 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1463 /* Set the Version Number */
1464 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1466 /* Set the method to PUBLISH */
1467 icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
1469 /* Now go through the room encapsulating all calendar items. */
1470 CtdlForEachMessage(MSGS_ALL, 0, NULL,
1473 ical_getics_backend,
1477 ser = strdup(icalcomponent_as_ical_string(encaps));
1478 client_write(ser, strlen(ser));
1481 icalcomponent_free(encaps); /* Don't need this anymore. */
1487 * Delete all of the calendar items in the current room, and replace them
1488 * with calendar items from a client-supplied data stream.
1490 void ical_putics(void)
1492 char *calstream = NULL;
1496 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
1497 &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
1498 cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
1499 return; /* Not an iCalendar-centric room */
1502 if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
1503 cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
1507 cprintf("%d Transmit data now\n", SEND_LISTING);
1508 calstream = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
1509 if (calstream == NULL) {
1513 cal = icalcomponent_new_from_string(calstream);
1517 /* We got our data stream -- now do something with it. */
1519 /* Delete the existing messages in the room, because we are replacing
1520 * the entire calendar with an entire new (or updated) calendar.
1521 * (Careful: this opens an S_ROOMS critical section!)
1523 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
1525 /* If the top-level component is *not* a VCALENDAR, we can drop it right
1526 * in. This will almost never happen.
1528 if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
1529 ical_write_to_cal(NULL, cal);
1532 * In the more likely event that we're looking at a VCALENDAR with the VEVENT
1533 * and other components encapsulated inside, we have to extract them.
1536 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1538 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1539 ical_write_to_cal(NULL, c);
1543 icalcomponent_free(cal);
1548 * All Citadel calendar commands from the client come through here.
1550 void cmd_ical(char *argbuf)
1558 extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
1560 /* Allow "test" and "freebusy" subcommands without logging in. */
1562 if (!strcasecmp(subcmd, "test")) {
1563 cprintf("%d This server supports calendaring\n", CIT_OK);
1567 if (!strcasecmp(subcmd, "freebusy")) {
1568 extract_token(who, argbuf, 1, '|', sizeof who);
1573 if (!strcasecmp(subcmd, "sgi")) {
1574 CIT_ICAL->server_generated_invitations =
1575 (extract_int(argbuf, 1) ? 1 : 0) ;
1577 CIT_OK, CIT_ICAL->server_generated_invitations);
1581 if (CtdlAccessCheck(ac_logged_in)) return;
1583 if (!strcasecmp(subcmd, "respond")) {
1584 msgnum = extract_long(argbuf, 1);
1585 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1586 extract_token(action, argbuf, 3, '|', sizeof action);
1587 ical_respond(msgnum, partnum, action);
1591 if (!strcasecmp(subcmd, "handle_rsvp")) {
1592 msgnum = extract_long(argbuf, 1);
1593 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1594 extract_token(action, argbuf, 3, '|', sizeof action);
1595 ical_handle_rsvp(msgnum, partnum, action);
1599 if (!strcasecmp(subcmd, "conflicts")) {
1600 msgnum = extract_long(argbuf, 1);
1601 extract_token(partnum, argbuf, 2, '|', sizeof partnum);
1602 ical_conflicts(msgnum, partnum);
1606 if (!strcasecmp(subcmd, "getics")) {
1611 if (!strcasecmp(subcmd, "putics")) {
1616 cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
1622 * We don't know if the calendar room exists so we just create it at login
1624 void ical_create_room(void)
1629 /* Create the calendar room if it doesn't already exist */
1630 create_room(USERCALENDARROOM, 4, "", 0, 1, 0, VIEW_CALENDAR);
1632 /* Set expiration policy to manual; otherwise objects will be lost! */
1633 if (lgetroom(&qr, USERCALENDARROOM)) {
1634 CtdlLogPrintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1637 qr.QRep.expire_mode = EXPIRE_MANUAL;
1638 qr.QRdefaultview = VIEW_CALENDAR; /* 3 = calendar view */
1641 /* Set the view to a calendar view */
1642 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1643 vbuf.v_view = VIEW_CALENDAR;
1644 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1646 /* Create the tasks list room if it doesn't already exist */
1647 create_room(USERTASKSROOM, 4, "", 0, 1, 0, VIEW_TASKS);
1649 /* Set expiration policy to manual; otherwise objects will be lost! */
1650 if (lgetroom(&qr, USERTASKSROOM)) {
1651 CtdlLogPrintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1654 qr.QRep.expire_mode = EXPIRE_MANUAL;
1655 qr.QRdefaultview = VIEW_TASKS;
1658 /* Set the view to a task list view */
1659 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1660 vbuf.v_view = VIEW_TASKS;
1661 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1663 /* Create the notes room if it doesn't already exist */
1664 create_room(USERNOTESROOM, 4, "", 0, 1, 0, VIEW_NOTES);
1666 /* Set expiration policy to manual; otherwise objects will be lost! */
1667 if (lgetroom(&qr, USERNOTESROOM)) {
1668 CtdlLogPrintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
1671 qr.QRep.expire_mode = EXPIRE_MANUAL;
1672 qr.QRdefaultview = VIEW_NOTES;
1675 /* Set the view to a notes view */
1676 CtdlGetRelationship(&vbuf, &CC->user, &qr);
1677 vbuf.v_view = VIEW_NOTES;
1678 CtdlSetRelationship(&vbuf, &CC->user, &qr);
1685 * ical_send_out_invitations() is called by ical_saving_vevent() when it
1688 void ical_send_out_invitations(icalcomponent *cal) {
1689 icalcomponent *the_request = NULL;
1690 char *serialized_request = NULL;
1691 icalcomponent *encaps = NULL;
1692 char *request_message_text = NULL;
1693 struct CtdlMessage *msg = NULL;
1694 struct recptypes *valid = NULL;
1695 char attendees_string[SIZ];
1696 int num_attendees = 0;
1697 char this_attendee[256];
1698 icalproperty *attendee = NULL;
1699 char summary_string[SIZ];
1700 icalproperty *summary = NULL;
1704 CtdlLogPrintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
1709 /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
1710 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1711 ical_send_out_invitations(
1712 icalcomponent_get_first_component(
1713 cal, ICAL_VEVENT_COMPONENT
1719 /* Clone the event */
1720 the_request = icalcomponent_new_clone(cal);
1721 if (the_request == NULL) {
1722 CtdlLogPrintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
1726 /* Extract the summary string -- we'll use it as the
1727 * message subject for the request
1729 strcpy(summary_string, "Meeting request");
1730 summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
1731 if (summary != NULL) {
1732 if (icalproperty_get_summary(summary)) {
1733 strcpy(summary_string,
1734 icalproperty_get_summary(summary) );
1738 /* Determine who the recipients of this message are (the attendees) */
1739 strcpy(attendees_string, "");
1740 for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
1741 if (icalproperty_get_attendee(attendee)) {
1742 safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
1743 if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
1744 strcpy(this_attendee, &this_attendee[7]);
1746 if (!CtdlIsMe(this_attendee, sizeof this_attendee)) { /* don't send an invitation to myself! */
1747 snprintf(&attendees_string[strlen(attendees_string)],
1748 sizeof(attendees_string) - strlen(attendees_string),
1758 CtdlLogPrintf(CTDL_DEBUG, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
1760 /* If there are no attendees, there are no invitations to send, so...
1761 * don't bother putting one together! Punch out, Maverick!
1763 if (num_attendees == 0) {
1764 icalcomponent_free(the_request);
1768 /* Encapsulate the VEVENT component into a complete VCALENDAR */
1769 encaps = icalcomponent_new_vcalendar();
1770 if (encaps == NULL) {
1771 CtdlLogPrintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
1772 __FILE__, __LINE__);
1773 icalcomponent_free(the_request);
1777 /* Set the Product ID */
1778 icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
1780 /* Set the Version Number */
1781 icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
1783 /* Set the method to REQUEST */
1784 icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
1786 /* Now make sure all of the DTSTART and DTEND properties are UTC. */
1787 ical_dezonify(the_request);
1789 /* Here we go: put the VEVENT into the VCALENDAR. We now no longer
1790 * are responsible for "the_request"'s memory -- it will be freed
1791 * when we free "encaps".
1793 icalcomponent_add_component(encaps, the_request);
1796 serialized_request = strdup(icalcomponent_as_ical_string(encaps));
1797 icalcomponent_free(encaps); /* Don't need this anymore. */
1798 if (serialized_request == NULL) return;
1800 reqsize = strlen(serialized_request) + SIZ;
1801 request_message_text = malloc(reqsize);
1802 if (request_message_text != NULL) {
1803 snprintf(request_message_text, reqsize,
1804 "Content-type: text/calendar\r\n\r\n%s\r\n",
1808 msg = CtdlMakeMessage(&CC->user,
1809 "", /* No single recipient here */
1810 "", /* No single recipient here */
1811 CC->room.QRname, 0, FMT_RFC822,
1814 summary_string, /* Use summary for subject */
1816 request_message_text,
1820 valid = validate_recipients(attendees_string, NULL, 0);
1821 CtdlSubmitMsg(msg, valid, "", QP_EADDR);
1822 CtdlFreeMessage(msg);
1823 free_recipients(valid);
1826 free(serialized_request);
1831 * When a calendar object is being saved, determine whether it's a VEVENT
1832 * and the user saving it is the organizer. If so, send out invitations
1833 * to any listed attendees.
1836 void ical_saving_vevent(icalcomponent *cal) {
1838 icalproperty *organizer = NULL;
1839 char organizer_string[SIZ];
1841 CtdlLogPrintf(CTDL_DEBUG, "ical_saving_vevent() has been called!\n");
1843 /* Don't send out invitations unless the client wants us to. */
1844 if (CIT_ICAL->server_generated_invitations == 0) {
1848 /* Don't send out invitations if we've been asked not to. */
1849 if (CIT_ICAL->avoid_sending_invitations > 0) {
1853 strcpy(organizer_string, "");
1855 * The VEVENT subcomponent is the one we're interested in.
1856 * Send out invitations if, and only if, this user is the Organizer.
1858 if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
1859 organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
1860 if (organizer != NULL) {
1861 if (icalproperty_get_organizer(organizer)) {
1862 strcpy(organizer_string,
1863 icalproperty_get_organizer(organizer));
1866 if (!strncasecmp(organizer_string, "MAILTO:", 7)) {
1867 strcpy(organizer_string, &organizer_string[7]);
1868 striplt(organizer_string);
1870 * If the user saving the event is listed as the
1871 * organizer, then send out invitations.
1873 if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
1874 ical_send_out_invitations(cal);
1879 /* If the component has subcomponents, recurse through them. */
1880 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
1882 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
1883 /* Recursively process subcomponent */
1884 ical_saving_vevent(c);
1892 * Back end for ical_obj_beforesave()
1893 * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
1894 * the summary of the event (becomes message subject),
1895 * and the start time (becomes message date/time).
1897 void ical_obj_beforesave_backend(char *name, char *filename, char *partnum,
1898 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
1899 char *encoding, void *cbuserdata)
1901 icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
1903 char new_uid[256] = "";
1904 char buf[1024] = "";
1905 struct CtdlMessage *msg = (struct CtdlMessage *) cbuserdata;
1909 /* We're only interested in calendar data. */
1910 if ( (strcasecmp(cbtype, "text/calendar"))
1911 && (strcasecmp(cbtype, "application/ics")) ) {
1915 /* Hunt for the UID and drop it in
1916 * the "user data" pointer for the MIME parser. When
1917 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
1920 whole_cal = icalcomponent_new_from_string(content);
1923 if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
1924 nested_event = icalcomponent_get_first_component(
1925 cal, ICAL_VEVENT_COMPONENT);
1926 if (nested_event != NULL) {
1930 nested_todo = icalcomponent_get_first_component(
1931 cal, ICAL_VTODO_COMPONENT);
1932 if (nested_todo != NULL) {
1940 /* Set the message EUID to the iCalendar UID */
1942 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1944 /* If there's no uid we must generate one */
1945 generate_uuid(new_uid);
1946 icalcomponent_add_property(cal, icalproperty_new_uid(new_uid));
1947 p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
1950 safestrncpy(buf, icalproperty_get_comment(p), sizeof buf);
1951 if (!IsEmptyStr(buf)) {
1952 if (msg->cm_fields['E'] != NULL) {
1953 free(msg->cm_fields['E']);
1955 msg->cm_fields['E'] = strdup(buf);
1956 CtdlLogPrintf(CTDL_DEBUG, "Saving calendar UID <%s>\n", buf);
1960 /* Set the message subject to the iCalendar summary */
1962 p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
1964 safestrncpy(buf, icalproperty_get_comment(p), sizeof buf);
1965 if (!IsEmptyStr(buf)) {
1966 if (msg->cm_fields['U'] != NULL) {
1967 free(msg->cm_fields['U']);
1969 msg->cm_fields['U'] = strdup(buf);
1973 /* Set the message date/time to the iCalendar start time */
1975 p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
1978 idtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
1980 if (msg->cm_fields['T'] != NULL) {
1981 free(msg->cm_fields['T']);
1983 msg->cm_fields['T'] = strdup("000000000000000000");
1984 sprintf(msg->cm_fields['T'], "%ld", idtstart);
1989 icalcomponent_free(cal);
1990 if (whole_cal != cal) {
1991 icalcomponent_free(whole_cal);
2000 * See if we need to prevent the object from being saved (we don't allow
2001 * MIME types other than text/calendar in "calendar" or "tasks" rooms).
2003 * If the message is being saved, we also set various message header fields
2004 * using data found in the iCalendar object.
2006 int ical_obj_beforesave(struct CtdlMessage *msg)
2008 /* First determine if this is a calendar or tasks room */
2009 if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
2010 && (CC->room.QRdefaultview != VIEW_TASKS)
2012 return(0); /* Not an iCalendar-centric room */
2015 /* It must be an RFC822 message! */
2016 if (msg->cm_format_type != 4) {
2017 CtdlLogPrintf(CTDL_DEBUG, "Rejecting non-RFC822 message\n");
2018 return(1); /* You tried to save a non-RFC822 message! */
2021 if (msg->cm_fields['M'] == NULL) {
2022 return(1); /* You tried to save a null message! */
2025 /* Do all of our lovely back-end parsing */
2026 mime_parser(msg->cm_fields['M'],
2028 *ical_obj_beforesave_backend,
2039 * Things we need to do after saving a calendar event.
2041 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
2042 char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
2043 char *encoding, void *cbuserdata)
2047 /* We're only interested in calendar items here. */
2048 if ( (strcasecmp(cbtype, "text/calendar"))
2049 && (strcasecmp(cbtype, "application/ics")) ) {
2053 /* Hunt for the UID and drop it in
2054 * the "user data" pointer for the MIME parser. When
2055 * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
2058 if ( (!strcasecmp(cbtype, "text/calendar"))
2059 || (!strcasecmp(cbtype, "application/ics")) ) {
2060 cal = icalcomponent_new_from_string(content);
2062 ical_saving_vevent(cal);
2063 icalcomponent_free(cal);
2070 * Things we need to do after saving a calendar event.
2071 * (This will start back end tasks such as automatic generation of invitations,
2072 * if such actions are appropriate.)
2074 int ical_obj_aftersave(struct CtdlMessage *msg)
2076 char roomname[ROOMNAMELEN];
2079 * If this isn't the Calendar> room, no further action is necessary.
2082 /* First determine if this is our room */
2083 MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
2084 if (strcasecmp(roomname, CC->room.QRname)) {
2085 return(0); /* Not the Calendar room -- don't do anything. */
2088 /* It must be an RFC822 message! */
2089 if (msg->cm_format_type != 4) return(1);
2091 /* Reject null messages */
2092 if (msg->cm_fields['M'] == NULL) return(1);
2094 /* Now recurse through it looking for our icalendar data */
2095 mime_parser(msg->cm_fields['M'],
2097 *ical_obj_aftersave_backend,
2107 void ical_session_startup(void) {
2108 CIT_ICAL = malloc(sizeof(struct cit_ical));
2109 memset(CIT_ICAL, 0, sizeof(struct cit_ical));
2112 void ical_session_shutdown(void) {
2118 * Back end for ical_fixed_output()
2120 void ical_fixed_output_backend(icalcomponent *cal,
2127 p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
2129 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2132 p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
2134 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2137 p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
2139 cprintf("%s\n", (const char *)icalproperty_get_comment(p));
2142 /* If the component has attendees, iterate through them. */
2143 for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
2144 safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
2145 if (!strncasecmp(buf, "MAILTO:", 7)) {
2147 /* screen name or email address */
2148 strcpy(buf, &buf[7]);
2150 cprintf("%s ", buf);
2155 /* If the component has subcomponents, recurse through them. */
2156 for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
2158 c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
2159 /* Recursively process subcomponent */
2160 ical_fixed_output_backend(c, recursion_level+1);
2167 * Function to output iCalendar data as plain text. Nobody uses MSG0
2168 * anymore, so really this is just so we expose the vCard data to the full
2171 void ical_fixed_output(char *ptr, int len) {
2175 stringy_cal = malloc(len + 1);
2176 safestrncpy(stringy_cal, ptr, len + 1);
2177 cal = icalcomponent_new_from_string(stringy_cal);
2185 ical_fixed_output_backend(cal, 0);
2187 /* Free the memory we obtained from libical's constructor */
2188 icalcomponent_free(cal);
2193 void serv_calendar_destroy(void)
2195 icaltimezone_free_builtin_timezones();
2199 * Register this module with the Citadel server.
2201 CTDL_MODULE_INIT(calendar)
2205 CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
2206 CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
2207 CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
2208 CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
2209 CtdlRegisterSessionHook(ical_session_startup, EVT_START);
2210 CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
2211 CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
2212 CtdlRegisterFixedOutputHook("application/ics", ical_fixed_output);
2213 CtdlRegisterCleanupHook(serv_calendar_destroy);
2216 /* return our Subversion id for the Log */