Straightn http client generation
[citadel.git] / citadel / modules / extnotify / funambol65.c
index af46df28c8d605ff32b035728b6626a6e74c55f9..bff95fbb49fc0c7b399e47303ab6f801e38603d3 100644 (file)
@@ -1,14 +1,29 @@
 /* 
-* \file funambol65.c
-* @author Mathew McBride
-* 
-* This module facilitates notifications to a Funambol server
-* for push email
-*
-* Based on bits of the previous serv_funambol
-* Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
-*/
-#include "extnotify.h"
+ * funambol65.c
+ * Author: Mathew McBride
+ * 
+ * This module facilitates notifications to a Funambol server
+ * for push email
+ *
+ * Based on bits of the previous serv_funambol
+ * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
+ *
+ * Copyright (c) 2008-2010
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ */
 
 #include <stdio.h>
 #include <string.h>
 #include "msgbase.h"
 #include "ctdl_module.h"
 
+#include "event_client.h"
+#include "extnotify.h"
 
-
-
-size_t extnotify_callback(void *ptr, size_t size, size_t nmemb, void *stream)
-{
-       return size * nmemb; /* don't care, just discard it so it doesn't end up on the console... */
-}
-struct fh_data {
-       char *buf;
-       int total_bytes_received;
-       int maxbytes;
-};
+eNextState EvaluateResult(AsyncIO *IO);
+eNextState ExtNotifyTerminate(AsyncIO *IO);
+eNextState ExtNotifyShutdownAbort(AsyncIO *IO);
 
 /*
-* \brief Sends a message to the Funambol server notifying 
+* \brief Sends a message to the Funambol server notifying
 * of new mail for a user
 * Returns 0 if unsuccessful
 */
-int notify_http_server(char *remoteurl, 
-                      char* template, long tlen, 
+int notify_http_server(char *remoteurl,
+                      const char* template, long tlen,
                       char *user,
-                      char *msgid, 
-                      long MsgNum) 
+                      char *msgid,
+                      long MsgNum,
+                      NotifyContext *Ctx)
 {
-       char *pchs, *pche;
-       char userpass[SIZ];
-       char retbuf[SIZ];
+       CURLcode sta;
        char msgnumstr[128];
        char *buf = NULL;
-       CURL *curl;
-       CURLcode res;
-       struct curl_slist * headers=NULL;
-       char errmsg[1024] = "";
        char *SOAPMessage = NULL;
-       char *contenttype;
-        struct fh_data fh = {
-                retbuf,
-                0,
-                SIZ
-        };
-               
-       curl = curl_easy_init();
-       if (!curl) {
-               CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
-               return 1;
-       }
-
-       curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
-       curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
-       curl_easy_setopt(curl, CURLOPT_WRITEDATA, fh);
-       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, extnotify_callback); /* don't care..*/
-       curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
-       curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
-
-       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);
-
-       }
-#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);
+       char *contenttype = NULL;
+       StrBuf *ReplyBuf;
+       StrBuf *Buf;
+       CURL *chnd;
+       AsyncIO *IO;
+
+       IO = (AsyncIO*) malloc(sizeof(AsyncIO));
+       memset(IO, 0, sizeof(AsyncIO));
+
+       if (! InitcURLIOStruct(IO,
+                              NULL, /* we don't have personal data anymore. */
+                              "Citadel ExtNotify",
+                              EvaluateResult,
+                              ExtNotifyTerminate,
+                              ExtNotifyShutdownAbort))
+       {
+               syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
+               goto abort;
        }
 
