Moved to new module init structure.
[citadel.git] / citadel / serv_calendar.c
index 8b121aef56f9b11f0aa46cee4866eb119bb0d67e..de35044412af7d29c44c283cb159b2e309bc9464 100644 (file)
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
-#include "sysdep_decls.h"
 #include "support.h"
 #include "config.h"
-#include "serv_extensions.h"
 #include "user_ops.h"
 #include "room_ops.h"
 #include "tools.h"
 #include "internet_addressing.h"
 #include "serv_calendar.h"
 #include "euidindex.h"
+#include "ctdl_module.h"
 
 #ifdef CITADEL_WITH_CALENDAR_SERVICE
 
 #include <ical.h>
 #include "ical_dezonify.h"
 
+
+
 struct ical_respond_data {
        char desired_partnum[SIZ];
        icalcomponent *cal;
@@ -104,8 +105,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];
@@ -184,12 +183,8 @@ void ical_add(icalcomponent *cal, int recursion_level) {
  * Send a reply to a meeting invitation.
  *
  * 'request' is the invitation to reply to.
- * 'action' is the string "accept" or "decline".
+ * 'action' is the string "accept" or "decline" or "tentative".
  *
- * (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;
@@ -246,7 +241,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                                                        if (me_attend) icalproperty_free(me_attend);
                                                        me_attend = icalproperty_new_clone(attendee);
                                                }
-                                               free(recp);
+                                               free_recipients(recp);
                                        }
                                }
                        }
@@ -301,7 +296,6 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                                        icalproperty_get_summary(summary) );
                        }
                }
-
        }
 
        /* Now generate the reply message and send it out. */
@@ -312,7 +306,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
        reply_message_text = malloc(strlen(serialized_reply) + SIZ);
        if (reply_message_text != NULL) {
                sprintf(reply_message_text,
-                       "Content-type: text/calendar\r\n\r\n%s\r\n",
+                       "Content-type: text/calendar charset=\"utf-8\"\r\n\r\n%s\r\n",
                        serialized_reply
                );
 
@@ -321,6 +315,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                        "",                     /* cc */
                        CC->room.QRname, 0, FMT_RFC822,
                        "",
+                       "",
                        summary_string,         /* Use summary for subject */
                        NULL,
                        reply_message_text);
@@ -329,6 +324,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                        valid = validate_recipients(organizer_string);
                        CtdlSubmitMsg(msg, valid, "");
                        CtdlFreeMessage(msg);
+                       free_recipients(valid);
                }
        }
        free(serialized_reply);
