]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_calendar.c
* Do calendar support only if libical header *and* library are present
[citadel.git] / citadel / serv_calendar.c
index 9620ce9752d53cbdab96f8ee16071605cb4d985e..60d6db9471414d114ccef06fed662e84cbf53298 100644 (file)
@@ -7,6 +7,8 @@
  *
  */
 
+#define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
+
 #include "sysdep.h"
 #include <unistd.h>
 #include <sys/types.h>
@@ -16,7 +18,6 @@
 #ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
-#include "serv_calendar.h"
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
 #include "tools.h"
 #include "msgbase.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];
        icalcomponent *cal;
 };
 
+/* Session-local data for calendaring. */
+long SYM_CIT_ICAL;
 
 /*
  * Write a calendar object into the specified user's calendar room.
  */
 void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
-        char temp[PATH_MAX];
-        FILE *fp;
+       char temp[PATH_MAX];
+       FILE *fp;
        char *ser;
 
-        strcpy(temp, tmpnam(NULL));
+       strcpy(temp, tmpnam(NULL));
        ser = icalcomponent_as_ical_string(cal);
        if (ser == NULL) return;
 
        /* Make a temp file out of it */
-        fp = fopen(temp, "w");
-        if (fp == NULL) return;
+       fp = fopen(temp, "w");
+       if (fp == NULL) return;
        fwrite(ser, strlen(ser), 1, fp);
-        fclose(fp);
+       fclose(fp);
 
-        /* This handy API function does all the work for us.
+       /* This handy API function does all the work for us.
         */
-        CtdlWriteObject(USERCALENDARROOM,      /* which room */
+       CtdlWriteObject(USERCALENDARROOM,       /* which room */
                        "text/calendar",        /* MIME type */
                        temp,                   /* temp file */
                        u,                      /* which user */
@@ -69,7 +73,7 @@ void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
                        0,              /* don't delete others of this type */
                        0);                     /* no flags */
 
-        unlink(temp);
+       unlink(temp);
 }
 
 
@@ -268,6 +272,9 @@ void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
        }
        if (strcasecmp(partnum, ird->desired_partnum)) return;
        ird->cal = icalcomponent_new_from_string(content);
+       if (ird->cal != NULL) {
+               ical_dezonify(ird->cal);
+       }
 }
 
 
@@ -346,6 +353,361 @@ void ical_respond(long msgnum, char *partnum, char *action) {
 }
 
 
