]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/extnotify/funambol65.c
* fix typo in HTTP headers. thanks to koos for pointing this out.
[citadel.git] / citadel / modules / extnotify / funambol65.c
index 90fe4eb0ced41745c1b23eba1ed003a2b3fb10bd..30d6d0fe2f8cfd246aa1e3f5ca11fd7146af214c 100644 (file)
@@ -17,6 +17,8 @@
 #include <time.h>
 #include <libcitadel.h>
 #include <errno.h>
+#include <unistd.h>
+#include <curl/curl.h>
 
 #include "citadel.h"
 #include "citadel_dirs.h"
 * of new mail for a user
 * Returns 0 if unsuccessful
 */
-int notify_funambol_server(char *user) {
-       char port[1024];
-       int sock = -1;
+int notify_http_server(char *remoteurl, 
+                      const char* template, long tlen, 
+                      char *user,
+                      char *msgid, 
+                      long MsgNum) 
+{
+       char curl_errbuf[CURL_ERROR_SIZE];
+       char *pchs, *pche;
+       char userpass[SIZ];
+       char msgnumstr[128];
        char *buf = NULL;
+       CURL *curl;
+       CURLcode res;
+       struct curl_slist * headers=NULL;
+       char errmsg[1024] = "";
        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) 
-               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);
+       char *contenttype = NULL;
+       StrBuf *ReplyBuf;
 
-               aide_message(buf, "External notifier unable to connect remote host!");
-               goto bail;
+       curl = curl_easy_init();
+       if (!curl) {
+               CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
+               return 1;
        }
-       // Load the template SOAP message. Get mallocs done too
-       template = fopen(file_funambol_msg, "r");
 
-       if (template == NULL) {
-               char buf[SIZ];
+       curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
+       curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
+       ReplyBuf = NewStrBuf();
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
+       curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
+       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
+       curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
 
-               snprintf(buf, SIZ, 
-                        "Cannot load template file %s [%s]won't send notification\r\n", 
-                        file_funambol_msg, strerror(errno));
-               CtdlLogPrintf(CTDL_ERR, buf);
+       pchs = strchr(remoteurl, ':');
+       pche = strchr(remoteurl, '@');
+       if ((pche != NULL) && 
+           (pchs != NULL) && 
+           (pchs < pche) && ((pche - pchs) < SIZ)) {
+               memcpy(userpass, pchs + 3, pche - pchs - 3);
+               
+               userpass[pche - pchs - 3] = '\0';
+               curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
+               curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
 
-               aide_message(buf, "External notifier unable to find message template!");
-               goto free;
+       }
+#ifdef CURLOPT_HTTP_CONTENT_DECODING
+       curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
+       curl_easy_setopt(curl, CURLOPT_ENCODING, "");
+#endif
+       curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
+       curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);           /* die after 180 seconds */
+       if (!IsEmptyStr(config.c_ip_addr)) {
+               curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
        }
 
+       headers = curl_slist_append(headers,"Accept: application/soap+xml, application/mime, multipart/related, text/*");
+       headers = curl_slist_append(headers,"Pragma: no-cache");
 
