a3c744ece47f08661dcc2a87c1020f6745851d6c
[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
52 /*
53 * \brief Sends a message to the Funambol server notifying 
54 * of new mail for a user
55 * Returns 0 if unsuccessful
56 */
57 int notify_http_server(char *remoteurl, 
58                        const char* template, long tlen, 
59                        char *user,
60                        char *msgid, 
61                        long MsgNum, 
62                        NotifyContext *Ctx) 
63 {
64         CURLcode sta;
65         char msgnumstr[128];
66         char *buf = NULL;
67         char *SOAPMessage = NULL;
68         char *contenttype = NULL;
69         StrBuf *ReplyBuf;
70         StrBuf *Buf;
71         CURL *chnd;
72         AsyncIO *IO;
73
74         IO = (AsyncIO*) malloc(sizeof(AsyncIO));
75         memset(IO, 0, sizeof(AsyncIO));
76         IO->CitContext = CloneContext(CC);
77
78         snprintf(msgnumstr, 128, "%ld", MsgNum);
79
80         if (tlen > 0) {
81                 /* Load the template message. Get mallocs done too */
82                 FILE *Ftemplate = NULL;
83                 const char *mimetype;
84
85                 Ftemplate = fopen(template, "r");
86                 if (Ftemplate == NULL) {
87                         char buf[SIZ];
88
89                         snprintf(buf, SIZ, 
90                                  "Cannot load template file %s [%s]won't send notification\r\n", 
91                                  file_funambol_msg, strerror(errno));
92                         CtdlLogPrintf(CTDL_ERR, buf);
93
94                         CtdlAideMessage(buf, "External notifier unable to find message template!");
95                         goto abort;
96                 }
97                 mimetype = GuessMimeByFilename(template, tlen);
98
99                 buf = malloc(SIZ);
100                 memset(buf, 0, SIZ);
101                 SOAPMessage = malloc(3072);
102                 memset(SOAPMessage, 0, 3072);
103         
104                 while(fgets(buf, SIZ, Ftemplate) != NULL) {
105                         strcat(SOAPMessage, buf);
106                 }
107                 fclose(Ftemplate);
108         
109                 if (strlen(SOAPMessage) < 0) {
110                         char buf[SIZ];
111
112                         snprintf(buf, SIZ, 
113                                  "Cannot load template file %s; won't send notification\r\n", 
114                                  file_funambol_msg);
115                         CtdlLogPrintf(CTDL_ERR, buf);
116
117                         CtdlAideMessage(buf, "External notifier unable to load message template!");
118                         goto abort;
119                 }
120                 // Do substitutions
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);
125
126                 /* pass our list of custom made headers */
127
128                 contenttype=(char*) malloc(40+strlen(mimetype));
129                 sprintf(contenttype,"Content-Type: %s; charset=utf-8", mimetype);
130
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");
135
136                 /* Now specify the POST binary data */
137                 IO->HttpReq.PlainPostData = SOAPMessage;
138                 IO->HttpReq.PlainPostDataLen = strlen(SOAPMessage);
139         }
140         else {
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");
147         }
148
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, 
154 //                        Ctx, 
155                           NULL,
156                           "Citadel ExtNotify",
157                           EvaluateResult))
158         {
159                 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
160                 goto abort;
161         }
162         chnd = IO->HttpReq.chnd;
163         OPT(SSL_VERIFYPEER, 0);
164         OPT(SSL_VERIFYHOST, 0);
165 /*
166         ReplyBuf = NewStrBuf();
167         curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
168         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
169 */
170         if (
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"))
175         ) {
176                 OPT(INTERFACE, config.c_ip_addr);
177         }
178
179         evcurl_handle_start(IO);
180
181         return 0;
182 abort:
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);
189         return 1;
190 }
191
192
193 eNextState EvaluateResult(AsyncIO *IO)
194 {
195
196         if (IO->HttpReq.httpcode != 200) {
197                 StrBuf *ErrMsg;
198
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);                  
214                 }
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);
218                 }
219                 else 
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!");
223         }
224
225         CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
226 /*
227         while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
228                 FreeStrBuf(&Ctx.NotifyHostList[i]);
229
230         if (Ctx.NotifyErrors != NULL)
231         {
232                 long len;
233                 const char *Key;
234                 HashPos *It;
235                 void *vErr;
236                 StrBuf *ErrMsg;
237
238                 It = GetNewHashPos(Ctx.NotifyErrors, 0);
239                 while (GetNextHashPos(Ctx.NotifyErrors, It, &len, &Key, &vErr) && 
240                        (vErr != NULL)) {
241                         ErrMsg = (StrBuf*) vErr;
242                         quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED, 
243                                         "Failed to notify external service about inbound mail");
244                 }
245
246                 DeleteHashPos(&It);
247                 DeleteHash(&Ctx.NotifyErrors);
248         }
249 */
250
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);
257         return 0;
258 }