3 * @author Mathew McBride
5 * This module facilitates notifications to a Funambol server
8 * Based on bits of the previous serv_funambol
9 * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
11 #include "extnotify.h"
16 #include <sys/socket.h>
18 #include <libcitadel.h>
21 #include <curl/curl.h>
24 #include "citadel_dirs.h"
25 #include "clientsocket.h"
28 #include "sysdep_decls.h"
30 #include "ctdl_module.h"
35 size_t extnotify_callback(void *ptr, size_t size, size_t nmemb, void *stream)
37 return size * nmemb; /* don't care, just discard it so it doesn't end up on the console... */
41 int total_bytes_received;
46 * \brief Sends a message to the Funambol server notifying
47 * of new mail for a user
48 * Returns 0 if unsuccessful
50 int notify_http_server(char *remoteurl,
51 const char* template, long tlen,
63 struct curl_slist * headers=NULL;
64 char errmsg[1024] = "";
65 char *SOAPMessage = NULL;
66 char *contenttype = NULL;
73 curl = curl_easy_init();
75 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
79 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
80 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
81 curl_easy_setopt(curl, CURLOPT_WRITEDATA, fh);
82 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, extnotify_callback); /* don't care..*/
83 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
84 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
86 pchs = strchr(remoteurl, ':');
87 pche = strchr(remoteurl, '@');
90 (pchs < pche) && ((pche - pchs) < SIZ)) {
91 memcpy(userpass, pchs + 3, pche - pchs - 3);
93 userpass[pche - pchs - 3] = '\0';
94 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
95 curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
98 #ifdef CURLOPT_HTTP_CONTENT_DECODING
99 curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
100 curl_easy_setopt(curl, CURLOPT_ENCODING, "");
102 curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
103 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180); /* die after 180 seconds */
104 if (!IsEmptyStr(config.c_ip_addr)) {
105 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
108 headers = curl_slist_append(headers,"Accept: application/soap+xml, application/dime, multipart/related, text/*");
109 headers = curl_slist_append(headers,"Pragma: no-cache");
112 /* Load the template message. Get mallocs done too */
113 FILE *Ftemplate = NULL;
114 const char *mimetype;
116 Ftemplate = fopen(template, "r");
117 if (Ftemplate == NULL) {
121 "Cannot load template file %s [%s]won't send notification\r\n",
122 file_funambol_msg, strerror(errno));
123 CtdlLogPrintf(CTDL_ERR, buf);
125 aide_message(buf, "External notifier unable to find message template!");
128 mimetype = GuessMimeByFilename(template, tlen);
130 snprintf(msgnumstr, 128, "%ld", MsgNum);
134 SOAPMessage = malloc(3072);
135 memset(SOAPMessage, 0, 3072);
137 while(fgets(buf, SIZ, Ftemplate) != NULL) {
138 strcat(SOAPMessage, buf);
142 if (strlen(SOAPMessage) < 0) {
146 "Cannot load template file %s; won't send notification\r\n",
148 CtdlLogPrintf(CTDL_ERR, buf);
150 aide_message(buf, "External notifier unable to load message template!");
154 help_subst(SOAPMessage, "^notifyuser", user);
155 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
156 help_subst(SOAPMessage, "^msgid", msgid);
157 help_subst(SOAPMessage, "^msgnum", msgnumstr);
159 curl_easy_setopt(curl, CURLOPT_URL, remoteurl);
161 /* pass our list of custom made headers */
163 contenttype=(char*) malloc(40+strlen(mimetype));
164 sprintf(contenttype,"Content-type: %s; charset=utf-8", mimetype);
166 headers = curl_slist_append(headers, "SOAPAction: \"\"");
167 headers = curl_slist_append(headers, contenttype);
169 /* Now specify the POST binary data */
171 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, SOAPMessage);
172 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(SOAPMessage));
173 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
176 help_subst(remoteurl, "^notifyuser", user);
177 help_subst(remoteurl, "^syncsource", config.c_funambol_source);
178 help_subst(remoteurl, "^msgid", msgid);
179 help_subst(remoteurl, "^msgnum", msgnumstr);
180 curl_easy_setopt(curl, CURLOPT_URL, remoteurl);
181 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
184 res = curl_easy_perform(curl);
186 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
189 CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
191 curl_slist_free_all (headers);
192 curl_easy_cleanup(curl);
193 if (contenttype) free(contenttype);
194 if (SOAPMessage != NULL) free(SOAPMessage);
195 if (buf != NULL) free(buf);
201 sprintf(port, "%d", config.c_funambol_port);
202 sock = sock_connect(config.c_funambol_host, port, "tcp");
204 CtdlLogPrintf(CTDL_DEBUG, "Connected to Funambol!\n");
209 "Unable to connect to %s:%d [%s]; won't send notification\r\n",
210 config.c_funambol_host,
211 config.c_funambol_port,
213 CtdlLogPrintf(CTDL_ERR, buf);
215 aide_message(buf, "External notifier unable to connect remote host!");
219 // if (funambolCreds != NULL) free(funambolCreds);
220 //if (SOAPHeader != NULL) free(SOAPHeader);
223 /* Build the HTTP request header */
227 sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
229 strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
230 sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
233 config.c_funambol_host,
234 config.c_funambol_port
236 strcat(SOAPHeader,buf);
237 strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
238 sprintf(buf, "Content-Length: %d \r\n",
239 strlen(SOAPMessage));
240 strcat(SOAPHeader, buf);
243 /* funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
244 memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
245 CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
246 sprintf(buf, "Authorization: Basic %s\r\n\r\n",
248 strcat(SOAPHeader, buf);
250 sock_write(sock, SOAPHeader, strlen(SOAPHeader));
251 sock_write(sock, SOAPMessage, strlen(SOAPMessage));
252 sock_shutdown(sock, SHUT_WR);
255 CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
256 if (sock_getln(sock, buf, SIZ) < 0) {
259 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
260 if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {