]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_calendar.c
*** empty log message ***
[citadel.git] / citadel / serv_calendar.c
index e6a8ef32d4175c77941b5e20d19dc2527016f2c0..d0d152cc7909c5a0ea43bc61497f550084b8b821 100644 (file)
@@ -24,7 +24,7 @@
 #include "sysdep_decls.h"
 #include "support.h"
 #include "config.h"
-#include "dynloader.h"
+#include "serv_extensions.h"
 #include "user_ops.h"
 #include "room_ops.h"
 #include "tools.h"
 #include "mime_parser.h"
 #include "serv_calendar.h"
 
-#ifdef HAVE_ICAL_H
+#ifdef CITADEL_WITH_CALENDAR_SERVICE
 
 #include <ical.h>
+#include "ical_dezonify.h"
 
 struct ical_respond_data {
        char desired_partnum[SIZ];
@@ -44,13 +45,87 @@ struct ical_respond_data {
 /* Session-local data for calendaring. */
 long SYM_CIT_ICAL;
 
+
+/*
+ * Utility function to create a new VCALENDAR component with some of the
+ * required fields already set the way we like them.
+ */
+icalcomponent *icalcomponent_new_citadel_vcalendar(void) {
+       icalcomponent *encaps;
+
+       encaps = icalcomponent_new_vcalendar();
+       if (encaps == NULL) {
+               lprintf(3, "Error at %s:%d - could not allocate component!\n",
+                       __FILE__, __LINE__);
+               return NULL;
+       }
+
+       /* Set the Product ID */
+       icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
+
+       /* Set the Version Number */
+       icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
+
+       return(encaps);
+}
+
+
+/*
+ * Utility function to encapsulate a subcomponent into a full VCALENDAR
+ */
+icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
+       icalcomponent *encaps;
+
+       /* If we're already looking at a full VCALENDAR component,
+        * don't bother ... just return itself.
+        */
+       if (icalcomponent_isa(subcomp) == ICAL_VCALENDAR_COMPONENT) {
+               return subcomp;
+       }
+
+       /* Encapsulate the VEVENT component into a complete VCALENDAR */
+       encaps = icalcomponent_new_citadel_vcalendar();
+       if (encaps == NULL) return NULL;
+
+       /* Encapsulate the subcomponent inside */
+       icalcomponent_add_component(encaps, subcomp);
+
+       /* Convert all timestamps to UTC so we don't have to deal with
+        * stupid VTIMEZONE crap.
+        */
+       ical_dezonify(encaps);
+
+       /* Return the object we just created. */
+       return(encaps);
+}
+
+
+
+
 /*
  * Write a calendar object into the specified user's calendar room.
+ * 
+ * ok
  */
-void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
+void ical_write_to_cal(struct user *u, icalcomponent *cal) {
        char temp[PATH_MAX];
        FILE *fp;
        char *ser;
+       icalcomponent *encaps;
+
+       if (cal == NULL) return;
+
+       /* If the supplied object is a subcomponent, encapsulate it in
+        * a full VCALENDAR component, and save that instead.
+        */
+       if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
+               encaps = ical_encapsulate_subcomponent(
+                       icalcomponent_new_clone(cal)
+               );
+               ical_write_to_cal(u, encaps);
+               icalcomponent_free(encaps);
+               return;
+       }
 
        strcpy(temp, tmpnam(NULL));
        ser = icalcomponent_as_ical_string(cal);
@@ -78,6 +153,8 @@ void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
 
 /*
  * Add a calendar object to the user's calendar
+ * 
+ * ok because it uses ical_write_to_cal()
  */
 void ical_add(icalcomponent *cal, int recursion_level) {
        icalcomponent *c;
@@ -87,7 +164,7 @@ void ical_add(icalcomponent *cal, int recursion_level) {
         */
        if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
        
-               ical_write_to_cal(&CC->usersupp, cal);
+               ical_write_to_cal(&CC->user, cal);
 
        }
 
@@ -111,6 +188,8 @@ void ical_add(icalcomponent *cal, int recursion_level) {
  *
  * (Sorry about this being more than 80 columns ... there was just
  * no easy way to break it down sensibly.)
+ * 
+ * ok
  */
 void ical_send_a_reply(icalcomponent *request, char *action) {
        icalcomponent *the_reply = NULL;
@@ -163,7 +242,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                                        striplt(attendee_string);
                                        recp = validate_recipients(attendee_string);
                                        if (recp != NULL) {
-                                               if (!strcasecmp(recp->recp_local, CC->usersupp.fullname)) {
+                                               if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
                                                        if (me_attend) icalproperty_free(me_attend);
                                                        me_attend = icalproperty_new_clone(attendee);
                                                }
@@ -237,8 +316,8 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                        serialized_reply
                );
 
-               msg = CtdlMakeMessage(&CC->usersupp, organizer_string,
-                       CC->quickroom.QRname, 0, FMT_RFC822,
+               msg = CtdlMakeMessage(&CC->user, organizer_string,
+                       CC->room.QRname, 0, FMT_RFC822,
                        "",
                        summary_string,         /* Use summary for subject */
                        reply_message_text);
@@ -265,12 +344,30 @@ void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
        struct ical_respond_data *ird = NULL;
 
        ird = (struct ical_respond_data *) cbuserdata;
+
+       /* desired_partnum can be set to "_HUNT_" to have it just look for
+        * the first part with a content type of text/calendar.  Otherwise
+        * we have to only process the right one.
+        */
+       if (strcasecmp(ird->desired_partnum, "_HUNT_")) {
+               if (strcasecmp(partnum, ird->desired_partnum)) {
+                       return;
+               }
+       }
+
+       if (strcasecmp(cbtype, "text/calendar")) {
+               return;
+       }
+
        if (ird->cal != NULL) {
                icalcomponent_free(ird->cal);
                ird->cal = NULL;
        }
-       if (strcasecmp(partnum, ird->desired_partnum)) return;
+
        ird->cal = icalcomponent_new_from_string(content);
+       if (ird->cal != NULL) {
+               ical_dezonify(ird->cal);
+       }
 }
 
 
@@ -332,7 +429,7 @@ void ical_respond(long msgnum, char *partnum, char *action) {
                /* Now that we've processed this message, we don't need it
                 * anymore.  So delete it.
                 */
-               CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
+               CtdlDeleteMessages(CC->room.QRname, msgnum, "");
 
                /* Free the memory we allocated and return a response. */
                icalcomponent_free(ird.cal);
@@ -526,10 +623,10 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
        ical_learn_uid_of_reply(uid, cal);
        lprintf(9, "UID of event being replied to is <%s>\n", uid);
 
-       strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
+       strcpy(hold_rm, CC->room.QRname);       /* save current room */
 
-       if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
-               getroom(&CC->quickroom, hold_rm);
+       if (getroom(&CC->room, USERCALENDARROOM) != 0) {
+               getroom(&CC->room, hold_rm);
                lprintf(3, "cannot get user calendar room\n");
                return(2);
        }
@@ -547,7 +644,7 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
        CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
                template, ical_hunt_for_event_to_update, &msgnum_being_replaced);
        CtdlFreeMessage(template);
-       getroom(&CC->quickroom, hold_rm);       /* return to saved room */
+       getroom(&CC->room, hold_rm);    /* return to saved room */
 
        lprintf(9, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
        if (msgnum_being_replaced == 0) {
@@ -588,7 +685,7 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
        icalcomponent_free(original_event);     /* Don't need this anymore. */
        if (serialized_event == NULL) return(2);
 
-       MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
+       MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
 
        message_text = mallok(strlen(serialized_event) + SIZ);
        if (message_text != NULL) {
@@ -597,7 +694,7 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
                        serialized_event
                );
 
-               msg = CtdlMakeMessage(&CC->usersupp,
+               msg = CtdlMakeMessage(&CC->user,
                        "",                     /* No recipient */
                        roomname,
                        0, FMT_RFC822,
@@ -686,9 +783,9 @@ void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
                }
 
                /* Now that we've processed this message, we don't need it
-                * anymore.  So delete it.  FIXME uncomment this when ready!
+                * anymore.  So delete it.  (Maybe make this optional?)
                 */
-               CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
+               CtdlDeleteMessages(CC->room.QRname, msgnum, "");
 
                /* Free the memory we allocated and return a response. */
                icalcomponent_free(ird.cal);
@@ -728,6 +825,8 @@ icalproperty *ical_ctdl_get_subprop(
 
 /*
  * Check to see if two events overlap.  Returns nonzero if they do.
+ * (This function is used in both Citadel and WebCit.  If you change it in
+ * one place, change it in the other.  Better yet, put it in a library.)
  */
 int ical_ctdl_is_overlap(
                        struct icaltimetype t1start,
@@ -801,7 +900,7 @@ void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
        msg = CtdlFetchMessage(msgnum);
        if (msg == NULL) return;
        memset(&ird, 0, sizeof ird);
-       strcpy(ird.desired_partnum, "1");       /* hopefully it's always 1 */
+       strcpy(ird.desired_partnum, "_HUNT_");
        mime_parser(msg->cm_fields['M'],
                NULL,
                *ical_locate_part,              /* callback function */
@@ -848,7 +947,6 @@ void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
                strcpy(conflict_event_summary, icalproperty_get_comment(p));
        }
 
-
        icalcomponent_free(ird.cal);
 
        if (ical_ctdl_is_overlap(t1start, t1end, t2start, t2end)) {
@@ -876,10 +974,10 @@ void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
 void ical_hunt_for_conflicts(icalcomponent *cal) {
        char hold_rm[ROOMNAMELEN];
 
-       strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
+       strcpy(hold_rm, CC->room.QRname);       /* save current room */
 
-       if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
-               getroom(&CC->quickroom, hold_rm);
+       if (getroom(&CC->room, USERCALENDARROOM) != 0) {
+               getroom(&CC->room, hold_rm);
                cprintf("%d You do not have a calendar.\n", ERROR);
                return;
        }
@@ -893,7 +991,7 @@ void ical_hunt_for_conflicts(icalcomponent *cal) {
        );
 
        cprintf("000\n");
-       getroom(&CC->quickroom, hold_rm);       /* return to saved room */
+       getroom(&CC->room, hold_rm);    /* return to saved room */
 
 }
 
@@ -942,6 +1040,185 @@ void ical_conflicts(long msgnum, char *partnum) {
 
 
 
+/*
+ * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
+ */
+void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
+       icalproperty *p;
+       icalvalue *v;
+       struct icalperiodtype my_period;
+
+       if (cal == NULL) return;
+       my_period = icalperiodtype_null_period();
+
+       if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
+               ical_add_to_freebusy(fb,
+                       icalcomponent_get_first_component(
+                               cal, ICAL_VEVENT_COMPONENT
+                       )
+               );
+               return;
+       }
+
+       ical_dezonify(cal);
+
+       /* If this event is not opaque, the user isn't publishing it as
+        * busy time, so don't bother doing anything else.
+        */
+       p = icalcomponent_get_first_property(cal, ICAL_TRANSP_PROPERTY);
+       if (p != NULL) {
+               v = icalproperty_get_value(p);
+               if (v != NULL) {
+                       if (icalvalue_get_transp(v) != ICAL_TRANSP_OPAQUE) {
+                               return;
+                       }
+               }
+       }
+
+       /* Convert the DTSTART and DTEND properties to an icalperiod. */
+       p = icalcomponent_get_first_property(cal, ICAL_DTSTART_PROPERTY);
+       if (p != NULL) {
+               my_period.start = icalproperty_get_dtstart(p);
+       }
+
+       p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
+       if (p != NULL) {
+               my_period.end = icalproperty_get_dtstart(p);
+       }
+
+       /* Now add it. */
+       icalcomponent_add_property(fb,
+               icalproperty_new_freebusy(my_period)
+       );
+
+}
+
+
+
+/*
+ * Backend for ical_freebusy()
+ *
+ * This function simply loads the messages in the user's calendar room,
+ * which contain VEVENTs, then strips them of all non-freebusy data, and
+ * adds them to the supplied VCALENDAR.
+ *
+ */
+void ical_freebusy_backend(long msgnum, void *data) {
+       icalcomponent *cal;
+       struct CtdlMessage *msg;
+       struct ical_respond_data ird;
+
+       cal = (icalcomponent *)data;
+
+       msg = CtdlFetchMessage(msgnum);
+       if (msg == NULL) return;
+       memset(&ird, 0, sizeof ird);
+       strcpy(ird.desired_partnum, "_HUNT_");
+       mime_parser(msg->cm_fields['M'],
+               NULL,
+               *ical_locate_part,              /* callback function */
+               NULL, NULL,
+               (void *) &ird,                  /* user data */
+               0
+       );
+       CtdlFreeMessage(msg);
+
+       if (ird.cal == NULL) return;
+
+       ical_add_to_freebusy(cal, ird.cal);
+
+       /* Now free the memory. */
+       icalcomponent_free(ird.cal);
+}
+
+
+
+/*
+ * Grab another user's free/busy times
+ */
+void ical_freebusy(char *who) {
+       struct user usbuf;
+       char calendar_room_name[ROOMNAMELEN];
+       char hold_rm[ROOMNAMELEN];
+       char *serialized_request = NULL;
+       icalcomponent *encaps = NULL;
+       icalcomponent *fb = NULL;
+
+       if (getuser(&usbuf, who) != 0) {
+               cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
+               return;
+       }
+
+       MailboxName(calendar_room_name, sizeof calendar_room_name,
+               &usbuf, USERCALENDARROOM);
+
+       strcpy(hold_rm, CC->room.QRname);       /* save current room */
+
+       if (getroom(&CC->room, calendar_room_name) != 0) {
+               cprintf("%d Cannot open calendar\n", ERROR+ROOM_NOT_FOUND);
+               getroom(&CC->room, hold_rm);
+               return;
+       }
+
+       /* Create a VFREEBUSY subcomponent */
+       lprintf(9, "Creating VFREEBUSY component\n");
+       fb = icalcomponent_new_vfreebusy();
+       if (fb == NULL) {
+               cprintf("%d Internal error: cannot allocate memory.\n",
+                       ERROR+INTERNAL_ERROR);
+               icalcomponent_free(encaps);
+               getroom(&CC->room, hold_rm);
+               return;
+       }
+
+       lprintf(9, "Adding busy time from events\n");
+       CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
+               NULL, ical_freebusy_backend, (void *)fb
+       );
+
+       /* Set the method to PUBLISH */
+       icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
+
+       /* Set the DTSTAMP to right now. */
+       icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
+
+       /* FIXME we still need (at least) DTSTART, DTEND, and the user's
+        * e-mail address as ORGANIZER.
+        */
+
+       /* Put the freebusy component into the calendar component */
+       lprintf(9, "Encapsulating\n");
+       encaps = ical_encapsulate_subcomponent(fb);
+       if (encaps == NULL) {
+               icalcomponent_free(fb);
+               cprintf("%d Internal error: cannot allocate memory.\n",
+                       ERROR+INTERNAL_ERROR);
+               getroom(&CC->room, hold_rm);
+               return;
+       }
+
+       /* Set the method to PUBLISH */
+       lprintf(9, "Setting method\n");
+       icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
+
+       /* Serialize it */
+       lprintf(9, "Serializing\n");
+       serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
+       icalcomponent_free(encaps);     /* Don't need this anymore. */
+
+       cprintf("%d Here is the free/busy data:\n", LISTING_FOLLOWS);
+       if (serialized_request != NULL) {
+               client_write(serialized_request, strlen(serialized_request));
+               phree(serialized_request);
+       }
+       cprintf("\n000\n");
+
+       /* Go back to the room from which we came... */
+       getroom(&CC->room, hold_rm);
+}
+
+
+
 
 /*
  * All Citadel calendar commands from the client come through here.
@@ -952,42 +1229,49 @@ void cmd_ical(char *argbuf)
        long msgnum;
        char partnum[SIZ];
        char action[SIZ];
-
-       if (CtdlAccessCheck(ac_logged_in)) return;
+       char who[SIZ];
 
        extract(subcmd, argbuf, 0);
 
+       /* Allow "test" and "freebusy" subcommands without logging in. */
+
        if (!strcmp(subcmd, "test")) {
                cprintf("%d This server supports calendaring\n", CIT_OK);
                return;
        }
 
-       else if (!strcmp(subcmd, "respond")) {
+       if (!strcmp(subcmd, "freebusy")) {
+               extract(who, argbuf, 1);
+               ical_freebusy(who);
+               return;
+       }
+
+       if (CtdlAccessCheck(ac_logged_in)) return;
+
+       if (!strcmp(subcmd, "respond")) {
                msgnum = extract_long(argbuf, 1);
                extract(partnum, argbuf, 2);
                extract(action, argbuf, 3);
                ical_respond(msgnum, partnum, action);
+               return;
        }
 
-       else if (!strcmp(subcmd, "handle_rsvp")) {
+       if (!strcmp(subcmd, "handle_rsvp")) {
                msgnum = extract_long(argbuf, 1);
                extract(partnum, argbuf, 2);
                extract(action, argbuf, 3);
                ical_handle_rsvp(msgnum, partnum, action);
+               return;
        }
 
-       else if (!strcmp(subcmd, "conflicts")) {
+       if (!strcmp(subcmd, "conflicts")) {
                msgnum = extract_long(argbuf, 1);
                extract(partnum, argbuf, 2);
                ical_conflicts(msgnum, partnum);
-       }
-
-       else {
-               cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
                return;
        }
 
-       /* should never get here */
+       cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
 }
 
 
@@ -997,7 +1281,7 @@ void cmd_ical(char *argbuf)
  */
 void ical_create_room(void)
 {
-       struct quickroom qr;
+       struct room qr;
        struct visit vbuf;
 
        /* Create the calendar room if it doesn't already exist */
@@ -1009,12 +1293,13 @@ void ical_create_room(void)
                return;
        }
        qr.QRep.expire_mode = EXPIRE_MANUAL;
+       qr.QRdefaultview = 3;   /* 3 = calendar view */
        lputroom(&qr);
 
        /* Set the view to a calendar view */
-       CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
+       CtdlGetRelationship(&vbuf, &CC->user, &qr);
        vbuf.v_view = 3;        /* 3 = calendar */
-       CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
+       CtdlSetRelationship(&vbuf, &CC->user, &qr);
 
        /* Create the tasks list room if it doesn't already exist */
        create_room(USERTASKSROOM, 4, "", 0, 1, 0);
@@ -1025,12 +1310,13 @@ void ical_create_room(void)
                return;
        }
        qr.QRep.expire_mode = EXPIRE_MANUAL;
+       qr.QRdefaultview = 4;   /* 4 = tasks view */
        lputroom(&qr);
 
        /* Set the view to a task list view */
-       CtdlGetRelationship(&vbuf, &CC->usersupp, &qr);
+       CtdlGetRelationship(&vbuf, &CC->user, &qr);
        vbuf.v_view = 4;        /* 4 = tasks */
-       CtdlSetRelationship(&vbuf, &CC->usersupp, &qr);
+       CtdlSetRelationship(&vbuf, &CC->user, &qr);
 
        return;
 }
@@ -1043,6 +1329,7 @@ void ical_create_room(void)
 void ical_send_out_invitations(icalcomponent *cal) {
        icalcomponent *the_request = NULL;
        char *serialized_request = NULL;
+       icalcomponent *encaps = NULL;
        char *request_message_text = NULL;
        struct CtdlMessage *msg = NULL;
        struct recptypes *valid = NULL;
@@ -1052,13 +1339,23 @@ void ical_send_out_invitations(icalcomponent *cal) {
        icalproperty *attendee = NULL;
        char summary_string[SIZ];
        icalproperty *summary = NULL;
-       icalcomponent *encaps = NULL;
 
        if (cal == NULL) {
                lprintf(3, "ERROR: trying to reply to NULL event?\n");
                return;
        }
 
+
+       /* If this is a VCALENDAR component, look for a VEVENT subcomponent. */
+       if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
+               ical_send_out_invitations(
+                       icalcomponent_get_first_component(
+                               cal, ICAL_VEVENT_COMPONENT
+                       )
+               );
+               return;
+       }
+
        /* Clone the event */
        the_request = icalcomponent_new_clone(cal);
        if (the_request == NULL) {
@@ -1106,7 +1403,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
        }
 
        /* Encapsulate the VEVENT component into a complete VCALENDAR */
-       encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
+       encaps = icalcomponent_new_vcalendar();
        if (encaps == NULL) {
                lprintf(3, "Error at %s:%d - could not allocate component!\n",
                        __FILE__, __LINE__);
@@ -1123,7 +1420,8 @@ void ical_send_out_invitations(icalcomponent *cal) {
        /* Set the method to REQUEST */
        icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST);
 
-       /* FIXME: here we need to insert a VTIMEZONE object. */
+       /* Now make sure all of the DTSTART and DTEND properties are UTC. */
+       ical_dezonify(the_request);
 
        /* Here we go: put the VEVENT into the VCALENDAR.  We now no longer
         * are responsible for "the_request"'s memory -- it will be freed
@@ -1143,9 +1441,9 @@ void ical_send_out_invitations(icalcomponent *cal) {
                        serialized_request
                );
 
-               msg = CtdlMakeMessage(&CC->usersupp,
+               msg = CtdlMakeMessage(&CC->user,
                        "",                     /* No single recipient here */
-                       CC->quickroom.QRname, 0, FMT_RFC822,
+                       CC->room.QRname, 0, FMT_RFC822,
                        "",
                        summary_string,         /* Use summary for subject */
                        request_message_text);
@@ -1240,6 +1538,13 @@ void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
         */
        if (!strcasecmp(cbtype, "text/calendar")) {
                cal = icalcomponent_new_from_string(content);
+               if (cal != NULL) {
+                       if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
+                               cal = icalcomponent_get_first_component(
+                                       cal, ICAL_VEVENT_COMPONENT
+                               );
+                       }
+               }
                if (cal != NULL) {
                        p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
                        if (p != NULL) {
@@ -1289,8 +1594,8 @@ int ical_obj_beforesave(struct CtdlMessage *msg)
         */
 
        /* First determine if this is our room */
-       MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
-       if (strcasecmp(roomname, CC->quickroom.QRname)) {
+       MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
+       if (strcasecmp(roomname, CC->room.QRname)) {
                return 0;       /* It's not the Calendar room. */
        }
 
@@ -1387,8 +1692,8 @@ int ical_obj_aftersave(struct CtdlMessage *msg)
         */
 
        /* First determine if this is our room */
-       MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
-       if (strcasecmp(roomname, CC->quickroom.QRname)) {
+       MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
+       if (strcasecmp(roomname, CC->room.QRname)) {
                return 0;       /* It's not the Calendar room. */
        }
 
@@ -1427,21 +1732,20 @@ int ical_obj_aftersave(struct CtdlMessage *msg)
 
 
 void ical_session_startup(void) {
-       SYM_CIT_ICAL = CtdlGetDynamicSymbol();
        CtdlAllocUserData(SYM_CIT_ICAL, sizeof(struct cit_ical));
        memset(CIT_ICAL, 0, sizeof(struct cit_ical));
-       
 }
 
 
-#endif /* HAVE_ICAL_H */
+#endif /* CITADEL_WITH_CALENDAR_SERVICE */
 
 /*
  * Register this module with the Citadel server.
  */
-char *Dynamic_Module_Init(void)
+char *serv_calendar_init(void)
 {
-#ifdef HAVE_ICAL_H
+#ifdef CITADEL_WITH_CALENDAR_SERVICE
+       SYM_CIT_ICAL = CtdlGetDynamicSymbol();
        CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
        CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
        CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);