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>
20 #include "citadel_dirs.h"
21 #include "clientsocket.h"
23 #include "sysconfig.h"
25 #include "sysdep_decls.h"
28 * \brief Sends a message to the Funambol server notifying
29 * of new mail for a user
30 * Returns 0 if unsuccessful
32 int notify_funambol_server(char *user) {
42 sprintf(port, "%d", config.c_funambol_port);
43 sock = sock_connect(config.c_funambol_host, port, "tcp");
45 lprintf(CTDL_DEBUG, "Connected to Funambol!\n");
48 // Load the template SOAP message
49 template = fopen(file_funambol_msg, "r");
52 SOAPMessage = malloc(3072);
53 memset(SOAPMessage, 0, 3072);
54 while(fgets(buf, SIZ, template) != NULL) {
55 strcat(SOAPMessage, buf);
59 if (strlen(SOAPMessage) < 0) {
60 printf("Cannot load template file\r\n");
64 help_subst(SOAPMessage, "^notifyuser", user);
65 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
67 /* Build the HTTP request header */
68 SOAPHeader = malloc(SIZ);
69 memset(SOAPHeader, 0, SIZ);
70 sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
72 strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
73 sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
76 config.c_funambol_host,
77 config.c_funambol_port
79 strcat(SOAPHeader,buf);
80 strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
81 sprintf(buf, "Content-Length: %d \r\n",
83 strcat(SOAPHeader, buf);
85 funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
86 memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
88 CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
91 sprintf(buf, "Authorization: Basic %s\r\n\r\n",
93 strcat(SOAPHeader, buf);
95 int written_header = sock_write(sock, SOAPHeader, strlen(SOAPHeader));
96 int written_body = sock_write(sock, SOAPMessage, strlen(SOAPMessage));
97 sock_shutdown(sock, SHUT_WR);
100 lprintf(CTDL_DEBUG, "Awaiting response\n");
101 if (sock_getln(sock, buf, SIZ) < 0) {
104 lprintf(CTDL_DEBUG, "<%s\n", buf);
105 if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
109 lprintf(CTDL_DEBUG, "Funambol notified\n");