]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/calendar/serv_calendar.c
* give the flag to the CtdlDoIHavePermissionToPostInThisRoom in through the parameter...
[citadel.git] / citadel / modules / calendar / serv_calendar.c
index 619194cf0e28c676339f559074bb0045a9c0591e..526d737c50b124e2141e4dd21418e6dac48e44f6 100644 (file)
@@ -19,6 +19,7 @@
 #ifdef HAVE_STRINGS_H
 #include <strings.h>
 #endif
+#include <libcitadel.h>
 #include "citadel.h"
 #include "server.h"
 #include "citserver.h"
@@ -26,9 +27,7 @@
 #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"
@@ -105,12 +104,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 +121,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);
+               }
+       }
+
+       /* 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);
+       }
 
-       /* 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);
+       /* In either case, now we can free the serialized calendar object */
+//     free(ser);
 }
 
 
@@ -235,7 +264,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, 0);
                                        if (recp != NULL) {
                                                if (!strcasecmp(recp->recp_local, CC->user.fullname)) {
                                                        if (me_attend) icalproperty_free(me_attend);
@@ -321,7 +350,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, 0);
                        CtdlSubmitMsg(msg, valid, "");
                        CtdlFreeMessage(msg);
                        free_recipients(valid);
@@ -1185,7 +1214,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, 0);
                lprintf(CTDL_DEBUG, "Trying <%s>\n", buf);
                if (recp != NULL) {
                        if (recp->num_local == 1) {
@@ -1201,7 +1230,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, 0);
                if (recp != NULL) {
                        if (recp->num_local == 1) {
                                found_user = getuser(&usbuf, recp->recp_local);
@@ -1224,7 +1253,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, 0);
                                if (recp != NULL) {
                                        if (recp->num_local == 1) {
                                                found_user = getuser(&usbuf, recp->recp_local);
@@ -1257,7 +1286,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 +1302,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 +1485,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 +1506,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 +1516,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);
                }
        }
 
@@ -1766,7 +1794,7 @@ void ical_send_out_invitations(icalcomponent *cal) {
                        request_message_text);
        
                if (msg != NULL) {
-                       valid = validate_recipients(attendees_string);
+                       valid = validate_recipients(attendees_string, 0);
                        CtdlSubmitMsg(msg, valid, "");
                        CtdlFreeMessage(msg);
                        free_recipients(valid);
@@ -2132,6 +2160,12 @@ void ical_fixed_output(char *ptr, int len) {
 }
 
 
+
+void serv_calendar_destroy(void)
+{
+       icaltimezone_free_builtin_timezones();
+}
+
 #endif /* CITADEL_WITH_CALENDAR_SERVICE */
 
 /*
@@ -2139,25 +2173,21 @@ void ical_fixed_output(char *ptr, int len) {
  */
 CTDL_MODULE_INIT(calendar)
 {
+       if (!threading)
+       {
 #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);
+               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);
+               CtdlRegisterCleanupHook(serv_calendar_destroy);
 #endif
-
+       }
+       
        /* return our Subversion id for the Log */
        return "$Id$";
 }
 
-
-
-void serv_calendar_destroy(void)
-{
-#ifdef CITADEL_WITH_CALENDAR_SERVICE
-       icaltimezone_free_builtin_timezones();
-#endif
-}