libical, expat, and libsieve are now *required*.
[citadel.git] / citadel / modules / calendar / serv_calendar.c
index 619194cf0e28c676339f559074bb0045a9c0591e..7e0ed46ab22734533ed81baa9837ab9254833d86 100644 (file)
@@ -19,6 +19,8 @@
 #ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
+#include <ical.h>
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
 #include "config.h"
 #include "user_ops.h"
 #include "room_ops.h"
-#include "tools.h"
 #include "msgbase.h"
-#include "mime_parser.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"
 
 
@@ -105,12 +101,16 @@ icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
 
 /*
  * Write a calendar object into the specified user's calendar room.
+ * If the supplied user is NULL, this function writes the calendar object
+ * to the currently selected room.
  */
 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
        char temp[PATH_MAX];
-       FILE *fp;
-       char *ser;
-       icalcomponent *encaps;
+       FILE *fp = NULL;
+       char *ser = NULL;
+       icalcomponent *encaps = NULL;
+       struct CtdlMessage *msg = NULL;
+       icalcomponent *tmp=NULL;
 
        if (cal == NULL) return;
 
@@ -118,35 +118,61 @@ void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
         * a full VCALENDAR component, and save that instead.
         */
        if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
-               encaps = ical_encapsulate_subcomponent(
-                       icalcomponent_new_clone(cal)
-               );
+               tmp = icalcomponent_new_clone(cal);
+               encaps = ical_encapsulate_subcomponent(tmp);
                ical_write_to_cal(u, encaps);
+               icalcomponent_free(tmp);
                icalcomponent_free(encaps);
                return;
        }
 
-       CtdlMakeTempFileName(temp, sizeof temp);
        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;
-       fwrite(ser, strlen(ser), 1, fp);
-       fclose(fp);
+       /* If the caller supplied a user, write to that user's default calendar room */
+       if (u) {
+               /* Make a temp file out of it */
+               CtdlMakeTempFileName(temp, sizeof temp);
+               fp = fopen(temp, "w");
+               if (fp != NULL) {
+                       fwrite(ser, strlen(ser), 1, fp);
+                       fclose(fp);
+               
+                       /* This handy API function does all the work for us. */
+                       CtdlWriteObject(USERCALENDARROOM,       /* which room */
+                               "text/calendar",        /* MIME type */
+                               temp,                   /* temp file */
+                               u,                      /* which user */
+                               0,                      /* not binary */
+                               0,                      /* don't delete others of this type */
+                               0                       /* no flags */
+                       );
+                       unlink(temp);
+               }
+       }
 
-       /* This handy API function does all the work for us.
-        */
-       CtdlWriteObject(USERCALENDARROOM,       /* which room */
-                       "text/calendar",        /* MIME type */
-                       temp,                   /* temp file */
-                       u,                      /* which user */
-                       0,                      /* not binary */
-                       0,              /* don't delete others of this type */
-                       0);                     /* no flags */
-
-       unlink(temp);
+       /* If the caller did not supply a user, write to the currently selected room */
+       if (!u) {
+               msg = malloc(sizeof(struct CtdlMessage));
+               memset(msg, 0, sizeof(struct CtdlMessage));
+               msg->cm_magic = CTDLMESSAGE_MAGIC;
+               msg->cm_anon_type = MES_NORMAL;
+               msg->cm_format_type = 4;
+               msg->cm_fields['A'] = strdup(CC->user.fullname);
+               msg->cm_fields['O'] = strdup(CC->room.QRname);
+               msg->cm_fields['N'] = strdup(config.c_nodename);
+               msg->cm_fields['H'] = strdup(config.c_humannode);
+               msg->cm_fields['M'] = malloc(strlen(ser) + 40);
+               strcpy(msg->cm_fields['M'], "Content-type: text/calendar\r\n\r\n");
+               strcat(msg->cm_fields['M'], ser);
+       
+               /* Now write the data */
+               CtdlSubmitMsg(msg, NULL, "");
+               CtdlFreeMessage(msg);
+       }
+
+       /* In either case, now we can free the serialized calendar object */
+//     free(ser);
 }
 
 
