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