]> code.citadel.org Git - citadel.git/blobdiff - citadel/serv_calendar.c
* Replaced all "Citadel/UX" references with "Citadel"
[citadel.git] / citadel / serv_calendar.c
index 84c99721b0c8f6ca3645527c887b51d3cef6a9d0..e60cba927ba4cd319685e3b937a4d38c64455630 100644 (file)
@@ -2,7 +2,7 @@
  * $Id$ 
  *
  * This module implements iCalendar object processing and the Calendar>
- * room on a Citadel/UX server.  It handles iCalendar objects using the
+ * room on a Citadel server.  It handles iCalendar objects using the
  * iTIP protocol.  See RFCs 2445 and 2446.
  *
  */
@@ -10,6 +10,7 @@
 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
 
 #include "sysdep.h"
+#include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
 #include <limits.h>
@@ -30,6 +31,7 @@
 #include "tools.h"
 #include "msgbase.h"
 #include "mime_parser.h"
+#include "internet_addressing.h"
 #include "serv_calendar.h"
 
 #ifdef CITADEL_WITH_CALENDAR_SERVICE
@@ -42,8 +44,29 @@ struct ical_respond_data {
        icalcomponent *cal;
 };
 
-/* 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(CTDL_CRIT, "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);
+}
 
 
 /*
@@ -60,18 +83,8 @@ icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
        }
 
        /* 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__);
-               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"));
+       encaps = icalcomponent_new_citadel_vcalendar();
+       if (encaps == NULL) return NULL;
 
        /* Encapsulate the subcomponent inside */
        icalcomponent_add_component(encaps, subcomp);
@@ -93,7 +106,7 @@ icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
  * 
  * ok
  */
-void ical_write_to_cal(struct usersupp *u, icalcomponent *cal) {
+void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
        char temp[PATH_MAX];
        FILE *fp;
        char *ser;
@@ -150,7 +163,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);
 
        }
 
@@ -198,13 +211,13 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
        strcpy(summary_string, "Calendar item");
 
        if (request == NULL) {
-               lprintf(3, "ERROR: trying to reply to NULL event?\n");
+               lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
                return;
        }
 
        the_reply = icalcomponent_new_clone(request);
        if (the_reply == NULL) {
-               lprintf(3, "ERROR: cannot clone request\n");
+               lprintf(CTDL_ERR, "ERROR: cannot clone request\n");
                return;
        }
 
@@ -228,11 +241,11 @@ 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);
                                                }
-                                               phree(recp);
+                                               free(recp);
                                        }
                                }
                        }
@@ -291,19 +304,19 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
        }
 
        /* Now generate the reply message and send it out. */
-       serialized_reply = strdoop(icalcomponent_as_ical_string(the_reply));
+       serialized_reply = strdup(icalcomponent_as_ical_string(the_reply));
        icalcomponent_free(the_reply);  /* don't need this anymore */
        if (serialized_reply == NULL) return;
 
-       reply_message_text = mallok(strlen(serialized_reply) + SIZ);
+       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",
                        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);
@@ -314,7 +327,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                        CtdlFreeMessage(msg);
                }
        }
-       phree(serialized_reply);
+       free(serialized_reply);
 }
 
 
@@ -330,11 +343,26 @@ 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);
@@ -359,10 +387,10 @@ void ical_respond(long msgnum, char *partnum, char *action) {
                return;
        }
 
-       msg = CtdlFetchMessage(msgnum);
+       msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) {
                cprintf("%d Message %ld not found.\n",
-                       ERROR+ILLEGAL_VALUE,
+                       ERROR + ILLEGAL_VALUE,
                        (long)msgnum
                );
                return;
@@ -400,7 +428,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);
@@ -409,7 +437,7 @@ void ical_respond(long msgnum, char *partnum, char *action) {
                return;
        }
        else {
-               cprintf("%d No calendar object found\n", ERROR);
+               cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
                return;
        }
 
@@ -592,13 +620,13 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
        /* 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);
+       lprintf(CTDL_DEBUG, "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);
-               lprintf(3, "cannot get user calendar room\n");
+       if (getroom(&CC->room, USERCALENDARROOM) != 0) {
+               getroom(&CC->room, hold_rm);
+               lprintf(CTDL_CRIT, "cannot get user calendar room\n");
                return(2);
        }
 
@@ -609,15 +637,15 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
         * the event, this will work.
         */
        template = (struct CtdlMessage *)
