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 notify_http_server(remoteurl,
289 strlen(file_funambol_msg),/*GNA*/
301 StrBuf *FileBuf = NewStrBuf();
303 for (i = 0; i < Ctx->nNotifyHosts; i++)
306 URL = GetNHBuf(i*2, 0, Ctx->NotifyHostList);
307 if (URL==NULL) break;
308 File = GetNHBuf(i*2 + 1, 0, Ctx->NotifyHostList);
309 if (File==NULL) break;
311 if (StrLength(File)>0)
312 StrBufPrintf(FileBuf, "%s/%s",
316 FlushStrBuf(FileBuf);
317 memcpy(URLBuf, ChrPtr(URL), StrLength(URL) + 1);
319 notify_http_server(URLBuf,
327 FreeStrBuf(&FileBuf);
335 commandSiz = sizeof(config.c_pager_program) + strlen(PagerNo) + strlen(msg->cm_fields['2']) + 5;
336 command = malloc(commandSiz);
337 snprintf(command, commandSiz, "%s %s -u %s", config.c_pager_program, PagerNo, msg->cm_fields['2']);
348 CtdlFreeMessage(msg);
349 todelete[0] = NotifyMsgnum;
350 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
354 * \brief Run through the pager room queue
355 * Checks to see what notification option the user has set
357 void do_extnotify_queue(void)
360 static int doing_queue = 0;
364 * This is a simple concurrency check to make sure only one queue run
365 * is done at a time. We could do this with a mutex, but since we
366 * don't really require extremely fine granularity here, we'll do it
367 * with a static variable instead.
369 if (IsEmptyStr(config.c_pager_program) &&
370 IsEmptyStr(config.c_funambol_host))
372 syslog(LOG_ERR, "No external notifiers configured on system/user\n");
376 if (doing_queue) return;
379 become_session(&extnotify_queue_CC);
381 pthread_setspecific(MyConKey, (void *)&extnotify_queue_CC);
384 * Go ahead and run the queue
386 syslog(LOG_DEBUG, "serv_extnotify: processing notify queue\n");
388 memset(&Ctx, 0, sizeof(NotifyContext));
389 if ((GetNotifyHosts(&Ctx) > 0) &&
390 (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0))
392 syslog(LOG_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
393 if (Ctx.nNotifyHosts > 0)
395 for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
396 FreeStrBuf(&Ctx.NotifyHostList[i]);
397 free(Ctx.NotifyHostList);
401 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
402 SPOOLMIME, NULL, process_notify, &Ctx);
403 syslog(LOG_DEBUG, "serv_extnotify: queue run completed\n");
405 if (Ctx.nNotifyHosts > 0)
407 for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
408 FreeStrBuf(&Ctx.NotifyHostList[i]);
409 free(Ctx.NotifyHostList);
415 /* Create the notify message queue. We use the exact same room
416 * as the Funambol module.
418 * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
419 * and sets as system room.
421 void create_extnotify_queue(void) {
422 struct ctdlroom qrbuf;
424 CtdlCreateRoom(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
426 CtdlFillSystemContext(&extnotify_queue_CC, "Extnotify");
429 * Make sure it's set to be a "system room" so it doesn't show up
430 * in the <K>nown rooms list for Aides.
432 if (CtdlGetRoomLock(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
433 qrbuf.QRflags2 |= QR2_SYSTEM;
434 CtdlPutRoomLock(&qrbuf);
439 CTDL_MODULE_INIT(extnotify)
443 create_extnotify_queue();
444 CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
446 /* return our module name for the log */