Applied Matt's new patch. Adds 'extnotify' and removes
[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
20 #include "citadel_dirs.h"
21 #include "clientsocket.h"
22 #include "sysdep.h"
23 #include "sysconfig.h"
24 #include "config.h"
25 #include "sysdep_decls.h"
26
27 /*
28 * \brief Sends a message to the Funambol server notifying 
29 * of new mail for a user
30 * Returns 0 if unsuccessful
31 */
32 int notify_funambol_server(char *user) {
33         char port[1024];
34         int sock = -1;
35         char *buf;
36         char *SOAPMessage;
37         char *SOAPHeader;
38         char *funambolCreds;
39         FILE *template;
40         FILE *fnblConf;
41         
42         sprintf(port, "%d", config.c_funambol_port);
43         sock = sock_connect(config.c_funambol_host, port, "tcp");
44         if (sock >= 0) 
45                 lprintf(CTDL_DEBUG, "Connected to Funambol!\n");
46         else 
47                 goto bail;
48         // Load the template SOAP message
49         template = fopen(file_funambol_msg, "r");
50         buf = malloc(SIZ);
51         memset(buf, 0, SIZ);
52         SOAPMessage = malloc(3072);
53         memset(SOAPMessage, 0, 3072);
54         while(fgets(buf, SIZ, template) != NULL) {
55                 strcat(SOAPMessage, buf);
56         }
57         fclose(template);
58         
59         if (strlen(SOAPMessage) < 0) {
60                 printf("Cannot load template file\r\n");
61                 goto bail;
62         }
63         // Do substitutions
64         help_subst(SOAPMessage, "^notifyuser", user);
65         help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
66         
67         /* Build the HTTP request header */
68         SOAPHeader  = malloc(SIZ);
69         memset(SOAPHeader, 0, SIZ);
70         sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
71                 FUNAMBOL_WS);
72         strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
73         sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
74                 "Citadel",
75                 REV_LEVEL,
76                 config.c_funambol_host,
77                 config.c_funambol_port
78                 );
79         strcat(SOAPHeader,buf);
80         strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
81         sprintf(buf, "Content-Length: %d \r\n",
82                 strlen(SOAPMessage));
83         strcat(SOAPHeader, buf);
84         
85         funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
86         memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
87
88         CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
89         
90         
91         sprintf(buf, "Authorization: Basic %s\r\n\r\n",
92                 funambolCreds);
93         strcat(SOAPHeader, buf);
94         
95         int written_header = sock_write(sock, SOAPHeader, strlen(SOAPHeader));
96         int written_body = sock_write(sock, SOAPMessage, strlen(SOAPMessage));
97         sock_shutdown(sock, SHUT_WR);
98         
99         /* Response */
100         lprintf(CTDL_DEBUG, "Awaiting response\n");
101         if (sock_getln(sock, buf, SIZ) < 0) {
102                 goto bail;
103         }
104         lprintf(CTDL_DEBUG, "<%s\n", buf);
105         if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
106                 
107                 goto bail;
108         }
109         lprintf(CTDL_DEBUG, "Funambol notified\n");
110         
111 bail:
112         close(sock);
113         free(funambolCreds);
114         free(SOAPMessage);
115         free(buf);
116         free(SOAPHeader);
117         return 0;
118 }
119