]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/extnotify/funambol65.c
* this include is needed on *bsd
[citadel.git] / citadel / modules / extnotify / funambol65.c
index 6b394e596809e42d47ef55ff3f9647674dff1a81..cebed9a05d2fda83a06d8c11ad2e893b9163bfea 100644 (file)
 #include <sys/socket.h>
 #include <time.h>
 #include <libcitadel.h>
+#include <errno.h>
+#include <unistd.h>
 
+#include "citadel.h"
 #include "citadel_dirs.h"
 #include "clientsocket.h"
 #include "sysdep.h"
-#include "sysconfig.h"
 #include "config.h"
 #include "sysdep_decls.h"
+#include "msgbase.h"
+#include "ctdl_module.h"
 
 /*
 * \brief Sends a message to the Funambol server notifying 
 int notify_funambol_server(char *user) {
        char port[1024];
        int sock = -1;
-       char *buf;
-       char *SOAPMessage;
-       char *SOAPHeader;
-       char *funambolCreds;
-       FILE *template;
-       FILE *fnblConf;
+       char *buf = NULL;
+       char *SOAPMessage = NULL;
+       char *SOAPHeader = NULL;
+       char *funambolCreds = NULL;
+       FILE *template = NULL;
        
        sprintf(port, "%d", config.c_funambol_port);
        sock = sock_connect(config.c_funambol_host, port, "tcp");
        if (sock >= 0) 
-               lprintf(CTDL_DEBUG, "Connected to Funambol!\n");
-       else 
+               CtdlLogPrintf(CTDL_DEBUG, "Connected to Funambol!\n");
+       else {
+               char buf[SIZ];
+
+               snprintf(buf, SIZ, 
+                        "Unable to connect to %s:%d [%s]; won't send notification\r\n", 
+                        config.c_funambol_host, 
+                        config.c_funambol_port, 
+                        strerror(errno));
+               CtdlLogPrintf(CTDL_ERR, buf);
+
+               aide_message(buf, "External notifier unable to connect remote host!");
                goto bail;
-       // Load the template SOAP message
+       }
+       // Load the template SOAP message. Get mallocs done too
        template = fopen(file_funambol_msg, "r");
+
+       if (template == NULL) {
+               char buf[SIZ];
+
+               snprintf(buf, SIZ, 
+                        "Cannot load template file %s [%s]won't send notification\r\n", 
+                        file_funambol_msg, strerror(errno));
+               CtdlLogPrintf(CTDL_ERR, buf);
+
+               aide_message(buf, "External notifier unable to find message template!");
+               goto free;
+       }
+
+
        buf = malloc(SIZ);
        memset(buf, 0, SIZ);
        SOAPMessage = malloc(3072);
        memset(SOAPMessage, 0, 3072);
+       
+       SOAPHeader  = malloc(SIZ);
+       memset(SOAPHeader, 0, SIZ);
+       
+       funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
+       memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
+       
        while(fgets(buf, SIZ, template) != NULL) {
                strcat(SOAPMessage, buf);
        }
        fclose(template);
        
        if (strlen(SOAPMessage) < 0) {
-               printf("Cannot load template file\r\n");
-               goto bail;
+               char buf[SIZ];
+
+               snprintf(buf, SIZ, 
+                        "Cannot load template file %s; won't send notification\r\n", 
+                        file_funambol_msg);
+               CtdlLogPrintf(CTDL_ERR, buf);
+
+               aide_message(buf, "External notifier unable to load message template!");
+               goto free;
        }
        // Do substitutions
        help_subst(SOAPMessage, "^notifyuser", user);
        help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
        
        /* Build the HTTP request header */
-       SOAPHeader  = malloc(SIZ);
-       memset(SOAPHeader, 0, SIZ);
+
+       
        sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
                FUNAMBOL_WS);
        strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
@@ -82,8 +124,7 @@ int notify_funambol_server(char *user) {
                strlen(SOAPMessage));
        strcat(SOAPHeader, buf);
        
-       funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
-       memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
+       
 
        CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
        
@@ -92,28 +133,28 @@ int notify_funambol_server(char *user) {
                funambolCreds);
        strcat(SOAPHeader, buf);
        
-       int written_header = sock_write(sock, SOAPHeader, strlen(SOAPHeader));
-       int written_body = sock_write(sock, SOAPMessage, strlen(SOAPMessage));
+       sock_write(sock, SOAPHeader, strlen(SOAPHeader));
+       sock_write(sock, SOAPMessage, strlen(SOAPMessage));
        sock_shutdown(sock, SHUT_WR);
        
        /* Response */
-       lprintf(CTDL_DEBUG, "Awaiting response\n");
+       CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
         if (sock_getln(sock, buf, SIZ) < 0) {
-                goto bail;
+                goto free;
         }
-        lprintf(CTDL_DEBUG, "<%s\n", buf);
+        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
        if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
                
-               goto bail;
+               goto free;
        }
-       lprintf(CTDL_DEBUG, "Funambol notified\n");
-       
+       CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
+free:
+       if (funambolCreds != NULL) free(funambolCreds);
+       if (SOAPMessage != NULL) free(SOAPMessage);
+       if (buf != NULL) free(buf);
+       if (SOAPHeader != NULL) free(SOAPHeader);
 bail:
        close(sock);
-       free(funambolCreds);
-       free(SOAPMessage);
-       free(buf);
-       free(SOAPHeader);
        return 0;
 }