-       headers = curl_slist_append(headers,"Accept: application/soap+xml, application/dime, multipart/related, text/*");
-       headers = curl_slist_append(headers,"Pragma: no-cache");
+       snprintf(msgnumstr, 128, "%ld", MsgNum);
 
        if (tlen > 0) {
                /* Load the template message. Get mallocs done too */
@@ -114,150 +95,213 @@ int notify_http_server(char *remoteurl,
                const char *mimetype;
 
                Ftemplate = fopen(template, "r");
-               mimetype = GuessMimeByFilename(template, tlen);
-               if (template == NULL) {
+               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;
+                       snprintf(buf, SIZ,
+                                "Cannot load template file %s [%s] "
+                                "won't send notification\r\n",
+                                file_funambol_msg,
+                                strerror(errno));
+                       syslog(LOG_ERR, "%s", buf);
+                       // TODO: once an hour!
+                       CtdlAideMessage(
+                               buf,
+                               "External notifier: "
+                               "unable to find message template!");
+                       goto abort;
                }
-               snprintf(msgnumstr, 128, "%ld", MsgNum);
+               mimetype = GuessMimeByFilename(template, tlen);
 
                buf = malloc(SIZ);
                memset(buf, 0, SIZ);
                SOAPMessage = malloc(3072);
                memset(SOAPMessage, 0, 3072);
-       
+
                while(fgets(buf, SIZ, Ftemplate) != NULL) {
                        strcat(SOAPMessage, buf);
                }
                fclose(Ftemplate);
-       
+
                if (strlen(SOAPMessage) < 0) {
                        char buf[SIZ];
 
-                       snprintf(buf, SIZ, 
-                                "Cannot load template file %s; won't send notification\r\n", 
+                       snprintf(buf, SIZ,
+                                "Cannot load template file %s;"
+                                " won't send notification\r\n",
                                 file_funambol_msg);
-                       CtdlLogPrintf(CTDL_ERR, buf);
+                       syslog(LOG_ERR, "%s", buf);
 
-                       aide_message(buf, "External notifier unable to load message template!");
-                       goto free;
+                       CtdlAideMessage(buf, "External notifier: "
+                                       "unable to load message template!");
+                       goto abort;
                }
                // Do substitutions
                help_subst(SOAPMessage, "^notifyuser", user);
-               help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
+               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 */
 
                contenttype=(char*) malloc(40+strlen(mimetype));
-               sprintf(contenttype,"Content-type: %s; charset=utf-8", mimetype);
+               sprintf(contenttype,
+                       "Content-Type: %s; charset=utf-8",
+                       mimetype);
 
-               headers = curl_slist_append(headers, "SOAPAction: \"\"");
-               headers = curl_slist_append(headers, contenttype);
+               IO->HttpReq.headers = curl_slist_append(
+                       IO->HttpReq.headers,
+                       "SOAPAction: \"\"");
 
-               /* Now specify the POST binary data */
+               IO->HttpReq.headers = curl_slist_append(
+                       IO->HttpReq.headers,
+                       contenttype);
 
-               curl_easy_setopt(curl, CURLOPT_POSTFIELDS, SOAPMessage);
-               curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(SOAPMessage));
-               curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
+               IO->HttpReq.headers = curl_slist_append(
+                       IO->HttpReq.headers,
+                       "Accept: application/soap+xml, "
+                       "application/mime, multipart/related, text/*");
+
+               IO->HttpReq.headers = curl_slist_append(
+                       IO->HttpReq.headers,
+                       "Pragma: no-cache");
+
+               /* Now specify the POST binary data */
+               IO->HttpReq.PlainPostData = SOAPMessage;
+               IO->HttpReq.PlainPostDataLen = strlen(SOAPMessage);
        }
        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);
-       }
 
-       res = curl_easy_perform(curl);
-       if (res) {
-               CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
+               IO->HttpReq.headers = curl_slist_append(
+                       IO->HttpReq.headers,
+                       "Accept: application/soap+xml, "
+                       "application/mime, multipart/related, text/*");
+
+               IO->HttpReq.headers = curl_slist_append(
+                       IO->HttpReq.headers,
+                       "Pragma: no-cache");
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
-free:
-       curl_slist_free_all (headers);
-       curl_easy_cleanup(curl);
+       Buf = NewStrBufPlain (remoteurl, -1);
+       ParseURL(&IO->ConnectMe, Buf, 80);
+       FreeStrBuf(&Buf); /* TODO: this is uncool... */
+       CurlPrepareURL(IO->ConnectMe);
+
+       chnd = IO->HttpReq.chnd;
+       OPT(SSL_VERIFYPEER, 0);
+       OPT(SSL_VERIFYHOST, 0);
+
+       QueueCurlContext(IO);
+
+       return 0;
+abort:
+
        if (contenttype) free(contenttype);
        if (SOAPMessage != NULL) free(SOAPMessage);
        if (buf != NULL) free(buf);
-       return 0;
+       FreeStrBuf (&ReplyBuf);
+       return 1;
 }
 
 