@@ -235,7 +261,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                                if (!strncasecmp(attendee_string, "MAILTO:", 7)) {
                                        strcpy(attendee_string, &attendee_string[7]);
                                        striplt(attendee_string);
-                                       recp = validate_recipients(attendee_string);
+                                       recp = validate_recipients(attendee_string, NULL, 0);
                                        if (recp != NULL) {
                                                if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
                                                        if (me_attend) icalproperty_free(me_attend);
@@ -321,7 +347,7 @@ void ical_send_a_reply(icalcomponent *request, char *action) {
                        reply_message_text);
        
                if (msg != NULL) {
-                       valid = validate_recipients(organizer_string);
+                       valid = validate_recipients(organizer_string, NULL, 0);
                        CtdlSubmitMsg(msg, valid, "");
                        CtdlFreeMessage(msg);
                        free_recipients(valid);
@@ -354,7 +380,8 @@ void ical_locate_part(char *name, char *filename, char *partnum, char *disp,
                }
        }
 
-       if (strcasecmp(cbtype, "text/calendar")) {
+       if (  (strcasecmp(cbtype, "text/calendar"))
+          && (strcasecmp(cbtype, "application/ics")) ) {
                return;
        }
 
@@ -500,7 +527,8 @@ void ical_locate_original_event(char *name, char *filename, char *partnum, char
 
        struct original_event_container *oec = NULL;
 
-       if (strcasecmp(cbtype, "text/calendar")) {
+       if (  (strcasecmp(cbtype, "text/calendar"))
+          && (strcasecmp(cbtype, "application/ics")) ) {
                return;
        }
        oec = (struct original_event_container *) cbuserdata;
@@ -1185,7 +1213,7 @@ void ical_freebusy(char *who) {
        /* If not found, try it as an unqualified email address. */
        if (found_user != 0) {
                strcpy(buf, who);
-               recp = validate_recipients(buf);
+               recp = validate_recipients(buf, NULL, 0);
                lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
                if (recp != NULL) {
                        if (recp->num_local == 1) {
@@ -1201,7 +1229,7 @@ void ical_freebusy(char *who) {
        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);
+               recp = validate_recipients(buf, NULL, 0);
                if (recp != NULL) {
                        if (recp->num_local == 1) {
                                found_user = getuser(&usbuf, recp->recp_local);
@@ -1224,7 +1252,7 @@ void ical_freebusy(char *who) {
                           || (!strcasecmp(type, "directory")) ) {
                                snprintf(buf, sizeof buf, "%s@%s", who, host);
                                lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
-                               recp = validate_recipients(buf);
+                               recp = validate_recipients(buf, NULL, 0);
                                if (recp != NULL) {
                                        if (recp->num_local == 1) {
                                                found_user = getuser(&usbuf, recp->recp_local);
@@ -1257,7 +1285,6 @@ void ical_freebusy(char *who) {
        if (fb == NULL) {
                cprintf("%d Internal error: cannot allocate memory.\n",
                        ERROR + INTERNAL_ERROR);
-               icalcomponent_free(encaps);
                getroom(&CC->room, hold_rm);
                return;
        }
@@ -1274,7 +1301,7 @@ void ical_freebusy(char *who) {
                strcat(buf, "@");
                strcat(buf, config.c_fqdn);
        }
-       for (i=0; i<strlen(buf); ++i) {
+       for (i=0; buf[i]; ++i) {
                if (buf[i]==' ') buf[i] = '_';
        }
        icalcomponent_add_property(fb, icalproperty_new_organizer(buf));
@@ -1457,7 +1484,7 @@ void ical_putics(void)
        }
 
        cprintf("%d Transmit data now\n", SEND_LISTING);
-        calstream = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0);
+        calstream = CtdlReadMessageBody("000", config.c_maxmsglen, NULL, 0, 0);
        if (calstream == NULL) {
                return;
        }
@@ -1478,7 +1505,7 @@ void ical_putics(void)
         * in.  This will almost never happen.
         */
        if (icalcomponent_isa(cal) != ICAL_VCALENDAR_COMPONENT) {
-               ical_write_to_cal(&CC->user, cal);
+               ical_write_to_cal(NULL, cal);
        }
        /*
         * In the more likely event that we're looking at a VCALENDAR with the VEVENT
@@ -1488,7 +1515,7 @@ void ical_putics(void)
                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);
+                       ical_write_to_cal(NULL, c);
                }
        }
 
@@ -1650,6 +1677,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
        icalproperty *attendee = NULL;
        char summary_string[SIZ];
        icalproperty *summary = NULL;
+       size_t reqsize;
 
        if (cal == NULL) {
                lprintf(CTDL_ERR, "ERROR: trying to reply to NULL event?\n");
@@ -1748,9 +1776,10 @@ void ical_send_out_invitations(icalcomponent *cal) {
        icalcomponent_free(encaps);     /* Don't need this anymore. */
        if (serialized_request == NULL) return;
 
-       request_message_text = malloc(strlen(serialized_request) + SIZ);
+       reqsize = strlen(serialized_request) + SIZ;
+       request_message_text = malloc(reqsize);
        if (request_message_text != NULL) {
-               sprintf(request_message_text,
+               snprintf(request_message_text, reqsize,
                        "Content-type: text/calendar\r\n\r\n%s\r\n",
                        serialized_request
                );
@@ -1766,7 +1795,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
                        request_message_text);
        
                if (msg != NULL) {
-                       valid = validate_recipients(attendees_string);
+                       valid = validate_recipients(attendees_string, NULL, 0);
                        CtdlSubmitMsg(msg, valid, "");
                        CtdlFreeMessage(msg);
                        free_recipients(valid);
@@ -1855,7 +1884,8 @@ void ical_ctdl_set_exclusive_msgid(char *name, char *filename, char *partnum,
        imm = (struct icalmessagemod *)cbuserdata;
 
        /* We're only interested in calendar data. */
-       if (strcasecmp(cbtype, "text/calendar")) {
+       if (  (strcasecmp(cbtype, "text/calendar"))
+          && (strcasecmp(cbtype, "application/ics")) ) {
                return;
        }
 
@@ -1990,7 +2020,8 @@ void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
        icalcomponent *cal;
 
        /* We're only interested in calendar items here. */
-       if (strcasecmp(cbtype, "text/calendar")) {
+       if (  (strcasecmp(cbtype, "text/calendar"))
+          && (strcasecmp(cbtype, "application/ics")) ) {
                return;
        }
 
@@ -1999,7 +2030,8 @@ void ical_obj_aftersave_backend(char *name, char *filename, char *partnum,
         * ical_obj_beforesave() sees it there, it'll set the Exclusive msgid
         * to that string.
         */
-       if (!strcasecmp(cbtype, "text/calendar")) {
+       if (  (!strcasecmp(cbtype, "text/calendar"))
+          || (!strcasecmp(cbtype, "application/ics")) ) {
                cal = icalcomponent_new_from_string(content);
                if (cal != NULL) {
                        ical_saving_vevent(cal);
@@ -2132,32 +2164,31 @@ void ical_fixed_output(char *ptr, int len) {
 }
 
 
-#endif /* CITADEL_WITH_CALENDAR_SERVICE */
+
+void serv_calendar_destroy(void)
+{
+       icaltimezone_free_builtin_timezones();
+}
 
 /*
  * Register this module with the Citadel server.
  */
 CTDL_MODULE_INIT(calendar)
 {
-#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);
-       CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
-       CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
-#endif
-
+       if (!threading)
+       {
+               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);
+               CtdlRegisterSessionHook(ical_session_shutdown, EVT_STOP);
+               CtdlRegisterFixedOutputHook("text/calendar", ical_fixed_output);
+               CtdlRegisterFixedOutputHook("application/ics", ical_fixed_output);
+               CtdlRegisterCleanupHook(serv_calendar_destroy);
+       }
+       
        /* return our Subversion id for the Log */
        return "$Id$";
 }
 
-
-
-void serv_calendar_destroy(void)
-{
-#ifdef CITADEL_WITH_CALENDAR_SERVICE
-       icaltimezone_free_builtin_timezones();
-#endif
-}