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. Get mallocs done too
49 template = fopen(file_funambol_msg, "r");
52 SOAPMessage = malloc(3072);
53 memset(SOAPMessage, 0, 3072);
55 SOAPHeader = malloc(SIZ);
56 memset(SOAPHeader, 0, SIZ);
58 funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
59 memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
61 while(fgets(buf, SIZ, template) != NULL) {
62 strcat(SOAPMessage, buf);
66 if (strlen(SOAPMessage) < 0) {
67 printf("Cannot load template file\r\n");
71 help_subst(SOAPMessage, "^notifyuser", user);
72 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
74 /* Build the HTTP request header */
77 sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
79 strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
80 sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
83 config.c_funambol_host,
84 config.c_funambol_port
86 strcat(SOAPHeader,buf);
87 strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
88 sprintf(buf, "Content-Length: %d \r\n",
90 strcat(SOAPHeader, buf);
94 CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
97 sprintf(buf, "Authorization: Basic %s\r\n\r\n",
99 strcat(SOAPHeader, buf);
101 int written_header = sock_write(sock, SOAPHeader, strlen(SOAPHeader));
102 int written_body = sock_write(sock, SOAPMessage, strlen(SOAPMessage));
103 sock_shutdown(sock, SHUT_WR);
106 lprintf(CTDL_DEBUG, "Awaiting response\n");
107 if (sock_getln(sock, buf, SIZ) < 0) {
110 lprintf(CTDL_DEBUG, "<%s\n", buf);
111 if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
115 lprintf(CTDL_DEBUG, "Funambol notified\n");
117 if (funambolCreds != NULL) free(funambolCreds);
118 if (SOAPMessage != NULL) free(SOAPMessage);
119 if (buf != NULL) free(buf);
120 if (SOAPHeader != NULL) free(SOAPHeader);