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, char *msgid, long MsgNum) {
41 char *SOAPMessage = NULL;
42 char *SOAPHeader = NULL;
43 char *funambolCreds = NULL;
44 FILE *template = NULL;
46 sprintf(port, "%d", config.c_funambol_port);
47 sock = sock_connect(config.c_funambol_host, port, "tcp");
49 CtdlLogPrintf(CTDL_DEBUG, "Connected to Funambol!\n");
54 "Unable to connect to %s:%d [%s]; won't send notification\r\n",
55 config.c_funambol_host,
56 config.c_funambol_port,
58 CtdlLogPrintf(CTDL_ERR, buf);
60 aide_message(buf, "External notifier unable to connect remote host!");
63 // Load the template SOAP message. Get mallocs done too
64 template = fopen(file_funambol_msg, "r");
66 if (template == NULL) {
70 "Cannot load template file %s [%s]won't send notification\r\n",
71 file_funambol_msg, strerror(errno));
72 CtdlLogPrintf(CTDL_ERR, buf);
74 aide_message(buf, "External notifier unable to find message template!");
77 snprintf(msgnumstr, 128, "%ld", MsgNum);
81 SOAPMessage = malloc(3072);
82 memset(SOAPMessage, 0, 3072);
84 SOAPHeader = malloc(SIZ);
85 memset(SOAPHeader, 0, SIZ);
87 funambolCreds = malloc(strlen(config.c_funambol_auth)*2);
88 memset(funambolCreds, 0, strlen(config.c_funambol_auth)*2);
90 while(fgets(buf, SIZ, template) != NULL) {
91 strcat(SOAPMessage, buf);
95 if (strlen(SOAPMessage) < 0) {
99 "Cannot load template file %s; won't send notification\r\n",
101 CtdlLogPrintf(CTDL_ERR, buf);
103 aide_message(buf, "External notifier unable to load message template!");
107 help_subst(SOAPMessage, "^notifyuser", user);
108 help_subst(SOAPMessage, "^syncsource", config.c_funambol_source);
109 help_subst(SOAPMessage, "^msgid", msgid);
110 help_subst(SOAPMessage, "^msgnum", msgnumstr);
112 /* Build the HTTP request header */
115 sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
117 strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
118 sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
121 config.c_funambol_host,
122 config.c_funambol_port
124 strcat(SOAPHeader,buf);
125 strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
126 sprintf(buf, "Content-Length: %d \r\n",
127 strlen(SOAPMessage));
128 strcat(SOAPHeader, buf);
132 CtdlEncodeBase64(funambolCreds, config.c_funambol_auth, strlen(config.c_funambol_auth), 0);
135 sprintf(buf, "Authorization: Basic %s\r\n\r\n",
137 strcat(SOAPHeader, buf);
139 sock_write(sock, SOAPHeader, strlen(SOAPHeader));
140 sock_write(sock, SOAPMessage, strlen(SOAPMessage));
141 sock_shutdown(sock, SHUT_WR);
144 CtdlLogPrintf(CTDL_DEBUG, "Awaiting response\n");
145 if (sock_getln(sock, buf, SIZ) < 0) {
148 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
149 if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
153 CtdlLogPrintf(CTDL_DEBUG, "Funambol notified\n");
155 if (funambolCreds != NULL) free(funambolCreds);
156 if (SOAPMessage != NULL) free(SOAPMessage);
157 if (buf != NULL) free(buf);
158 if (SOAPHeader != NULL) free(SOAPHeader);