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 "citserver.h"
40 #include "citadel_dirs.h"
41 #include "clientsocket.h"
44 #include "sysdep_decls.h"
46 #include "ctdl_module.h"
48 #include "event_client.h"
49 #include "extnotify.h"
51 eNextState EvaluateResult(AsyncIO *IO);
52 eNextState ExtNotifyTerminate(AsyncIO *IO);
53 eNextState ExtNotifyTerminateDB(AsyncIO *IO);
54 eNextState ExtNotifyShutdownAbort(AsyncIO *IO);
57 * \brief Sends a message to the Funambol server notifying
58 * of new mail for a user
59 * Returns 0 if unsuccessful
61 int notify_http_server(char *remoteurl,
62 const char* template, long tlen,
71 char *SOAPMessage = NULL;
72 char *contenttype = NULL;
78 IO = (AsyncIO*) malloc(sizeof(AsyncIO));
79 memset(IO, 0, sizeof(AsyncIO));
81 if (! InitcURLIOStruct(IO,
82 NULL, /* we don't have personal data anymore. */
87 ExtNotifyShutdownAbort))
89 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
93 snprintf(msgnumstr, 128, "%ld", MsgNum);
96 /* Load the template message. Get mallocs done too */
100 const char *Err = NULL;
102 fd = open(template, O_RDONLY);
104 (fstat(fd, &statbuf) == -1))
109 "Cannot load template file %s [%s] "
110 "won't send notification\r\n",
113 syslog(LOG_ERR, "%s", buf);
114 // TODO: once an hour!
117 "External notifier: "
118 "unable to find/stat message template!");
122 Buf = NewStrBufPlain(NULL, statbuf.st_size + 1);
123 if (StrBufReadBLOB(Buf, &fd, 1, statbuf.st_size, &Err) < 0) {
129 "Cannot load template file %s [%s] "
130 "won't send notification\r\n",
133 syslog(LOG_ERR, "%s", buf);
134 // TODO: once an hour!
137 "External notifier: "
138 "unable to load message template!");
143 mimetype = GuessMimeByFilename(template, tlen);
145 SOAPMessage = SmashStrBuf(&Buf);
148 help_subst(SOAPMessage, "^notifyuser", user);
149 help_subst(SOAPMessage, "^syncsource",
150 config.c_funambol_source);
151 help_subst(SOAPMessage, "^msgid", msgid);
152 help_subst(SOAPMessage, "^msgnum", msgnumstr);
154 /* pass our list of custom made headers */
156 contenttype=(char*) malloc(40+strlen(mimetype));
158 "Content-Type: %s; charset=utf-8",
161 IO->HttpReq.headers = curl_slist_append(
165 IO->HttpReq.headers = curl_slist_append(
170 IO->HttpReq.headers = curl_slist_append(
172 "Accept: application/soap+xml, "
173 "application/mime, multipart/related, text/*");
175 IO->HttpReq.headers = curl_slist_append(
179 /* Now specify the POST binary data */
180 IO->HttpReq.PlainPostData = SOAPMessage;
181 IO->HttpReq.PlainPostDataLen = strlen(SOAPMessage);
184 help_subst(remoteurl, "^notifyuser", user);
185 help_subst(remoteurl, "^syncsource", config.c_funambol_source);
186 help_subst(remoteurl, "^msgid", msgid);
187 help_subst(remoteurl, "^msgnum", msgnumstr);
189 IO->HttpReq.headers = curl_slist_append(
191 "Accept: application/soap+xml, "
192 "application/mime, multipart/related, text/*");
194 IO->HttpReq.headers = curl_slist_append(
199 Buf = NewStrBufPlain (remoteurl, -1);
200 ParseURL(&IO->ConnectMe, Buf, 80);
201 FreeStrBuf(&Buf); /* TODO: this is uncool... */
202 CurlPrepareURL(IO->ConnectMe);
204 chnd = IO->HttpReq.chnd;
205 OPT(SSL_VERIFYPEER, 0);
206 OPT(SSL_VERIFYHOST, 0);
208 QueueCurlContext(IO);
213 if (contenttype) free(contenttype);
214 if (SOAPMessage != NULL) free(SOAPMessage);
215 if (buf != NULL) free(buf);
216 FreeStrBuf (&ReplyBuf);
221 eNextState EvaluateResult(AsyncIO *IO)
224 if (IO->HttpReq.httpcode != 200) {
227 syslog(LOG_ALERT, "libcurl error %ld: %s\n",
228 IO->HttpReq.httpcode,
229 IO->HttpReq.errdesc);
231 ErrMsg = NewStrBufPlain(
232 HKEY("Error sending your Notification\n"));
233 StrBufAppendPrintf(ErrMsg, "\nlibcurl error %ld: \n\t\t%s\n",
234 IO->HttpReq.httpcode,
235 IO->HttpReq.errdesc);
237 StrBufAppendBufPlain(ErrMsg,
238 HKEY("\nWas Trying to send: \n"),
241 StrBufAppendBufPlain(ErrMsg, IO->ConnectMe->PlainUrl, -1, 0);
242 if (IO->HttpReq.PlainPostDataLen > 0) {
243 StrBufAppendBufPlain(
245 HKEY("\nThe Post document was: \n"),
247 StrBufAppendBufPlain(ErrMsg,
248 IO->HttpReq.PlainPostData,
249 IO->HttpReq.PlainPostDataLen, 0);
250 StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);
252 if (StrLength(IO->HttpReq.ReplyData) > 0) {
253 StrBufAppendBufPlain(
255 HKEY("\n\nThe Serverreply was: \n\n"),
257 StrBufAppendBuf(ErrMsg, IO->HttpReq.ReplyData, 0);
260 StrBufAppendBufPlain(
262 HKEY("\n\nThere was no Serverreply.\n\n"),
264 ///ExtNotify_PutErrorMessage(Ctx, ErrMsg);
265 CtdlAideMessage(ChrPtr(ErrMsg),
266 "External notifier: "
267 "unable to contact notification host!");
271 syslog(LOG_DEBUG, "Funambol notified\n");
273 while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
274 FreeStrBuf(&Ctx.NotifyHostList[i]);
276 if (Ctx.NotifyErrors != NULL)
284 It = GetNewHashPos(Ctx.NotifyErrors, 0);
285 while (GetNextHashPos(Ctx.NotifyErrors,
286 It, &len, &Key, &vErr) &&
288 ErrMsg = (StrBuf*) vErr;
289 quickie_message("Citadel", NULL, NULL,
290 AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
291 "Failed to notify external service about inbound mail");
295 DeleteHash(&Ctx.NotifyErrors);
299 //// curl_slist_free_all (headers);
300 /// curl_easy_cleanup(curl);
301 ///if (contenttype) free(contenttype);
302 ///if (SOAPMessage != NULL) free(SOAPMessage);
303 ///if (buf != NULL) free(buf);
304 ///FreeStrBuf (&ReplyBuf);
305 return eTerminateConnection;
308 eNextState ExtNotifyTerminateDB(AsyncIO *IO)
313 eNextState ExtNotifyTerminate(AsyncIO *IO)
318 eNextState ExtNotifyShutdownAbort(AsyncIO *IO)