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-2011
13 * This program is open source 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
37 #include <sys/types.h>
39 #if TIME_WITH_SYS_TIME
40 # include <sys/time.h>
44 # include <sys/time.h>
53 #include <sys/socket.h>
54 #include <libcitadel.h>
57 #include "citserver.h"
64 #include "internet_addressing.h"
66 #include "clientsocket.h"
67 #include "event_client.h"
68 #include "extnotify.h"
69 #include "ctdl_module.h"
71 struct CitContext extnotify_queue_CC;
73 void ExtNotify_PutErrorMessage(NotifyContext *Ctx, StrBuf *ErrMsg)
76 if (Ctx->NotifyErrors == NULL)
77 Ctx->NotifyErrors = NewHash(1, Flathash);
79 nNext = GetCount(Ctx->NotifyErrors) + 1;
80 Put(Ctx->NotifyErrors,
87 StrBuf* GetNHBuf(int i, int allocit, StrBuf **NotifyHostList)
89 if ((NotifyHostList[i] == NULL) && (allocit != 0))
90 NotifyHostList[i] = NewStrBuf();
91 return NotifyHostList[i];
95 int GetNotifyHosts(NotifyContext *Ctx)
97 char NotifyHostsBuf[SIZ];
102 const char *pchs, *pche;
103 const char *NextHost = NULL;
105 /* See if we have any Notification Hosts configured */
106 Ctx->nNotifyHosts = get_hosts(NotifyHostsBuf, "notify");
107 if (Ctx->nNotifyHosts < 1)
110 Ctx->NotifyHostList = malloc(sizeof(StrBuf*) * 2 * (Ctx->nNotifyHosts + 1));
111 memset(Ctx->NotifyHostList, 0, sizeof(StrBuf*) * 2 * (Ctx->nNotifyHosts + 1));
113 NotifyBuf = NewStrBufPlain(NotifyHostsBuf, -1);
114 /* get all configured notifiers's */
115 for (notify=0; notify<Ctx->nNotifyHosts; notify++) {
117 Host = GetNHBuf(notify * 2, 1, Ctx->NotifyHostList);
118 StrBufExtract_NextToken(Host, NotifyBuf, &NextHost, '|');
120 pche = strchr(pchs, ':');
123 "extnotify: filename of notification template not found in %s.\n",
127 File = GetNHBuf(notify * 2 + 1, 1, Ctx->NotifyHostList);
128 StrBufPlain(File, pchs, pche - pchs);
129 StrBufCutLeft(Host, pche - pchs + 1);
131 FreeStrBuf(&NotifyBuf);
132 return Ctx->nNotifyHosts;
137 /*! \brief Get configuration message for pager/funambol system from the
138 * users "My Citadel Config" room
140 eNotifyType extNotify_getConfigMessage(char *username, char **PagerNumber, char **FreeMe)
142 struct ctdlroom qrbuf; // scratch for room
143 struct ctdluser user; // ctdl user instance
144 char configRoomName[ROOMNAMELEN];
145 struct CtdlMessage *msg = NULL;
146 struct cdbdata *cdbfr;
147 long *msglist = NULL;
154 CtdlGetUser(&user, username);
156 CtdlMailboxName(configRoomName, sizeof configRoomName, &user, USERCONFIGROOM);
158 CtdlGetRoom(&qrbuf, configRoomName);
159 /* Do something really, really stoopid here. Raid the room on ourselves,
160 * loop through the messages manually and find it. I don't want
161 * to use a CtdlForEachMessage callback here, as we would be
163 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
165 msglist = (long *) cdbfr->ptr;
166 cdbfr->ptr = NULL; /* CtdlForEachMessage() now owns this memory */
167 num_msgs = cdbfr->len / sizeof(long);
170 syslog(LOG_DEBUG, "extNotify_getConfigMessage: No config messages found\n");
171 return eNone; /* No messages at all? No further action. */
173 for (a = 0; a < num_msgs; ++a) {
174 msg = CtdlFetchMessage(msglist[a], 1);
176 if ((msg->cm_fields['U'] != NULL) &&
177 (strncasecmp(msg->cm_fields['U'], PAGER_CONFIG_MESSAGE,
178 strlen(PAGER_CONFIG_MESSAGE)) == 0)) {
181 CtdlFreeMessage(msg);
190 // Do a simple string search to see if 'funambol' is selected as the
191 // type. This string would be at the very top of the message contents.
193 configMsg = msg->cm_fields['M'];
194 msg->cm_fields['M'] = NULL;
195 CtdlFreeMessage(msg);
197 /* here we would find the pager number... */
198 pch = strchr(configMsg, '\n');
206 * 1. The user has configured paging / They have and disabled it
207 * AND 2. There is an external pager program
208 * 3. A Funambol server has been entered
211 if (!strncasecmp(configMsg, "none", 4))
217 if (!strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP)))
220 return eHttpMessages;
222 if (!strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT)))
227 else if (!strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM)))
229 // whats the pager number?
230 if (!pch || (*pch == '\0'))
236 while (isspace(*pch))
239 while (isdigit(*pch) || (*pch == '+'))
252 * Process messages in the external notification queue
254 void process_notify(long NotifyMsgnum, void *usrdata)
260 struct CtdlMessage *msg;
267 Ctx = (NotifyContext*) usrdata;
269 msg = CtdlFetchMessage(NotifyMsgnum, 1);
270 if ( msg->cm_fields['2'] != NULL)
272 Type = extNotify_getConfigMessage(msg->cm_fields['2'], &PagerNo, &FreeMe);
274 pch = strstr(msg->cm_fields['M'], "msgid|");
276 msgnum = atol(pch + sizeof("msgid"));
281 snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
282 config.c_funambol_auth,
283 config.c_funambol_host,
284 config.c_funambol_port,
287 SubC = CloneContext (CC);
288 SubC->session_specific_data = NULL;// (char*) DupNotifyContext(Ctx);
290 notify_http_server(remoteurl,
292 strlen(file_funambol_msg),/*GNA*/
304 StrBuf *FileBuf = NewStrBuf();
306 for (i = 0; i < Ctx->nNotifyHosts; i++)
309 URL = GetNHBuf(i*2, 0, Ctx->NotifyHostList);
310 if (URL==NULL) break;
311 File = GetNHBuf(i*2 + 1, 0, Ctx->NotifyHostList);
312 if (File==NULL) break;
314 if (StrLength(File)>0)
315 StrBufPrintf(FileBuf, "%s/%s",
319 FlushStrBuf(FileBuf);
320 memcpy(URLBuf, ChrPtr(URL), StrLength(URL) + 1);
322 SubC = CloneContext (CC);
323 SubC->session_specific_data = NULL;// (char*) DupNotifyContext(Ctx);
324 notify_http_server(URLBuf,
332 FreeStrBuf(&FileBuf);
340 commandSiz = sizeof(config.c_pager_program) + strlen(PagerNo) + strlen(msg->cm_fields['2']) + 5;
341 command = malloc(commandSiz);
342 snprintf(command, commandSiz, "%s %s -u %s", config.c_pager_program, PagerNo, msg->cm_fields['2']);
353 CtdlFreeMessage(msg);
354 todelete[0] = NotifyMsgnum;
355 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
359 * \brief Run through the pager room queue
360 * Checks to see what notification option the user has set
362 void do_extnotify_queue(void)
365 static int doing_queue = 0;
369 * This is a simple concurrency check to make sure only one queue run
370 * is done at a time. We could do this with a mutex, but since we
371 * don't really require extremely fine granularity here, we'll do it
372 * with a static variable instead.
375 if (IsEmptyStr(config.c_pager_program) &&
376 IsEmptyStr(config.c_funambol_host))
378 syslog(LOG_ERR, "No external notifiers configured on system/user\n");
382 if (doing_queue) return;
385 pthread_setspecific(MyConKey, (void *)&extnotify_queue_CC);
388 * Go ahead and run the queue
390 syslog(LOG_DEBUG, "serv_extnotify: processing notify queue\n");
392 memset(&Ctx, 0, sizeof(NotifyContext));
393 if ((GetNotifyHosts(&Ctx) > 0) &&
394 (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0))
396 syslog(LOG_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
397 if (Ctx.nNotifyHosts > 0)
399 for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
400 FreeStrBuf(&Ctx.NotifyHostList[i]);
401 free(Ctx.NotifyHostList);
405 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
406 SPOOLMIME, NULL, process_notify, &Ctx);
407 syslog(LOG_DEBUG, "serv_extnotify: queue run completed\n");
409 if (Ctx.nNotifyHosts > 0)
411 for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
412 FreeStrBuf(&Ctx.NotifyHostList[i]);
413 free(Ctx.NotifyHostList);
419 /* Create the notify message queue. We use the exact same room
420 * as the Funambol module.
422 * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
423 * and sets as system room.
425 void create_extnotify_queue(void) {
426 struct ctdlroom qrbuf;
428 CtdlCreateRoom(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
430 CtdlFillSystemContext(&extnotify_queue_CC, "Extnotify");
433 * Make sure it's set to be a "system room" so it doesn't show up
434 * in the <K>nown rooms list for Aides.
436 if (CtdlGetRoomLock(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
437 qrbuf.QRflags2 |= QR2_SYSTEM;
438 CtdlPutRoomLock(&qrbuf);
443 CTDL_MODULE_INIT(extnotify)
447 create_extnotify_queue();
448 CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
450 /* return our module name for the log */