]> 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 beca84454a4d03750b14a04deff2daa59a530be7..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
@@ -243,7 +245,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                                                        if (me_attend) icalproperty_free(me_attend);
                                                        me_attend = icalproperty_new_clone(attendee);
                                                }
-                                               phree(recp);
+                                               free(recp);
                                        }
                                }
                        }
@@ -302,11 +304,11 @@ 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",
@@ -325,7 +327,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                        CtdlFreeMessage(msg);
                }
        }
-       phree(serialized_reply);
+       free(serialized_reply);
 }
 
 
@@ -385,7 +387,7 @@ 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,
@@ -635,9 +637,9 @@ 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);
@@ -654,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 */
        }
@@ -678,13 +680,13 @@ 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->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",
@@ -706,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);
 }
 
@@ -731,7 +733,7 @@ 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,
@@ -894,7 +896,7 @@ 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, "_HUNT_");
@@ -1001,7 +1003,7 @@ 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,
@@ -1088,6 +1090,42 @@ void ical_add_to_freebusy(icalcomponent *fb, icalcomponent *cal) {
                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) );
+               }
+       }
+
 }
 
 
@@ -1107,7 +1145,7 @@ 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, "_HUNT_");
@@ -1140,8 +1178,71 @@ void ical_freebusy(char *who) {
        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);
+               }
+       }
 
-       if (getuser(&usbuf, who) != 0) {
+       /* 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 (found_user != 0) {
                cprintf("%d No such user.\n", ERROR + NO_SUCH_USER);
                return;
        }
@@ -1168,20 +1269,38 @@ void ical_freebusy(char *who) {
                return;
        }
 
-       lprintf(CTDL_DEBUG, "Adding busy time from events\n");
-       CtdlForEachMessage(MSGS_ALL, 0, "text/calendar",
-               NULL, ical_freebusy_backend, (void *)fb
-       );
-
        /* Set the method to PUBLISH */
        icalcomponent_set_method(fb, ICAL_METHOD_PUBLISH);
 
        /* Set the DTSTAMP to right now. */
        icalcomponent_set_dtstamp(fb, icaltime_from_timet(time(NULL), 0));
 
-       /* FIXME we still need (at least) DTSTART, DTEND, and the user's
-        * e-mail address as ORGANIZER.
+       /* 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 *)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");
@@ -1200,13 +1319,13 @@ void ical_freebusy(char *who) {
 
        /* Serialize it */
        lprintf(CTDL_DEBUG, "Serializing\n");
-       serialized_request = strdoop(icalcomponent_as_ical_string(encaps));
+       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));
-               phree(serialized_request);
+               free(serialized_request);
        }
        cprintf("\n000\n");
 
@@ -1438,11 +1557,11 @@ 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",
@@ -1462,7 +1581,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
                        CtdlFreeMessage(msg);
                }
        }
-       phree(serialized_request);
+       free(serialized_request);
 }
 
 
@@ -1633,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;