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>
21 #include "citadel_dirs.h"
22 #include "clientsocket.h"
25 #include "sysdep_decls.h"
27 #include "ctdl_module.h"
30 * \brief Sends a message to the Funambol server notifying
31 * of new mail for a user
32 * Returns 0 if unsuccessful
34 int notify_funambol_server(char *user) {
43 sprintf(port, "%d", config.c_funambol_port);
44 sock = sock_connect(config.c_funambol_host, port, "tcp");
46 CtdlLogPrintf(CTDL_DEBUG, "Connected to Funambol!\n");
49 // Load the template SOAP message. Get mallocs done too
50 template = fopen(file_funambol_msg, "r");
53 SOAPMessage = malloc(3072);
54 memset(SOAPMessage, 0, 3072);
56 SOAPHeader = malloc(SIZ);
57 memset(SOAPHeader, 0, SIZ);
59 funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
60 memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
62 while(fgets(buf, SIZ, template) != NULL) {
63 strcat(SOAPMessage, buf);
67 if (strlen(SOAPMessage) < 0) {
68 printf("Cannot load template file\r\n");
72 help_subst(SOAPMessage, "^notifyuser", user);
73 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
75 /* Build the HTTP request header */
78 sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
80 strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
81 sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
84 config.c_funambol_host,
85 config.c_funambol_port
87 strcat(SOAPHeader,buf);
88 strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
89 sprintf(buf, "Content-Length: %d \r\n",
91 strcat(SOAPHeader, buf);
95 CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
98 sprintf(buf, "Authorization: Basic %s\r\n\r\n",
100 strcat(SOAPHeader, buf);
102 sock_write(sock, SOAPHeader, strlen(SOAPHeader));
103 sock_write(sock, SOAPMessage, strlen(SOAPMessage));
104 sock_shutdown(sock, SHUT_WR);
107 CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
108 if (sock_getln(sock, buf, SIZ) < 0) {
111 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
112 if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
116 CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
118 if (funambolCreds != NULL) free(funambolCreds);
119 if (SOAPMessage != NULL) free(SOAPMessage);
120 if (buf != NULL) free(buf);
121 if (SOAPHeader != NULL) free(SOAPHeader);