-       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);
+       if (tlen > 0) {
+               /* Load the template message. Get mallocs done too */
+               FILE *Ftemplate = NULL;
+               const char *mimetype;
+
+               Ftemplate = fopen(template, "r");
+               if (Ftemplate == 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;
+               }
+               mimetype = GuessMimeByFilename(template, tlen);
+
+               snprintf(msgnumstr, 128, "%ld", MsgNum);
+
+               buf = malloc(SIZ);
+               memset(buf, 0, SIZ);
+               SOAPMessage = malloc(3072);
+               memset(SOAPMessage, 0, 3072);
        
-       while(fgets(buf, SIZ, template) != NULL) {
-               strcat(SOAPMessage, buf);
-       }
-       fclose(template);
+               while(fgets(buf, SIZ, Ftemplate) != NULL) {
+                       strcat(SOAPMessage, buf);
+               }
+               fclose(Ftemplate);
        
-       if (strlen(SOAPMessage) < 0) {
-               char buf[SIZ];
+               if (strlen(SOAPMessage) < 0) {
+                       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);
+               help_subst(SOAPMessage, "^msgid", msgid);
+               help_subst(SOAPMessage, "^msgnum", msgnumstr);
+
+               curl_easy_setopt(curl, CURLOPT_URL, remoteurl);
+
+               /* pass our list of custom made headers */
 
-               snprintf(buf, SIZ, 
-                        "Cannot load template file %s; won't send notification\r\n", 
-                        file_funambol_msg);
-               CtdlLogPrintf(CTDL_ERR, buf);
+               contenttype=(char*) malloc(40+strlen(mimetype));
+               sprintf(contenttype,"Content-Type: %s; charset=utf-8", mimetype);
 
-               aide_message(buf, "External notifier unable to load message template!");
-               goto free;
+               headers = curl_slist_append(headers, "SOAPAction: \"\"");
+               headers = curl_slist_append(headers, contenttype);
+
+               /* Now specify the POST binary data */
+
+               curl_easy_setopt(curl, CURLOPT_POSTFIELDS, SOAPMessage);
+               curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(SOAPMessage));
+               curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
+       }
+       else {
+               help_subst(remoteurl, "^notifyuser", user);
+               help_subst(remoteurl, "^syncsource", config.c_funambol_source);
+               help_subst(remoteurl, "^msgid", msgid);
+               help_subst(remoteurl, "^msgnum", msgnumstr);
+               curl_easy_setopt(curl, CURLOPT_URL, remoteurl);
+               curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        }
-       // Do substitutions
-       help_subst(SOAPMessage, "^notifyuser", user);
-       help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
-       
-       /* Build the HTTP request header */
 
-       
-       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");
-       sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
-               "Citadel",
-               REV_LEVEL,
-               config.c_funambol_host,
-               config.c_funambol_port
-               );
-       strcat(SOAPHeader,buf);
-       strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
-       sprintf(buf, "Content-Length: %d \r\n",
-               strlen(SOAPMessage));
-       strcat(SOAPHeader, buf);
-       
-       
+       res = curl_easy_perform(curl);
+       if (res) {
+               StrBuf *ErrMsg;
 
-       CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
-       
-       
-       sprintf(buf, "Authorization: Basic %s\r\n\r\n",
-               funambolCreds);
-       strcat(SOAPHeader, buf);
-       
-       sock_write(sock, SOAPHeader, strlen(SOAPHeader));
-       sock_write(sock, SOAPMessage, strlen(SOAPMessage));
-       sock_shutdown(sock, SHUT_WR);
-       
-       /* Response */
-       CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
-        if (sock_getln(sock, buf, SIZ) < 0) {
-                goto free;
-        }
-        CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
-       if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
-               
-               goto free;
+               CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
+               ErrMsg = NewStrBufPlain(HKEY("Error sending your Notification\n"));
+               StrBufAppendPrintf(ErrMsg, "\nlibcurl error %d: %s\n", res, errmsg);
+               StrBufAppendBufPlain(ErrMsg, curl_errbuf, -1, 0);
+               StrBufAppendBufPlain(ErrMsg, HKEY("\nWas Trying to send: \n"), 0);
+               StrBufAppendBufPlain(ErrMsg, remoteurl, -1, 0);
+               if (tlen > 0) {
+                       StrBufAppendBufPlain(ErrMsg, HKEY("\nThe Post document was: \n"), 0);
+                       StrBufAppendBufPlain(ErrMsg, SOAPMessage, -1, 0);
+                       StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);                  
+               }
+               if (StrLength(ReplyBuf) > 0) {                  
+                       StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThe Serverreply was: \n\n"), 0);
+                       StrBufAppendBuf(ErrMsg, ReplyBuf, 0);
+               }
+               else 
+                       StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThere was no Serverreply.\n\n"), 0);
+/* TODO: this will change the floor we're in :(
+               quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED, 
+                               "Failed to notify external service about inbound mail");
+*/
+               FreeStrBuf(&ErrMsg);
        }
+
        CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
 free:
-       if (funambolCreds != NULL) free(funambolCreds);
+       curl_slist_free_all (headers);
+       curl_easy_cleanup(curl);
+       if (contenttype) free(contenttype);
        if (SOAPMessage != NULL) free(SOAPMessage);
        if (buf != NULL) free(buf);
-       if (SOAPHeader != NULL) free(SOAPHeader);
-bail:
-       close(sock);
+       FreeStrBuf (&ReplyBuf);
        return 0;
 }
-