2d65f20ef66019ba193c8d5d23a4610c4eec96b7
[citadel.git] / citadel / modules / extnotify / funambol65.c
1 /* 
2  * funambol65.c
3  * Author: Mathew McBride
4  * 
5  * This module facilitates notifications to a Funambol server
6  * for push email
7  *
8  * Based on bits of the previous serv_funambol
9  * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
10  *
11  * Copyright (c) 2008-2010
12  *
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.
17  *
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.
22  *
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
26  */
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/socket.h>
32 #include <time.h>
33 #include <libcitadel.h>
34 #include <errno.h>
35 #include <unistd.h>
36 #include <curl/curl.h>
37
38 #include "citadel.h"
39 #include "citadel_dirs.h"
40 #include "clientsocket.h"
41 #include "sysdep.h"
42 #include "config.h"
43 #include "sysdep_decls.h"
44 #include "msgbase.h"
45 #include "ctdl_module.h"
46
47 #include "event_client.h"
48 #include "extnotify.h"
49
50 eNextState EvaluateResult(AsyncIO *IO);
51 eNextState ExtNotifyTerminate(AsyncIO *IO);
52 eNextState ExtNotifyShutdownAbort(AsyncIO *IO);
53
54 /*
55 * \brief Sends a message to the Funambol server notifying 
56 * of new mail for a user
57 * Returns 0 if unsuccessful
58 */
59 int notify_http_server(char *remoteurl, 
60                        const char* template, long tlen, 
61                        char *user,
62                        char *msgid, 
63                        long MsgNum, 
64                        NotifyContext *Ctx) 
65 {
66         CURLcode sta;
67         char msgnumstr[128];
68         char *buf = NULL;
69         char *SOAPMessage = NULL;
70         char *contenttype = NULL;
71         StrBuf *ReplyBuf;
72         StrBuf *Buf;
73         CURL *chnd;
74         AsyncIO *IO;
75
76         IO = (AsyncIO*) malloc(sizeof(AsyncIO));
77         memset(IO, 0, sizeof(AsyncIO));
78         IO->CitContext = CloneContext(CC);
79
80         snprintf(msgnumstr, 128, "%ld", MsgNum);
81
82         if (tlen > 0) {
83                 /* Load the template message. Get mallocs done too */
84                 FILE *Ftemplate = NULL;
85                 const char *mimetype;
86
87                 Ftemplate = fopen(template, "r");
88                 if (Ftemplate == NULL) {
89                         char buf[SIZ];
90
91                         snprintf(buf, SIZ, 
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);
95
96                         CtdlAideMessage(buf, "External notifier unable to find message template!");
97                         goto abort;
98                 }
99                 mimetype = GuessMimeByFilename(template, tlen);
100
101                 buf = malloc(SIZ);
102                 memset(buf, 0, SIZ);
103                 SOAPMessage = malloc(3072);
104                 memset(SOAPMessage, 0, 3072);
105         
106                 while(fgets(buf, SIZ, Ftemplate) != NULL) {
107                         strcat(SOAPMessage, buf);
108                 }
109                 fclose(Ftemplate);
110         
111                 if (strlen(SOAPMessage) < 0) {
112                         char buf[SIZ];
113
114                         snprintf(buf, SIZ, 
115                                  "Cannot load template file %s; won't send notification\r\n", 
116                                  file_funambol_msg);
117                         syslog(LOG_ERR, "%s", buf);
118
119                         CtdlAideMessage(buf, "External notifier unable to load message template!");
120                         goto abort;
121                 }
122                 // Do substitutions
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);
127
128                 /* pass our list of custom made headers */
129
130                 contenttype=(char*) malloc(40+strlen(mimetype));
131                 sprintf(contenttype,"Content-Type: %s; charset=utf-8", mimetype);
132
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");
137
138                 /* Now specify the POST binary data */
139                 IO->HttpReq.PlainPostData = SOAPMessage;
140                 IO->HttpReq.PlainPostDataLen = strlen(SOAPMessage);
141         }
142         else {
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");
149         }
150
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, 
156 //                        Ctx, 
157                           NULL,
158                           "Citadel ExtNotify",
159                           EvaluateResult, 
160                           ExtNotifyTerminate,
161                           ExtNotifyShutdownAbort))
162         {
163                 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
164                 goto abort;
165         }
166         chnd = IO->HttpReq.chnd;
167         OPT(SSL_VERIFYPEER, 0);
168         OPT(SSL_VERIFYHOST, 0);
169 /*
170         ReplyBuf = NewStrBuf();
171         curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
172         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
173 */
174         if (
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"))
179         ) {
180                 OPT(INTERFACE, config.c_ip_addr);
181         }
182
183         QueueCurlContext(IO);
184
185         return 0;
186 abort:
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);
193         return 1;
194 }
195
196
197 eNextState EvaluateResult(AsyncIO *IO)
198 {
199
200         if (IO->HttpReq.httpcode != 200) {
201                 StrBuf *ErrMsg;
202
203                 syslog(LOG_ALERT, "libcurl error %ld: %s\n", 
204                               IO->HttpReq.httpcode, 
205                               IO->HttpReq.errdesc);
206
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);                  
219                 }
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);
223                 }
224                 else 
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!");
228         }
229
230         syslog(LOG_DEBUG, "Funambol notified\n");
231 /*
232         while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
233                 FreeStrBuf(&Ctx.NotifyHostList[i]);
234
235         if (Ctx.NotifyErrors != NULL)
236         {
237                 long len;
238                 const char *Key;
239                 HashPos *It;
240                 void *vErr;
241                 StrBuf *ErrMsg;
242
243                 It = GetNewHashPos(Ctx.NotifyErrors, 0);
244                 while (GetNextHashPos(Ctx.NotifyErrors, It, &len, &Key, &vErr) && 
245                        (vErr != NULL)) {
246                         ErrMsg = (StrBuf*) vErr;
247                         quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED, 
248                                         "Failed to notify external service about inbound mail");
249                 }
250
251                 DeleteHashPos(&It);
252                 DeleteHash(&Ctx.NotifyErrors);
253         }
254 */
255
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);
262         return 0;
263 }
264
265 eNextState ExtNotifyTerminate(AsyncIO *IO)
266 {
267         free(IO);
268         return eAbort;
269 }
270 eNextState ExtNotifyShutdownAbort(AsyncIO *IO)
271 {
272         free(IO);
273         return eAbort;
274 }