5 * This module implements an external pager hook for when notifcation
6 * of a new email is wanted.
8 * Based on bits of serv_funambol
9 * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
11 * Copyright (c) 2008-2009
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #include <sys/types.h>
38 #if TIME_WITH_SYS_TIME
39 # include <sys/time.h>
43 # include <sys/time.h>
52 #include <sys/socket.h>
53 #include <libcitadel.h>
56 #include "citserver.h"
63 #include "internet_addressing.h"
65 #include "clientsocket.h"
66 #include "extnotify.h"
68 #include "ctdl_module.h"
72 void ExtNotify_PutErrorMessage(NotifyContext *Ctx, StrBuf *ErrMsg)
75 if (Ctx->NotifyErrors == NULL)
76 Ctx->NotifyErrors = NewHash(1, Flathash);
78 nNext = GetCount(Ctx->NotifyErrors) + 1;
79 Put(Ctx->NotifyErrors,
89 StrBuf* GetNHBuf(int i, int allocit, StrBuf **NotifyHostList)
91 if ((NotifyHostList[i] == NULL) && (allocit != 0))
92 NotifyHostList[i] = NewStrBuf();
93 return NotifyHostList[i];
97 StrBuf** GetNotifyHosts(void)
99 char NotifyHostsBuf[SIZ];
104 const char *pchs, *pche;
105 const char *NextHost = NULL;
106 StrBuf **NotifyHostList;
109 /* See if we have any Notification Hosts configured */
110 num_notify = get_hosts(NotifyHostsBuf, "notify");
114 NotifyHostList = malloc(sizeof(StrBuf*) * 2 * (num_notify + 1));
115 memset(NotifyHostList, 0, sizeof(StrBuf*) * 2 * (num_notify + 1));
117 NotifyBuf = NewStrBufPlain(NotifyHostsBuf, -1);
118 /* get all configured notifiers's */
119 for (notify=0; notify<num_notify; notify++) {
121 Host = GetNHBuf(notify * 2, 1, NotifyHostList);
122 StrBufExtract_NextToken(Host, NotifyBuf, &NextHost, '|');
124 pche = strchr(pchs, ':');
126 CtdlLogPrintf(CTDL_ERR,
127 __FILE__": filename not found in %s.\n",
131 File = GetNHBuf(notify * 2 + 1, 1, NotifyHostList);
132 StrBufPlain(File, pchs, pche - pchs);
133 StrBufCutLeft(Host, pche - pchs + 1);
135 return NotifyHostList;
139 /*! \brief Create the notify message queue. We use the exact same room
140 * as the Funambol module.
142 * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
143 * and sets as system room.
145 void create_extnotify_queue(void) {
146 struct ctdlroom qrbuf;
148 CtdlCreateRoom(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
151 * Make sure it's set to be a "system room" so it doesn't show up
152 * in the <K>nown rooms list for Aides.
154 if (CtdlGetRoomLock(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
155 qrbuf.QRflags2 |= QR2_SYSTEM;
156 CtdlPutRoomLock(&qrbuf);
160 * \brief Run through the pager room queue
162 void do_extnotify_queue(void)
166 static int doing_queue = 0;
168 CitContext extnotifyCC;
171 * This is a simple concurrency check to make sure only one queue run
172 * is done at a time. We could do this with a mutex, but since we
173 * don't really require extremely fine granularity here, we'll do it
174 * with a static variable instead.
176 if (doing_queue) return;
179 /* Give this thread its own private CitContext */
180 CtdlFillSystemContext(&extnotifyCC, "extnotify");
181 citthread_setspecific(MyConKey, (void *)&extnotifyCC );
184 * Go ahead and run the queue
186 CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: processing notify queue\n");
188 memset(&Ctx, 0, sizeof(NotifyContext));
189 Ctx.NotifyHostList = GetNotifyHosts();
190 if (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
191 CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
192 CtdlClearSystemContext();
195 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
196 SPOOLMIME, NULL, process_notify, &Ctx);
198 while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
199 FreeStrBuf(&Ctx.NotifyHostList[i]);
201 if (Ctx.NotifyErrors != NULL)
209 It = GetNewHashPos(Ctx.NotifyErrors, 0);
210 while (GetNextHashPos(Ctx.NotifyErrors, It, &len, &Key, &vErr) &&
212 ErrMsg = (StrBuf*) vErr;
213 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
214 "Failed to notify external service about inbound mail");
218 DeleteHash(&Ctx.NotifyErrors);
221 CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: queue run completed\n");
223 CtdlClearSystemContext();
226 * \brief Process messages in the external notification queue
228 void process_notify(long NotifyMsgnum, void *usrdata)
234 int extPagerAllowedHttp;
235 int extPagerAllowedSystem;
239 struct CtdlMessage *msg;
241 Ctx = (NotifyContext*) usrdata;
243 msg = CtdlFetchMessage(NotifyMsgnum, 1);
244 if ( msg->cm_fields['W'] == NULL) {
248 configMsgNum = extNotify_getConfigMessage(msg->cm_fields['W']);
250 extNotify_getPrefs(configMsgNum, &configMsg[0]);
253 * 1. The user has configured paging / They have and disabled it
254 * AND 2. There is an external pager program
255 * 3. A Funambol server has been entered
258 if ((configMsgNum == -1) ||
259 ((strncasecmp(configMsg, "none", 4) == 0) &&
260 IsEmptyStr(config.c_pager_program) &&
261 IsEmptyStr(config.c_funambol_host))) {
262 CtdlLogPrintf(CTDL_DEBUG, "No external notifiers configured on system/user");
266 // Can Funambol take the message?
267 pch = strchr(configMsg, '\n');
270 fnblAllowed = strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT));
271 extPagerAllowedHttp = strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP));
272 extPagerAllowedSystem = strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM));
273 pch = strstr(msg->cm_fields['M'], "msgid|");
275 msgnum = atol(pch + sizeof("msgid"));
276 if (fnblAllowed == 0) {
278 snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
279 config.c_funambol_auth,
280 config.c_funambol_host,
281 config.c_funambol_port,
283 notify_http_server(remoteurl,
285 strlen(file_funambol_msg),/*GNA*/
290 } else if (extPagerAllowedHttp == 0) {
295 StrBuf *FileBuf = NewStrBuf();
300 URL = GetNHBuf(i*2, 0, Ctx->NotifyHostList);
301 if (URL==NULL) break;
302 File = GetNHBuf(i*2 + 1, 0, Ctx->NotifyHostList);
303 if (File==NULL) break;
305 if (StrLength(File)>0)
306 StrBufPrintf(FileBuf, "%s/%s",
310 FlushStrBuf(FileBuf);
311 memcpy(URLBuf, ChrPtr(URL), StrLength(URL) + 1);
313 notify_http_server(URLBuf,
322 FreeStrBuf(&FileBuf);
324 else if (extPagerAllowedSystem == 0) {
329 number = strtok(configMsg, "textmessage\n");
330 commandSiz = sizeof(config.c_pager_program) + strlen(number) + strlen(msg->cm_fields['W']) + 5;
331 command = malloc(commandSiz);
332 snprintf(command, commandSiz, "%s %s -u %s", config.c_pager_program, number, msg->cm_fields['W']);
337 CtdlFreeMessage(msg);
338 memset(configMsg, 0, sizeof(configMsg));
339 todelete[0] = NotifyMsgnum;
340 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
343 /*! \brief Checks to see what notification option the user has set
346 void extNotify_getPrefs(long configMsgNum, char *configMsg)
348 struct CtdlMessage *prefMsg;
349 // Do a simple string search to see if 'funambol' is selected as the
350 // type. This string would be at the very top of the message contents.
351 if (configMsgNum == -1) {
352 CtdlLogPrintf(CTDL_ERR, "extNotify_isAllowedByPrefs was passed a non-existant config message id\n");
355 prefMsg = CtdlFetchMessage(configMsgNum, 1);
356 strncpy(configMsg, prefMsg->cm_fields['M'], strlen(prefMsg->cm_fields['M']));
357 CtdlFreeMessage(prefMsg);
360 /*! \brief Get configuration message for pager/funambol system from the
361 * users "My Citadel Config" room
363 long extNotify_getConfigMessage(char *username) {
364 struct ctdlroom qrbuf; // scratch for room
365 struct ctdluser user; // ctdl user instance
366 char configRoomName[ROOMNAMELEN];
367 struct CtdlMessage *msg;
368 struct cdbdata *cdbfr;
369 long *msglist = NULL;
371 long confMsgNum = -1;
375 CtdlGetUser(&user, username);
377 CtdlMailboxName(configRoomName, sizeof configRoomName, &user, USERCONFIGROOM);
379 CtdlGetRoom(&qrbuf, configRoomName);
380 /* Do something really, really stoopid here. Raid the room on ourselves,
381 * loop through the messages manually and find it. I don't want
382 * to use a CtdlForEachMessage callback here, as we would be
384 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
386 msglist = (long *) cdbfr->ptr;
387 cdbfr->ptr = NULL; /* CtdlForEachMessage() now owns this memory */
388 num_msgs = cdbfr->len / sizeof(long);
391 CtdlLogPrintf(CTDL_DEBUG, "extNotify_getConfigMessage: No config messages found\n");
392 return -1; /* No messages at all? No further action. */
394 for (a = 0; a < num_msgs; ++a) {
395 msg = CtdlFetchMessage(msglist[a], 1);
397 if ((msg->cm_fields['U'] != NULL) &&
398 (strncasecmp(msg->cm_fields['U'], PAGER_CONFIG_MESSAGE,
399 strlen(PAGER_CONFIG_MESSAGE)) == 0)) {
400 confMsgNum = msglist[a];
402 CtdlFreeMessage(msg);
409 CTDL_MODULE_INIT(extnotify)
413 create_extnotify_queue();
414 CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
416 /* return our Subversion id for the Log */