sending http requests instant now.
[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 /*
51 * \brief Sends a message to the Funambol server notifying 
52 * of new mail for a user
53 * Returns 0 if unsuccessful
54 */
55 int notify_http_server(char *remoteurl, 
56                        const char* template, long tlen, 
57                        char *user,
58                        char *msgid, 
59                        long MsgNum, 
60                        NotifyContext *Ctx) 
61 {
62         CURLcode sta;
63         char msgnumstr[128];
64         char *buf = NULL;
65         char *SOAPMessage = NULL;
66         char *contenttype = NULL;
67         StrBuf *ReplyBuf;
68         CURL *chnd;
69
70         snprintf(msgnumstr, 128, "%ld", MsgNum);
71
72         if (tlen > 0) {
73                 /* Load the template message. Get mallocs done too */
74                 FILE *Ftemplate = NULL;
75                 const char *mimetype;
76
77                 Ftemplate = fopen(template, "r");
78                 if (Ftemplate == NULL) {
79                         char buf[SIZ];
80
81                         snprintf(buf, SIZ, 
82                                  "Cannot load template file %s [%s]won't send notification\r\n", 
83                                  file_funambol_msg, strerror(errno));
84                         CtdlLogPrintf(CTDL_ERR, buf);
85
86                         CtdlAideMessage(buf, "External notifier unable to find message template!");
87                         goto abort;
88                 }
89                 mimetype = GuessMimeByFilename(template, tlen);
90
91                 buf = malloc(SIZ);
92                 memset(buf, 0, SIZ);
93                 SOAPMessage = malloc(3072);
94                 memset(SOAPMessage, 0, 3072);
95         
96                 while(fgets(buf, SIZ, Ftemplate) != NULL) {
97                         strcat(SOAPMessage, buf);
98                 }
99                 fclose(Ftemplate);
100         
101                 if (strlen(SOAPMessage) < 0) {
102                         char buf[SIZ];
103
104                         snprintf(buf, SIZ, 
105                                  "Cannot load template file %s; won't send notification\r\n", 
106                                  file_funambol_msg);
107                         CtdlLogPrintf(CTDL_ERR, buf);
108
109                         CtdlAideMessage(buf, "External notifier unable to load message template!");
110                         goto abort;
111                 }
112                 // Do substitutions
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);
117
118                 /* pass our list of custom made headers */
119
120                 contenttype=(char*) malloc(40+strlen(mimetype));
121                 sprintf(contenttype,"Content-Type: %s; charset=utf-8", mimetype);
122
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");
127
128                 /* Now specify the POST binary data */
129                 Ctx->HTTPData.PlainPostData = SOAPMessage;
130                 Ctx->HTTPData.PlainPostDataLen = strlen(SOAPMessage);
131         }
132         else {
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");
139         }
140
141         ParseURL(&Ctx->HTTPData.URL, NewStrBufPlain (remoteurl, -1), 80);
142         CurlPrepareURL(Ctx->HTTPData.URL);
143         int CallBack;
144         if (! evcurl_init(&Ctx->HTTPData, 
145                           Ctx, 
146                           "Citadel ExtNotify",
147                           CallBack))
148         {
149                 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
150                 goto abort;
151         }
152         chnd = Ctx->HTTPData.chnd;
153         OPT(SSL_VERIFYPEER, 0);
154         OPT(SSL_VERIFYHOST, 0);
155 /*
156         ReplyBuf = NewStrBuf();
157         curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
158         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
159 */
160         if (
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"))
165         ) {
166                 OPT(INTERFACE, config.c_ip_addr);
167         }
168
169         evcurl_handle_start(&Ctx->HTTPData);
170
171         return 0;
172 abort:
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);
179         return 1;
180 }
181
182
183
184 int EvaluateResult(NotifyContext *Ctx, int res, int b)
185 {
186         if (res) {
187                 StrBuf *ErrMsg;
188
189                 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", 
190                               res, 
191                               Ctx->HTTPData.errdesc);
192                 ErrMsg = NewStrBufPlain(HKEY("Error sending your Notification\n"));
193                 StrBufAppendPrintf(ErrMsg, "\nlibcurl error %d: %s\n", 
194                                    res, 
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);                  
205                 }
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);
209                 }
210                 else 
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!");
214         }
215
216         CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
217
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);
224         return 0;
225 }