@@ -378,7 +374,7 @@ void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
  * Respond to a meeting request.
  */
 void ical_respond(long msgnum, char *partnum, char *action) {
-       struct CtdlMessage *msg;
+       struct CtdlMessage *msg = NULL;
        struct ical_respond_data ird;
 
        if (
@@ -430,9 +426,9 @@ 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.
+                * anymore.  So delete it.  (NOTE we don't do this anymore.)
+               CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
                 */
-               CtdlDeleteMessages(CC->room.QRname, msgnum, "", 1);
 
                /* Free the memory we allocated and return a response. */
                icalcomponent_free(ird.cal);
@@ -608,7 +604,7 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
        char uid[SIZ];
        char hold_rm[ROOMNAMELEN];
        long msgnum_being_replaced = 0;
-       struct CtdlMessage *msg;
+       struct CtdlMessage *msg = NULL;
        struct original_event_container oec;
        icalcomponent *original_event;
        char *serialized_event = NULL;
@@ -682,7 +678,7 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
        message_text = malloc(strlen(serialized_event) + SIZ);
        if (message_text != NULL) {
                sprintf(message_text,
-                       "Content-type: text/calendar\r\n\r\n%s\r\n",
+                       "Content-type: text/calendar charset=\"utf-8\"\r\n\r\n%s\r\n",
                        serialized_event
                );
 
@@ -692,6 +688,7 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
                        roomname,
                        0, FMT_RFC822,
                        "",
+                       "",
                        "",             /* no subject */
                        NULL,
                        message_text);
@@ -714,7 +711,7 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
  * passes it up to ical_update_my_calendar_with_reply() for processing.
  */
 void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
-       struct CtdlMessage *msg;
+       struct CtdlMessage *msg = NULL;
        struct ical_respond_data ird;
        int ret;
 
@@ -777,9 +774,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.  (Maybe make this optional?)
+                * anymore.  So delete it.  (Don't do this anymore.)
+               CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
                 */
-               CtdlDeleteMessages(CC->room.QRname, msgnum, "", 1);
 
                /* Free the memory we allocated and return a response. */
                icalcomponent_free(ird.cal);
@@ -878,7 +875,7 @@ int ical_ctdl_is_overlap(
  */
 void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
        icalcomponent *cal;
-       struct CtdlMessage *msg;
+       struct CtdlMessage *msg = NULL;
        struct ical_respond_data ird;
        struct icaltimetype t1start, t1end, t2start, t2end;
        icalproperty *p;
@@ -978,7 +975,8 @@ 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,
+               NULL,
                NULL,
                ical_hunt_for_conflicts_backend,
                (void *) cal
@@ -995,7 +993,7 @@ void ical_hunt_for_conflicts(icalcomponent *cal) {
  * Hunt for conflicts (Phase 1 -- retrieve the object and call Phase 2)
  */
 void ical_conflicts(long msgnum, char *partnum) {
-       struct CtdlMessage *msg;
+       struct CtdlMessage *msg = NULL;
        struct ical_respond_data ird;
 
        msg = CtdlFetchMessage(msgnum, 1);
@@ -1135,7 +1133,7 @@ void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
  */
 void ical_freebusy_backend(long msgnum, void *data) {
        icalcomponent *cal;
-       struct CtdlMessage *msg;
+       struct CtdlMessage *msg = NULL;
        struct ical_respond_data ird;
 
        cal = (icalcomponent *)data;
@@ -1193,7 +1191,7 @@ void ical_freebusy(char *who) {
                        if (recp->num_local == 1) {
                                found_user = getuser(&usbuf, recp->recp_local);
                        }
-                       free(recp);
+                       free_recipients(recp);
                }
        }
 
@@ -1208,7 +1206,7 @@ void ical_freebusy(char *who) {
                        if (recp->num_local == 1) {
                                found_user = getuser(&usbuf, recp->recp_local);
                        }
-                       free(recp);
+                       free_recipients(recp);
                }
        }
 
@@ -1231,7 +1229,7 @@ void ical_freebusy(char *who) {
                                        if (recp->num_local == 1) {
                                                found_user = getuser(&usbuf, recp->recp_local);
                                        }
-                                       free(recp);
+                                       free_recipients(recp);
                                }
                        }
                }
@@ -1283,9 +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",
-               NULL, ical_freebusy_backend, (void *)fb
-       );
+       CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, ical_freebusy_backend, (void *)fb );
 
        /* If values for DTSTART and DTEND are still not present, set them
         * to yesterday and tomorrow as default values.
@@ -1337,8 +1333,8 @@ void ical_freebusy(char *who) {
  * for calendar events and adds them each into one big calendar component.
  */
 void ical_getics_backend(long msgnum, void *data) {
-       icalcomponent *encaps;
-       struct CtdlMessage *msg;
+       icalcomponent *encaps, *c;
+       struct CtdlMessage *msg = NULL;
        struct ical_respond_data ird;
 
        encaps = (icalcomponent *)data;
@@ -1363,12 +1359,27 @@ void ical_getics_backend(long msgnum, void *data) {
 
        /* 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". (FIXME strip out the old encapsulation)
+        * when we free "encaps".
         */
-       icalcomponent_add_component(encaps, ird.cal);
-       //cprintf("%s\n", icalcomponent_as_ical_string(encaps));
-       cprintf("%s\n", icalcomponent_as_ical_string(ird.cal));
-       icalcomponent_free(ird.cal);
+
+       /* 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);
+       }
 }
 
 
@@ -1380,6 +1391,13 @@ void ical_getics_backend(long msgnum, void *data) {
 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) {
@@ -1397,22 +1415,87 @@ void ical_getics(void)
        /* Set the Version Number */
        icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
 
-       /* Set the method to REQUEST */
+       /* Set the method to PUBLISH */
        icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
 
        /* Now go through the room encapsulating all calendar items. */
-       CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
+       CtdlForEachMessage(MSGS_ALL, 0, NULL,
+               NULL,
                NULL,
                ical_getics_backend,
                (void *) encaps
        );
 
-       cprintf("000\n");
+       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.
  */
@@ -1477,6 +1560,11 @@ void cmd_ical(char *argbuf)
                return;
        }
 
+       if (!strcasecmp(subcmd, "putics")) {
+               ical_putics();
+               return;
+       }
+
        cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
 }
 
@@ -1672,6 +1760,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
                        "",                     /* No single recipient here */
                        CC->room.QRname, 0, FMT_RFC822,
                        "",
+                       "",
                        summary_string,         /* Use summary for subject */
                        NULL,
                        request_message_text);
@@ -1680,6 +1769,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
                        valid = validate_recipients(attendees_string);
                        CtdlSubmitMsg(msg, valid, "");
                        CtdlFreeMessage(msg);
+                       free_recipients(valid);
                }
        }
        free(serialized_request);
