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-2015
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.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
25 #include <sys/socket.h>
27 #include <libcitadel.h>
30 #include <curl/curl.h>
33 #include "citserver.h"
34 #include "citadel_dirs.h"
35 #include "clientsocket.h"
38 #include "sysdep_decls.h"
40 #include "ctdl_module.h"
42 #include "event_client.h"
43 #include "extnotify.h"
45 eNextState EvaluateResult(AsyncIO *IO);
46 eNextState ExtNotifyTerminate(AsyncIO *IO);
47 eNextState ExtNotifyTerminateDB(AsyncIO *IO);
48 eNextState ExtNotifyShutdownAbort(AsyncIO *IO);
51 * \brief Sends a message to the Funambol server notifying
52 * of new mail for a user
53 * Returns 0 if unsuccessful
55 int notify_http_server(char *remoteurl,
56 const char* template, long tlen,
65 char *SOAPMessage = NULL;
66 char *contenttype = NULL;
72 IO = (AsyncIO*) malloc(sizeof(AsyncIO));
73 memset(IO, 0, sizeof(AsyncIO));
75 if (! InitcURLIOStruct(IO,
76 NULL, /* we don't have personal data anymore. */
81 ExtNotifyShutdownAbort))
83 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
87 snprintf(msgnumstr, 128, "%ld", MsgNum);
90 /* Load the template message. Get mallocs done too */
94 const char *Err = NULL;
96 fd = open(template, O_RDONLY);
98 (fstat(fd, &statbuf) == -1))
103 "Cannot load template file %s [%s] "
104 "won't send notification\r\n",
107 syslog(LOG_ERR, "%s", buf);
108 // TODO: once an hour!
111 "External notifier: "
112 "unable to find/stat message template!");
116 Buf = NewStrBufPlain(NULL, statbuf.st_size + 1);
117 if (StrBufReadBLOB(Buf, &fd, 1, statbuf.st_size, &Err) < 0) {
123 "Cannot load template file %s [%s] "
124 "won't send notification\r\n",
127 syslog(LOG_ERR, "%s", buf);
128 // TODO: once an hour!
131 "External notifier: "
132 "unable to load message template!");
137 mimetype = GuessMimeByFilename(template, tlen);
139 SOAPMessage = SmashStrBuf(&Buf);
142 help_subst(SOAPMessage, "^notifyuser", user);
143 help_subst(SOAPMessage, "^syncsource", CtdlGetConfigStr("c_funambol_source"));
144 help_subst(SOAPMessage, "^msgid", msgid);
145 help_subst(SOAPMessage, "^msgnum", msgnumstr);
147 /* pass our list of custom made headers */
149 contenttype=(char*) malloc(40+strlen(mimetype));
151 "Content-Type: %s; charset=utf-8",
154 IO->HttpReq.headers = curl_slist_append(
158 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", CtdlGetConfigStr("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!");
264 syslog(LOG_DEBUG, "Funambol notified\n");
266 while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
267 FreeStrBuf(&Ctx.NotifyHostList[i]);
269 if (Ctx.NotifyErrors != NULL)
277 It = GetNewHashPos(Ctx.NotifyErrors, 0);
278 while (GetNextHashPos(Ctx.NotifyErrors,
279 It, &len, &Key, &vErr) &&
281 ErrMsg = (StrBuf*) vErr;
282 quickie_message("Citadel", NULL, NULL,
283 AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
284 "Failed to notify external service about inbound mail");
288 DeleteHash(&Ctx.NotifyErrors);
292 //// curl_slist_free_all (headers);
293 /// curl_easy_cleanup(curl);
294 ///if (contenttype) free(contenttype);
295 ///if (SOAPMessage != NULL) free(SOAPMessage);
296 ///if (buf != NULL) free(buf);
297 ///FreeStrBuf (&ReplyBuf);
298 return eTerminateConnection;
301 eNextState ExtNotifyTerminateDB(AsyncIO *IO)
306 eNextState ExtNotifyTerminate(AsyncIO *IO)
311 eNextState ExtNotifyShutdownAbort(AsyncIO *IO)