+/*
+ * Figure out the UID of the calendar event being referred to in a
+ * REPLY object.  This function is recursive.
+ */
+void ical_learn_uid_of_reply(char *uidbuf, icalcomponent *cal) {
+       icalcomponent *subcomponent;
+       icalproperty *p;
+
+       /* If this object is a REPLY, then extract the UID. */
+       if (icalcomponent_isa(cal) == ICAL_VEVENT_COMPONENT) {
+               p = icalcomponent_get_first_property(cal, ICAL_UID_PROPERTY);
+               if (p != NULL) {
+                       strcpy(uidbuf, icalproperty_get_comment(p));
+               }
+       }
+
+       /* Otherwise, recurse through any VEVENT subcomponents.  We do NOT want the
+        * UID of the reply; we want the UID of the invitation being replied to.
+        */
+       for (subcomponent = icalcomponent_get_first_component(cal, ICAL_VEVENT_COMPONENT);
+           subcomponent != NULL;
+           subcomponent = icalcomponent_get_next_component(cal, ICAL_VEVENT_COMPONENT) ) {
+               ical_learn_uid_of_reply(uidbuf, subcomponent);
+       }
+}
+
+
+/*
+ * ical_update_my_calendar_with_reply() refers to this callback function; when we
+ * locate the message containing the calendar event we're replying to, this function
+ * gets called.  It basically just sticks the message number in a supplied buffer.
+ */
+void ical_hunt_for_event_to_update(long msgnum, void *data) {
+       long *msgnumptr;
+
+       msgnumptr = (long *) data;
+       *msgnumptr = msgnum;
+}
+
+
+struct original_event_container {
+       icalcomponent *c;
+};
+
+/*
+ * Callback function for mime parser that hunts for calendar content types
+ * and turns them into calendar objects (called by ical_update_my_calendar_with_reply()
+ * 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 *cbuserdata) {
+
+       struct original_event_container *oec = NULL;
+
+       if (strcasecmp(cbtype, "text/calendar")) {
+               return;
+       }
+       oec = (struct original_event_container *) cbuserdata;
+       if (oec->c != NULL) {
+               icalcomponent_free(oec->c);
+       }
+       oec->c = icalcomponent_new_from_string(content);
+}
+
+
+/*
+ * Merge updated attendee information from a REPLY into an existing event.
+ */
+void ical_merge_attendee_reply(icalcomponent *event, icalcomponent *reply) {
+       icalcomponent *c;
+       icalproperty *e_attendee, *r_attendee;
+
+       /* First things first.  If we're not looking at a VEVENT component,
+        * recurse through subcomponents until we find one.
+        */
+       if (icalcomponent_isa(event) != ICAL_VEVENT_COMPONENT) {
+               for (c = icalcomponent_get_first_component(event, ICAL_VEVENT_COMPONENT);
+                   c != NULL;
+                   c = icalcomponent_get_next_component(event, ICAL_VEVENT_COMPONENT) ) {
+                       ical_merge_attendee_reply(c, reply);
+               }
+               return;
+       }
+
+       /* Now do the same thing with the reply.
+        */
+       if (icalcomponent_isa(reply) != ICAL_VEVENT_COMPONENT) {
+               for (c = icalcomponent_get_first_component(reply, ICAL_VEVENT_COMPONENT);
+                   c != NULL;
+                   c = icalcomponent_get_next_component(reply, ICAL_VEVENT_COMPONENT) ) {
+                       ical_merge_attendee_reply(event, c);
+               }
+               return;
+       }
+
+       /* Clone the reply, because we're going to rip its guts out. */
+       reply = icalcomponent_new_clone(reply);
+
+       /* At this point we're looking at the correct subcomponents.
+        * Iterate through the attendees looking for a match.
+        */
+STARTOVER:
+       for (e_attendee = icalcomponent_get_first_property(event, ICAL_ATTENDEE_PROPERTY);
+           e_attendee != NULL;
+           e_attendee = icalcomponent_get_next_property(event, ICAL_ATTENDEE_PROPERTY)) {
+
+               for (r_attendee = icalcomponent_get_first_property(reply, ICAL_ATTENDEE_PROPERTY);
+                   r_attendee != NULL;
+                   r_attendee = icalcomponent_get_next_property(reply, ICAL_ATTENDEE_PROPERTY)) {
+
+                       /* Check to see if these two attendees match...
+                        */
+                       if (!strcasecmp(
+                          icalproperty_get_attendee(e_attendee),
+                          icalproperty_get_attendee(r_attendee)
+                       )) {
+                               /* ...and if they do, remove the attendee from the event
+                                * and replace it with the attendee from the reply.  (The
+                                * reply's copy will have the same address, but an updated
+                                * status.)
+                                */
+                               TRACE;
+                               icalcomponent_remove_property(event, e_attendee);
+                               TRACE;
+                               icalproperty_free(e_attendee);
+                               TRACE;
+                               icalcomponent_remove_property(reply, r_attendee);
+                               TRACE;
+                               icalcomponent_add_property(event, r_attendee);
+                               TRACE;
+
+                               /* Since we diddled both sets of attendees, we have to start
+                                * the iteration over again.  This will not create an infinite
+                                * loop because we removed the attendee from the reply.  (That's
+                                * why we cloned the reply, and that's what we mean by "ripping
+                                * its guts out.")
+                                */
+                               goto STARTOVER;
+                       }
+       
+               }
+       }
+
+       /* Free the *clone* of the reply. */
+       icalcomponent_free(reply);
+}
+
+
+
+
+/*
+ * Handle an incoming RSVP (object with method==ICAL_METHOD_REPLY) for a
+ * calendar event.  The object has already been deserialized for us; all
+ * we have to do here is hunt for the event in our calendar, merge in the
+ * updated attendee status, and save it again.
+ *
+ * This function returns 0 on success, 1 if the event was not found in the
+ * user's calendar, or 2 if an internal error occurred.
+ */
+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;
+       char *serialized_event = NULL;
+       char roomname[ROOMNAMELEN];
+       char *message_text = NULL;
+
+       /* Figure out just what event it is we're dealing with */
+       strcpy(uid, "--==<< InVaLiD uId >>==--");
+       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 */
+
+       if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
+               getroom(&CC->quickroom, hold_rm);
+               lprintf(3, "cannot get user calendar room\n");
+               return(2);
+       }
+
+       /*
+        * Pound through the user's calendar looking 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 *)
+               mallok(sizeof(struct CtdlMessage));
+       memset(template, 0, sizeof(struct CtdlMessage));
+       template->cm_fields['E'] = strdoop(uid);
+       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 */
+
+       lprintf(9, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
+       if (msgnum_being_replaced == 0) {
+               return(1);                      /* no calendar event found */
+       }
+
+       /* Now we know the ID of the message containing the event being updated.
+        * We don't actually have to delete it; that'll get taken care of by the
+        * server when we save another event with the same UID.  This just gives
+        * us the ability to load the event into memory so we can diddle the
+        * attendees.
+        */
+       msg = CtdlFetchMessage(msgnum_being_replaced);
+       if (msg == NULL) {
+               return(2);                      /* internal error */
+       }
+       oec.c = NULL;
+       mime_parser(msg->cm_fields['M'],
+               NULL,
+               *ical_locate_original_event,    /* callback function */
+               NULL, NULL,
+               &oec,                           /* user data */
+               0
+       );
+       CtdlFreeMessage(msg);
+
+       original_event = oec.c;
+       if (original_event == NULL) {
+               lprintf(3, "ERROR: Original_component is NULL.\n");
+               return(2);
+       }
+
+       /* Merge the attendee's updated status into the event */
+       ical_merge_attendee_reply(original_event, cal);
+
+       /* Serialize it */
+       serialized_event = strdoop(icalcomponent_as_ical_string(original_event));
+       icalcomponent_free(original_event);     /* Don't need this anymore. */
+       if (serialized_event == NULL) return(2);
+
+       MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
+
+       message_text = mallok(strlen(serialized_event) + SIZ);
+       if (message_text != NULL) {
+               sprintf(message_text,
+                       "Content-type: text/calendar\r\n\r\n%s\r\n",
+                       serialized_event
+               );
+
+               msg = CtdlMakeMessage(&CC->usersupp,
+                       "",                     /* No recipient */
+                       roomname,
+                       0, FMT_RFC822,
+                       "",
+                       "",             /* no subject */
+                       message_text);
+       
+               if (msg != NULL) {
+                       CIT_ICAL->avoid_sending_invitations = 1;
+                       CtdlSubmitMsg(msg, NULL, roomname);
+                       CtdlFreeMessage(msg);
+                       CIT_ICAL->avoid_sending_invitations = 0;
+               }
+       }
+       phree(serialized_event);
+       return(0);
+}
+
+
+/*
+ * Handle an incoming RSVP for an event.  (This is the server subcommand part; it
+ * simply extracts the calendar object from the message, deserializes it, and
+ * 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 ical_respond_data ird;
+       int ret;
+
+       if (
+          (strcasecmp(action, "update"))
+          && (strcasecmp(action, "ignore"))
+       ) {
+               cprintf("%d Action must be 'update' or 'ignore'\n",
+                       ERROR + ILLEGAL_VALUE
+               );
+               return;
+       }
+
+       msg = CtdlFetchMessage(msgnum);
+       if (msg == NULL) {
+               cprintf("%d Message %ld not found.\n",
+                       ERROR+ILLEGAL_VALUE,
+                       (long)msgnum
+               );
+               return;
+       }
+
+       memset(&ird, 0, sizeof ird);
+       strcpy(ird.desired_partnum, partnum);
+       mime_parser(msg->cm_fields['M'],
+               NULL,
+               *ical_locate_part,              /* callback function */
+               NULL, NULL,
+               (void *) &ird,                  /* user data */
+               0
+       );
+
+       /* We're done with the incoming message, because we now have a
+        * calendar object in memory.
+        */
+       CtdlFreeMessage(msg);
+
+       /*
+        * Here is the real meat of this function.  Handle the event.
+        */
+       if (ird.cal != NULL) {
+               /* Update the user's calendar if necessary */
+               if (!strcasecmp(action, "update")) {
+                       ret = ical_update_my_calendar_with_reply(ird.cal);
+                       if (ret == 0) {
+                               cprintf("%d Your calendar has been updated with this reply.\n",
+                                       CIT_OK);
+                       }
+                       else if (ret == 1) {
+                               cprintf("%d This event does not exist in your calendar.\n",
+                                       ERROR + FILE_NOT_FOUND);
+                       }
+                       else {
+                               cprintf("%d An internal error occurred.\n",
+                                       ERROR + INTERNAL_ERROR);
+                       }
+               }
+               else {
+                       cprintf("%d This reply has been ignored.\n", CIT_OK);
+               }
+
+               /* Now that we've processed this message, we don't need it
+                * anymore.  So delete it.  FIXME uncomment this when ready!
+                */
+               CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
+
+               /* Free the memory we allocated and return a response. */
+               icalcomponent_free(ird.cal);
+               ird.cal = NULL;
+               return;
+       }
+       else {
+               cprintf("%d No calendar object found\n", ERROR);
+               return;
+       }
+
+       /* should never get here */
+}
+
+
 /*
  * Search for a property in both the top level and in a VEVENT subcomponent
  */
