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 free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
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.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 ExtNotifyShutdownAbort(AsyncIO *IO);
55 * \brief Sends a message to the Funambol server notifying
56 * of new mail for a user
57 * Returns 0 if unsuccessful
59 int notify_http_server(char *remoteurl,
60 const char* template, long tlen,
69 char *SOAPMessage = NULL;
70 char *contenttype = NULL;
76 IO = (AsyncIO*) malloc(sizeof(AsyncIO));
77 memset(IO, 0, sizeof(AsyncIO));
79 if (! InitcURLIOStruct(IO,
80 NULL, /* we don't have personal data anymore. */
84 ExtNotifyShutdownAbort))
86 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
90 snprintf(msgnumstr, 128, "%ld", MsgNum);
93 /* Load the template message. Get mallocs done too */
94 FILE *Ftemplate = NULL;
97 Ftemplate = fopen(template, "r");
98 if (Ftemplate == NULL) {
102 "Cannot load template file %s [%s] "
103 "won't send notification\r\n",
106 syslog(LOG_ERR, "%s", buf);
107 // TODO: once an hour!
110 "External notifier: "
111 "unable to find message template!");
114 mimetype = GuessMimeByFilename(template, tlen);
118 SOAPMessage = malloc(3072);
119 memset(SOAPMessage, 0, 3072);
121 while(fgets(buf, SIZ, Ftemplate) != NULL) {
122 strcat(SOAPMessage, buf);
126 if (strlen(SOAPMessage) < 0) {
130 "Cannot load template file %s;"
131 " won't send notification\r\n",
133 syslog(LOG_ERR, "%s", buf);
135 CtdlAideMessage(buf, "External notifier: "
136 "unable to load message template!");
140 help_subst(SOAPMessage, "^notifyuser", user);
141 help_subst(SOAPMessage, "^syncsource",
142 config.c_funambol_source);
143 help_subst(SOAPMessage, "^msgid", msgid);
144 help_subst(SOAPMessage, "^msgnum", msgnumstr);
146 /* pass our list of custom made headers */
148 contenttype=(char*) malloc(40+strlen(mimetype));
150 "Content-Type: %s; charset=utf-8",
153 IO->HttpReq.headers = curl_slist_append(
157 IO->HttpReq.headers = curl_slist_append(
161 IO->HttpReq.headers = curl_slist_append(
163 "Accept: application/soap+xml, "
164 "application/mime, multipart/related, text/*");
166 IO->HttpReq.headers = curl_slist_append(
170 /* Now specify the POST binary data */
171 IO->HttpReq.PlainPostData = SOAPMessage;
172 IO->HttpReq.PlainPostDataLen = strlen(SOAPMessage);
175 help_subst(remoteurl, "^notifyuser", user);
176 help_subst(remoteurl, "^syncsource", config.c_funambol_source);
177 help_subst(remoteurl, "^msgid", msgid);
178 help_subst(remoteurl, "^msgnum", msgnumstr);
180 IO->HttpReq.headers = curl_slist_append(
182 "Accept: application/soap+xml, "
183 "application/mime, multipart/related, text/*");
185 IO->HttpReq.headers = curl_slist_append(
190 Buf = NewStrBufPlain (remoteurl, -1);
191 ParseURL(&IO->ConnectMe, Buf, 80);
192 FreeStrBuf(&Buf); /* TODO: this is uncool... */
193 CurlPrepareURL(IO->ConnectMe);
195 chnd = IO->HttpReq.chnd;
196 OPT(SSL_VERIFYPEER, 0);
197 OPT(SSL_VERIFYHOST, 0);
199 QueueCurlContext(IO);
204 if (contenttype) free(contenttype);
205 if (SOAPMessage != NULL) free(SOAPMessage);
206 if (buf != NULL) free(buf);
207 FreeStrBuf (&ReplyBuf);
212 eNextState EvaluateResult(AsyncIO *IO)
215 if (IO->HttpReq.httpcode != 200) {
218 syslog(LOG_ALERT, "libcurl error %ld: %s\n",
219 IO->HttpReq.httpcode,
220 IO->HttpReq.errdesc);
222 ErrMsg = NewStrBufPlain(
223 HKEY("Error sending your Notification\n"));
224 StrBufAppendPrintf(ErrMsg, "\nlibcurl error %ld: \n\t\t%s\n",
225 IO->HttpReq.httpcode,
226 IO->HttpReq.errdesc);
228 StrBufAppendBufPlain(ErrMsg,
229 HKEY("\nWas Trying to send: \n"),
232 StrBufAppendBufPlain(ErrMsg, IO->ConnectMe->PlainUrl, -1, 0);
233 if (IO->HttpReq.PlainPostDataLen > 0) {
234 StrBufAppendBufPlain(
236 HKEY("\nThe Post document was: \n"),
238 StrBufAppendBufPlain(ErrMsg,
239 IO->HttpReq.PlainPostData,
240 IO->HttpReq.PlainPostDataLen, 0);
241 StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);
243 if (StrLength(IO->HttpReq.ReplyData) > 0) {
244 StrBufAppendBufPlain(
246 HKEY("\n\nThe Serverreply was: \n\n"),
248 StrBufAppendBuf(ErrMsg, IO->HttpReq.ReplyData, 0);
251 StrBufAppendBufPlain(
253 HKEY("\n\nThere was no Serverreply.\n\n"),
255 ///ExtNotify_PutErrorMessage(Ctx, ErrMsg);
256 CtdlAideMessage(ChrPtr(ErrMsg),
257 "External notifier: "
258 "unable to contact notification host!");
261 syslog(LOG_DEBUG, "Funambol notified\n");
263 while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
264 FreeStrBuf(&Ctx.NotifyHostList[i]);
266 if (Ctx.NotifyErrors != NULL)
274 It = GetNewHashPos(Ctx.NotifyErrors, 0);
275 while (GetNextHashPos(Ctx.NotifyErrors,
276 It, &len, &Key, &vErr) &&
278 ErrMsg = (StrBuf*) vErr;
279 quickie_message("Citadel", NULL, NULL,
280 AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
281 "Failed to notify external service about inbound mail");
285 DeleteHash(&Ctx.NotifyErrors);
289 //// curl_slist_free_all (headers);
290 /// curl_easy_cleanup(curl);
291 ///if (contenttype) free(contenttype);
292 ///if (SOAPMessage != NULL) free(SOAPMessage);
293 ///if (buf != NULL) free(buf);
294 ///FreeStrBuf (&ReplyBuf);
298 eNextState ExtNotifyTerminate(AsyncIO *IO)
303 eNextState ExtNotifyShutdownAbort(AsyncIO *IO)