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"
65 #include "internet_addressing.h"
67 #include "clientsocket.h"
68 #include "extnotify.h"
70 #include "ctdl_module.h"
74 void ExtNotify_PutErrorMessage(NotifyContext *Ctx, StrBuf *ErrMsg)
77 if (Ctx->NotifyErrors == NULL)
78 Ctx->NotifyErrors = NewHash(1, Flathash);
80 nNext = GetCount(Ctx->NotifyErrors) + 1;
81 Put(Ctx->NotifyErrors,
91 StrBuf* GetNHBuf(int i, int allocit, StrBuf **NotifyHostList)
93 if ((NotifyHostList[i] == NULL) && (allocit != 0))
94 NotifyHostList[i] = NewStrBuf();
95 return NotifyHostList[i];
99 StrBuf** GetNotifyHosts(void)
101 char NotifyHostsBuf[SIZ];
106 const char *pchs, *pche;
107 const char *NextHost = NULL;
108 StrBuf **NotifyHostList;
111 /* See if we have any Notification Hosts configured */
112 num_notify = get_hosts(NotifyHostsBuf, "notify");
116 NotifyHostList = malloc(sizeof(StrBuf*) * 2 * (num_notify + 1));
117 memset(NotifyHostList, 0, sizeof(StrBuf*) * 2 * (num_notify + 1));
119 NotifyBuf = NewStrBufPlain(NotifyHostsBuf, -1);
120 /* get all configured notifiers's */
121 for (notify=0; notify<num_notify; notify++) {
123 Host = GetNHBuf(notify * 2, 1, NotifyHostList);
124 StrBufExtract_NextToken(Host, NotifyBuf, &NextHost, '|');
126 pche = strchr(pchs, ':');
128 CtdlLogPrintf(CTDL_ERR,
129 __FILE__": filename not found in %s.\n",
133 File = GetNHBuf(notify * 2 + 1, 1, NotifyHostList);
134 StrBufPlain(File, pchs, pche - pchs);
135 StrBufCutLeft(Host, pche - pchs + 1);
137 return NotifyHostList;
141 /*! \brief Create the notify message queue. We use the exact same room
142 * as the Funambol module.
144 * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
145 * and sets as system room.
147 void create_extnotify_queue(void) {
148 struct ctdlroom qrbuf;
150 create_room(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
153 * Make sure it's set to be a "system room" so it doesn't show up
154 * in the <K>nown rooms list for Aides.
156 if (lgetroom(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
157 qrbuf.QRflags2 |= QR2_SYSTEM;
162 * \brief Run through the pager room queue
164 void do_extnotify_queue(void)
167 static int doing_queue = 0;
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;
180 * Go ahead and run the queue
182 CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: processing notify queue\n");
184 memset(&Ctx, 0, sizeof(NotifyContext));
185 Ctx.NotifyHostList = GetNotifyHosts();
186 if (getroom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
187 CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
190 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
191 SPOOLMIME, NULL, process_notify, &Ctx);
193 while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
194 FreeStrBuf(&Ctx.NotifyHostList[i]);
196 if (Ctx.NotifyErrors != NULL)
204 It = GetNewHashPos(Ctx.NotifyErrors, 0);
205 while (GetNextHashPos(Ctx.NotifyErrors, It, &len, &Key, &vErr) &&
207 ErrMsg = (StrBuf*) vErr;
208 quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED,
209 "Failed to notify external service about inbound mail");
213 DeleteHash(&Ctx.NotifyErrors);
216 CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: queue run completed\n");
220 * \brief Process messages in the external notification queue
222 void process_notify(long NotifyMsgnum, void *usrdata)
228 int extPagerAllowedHttp;
229 int extPagerAllowedSystem;
233 struct CtdlMessage *msg;
235 Ctx = (NotifyContext*) usrdata;
237 msg = CtdlFetchMessage(NotifyMsgnum, 1);
238 if ( msg->cm_fields['W'] == NULL) {
242 configMsgNum = extNotify_getConfigMessage(msg->cm_fields['W']);
244 extNotify_getPrefs(configMsgNum, &configMsg[0]);
247 * 1. The user has configured paging / They have and disabled it
248 * AND 2. There is an external pager program
249 * 3. A Funambol server has been entered
252 if ((configMsgNum == -1) ||
253 ((strncasecmp(configMsg, "none", 4) == 0) &&
254 IsEmptyStr(config.c_pager_program) &&
255 IsEmptyStr(config.c_funambol_host))) {
256 CtdlLogPrintf(CTDL_DEBUG, "No external notifiers configured on system/user");
260 // Can Funambol take the message?
261 pch = strchr(configMsg, '\n');
264 fnblAllowed = strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT));
265 extPagerAllowedHttp = strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP));
266 extPagerAllowedSystem = strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM));
267 pch = strstr(msg->cm_fields['M'], "msgid|");
269 msgnum = atol(pch + sizeof("msgid"));
270 if (fnblAllowed == 0) {
272 snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
273 config.c_funambol_auth,
274 config.c_funambol_host,
275 config.c_funambol_port,
277 notify_http_server(remoteurl,
279 strlen(file_funambol_msg),/*GNA*/
284 } else if (extPagerAllowedHttp == 0) {
289 StrBuf *FileBuf = NewStrBuf();
294 URL = GetNHBuf(i*2, 0, Ctx->NotifyHostList);
295 if (URL==NULL) break;
296 File = GetNHBuf(i*2 + 1, 0, Ctx->NotifyHostList);
297 if (File==NULL) break;
299 if (StrLength(File)>0)
300 StrBufPrintf(FileBuf, "%s/%s",
304 FlushStrBuf(FileBuf);
305 memcpy(URLBuf, ChrPtr(URL), StrLength(URL) + 1);
307 notify_http_server(URLBuf,
316 FreeStrBuf(&FileBuf);
318 else if (extPagerAllowedSystem == 0) {
323 number = strtok(configMsg, "textmessage\n");
324 commandSiz = sizeof(config.c_pager_program) + strlen(number) + strlen(msg->cm_fields['W']) + 5;
325 command = malloc(commandSiz);
326 snprintf(command, commandSiz, "%s %s -u %s", config.c_pager_program, number, msg->cm_fields['W']);
331 CtdlFreeMessage(msg);
332 memset(configMsg, 0, sizeof(configMsg));
333 todelete[0] = NotifyMsgnum;
334 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
337 /*! \brief Checks to see what notification option the user has set
340 void extNotify_getPrefs(long configMsgNum, char *configMsg)
342 struct CtdlMessage *prefMsg;
343 // Do a simple string search to see if 'funambol' is selected as the
344 // type. This string would be at the very top of the message contents.
345 if (configMsgNum == -1) {
346 CtdlLogPrintf(CTDL_ERR, "extNotify_isAllowedByPrefs was passed a non-existant config message id\n");
349 prefMsg = CtdlFetchMessage(configMsgNum, 1);
350 strncpy(configMsg, prefMsg->cm_fields['M'], strlen(prefMsg->cm_fields['M']));
351 CtdlFreeMessage(prefMsg);
354 /*! \brief Get configuration message for pager/funambol system from the
355 * users "My Citadel Config" room
357 long extNotify_getConfigMessage(char *username) {
358 struct ctdlroom qrbuf; // scratch for room
359 struct ctdluser user; // ctdl user instance
360 char configRoomName[ROOMNAMELEN];
361 struct CtdlMessage *msg;
362 struct cdbdata *cdbfr;
363 long *msglist = NULL;
365 long confMsgNum = -1;
369 getuser(&user, username);
371 MailboxName(configRoomName, sizeof configRoomName, &user, USERCONFIGROOM);
373 getroom(&qrbuf, configRoomName);
374 /* Do something really, really stoopid here. Raid the room on ourselves,
375 * loop through the messages manually and find it. I don't want
376 * to use a CtdlForEachMessage callback here, as we would be
378 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
380 msglist = (long *) cdbfr->ptr;
381 cdbfr->ptr = NULL; /* CtdlForEachMessage() now owns this memory */
382 num_msgs = cdbfr->len / sizeof(long);
385 CtdlLogPrintf(CTDL_DEBUG, "extNotify_getConfigMessage: No config messages found\n");
386 return -1; /* No messages at all? No further action. */
388 for (a = 0; a < num_msgs; ++a) {
389 msg = CtdlFetchMessage(msglist[a], 1);
391 if ((msg->cm_fields['U'] != NULL) &&
392 (strncasecmp(msg->cm_fields['U'], PAGER_CONFIG_MESSAGE,
393 strlen(PAGER_CONFIG_MESSAGE)) == 0)) {
394 confMsgNum = msglist[a];
396 CtdlFreeMessage(msg);
403 CTDL_MODULE_INIT(extnotify)
407 create_extnotify_queue();
408 CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
410 /* return our Subversion id for the Log */