@@ -516,26 +878,26 @@ void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
  * with this one.
  */
 void ical_hunt_for_conflicts(icalcomponent *cal) {
-        char hold_rm[ROOMNAMELEN];
+       char hold_rm[ROOMNAMELEN];
 
-        strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
+       strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
 
-        if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
-                getroom(&CC->quickroom, hold_rm);
+       if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
+               getroom(&CC->quickroom, hold_rm);
                cprintf("%d You do not have a calendar.\n", ERROR);
                return;
-        }
+       }
 
        cprintf("%d Conflicting events:\n", LISTING_FOLLOWS);
 
-        CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
+       CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
                NULL,
                ical_hunt_for_conflicts_backend,
                (void *) cal
        );
 
        cprintf("000\n");
-        getroom(&CC->quickroom, hold_rm);      /* return to saved room */
+       getroom(&CC->quickroom, hold_rm);       /* return to saved room */
 
 }
 
@@ -611,6 +973,13 @@ void cmd_ical(char *argbuf)
                ical_respond(msgnum, partnum, action);
        }
 
+       else if (!strcmp(subcmd, "handle_rsvp")) {
+               msgnum = extract_long(argbuf, 1);
+               extract(partnum, argbuf, 2);
+               extract(action, argbuf, 3);
+               ical_handle_rsvp(msgnum, partnum, action);
+       }
+
        else if (!strcmp(subcmd, "conflicts")) {
                msgnum = extract_long(argbuf, 1);
                extract(partnum, argbuf, 2);
@@ -676,8 +1045,123 @@ void ical_create_room(void)
  * finds a VEVENT.
  */
 void ical_send_out_invitations(icalcomponent *cal) {
-       lprintf(9, "ORGANIZER IS ME!  Sending out invitations!\n");
-       /** FIXME ** Now go and implement this. **/
+       icalcomponent *the_request = NULL;
+       char *serialized_request = NULL;
+       char *request_message_text = NULL;
+       struct CtdlMessage *msg = NULL;
+       struct recptypes *valid = NULL;
+       char attendees_string[SIZ];
+       int num_attendees = 0;
+       char this_attendee[SIZ];
+       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;
+       }
+
+       /* Clone the event */
+       the_request = icalcomponent_new_clone(cal);
+       if (the_request == NULL) {
+               lprintf(3, "ERROR: cannot clone calendar object\n");
+               return;
+       }
+
+       /* Extract the summary string -- we'll use it as the
+        * message subject for the request
+        */
+       strcpy(summary_string, "Meeting request");
+       summary = icalcomponent_get_first_property(the_request, ICAL_SUMMARY_PROPERTY);
+       if (summary != NULL) {
+               if (icalproperty_get_summary(summary)) {
+                       strcpy(summary_string,
+                               icalproperty_get_summary(summary) );
+               }
+       }
+
+       /* Determine who the recipients of this message are (the attendees) */
+       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) );
+                       if (!strncasecmp(this_attendee, "MAILTO:", 7)) {
+                               strcpy(this_attendee, &this_attendee[7]);
+                               snprintf(&attendees_string[strlen(attendees_string)],
+                                       sizeof(attendees_string) - strlen(attendees_string),
+                                       "%s, ",
+                                       this_attendee
+                               );
+                               ++num_attendees;
+                       }
+               }
+       }
+
+       lprintf(9, "<%d> attendees: <%s>\n", num_attendees, attendees_string);
+
+       /* If there are no attendees, there are no invitations to send, so...
+        * don't bother putting one together!  Punch out, Maverick!
+        */
+       if (num_attendees == 0) {
+               icalcomponent_free(the_request);
+               return;
+       }
+
+       /* Encapsulate the VEVENT component into a complete VCALENDAR */
+       encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
+       if (encaps == NULL) {
+               lprintf(3, "Error at %s:%d - could not allocate component!\n",
+                       __FILE__, __LINE__);
+               icalcomponent_free(the_request);
+               return;
+       }
+
+       /* 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_REQUEST);
+
+       /* 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
+        * when we free "encaps".
+        */
+       icalcomponent_add_component(encaps, the_request);
+
+       /* Serialize it */
+       serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
+       icalcomponent_free(encaps);     /* Don't need this anymore. */
+       if (serialized_request == NULL) return;
+
+       request_message_text = mallok(strlen(serialized_request) + SIZ);
+       if (request_message_text != NULL) {
+               sprintf(request_message_text,
+                       "Content-type: text/calendar\r\n\r\n%s\r\n",
+                       serialized_request
+               );
+
+               msg = CtdlMakeMessage(&CC->usersupp,
+                       "",                     /* No single recipient here */
+                       CC->quickroom.QRname, 0, FMT_RFC822,
+                       "",
+                       summary_string,         /* Use summary for subject */
+                       request_message_text);
+       
+               if (msg != NULL) {
+                       valid = validate_recipients(attendees_string);
+                       CtdlSubmitMsg(msg, valid, "");
+                       CtdlFreeMessage(msg);
+               }
+       }
+       phree(serialized_request);
 }
 
 
