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*) *
112 (Ctx->nNotifyHosts + 1));
113 memset(Ctx->NotifyHostList, 0,
114 sizeof(StrBuf*) * 2 * (Ctx->nNotifyHosts + 1));
116 NotifyBuf = NewStrBufPlain(NotifyHostsBuf, -1);
117 /* get all configured notifiers's */
118 for (notify=0; notify<Ctx->nNotifyHosts; notify++) {
120 Host = GetNHBuf(notify * 2, 1, Ctx->NotifyHostList);
121 StrBufExtract_NextToken(Host, NotifyBuf, &NextHost, '|');
123 pche = strchr(pchs, ':');
126 "extnotify: filename of notification "
127 "template not found in %s.\n",
131 File = GetNHBuf(notify * 2 + 1, 1, Ctx->NotifyHostList);
132 StrBufPlain(File, pchs, pche - pchs);
133 StrBufCutLeft(Host, pche - pchs + 1);
135 FreeStrBuf(&NotifyBuf);
136 return Ctx->nNotifyHosts;
141 /*! \brief Get configuration message for pager/funambol system from the
142 * users "My Citadel Config" room
144 eNotifyType extNotify_getConfigMessage(char *username,
148 struct ctdlroom qrbuf; // scratch for room
149 struct ctdluser user; // ctdl user instance
150 char configRoomName[ROOMNAMELEN];
151 struct CtdlMessage *msg = NULL;
152 struct cdbdata *cdbfr;
153 long *msglist = NULL;
160 CtdlGetUser(&user, username);
162 CtdlMailboxName(configRoomName,
163 sizeof(configRoomName),
167 CtdlGetRoom(&qrbuf, configRoomName);
168 /* Do something really, really stoopid here. Raid the room on ourselves,
169 * loop through the messages manually and find it. I don't want
170 * to use a CtdlForEachMessage callback here, as we would be
172 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
174 msglist = (long *) cdbfr->ptr;
176 /* CtdlForEachMessage() now owns this memory */
177 num_msgs = cdbfr->len / sizeof(long);
181 "extNotify_getConfigMessage: "
182 "No config messages found\n");
183 return eNone; /* No messages at all? No further action. */
185 for (a = 0; a < num_msgs; ++a) {
186 msg = CtdlFetchMessage(msglist[a], 1);
188 if ((msg->cm_fields['U'] != NULL) &&
189 (strncasecmp(msg->cm_fields['U'],
190 PAGER_CONFIG_MESSAGE,
191 strlen(PAGER_CONFIG_MESSAGE)) == 0))
195 CtdlFreeMessage(msg);
204 // Do a simple string search to see if 'funambol' is selected as the
205 // type. This string would be at the very top of the message contents.
207 configMsg = msg->cm_fields['M'];
208 msg->cm_fields['M'] = NULL;
209 CtdlFreeMessage(msg);
211 /* here we would find the pager number... */
212 pch = strchr(configMsg, '\n');
220 * 1. The user has configured paging / They have and disabled it
221 * AND 2. There is an external pager program
222 * 3. A Funambol server has been entered
225 if (!strncasecmp(configMsg, "none", 4))
231 if (!strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP)))
234 return eHttpMessages;
236 if (!strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT)))
241 else if (!strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM)))
243 // whats the pager number?
244 if (!pch || (*pch == '\0'))
250 while (isspace(*pch))
253 while (isdigit(*pch) || (*pch == '+'))
266 * Process messages in the external notification queue
268 void process_notify(long NotifyMsgnum, void *usrdata)
274 struct CtdlMessage *msg;
280 Ctx = (NotifyContext*) usrdata;
282 msg = CtdlFetchMessage(NotifyMsgnum, 1);
283 if ( msg->cm_fields['2'] != NULL)
285 Type = extNotify_getConfigMessage(
290 pch = strstr(msg->cm_fields['M'], "msgid|");
292 msgnum = atol(pch + sizeof("msgid"));
297 snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
298 config.c_funambol_auth,
299 config.c_funambol_host,
300 config.c_funambol_port,
303 notify_http_server(remoteurl,
305 strlen(file_funambol_msg),/*GNA*/
317 StrBuf *FileBuf = NewStrBuf();
319 for (i = 0; i < Ctx->nNotifyHosts; i++)
322 URL = GetNHBuf(i*2, 0, Ctx->NotifyHostList);
323 if (URL==NULL) break;
324 File = GetNHBuf(i*2 + 1, 0,
325 Ctx->NotifyHostList);
326 if (File==NULL) break;
328 if (StrLength(File)>0)
329 StrBufPrintf(FileBuf, "%s/%s",
333 FlushStrBuf(FileBuf);
334 memcpy(URLBuf, ChrPtr(URL), StrLength(URL) + 1);
336 notify_http_server(URLBuf,
344 FreeStrBuf(&FileBuf);
352 commandSiz = sizeof(config.c_pager_program) +
354 strlen(msg->cm_fields['2']) + 5;
356 command = malloc(commandSiz);
361 config.c_pager_program,
363 msg->cm_fields['2']);
375 CtdlFreeMessage(msg);
376 todelete[0] = NotifyMsgnum;
377 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
381 * \brief Run through the pager room queue
382 * Checks to see what notification option the user has set
384 void do_extnotify_queue(void)
387 static int doing_queue = 0;
391 * This is a simple concurrency check to make sure only one queue run
392 * is done at a time. We could do this with a mutex, but since we
393 * don't really require extremely fine granularity here, we'll do it
394 * with a static variable instead.
396 if (IsEmptyStr(config.c_pager_program) &&
397 IsEmptyStr(config.c_funambol_host))
400 "No external notifiers configured on system/user\n");
409 become_session(&extnotify_queue_CC);
411 pthread_setspecific(MyConKey, (void *)&extnotify_queue_CC);
414 * Go ahead and run the queue
416 syslog(LOG_DEBUG, "serv_extnotify: processing notify queue\n");
418 memset(&Ctx, 0, sizeof(NotifyContext));
419 if ((GetNotifyHosts(&Ctx) > 0) &&
420 (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0))
422 syslog(LOG_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
423 if (Ctx.nNotifyHosts > 0)
425 for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
426 FreeStrBuf(&Ctx.NotifyHostList[i]);
427 free(Ctx.NotifyHostList);
431 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
432 SPOOLMIME, NULL, process_notify, &Ctx);
433 syslog(LOG_DEBUG, "serv_extnotify: queue run completed\n");
435 if (Ctx.nNotifyHosts > 0)
437 for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
438 FreeStrBuf(&Ctx.NotifyHostList[i]);
439 free(Ctx.NotifyHostList);
445 /* Create the notify message queue. We use the exact same room
446 * as the Funambol module.
448 * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
449 * and sets as system room.
451 void create_extnotify_queue(void) {
452 struct ctdlroom qrbuf;
454 CtdlCreateRoom(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_QUEUE);
456 CtdlFillSystemContext(&extnotify_queue_CC, "Extnotify");
459 * Make sure it's set to be a "system room" so it doesn't show up
460 * in the <K>nown rooms list for Aides.
462 if (CtdlGetRoomLock(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
463 qrbuf.QRflags2 |= QR2_SYSTEM;
464 CtdlPutRoomLock(&qrbuf);
469 CTDL_MODULE_INIT(extnotify)
473 create_extnotify_queue();
474 CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER, PRIO_SEND + 10);
476 /* return our module name for the log */