@@ -1697,6 +1787,8 @@ void ical_saving_vevent(icalcomponent *cal) {
        icalproperty *organizer = NULL;
        char organizer_string[SIZ];
 
+       lprintf(CTDL_DEBUG, "ical_saving_vevent() has been called!\n");
+
        /* Don't send out invitations unless the client wants us to. */
        if (CIT_ICAL->server_generated_invitations == 0) {
                return;
@@ -1713,8 +1805,7 @@ void ical_saving_vevent(icalcomponent *cal) {
         * Send out invitations if, and only if, this user is the Organizer.
         */
        if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
-               organizer = icalcomponent_get_first_property(cal,
-                                               ICAL_ORGANIZER_PROPERTY);
+               organizer = icalcomponent_get_first_property(cal, ICAL_ORGANIZER_PROPERTY);
                if (organizer != NULL) {
                        if (icalproperty_get_organizer(organizer)) {
                                strcpy(organizer_string,
@@ -1756,36 +1847,41 @@ void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
                char *disp, void *content, char *cbtype, char *cbcharset, size_t length,
                char *encoding, void *cbuserdata)
 {
-       icalcomponent *cal, *nested_event, *nested_todo;
+       icalcomponent *cal, *nested_event, *nested_todo, *whole_cal;
        icalproperty *p;
        struct icalmessagemod *imm;
        char new_uid[SIZ];
 
        imm = (struct icalmessagemod *)cbuserdata;
 
-       /* If this is a text/calendar object, hunt for the UID and drop it in
+       /* We're only interested in calendar data. */
+       if (strcasecmp(cbtype, "text/calendar")) {
+               return;
+       }
+
+       /* Hunt for the UID and drop it in
         * the "user data" pointer for the MIME parser.  When
         * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
         * to that string.
         */
-       if (!strcasecmp(cbtype, "text/calendar")) {
-               cal = icalcomponent_new_from_string(content);
-               if (cal != NULL) {
-                       if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
-                               nested_event = icalcomponent_get_first_component(
-                                       cal, ICAL_VEVENT_COMPONENT
-                               );
+       whole_cal = icalcomponent_new_from_string(content);
+       cal = whole_cal;
+       if (cal != NULL) {
+               if (icalcomponent_isa(cal) == ICAL_VCALENDAR_COMPONENT) {
+                       nested_event = icalcomponent_get_first_component(
+                               cal, ICAL_VEVENT_COMPONENT);
+                       if (nested_event != NULL) {
+                               cal = nested_event;
+                       }
+                       else {
                                nested_todo = icalcomponent_get_first_component(
-                                       cal, ICAL_VTODO_COMPONENT
-                               );
-                               if (nested_event != NULL) {
-                                       cal = nested_event;
-                               }
-                               else if (nested_todo != NULL) {
+                                       cal, ICAL_VTODO_COMPONENT);
+                               if (nested_todo != NULL) {
                                        cal = nested_todo;
                                }
                        }
                }
+               
                if (cal != NULL) {
                        p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
                        if (p == NULL) {
@@ -1796,7 +1892,6 @@ 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)); old aethera hack */
                        }
                        p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
                        if (p != NULL) {
@@ -1806,7 +1901,10 @@ void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
                        if (p != NULL) {
                                imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
                        }
-                       icalcomponent_free(cal);
+               }
+               icalcomponent_free(cal);
+               if (whole_cal != cal) {
+                       icalcomponent_free(whole_cal);
                }
        }
 }
@@ -1814,7 +1912,6 @@ void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
 
 
 
-
 /*
  * See if we need to prevent the object from being saved (we don't allow
  * MIME types other than text/calendar in "calendar" or "tasks"  rooms).  Also,
@@ -1828,66 +1925,58 @@ void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
  */
 int ical_obj_beforesave(struct CtdlMessage *msg)
 {
-       char *p;
-       int a;
        struct icalmessagemod imm;
 
        /* First determine if this is a calendar or tasks room */
-       if ( (CC->room.QRdefaultview != VIEW_CALENDAR)
-          &&(CC->room.QRdefaultview != VIEW_TASKS) ) {
+       if (  (CC->room.QRdefaultview != VIEW_CALENDAR)
+          && (CC->room.QRdefaultview != VIEW_TASKS)
+       ) {
                return(0);              /* Not a vCalendar-centric room */
        }
 
        /* It must be an RFC822 message! */
        if (msg->cm_format_type != 4) {
-               return 1;       /* You tried to save a non-RFC822 message! */
+               lprintf(CTDL_DEBUG, "Rejecting non-RFC822 message\n");
+               return(1);              /* You tried to save a non-RFC822 message! */
        }
+
+       if (msg->cm_fields['M'] == NULL) {
+               return(1);              /* You tried to save a null message! */
+       }
+
+       memset(&imm, 0, sizeof(struct icalmessagemod));
        
-       /* Find the Content-Type: header */
-       p = msg->cm_fields['M'];
-       a = strlen(p);
-       while (--a > 0) {
-               if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
-                       if (!strncasecmp(p + 14, "text/calendar", 13)) {
-                               memset(&imm, 0, sizeof(struct icalmessagemod));
-                               mime_parser(msg->cm_fields['M'],
-                                       NULL,
-                                       *ical_ctdl_set_exclusive_msgid,
-                                       NULL, NULL,
-                                       (void *)&imm,
-                                       0
-                               );
-                               if (strlen(imm.uid) > 0) {
-                                       if (msg->cm_fields['E'] != NULL) {
-                                               free(msg->cm_fields['E']);
-                                       }
-                                       msg->cm_fields['E'] = strdup(imm.uid);
-                               }
-                               if (strlen(imm.subject) > 0) {
-                                       if (msg->cm_fields['U'] != NULL) {
-                                               free(msg->cm_fields['U']);
-                                       }
-                                       msg->cm_fields['U'] = strdup(imm.subject);
-                               }
-                               if (imm.dtstart > 0) {
-                                       if (msg->cm_fields['T'] != NULL) {
-                                               free(msg->cm_fields['T']);
-                                       }
-                                       msg->cm_fields['T'] = strdup("000000000000000000");
-                                       sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
-                               }
-                               return 0;
-                       }
-                       else {
-                               return 1;
-                       }
+       /* Do all of our lovely back-end parsing */
+       mime_parser(msg->cm_fields['M'],
+               NULL,
+               *ical_ctdl_set_exclusive_msgid,
+               NULL, NULL,
+               (void *)&imm,
+               0
+       );
+
+       if (strlen(imm.uid) > 0) {
+               if (msg->cm_fields['E'] != NULL) {
+                       free(msg->cm_fields['E']);
                }
-               p++;
+               msg->cm_fields['E'] = strdup(imm.uid);
+               lprintf(CTDL_DEBUG, "Saving calendar UID <%s>\n", msg->cm_fields['E']);
        }
-       
-       /* Oops!  No Content-Type in this message!  How'd that happen? */
-       lprintf(CTDL_ERR, "RFC822 message with no Content-Type header!\n");
-       return 1;
+       if (strlen(imm.subject) > 0) {
+               if (msg->cm_fields['U'] != NULL) {
+                       free(msg->cm_fields['U']);
+               }
+               msg->cm_fields['U'] = strdup(imm.subject);
+       }
+       if (imm.dtstart > 0) {
+               if (msg->cm_fields['T'] != NULL) {
+                       free(msg->cm_fields['T']);
+               }
+               msg->cm_fields['T'] = strdup("000000000000000000");
+               sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
+       }
+
+       return(0);
 }
 
 
@@ -1900,7 +1989,12 @@ void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
 {
        icalcomponent *cal;
 
-       /* If this is a text/calendar object, hunt for the UID and drop it in
+       /* We're only interested in calendar items here. */
+       if (strcasecmp(cbtype, "text/calendar")) {
+               return;
+       }
+
+       /* Hunt for the UID and drop it in
         * the "user data" pointer for the MIME parser.  When
         * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
         * to that string.
@@ -1917,12 +2011,12 @@ void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
 
 /* 
  * Things we need to do after saving a calendar event.
+ * (This will start back end tasks such as automatic generation of invitations,
+ * if such actions are appropriate.)
  */
 int ical_obj_aftersave(struct CtdlMessage *msg)
 {
        char roomname[ROOMNAMELEN];
-       char *p;
-       int a;
 
        /*
         * If this isn't the Calendar> room, no further action is necessary.
@@ -1931,40 +2025,25 @@ int ical_obj_aftersave(struct CtdlMessage *msg)
        /* First determine if this is our room */
        MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
        if (strcasecmp(roomname, CC->room.QRname)) {
-               return 0;       /* It's not the Calendar room. */
+               return(0);      /* Not the Calendar room -- don't do anything. */
        }
 
-       /* 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);
+
+       /* Reject null messages */
+       if (msg->cm_fields['M'] == NULL) return(1);
        
-       /* Find the Content-Type: header */
-       p = msg->cm_fields['M'];
-       a = strlen(p);
-       while (--a > 0) {
-               if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
-                       if (!strncasecmp(p + 14, "text/calendar", 13)) {
-                               mime_parser(msg->cm_fields['M'],
-                                       NULL,
-                                       *ical_obj_aftersave_backend,
-                                       NULL, NULL,
-                                       NULL,
-                                       0
-                               );
-                               return 0;
-                       }
-                       else {
-                               return 1;
-                       }
-               }
-               p++;
-       }
-       
-       /* Oops!  No Content-Type in this message!  How'd that happen? */
-       lprintf(CTDL_ERR, "RFC822 message with no Content-Type header!\n");
-       return 1;
+       /* Now recurse through it looking for our icalendar data */
+       mime_parser(msg->cm_fields['M'],
+               NULL,
+               *ical_obj_aftersave_backend,
+               NULL, NULL,
+               NULL,
+               0
+       );
+
+       return(0);
 }
 
 
@@ -1985,87 +2064,26 @@ void ical_fixed_output_backend(icalcomponent *cal,
                        int recursion_level
 ) {
        icalcomponent *c;
-       icalproperty *method = NULL;
-       icalproperty_method the_method = ICAL_METHOD_NONE;
        icalproperty *p;
-       struct icaltimetype t;
-       time_t tt;
        char buf[256];
 
-       /* Look for a method */
-       method = icalcomponent_get_first_property(cal, ICAL_METHOD_PROPERTY);
-
-       /* See what we need to do with this */
-       if (method != NULL) {
-               the_method = icalproperty_get_method(method);
-               switch(the_method) {
-                   case ICAL_METHOD_REQUEST:
-                       cprintf("Meeting invitation\n");
-                       break;
-                   case ICAL_METHOD_REPLY:
-                       cprintf("Attendee's reply to your invitation\n");
-                       break;
-                   case ICAL_METHOD_PUBLISH:
-                       cprintf("Published event\n");
-                       break;
-                   default:
-                       cprintf("This is an unknown type of calendar item.\n");
-                       break;
-               }
-       }
-
        p = icalcomponent_get_first_property(cal, ICAL_SUMMARY_PROPERTY);
        if (p != NULL) {
-               cprintf("Summary: %s\n", (const char *)icalproperty_get_comment(p));
+               cprintf("%s\n", (const char *)icalproperty_get_comment(p));
        }
 
        p = icalcomponent_get_first_property(cal, ICAL_LOCATION_PROPERTY);
        if (p != NULL) {
-               cprintf("Location: %s\n", (const char *)icalproperty_get_comment(p));
-       }
-
-       /*
-        * Only show start/end times if we're actually looking at the VEVENT
-        * component.  Otherwise it shows bogus dates for things like timezone.
-        */
-       if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
-
-               p = icalcomponent_get_first_property(cal,
-                                               ICAL_DTSTART_PROPERTY);
-               if (p != NULL) {
-                       t = icalproperty_get_dtstart(p);
-
-                       if (t.is_date) {
-                               cprintf("Date: %s %d, %d\n",
-                                       ascmonths[t.month - 1],
-                                       t.day, t.year
-                               );
-                       }
-                       else {
-                               tt = icaltime_as_timet(t);
-                               fmt_date(buf, sizeof buf, tt, 0);
-                               cprintf("Starting date/time: %s\n", buf);
-                       }
-               }
-       
-               p = icalcomponent_get_first_property(cal, ICAL_DTEND_PROPERTY);
-               if (p != NULL) {
-                       t = icalproperty_get_dtend(p);
-                       tt = icaltime_as_timet(t);
-                       fmt_date(buf, sizeof buf, tt, 0);
-                       cprintf("Ending date/time: %s\n", buf);
-               }
-
+               cprintf("%s\n", (const char *)icalproperty_get_comment(p));
        }
 
        p = icalcomponent_get_first_property(cal, ICAL_DESCRIPTION_PROPERTY);
        if (p != NULL) {
-               cprintf("Description: %s\n", (const char *)icalproperty_get_comment(p));
+               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)) {
-               cprintf("Attendee: ");
                safestrncpy(buf, icalproperty_get_attendee(p), sizeof buf);
                if (!strncasecmp(buf, "MAILTO:", 7)) {
 
@@ -2089,7 +2107,7 @@ void ical_fixed_output_backend(icalcomponent *cal,
 
 
 /*
- * Function to output a calendar item  as plain text.  Nobody uses MSG0
+ * 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.
  */
@@ -2103,7 +2121,6 @@ void ical_fixed_output(char *ptr, int len) {
        free(stringy_cal);
 
        if (cal == NULL) {
-               cprintf("There was an error parsing this calendar item.\n");
                return;
        }
 
@@ -2120,7 +2137,7 @@ void ical_fixed_output(char *ptr, int len) {
 /*
  * Register this module with the Citadel server.
  */
-char *serv_calendar_init(void)
+CTDL_MODULE_INIT(calendar)
 {
 #ifdef CITADEL_WITH_CALENDAR_SERVICE
        CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
@@ -2131,11 +2148,16 @@ char *serv_calendar_init(void)
        CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
        CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
 #endif
+
+       /* return our Subversion id for the Log */
        return "$Id$";
 }
 
 
 
-
-
-
+void serv_calendar_destroy(void)
+{
+#ifdef CITADEL_WITH_CALENDAR_SERVICE
+       icaltimezone_free_builtin_timezones();
+#endif
+}