* this include is needed on *bsd
[citadel.git] / citadel / modules / extnotify / funambol65.c
1 /* 
2 * \file 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 #include "extnotify.h"
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <sys/socket.h>
17 #include <time.h>
18 #include <libcitadel.h>
19 #include <errno.h>
20 #include <unistd.h>
21
22 #include "citadel.h"
23 #include "citadel_dirs.h"
24 #include "clientsocket.h"
25 #include "sysdep.h"
26 #include "config.h"
27 #include "sysdep_decls.h"
28 #include "msgbase.h"
29 #include "ctdl_module.h"
30
31 /*
32 * \brief Sends a message to the Funambol server notifying 
33 * of new mail for a user
34 * Returns 0 if unsuccessful
35 */
36 int notify_funambol_server(char *user) {
37         char port[1024];
38         int sock = -1;
39         char *buf = NULL;
40         char *SOAPMessage = NULL;
41         char *SOAPHeader = NULL;
42         char *funambolCreds = NULL;
43         FILE *template = NULL;
44         
45         sprintf(port, "%d", config.c_funambol_port);
46         sock = sock_connect(config.c_funambol_host, port, "tcp");
47         if (sock >= 0) 
48                 CtdlLogPrintf(CTDL_DEBUG, "Connected to Funambol!\n");
49         else {
50                 char buf[SIZ];
51
52                 snprintf(buf, SIZ, 
53                          "Unable to connect to %s:%d [%s]; won't send notification\r\n", 
54                          config.c_funambol_host, 
55                          config.c_funambol_port, 
56                          strerror(errno));
57                 CtdlLogPrintf(CTDL_ERR, buf);
58
59                 aide_message(buf, "External notifier unable to connect remote host!");
60                 goto bail;
61         }
62         // Load the template SOAP message. Get mallocs done too
63         template = fopen(file_funambol_msg, "r");
64
65         if (template == NULL) {
66                 char buf[SIZ];
67
68                 snprintf(buf, SIZ, 
69                          "Cannot load template file %s [%s]won't send notification\r\n", 
70                          file_funambol_msg, strerror(errno));
71                 CtdlLogPrintf(CTDL_ERR, buf);
72
73                 aide_message(buf, "External notifier unable to find message template!");
74                 goto free;
75         }
76
77
78         buf = malloc(SIZ);
79         memset(buf, 0, SIZ);
80         SOAPMessage = malloc(3072);
81         memset(SOAPMessage, 0, 3072);
82         
83         SOAPHeader  = malloc(SIZ);
84         memset(SOAPHeader, 0, SIZ);
85         
86         funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
87         memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
88         
89         while(fgets(buf, SIZ, template) != NULL) {
90                 strcat(SOAPMessage, buf);
91         }
92         fclose(template);
93         
94         if (strlen(SOAPMessage) < 0) {
95                 char buf[SIZ];
96
97                 snprintf(buf, SIZ, 
98                          "Cannot load template file %s; won't send notification\r\n", 
99                          file_funambol_msg);
100                 CtdlLogPrintf(CTDL_ERR, buf);
101
102                 aide_message(buf, "External notifier unable to load message template!");
103                 goto free;
104         }
105         // Do substitutions
106         help_subst(SOAPMessage, "^notifyuser", user);
107         help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
108         
109         /* Build the HTTP request header */
110
111         
112         sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
113                 FUNAMBOL_WS);
114         strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
115         sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
116                 "Citadel",
117                 REV_LEVEL,
118                 config.c_funambol_host,
119                 config.c_funambol_port
120                 );
121         strcat(SOAPHeader,buf);
122         strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
123         sprintf(buf, "Content-Length: %d \r\n",
124                 strlen(SOAPMessage));
125         strcat(SOAPHeader, buf);
126         
127         
128
129         CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
130         
131         
132         sprintf(buf, "Authorization: Basic %s\r\n\r\n",
133                 funambolCreds);
134         strcat(SOAPHeader, buf);
135         
136         sock_write(sock, SOAPHeader, strlen(SOAPHeader));
137         sock_write(sock, SOAPMessage, strlen(SOAPMessage));
138         sock_shutdown(sock, SHUT_WR);
139         
140         /* Response */
141         CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
142         if (sock_getln(sock, buf, SIZ) < 0) {
143                 goto free;
144         }
145         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
146         if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
147                 
148                 goto free;
149         }
150         CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
151 free:
152         if (funambolCreds != NULL) free(funambolCreds);
153         if (SOAPMessage != NULL) free(SOAPMessage);
154         if (buf != NULL) free(buf);
155         if (SOAPHeader != NULL) free(SOAPHeader);
156 bail:
157         close(sock);
158         return 0;
159 }
160