2 * This module implements a notifier for Funambol push email.
3 * Based on bits of serv_spam, serv_smtp
6 #define FUNAMBOL_WS "/funambol/services/admin"
15 #include <sys/types.h>
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
31 #include <sys/socket.h>
34 #include "sysdep_decls.h"
35 #include "citserver.h"
39 #include "serv_extensions.h"
46 #include "internet_addressing.h"
48 #include "clientsocket.h"
49 #include "serv_funambol.h"
51 * Create the notify message queue
53 void create_notify_queue(void) {
54 struct ctdlroom qrbuf;
56 create_room(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
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.
62 if (lgetroom(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
63 qrbuf.QRflags2 |= QR2_SYSTEM;
67 void do_notify_queue(void) {
68 static int doing_queue = 0;
71 * This is a simple concurrency check to make sure only one queue run
72 * is done at a time. We could do this with a mutex, but since we
73 * don't really require extremely fine granularity here, we'll do it
74 * with a static variable instead.
76 if (doing_queue) return;
80 * Go ahead and run the queue
82 lprintf(CTDL_INFO, "serv_funambol: processing notify queue\n");
84 if (getroom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
85 lprintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
88 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
89 SPOOLMIME, NULL, notify_funambol, NULL);
91 lprintf(CTDL_INFO, "serv_funambol: queue run completed\n");
96 * Connect to the Funambol server and scan a message.
98 int notify_funambol(long msgnum, void *userdata) {
99 struct CtdlMessage *msg;
102 char SOAPHeader[SIZ];
105 /* W means 'Wireless'... */
106 msg = CtdlFetchMessage(msgnum, 1);
107 if ( msg->cm_fields['W'] == NULL) {
110 /* Are we allowed to push? */
111 if ( strlen(config.c_funambol_host) == 0) {
114 lprintf(CTDL_INFO, "Push enabled\n");
117 sprintf(port, "%d", config.c_funambol_port);
118 lprintf(CTDL_INFO, "Connecting to Funambol at <%s>\n", config.c_funambol_host);
119 sock = sock_connect(config.c_funambol_host, port, "tcp");
120 if (sock >= 0) lprintf(CTDL_DEBUG, "Connected!\n");
123 /* If the service isn't running, pass for now */
127 /* Build a SOAP message, delicately, by hand */
128 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\">");
129 strcat(SOAPData, "<soapenv:Body><sendNotificationMessages soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
130 strcat(SOAPData, "<arg0 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">");
131 strcat(SOAPData, msg->cm_fields['W']);
132 strcat(SOAPData, "</arg0>");
133 strcat(SOAPData, "<arg1 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\"><?xml version="1.0" encoding="UTF-8"?>\r\n");
134 strcat(SOAPData, "<java version="1.5.0_10" class="java.beans.XMLDecoder"> \r\n");
135 strcat(SOAPData, " <array class="com.funambol.framework.core.Alert" length="1">\r\n");
136 strcat(SOAPData, " <void index="0">\r\n");
137 strcat(SOAPData, " <object class="com.funambol.framework.core.Alert">\r\n");
138 strcat(SOAPData, " <void property="cmdID">\r\n");
139 strcat(SOAPData, " <object class="com.funambol.framework.core.CmdID"/>\r\n");
140 strcat(SOAPData, " </void>");
141 strcat(SOAPData, " <void property="data">\r\n");
142 strcat(SOAPData, " <int>210</int>\r\n");
143 strcat(SOAPData, " </void>\r\n");
144 strcat(SOAPData, " <void property="items">\r\n");
145 strcat(SOAPData, " <void method="add">\r\n");
146 strcat(SOAPData, " <object class="com.funambol.framework.core.Item">\r\n");
147 strcat(SOAPData, " <void property="meta">\r\n");
148 strcat(SOAPData, " <object class="com.funambol.framework.core.Meta">\r\n");
149 strcat(SOAPData, " <void property="metInf">\r\n");
150 strcat(SOAPData, " <void property="type">\r\n");
151 strcat(SOAPData, " <string>application/vnd.omads-email+xml</string>\r\n");
152 strcat(SOAPData, " </void>\r\n");
153 strcat(SOAPData, " </void>\r\n");
154 strcat(SOAPData, " </object>\r\n");
155 strcat(SOAPData, " </void>\r\n");
156 strcat(SOAPData, " <void property="target">\r\n");
157 strcat(SOAPData, " <object class="com.funambol.framework.core.Target">\r\n");
158 strcat(SOAPData, " <void property="locURI">\r\n");
159 strcat(SOAPData, " <string>");
160 strcat(SOAPData, config.c_funambol_source);
161 strcat(SOAPData, "</string>\r\n");
162 strcat(SOAPData, " </void>\r\n");
163 strcat(SOAPData, " </object>\r\n");
164 strcat(SOAPData, " </void>\r\n");
165 strcat(SOAPData, " </object>\r\n");
166 strcat(SOAPData, " </void>\r\n");
167 strcat(SOAPData, " </void>\r\n");
168 strcat(SOAPData, " </object>\r\n");
169 strcat(SOAPData, " </void>\r\n");
170 strcat(SOAPData, " </array>\r\n");
171 strcat(SOAPData, "</java>");
172 strcat(SOAPData,"</arg1><arg2 href=\"#id0\"/></sendNotificationMessages><multiRef id=\"id0\" soapenc:root=\"0\"\r\n");
173 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>");
176 lprintf(CTDL_DEBUG, "Transmitting command\n");
177 sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
179 strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
180 sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
183 config.c_funambol_host,
184 config.c_funambol_port
186 strcat(SOAPHeader,buf);
187 strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
188 sprintf(buf, "Content-Length: %d\r\n",
190 strcat(SOAPHeader, buf);
191 sprintf(buf, "Authorization: Basic %s\r\n\r\n",
192 config.c_funambol_auth);
193 strcat(SOAPHeader, buf);
195 sock_write(sock, SOAPHeader, strlen(SOAPHeader));
196 sock_write(sock, SOAPData, strlen(SOAPData));
197 sock_shutdown(sock, SHUT_WR);
200 lprintf(CTDL_DEBUG, "Awaiting response\n");
201 if (sock_gets(sock, buf) < 0) {
204 lprintf(CTDL_DEBUG, "<%s\n", buf);
205 if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
209 lprintf(CTDL_DEBUG, "Funambol notified\n");
210 /* We should allow retries here but for now purge after one go */
214 CtdlFreeMessage(msg);
216 todelete[0] = msgnum;
217 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
223 char *serv_funambol_init(void)
225 create_notify_queue();
226 CtdlRegisterSessionHook(do_notify_queue, EVT_TIMER);
227 return "$Id: serv_funambol.c $";