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 */
99 const char *Err = NULL;
101 fd = open(template, O_RDONLY);
103 (fstat(fd, &statbuf) == -1))
108 "Cannot load template file %s [%s] "
109 "won't send notification\r\n",
112 syslog(LOG_ERR, "%s", buf);
113 // TODO: once an hour!
116 "External notifier: "
117 "unable to find/stat message template!");
121 Buf = NewStrBufPlain(NULL, statbuf.st_size + 1);
122 if (StrBufReadBLOB(Buf, &fd, 1, statbuf.st_size, &Err) < 0) {
128 "Cannot load template file %s [%s] "
129 "won't send notification\r\n",
132 syslog(LOG_ERR, "%s", buf);
133 // TODO: once an hour!
136 "External notifier: "
137 "unable to load message template!");
142 mimetype = GuessMimeByFilename(template, tlen);
144 SOAPMessage = SmashStrBuf(&Buf);
147 help_subst(SOAPMessage, "^notifyuser", user);
148 help_subst(SOAPMessage, "^syncsource",
149 config.c_funambol_source);
150 help_subst(SOAPMessage, "^msgid", msgid);
151 help_subst(SOAPMessage, "^msgnum", msgnumstr);
153 /* pass our list of custom made headers */
155 contenttype=(char*) malloc(40+strlen(mimetype));
157 "Content-Type: %s; charset=utf-8",
160 IO->HttpReq.headers = curl_slist_append(
164 IO->HttpReq.headers = curl_slist_append(
169 IO->HttpReq.headers = curl_slist_append(
171 "Accept: application/soap+xml, "
172 "application/mime, multipart/related, text/*");
174 IO->HttpReq.headers = curl_slist_append(
178 /* Now specify the POST binary data */
179 IO->HttpReq.PlainPostData = SOAPMessage;
180 IO->HttpReq.PlainPostDataLen = strlen(SOAPMessage);
183 help_subst(remoteurl, "^notifyuser", user);
184 help_subst(remoteurl, "^syncsource", config.c_funambol_source);
185 help_subst(remoteurl, "^msgid", msgid);
186 help_subst(remoteurl, "^msgnum", msgnumstr);
188 IO->HttpReq.headers = curl_slist_append(
190 "Accept: application/soap+xml, "
191 "application/mime, multipart/related, text/*");
193 IO->HttpReq.headers = curl_slist_append(
198 Buf = NewStrBufPlain (remoteurl, -1);
199 ParseURL(&IO->ConnectMe, Buf, 80);
200 FreeStrBuf(&Buf); /* TODO: this is uncool... */
201 CurlPrepareURL(IO->ConnectMe);
203 chnd = IO->HttpReq.chnd;
204 OPT(SSL_VERIFYPEER, 0);
205 OPT(SSL_VERIFYHOST, 0);
207 QueueCurlContext(IO);
212 if (contenttype) free(contenttype);
213 if (SOAPMessage != NULL) free(SOAPMessage);
214 if (buf != NULL) free(buf);
215 FreeStrBuf (&ReplyBuf);
220 eNextState EvaluateResult(AsyncIO *IO)
223 if (IO->HttpReq.httpcode != 200) {
226 syslog(LOG_ALERT, "libcurl error %ld: %s\n",
227 IO->HttpReq.httpcode,
228 IO->HttpReq.errdesc);
230 ErrMsg = NewStrBufPlain(
231 HKEY("Error sending your Notification\n"));
232 StrBufAppendPrintf(ErrMsg, "\nlibcurl error %ld: \n\t\t%s\n",
233 IO->HttpReq.httpcode,
234 IO->HttpReq.errdesc);
236 StrBufAppendBufPlain(ErrMsg,
237 HKEY("\nWas Trying to send: \n"),
240 StrBufAppendBufPlain(ErrMsg, IO->ConnectMe->PlainUrl, -1, 0);
241 if (IO->HttpReq.PlainPostDataLen > 0) {
242 StrBufAppendBufPlain(
244 HKEY("\nThe Post document was: \n"),
246 StrBufAppendBufPlain(ErrMsg,
247 IO->HttpReq.PlainPostData,
248 IO->HttpReq.PlainPostDataLen, 0);
249 StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);
251 if (StrLength(IO->HttpReq.ReplyData) > 0) {
252 StrBufAppendBufPlain(
254 HKEY("\n\nThe Serverreply was: \n\n"),
256 StrBufAppendBuf(ErrMsg, IO->HttpReq.ReplyData, 0);
259 StrBufAppendBufPlain(
261 HKEY("\n\nThere was no Serverreply.\n\n"),
263 ///ExtNotify_PutErrorMessage(Ctx, ErrMsg);
264 CtdlAideMessage(ChrPtr(ErrMsg),
265 "External notifier: "
266 "unable to contact notification host!");
269 syslog(LOG_DEBUG, "Funambol notified\n");
271 while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
272 FreeStrBuf(&Ctx.NotifyHostList[i]);
274 if (Ctx.NotifyErrors != NULL)
282 It = GetNewHashPos(Ctx.NotifyErrors, 0);
283 while (GetNextHashPos(Ctx.NotifyErrors,
284 It, &len, &Key, &vErr) &&
286 ErrMsg = (StrBuf*) vErr;
287 quickie_message("Citadel", NULL, NULL,
288 AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
289 "Failed to notify external service about inbound mail");
293 DeleteHash(&Ctx.NotifyErrors);
297 //// curl_slist_free_all (headers);
298 /// curl_easy_cleanup(curl);
299 ///if (contenttype) free(contenttype);
300 ///if (SOAPMessage != NULL) free(SOAPMessage);
301 ///if (buf != NULL) free(buf);
302 ///FreeStrBuf (&ReplyBuf);
306 eNextState ExtNotifyTerminateDB(AsyncIO *IO)
311 eNextState ExtNotifyTerminate(AsyncIO *IO)
316 eNextState ExtNotifyShutdownAbort(AsyncIO *IO)