* add webcit database message ID to the notification template.
[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, long MsgNum) {
37         char port[1024];
38         char msgnumstr[128];
39         int sock = -1;
40         char *buf = NULL;
41         char *SOAPMessage = NULL;
42         char *SOAPHeader = NULL;
43         char *funambolCreds = NULL;
44         FILE *template = NULL;
45         
46         sprintf(port, "%d", config.c_funambol_port);
47         sock = sock_connect(config.c_funambol_host, port, "tcp");
48         if (sock >= 0) 
49                 CtdlLogPrintf(CTDL_DEBUG, "Connected to Funambol!\n");
50         else {
51                 char buf[SIZ];
52
53                 snprintf(buf, SIZ, 
54                          "Unable to connect to %s:%d [%s]; won't send notification\r\n", 
55                          config.c_funambol_host, 
56                          config.c_funambol_port, 
57                          strerror(errno));
58                 CtdlLogPrintf(CTDL_ERR, buf);
59
60                 aide_message(buf, "External notifier unable to connect remote host!");
61                 goto bail;
62         }
63         // Load the template SOAP message. Get mallocs done too
64         template = fopen(file_funambol_msg, "r");
65
66         if (template == NULL) {
67                 char buf[SIZ];
68
69                 snprintf(buf, SIZ, 
70                          "Cannot load template file %s [%s]won't send notification\r\n", 
71                          file_funambol_msg, strerror(errno));
72                 CtdlLogPrintf(CTDL_ERR, buf);
73
74                 aide_message(buf, "External notifier unable to find message template!");
75                 goto free;
76         }
77         snprintf(msgnumstr, 128, "%ld", MsgNum);
78
79         buf = malloc(SIZ);
80         memset(buf, 0, SIZ);
81         SOAPMessage = malloc(3072);
82         memset(SOAPMessage, 0, 3072);
83         
84         SOAPHeader  = malloc(SIZ);
85         memset(SOAPHeader, 0, SIZ);
86         
87         funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
88         memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
89         
90         while(fgets(buf, SIZ, template) != NULL) {
91                 strcat(SOAPMessage, buf);
92         }
93         fclose(template);
94         
95         if (strlen(SOAPMessage) < 0) {
96                 char buf[SIZ];
97
98                 snprintf(buf, SIZ, 
99                          "Cannot load template file %s; won't send notification\r\n", 
100                          file_funambol_msg);
101                 CtdlLogPrintf(CTDL_ERR, buf);
102
103                 aide_message(buf, "External notifier unable to load message template!");
104                 goto free;
105         }
106         // Do substitutions
107         help_subst(SOAPMessage, "^notifyuser", user);
108         help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
109         help_subst(SOAPMessage, "^msgid", msgid);
110         help_subst(SOAPMessage, "^msgnum", msgnumstr);
111
112         /* Build the HTTP request header */
113
114         
115         sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
116                 FUNAMBOL_WS);
117         strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
118         sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
119                 "Citadel",
120                 REV_LEVEL,
121                 config.c_funambol_host,
122                 config.c_funambol_port
123                 );
124         strcat(SOAPHeader,buf);
125         strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
126         sprintf(buf, "Content-Length: %d \r\n",
127                 strlen(SOAPMessage));
128         strcat(SOAPHeader, buf);
129         
130         
131
132         CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
133         
134         
135         sprintf(buf, "Authorization: Basic %s\r\n\r\n",
136                 funambolCreds);
137         strcat(SOAPHeader, buf);
138         
139         sock_write(sock, SOAPHeader, strlen(SOAPHeader));
140         sock_write(sock, SOAPMessage, strlen(SOAPMessage));
141         sock_shutdown(sock, SHUT_WR);
142         
143         /* Response */
144         CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
145         if (sock_getln(sock, buf, SIZ) < 0) {
146                 goto free;
147         }
148         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
149         if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
150                 
151                 goto free;
152         }
153         CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
154 free:
155         if (funambolCreds != NULL) free(funambolCreds);
156         if (SOAPMessage != NULL) free(SOAPMessage);
157         if (buf != NULL) free(buf);
158         if (SOAPHeader != NULL) free(SOAPHeader);
159 bail:
160         close(sock);
161         return 0;
162 }
163