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 * Copyright (c) 2008-2010
13 * This program is open source software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 3.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
31 #include <sys/socket.h>
33 #include <libcitadel.h>
36 #include <curl/curl.h>
39 #include "citadel_dirs.h"
40 #include "clientsocket.h"
43 #include "sysdep_decls.h"
45 #include "ctdl_module.h"
47 #include "event_client.h"
48 #include "extnotify.h"
50 eNextState EvaluateResult(AsyncIO *IO);
51 eNextState ExtNotifyTerminate(AsyncIO *IO);
52 eNextState ExtNotifyTerminateDB(AsyncIO *IO);
53 eNextState ExtNotifyShutdownAbort(AsyncIO *IO);
56 * \brief Sends a message to the Funambol server notifying
57 * of new mail for a user
58 * Returns 0 if unsuccessful
60 int notify_http_server(char *remoteurl,
61 const char* template, long tlen,
70 char *SOAPMessage = NULL;
71 char *contenttype = NULL;
77 IO = (AsyncIO*) malloc(sizeof(AsyncIO));
78 memset(IO, 0, sizeof(AsyncIO));
80 if (! InitcURLIOStruct(IO,
81 NULL, /* we don't have personal data anymore. */
86 ExtNotifyShutdownAbort))
88 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
92 snprintf(msgnumstr, 128, "%ld", MsgNum);
95 /* Load the template message. Get mallocs done too */
96 FILE *Ftemplate = NULL;
99 Ftemplate = fopen(template, "r");
100 if (Ftemplate == NULL) {
104 "Cannot load template file %s [%s] "
105 "won't send notification\r\n",
108 syslog(LOG_ERR, "%s", buf);
109 // TODO: once an hour!
112 "External notifier: "
113 "unable to find message template!");
116 mimetype = GuessMimeByFilename(template, tlen);
120 SOAPMessage = malloc(3072);
121 memset(SOAPMessage, 0, 3072);
123 while(fgets(buf, SIZ, Ftemplate) != NULL) {
124 strcat(SOAPMessage, buf);
128 if (strlen(SOAPMessage) < 0) {
132 "Cannot load template file %s;"
133 " won't send notification\r\n",
135 syslog(LOG_ERR, "%s", buf);
137 CtdlAideMessage(buf, "External notifier: "
138 "unable to load message template!");
142 help_subst(SOAPMessage, "^notifyuser", user);
143 help_subst(SOAPMessage, "^syncsource",
144 config.c_funambol_source);
145 help_subst(SOAPMessage, "^msgid", msgid);
146 help_subst(SOAPMessage, "^msgnum", msgnumstr);
148 /* pass our list of custom made headers */
150 contenttype=(char*) malloc(40+strlen(mimetype));
152 "Content-Type: %s; charset=utf-8",
155 IO->HttpReq.headers = curl_slist_append(
159 IO->HttpReq.headers = curl_slist_append(
163 IO->HttpReq.headers = curl_slist_append(
165 "Accept: application/soap+xml, "
166 "application/mime, multipart/related, text/*");
168 IO->HttpReq.headers = curl_slist_append(
172 /* Now specify the POST binary data */
173 IO->HttpReq.PlainPostData = SOAPMessage;
174 IO->HttpReq.PlainPostDataLen = strlen(SOAPMessage);
177 help_subst(remoteurl, "^notifyuser", user);
178 help_subst(remoteurl, "^syncsource", config.c_funambol_source);
179 help_subst(remoteurl, "^msgid", msgid);
180 help_subst(remoteurl, "^msgnum", msgnumstr);
182 IO->HttpReq.headers = curl_slist_append(
184 "Accept: application/soap+xml, "
185 "application/mime, multipart/related, text/*");
187 IO->HttpReq.headers = curl_slist_append(
192 Buf = NewStrBufPlain (remoteurl, -1);
193 ParseURL(&IO->ConnectMe, Buf, 80);
194 FreeStrBuf(&Buf); /* TODO: this is uncool... */
195 CurlPrepareURL(IO->ConnectMe);
197 chnd = IO->HttpReq.chnd;
198 OPT(SSL_VERIFYPEER, 0);
199 OPT(SSL_VERIFYHOST, 0);
201 QueueCurlContext(IO);
206 if (contenttype) free(contenttype);
207 if (SOAPMessage != NULL) free(SOAPMessage);
208 if (buf != NULL) free(buf);
209 FreeStrBuf (&ReplyBuf);
214 eNextState EvaluateResult(AsyncIO *IO)
217 if (IO->HttpReq.httpcode != 200) {
220 syslog(LOG_ALERT, "libcurl error %ld: %s\n",
221 IO->HttpReq.httpcode,
222 IO->HttpReq.errdesc);
224 ErrMsg = NewStrBufPlain(
225 HKEY("Error sending your Notification\n"));
226 StrBufAppendPrintf(ErrMsg, "\nlibcurl error %ld: \n\t\t%s\n",
227 IO->HttpReq.httpcode,
228 IO->HttpReq.errdesc);
230 StrBufAppendBufPlain(ErrMsg,
231 HKEY("\nWas Trying to send: \n"),
234 StrBufAppendBufPlain(ErrMsg, IO->ConnectMe->PlainUrl, -1, 0);
235 if (IO->HttpReq.PlainPostDataLen > 0) {
236 StrBufAppendBufPlain(
238 HKEY("\nThe Post document was: \n"),
240 StrBufAppendBufPlain(ErrMsg,
241 IO->HttpReq.PlainPostData,
242 IO->HttpReq.PlainPostDataLen, 0);
243 StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);
245 if (StrLength(IO->HttpReq.ReplyData) > 0) {
246 StrBufAppendBufPlain(
248 HKEY("\n\nThe Serverreply was: \n\n"),
250 StrBufAppendBuf(ErrMsg, IO->HttpReq.ReplyData, 0);
253 StrBufAppendBufPlain(
255 HKEY("\n\nThere was no Serverreply.\n\n"),
257 ///ExtNotify_PutErrorMessage(Ctx, ErrMsg);
258 CtdlAideMessage(ChrPtr(ErrMsg),
259 "External notifier: "
260 "unable to contact notification host!");
263 syslog(LOG_DEBUG, "Funambol notified\n");
265 while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
266 FreeStrBuf(&Ctx.NotifyHostList[i]);
268 if (Ctx.NotifyErrors != NULL)
276 It = GetNewHashPos(Ctx.NotifyErrors, 0);
277 while (GetNextHashPos(Ctx.NotifyErrors,
278 It, &len, &Key, &vErr) &&
280 ErrMsg = (StrBuf*) vErr;
281 quickie_message("Citadel", NULL, NULL,
282 AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
283 "Failed to notify external service about inbound mail");
287 DeleteHash(&Ctx.NotifyErrors);
291 //// curl_slist_free_all (headers);
292 /// curl_easy_cleanup(curl);
293 ///if (contenttype) free(contenttype);
294 ///if (SOAPMessage != NULL) free(SOAPMessage);
295 ///if (buf != NULL) free(buf);
296 ///FreeStrBuf (&ReplyBuf);
300 eNextState ExtNotifyTerminateDB(AsyncIO *IO)
305 eNextState ExtNotifyTerminate(AsyncIO *IO)
310 eNextState ExtNotifyShutdownAbort(AsyncIO *IO)