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);
53 * \brief Sends a message to the Funambol server notifying
54 * of new mail for a user
55 * Returns 0 if unsuccessful
57 int notify_http_server(char *remoteurl,
58 const char* template, long tlen,
67 char *SOAPMessage = NULL;
68 char *contenttype = NULL;
74 IO = (AsyncIO*) malloc(sizeof(AsyncIO));
75 memset(IO, 0, sizeof(AsyncIO));
78 snprintf(msgnumstr, 128, "%ld", MsgNum);
81 /* Load the template message. Get mallocs done too */
82 FILE *Ftemplate = NULL;
85 Ftemplate = fopen(template, "r");
86 if (Ftemplate == NULL) {
90 "Cannot load template file %s [%s]won't send notification\r\n",
91 file_funambol_msg, strerror(errno));
92 CtdlLogPrintf(CTDL_ERR, buf);
94 CtdlAideMessage(buf, "External notifier unable to find message template!");
97 mimetype = GuessMimeByFilename(template, tlen);
101 SOAPMessage = malloc(3072);
102 memset(SOAPMessage, 0, 3072);
104 while(fgets(buf, SIZ, Ftemplate) != NULL) {
105 strcat(SOAPMessage, buf);
109 if (strlen(SOAPMessage) < 0) {
113 "Cannot load template file %s; won't send notification\r\n",
115 CtdlLogPrintf(CTDL_ERR, buf);
117 CtdlAideMessage(buf, "External notifier unable to load message template!");
121 help_subst(SOAPMessage, "^notifyuser", user);
122 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
123 help_subst(SOAPMessage, "^msgid", msgid);
124 help_subst(SOAPMessage, "^msgnum", msgnumstr);
126 /* pass our list of custom made headers */
128 contenttype=(char*) malloc(40+strlen(mimetype));
129 sprintf(contenttype,"Content-Type: %s; charset=utf-8", mimetype);
131 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "SOAPAction: \"\"");
132 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, contenttype);
133 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "Accept: application/soap+xml, application/mime, multipart/related, text/*");
134 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "Pragma: no-cache");
136 /* Now specify the POST binary data */
137 IO->HttpReq.PlainPostData = SOAPMessage;
138 IO->HttpReq.PlainPostDataLen = strlen(SOAPMessage);
141 help_subst(remoteurl, "^notifyuser", user);
142 help_subst(remoteurl, "^syncsource", config.c_funambol_source);
143 help_subst(remoteurl, "^msgid", msgid);
144 help_subst(remoteurl, "^msgnum", msgnumstr);
145 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "Accept: application/soap+xml, application/mime, multipart/related, text/*");
146 IO->HttpReq.headers = curl_slist_append(IO->HttpReq.headers, "Pragma: no-cache");
149 Buf = NewStrBufPlain (remoteurl, -1);
150 ParseURL(&IO->ConnectMe, Buf, 80);
151 FreeStrBuf(&Buf); /* TODO: this is uncool... */
152 CurlPrepareURL(IO->ConnectMe);
153 if (! evcurl_init(IO,
159 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
162 chnd = IO->HttpReq.chnd;
163 OPT(SSL_VERIFYPEER, 0);
164 OPT(SSL_VERIFYHOST, 0);
166 ReplyBuf = NewStrBuf();
167 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
168 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
171 (!IsEmptyStr(config.c_ip_addr))
172 && (strcmp(config.c_ip_addr, "*"))
173 && (strcmp(config.c_ip_addr, "::"))
174 && (strcmp(config.c_ip_addr, "0.0.0.0"))
176 OPT(INTERFACE, config.c_ip_addr);
179 evcurl_handle_start(IO);
183 /// curl_slist_free_all (headers);
184 /// curl_easy_cleanup(curl);
185 if (contenttype) free(contenttype);
186 if (SOAPMessage != NULL) free(SOAPMessage);
187 if (buf != NULL) free(buf);
188 FreeStrBuf (&ReplyBuf);
193 eNextState EvaluateResult(AsyncIO *IO)
196 if (IO->HttpReq.httpcode != 200) {
199 CtdlLogPrintf(CTDL_ALERT, "libcurl error %ld: %s\n",
200 IO->HttpReq.httpcode,
201 IO->HttpReq.errdesc);
202 ErrMsg = NewStrBufPlain(HKEY("Error sending your Notification\n"));
203 StrBufAppendPrintf(ErrMsg, "\nlibcurl error %ld: \n\t\t%s\n",
204 IO->HttpReq.httpcode,
205 IO->HttpReq.errdesc);
206 StrBufAppendBufPlain(ErrMsg, HKEY("\nWas Trying to send: \n"), 0);
207 StrBufAppendBufPlain(ErrMsg, IO->ConnectMe->PlainUrl, -1, 0);
208 if (IO->HttpReq.PlainPostDataLen > 0) {
209 StrBufAppendBufPlain(ErrMsg, HKEY("\nThe Post document was: \n"), 0);
210 StrBufAppendBufPlain(ErrMsg,
211 IO->HttpReq.PlainPostData,
212 IO->HttpReq.PlainPostDataLen, 0);
213 StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);
215 if (StrLength(IO->HttpReq.ReplyData) > 0) {
216 StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThe Serverreply was: \n\n"), 0);
217 StrBufAppendBuf(ErrMsg, IO->HttpReq.ReplyData, 0);
220 StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThere was no Serverreply.\n\n"), 0);
221 ///ExtNotify_PutErrorMessage(Ctx, ErrMsg);
222 CtdlAideMessage(ChrPtr(ErrMsg), "External notifier unable to load message template!");
225 CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
227 while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
228 FreeStrBuf(&Ctx.NotifyHostList[i]);
230 if (Ctx.NotifyErrors != NULL)
238 It = GetNewHashPos(Ctx.NotifyErrors, 0);
239 while (GetNextHashPos(Ctx.NotifyErrors, It, &len, &Key, &vErr) &&
241 ErrMsg = (StrBuf*) vErr;
242 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
243 "Failed to notify external service about inbound mail");
247 DeleteHash(&Ctx.NotifyErrors);
251 //// curl_slist_free_all (headers);
252 /// curl_easy_cleanup(curl);
253 ///if (contenttype) free(contenttype);
254 ///if (SOAPMessage != NULL) free(SOAPMessage);
255 ///if (buf != NULL) free(buf);
256 ///FreeStrBuf (&ReplyBuf);