* Reference count adjustments are now deferred by queuing
[citadel.git] / citadel / serv_calendar.c
index 08e090a9bb8079665e279c79e4ef61386f66f161..cbb3f84c44af6b378b5bbce5c3a29bcc6bed9ca9 100644 (file)
@@ -33,6 +33,7 @@
 #include "mime_parser.h"
 #include "internet_addressing.h"
 #include "serv_calendar.h"
+#include "euidindex.h"
 
 #ifdef CITADEL_WITH_CALENDAR_SERVICE
 
@@ -103,8 +104,6 @@ icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
 
 /*
  * Write a calendar object into the specified user's calendar room.
- * 
- * ok
  */
 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
        char temp[PATH_MAX];
@@ -126,7 +125,7 @@ void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
                return;
        }
 
-       strcpy(temp, tmpnam(NULL));
+       CtdlMakeTempFileName(temp, sizeof temp);
        ser = icalcomponent_as_ical_string(cal);
        if (ser == NULL) return;
 
@@ -315,10 +314,13 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                        serialized_reply
                );
 
-               msg = CtdlMakeMessage(&CC->user, organizer_string,
+               msg = CtdlMakeMessage(&CC->user,
+                       organizer_string,       /* to */
+                       "",                     /* cc */
                        CC->room.QRname, 0, FMT_RFC822,
                        "",
                        summary_string,         /* Use summary for subject */
+                       NULL,
                        reply_message_text);
        
                if (msg != NULL) {
@@ -337,7 +339,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
  * and turns them into calendar objects
  */
 void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
-               void *content, char *cbtype, size_t length, char *encoding,
+               void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                void *cbuserdata) {
 
        struct ical_respond_data *ird = NULL;
@@ -428,7 +430,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->room.QRname, msgnum, "");
+               CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
 
                /* Free the memory we allocated and return a response. */
                icalcomponent_free(ird.cal);
@@ -495,7 +497,7 @@ struct original_event_container {
  * to fetch the object being updated)
  */
 void ical_locate_original_event(char *name, char *filename, char *partnum, char *disp,
-               void *content, char *cbtype, size_t length, char *encoding,
+               void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
                void *cbuserdata) {
 
        struct original_event_container *oec = NULL;
@@ -604,7 +606,6 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
        char uid[SIZ];
        char hold_rm[ROOMNAMELEN];
        long msgnum_being_replaced = 0;
-       struct CtdlMessage *template = NULL;
        struct CtdlMessage *msg;
        struct original_event_container oec;
        icalcomponent *original_event;
@@ -626,18 +627,13 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
        }
 
        /*
-        * Pound through the user's calendar looking for a message with
+        * Look in the EUID index for a message with
         * the Citadel EUID set to the value we're looking for.  Since
         * Citadel always sets the message EUID to the vCalendar UID of
         * the event, this will work.
         */
-       template = (struct CtdlMessage *)
-               malloc(sizeof(struct CtdlMessage));
-       memset(template, 0, sizeof(struct CtdlMessage));
-       template->cm_fields['E'] = strdup(uid);
-       CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
-               template, ical_hunt_for_event_to_update, &msgnum_being_replaced);
-       CtdlFreeMessage(template);
+       msgnum_being_replaced = locate_message_by_euid(uid, &CC->room);
+
        getroom(&CC->room, hold_rm);    /* return to saved room */
 
        lprintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