@@ -692,6 +1176,13 @@ void ical_saving_vevent(icalcomponent *cal) {
        icalproperty *organizer = NULL;
        char organizer_string[SIZ];
 
+       /* Don't send out invitations if we've been asked not to. */
+       lprintf(9, "CIT_ICAL->avoid_sending_invitations = %d\n",
+               CIT_ICAL->avoid_sending_invitations);
+       if (CIT_ICAL->avoid_sending_invitations > 0) {
+               return;
+       }
+
        strcpy(organizer_string, "");
        /*
         * The VEVENT subcomponent is the one we're interested in.
@@ -733,7 +1224,9 @@ void ical_saving_vevent(icalcomponent *cal) {
 
 /*
  * Back end for ical_obj_beforesave()
- * This hunts for the UID of the calendar event.
+ * This hunts for the UID of the calendar event (becomes Citadel msg EUID),
+ * the summary of the event (becomes message subject),
+ * and the start time (becomes message date/time).
  */
 void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
                char *disp, void *content, char *cbtype, size_t length,
@@ -741,6 +1234,9 @@ void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
 {
        icalcomponent *cal;
        icalproperty *p;
+       struct icalmessagemod *imm;
+
+       imm = (struct icalmessagemod *)cbuserdata;
 
        /* If this is a text/calendar object, hunt for the UID and drop it in
         * the "user data" pointer for the MIME parser.  When
@@ -752,11 +1248,17 @@ void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
                if (cal != NULL) {
                        p = ical_ctdl_get_subprop(cal, ICAL_UID_PROPERTY);
                        if (p != NULL) {
-                               strcpy((char *)cbuserdata,
-                                       icalproperty_get_comment(p)
-                               );
+                               strcpy(imm->uid, icalproperty_get_comment(p));
+                       }
+                       p = ical_ctdl_get_subprop(cal, ICAL_SUMMARY_PROPERTY);
+                       if (p != NULL) {
+                               strcpy(imm->subject,
+                                               icalproperty_get_comment(p));
+                       }
+                       p = ical_ctdl_get_subprop(cal, ICAL_DTSTART_PROPERTY);
+                       if (p != NULL) {
+                               imm->dtstart = icaltime_as_timet(icalproperty_get_dtstart(p));
                        }
-                       ical_saving_vevent(cal);
                        icalcomponent_free(cal);
                }
        }
@@ -773,13 +1275,16 @@ void ical_ctdl_set_extended_msgid(char *name, char *filename, char *partnum,
  * ID to the UID of the object.  This causes our replication checker to
  * automatically delete any existing instances of the same object.  (Isn't
  * that cool?)
+ *
+ * We also set the message's Subject to the event summary, and the Date/time to
+ * the event start time.
  */
 int ical_obj_beforesave(struct CtdlMessage *msg)
 {
        char roomname[ROOMNAMELEN];
        char *p;
        int a;
-       char eidbuf[SIZ];
+       struct icalmessagemod imm;
 
        /*
         * Only messages with content-type text/calendar
@@ -807,19 +1312,32 @@ int ical_obj_beforesave(struct CtdlMessage *msg)
        while (--a > 0) {
                if (!strncasecmp(p, "Content-Type: ", 14)) {    /* Found it */
                        if (!strncasecmp(p + 14, "text/calendar", 13)) {
-                               strcpy(eidbuf, "");
+                               memset(&imm, 0, sizeof(struct icalmessagemod));
                                mime_parser(msg->cm_fields['M'],
                                        NULL,
                                        *ical_ctdl_set_extended_msgid,
                                        NULL, NULL,
-                                       (void *)eidbuf,
+                                       (void *)&imm,
                                        0
                                );
-                               if (strlen(eidbuf) > 0) {
+                               if (strlen(imm.uid) > 0) {
                                        if (msg->cm_fields['E'] != NULL) {
                                                phree(msg->cm_fields['E']);
                                        }
-                                       msg->cm_fields['E'] = strdoop(eidbuf);
+                                       msg->cm_fields['E'] = strdoop(imm.uid);
+                               }
+                               if (strlen(imm.subject) > 0) {
+                                       if (msg->cm_fields['U'] != NULL) {
+                                               phree(msg->cm_fields['U']);
+                                       }
+                                       msg->cm_fields['U'] = strdoop(imm.subject);
+                               }
+                               if (imm.dtstart > 0) {
+                                       if (msg->cm_fields['T'] != NULL) {
+                                               phree(msg->cm_fields['T']);
+                                       }
+                                       msg->cm_fields['T'] = strdoop("000000000000000000");
+                                       sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
                                }
                                return 0;
                        }
@@ -836,17 +1354,104 @@ int ical_obj_beforesave(struct CtdlMessage *msg)
 }
 
 
-#endif /* HAVE_ICAL_H */
+/*
+ * 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 *encoding, void *cbuserdata)
+{
+       icalcomponent *cal;
+
+       /* If this is a text/calendar object, 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 Extended msgid
+        * to that string.
+        */
+       if (!strcasecmp(cbtype, "text/calendar")) {
+               cal = icalcomponent_new_from_string(content);
+               if (cal != NULL) {
+                       ical_saving_vevent(cal);
+                       icalcomponent_free(cal);
+               }
+       }
+}
+
+
+/* 
+ * Things we need to do after saving a calendar event.
+ */
+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.
+        */
+
+       /* First determine if this is our room */
+       MailboxName(roomname, sizeof roomname, &CC->usersupp, USERCALENDARROOM);
+       if (strcasecmp(roomname, CC->quickroom.QRname)) {
+               return 0;       /* It's not the Calendar room. */
+       }
+
+       /* 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 */
+       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(7, "RFC822 message with no Content-Type header!\n");
+       return 1;
+}
+
+
+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 /* CITADEL_WITH_CALENDAR_SERVICE */
 
 /*
  * Register this module with the Citadel server.
  */
 char *Dynamic_Module_Init(void)
 {
-#ifdef HAVE_ICAL_H
+#ifdef CITADEL_WITH_CALENDAR_SERVICE
        CtdlRegisterMessageHook(ical_obj_beforesave, EVT_BEFORESAVE);
+       CtdlRegisterMessageHook(ical_obj_aftersave, EVT_AFTERSAVE);
        CtdlRegisterSessionHook(ical_create_room, EVT_LOGIN);
        CtdlRegisterProtoHook(cmd_ical, "ICAL", "Citadel iCal commands");
+       CtdlRegisterSessionHook(ical_session_startup, EVT_START);
 #endif
        return "$Id$";
 }