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 "citserver.h"
43 #include "internet_addressing.h"
45 #include "clientsocket.h"
47 #include "serv_funambol.h"
48 #include "serv_pager.h"
51 #include "ctdl_module.h"
53 #define FUNAMBOL_CONFIG_TEXT "funambol"
56 * Create the notify message queue
58 void create_notify_queue(void) {
59 struct ctdlroom qrbuf;
61 create_room(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
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.
67 if (lgetroom(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
68 qrbuf.QRflags2 |= QR2_SYSTEM;
72 void do_notify_queue(void) {
73 static int doing_queue = 0;
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.
81 if (doing_queue) return;
85 * Go ahead and run the queue
87 lprintf(CTDL_INFO, "serv_funambol: processing notify queue\n");
89 if (getroom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
90 lprintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
93 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
94 SPOOLMIME, NULL, notify_funambol, NULL);
96 lprintf(CTDL_INFO, "serv_funambol: queue run completed\n");
101 * Connect to the Funambol server and scan a message.
103 void notify_funambol(long msgnum, void *userdata) {
104 struct CtdlMessage *msg;
107 char SOAPHeader[SIZ];
110 /* W means 'Wireless'... */
111 msg = CtdlFetchMessage(msgnum, 1);
112 if ( msg->cm_fields['W'] == NULL) {
115 long configMsgNum = pager_getConfigMessage(msg->cm_fields['W']);
116 int allowed = funambol_isAllowedByPrefs(configMsgNum);
120 /* Are we allowed to push? */
121 if (IsEmptyStr(config.c_funambol_host)) {
124 lprintf(CTDL_INFO, "Push enabled\n");
126 // Does the user want it?
127 sprintf(port, "%d", config.c_funambol_port);
128 lprintf(CTDL_INFO, "Connecting to Funambol at <%s>\n", config.c_funambol_host);
129 sock = sock_connect(config.c_funambol_host, port, "tcp");
130 if (sock >= 0) lprintf(CTDL_DEBUG, "Connected!\n");
133 /* If the service isn't running, pass for now */
137 /* Build a SOAP message, delicately, by hand */
138 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\">");
139 strcat(SOAPData, "<soapenv:Body><sendNotificationMessages soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">");
140 strcat(SOAPData, "<arg0 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\">");
141 strcat(SOAPData, msg->cm_fields['W']);
142 strcat(SOAPData, "</arg0>");
143 strcat(SOAPData, "<arg1 xsi:type=\"soapenc:string\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\"><?xml version="1.0" encoding="UTF-8"?>\r\n");
144 strcat(SOAPData, "<java version="1.5.0_10" class="java.beans.XMLDecoder"> \r\n");
145 strcat(SOAPData, " <array class="com.funambol.framework.core.Alert" length="1">\r\n");
146 strcat(SOAPData, " <void index="0">\r\n");
147 strcat(SOAPData, " <object class="com.funambol.framework.core.Alert">\r\n");
148 strcat(SOAPData, " <void property="cmdID">\r\n");
149 strcat(SOAPData, " <object class="com.funambol.framework.core.CmdID"/>\r\n");
150 strcat(SOAPData, " </void>");
151 strcat(SOAPData, " <void property="data">\r\n");
152 strcat(SOAPData, " <int>210</int>\r\n");
153 strcat(SOAPData, " </void>\r\n");
154 strcat(SOAPData, " <void property="items">\r\n");
155 strcat(SOAPData, " <void method="add">\r\n");
156 strcat(SOAPData, " <object class="com.funambol.framework.core.Item">\r\n");
157 strcat(SOAPData, " <void property="meta">\r\n");
158 strcat(SOAPData, " <object class="com.funambol.framework.core.Meta">\r\n");
159 strcat(SOAPData, " <void property="metInf">\r\n");
160 strcat(SOAPData, " <void property="type">\r\n");
161 strcat(SOAPData, " <string>application/vnd.omads-email+xml</string>\r\n");
162 strcat(SOAPData, " </void>\r\n");
163 strcat(SOAPData, " </void>\r\n");
164 strcat(SOAPData, " </object>\r\n");
165 strcat(SOAPData, " </void>\r\n");
166 strcat(SOAPData, " <void property="target">\r\n");
167 strcat(SOAPData, " <object class="com.funambol.framework.core.Target">\r\n");
168 strcat(SOAPData, " <void property="locURI">\r\n");
169 strcat(SOAPData, " <string>");
170 strcat(SOAPData, config.c_funambol_source);
171 strcat(SOAPData, "</string>\r\n");
172 strcat(SOAPData, " </void>\r\n");
173 strcat(SOAPData, " </object>\r\n");
174 strcat(SOAPData, " </void>\r\n");
175 strcat(SOAPData, " </object>\r\n");
176 strcat(SOAPData, " </void>\r\n");
177 strcat(SOAPData, " </void>\r\n");
178 strcat(SOAPData, " </object>\r\n");
179 strcat(SOAPData, " </void>\r\n");
180 strcat(SOAPData, " </array>\r\n");
181 strcat(SOAPData, "</java>");
182 strcat(SOAPData,"</arg1><arg2 href=\"#id0\"/></sendNotificationMessages><multiRef id=\"id0\" soapenc:root=\"0\"\r\n");
183 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>");
186 lprintf(CTDL_DEBUG, "Transmitting command\n");
187 sprintf(SOAPHeader, "POST %s HTTP/1.0\r\nContent-type: text/xml; charset=utf-8\r\n",
189 strcat(SOAPHeader,"Accept: application/soap+xml, application/dime, multipart/related, text/*\r\n");
190 sprintf(buf, "User-Agent: %s/%d\r\nHost: %s:%d\r\nCache-control: no-cache\r\n",
193 config.c_funambol_host,
194 config.c_funambol_port
196 strcat(SOAPHeader,buf);
197 strcat(SOAPHeader,"Pragma: no-cache\r\nSOAPAction: \"\"\r\n");
198 sprintf(buf, "Content-Length: " SIZE_T_FMT "\r\n",
200 strcat(SOAPHeader, buf);
201 sprintf(buf, "Authorization: Basic %s\r\n\r\n",
202 config.c_funambol_auth);
203 strcat(SOAPHeader, buf);
205 sock_write(sock, SOAPHeader, strlen(SOAPHeader));
206 sock_write(sock, SOAPData, strlen(SOAPData));
207 sock_shutdown(sock, SHUT_WR);
210 lprintf(CTDL_DEBUG, "Awaiting response\n");
211 if (sock_getln(sock, buf, sizeof buf) < 0) {
214 lprintf(CTDL_DEBUG, "<%s\n", buf);
215 if (strncasecmp(buf, "HTTP/1.1 200 OK", strlen("HTTP/1.1 200 OK"))) {
219 lprintf(CTDL_DEBUG, "Funambol notified\n");
220 /* We should allow retries here but for now purge after one go */
224 CtdlFreeMessage(msg);
226 todelete[0] = msgnum;
227 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
230 int funambol_isAllowedByPrefs(long configMsgNum) {
231 // Do a simple string search to see if 'funambol' is selected as the
232 // type. This string would be at the very top of the message contents.
233 if (configMsgNum == -1) {
236 struct CtdlMessage *prefMsg;
237 prefMsg = CtdlFetchMessage(configMsgNum, 1);
238 char *msgContents = prefMsg->cm_fields['M'];
239 return strncasecmp(msgContents, FUNAMBOL_CONFIG_TEXT, strlen(FUNAMBOL_CONFIG_TEXT));
242 CTDL_MODULE_INIT(funambol)
244 create_notify_queue();
245 CtdlRegisterSessionHook(do_notify_queue, EVT_TIMER);
247 /* return our Subversion id for the Log */