-               mallok(sizeof(struct CtdlMessage));
+               malloc(sizeof(struct CtdlMessage));
        memset(template, 0, sizeof(struct CtdlMessage));
-       template->cm_fields['E'] = strdoop(uid);
+       template->cm_fields['E'] = strdup(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 */
+       getroom(&CC->room, hold_rm);    /* return to saved room */
 
-       lprintf(9, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
+       lprintf(CTDL_DEBUG, "msgnum_being_replaced == %ld\n", msgnum_being_replaced);
        if (msgnum_being_replaced == 0) {
                return(1);                      /* no calendar event found */
        }
@@ -628,7 +656,7 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
         * us the ability to load the event into memory so we can diddle the
         * attendees.
         */
-       msg = CtdlFetchMessage(msgnum_being_replaced);
+       msg = CtdlFetchMessage(msgnum_being_replaced, 1);
        if (msg == NULL) {
                return(2);                      /* internal error */
        }
@@ -644,7 +672,7 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
 
        original_event = oec.c;
        if (original_event == NULL) {
-               lprintf(3, "ERROR: Original_component is NULL.\n");
+               lprintf(CTDL_ERR, "ERROR: Original_component is NULL.\n");
                return(2);
        }
 
@@ -652,20 +680,20 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
        ical_merge_attendee_reply(original_event, cal);
 
        /* Serialize it */
-       serialized_event = strdoop(icalcomponent_as_ical_string(original_event));
+       serialized_event = strdup(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);
+       MailboxName(roomname, sizeof roomname, &CC->user, USERCALENDARROOM);
 
-       message_text = mallok(strlen(serialized_event) + SIZ);
+       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",
                        serialized_event
                );
 
-               msg = CtdlMakeMessage(&CC->usersupp,
+               msg = CtdlMakeMessage(&CC->user,
                        "",                     /* No recipient */
                        roomname,
                        0, FMT_RFC822,
@@ -680,7 +708,7 @@ int ical_update_my_calendar_with_reply(icalcomponent *cal) {
                        CIT_ICAL->avoid_sending_invitations = 0;
                }
        }
-       phree(serialized_event);
+       free(serialized_event);
        return(0);
 }
 
@@ -705,10 +733,10 @@ void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
                return;
        }
 
-       msg = CtdlFetchMessage(msgnum);
+       msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) {
                cprintf("%d Message %ld not found.\n",
-                       ERROR+ILLEGAL_VALUE,
+                       ERROR + ILLEGAL_VALUE,
                        (long)msgnum
                );
                return;
@@ -754,9 +782,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);
@@ -764,7 +792,7 @@ void ical_handle_rsvp(long msgnum, char *partnum, char *action) {
                return;
        }
        else {
-               cprintf("%d No calendar object found\n", ERROR);
+               cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
                return;
        }
 
@@ -796,6 +824,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,
@@ -866,10 +896,10 @@ void ical_hunt_for_conflicts_backend(long msgnum, void *data) {
        strcpy(conflict_event_uid, "");
        strcpy(conflict_event_summary, "");
 
-       msg = CtdlFetchMessage(msgnum);
+       msg = CtdlFetchMessage(msgnum, 1);
        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 */
@@ -916,7 +946,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)) {
@@ -944,11 +973,11 @@ 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);
-               cprintf("%d You do not have a calendar.\n", ERROR);
+       if (getroom(&CC->room, USERCALENDARROOM) != 0) {
+               getroom(&CC->room, hold_rm);
+               cprintf("%d You do not have a calendar.\n", ERROR + ROOM_NOT_FOUND);
                return;
        }
 
@@ -961,7 +990,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 */
 
 }
 
