* More license declarations
[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-2009
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 "extnotify.h"
48
49 /*
50 * \brief Sends a message to the Funambol server notifying 
51 * of new mail for a user
52 * Returns 0 if unsuccessful
53 */
54 int notify_http_server(char *remoteurl, 
55                        const char* template, long tlen, 
56                        char *user,
57                        char *msgid, 
58                        long MsgNum, 
59                        NotifyContext *Ctx) 
60 {
61         char curl_errbuf[CURL_ERROR_SIZE];
62         char *pchs, *pche;
63         char userpass[SIZ];
64         char msgnumstr[128];
65         char *buf = NULL;
66         CURL *curl;
67         CURLcode res;
68         struct curl_slist * headers=NULL;
69         char errmsg[1024] = "";
70         char *SOAPMessage = NULL;
71         char *contenttype = NULL;
72         StrBuf *ReplyBuf;
73
74         curl = curl_easy_init();
75         if (!curl) {
76                 CtdlLogPrintf(CTDL_ALERT, "Unable to initialize libcurl.\n");
77                 return 1;
78         }
79
80         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
81         curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
82         ReplyBuf = NewStrBuf();
83         curl_easy_setopt(curl, CURLOPT_WRITEDATA, ReplyBuf);
84         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlFillStrBuf_callback);
85         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errmsg);
86         curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
87         curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_errbuf);
88
89         pchs = strchr(remoteurl, ':');
90         pche = strchr(remoteurl, '@');
91         if ((pche != NULL) && 
92             (pchs != NULL) && 
93             (pchs < pche) && ((pche - pchs) < SIZ)) {
94                 memcpy(userpass, pchs + 3, pche - pchs - 3);
95                 
96                 userpass[pche - pchs - 3] = '\0';
97                 curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
98                 curl_easy_setopt(curl, CURLOPT_USERPWD, userpass);
99
100         }
101 #ifdef CURLOPT_HTTP_CONTENT_DECODING
102         curl_easy_setopt(curl, CURLOPT_HTTP_CONTENT_DECODING, 1);
103         curl_easy_setopt(curl, CURLOPT_ENCODING, "");
104 #endif
105         curl_easy_setopt(curl, CURLOPT_USERAGENT, CITADEL);
106         curl_easy_setopt(curl, CURLOPT_TIMEOUT, 180);           /* die after 180 seconds */
107         if (!IsEmptyStr(config.c_ip_addr)) {
108                 curl_easy_setopt(curl, CURLOPT_INTERFACE, config.c_ip_addr);
109         }
110
111         headers = curl_slist_append(headers,"Accept: application/soap+xml, application/mime, multipart/related, text/*");
112         headers = curl_slist_append(headers,"Pragma: no-cache");
113
114         if (tlen > 0) {
115                 /* Load the template message. Get mallocs done too */
116                 FILE *Ftemplate = NULL;
117                 const char *mimetype;
118
119                 Ftemplate = fopen(template, "r");
120                 if (Ftemplate == NULL) {
121                         char buf[SIZ];
122
123                         snprintf(buf, SIZ, 
124                                  "Cannot load template file %s [%s]won't send notification\r\n", 
125                                  file_funambol_msg, strerror(errno));
126                         CtdlLogPrintf(CTDL_ERR, buf);
127
128                         aide_message(buf, "External notifier unable to find message template!");
129                         goto free;
130                 }
131                 mimetype = GuessMimeByFilename(template, tlen);
132
133                 snprintf(msgnumstr, 128, "%ld", MsgNum);
134
135                 buf = malloc(SIZ);
136                 memset(buf, 0, SIZ);
137                 SOAPMessage = malloc(3072);
138                 memset(SOAPMessage, 0, 3072);
139         
140                 while(fgets(buf, SIZ, Ftemplate) != NULL) {
141                         strcat(SOAPMessage, buf);
142                 }
143                 fclose(Ftemplate);
144         
145                 if (strlen(SOAPMessage) < 0) {
146                         char buf[SIZ];
147
148                         snprintf(buf, SIZ, 
149                                  "Cannot load template file %s; won't send notification\r\n", 
150                                  file_funambol_msg);
151                         CtdlLogPrintf(CTDL_ERR, buf);
152
153                         aide_message(buf, "External notifier unable to load message template!");
154                         goto free;
155                 }
156                 // Do substitutions
157                 help_subst(SOAPMessage, "^notifyuser", user);
158                 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
159                 help_subst(SOAPMessage, "^msgid", msgid);
160                 help_subst(SOAPMessage, "^msgnum", msgnumstr);
161
162                 curl_easy_setopt(curl, CURLOPT_URL, remoteurl);
163
164                 /* pass our list of custom made headers */
165
166                 contenttype=(char*) malloc(40+strlen(mimetype));
167                 sprintf(contenttype,"Content-Type: %s; charset=utf-8", mimetype);
168
169                 headers = curl_slist_append(headers, "SOAPAction: \"\"");
170                 headers = curl_slist_append(headers, contenttype);
171
172                 /* Now specify the POST binary data */
173
174                 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, SOAPMessage);
175                 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(SOAPMessage));
176                 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
177         }
178         else {
179                 help_subst(remoteurl, "^notifyuser", user);
180                 help_subst(remoteurl, "^syncsource", config.c_funambol_source);
181                 help_subst(remoteurl, "^msgid", msgid);
182                 help_subst(remoteurl, "^msgnum", msgnumstr);
183                 curl_easy_setopt(curl, CURLOPT_URL, remoteurl);
184                 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
185         }
186
187         res = curl_easy_perform(curl);
188         if (res) {
189                 StrBuf *ErrMsg;
190
191                 CtdlLogPrintf(CTDL_ALERT, "libcurl error %d: %s\n", res, errmsg);
192                 ErrMsg = NewStrBufPlain(HKEY("Error sending your Notification\n"));
193                 StrBufAppendPrintf(ErrMsg, "\nlibcurl error %d: %s\n", res, errmsg);
194                 StrBufAppendBufPlain(ErrMsg, curl_errbuf, -1, 0);
195                 StrBufAppendBufPlain(ErrMsg, HKEY("\nWas Trying to send: \n"), 0);
196                 StrBufAppendBufPlain(ErrMsg, remoteurl, -1, 0);
197                 if (tlen > 0) {
198                         StrBufAppendBufPlain(ErrMsg, HKEY("\nThe Post document was: \n"), 0);
199                         StrBufAppendBufPlain(ErrMsg, SOAPMessage, -1, 0);
200                         StrBufAppendBufPlain(ErrMsg, HKEY("\n\n"), 0);                  
201                 }
202                 if (StrLength(ReplyBuf) > 0) {                  
203                         StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThe Serverreply was: \n\n"), 0);
204                         StrBufAppendBuf(ErrMsg, ReplyBuf, 0);
205                 }
206                 else 
207                         StrBufAppendBufPlain(ErrMsg, HKEY("\n\nThere was no Serverreply.\n\n"), 0);
208                 ExtNotify_PutErrorMessage(Ctx, ErrMsg);
209         }
210
211         CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
212 free:
213         curl_slist_free_all (headers);
214         curl_easy_cleanup(curl);
215         if (contenttype) free(contenttype);
216         if (SOAPMessage != NULL) free(SOAPMessage);
217         if (buf != NULL) free(buf);
218         FreeStrBuf (&ReplyBuf);
219         return 0;
220 }