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"
51 * \brief Sends a message to the Funambol server notifying
52 * of new mail for a user
53 * Returns 0 if unsuccessful
55 int notify_http_server(char *remoteurl,
56 const char* template, long tlen,
65 char *SOAPMessage = NULL;
66 char *contenttype = NULL;
70 snprintf(msgnumstr, 128, "%ld", MsgNum);
73 /* Load the template message. Get mallocs done too */
74 FILE *Ftemplate = NULL;
77 Ftemplate = fopen(template, "r");
78 if (Ftemplate == NULL) {
82 "Cannot load template file %s [%s]won't send notification\r\n",
83 file_funambol_msg, strerror(errno));
84 CtdlLogPrintf(CTDL_ERR, buf);
86 CtdlAideMessage(buf, "External notifier unable to find message template!");
89 mimetype = GuessMimeByFilename(template, tlen);
93 SOAPMessage = malloc(3072);
94 memset(SOAPMessage, 0, 3072);
96 while(fgets(buf, SIZ, Ftemplate) != NULL) {
97 strcat(SOAPMessage, buf);
101 if (strlen(SOAPMessage) < 0) {
105 "Cannot load template file %s; won't send notification\r\n",
107 CtdlLogPrintf(CTDL_ERR, buf);
109 CtdlAideMessage(buf, "External notifier unable to load message template!");
113 help_subst(SOAPMessage, "^notifyuser", user);
114 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
115 help_subst(SOAPMessage, "^msgid", msgid);
116 help_subst(SOAPMessage, "^msgnum", msgnumstr);
118 /* pass our list of custom made headers */
120 contenttype=(char*) malloc(40+strlen(mimetype));
121 sprintf(contenttype,"Content-Type: %s; charset=utf-8", mimetype);
123 Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, "SOAPAction: \"\"");
124 Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, contenttype);
125 Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, "Accept: application/soap+xml, application/mime, multipart/related, text/*");
126 Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, "Pragma: no-cache");
128 /* Now specify the POST binary data */
129 Ctx->HTTPData.PlainPostData = SOAPMessage;
130 Ctx->HTTPData.PlainPostDataLen = strlen(SOAPMessage);
133 help_subst(remoteurl, "^notifyuser", user);
134 help_subst(remoteurl, "^syncsource", config.c_funambol_source);
135 help_subst(remoteurl, "^msgid", msgid);
136 help_subst(remoteurl, "^msgnum", msgnumstr);
137 Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, "Accept: application/soap+xml, application/mime, multipart/related, text/*");
138 Ctx->HTTPData.headers = curl_slist_append(Ctx->HTTPData.headers, "Pragma: no-cache");
141 ParseURL(&Ctx->HTTPData.URL, NewStrBufPlain (remoteurl, -1), 80);
142 CurlPrepareURL(Ctx->HTTPData.URL);
144 if (! evcurl_init(&Ctx->HTTPData,
149 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
152 chnd = Ctx->HTTPData.chnd;
153 OPT(SSL_VERIFYPEER, 0);
154 OPT(SSL_VERIFYHOST, 0);
156 ReplyBuf = NewStrBuf();
157 curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
158 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
161 (!IsEmptyStr(config.c_ip_addr))
162 && (strcmp(config.c_ip_addr, "*"))
163 && (strcmp(config.c_ip_addr, "::"))
164 && (strcmp(config.c_ip_addr, "0.0.0.0"))
166 OPT(INTERFACE, config.c_ip_addr);
169 evcurl_handle_start(&Ctx->HTTPData);
173 /// curl_slist_free_all (headers);
174 /// curl_easy_cleanup(curl);
175 if (contenttype) free(contenttype);
176 if (SOAPMessage != NULL) free(SOAPMessage);
177 if (buf != NULL) free(buf);
178 FreeStrBuf (&ReplyBuf);
184 int EvaluateResult(NotifyContext *Ctx, int res, int b)
189 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n",
191 Ctx->HTTPData.errdesc);
192 ErrMsg = NewStrBufPlain(HKEY("Error sending your Notification\n"));
193 StrBufAppendPrintf(ErrMsg, "\nlibcurl error %d: %s\n",
195 Ctx->HTTPData.errdesc);
196 /// StrBufAppendBufPlain(ErrMsg, curl_errbuf, -1, 0);
197 StrBufAppendBufPlain(ErrMsg, HKEY("\nWas Trying to send: \n"), 0);
198 StrBufAppendBufPlain(ErrMsg, Ctx->HTTPData.URL->PlainUrl, -1, 0);
199 if (Ctx->HTTPData.PlainPostDataLen > 0) {
200 StrBufAppendBufPlain(ErrMsg, HKEY("\nThe Post document was: \n"), 0);
201 StrBufAppendBufPlain(ErrMsg,
202 Ctx->HTTPData.PlainPostData,
203 Ctx->HTTPData.PlainPostDataLen, 0);
204 StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);
206 if (StrLength(Ctx->HTTPData.ReplyData) > 0) {
207 StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThe Serverreply was: \n\n"), 0);
208 StrBufAppendBuf(ErrMsg, Ctx->HTTPData.ReplyData, 0);
211 StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThere was no Serverreply.\n\n"), 0);
212 ///ExtNotify_PutErrorMessage(Ctx, ErrMsg);
213 CtdlAideMessage(ChrPtr(ErrMsg), "External notifier unable to load message template!");
216 CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
218 //// curl_slist_free_all (headers);
219 /// curl_easy_cleanup(curl);
220 ///if (contenttype) free(contenttype);
221 ///if (SOAPMessage != NULL) free(SOAPMessage);
222 ///if (buf != NULL) free(buf);
223 ///FreeStrBuf (&ReplyBuf);