X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=citadel%2Fmodules%2Fextnotify%2Ffunambol65.c;h=bff95fbb49fc0c7b399e47303ab6f801e38603d3;hb=c855d497545dad80942a194624c111a54cd1fdc7;hp=fbba1ed54558bdd9833dfc537b56d5b610df3de9;hpb=f7b1efa87ade0ab3d80f39accf47f1de93c61b3e;p=citadel.git diff --git a/citadel/modules/extnotify/funambol65.c b/citadel/modules/extnotify/funambol65.c index fbba1ed54..bff95fbb4 100644 --- a/citadel/modules/extnotify/funambol65.c +++ b/citadel/modules/extnotify/funambol65.c @@ -47,17 +47,21 @@ #include "event_client.h" #include "extnotify.h" +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, - const char* template, long tlen, +int notify_http_server(char *remoteurl, + const char* template, long tlen, char *user, - char *msgid, - long MsgNum, - NotifyContext *Ctx) + char *msgid, + long MsgNum, + NotifyContext *Ctx) { CURLcode sta; char msgnumstr[128]; @@ -65,7 +69,23 @@ 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)); + + 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; + } snprintf(msgnumstr, 128, "%ld", MsgNum); @@ -78,12 +98,17 @@ int notify_http_server(char *remoteurl, 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); - - CtdlAideMessage(buf, "External notifier unable to find message template!"); + 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; } mimetype = GuessMimeByFilename(template, tlen); @@ -92,86 +117,90 @@ int notify_http_server(char *remoteurl, 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); - CtdlAideMessage(buf, "External notifier unable to load message template!"); + 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); /* 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); - Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, "SOAPAction: \"\""); - Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, contenttype); - Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, "Accept: application/soap+xml, application/mime, multipart/related, text/*"); - Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, "Pragma: no-cache"); + IO->HttpReq.headers = curl_slist_append( + IO->HttpReq.headers, + "SOAPAction: \"\""); + + IO->HttpReq.headers = curl_slist_append( + IO->HttpReq.headers, + contenttype); + + 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 */ - Ctx->HTTPData.PlainPostData = SOAPMessage; - Ctx->HTTPData.PlainPostDataLen = strlen(SOAPMessage); + 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); - Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, "Accept: application/soap+xml, application/mime, multipart/related, text/*"); - Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, "Pragma: no-cache"); - } - ParseURL(&Ctx->HTTPData.URL, NewStrBufPlain (remoteurl, -1), 80); - CurlPrepareURL(Ctx->HTTPData.URL); - int CallBack; - if (! evcurl_init(&Ctx->HTTPData, - Ctx, - "Citadel ExtNotify", - CallBack)) - { - CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n"); - goto abort; + 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"); } - chnd = Ctx->HTTPData.chnd; + + 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); -/* - ReplyBuf = NewStrBuf(); - curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf); - curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback); -*/ - if ( - (!IsEmptyStr(config.c_ip_addr)) - && (strcmp(config.c_ip_addr, "*")) - && (strcmp(config.c_ip_addr, "::")) - && (strcmp(config.c_ip_addr, "0.0.0.0")) - ) { - OPT(INTERFACE, config.c_ip_addr); - } - evcurl_handle_start(&Ctx->HTTPData); + QueueCurlContext(IO); return 0; abort: -/// curl_slist_free_all (headers); -/// curl_easy_cleanup(curl); + if (contenttype) free(contenttype); if (SOAPMessage != NULL) free(SOAPMessage); if (buf != NULL) free(buf); @@ -180,40 +209,56 @@ abort: } - -int EvaluateResult(NotifyContext *Ctx, int res, int b) +eNextState EvaluateResult(AsyncIO *IO) { - if (res) { + + if (IO->HttpReq.httpcode != 200) { StrBuf *ErrMsg; - CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", - res, - Ctx->HTTPData.errdesc); - ErrMsg = NewStrBufPlain(HKEY("Error sending your Notification\n")); - StrBufAppendPrintf(ErrMsg, "\nlibcurl error %d: %s\n", - res, - Ctx->HTTPData.errdesc); -/// StrBufAppendBufPlain(ErrMsg, curl_errbuf, -1, 0); - StrBufAppendBufPlain(ErrMsg, HKEY("\nWas Trying to send: \n"), 0); - StrBufAppendBufPlain(ErrMsg, Ctx->HTTPData.URL->PlainUrl, -1, 0); - if (Ctx->HTTPData.PlainPostDataLen > 0) { - StrBufAppendBufPlain(ErrMsg, HKEY("\nThe Post document was: \n"), 0); - StrBufAppendBufPlain(ErrMsg, - Ctx->HTTPData.PlainPostData, - Ctx->HTTPData.PlainPostDataLen, 0); - StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0); + 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(Ctx->HTTPData.ReplyData) > 0) { - StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThe Serverreply was: \n\n"), 0); - StrBufAppendBuf(ErrMsg, Ctx->HTTPData.ReplyData, 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); + else + StrBufAppendBufPlain( + ErrMsg, + HKEY("\n\nThere was no Serverreply.\n\n"), + 0); ///ExtNotify_PutErrorMessage(Ctx, ErrMsg); - CtdlAideMessage(ChrPtr(ErrMsg), "External notifier unable to load message template!"); + CtdlAideMessage(ChrPtr(ErrMsg), + "External notifier: " + "unable to contact notification host!"); } - CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n"); + syslog(LOG_DEBUG, "Funambol notified\n"); /* while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL)) FreeStrBuf(&Ctx.NotifyHostList[i]); @@ -227,11 +272,13 @@ int EvaluateResult(NotifyContext *Ctx, int res, int b) StrBuf *ErrMsg; It = GetNewHashPos(Ctx.NotifyErrors, 0); - while (GetNextHashPos(Ctx.NotifyErrors, It, &len, &Key, &vErr) && + 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"); + quickie_message("Citadel", NULL, NULL, + AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED, + "Failed to notify external service about inbound mail"); } DeleteHashPos(&It); @@ -247,3 +294,14 @@ int EvaluateResult(NotifyContext *Ctx, int res, int b) ///FreeStrBuf (&ReplyBuf); return 0; } + +eNextState ExtNotifyTerminate(AsyncIO *IO) +{ + free(IO); + return eAbort; +} +eNextState ExtNotifyShutdownAbort(AsyncIO *IO) +{ + free(IO); + return eAbort; +}