-/*     
-       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);
+eNextState EvaluateResult(AsyncIO *IO)
+{
 
-               aide_message(buf, "External notifier unable to connect remote host!");
-               goto bail;
+       if (IO->HttpReq.httpcode != 200) {
+               StrBuf *ErrMsg;
+
+               syslog(LOG_ALERT, "libcurl error %ld: %s\n",
+                             IO->HttpReq.httpcode,
+                             IO->HttpReq.errdesc);
+
+               ErrMsg = NewStrBufPlain(
+                       HKEY("Error sending your Notification\n"));
+               StrBufAppendPrintf(ErrMsg, "\nlibcurl error %ld: \n\t\t%s\n",
+                                  IO->HttpReq.httpcode,
+                                  IO->HttpReq.errdesc);
+
+               StrBufAppendBufPlain(ErrMsg,
+                                    HKEY("\nWas Trying to send: \n"),
+                                    0);
+
+               StrBufAppendBufPlain(ErrMsg, IO->ConnectMe->PlainUrl, -1, 0);
+               if (IO->HttpReq.PlainPostDataLen > 0) {
+                       StrBufAppendBufPlain(
+                               ErrMsg,
+                               HKEY("\nThe Post document was: \n"),
+                               0);
+                       StrBufAppendBufPlain(ErrMsg,
+                                            IO->HttpReq.PlainPostData,
+                                            IO->HttpReq.PlainPostDataLen, 0);
+                       StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);
+               }
+               if (StrLength(IO->HttpReq.ReplyData) > 0) {
+                       StrBufAppendBufPlain(
+                               ErrMsg,
+                               HKEY("\n\nThe Serverreply was: \n\n"),
+                               0);
+                       StrBufAppendBuf(ErrMsg, IO->HttpReq.ReplyData, 0);
+               }
+               else
+                       StrBufAppendBufPlain(
+                               ErrMsg,
+                               HKEY("\n\nThere was no Serverreply.\n\n"),
+                               0);
+               ///ExtNotify_PutErrorMessage(Ctx, ErrMsg);
+               CtdlAideMessage(ChrPtr(ErrMsg),
+                               "External notifier: "
+                               "unable to contact notification host!");
        }
-*/
-//     if (funambolCreds != NULL) free(funambolCreds);
-       //if (SOAPHeader != NULL) free(SOAPHeader);
-       ///close(sock);
-
-       /* Build the HTTP request header */
 
-       
+       syslog(LOG_DEBUG, "Funambol notified\n");
 /*
-       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);
-*/
-       
-/*     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);     
-       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;
+       while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
+               FreeStrBuf(&Ctx.NotifyHostList[i]);
+
+       if (Ctx.NotifyErrors != NULL)
+       {
+               long len;
+               const char *Key;
+               HashPos *It;
+               void *vErr;
+               StrBuf *ErrMsg;
+
+               It = GetNewHashPos(Ctx.NotifyErrors, 0);
+               while (GetNextHashPos(Ctx.NotifyErrors,
+               It, &len, &Key, &vErr) &&
+                      (vErr != NULL)) {
+                       ErrMsg = (StrBuf*) vErr;
+                       quickie_message("Citadel", NULL, NULL,
+                       AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
+                       "Failed to notify external service about inbound mail");
+               }
+
+               DeleteHashPos(&It);
+               DeleteHash(&Ctx.NotifyErrors);
        }
 */
+
+////   curl_slist_free_all (headers);
+///    curl_easy_cleanup(curl);
+       ///if (contenttype) free(contenttype);
+       ///if (SOAPMessage != NULL) free(SOAPMessage);
+       ///if (buf != NULL) free(buf);
+       ///FreeStrBuf (&ReplyBuf);
+       return 0;
+}
+
+eNextState ExtNotifyTerminate(AsyncIO *IO)
+{
+       free(IO);
+       return eAbort;
+}
+eNextState ExtNotifyShutdownAbort(AsyncIO *IO)
+{
+       free(IO);
+       return eAbort;
+}