* Fixed a multiselect bug in the mailbox view. Ctrl-click was selecting a message...
[citadel.git] / citadel / modules / extnotify / funambol65.c
blob90fe4eb0ced41745c1b23eba1ed003a2b3fb10bd
1 /*
2 * \file funambol65.c
3 * @author Mathew McBride
4 *
5 * This module facilitates notifications to a Funambol server
6 * for push email
8 * Based on bits of the previous serv_funambol
9 * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
11 #include "extnotify.h"
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>
21 #include "citadel.h"
22 #include "citadel_dirs.h"
23 #include "clientsocket.h"
24 #include "sysdep.h"
25 #include "config.h"
26 #include "sysdep_decls.h"
27 #include "msgbase.h"
28 #include "ctdl_module.h"
31 * \brief Sends a message to the Funambol server notifying
32 * of new mail for a user
33 * Returns 0 if unsuccessful
35 int notify_funambol_server(char *user) {
36 char port[1024];
37 int sock = -1;
38 char *buf = NULL;
39 char *SOAPMessage = NULL;
40 char *SOAPHeader = NULL;
41 char *funambolCreds = NULL;
42 FILE *template = NULL;
44 sprintf(port, "%d", config.c_funambol_port);
45 sock = sock_connect(config.c_funambol_host, port, "tcp");
46 if (sock >= 0)
47 CtdlLogPrintf(CTDL_DEBUG, "Connected to Funambol!\n");
48 else {
49 char buf[SIZ];
51 snprintf(buf, SIZ,
52 "Unable to connect to %s:%d [%s]; won't send notification\r\n",
53 config.c_funambol_host,
54 config.c_funambol_port,
55 strerror(errno));
56 CtdlLogPrintf(CTDL_ERR, buf);
58 aide_message(buf, "External notifier unable to connect remote host!");
59 goto bail;
61 // Load the template SOAP message. Get mallocs done too
62 template = fopen(file_funambol_msg, "r");
64 if (template == NULL) {
65 char buf[SIZ];
67 snprintf(buf, SIZ,
68 "Cannot load template file %s [%s]won't send notification\r\n",
69 file_funambol_msg, strerror(errno));
70 CtdlLogPrintf(CTDL_ERR, buf);
72 aide_message(buf, "External notifier unable to find message template!");
73 goto free;
77 buf = malloc(SIZ);
78 memset(buf, 0, SIZ);
79 SOAPMessage = malloc(3072);
80 memset(SOAPMessage, 0, 3072);
82 SOAPHeader = malloc(SIZ);
83 memset(SOAPHeader, 0, SIZ);
85 funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
86 memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
88 while(fgets(buf, SIZ, template) != NULL) {
89 strcat(SOAPMessage, buf);
91 fclose(template);
93 if (strlen(SOAPMessage) < 0) {
94 char buf[SIZ];
96 snprintf(buf, SIZ,
97 "Cannot load template file %s; won't send notification\r\n",
98 file_funambol_msg);
99 CtdlLogPrintf(CTDL_ERR, buf);
101 aide_message(buf, "External notifier unable to load message template!");
102 goto free;
104 // Do substitutions
105 help_subst(SOAPMessage, "^notifyuser", user);
106 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
108 /* Build the HTTP request header */
111 sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
112 FUNAMBOL_WS);
113 strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
114 sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
115 "Citadel",
116 REV_LEVEL,
117 config.c_funambol_host,
118 config.c_funambol_port
120 strcat(SOAPHeader,buf);
121 strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
122 sprintf(buf, "Content-Length: %d \r\n",
123 strlen(SOAPMessage));
124 strcat(SOAPHeader, buf);
128 CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
131 sprintf(buf, "Authorization: Basic %s\r\n\r\n",
132 funambolCreds);
133 strcat(SOAPHeader, buf);
135 sock_write(sock, SOAPHeader, strlen(SOAPHeader));
136 sock_write(sock, SOAPMessage, strlen(SOAPMessage));
137 sock_shutdown(sock, SHUT_WR);
139 /* Response */
140 CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
141 if (sock_getln(sock, buf, SIZ) < 0) {
142 goto free;
144 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
145 if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
147 goto free;
149 CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
150 free:
151 if (funambolCreds != NULL) free(funambolCreds);
152 if (SOAPMessage != NULL) free(SOAPMessage);
153 if (buf != NULL) free(buf);
154 if (SOAPHeader != NULL) free(SOAPHeader);
155 bail:
156 close(sock);
157 return 0;