f73ef04b8cff62095b4c48787ca1385ac785dbf9
[citadel.git] / citadel / serv_funambol.c
1 /*
2  * This module implements a notifier for Funambol push email.
3  */
4
5 #define FUNAMBOL_WS       "/funambol/services/admin"
6 #include "sysdep.h"
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <signal.h>
12 #include <pwd.h>
13 #include <errno.h>
14 #include <sys/types.h>
15
16 #if TIME_WITH_SYS_TIME
17 # include <sys/time.h>
18 # include <time.h>
19 #else
20 # if HAVE_SYS_TIME_H
21 #  include <sys/time.h>
22 # else
23 #  include <time.h>
24 # endif
25 #endif
26
27 #include <sys/wait.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <sys/socket.h>
31 #include "citadel.h"
32 #include "server.h"
33 #include "sysdep_decls.h"
34 #include "citserver.h"
35 #include "support.h"
36 #include "config.h"
37 #include "control.h"
38 #include "serv_extensions.h"
39 #include "room_ops.h"
40 #include "user_ops.h"
41 #include "policy.h"
42 #include "database.h"
43 #include "msgbase.h"
44 #include "tools.h"
45 #include "internet_addressing.h"
46 #include "domain.h"
47 #include "clientsocket.h"
48
49
50
51 /*
52  * Connect to the Funambol server and scan a message.
53  */
54 int notify_funambol(struct CtdlMessage *msg) {
55         int sock = (-1);
56         char buf[SIZ];
57         char SOAPHeader[SIZ];
58         char SOAPData[SIZ];
59         char port[SIZ];
60         /* W means 'Wireless'... */
61         if ( msg->cm_fields['W'] == NULL) {
62                 return(0);
63         }
64         /* Are we allowed to push? */
65         if ( strlen(config.c_funambol_host) == 0) {
66                 return (0);
67         } else {
68                 lprintf(CTDL_INFO, "Push enabled\n");
69         }
70         sprintf(port, "%d", config.c_funambol_port);
71                 lprintf(CTDL_INFO, "Connecting to Funambol at <%s>\n", config.c_funambol_host);
72                 sock = sock_connect(config.c_funambol_host, port, "tcp");
73                 if (sock >= 0) lprintf(CTDL_DEBUG, "Connected!\n");
74
75         if (sock < 0) {
76                 /* If the service isn't running, just pass the mail
77                  * through.  Potentially throwing away mails isn't good.
78                  */
79                 return(0);
80         }
81         
82         /* Build a SOAP message, delicately, by hand */
83         strcat(SOAPData, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
84         strcat(SOAPData, "<soapenv:Body><sendNotificationMessages soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
85         strcat(SOAPData, "<arg0 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">");
86         strcat(SOAPData, msg->cm_fields['W']);
87         strcat(SOAPData, "</arg0>");
88         strcat(SOAPData, "<arg1 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\r\n");
89         strcat(SOAPData, "&lt;java version=&quot;1.5.0_10&quot; class=&quot;java.beans.XMLDecoder&quot;&gt; \r\n");
90         strcat(SOAPData, " &lt;array class=&quot;com.funambol.framework.core.Alert&quot; length=&quot;1&quot;&gt;\r\n");
91         strcat(SOAPData, "  &lt;void index=&quot;0&quot;&gt;\r\n");
92         strcat(SOAPData, "   &lt;object class=&quot;com.funambol.framework.core.Alert&quot;&gt;\r\n");
93         strcat(SOAPData, "    &lt;void property=&quot;cmdID&quot;>\r\n");
94         strcat(SOAPData, "     &lt;object class=&quot;com.funambol.framework.core.CmdID&quot;/&gt;\r\n");
95         strcat(SOAPData, "    &lt;/void&gt;");
96         strcat(SOAPData, "    &lt;void property=&quot;data&quot;&gt;\r\n");
97         strcat(SOAPData, "     &lt;int&gt;210&lt;/int&gt;\r\n");
98         strcat(SOAPData, "    &lt;/void&gt;\r\n");
99         strcat(SOAPData, "    &lt;void property=&quot;items&quot;&gt;\r\n");
100         strcat(SOAPData, "     &lt;void method=&quot;add&quot;&gt;\r\n"); 
101         strcat(SOAPData, "      &lt;object class=&quot;com.funambol.framework.core.Item&quot;&gt;\r\n"); 
102         strcat(SOAPData, "       &lt;void property=&quot;meta&quot;&gt;\r\n"); 
103         strcat(SOAPData, "        &lt;object class=&quot;com.funambol.framework.core.Meta&quot;&gt;\r\n"); 
104         strcat(SOAPData, "         &lt;void property=&quot;metInf&quot;&gt;\r\n");
105         strcat(SOAPData, "          &lt;void property=&quot;type&quot;&gt;\r\n");
106         strcat(SOAPData, "           &lt;string&gt;application/vnd.omads-email+xml&lt;/string&gt;\r\n");
107         strcat(SOAPData, "          &lt;/void&gt;\r\n"); 
108         strcat(SOAPData, "         &lt;/void&gt;\r\n"); 
109         strcat(SOAPData, "        &lt;/object&gt;\r\n"); 
110         strcat(SOAPData, "       &lt;/void&gt;\r\n"); 
111         strcat(SOAPData, "       &lt;void property=&quot;target&quot;&gt;\r\n"); 
112         strcat(SOAPData, "        &lt;object class=&quot;com.funambol.framework.core.Target&quot;&gt;\r\n");
113         strcat(SOAPData, "         &lt;void property=&quot;locURI&quot;&gt;\r\n");
114         strcat(SOAPData, "          &lt;string&gt;");
115         strcat(SOAPData, config.c_funambol_source);
116         strcat(SOAPData, "&lt;/string&gt;\r\n");
117         strcat(SOAPData, "         &lt;/void&gt;\r\n");
118         strcat(SOAPData, "        &lt;/object&gt;\r\n");
119         strcat(SOAPData, "       &lt;/void&gt;\r\n");
120         strcat(SOAPData, "      &lt;/object&gt;\r\n");
121         strcat(SOAPData, "     &lt;/void&gt;\r\n");
122         strcat(SOAPData, "    &lt;/void&gt;\r\n");
123         strcat(SOAPData, "   &lt;/object&gt;\r\n");
124         strcat(SOAPData, "  &lt;/void&gt;\r\n");
125         strcat(SOAPData, " &lt;/array&gt;\r\n");
126         strcat(SOAPData, "&lt;/java&gt;");
127         strcat(SOAPData,"</arg1><arg2 href=\"#id0\"/></sendNotificationMessages><multiRef id=\"id0\" soapenc:root=\"0\"\r\n");
128         strcat(SOAPData,"soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"soapenc:int\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">1</multiRef></soapenv:Body></soapenv:Envelope>");
129         
130         /* Command */
131         lprintf(CTDL_DEBUG, "Transmitting command\n");
132         sprintf(buf, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
133                 FUNAMBOL_WS);
134         strcat(SOAPHeader,buf);
135         strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
136         sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
137                 "Citadel",
138                 REV_LEVEL,
139                 config.c_funambol_host,
140                 config.c_funambol_port
141                 );
142                 strcat(SOAPHeader,buf);
143         strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
144         sprintf(buf, "Content-Length: %d\r\n",
145                 strlen(SOAPData));
146         strcat(SOAPHeader, buf);
147         sprintf(buf, "Authorization: Basic %s\r\n\r\n",
148                 config.c_funambol_auth);
149         strcat(SOAPHeader, buf);
150         
151         sock_write(sock, SOAPHeader, strlen(SOAPHeader));
152         sock_write(sock, SOAPData, strlen(SOAPData));
153         sock_shutdown(sock, SHUT_WR);
154         
155         /* Response */
156         lprintf(CTDL_DEBUG, "Awaiting response\n");
157         if (sock_gets(sock, buf) < 0) {
158                 goto bail;
159         }
160         lprintf(CTDL_DEBUG, "<%s\n", buf);
161         if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
162                 goto bail;
163         }
164         lprintf(CTDL_DEBUG, "Funambol notified\n");
165
166 bail:   close(sock);
167         return(0);
168 }
169
170
171
172 char *serv_funambol_init(void)
173 {
174         CtdlRegisterMessageHook(notify_funambol, EVT_AFTERSAVE);
175         return "$Id: serv_funambol.c $";
176 }