Straightn http client generation
[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
79         if (! InitcURLIOStruct(IO,
80                                NULL, /* we don't have personal data anymore. */
81                                "Citadel ExtNotify",
82                                EvaluateResult,
83                                ExtNotifyTerminate,
84                                ExtNotifyShutdownAbort))
85         {
86                 syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
87                 goto abort;
88         }
89
90         snprintf(msgnumstr, 128, "%ld", MsgNum);
91
92         if (tlen > 0) {
93                 /* Load the template message. Get mallocs done too */
94                 FILE *Ftemplate = NULL;
95                 const char *mimetype;
96
97                 Ftemplate = fopen(template, "r");
98                 if (Ftemplate == NULL) {
99                         char buf[SIZ];
100
101                         snprintf(buf, SIZ,
102                                  "Cannot load template file %s [%s] "
103                                  "won't send notification\r\n",
104                                  file_funambol_msg,
105                                  strerror(errno));
106                         syslog(LOG_ERR, "%s", buf);
107                         // TODO: once an hour!
108                         CtdlAideMessage(
109                                 buf,
110                                 "External notifier: "
111                                 "unable to find message template!");
112                         goto abort;
113                 }
114                 mimetype = GuessMimeByFilename(template, tlen);
115
116                 buf = malloc(SIZ);
117                 memset(buf, 0, SIZ);
118                 SOAPMessage = malloc(3072);
119                 memset(SOAPMessage, 0, 3072);
120
121                 while(fgets(buf, SIZ, Ftemplate) != NULL) {
122                         strcat(SOAPMessage, buf);
123                 }
124                 fclose(Ftemplate);
125
126                 if (strlen(SOAPMessage) < 0) {
127                         char buf[SIZ];
128
129                         snprintf(buf, SIZ,
130                                  "Cannot load template file %s;"
131                                  " won't send notification\r\n",
132                                  file_funambol_msg);
133                         syslog(LOG_ERR, "%s", buf);
134
135                         CtdlAideMessage(buf, "External notifier: "
136                                         "unable to load message template!");
137                         goto abort;
138                 }
139                 // Do substitutions
140                 help_subst(SOAPMessage, "^notifyuser", user);
141                 help_subst(SOAPMessage, "^syncsource",
142                            config.c_funambol_source);
143                 help_subst(SOAPMessage, "^msgid", msgid);
144                 help_subst(SOAPMessage, "^msgnum", msgnumstr);
145
146                 /* pass our list of custom made headers */
147
148                 contenttype=(char*) malloc(40+strlen(mimetype));
149                 sprintf(contenttype,
150                         "Content-Type: %s; charset=utf-8",
151                         mimetype);
152
153                 IO->HttpReq.headers = curl_slist_append(
154                         IO->HttpReq.headers,
155                         "SOAPAction: \"\"");
156
157                 IO->HttpReq.headers = curl_slist_append(
158                         IO->HttpReq.headers,
159                         contenttype);
160
161                 IO->HttpReq.headers = curl_slist_append(
162                         IO->HttpReq.headers,
163                         "Accept: application/soap+xml, "
164                         "application/mime, multipart/related, text/*");
165
166                 IO->HttpReq.headers = curl_slist_append(
167                         IO->HttpReq.headers,
168                         "Pragma: no-cache");
169
170                 /* Now specify the POST binary data */
171                 IO->HttpReq.PlainPostData = SOAPMessage;
172                 IO->HttpReq.PlainPostDataLen = strlen(SOAPMessage);
173         }
174         else {
175                 help_subst(remoteurl, "^notifyuser", user);
176                 help_subst(remoteurl, "^syncsource", config.c_funambol_source);
177                 help_subst(remoteurl, "^msgid", msgid);
178                 help_subst(remoteurl, "^msgnum", msgnumstr);
179
180                 IO->HttpReq.headers = curl_slist_append(
181                         IO->HttpReq.headers,
182                         "Accept: application/soap+xml, "
183                         "application/mime, multipart/related, text/*");
184
185                 IO->HttpReq.headers = curl_slist_append(
186                         IO->HttpReq.headers,
187                         "Pragma: no-cache");
188         }
189
190         Buf = NewStrBufPlain (remoteurl, -1);
191         ParseURL(&IO->ConnectMe, Buf, 80);
192         FreeStrBuf(&Buf); /* TODO: this is uncool... */
193         CurlPrepareURL(IO->ConnectMe);
194
195         chnd = IO->HttpReq.chnd;
196         OPT(SSL_VERIFYPEER, 0);
197         OPT(SSL_VERIFYHOST, 0);
198
199         QueueCurlContext(IO);
200
201         return 0;
202 abort:
203
204         if (contenttype) free(contenttype);
205         if (SOAPMessage != NULL) free(SOAPMessage);
206         if (buf != NULL) free(buf);
207         FreeStrBuf (&ReplyBuf);
208         return 1;
209 }
210
211
212 eNextState EvaluateResult(AsyncIO *IO)
213 {
214
215         if (IO->HttpReq.httpcode != 200) {
216                 StrBuf *ErrMsg;
217
218                 syslog(LOG_ALERT, "libcurl error %ld: %s\n",
219                               IO->HttpReq.httpcode,
220                               IO->HttpReq.errdesc);
221
222                 ErrMsg = NewStrBufPlain(
223                         HKEY("Error sending your Notification\n"));
224                 StrBufAppendPrintf(ErrMsg, "\nlibcurl error %ld: \n\t\t%s\n",
225                                    IO->HttpReq.httpcode,
226                                    IO->HttpReq.errdesc);
227
228                 StrBufAppendBufPlain(ErrMsg,
229                                      HKEY("\nWas Trying to send: \n"),
230                                      0);
231
232                 StrBufAppendBufPlain(ErrMsg, IO->ConnectMe->PlainUrl, -1, 0);
233                 if (IO->HttpReq.PlainPostDataLen > 0) {
234                         StrBufAppendBufPlain(
235                                 ErrMsg,
236                                 HKEY("\nThe Post document was: \n"),
237                                 0);
238                         StrBufAppendBufPlain(ErrMsg,
239                                              IO->HttpReq.PlainPostData,
240                                              IO->HttpReq.PlainPostDataLen, 0);
241                         StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);
242                 }
243                 if (StrLength(IO->HttpReq.ReplyData) > 0) {
244                         StrBufAppendBufPlain(
245                                 ErrMsg,
246                                 HKEY("\n\nThe Serverreply was: \n\n"),
247                                 0);
248                         StrBufAppendBuf(ErrMsg, IO->HttpReq.ReplyData, 0);
249                 }
250                 else
251                         StrBufAppendBufPlain(
252                                 ErrMsg,
253                                 HKEY("\n\nThere was no Serverreply.\n\n"),
254                                 0);
255                 ///ExtNotify_PutErrorMessage(Ctx, ErrMsg);
256                 CtdlAideMessage(ChrPtr(ErrMsg),
257                                 "External notifier: "
258                                 "unable to contact notification host!");
259         }
260
261         syslog(LOG_DEBUG, "Funambol notified\n");
262 /*
263         while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
264                 FreeStrBuf(&Ctx.NotifyHostList[i]);
265
266         if (Ctx.NotifyErrors != NULL)
267         {
268                 long len;
269                 const char *Key;
270                 HashPos *It;
271                 void *vErr;
272                 StrBuf *ErrMsg;
273
274                 It = GetNewHashPos(Ctx.NotifyErrors, 0);
275                 while (GetNextHashPos(Ctx.NotifyErrors,
276                 It, &len, &Key, &vErr) &&
277                        (vErr != NULL)) {
278                         ErrMsg = (StrBuf*) vErr;
279                         quickie_message("Citadel", NULL, NULL,
280                         AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
281                         "Failed to notify external service about inbound mail");
282                 }
283
284                 DeleteHashPos(&It);
285                 DeleteHash(&Ctx.NotifyErrors);
286         }
287 */
288
289 ////    curl_slist_free_all (headers);
290 ///     curl_easy_cleanup(curl);
291         ///if (contenttype) free(contenttype);
292         ///if (SOAPMessage != NULL) free(SOAPMessage);
293         ///if (buf != NULL) free(buf);
294         ///FreeStrBuf (&ReplyBuf);
295         return 0;
296 }
297
298 eNextState ExtNotifyTerminate(AsyncIO *IO)
299 {
300         free(IO);
301         return eAbort;
302 }
303 eNextState ExtNotifyShutdownAbort(AsyncIO *IO)
304 {
305         free(IO);
306         return eAbort;
307 }