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