@@ -689,11 +685,13 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
                );
 
                msg = CtdlMakeMessage(&CC->user,
+                       "",                     /* No recipient */
                        "",                     /* No recipient */
                        roomname,
                        0, FMT_RFC822,
                        "",
                        "",             /* no subject */
+                       NULL,
                        message_text);
        
                if (msg != NULL) {
@@ -779,7 +777,7 @@ 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.  (Maybe make this optional?)
                 */
-               CtdlDeleteMessages(CC->room.QRname, msgnum, "");
+               CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
 
                /* Free the memory we allocated and return a response. */
                icalcomponent_free(ird.cal);
@@ -978,7 +976,7 @@ void ical_hunt_for_conflicts(icalcomponent *cal) {
 
        cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
 
-       CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
+       CtdlForEachMessage(MSGS_ALL, 0, NULL, "text/calendar",
                NULL,
                ical_hunt_for_conflicts_backend,
                (void *) cal
@@ -1175,9 +1173,9 @@ void ical_freebusy(char *who) {
        icalcomponent *fb = NULL;
        int found_user = (-1);
        struct recptypes *recp = NULL;
-       char buf[SIZ];
-       char host[SIZ];
-       char type[SIZ];
+       char buf[256];
+       char host[256];
+       char type[256];
        int i = 0;
        int config_lines = 0;
 
@@ -1218,9 +1216,9 @@ void ical_freebusy(char *who) {
        if (found_user != 0) {
                config_lines = num_tokens(inetcfg, '\n');
                for (i=0; ((i < config_lines) && (found_user != 0)); ++i) {
-                       extract_token(buf, inetcfg, i, '\n');
-                       extract_token(host, buf, 0, '|');
-                       extract_token(type, buf, 1, '|');
+                       extract_token(buf, inetcfg, i, '\n', sizeof buf);
+                       extract_token(host, buf, 0, '|', sizeof host);
+                       extract_token(type, buf, 1, '|', sizeof type);
 
                        if ( (!strcasecmp(type, "localhost"))
                           || (!strcasecmp(type, "directory")) ) {
@@ -1283,7 +1281,7 @@ void ical_freebusy(char *who) {
 
        /* Add busy time from events */
        lprintf(CTDL_DEBUG, "Adding busy time from events\n");
-       CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
+       CtdlForEachMessage(MSGS_ALL, 0, NULL, "text/calendar",
                NULL, ical_freebusy_backend, (void *)fb
        );
 
@@ -1330,19 +1328,188 @@ void ical_freebusy(char *who) {
 
 
 
+/*
+ * Backend for ical_getics()
+ * 
+ * This is a ForEachMessage() callback function that searches the current room
+ * for calendar events and adds them each into one big calendar component.
+ */
+void ical_getics_backend(long msgnum, void *data) {
+       icalcomponent *encaps, *c;
+       struct CtdlMessage *msg;
+       struct ical_respond_data ird;
+
+       encaps = (icalcomponent *)data;
+       if (encaps == NULL) return;
+
+       /* Look for the calendar event... */
+
+       msg = CtdlFetchMessage(msgnum, 1);
+       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;
+
+       /* Here we go: put the VEVENT into the VCALENDAR.  We now no longer
+        * are responsible for "the_request"'s memory -- it will be freed
+        * when we free "encaps".
+        */
+
+       /* If the top-level component is *not* a VCALENDAR, we can drop it right
+        * in.  This will almost never happen.
+        */
+       if (icalcomponent_isa(ird.cal) != ICAL_VCALENDAR_COMPONENT) {
+               icalcomponent_add_component(encaps, ird.cal);
+       }
+       /*
+        * In the more likely event that we're looking at a VCALENDAR with the VEVENT
+        * and other components encapsulated inside, we have to extract them.
+        */
+       else {
+               for (c = icalcomponent_get_first_component(ird.cal, ICAL_ANY_COMPONENT);
+                   (c != NULL);
+                   c = icalcomponent_get_next_component(ird.cal, ICAL_ANY_COMPONENT)) {
+                       icalcomponent_add_component(encaps, icalcomponent_new_clone(c));
+               }
+               icalcomponent_free(ird.cal);
+       }
+}
+
+
+
+/*
+ * Retrieve all of the calendar items in the current room, and output them
+ * as a single icalendar object.
+ */
+void ical_getics(void)
+{
+       icalcomponent *encaps = NULL;
+       char *ser = NULL;
+
+       if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
+          &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
+               cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
+               return;         /* Not a vCalendar-centric room */
+       }
+
+       encaps = icalcomponent_new_vcalendar();
+       if (encaps == NULL) {
+               lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
+                       __FILE__, __LINE__);
+               cprintf("%d Could not allocate memory\n", ERROR+INTERNAL_ERROR);
+               return;
+       }
+
+       cprintf("%d one big calendar\n", LISTING_FOLLOWS);
+
+       /* 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"));
+
+       /* Set the method to REQUEST */
+       icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
+
+       /* Now go through the room encapsulating all calendar items. */
+       CtdlForEachMessage(MSGS_ALL, 0, NULL,
+               "text/calendar",
+               NULL,
+               ical_getics_backend,
+               (void *) encaps
+       );
+
+       ser = strdup(icalcomponent_as_ical_string(encaps));
+       client_write(ser, strlen(ser));
+       free(ser);
+       cprintf("\n000\n");
+       icalcomponent_free(encaps);     /* Don't need this anymore. */
+
+}
+
+
+/*
+ * Delete all of the calendar items in the current room, and replace them
+ * with calendar items from a client-supplied data stream.
+ */
+void ical_putics(void)
+{
+       char *calstream = NULL;
+       icalcomponent *cal;
+       icalcomponent *c;
+
+       if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
+          &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
+               cprintf("%d Not a calendar room\n", ERROR+NOT_HERE);
+               return;         /* Not a vCalendar-centric room */
+       }
+
+       if (!CtdlDoIHavePermissionToDeleteMessagesFromThisRoom()) {
+               cprintf("%d Permission denied.\n", ERROR+HIGHER_ACCESS_REQUIRED);
+               return;
+       }
+
+       cprintf("%d Transmit data now\n", SEND_LISTING);
+        calstream = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0);
+       if (calstream == NULL) {
+               return;
+       }
+
+       cal = icalcomponent_new_from_string(calstream);
+       free(calstream);
+       ical_dezonify(cal);
+
+       /* We got our data stream -- now do something with it. */
+
+       /* Delete the existing messages in the room, because we are replacing
+        * the entire calendar with an entire new (or updated) calendar.
+        * (Careful: this opens an S_ROOMS critical section!)
+        */
+       CtdlDeleteMessages(CC->room.QRname, NULL, 0, "");
+
+       /* If the top-level component is *not* a VCALENDAR, we can drop it right
+        * in.  This will almost never happen.
+        */
+       if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
+               ical_write_to_cal(&CC->user, cal);
+       }
+       /*
+        * In the more likely event that we're looking at a VCALENDAR with the VEVENT
+        * and other components encapsulated inside, we have to extract them.
+        */
+       else {
+               for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
+                   (c != NULL);
+                   c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
+                       ical_write_to_cal(&CC->user, c);
+               }
+       }
+
+       icalcomponent_free(cal);
+}
+
 
 /*
  * All Citadel calendar commands from the client come through here.
  */
 void cmd_ical(char *argbuf)
 {
-       char subcmd[SIZ];
+       char subcmd[64];
        long msgnum;
-       char partnum[SIZ];
-       char action[SIZ];
-       char who[SIZ];
+       char partnum[256];
+       char action[256];
+       char who[256];
 
-       extract(subcmd, argbuf, 0);
+       extract_token(subcmd, argbuf, 0, '|', sizeof subcmd);
 
        /* Allow "test" and "freebusy" subcommands without logging in. */
 
@@ -1352,7 +1519,7 @@ void cmd_ical(char *argbuf)
        }
 
        if (!strcasecmp(subcmd, "freebusy")) {
-               extract(who, argbuf, 1);
+               extract_token(who, argbuf, 1, '|', sizeof who);
                ical_freebusy(who);
                return;
        }
@@ -1369,27 +1536,37 @@ void cmd_ical(char *argbuf)
 
        if (!strcasecmp(subcmd, "respond")) {
                msgnum = extract_long(argbuf, 1);
-               extract(partnum, argbuf, 2);
-               extract(action, argbuf, 3);
+               extract_token(partnum, argbuf, 2, '|', sizeof partnum);
+               extract_token(action, argbuf, 3, '|', sizeof action);
                ical_respond(msgnum, partnum, action);
                return;
        }
 
        if (!strcasecmp(subcmd, "handle_rsvp")) {
                msgnum = extract_long(argbuf, 1);
-               extract(partnum, argbuf, 2);
-               extract(action, argbuf, 3);
+               extract_token(partnum, argbuf, 2, '|', sizeof partnum);
+               extract_token(action, argbuf, 3, '|', sizeof action);
                ical_handle_rsvp(msgnum, partnum, action);
                return;
        }
 
        if (!strcasecmp(subcmd, "conflicts")) {
                msgnum = extract_long(argbuf, 1);
-               extract(partnum, argbuf, 2);
+               extract_token(partnum, argbuf, 2, '|', sizeof partnum);
                ical_conflicts(msgnum, partnum);
                return;
        }
 
+       if (!strcasecmp(subcmd, "getics")) {
+               ical_getics();
+               return;
+       }
+
+       if (!strcasecmp(subcmd, "putics")) {
+               ical_putics();
+               return;
+       }
+
        cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
 }
 
@@ -1471,7 +1648,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
        struct recptypes *valid = NULL;
        char attendees_string[SIZ];
        int num_attendees = 0;
-       char this_attendee[SIZ];
+       char this_attendee[256];
        icalproperty *attendee = NULL;
        char summary_string[SIZ];
        icalproperty *summary = NULL;
@@ -1515,11 +1692,11 @@ void ical_send_out_invitations(icalcomponent *cal) {
        strcpy(attendees_string, "");
        for (attendee = icalcomponent_get_first_property(the_request, ICAL_ATTENDEE_PROPERTY); attendee != NULL; attendee = icalcomponent_get_next_property(the_request, ICAL_ATTENDEE_PROPERTY)) {
                if (icalproperty_get_attendee(attendee)) {
-                       strcpy(this_attendee, icalproperty_get_attendee(attendee) );
+                       safestrncpy(this_attendee, icalproperty_get_attendee(attendee), sizeof this_attendee);
                        if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
                                strcpy(this_attendee, &this_attendee[7]);
 
-                               if (!CtdlIsMe(this_attendee)) { /* don't send an invitation to myself! */
+                               if (!CtdlIsMe(this_attendee, sizeof this_attendee)) {   /* don't send an invitation to myself! */
                                        snprintf(&attendees_string[strlen(attendees_string)],
                                                sizeof(attendees_string) - strlen(attendees_string),
                                                "%s, ",
@@ -1581,10 +1758,12 @@ void ical_send_out_invitations(icalcomponent *cal) {
                );
 
                msg = CtdlMakeMessage(&CC->user,
+                       "",                     /* No single recipient here */
                        "",                     /* No single recipient here */
                        CC->room.QRname, 0, FMT_RFC822,
                        "",
                        summary_string,         /* Use summary for subject */
+                       NULL,
                        request_message_text);
        
                if (msg != NULL) {
@@ -1639,7 +1818,7 @@ void ical_saving_vevent(icalcomponent *cal) {
                         * If the user saving the event is listed as the
                         * organizer, then send out invitations.
                         */
-                       if (CtdlIsMe(organizer_string)) {
+                       if (CtdlIsMe(organizer_string, sizeof organizer_string)) {
                                ical_send_out_invitations(cal);
                        }
                }
@@ -1664,10 +1843,10 @@ void ical_saving_vevent(icalcomponent *cal) {
  * and the start time (becomes message date/time).
  */
 void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
-               char *disp, void *content, char *cbtype, size_t length,
+               char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
                char *encoding, void *cbuserdata)
 {
-       icalcomponent *cal;
+       icalcomponent *cal, *nested_event, *nested_todo;
        icalproperty *p;
        struct icalmessagemod *imm;
        char new_uid[SIZ];
@@ -1683,9 +1862,18 @@ void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
                cal = icalcomponent_new_from_string(content);
                if (cal != NULL) {
                        if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
-                               cal = icalcomponent_get_first_component(
+                               nested_event = icalcomponent_get_first_component(
                                        cal, ICAL_VEVENT_COMPONENT
                                );
+                               nested_todo = icalcomponent_get_first_component(
+                                       cal, ICAL_VTODO_COMPONENT
+                               );
+                               if (nested_event != NULL) {
+                                       cal = nested_event;
+                               }
+                               else if (nested_todo != NULL) {
+                                       cal = nested_todo;
+                               }
                        }
                }
                if (cal != NULL) {
@@ -1698,14 +1886,11 @@ void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
                        }
                        if (p != NULL) {
                                strcpy(imm->uid, icalproperty_get_comment(p));
-                               strcpy(imm->subject,
-                                               icalproperty_get_comment(p));
+                               /* strcpy(imm->subject, icalproperty_get_comment(p)); old aethera hack */
                        }
                        p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
                        if (p != NULL) {
-                               strcat(imm->subject, " ");
-                               strcat(imm->subject,
-                                               icalproperty_get_comment(p));
+                               strcpy(imm->subject, icalproperty_get_comment(p));
                        }
                        p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
                        if (p != NULL) {
@@ -1733,14 +1918,13 @@ void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
  */
 int ical_obj_beforesave(struct CtdlMessage *msg)
 {
-       char roomname[ROOMNAMELEN];
        char *p;
        int a;
        struct icalmessagemod imm;
 
        /* First determine if this is a calendar or tasks room */
-       if ( (CC->curr_view != VIEW_CALENDAR)
-          &&(CC->curr_view != VIEW_TASKS) ) {
+       if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
+          &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
                return(0);              /* Not a vCalendar-centric room */
        }
 
@@ -1801,7 +1985,7 @@ int ical_obj_beforesave(struct CtdlMessage *msg)
  * Things we need to do after saving a calendar event.
  */
 void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
-               char *disp, void *content, char *cbtype, size_t length,
+               char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
                char *encoding, void *cbuserdata)
 {
        icalcomponent *cal;
@@ -1843,7 +2027,6 @@ int ical_obj_aftersave(struct CtdlMessage *msg)
        /* Then determine content-type of the message */
        
        /* It must be an RFC822 message! */
-       /* FIXME: Not handling MIME multipart messages; implement with IMIP */
        if (msg->cm_format_type != 4) return(1);
        
        /* Find the Content-Type: header */
@@ -1875,10 +2058,89 @@ int ical_obj_aftersave(struct CtdlMessage *msg)
 
 
 void ical_session_startup(void) {
-       CtdlAllocUserData(SYM_CIT_ICAL, sizeof(struct cit_ical));
+       CIT_ICAL = malloc(sizeof(struct cit_ical));
        memset(CIT_ICAL, 0, sizeof(struct cit_ical));
 }
 
+void ical_session_shutdown(void) {
+       free(CIT_ICAL);
+}
+
+
+/*
+ * Back end for ical_fixed_output()
+ */
+void ical_fixed_output_backend(icalcomponent *cal,
+                       int recursion_level
+) {
+       icalcomponent *c;
+       icalproperty *p;
+       char buf[256];
+
+       p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
+       if (p != NULL) {
+               cprintf("%s\n", (const char *)icalproperty_get_comment(p));
+       }
+
+       p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
+       if (p != NULL) {
+               cprintf("%s\n", (const char *)icalproperty_get_comment(p));
+       }
+
+       p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
+       if (p != NULL) {
+               cprintf("%s\n", (const char *)icalproperty_get_comment(p));
+       }
+
+       /* If the component has attendees, iterate through them. */
+       for (p = icalcomponent_get_first_property(cal, ICAL_ATTENDEE_PROPERTY); (p != NULL); p = icalcomponent_get_next_property(cal, ICAL_ATTENDEE_PROPERTY)) {
+               safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
+               if (!strncasecmp(buf, "MAILTO:", 7)) {
+
+                       /* screen name or email address */
+                       strcpy(buf, &buf[7]);
+                       striplt(buf);
+                       cprintf("%s ", buf);
+               }
+               cprintf("\n");
+       }
+
+       /* If the component has subcomponents, recurse through them. */
+       for (c = icalcomponent_get_first_component(cal, ICAL_ANY_COMPONENT);
+           (c != 0);
+           c = icalcomponent_get_next_component(cal, ICAL_ANY_COMPONENT)) {
+               /* Recursively process subcomponent */
+               ical_fixed_output_backend(c, recursion_level+1);
+       }
+}
+
+
+
+/*
+ * Function to output vcalendar data as plain text.  Nobody uses MSG0
+ * anymore, so really this is just so we expose the vCard data to the full
+ * text indexer.
+ */
+void ical_fixed_output(char *ptr, int len) {
+       icalcomponent *cal;
+       char *stringy_cal;
+
+       stringy_cal = malloc(len + 1);
+       safestrncpy(stringy_cal, ptr, len + 1);
+       cal = icalcomponent_new_from_string(stringy_cal);
+       free(stringy_cal);
+
+       if (cal == NULL) {
+               return;
+       }
+
+       ical_dezonify(cal);
+       ical_fixed_output_backend(cal, 0);
+
+       /* Free the memory we obtained from libical's constructor */
+       icalcomponent_free(cal);
+}
+
 
 #endif /* CITADEL_WITH_CALENDAR_SERVICE */
 
@@ -1893,6 +2155,14 @@ char *serv_calendar_init(void)
        CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
        CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
        CtdlRegisterSessionHook(ical_session_startup, EVT_START);
+       CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
+       CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
 #endif
        return "$Id$";
 }
+
+
+
+
+
+