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));
78 IO->CitContext = CloneContext(CC);
80 snprintf(msgnumstr, 128, "%ld", MsgNum);
83 /* Load the template message. Get mallocs done too */
84 FILE *Ftemplate = NULL;
87 Ftemplate = fopen(template, "r");
88 if (Ftemplate == NULL) {
92 "Cannot load template file %s [%s]won't send notification\r\n",
93 file_funambol_msg, strerror(errno));
94 syslog(LOG_ERR, "%s", buf);
96 CtdlAideMessage(buf, "External notifier unable to find message template!");
99 mimetype = GuessMimeByFilename(template, tlen);
103 SOAPMessage = malloc(3072);
104 memset(SOAPMessage, 0, 3072);
106 while(fgets(buf, SIZ, Ftemplate) != NULL) {
107 strcat(SOAPMessage, buf);
111 if (strlen(SOAPMessage) < 0) {
115 "Cannot load template file %s; won't send notification\r\n",
117 syslog(LOG_ERR, "%s", buf);
119 CtdlAideMessage(buf, "External notifier unable to load message template!");
123 help_subst(SOAPMessage, "^notifyuser", user);
124 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
125 help_subst(SOAPMessage, "^msgid", msgid);
126 help_subst(SOAPMessage, "^msgnum", msgnumstr);
128 /* pass our list of custom made headers */
130 contenttype=(char*) malloc(40+strlen(mimetype));
131 sprintf(contenttype,"Content-Type: %s; charset=utf-8", mimetype);
133 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "SOAPAction: \"\"");
134 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, contenttype);
135 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "Accept: application/soap+xml, application/mime, multipart/related, text/*");
136 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "Pragma: no-cache");
138 /* Now specify the POST binary data */
139 IO->HttpReq.PlainPostData = SOAPMessage;
140 IO->HttpReq.PlainPostDataLen = strlen(SOAPMessage);
143 help_subst(remoteurl, "^notifyuser", user);
144 help_subst(remoteurl, "^syncsource", config.c_funambol_source);
145 help_subst(remoteurl, "^msgid", msgid);
146 help_subst(remoteurl, "^msgnum", msgnumstr);
147 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "Accept: application/soap+xml, application/mime, multipart/related, text/*");
148 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "Pragma: no-cache");
151 Buf = NewStrBufPlain (remoteurl, -1);
152 ParseURL(&IO->ConnectMe, Buf, 80);
153 FreeStrBuf(&Buf); /* TODO: this is uncool... */
154 CurlPrepareURL(IO->ConnectMe);
155 if (! evcurl_init(IO,
161 ExtNotifyShutdownAbort))
163 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
166 chnd = IO->HttpReq.chnd;
167 OPT(SSL_VERIFYPEER, 0);
168 OPT(SSL_VERIFYHOST, 0);
170 ReplyBuf = NewStrBuf();
171 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
172 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
175 (!IsEmptyStr(config.c_ip_addr))
176 && (strcmp(config.c_ip_addr, "*"))
177 && (strcmp(config.c_ip_addr, "::"))
178 && (strcmp(config.c_ip_addr, "0.0.0.0"))
180 OPT(INTERFACE, config.c_ip_addr);
183 QueueCurlContext(IO);
187 /// curl_slist_free_all (headers);
188 /// curl_easy_cleanup(curl);
189 if (contenttype) free(contenttype);
190 if (SOAPMessage != NULL) free(SOAPMessage);
191 if (buf != NULL) free(buf);
192 FreeStrBuf (&ReplyBuf);
197 eNextState EvaluateResult(AsyncIO *IO)
200 if (IO->HttpReq.httpcode != 200) {
203 syslog(LOG_ALERT, "libcurl error %ld: %s\n",
204 IO->HttpReq.httpcode,
205 IO->HttpReq.errdesc);
207 ErrMsg = NewStrBufPlain(HKEY("Error sending your Notification\n"));
208 StrBufAppendPrintf(ErrMsg, "\nlibcurl error %ld: \n\t\t%s\n",
209 IO->HttpReq.httpcode,
210 IO->HttpReq.errdesc);
211 StrBufAppendBufPlain(ErrMsg, HKEY("\nWas Trying to send: \n"), 0);
212 StrBufAppendBufPlain(ErrMsg, IO->ConnectMe->PlainUrl, -1, 0);
213 if (IO->HttpReq.PlainPostDataLen > 0) {
214 StrBufAppendBufPlain(ErrMsg, HKEY("\nThe Post document was: \n"), 0);
215 StrBufAppendBufPlain(ErrMsg,
216 IO->HttpReq.PlainPostData,
217 IO->HttpReq.PlainPostDataLen, 0);
218 StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);
220 if (StrLength(IO->HttpReq.ReplyData) > 0) {
221 StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThe Serverreply was: \n\n"), 0);
222 StrBufAppendBuf(ErrMsg, IO->HttpReq.ReplyData, 0);
225 StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThere was no Serverreply.\n\n"), 0);
226 ///ExtNotify_PutErrorMessage(Ctx, ErrMsg);
227 CtdlAideMessage(ChrPtr(ErrMsg), "External notifier unable to load message template!");
230 syslog(LOG_DEBUG, "Funambol notified\n");
232 while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
233 FreeStrBuf(&Ctx.NotifyHostList[i]);
235 if (Ctx.NotifyErrors != NULL)
243 It = GetNewHashPos(Ctx.NotifyErrors, 0);
244 while (GetNextHashPos(Ctx.NotifyErrors, It, &len, &Key, &vErr) &&
246 ErrMsg = (StrBuf*) vErr;
247 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
248 "Failed to notify external service about inbound mail");
252 DeleteHash(&Ctx.NotifyErrors);
256 //// curl_slist_free_all (headers);
257 /// curl_easy_cleanup(curl);
258 ///if (contenttype) free(contenttype);
259 ///if (SOAPMessage != NULL) free(SOAPMessage);
260 ///if (buf != NULL) free(buf);
261 ///FreeStrBuf (&ReplyBuf);
265 eNextState ExtNotifyTerminate(AsyncIO *IO)
270 eNextState ExtNotifyShutdownAbort(AsyncIO *IO)