don't call curl_multi_add_handle() from outside of the event queue
[citadel.git] / citadel / modules / extnotify / funambol65.c
index 6049b179d2fbd8877286a476551a4a209eb094da..2f6ef6e5cb8fb26e2fc55087777dd68e16e8bde5 100644 (file)
@@ -48,6 +48,7 @@
 #include "extnotify.h"
 
 eNextState EvaluateResult(AsyncIO *IO);
+eNextState ExtNotifyTerminate(AsyncIO *IO);
 
 /*
 * \brief Sends a message to the Funambol server notifying 
@@ -67,12 +68,13 @@ int notify_http_server(char *remoteurl,
        char *SOAPMessage = NULL;
        char *contenttype = NULL;
        StrBuf *ReplyBuf;
+       StrBuf *Buf;
        CURL *chnd;
        AsyncIO *IO;
 
        IO = (AsyncIO*) malloc(sizeof(AsyncIO));
        memset(IO, 0, sizeof(AsyncIO));
-       IO->CitContext = CC;
+       IO->CitContext = CloneContext(CC);
 
        snprintf(msgnumstr, 128, "%ld", MsgNum);
 
@@ -88,7 +90,7 @@ int notify_http_server(char *remoteurl,
                        snprintf(buf, SIZ, 
                                 "Cannot load template file %s [%s]won't send notification\r\n", 
                                 file_funambol_msg, strerror(errno));
-                       CtdlLogPrintf(CTDL_ERR, buf);
+                       syslog(LOG_ERR, "%s", buf);
 
                        CtdlAideMessage(buf, "External notifier unable to find message template!");
                        goto abort;
@@ -111,7 +113,7 @@ int notify_http_server(char *remoteurl,
                        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);
 
                        CtdlAideMessage(buf, "External notifier unable to load message template!");
                        goto abort;
@@ -145,15 +147,18 @@ int notify_http_server(char *remoteurl,
                IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "Pragma: no-cache");
        }
 
-       ParseURL(&IO->ConnectMe, NewStrBufPlain (remoteurl, -1), 80);
+       Buf = NewStrBufPlain (remoteurl, -1);
+       ParseURL(&IO->ConnectMe, Buf, 80);
+       FreeStrBuf(&Buf); /* TODO: this is uncool... */
        CurlPrepareURL(IO->ConnectMe);
        if (! evcurl_init(IO, 
 //                       Ctx, 
                          NULL,
                          "Citadel ExtNotify",
-                         EvaluateResult))
+                         EvaluateResult, 
+                         ExtNotifyTerminate))
        {
-               CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
+               syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
                goto abort;
        }
        chnd = IO->HttpReq.chnd;
@@ -173,7 +178,7 @@ int notify_http_server(char *remoteurl,
                OPT(INTERFACE, config.c_ip_addr);
        }
 
-       evcurl_handle_start(IO);
+       QueueCurlContext(IO);
 
        return 0;
 abort:
@@ -193,9 +198,10 @@ eNextState EvaluateResult(AsyncIO *IO)
        if (IO->HttpReq.httpcode != 200) {
                StrBuf *ErrMsg;
 
-               CtdlLogPrintf(CTDL_ALERT, "libcurl error %ld: %s\n", 
+               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, 
@@ -219,7 +225,7 @@ eNextState EvaluateResult(AsyncIO *IO)
                CtdlAideMessage(ChrPtr(ErrMsg), "External notifier unable to load message template!");
        }
 
-       CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
+       syslog(LOG_DEBUG, "Funambol notified\n");
 /*
        while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
                FreeStrBuf(&Ctx.NotifyHostList[i]);
@@ -253,3 +259,9 @@ eNextState EvaluateResult(AsyncIO *IO)
        ///FreeStrBuf (&ReplyBuf);
        return 0;
 }
+
+eNextState ExtNotifyTerminate(AsyncIO *IO)
+{
+       free(IO);
+       return eAbort;
+}