3 * @author Mathew McBride
5 * This module facilitates notifications to a Funambol server
8 * Based on bits of the previous serv_funambol
9 * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
11 #include "extnotify.h"
16 #include <sys/socket.h>
18 #include <libcitadel.h>
22 #include "citadel_dirs.h"
23 #include "clientsocket.h"
26 #include "sysdep_decls.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
) {
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");
47 CtdlLogPrintf(CTDL_DEBUG
, "Connected to Funambol!\n");
52 "Unable to connect to %s:%d [%s]; won't send notification\r\n",
53 config
.c_funambol_host
,
54 config
.c_funambol_port
,
56 CtdlLogPrintf(CTDL_ERR
, buf
);
58 aide_message(buf
, "External notifier unable to connect remote host!");
61 // Load the template SOAP message. Get mallocs done too
62 template = fopen(file_funambol_msg
, "r");
64 if (template == NULL
) {
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!");
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
);
93 if (strlen(SOAPMessage
) < 0) {
97 "Cannot load template file %s; won't send notification\r\n",
99 CtdlLogPrintf(CTDL_ERR
, buf
);
101 aide_message(buf
, "External notifier unable to load message template!");
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",
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",
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",
133 strcat(SOAPHeader
, buf
);
135 sock_write(sock
, SOAPHeader
, strlen(SOAPHeader
));
136 sock_write(sock
, SOAPMessage
, strlen(SOAPMessage
));
137 sock_shutdown(sock
, SHUT_WR
);
140 CtdlLogPrintf(CTDL_DEBUG
, "Awaiting response\n");
141 if (sock_getln(sock
, buf
, SIZ
) < 0) {
144 CtdlLogPrintf(CTDL_DEBUG
, "<%s\n", buf
);
145 if (strncasecmp(buf
, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
149 CtdlLogPrintf(CTDL_DEBUG
, "Funambol notified\n");
151 if (funambolCreds
!= NULL
) free(funambolCreds
);
152 if (SOAPMessage
!= NULL
) free(SOAPMessage
);
153 if (buf
!= NULL
) free(buf
);
154 if (SOAPHeader
!= NULL
) free(SOAPHeader
);