From c4a1f6429ba1ace4e5cbcf2bb50d8f4f05bc45b1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Wilfried=20G=C3=B6esgens?= Date: Sat, 27 Jun 2009 13:55:18 +0000 Subject: [PATCH] * migrate extnotify to libcurl * start abstracting stuff so other notefications than funambol can be sent --- citadel/modules/extnotify/extnotify.h | 10 +- citadel/modules/extnotify/extnotify_main.c | 27 ++- citadel/modules/extnotify/funambol65.c | 242 +++++++++++++++------ 3 files changed, 202 insertions(+), 77 deletions(-) diff --git a/citadel/modules/extnotify/extnotify.h b/citadel/modules/extnotify/extnotify.h index a88de1d6a..2d1874f27 100644 --- a/citadel/modules/extnotify/extnotify.h +++ b/citadel/modules/extnotify/extnotify.h @@ -11,11 +11,17 @@ extern "C" { #define FUNAMBOL_CONFIG_TEXT "funambol" #define PAGER_CONFIG_MESSAGE "__ Push email settings __" -#define PAGER_CONFIG_TEXT "textmessage" +#define PAGER_CONFIG_SYSTEM "textmessage" +#define PAGER_CONFIG_HTTP "httpmessage" #define FUNAMBOL_WS "/funambol/services/admin" -int notify_funambol_server(char *user, char *msgid, long MsgNum); +int notify_http_server(char *remoteurl, + char* template, + long tlen, + char *user, + char *msgid, + long MsgNum); void extNotify_getPrefs(long configMsgNum, char *configMsg); long extNotify_getConfigMessage(char *username); diff --git a/citadel/modules/extnotify/extnotify_main.c b/citadel/modules/extnotify/extnotify_main.c index f87c392c0..0e2731d1a 100644 --- a/citadel/modules/extnotify/extnotify_main.c +++ b/citadel/modules/extnotify/extnotify_main.c @@ -131,13 +131,32 @@ void process_notify(long msgnum, void *usrdata) { goto nuke; } // Can Funambol take the message? - int fnblAllowed = strncasecmp(configMsg, FUNAMBOL_CONFIG_TEXT, strlen(FUNAMBOL_CONFIG_TEXT)); - int extPagerAllowed = strncasecmp(configMsg, PAGER_CONFIG_TEXT, strlen(PAGER_CONFIG_TEXT)); + int fnblAllowed = strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT)); + int extPagerAllowedHttp = strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP)); + int extPagerAllowedSystem = strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM)); if (fnblAllowed == 0) { - notify_funambol_server(msg->cm_fields['W'], + char remoteurl[SIZ]; + snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s", + config.c_funambol_auth, + config.c_funambol_host, + config.c_funambol_port, + FUNAMBOL_WS); + notify_http_server(remoteurl, + file_funambol_msg, + strlen(file_funambol_msg),/*GNA*/ + msg->cm_fields['W'], msg->cm_fields['I'], msgnum); - } else if (extPagerAllowed == 0) { + } else if (extPagerAllowedHttp) { +/* + notify_http_server(remoteurl, + file_funambol_msg, + strlen(file_funambol_msg),/ *GNA* / + msg->cm_fields['W'], + msg->cm_fields['I'], + msgnum); +*/ + } else if (extPagerAllowedSystem == 0) { char *number = strtok(configMsg, "textmessage\n"); int commandSiz = sizeof(config.c_pager_program) + strlen(number) + strlen(msg->cm_fields['W']) + 5; char *command = malloc(commandSiz); diff --git a/citadel/modules/extnotify/funambol65.c b/citadel/modules/extnotify/funambol65.c index 2c37b67da..af46df28c 100644 --- a/citadel/modules/extnotify/funambol65.c +++ b/citadel/modules/extnotify/funambol65.c @@ -18,6 +18,7 @@ #include #include #include +#include #include "citadel.h" #include "citadel_dirs.h" @@ -28,21 +29,174 @@ #include "msgbase.h" #include "ctdl_module.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; +}; + /* * \brief Sends a message to the Funambol server notifying * of new mail for a user * Returns 0 if unsuccessful */ -int notify_funambol_server(char *user, char *msgid, long MsgNum) { - char port[1024]; +int notify_http_server(char *remoteurl, + char* template, long tlen, + char *user, + char *msgid, + long MsgNum) +{ + char *pchs, *pche; + char userpass[SIZ]; + char retbuf[SIZ]; char msgnumstr[128]; - int sock = -1; 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; + 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); + } + + headers = curl_slist_append(headers,"Accept: application/soap+xml, application/dime, multipart/related, text/*"); + headers = curl_slist_append(headers,"Pragma: no-cache"); + + if (tlen > 0) { + /* Load the template message. Get mallocs done too */ + FILE *Ftemplate = NULL; + const char *mimetype; + + Ftemplate = fopen(template, "r"); + mimetype = GuessMimeByFilename(template, tlen); + 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; + } + snprintf(msgnumstr, 128, "%ld", MsgNum); + + 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", + 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 */ + + contenttype=(char*) malloc(40+strlen(mimetype)); + sprintf(contenttype,"Content-type: %s; charset=utf-8", mimetype); + + 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); + } + + res = curl_easy_perform(curl); + if (res) { + CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg); + } + + CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n"); +free: + curl_slist_free_all (headers); + curl_easy_cleanup(curl); + if (contenttype) free(contenttype); + if (SOAPMessage != NULL) free(SOAPMessage); + if (buf != NULL) free(buf); + return 0; +} + + +/* sprintf(port, "%d", config.c_funambol_port); sock = sock_connect(config.c_funambol_host, port, "tcp"); if (sock >= 0) @@ -60,58 +214,15 @@ int notify_funambol_server(char *user, char *msgid, long MsgNum) { aide_message(buf, "External notifier unable to connect remote host!"); goto bail; } - // 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; - } - snprintf(msgnumstr, 128, "%ld", MsgNum); - - 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) { - 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); +*/ +// if (funambolCreds != NULL) free(funambolCreds); + //if (SOAPHeader != NULL) free(SOAPHeader); + ///close(sock); /* 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"); @@ -126,12 +237,11 @@ int notify_funambol_server(char *user, char *msgid, long MsgNum) { sprintf(buf, "Content-Length: %d \r\n", strlen(SOAPMessage)); strcat(SOAPHeader, buf); +*/ - - - CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0); - - +/* 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); @@ -140,7 +250,7 @@ int notify_funambol_server(char *user, char *msgid, long MsgNum) { sock_write(sock, SOAPMessage, strlen(SOAPMessage)); sock_shutdown(sock, SHUT_WR); - /* Response */ + / * Response * / CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n"); if (sock_getln(sock, buf, SIZ) < 0) { goto free; @@ -150,14 +260,4 @@ int notify_funambol_server(char *user, char *msgid, long MsgNum) { goto free; } - 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); - return 0; -} - +*/ -- 2.30.2