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>
23 #include "citadel_dirs.h"
24 #include "clientsocket.h"
27 #include "sysdep_decls.h"
29 #include "ctdl_module.h"
32 * \brief Sends a message to the Funambol server notifying
33 * of new mail for a user
34 * Returns 0 if unsuccessful
36 int notify_funambol_server(char *user) {
40 char *SOAPMessage = NULL;
41 char *SOAPHeader = NULL;
42 char *funambolCreds = NULL;
43 FILE *template = NULL;
45 sprintf(port, "%d", config.c_funambol_port);
46 sock = sock_connect(config.c_funambol_host, port, "tcp");
48 CtdlLogPrintf(CTDL_DEBUG, "Connected to Funambol!\n");
53 "Unable to connect to %s:%d [%s]; won't send notification\r\n",
54 config.c_funambol_host,
55 config.c_funambol_port,
57 CtdlLogPrintf(CTDL_ERR, buf);
59 aide_message(buf, "External notifier unable to connect remote host!");
62 // Load the template SOAP message. Get mallocs done too
63 template = fopen(file_funambol_msg, "r");
65 if (template == NULL) {
69 "Cannot load template file %s [%s]won't send notification\r\n",
70 file_funambol_msg, strerror(errno));
71 CtdlLogPrintf(CTDL_ERR, buf);
73 aide_message(buf, "External notifier unable to find message template!");
80 SOAPMessage = malloc(3072);
81 memset(SOAPMessage, 0, 3072);
83 SOAPHeader = malloc(SIZ);
84 memset(SOAPHeader, 0, SIZ);
86 funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
87 memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
89 while(fgets(buf, SIZ, template) != NULL) {
90 strcat(SOAPMessage, buf);
94 if (strlen(SOAPMessage) < 0) {
98 "Cannot load template file %s; won't send notification\r\n",
100 CtdlLogPrintf(CTDL_ERR, buf);
102 aide_message(buf, "External notifier unable to load message template!");
106 help_subst(SOAPMessage, "^notifyuser", user);
107 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
109 /* Build the HTTP request header */
112 sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
114 strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
115 sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
118 config.c_funambol_host,
119 config.c_funambol_port
121 strcat(SOAPHeader,buf);
122 strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
123 sprintf(buf, "Content-Length: %d \r\n",
124 strlen(SOAPMessage));
125 strcat(SOAPHeader, buf);
129 CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
132 sprintf(buf, "Authorization: Basic %s\r\n\r\n",
134 strcat(SOAPHeader, buf);
136 sock_write(sock, SOAPHeader, strlen(SOAPHeader));
137 sock_write(sock, SOAPMessage, strlen(SOAPMessage));
138 sock_shutdown(sock, SHUT_WR);
141 CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
142 if (sock_getln(sock, buf, SIZ) < 0) {
145 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
146 if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
150 CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
152 if (funambolCreds != NULL) free(funambolCreds);
153 if (SOAPMessage != NULL) free(SOAPMessage);
154 if (buf != NULL) free(buf);
155 if (SOAPHeader != NULL) free(SOAPHeader);