HUGE PATCH. This moves all of mime_parser.c and all
[citadel.git] / citadel / modules / funambol / 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 <libcitadel.h>
33 #include "citadel.h"
34 #include "server.h"
35 #include "citserver.h"
36 #include "support.h"
37 #include "config.h"
38 #include "control.h"
39 #include "room_ops.h"
40 #include "user_ops.h"
41 #include "database.h"
42 #include "msgbase.h"
43 #include "internet_addressing.h"
44 #include "domain.h"
45 #include "clientsocket.h"
46
47 #include "serv_funambol.h"
48 #include "serv_pager.h"
49
50
51 #include "ctdl_module.h"
52
53 #define FUNAMBOL_CONFIG_TEXT "funambol"
54
55 /*
56  * Create the notify message queue
57  */
58 void create_notify_queue(void) {
59         struct ctdlroom qrbuf;
60
61         create_room(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
62
63         /*
64          * Make sure it's set to be a "system room" so it doesn't show up
65          * in the <K>nown rooms list for Aides.
66          */
67         if (lgetroom(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
68                 qrbuf.QRflags2 |= QR2_SYSTEM;
69                 lputroom(&qrbuf);
70         }
71 }
72 void do_notify_queue(void) {
73         static int doing_queue = 0;
74
75         /*
76          * This is a simple concurrency check to make sure only one queue run
77          * is done at a time.  We could do this with a mutex, but since we
78          * don't really require extremely fine granularity here, we'll do it
79          * with a static variable instead.
80          */
81         if (doing_queue) return;
82         doing_queue = 1;
83
84         /* 
85          * Go ahead and run the queue
86          */
87         lprintf(CTDL_INFO, "serv_funambol: processing notify queue\n");
88
89         if (getroom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
90                 lprintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
91                 return;
92         }
93         CtdlForEachMessage(MSGS_ALL, 0L, NULL,
94                 SPOOLMIME, NULL, notify_funambol, NULL);
95
96         lprintf(CTDL_INFO, "serv_funambol: queue run completed\n");
97         doing_queue = 0;
98 }
99
100 /*! \brief Sends a message to the Funambol server 
101  *                      to notify of new email
102  * 
103  * Connect to the Funambol server via HTTP and send a SOAP request
104  * to notify of new email
105  */
106 void notify_funambol(long msgnum, void *userdata) {
107         struct CtdlMessage *msg;
108         int sock = (-1);
109         char buf[SIZ];
110         char SOAPHeader[SIZ];
111         char SOAPData[SIZ];
112         char port[SIZ];
113         /* W means 'Wireless'... */
114         msg = CtdlFetchMessage(msgnum, 1);
115         if ( msg->cm_fields['W'] == NULL) {
116                 lprintf(CTDL_ERR, "serv_funambol: msg->cm-fields['W'] is NULL\n");
117                 goto nuke;
118         }
119         long configMsgNum = pager_getConfigMessage(msg->cm_fields['W']);
120         int allowed = funambol_isAllowedByPrefs(configMsgNum);
121         if (allowed != 0) {
122                 lprintf(CTDL_DEBUG, "serv_funambol: User does not want Funambol push. Aborting\n");
123                 return;
124         }
125         /* Are we allowed to push? */
126         if (IsEmptyStr(config.c_funambol_host)) {
127                 return;
128         } else {
129                 lprintf(CTDL_INFO, "Push enabled\n");
130         }
131         // Does the user want it?
132         sprintf(port, "%d", config.c_funambol_port);
133                 lprintf(CTDL_INFO, "Connecting to Funambol at <%s>\n", config.c_funambol_host);
134                 sock = sock_connect(config.c_funambol_host, port, "tcp");
135                 if (sock >= 0) lprintf(CTDL_DEBUG, "Connected!\n");
136
137         if (sock < 0) {
138                 /* If the service isn't running, pass for now */
139                 goto nuke;
140         }
141         
142         /* Build a SOAP message, delicately, by hand */
143         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\">");
144         strcat(SOAPData, "<soapenv:Body><sendNotificationMessages soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
145         strcat(SOAPData, "<arg0 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">");
146         strcat(SOAPData, msg->cm_fields['W']);
147         strcat(SOAPData, "</arg0>");
148         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");
149         strcat(SOAPData, "&lt;java version=&quot;1.5.0_10&quot; class=&quot;java.beans.XMLDecoder&quot;&gt; \r\n");
150         strcat(SOAPData, " &lt;array class=&quot;com.funambol.framework.core.Alert&quot; length=&quot;1&quot;&gt;\r\n");
151         strcat(SOAPData, "  &lt;void index=&quot;0&quot;&gt;\r\n");
152         strcat(SOAPData, "   &lt;object class=&quot;com.funambol.framework.core.Alert&quot;&gt;\r\n");
153         strcat(SOAPData, "    &lt;void property=&quot;cmdID&quot;>\r\n");
154         strcat(SOAPData, "     &lt;object class=&quot;com.funambol.framework.core.CmdID&quot;/&gt;\r\n");
155         strcat(SOAPData, "    &lt;/void&gt;");
156         strcat(SOAPData, "    &lt;void property=&quot;data&quot;&gt;\r\n");
157         strcat(SOAPData, "     &lt;int&gt;210&lt;/int&gt;\r\n");
158         strcat(SOAPData, "    &lt;/void&gt;\r\n");
159         strcat(SOAPData, "    &lt;void property=&quot;items&quot;&gt;\r\n");
160         strcat(SOAPData, "     &lt;void method=&quot;add&quot;&gt;\r\n"); 
161         strcat(SOAPData, "      &lt;object class=&quot;com.funambol.framework.core.Item&quot;&gt;\r\n"); 
162         strcat(SOAPData, "       &lt;void property=&quot;meta&quot;&gt;\r\n"); 
163         strcat(SOAPData, "        &lt;object class=&quot;com.funambol.framework.core.Meta&quot;&gt;\r\n"); 
164         strcat(SOAPData, "         &lt;void property=&quot;metInf&quot;&gt;\r\n");
165         strcat(SOAPData, "          &lt;void property=&quot;type&quot;&gt;\r\n");
166         strcat(SOAPData, "           &lt;string&gt;application/vnd.omads-email+xml&lt;/string&gt;\r\n");
167         strcat(SOAPData, "          &lt;/void&gt;\r\n"); 
168         strcat(SOAPData, "         &lt;/void&gt;\r\n"); 
169         strcat(SOAPData, "        &lt;/object&gt;\r\n"); 
170         strcat(SOAPData, "       &lt;/void&gt;\r\n"); 
171         strcat(SOAPData, "       &lt;void property=&quot;target&quot;&gt;\r\n"); 
172         strcat(SOAPData, "        &lt;object class=&quot;com.funambol.framework.core.Target&quot;&gt;\r\n");
173         strcat(SOAPData, "         &lt;void property=&quot;locURI&quot;&gt;\r\n");
174         strcat(SOAPData, "          &lt;string&gt;");
175         strcat(SOAPData, config.c_funambol_source);
176         strcat(SOAPData, "&lt;/string&gt;\r\n");
177         strcat(SOAPData, "         &lt;/void&gt;\r\n");
178         strcat(SOAPData, "        &lt;/object&gt;\r\n");
179         strcat(SOAPData, "       &lt;/void&gt;\r\n");
180         strcat(SOAPData, "      &lt;/object&gt;\r\n");
181         strcat(SOAPData, "     &lt;/void&gt;\r\n");
182         strcat(SOAPData, "    &lt;/void&gt;\r\n");
183         strcat(SOAPData, "   &lt;/object&gt;\r\n");
184         strcat(SOAPData, "  &lt;/void&gt;\r\n");
185         strcat(SOAPData, " &lt;/array&gt;\r\n");
186         strcat(SOAPData, "&lt;/java&gt;");
187         strcat(SOAPData,"</arg1><arg2 href=\"#id0\"/></sendNotificationMessages><multiRef id=\"id0\" soapenc:root=\"0\"\r\n");
188         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>");
189         
190         /* Command */
191         lprintf(CTDL_DEBUG, "Transmitting command\n");
192         sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
193                 FUNAMBOL_WS);
194         strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
195         sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
196                 "Citadel",
197                 REV_LEVEL,
198                 config.c_funambol_host,
199                 config.c_funambol_port
200                 );
201                 strcat(SOAPHeader,buf);
202         strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
203         sprintf(buf, "Content-Length: " SIZE_T_FMT "\r\n",
204                 strlen(SOAPData));
205         strcat(SOAPHeader, buf);
206         sprintf(buf, "Authorization: Basic %s\r\n\r\n",
207                 config.c_funambol_auth);
208         strcat(SOAPHeader, buf);
209         
210         sock_write(sock, SOAPHeader, strlen(SOAPHeader));
211         sock_write(sock, SOAPData, strlen(SOAPData));
212         sock_shutdown(sock, SHUT_WR);
213         
214         /* Response */
215         lprintf(CTDL_DEBUG, "Awaiting response\n");
216         if (sock_getln(sock, buf, sizeof buf) < 0) {
217                 goto bail;
218         }
219         lprintf(CTDL_DEBUG, "<%s\n", buf);
220         if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
221                 
222                 goto bail;
223         }
224         lprintf(CTDL_DEBUG, "Funambol notified\n");
225         /* We should allow retries here but for now purge after one go */
226         bail:           
227         close(sock);
228         nuke:
229         CtdlFreeMessage(msg);
230         long todelete[1];
231         todelete[0] = msgnum;
232         CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
233 }
234 /*! \brief Checks a preference message to see if Funambol push is configured by user
235  *
236  */
237 int funambol_isAllowedByPrefs(long configMsgNum) {
238         // Do a simple string search to see if 'funambol' is selected as the 
239         // type. This string would be at the very top of the message contents.
240         if (configMsgNum == -1) {
241                 lprintf(CTDL_ERR, "funambol_isAllowedByPrefs was passed a non-existant config message id\n");
242                 return -1;
243         }
244         struct CtdlMessage *prefMsg;
245         prefMsg = CtdlFetchMessage(configMsgNum, 1);
246         char *msgContents = prefMsg->cm_fields['M'];
247         return strncasecmp(msgContents, FUNAMBOL_CONFIG_TEXT, strlen(FUNAMBOL_CONFIG_TEXT));
248 }
249
250 CTDL_MODULE_INIT(funambol)
251 {
252         create_notify_queue();
253         CtdlRegisterSessionHook(do_notify_queue, EVT_TIMER);
254
255         /* return our Subversion id for the Log */
256         return "$Id$";
257 }