* Applied matt's funambol patch
[citadel.git] / citadel / serv_funambol.c
1 /*
2  * This module implements a notifier for Funambol push email.
3  * Based on bits of serv_spam, serv_smtp
4  */
5
6 #define FUNAMBOL_WS       "/funambol/services/admin"
7 #include "sysdep.h"
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <pwd.h>
14 #include <errno.h>
15 #include <sys/types.h>
16
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
19 # include <time.h>
20 #else
21 # if HAVE_SYS_TIME_H
22 #  include <sys/time.h>
23 # else
24 #  include <time.h>
25 # endif
26 #endif
27
28 #include <sys/wait.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <sys/socket.h>
32 #include "citadel.h"
33 #include "server.h"
34 #include "sysdep_decls.h"
35 #include "citserver.h"
36 #include "support.h"
37 #include "config.h"
38 #include "control.h"
39 #include "serv_extensions.h"
40 #include "room_ops.h"
41 #include "user_ops.h"
42 #include "policy.h"
43 #include "database.h"
44 #include "msgbase.h"
45 #include "tools.h"
46 #include "internet_addressing.h"
47 #include "domain.h"
48 #include "clientsocket.h"
49 #include "serv_funambol.h"
50 /*
51  * Create the notify message queue
52  */
53 void create_notify_queue(void) {
54         struct ctdlroom qrbuf;
55
56         create_room(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
57
58         /*
59          * Make sure it's set to be a "system room" so it doesn't show up
60          * in the <K>nown rooms list for Aides.
61          */
62         if (lgetroom(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
63                 qrbuf.QRflags2 |= QR2_SYSTEM;
64                 lputroom(&qrbuf);
65         }
66 }
67 /*
68  * Connect to the Funambol server and scan a message.
69  */
70 int notify_funambol(long msgnum, void *userdata) {
71         struct CtdlMessage *msg;
72         int sock = (-1);
73         char buf[SIZ];
74         char SOAPHeader[SIZ];
75         char SOAPData[SIZ];
76         char port[SIZ];
77         /* W means 'Wireless'... */
78         msg = CtdlFetchMessage(msgnum, 1);
79         if ( msg->cm_fields['W'] == NULL) {
80                 return(0);
81         }
82         /* Are we allowed to push? */
83         if ( strlen(config.c_funambol_host) == 0) {
84                 return (0);
85         } else {
86                 lprintf(CTDL_INFO, "Push enabled\n");
87         }
88         
89         sprintf(port, "%d", config.c_funambol_port);
90                 lprintf(CTDL_INFO, "Connecting to Funambol at <%s>\n", config.c_funambol_host);
91                 sock = sock_connect(config.c_funambol_host, port, "tcp");
92                 if (sock >= 0) lprintf(CTDL_DEBUG, "Connected!\n");
93
94         if (sock < 0) {
95                 /* If the service isn't running, pass for now */
96                 return(0);
97         }
98         
99         /* Build a SOAP message, delicately, by hand */
100         sprintf(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\">");
101         strcat(SOAPData, "<soapenv:Body><sendNotificationMessages soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
102         strcat(SOAPData, "<arg0 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">");
103         strcat(SOAPData, msg->cm_fields['W']);
104         strcat(SOAPData, "</arg0>");
105         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");
106         strcat(SOAPData, "&lt;java version=&quot;1.5.0_10&quot; class=&quot;java.beans.XMLDecoder&quot;&gt; \r\n");
107         strcat(SOAPData, " &lt;array class=&quot;com.funambol.framework.core.Alert&quot; length=&quot;1&quot;&gt;\r\n");
108         strcat(SOAPData, "  &lt;void index=&quot;0&quot;&gt;\r\n");
109         strcat(SOAPData, "   &lt;object class=&quot;com.funambol.framework.core.Alert&quot;&gt;\r\n");
110         strcat(SOAPData, "    &lt;void property=&quot;cmdID&quot;>\r\n");
111         strcat(SOAPData, "     &lt;object class=&quot;com.funambol.framework.core.CmdID&quot;/&gt;\r\n");
112         strcat(SOAPData, "    &lt;/void&gt;");
113         strcat(SOAPData, "    &lt;void property=&quot;data&quot;&gt;\r\n");
114         strcat(SOAPData, "     &lt;int&gt;210&lt;/int&gt;\r\n");
115         strcat(SOAPData, "    &lt;/void&gt;\r\n");
116         strcat(SOAPData, "    &lt;void property=&quot;items&quot;&gt;\r\n");
117         strcat(SOAPData, "     &lt;void method=&quot;add&quot;&gt;\r\n"); 
118         strcat(SOAPData, "      &lt;object class=&quot;com.funambol.framework.core.Item&quot;&gt;\r\n"); 
119         strcat(SOAPData, "       &lt;void property=&quot;meta&quot;&gt;\r\n"); 
120         strcat(SOAPData, "        &lt;object class=&quot;com.funambol.framework.core.Meta&quot;&gt;\r\n"); 
121         strcat(SOAPData, "         &lt;void property=&quot;metInf&quot;&gt;\r\n");
122         strcat(SOAPData, "          &lt;void property=&quot;type&quot;&gt;\r\n");
123         strcat(SOAPData, "           &lt;string&gt;application/vnd.omads-email+xml&lt;/string&gt;\r\n");
124         strcat(SOAPData, "          &lt;/void&gt;\r\n"); 
125         strcat(SOAPData, "         &lt;/void&gt;\r\n"); 
126         strcat(SOAPData, "        &lt;/object&gt;\r\n"); 
127         strcat(SOAPData, "       &lt;/void&gt;\r\n"); 
128         strcat(SOAPData, "       &lt;void property=&quot;target&quot;&gt;\r\n"); 
129         strcat(SOAPData, "        &lt;object class=&quot;com.funambol.framework.core.Target&quot;&gt;\r\n");
130         strcat(SOAPData, "         &lt;void property=&quot;locURI&quot;&gt;\r\n");
131         strcat(SOAPData, "          &lt;string&gt;");
132         strcat(SOAPData, config.c_funambol_source);
133         strcat(SOAPData, "&lt;/string&gt;\r\n");
134         strcat(SOAPData, "         &lt;/void&gt;\r\n");
135         strcat(SOAPData, "        &lt;/object&gt;\r\n");
136         strcat(SOAPData, "       &lt;/void&gt;\r\n");
137         strcat(SOAPData, "      &lt;/object&gt;\r\n");
138         strcat(SOAPData, "     &lt;/void&gt;\r\n");
139         strcat(SOAPData, "    &lt;/void&gt;\r\n");
140         strcat(SOAPData, "   &lt;/object&gt;\r\n");
141         strcat(SOAPData, "  &lt;/void&gt;\r\n");
142         strcat(SOAPData, " &lt;/array&gt;\r\n");
143         strcat(SOAPData, "&lt;/java&gt;");
144         strcat(SOAPData,"</arg1><arg2 href=\"#id0\"/></sendNotificationMessages><multiRef id=\"id0\" soapenc:root=\"0\"\r\n");
145         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>");
146         
147         /* Command */
148         lprintf(CTDL_DEBUG, "Transmitting command\n");
149         sprintf(buf, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
150                 FUNAMBOL_WS);
151         strcat(SOAPHeader,buf);
152         strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
153         sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
154                 "Citadel",
155                 REV_LEVEL,
156                 config.c_funambol_host,
157                 config.c_funambol_port
158                 );
159                 strcat(SOAPHeader,buf);
160         strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
161         sprintf(buf, "Content-Length: %d\r\n",
162                 strlen(SOAPData));
163         strcat(SOAPHeader, buf);
164         sprintf(buf, "Authorization: Basic %s\r\n\r\n",
165                 config.c_funambol_auth);
166         strcat(SOAPHeader, buf);
167         
168         sock_write(sock, SOAPHeader, strlen(SOAPHeader));
169         sock_write(sock, SOAPData, strlen(SOAPData));
170         sock_shutdown(sock, SHUT_WR);
171         
172         /* Response */
173         lprintf(CTDL_DEBUG, "Awaiting response\n");
174         if (sock_gets(sock, buf) < 0) {
175                 goto bail;
176         }
177         lprintf(CTDL_DEBUG, "<%s\n", buf);
178         if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
179                 
180                 goto bail;
181         }
182         lprintf(CTDL_DEBUG, "Funambol notified\n");
183         /* We should allow retries here but for now purge after one go */
184         bail:   
185         CtdlFreeMessage(msg);
186         long todelete[1];
187         todelete[0] = msgnum;
188         CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");   
189         close(sock);
190         return 0;
191 }
192
193
194 void do_notify_queue(void) {
195         static int doing_queue = 0;
196
197         /*
198          * This is a simple concurrency check to make sure only one queue run
199          * is done at a time.  We could do this with a mutex, but since we
200          * don't really require extremely fine granularity here, we'll do it
201          * with a static variable instead.
202          */
203         if (doing_queue) return;
204         doing_queue = 1;
205
206         /* 
207          * Go ahead and run the queue
208          */
209         lprintf(CTDL_INFO, "serv_funambol: processing notify queue\n");
210
211         if (getroom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
212                 lprintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
213                 return;
214         }
215         CtdlForEachMessage(MSGS_ALL, 0L, NULL,
216                 SPOOLMIME, NULL, notify_funambol, NULL);
217
218         lprintf(CTDL_INFO, "serv_funambol: queue run completed\n");
219         doing_queue = 0;
220 }
221
222
223 char *serv_funambol_init(void)
224 {
225         create_notify_queue();
226         CtdlRegisterSessionHook(do_notify_queue, EVT_TIMER);
227         return "$Id: serv_funambol.c $";
228 }