* add message ID to the notification template; not used for funnambol, but for possib...
[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, char *msgid) {
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         help_subst(SOAPMessage, "^msgid", msgid);
109         
110         /* Build the HTTP request header */
111
112         
113         sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
114                 FUNAMBOL_WS);
115         strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
116         sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
117                 "Citadel",
118                 REV_LEVEL,
119                 config.c_funambol_host,
120                 config.c_funambol_port
121                 );
122         strcat(SOAPHeader,buf);
123         strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
124         sprintf(buf, "Content-Length: %d \r\n",
125                 strlen(SOAPMessage));
126         strcat(SOAPHeader, buf);
127         
128         
129
130         CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
131         
132         
133         sprintf(buf, "Authorization: Basic %s\r\n\r\n",
134                 funambolCreds);
135         strcat(SOAPHeader, buf);
136         
137         sock_write(sock, SOAPHeader, strlen(SOAPHeader));
138         sock_write(sock, SOAPMessage, strlen(SOAPMessage));
139         sock_shutdown(sock, SHUT_WR);
140         
141         /* Response */
142         CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
143         if (sock_getln(sock, buf, SIZ) < 0) {
144                 goto free;
145         }
146         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
147         if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
148                 
149                 goto free;
150         }
151         CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
152 free:
153         if (funambolCreds != NULL) free(funambolCreds);
154         if (SOAPMessage != NULL) free(SOAPMessage);
155         if (buf != NULL) free(buf);
156         if (SOAPHeader != NULL) free(SOAPHeader);
157 bail:
158         close(sock);
159         return 0;
160 }
161