@@ -974,10 +1003,10 @@ void ical_conflicts(long msgnum, char *partnum) {
        struct CtdlMessage *msg;
        struct ical_respond_data ird;
 
-       msg = CtdlFetchMessage(msgnum);
+       msg = CtdlFetchMessage(msgnum, 1);
        if (msg == NULL) {
                cprintf("%d Message %ld not found.\n",
-                       ERROR+ILLEGAL_VALUE,
+                       ERROR + ILLEGAL_VALUE,
                        (long)msgnum
                );
                return;
@@ -1001,7 +1030,7 @@ void ical_conflicts(long msgnum, char *partnum) {
                return;
        }
        else {
-               cprintf("%d No calendar object found\n", ERROR);
+               cprintf("%d No calendar object found\n", ERROR + ROOM_NOT_FOUND);
                return;
        }
 
@@ -1011,18 +1040,18 @@ void ical_conflicts(long msgnum, char *partnum) {
 
 
 /*
- * Remove all properties from a VEVENT that are not supplying the
- * bare minimum for free/busy data.
+ * Look for busy time in a VEVENT and add it to the supplied VFREEBUSY.
  */
-void ical_freebusy_strip(icalcomponent *cal) {
-
+void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
        icalproperty *p;
-       int did_something = 1;
+       icalvalue *v;
+       struct icalperiodtype my_period;
 
        if (cal == NULL) return;
+       my_period = icalperiodtype_null_period();
 
        if (icalcomponent_isa(cal) != ICAL_VEVENT_COMPONENT) {
-               ical_freebusy_strip(
+               ical_add_to_freebusy(fb,
                        icalcomponent_get_first_component(
                                cal, ICAL_VEVENT_COMPONENT
                        )
@@ -1032,31 +1061,68 @@ void ical_freebusy_strip(icalcomponent *cal) {
 
        ical_dezonify(cal);
 
-       while (did_something) {
-               did_something = 0;
-               for (   p=icalcomponent_get_first_property
-                               (cal, ICAL_ANY_PROPERTY);
-                       p != NULL;
-                       p = icalcomponent_get_next_property
-                               (cal, ICAL_ANY_PROPERTY)
-               ) {
-
-                       if (
-                               (icalproperty_isa(p)==ICAL_DTSTART_PROPERTY)
-                          ||   (icalproperty_isa(p)==ICAL_DTEND_PROPERTY)
-                          ||   (icalproperty_isa(p)==ICAL_DURATION_PROPERTY)
-                          ||   (icalproperty_isa(p)==ICAL_FREEBUSY_PROPERTY)
-                          ||   (icalproperty_isa(p)==ICAL_TRANSP_PROPERTY)
-                          ) {
-                               /* keep it */
-                       }
-                       else {
-                               /* delete it */
-                               icalcomponent_remove_property(cal, p);
-                               icalproperty_free(p);
-                               did_something = 1;
+       /* 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)
+       );
+
+       /* Make sure the DTSTART property of the freebusy *list* is set to
+        * the DTSTART property of the *earliest event*.
+        */
+       p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
+       if (p == NULL) {
+               icalcomponent_set_dtstart(fb,
+                       icalcomponent_get_dtstart(cal) );
+       }
+       else {
+               if (icaltime_compare(
+                       icalcomponent_get_dtstart(cal),
+                       icalcomponent_get_dtstart(fb)
+                  ) < 0) {
+                       icalcomponent_set_dtstart(fb,
+                               icalcomponent_get_dtstart(cal) );
+               }
+       }
+
+       /* Make sure the DTEND property of the freebusy *list* is set to
+        * the DTEND property of the *latest event*.
+        */
+       p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
+       if (p == NULL) {
+               icalcomponent_set_dtend(fb,
+                       icalcomponent_get_dtend(cal) );
+       }
+       else {
+               if (icaltime_compare(
+                       icalcomponent_get_dtend(cal),
+                       icalcomponent_get_dtend(fb)
+                  ) > 0) {
+                       icalcomponent_set_dtend(fb,
+                               icalcomponent_get_dtend(cal) );
                }
        }
 
@@ -1079,10 +1145,10 @@ void ical_freebusy_backend(long msgnum, void *data) {
 
        cal = (icalcomponent *)data;
 
-       msg = CtdlFetchMessage(msgnum);
+       msg = CtdlFetchMessage(msgnum, 1);
        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 */
@@ -1094,13 +1160,10 @@ void ical_freebusy_backend(long msgnum, void *data) {
 
        if (ird.cal == NULL) return;
 
-       /* Strip it!  Strip it good! */
-       ical_freebusy_strip(ird.cal);
+       ical_add_to_freebusy(cal, ird.cal);
 
-       /* Encapsulate ird.cal inside cal (thereby also transferring
-        * ownership of the memory it consumes).
-        */
-       icalcomponent_add_component(cal, ird.cal);
+       /* Now free the memory. */
+       icalcomponent_free(ird.cal);
 }
 
 
@@ -1109,13 +1172,77 @@ void ical_freebusy_backend(long msgnum, void *data) {
  * Grab another user's free/busy times
  */
 void ical_freebusy(char *who) {
-       struct usersupp usbuf;
+       struct ctdluser usbuf;
        char calendar_room_name[ROOMNAMELEN];
        char hold_rm[ROOMNAMELEN];
        char *serialized_request = NULL;
        icalcomponent *encaps = NULL;
+       icalcomponent *fb = NULL;
+       int found_user = (-1);
+       struct recptypes *recp = NULL;
+       char buf[SIZ];
+       char host[SIZ];
+       char type[SIZ];
+       int i = 0;
+       int config_lines = 0;
+
+       /* First try an exact match. */
+       found_user = getuser(&usbuf, who);
+
+       /* If not found, try it as an unqualified email address. */
+       if (found_user != 0) {
+               strcpy(buf, who);
+               recp = validate_recipients(buf);
+               lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
+               if (recp != NULL) {
+                       if (recp->num_local == 1) {
+                               found_user = getuser(&usbuf, recp->recp_local);
+                       }
+                       free(recp);
+               }
+       }
+
+       /* If still not found, try it as an address qualified with the
+        * primary FQDN of this Citadel node.
+        */
+       if (found_user != 0) {
+               snprintf(buf, sizeof buf, "%s@%s", who, config.c_fqdn);
+               lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
+               recp = validate_recipients(buf);
+               if (recp != NULL) {
+                       if (recp->num_local == 1) {
+                               found_user = getuser(&usbuf, recp->recp_local);
+                       }
+                       free(recp);
+               }
+       }
+
+       /* Still not found?  Try qualifying it with every domain we
+        * might have addresses in.
+        */
+       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, '|');
+
+                       if ( (!strcasecmp(type, "localhost"))
+                          || (!strcasecmp(type, "directory")) ) {
+                               snprintf(buf, sizeof buf, "%s@%s", who, host);
+                               lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
+                               recp = validate_recipients(buf);
+                               if (recp != NULL) {
+                                       if (recp->num_local == 1) {
+                                               found_user = getuser(&usbuf, recp->recp_local);
+                                       }
+                                       free(recp);
+                               }
+                       }
+               }
+       }
 
-       if (getuser(&usbuf, who) != 0) {
+       if (found_user != 0) {
                cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
                return;
        }
@@ -1123,51 +1250,87 @@ void ical_freebusy(char *who) {
        MailboxName(calendar_room_name, sizeof calendar_room_name,
                &usbuf, USERCALENDARROOM);
 
-       strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
+       strcpy(hold_rm, CC->room.QRname);       /* save current room */
 
-       if (getroom(&CC->quickroom, USERCALENDARROOM) != 0) {
-               cprintf("%d Cannot open calendar\n", ERROR+ROOM_NOT_FOUND);
-               getroom(&CC->quickroom, hold_rm);
+       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 VCALENDAR in which we will encapsulate all the VEVENTs */
-       encaps = icalcomponent_new(ICAL_VCALENDAR_COMPONENT);
-       if (encaps == NULL) {
+       /* Create a VFREEBUSY subcomponent */
+       lprintf(CTDL_DEBUG, "Creating VFREEBUSY component\n");
+       fb = icalcomponent_new_vfreebusy();
+       if (fb == NULL) {
                cprintf("%d Internal error: cannot allocate memory.\n",
-                       ERROR+INTERNAL_ERROR);
-               getroom(&CC->quickroom, hold_rm);
+                       ERROR + INTERNAL_ERROR);
+               icalcomponent_free(encaps);
+               getroom(&CC->room, hold_rm);
                return;
        }
 
-       /* Set the Product ID */
-       icalcomponent_add_property(encaps, icalproperty_new_prodid(PRODID));
+       /* Set the method to PUBLISH */
+       icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
 
-       /* Set the Version Number */
-       icalcomponent_add_property(encaps, icalproperty_new_version("2.0"));
+       /* Set the DTSTAMP to right now. */
+       icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
 
-       /* Set the method to ???? FIXME
-       icalcomponent_set_method(encaps, ICAL_METHOD_REQUEST); */
+       /* Add the user's email address as ORGANIZER */
+       sprintf(buf, "MAILTO:%s", who);
+       if (strchr(buf, '@') == NULL) {
+               strcat(buf, "@");
+               strcat(buf, config.c_fqdn);
+       }
+       for (i=0; i<strlen(buf); ++i) {
+               if (buf[i]==' ') buf[i] = '_';
+       }
+       icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
 
+       /* 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 *)encaps
+               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.
+        */
+       if (icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY) == NULL) {
+               icalcomponent_set_dtstart(fb, icaltime_from_timet(time(NULL)-86400L, 0));
+       }
+       if (icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY) == NULL) {
+               icalcomponent_set_dtend(fb, icaltime_from_timet(time(NULL)+86400L, 0));
+       }
+
+       /* Put the freebusy component into the calendar component */
+       lprintf(CTDL_DEBUG, "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(CTDL_DEBUG, "Setting method\n");
+       icalcomponent_set_method(encaps, ICAL_METHOD_PUBLISH);
+
        /* Serialize it */
-       serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
+       lprintf(CTDL_DEBUG, "Serializing\n");
+       serialized_request = strdup(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));
+               free(serialized_request);
        }
        cprintf("\n000\n");
 
        /* Go back to the room from which we came... */
-       getroom(&CC->quickroom, hold_rm);
-
-       cprintf("%d not implemented yet\n", ERROR);
+       getroom(&CC->room, hold_rm);
 }
 
 
@@ -1184,46 +1347,55 @@ void cmd_ical(char *argbuf)
        char action[SIZ];
        char who[SIZ];
 
-       if (CtdlAccessCheck(ac_logged_in)) return;
-
        extract(subcmd, argbuf, 0);
 
-       if (!strcmp(subcmd, "test")) {
+       /* Allow "test" and "freebusy" subcommands without logging in. */
+
+       if (!strcasecmp(subcmd, "test")) {
                cprintf("%d This server supports calendaring\n", CIT_OK);
                return;
        }
 
-       else if (!strcmp(subcmd, "respond")) {
+       if (!strcasecmp(subcmd, "freebusy")) {
+               extract(who, argbuf, 1);
+               ical_freebusy(who);
+               return;
+       }
+
+       if (!strcasecmp(subcmd, "sgi")) {
+               CIT_ICAL->server_generated_invitations =
+                       (extract_int(argbuf, 1) ? 1 : 0) ;
+               cprintf("%d %d\n",
+                       CIT_OK, CIT_ICAL->server_generated_invitations);
+               return;
+       }
+
+       if (CtdlAccessCheck(ac_logged_in)) return;
+
+       if (!strcasecmp(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 (!strcasecmp(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 (!strcasecmp(subcmd, "conflicts")) {
                msgnum = extract_long(argbuf, 1);
                extract(partnum, argbuf, 2);
                ical_conflicts(msgnum, partnum);
-       }
-
-       else if (!strcmp(subcmd, "freebusy")) {
-               extract(who, argbuf, 1);
-               ical_freebusy(who);
-       }
-
-       else {
-               cprintf("%d Invalid subcommand\n", ERROR+CMD_NOT_SUPPORTED);
                return;
        }
 
-       /* should never get here */
+       cprintf("%d Invalid subcommand\n", ERROR + CMD_NOT_SUPPORTED);
 }
 
 
@@ -1233,7 +1405,7 @@ void cmd_ical(char *argbuf)
  */
 void ical_create_room(void)
 {
-       struct quickroom qr;
+       struct ctdlroom qr;
        struct visit vbuf;
 
        /* Create the calendar room if it doesn't already exist */
@@ -1241,7 +1413,7 @@ void ical_create_room(void)
 
        /* Set expiration policy to manual; otherwise objects will be lost! */
        if (lgetroom(&qr, USERCALENDARROOM)) {
-               lprintf(3, "Couldn't get the user calendar room!\n");
+               lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
                return;
        }
        qr.QRep.expire_mode = EXPIRE_MANUAL;
@@ -1249,16 +1421,16 @@ void ical_create_room(void)
        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);
 
        /* Set expiration policy to manual; otherwise objects will be lost! */
        if (lgetroom(&qr, USERTASKSROOM)) {
-               lprintf(3, "Couldn't get the user calendar room!\n");
+               lprintf(CTDL_CRIT, "Couldn't get the user calendar room!\n");
                return;
        }
        qr.QRep.expire_mode = EXPIRE_MANUAL;
@@ -1266,9 +1438,9 @@ void ical_create_room(void)
        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;
 }
@@ -1293,7 +1465,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
        icalproperty *summary = NULL;
 
        if (cal == NULL) {
-               lprintf(3, "ERROR: trying to reply to NULL event?\n");
+               lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
                return;
        }
 
@@ -1311,7 +1483,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
        /* Clone the event */
        the_request = icalcomponent_new_clone(cal);
        if (the_request == NULL) {
-               lprintf(3, "ERROR: cannot clone calendar object\n");
+               lprintf(CTDL_ERR, "ERROR: cannot clone calendar object\n");
                return;
        }
 
@@ -1334,17 +1506,20 @@ void ical_send_out_invitations(icalcomponent *cal) {
                        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;
+
+                               if (!CtdlIsMe(this_attendee)) { /* don't send an invitation to myself! */
+                                       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);
+       lprintf(CTDL_DEBUG, "<%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!
@@ -1355,9 +1530,9 @@ 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",
+               lprintf(CTDL_DEBUG, "Error at %s:%d - could not allocate component!\n",
                        __FILE__, __LINE__);
                icalcomponent_free(the_request);
                return;
@@ -1382,20 +1557,20 @@ void ical_send_out_invitations(icalcomponent *cal) {
        icalcomponent_add_component(encaps, the_request);
 
        /* Serialize it */
-       serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
+       serialized_request = strdup(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);
+       request_message_text = malloc(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,
+               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);
@@ -1406,7 +1581,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
                        CtdlFreeMessage(msg);
                }
        }
-       phree(serialized_request);
+       free(serialized_request);
 }
 
 
@@ -1421,9 +1596,12 @@ void ical_saving_vevent(icalcomponent *cal) {
        icalproperty *organizer = NULL;
        char organizer_string[SIZ];
 
+       /* Don't send out invitations unless the client wants us to. */
+       if (CIT_ICAL->server_generated_invitations == 0) {
+               return;
+       }
+
        /* 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;
        }
@@ -1546,8 +1724,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. */
        }
 
@@ -1574,21 +1752,21 @@ int ical_obj_beforesave(struct CtdlMessage *msg)
                                );
                                if (strlen(imm.uid) > 0) {
                                        if (msg->cm_fields['E'] != NULL) {
-                                               phree(msg->cm_fields['E']);
+                                               free(msg->cm_fields['E']);
                                        }
-                                       msg->cm_fields['E'] = strdoop(imm.uid);
+                                       msg->cm_fields['E'] = strdup(imm.uid);
                                }
                                if (strlen(imm.subject) > 0) {
                                        if (msg->cm_fields['U'] != NULL) {
-                                               phree(msg->cm_fields['U']);
+                                               free(msg->cm_fields['U']);
                                        }
-                                       msg->cm_fields['U'] = strdoop(imm.subject);
+                                       msg->cm_fields['U'] = strdup(imm.subject);
                                }
                                if (imm.dtstart > 0) {
                                        if (msg->cm_fields['T'] != NULL) {
-                                               phree(msg->cm_fields['T']);
+                                               free(msg->cm_fields['T']);
                                        }
-                                       msg->cm_fields['T'] = strdoop("000000000000000000");
+                                       msg->cm_fields['T'] = strdup("000000000000000000");
                                        sprintf(msg->cm_fields['T'], "%ld", imm.dtstart);
                                }
                                return 0;
@@ -1601,7 +1779,7 @@ int ical_obj_beforesave(struct CtdlMessage *msg)
        }
        
        /* Oops!  No Content-Type in this message!  How'd that happen? */
-       lprintf(7, "RFC822 message with no Content-Type header!\n");
+       lprintf(CTDL_ERR, "RFC822 message with no Content-Type header!\n");
        return 1;
 }
 
@@ -1644,8 +1822,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. */
        }
 
@@ -1678,7 +1856,7 @@ int ical_obj_aftersave(struct CtdlMessage *msg)
        }
        
        /* Oops!  No Content-Type in this message!  How'd that happen? */
-       lprintf(7, "RFC822 message with no Content-Type header!\n");
+       lprintf(CTDL_ERR, "RFC822 message with no Content-Type header!\n");
        return 1;
 }
 
@@ -1697,7 +1875,6 @@ void ical_session_startup(void) {
 char *serv_calendar_init(void)
 {
 #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);