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 "event_client.h"
67 #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, ':');
122 CtdlLogPrintf(CTDL_ERR,
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;
136 /*! \brief Get configuration message for pager/funambol system from the
137 * users "My Citadel Config" room
139 eNotifyType extNotify_getConfigMessage(char *username, char **PagerNumber, char **FreeMe)
141 struct ctdlroom qrbuf; // scratch for room
142 struct ctdluser user; // ctdl user instance
143 char configRoomName[ROOMNAMELEN];
144 struct CtdlMessage *msg = NULL;
145 struct cdbdata *cdbfr;
146 long *msglist = NULL;
153 CtdlGetUser(&user, username);
155 CtdlMailboxName(configRoomName, sizeof configRoomName, &user, USERCONFIGROOM);
157 CtdlGetRoom(&qrbuf, configRoomName);
158 /* Do something really, really stoopid here. Raid the room on ourselves,
159 * loop through the messages manually and find it. I don't want
160 * to use a CtdlForEachMessage callback here, as we would be
162 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
164 msglist = (long *) cdbfr->ptr;
165 cdbfr->ptr = NULL; /* CtdlForEachMessage() now owns this memory */
166 num_msgs = cdbfr->len / sizeof(long);
169 CtdlLogPrintf(CTDL_DEBUG, "extNotify_getConfigMessage: No config messages found\n");
170 return eNone; /* No messages at all? No further action. */
172 for (a = 0; a < num_msgs; ++a) {
173 msg = CtdlFetchMessage(msglist[a], 1);
175 if ((msg->cm_fields['U'] != NULL) &&
176 (strncasecmp(msg->cm_fields['U'], PAGER_CONFIG_MESSAGE,
177 strlen(PAGER_CONFIG_MESSAGE)) == 0)) {
180 CtdlFreeMessage(msg);
189 // Do a simple string search to see if 'funambol' is selected as the
190 // type. This string would be at the very top of the message contents.
192 configMsg = msg->cm_fields['M'];
193 msg->cm_fields['M'] = NULL;
194 CtdlFreeMessage(msg);
196 /* here we would find the pager number... */
197 pch = strchr(configMsg, '\n');
205 * 1. The user has configured paging / They have and disabled it
206 * AND 2. There is an external pager program
207 * 3. A Funambol server has been entered
210 if (!strncasecmp(configMsg, "none", 4))
216 if (!strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP)))
219 return eHttpMessages;
221 if (!strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT)))
226 else if (!strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM)))
228 // whats the pager number?
229 if (!pch || (*pch == '\0'))
235 while (isspace(*pch))
238 while (isdigit(*pch) || (*pch == '+'))
251 * Process messages in the external notification queue
253 void process_notify(long NotifyMsgnum, void *usrdata)
259 struct CtdlMessage *msg;
266 Ctx = (NotifyContext*) usrdata;
268 msg = CtdlFetchMessage(NotifyMsgnum, 1);
269 if ( msg->cm_fields['W'] != NULL)
271 Type = extNotify_getConfigMessage(msg->cm_fields['W'], &PagerNo, &FreeMe);
273 pch = strstr(msg->cm_fields['M'], "msgid|");
275 msgnum = atol(pch + sizeof("msgid"));
280 snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
281 config.c_funambol_auth,
282 config.c_funambol_host,
283 config.c_funambol_port,
286 SubC = CloneContext (CC);
287 SubC->session_specific_data = NULL;// (char*) DupNotifyContext(Ctx);
289 notify_http_server(remoteurl,
291 strlen(file_funambol_msg),/*GNA*/
303 StrBuf *FileBuf = NewStrBuf();
305 for (i = 0; i < Ctx->nNotifyHosts; i++)
308 URL = GetNHBuf(i*2, 0, Ctx->NotifyHostList);
309 if (URL==NULL) break;
310 File = GetNHBuf(i*2 + 1, 0, Ctx->NotifyHostList);
311 if (File==NULL) break;
313 if (StrLength(File)>0)
314 StrBufPrintf(FileBuf, "%s/%s",
318 FlushStrBuf(FileBuf);
319 memcpy(URLBuf, ChrPtr(URL), StrLength(URL) + 1);
321 SubC = CloneContext (CC);
322 SubC->session_specific_data = NULL;// (char*) DupNotifyContext(Ctx);
323 notify_http_server(URLBuf,
331 FreeStrBuf(&FileBuf);
339 commandSiz = sizeof(config.c_pager_program) + strlen(PagerNo) + strlen(msg->cm_fields['W']) + 5;
340 command = malloc(commandSiz);
341 snprintf(command, commandSiz, "%s %s -u %s", config.c_pager_program, PagerNo, msg->cm_fields['W']);
352 CtdlFreeMessage(msg);
353 todelete[0] = NotifyMsgnum;
354 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
363 * \brief Run through the pager room queue
365 void do_extnotify_queue(void)
368 static int doing_queue = 0;
372 * This is a simple concurrency check to make sure only one queue run
373 * is done at a time. We could do this with a mutex, but since we
374 * don't really require extremely fine granularity here, we'll do it
375 * with a static variable instead.
378 if (IsEmptyStr(config.c_pager_program) &&
379 IsEmptyStr(config.c_funambol_host))
381 CtdlLogPrintf(CTDL_DEBUG, "No external notifiers configured on system/user");
385 if (doing_queue) return;
388 citthread_setspecific(MyConKey, (void *)&extnotify_queue_CC);
391 * Go ahead and run the queue
393 CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: processing notify queue\n");
395 memset(&Ctx, 0, sizeof(NotifyContext));
396 if ((GetNotifyHosts(&Ctx) > 0) &&
397 (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0))
399 CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
400 CtdlClearSystemContext();
401 if (Ctx.nNotifyHosts > 0)
403 for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
404 FreeStrBuf(&Ctx.NotifyHostList[i]);
405 free(Ctx.NotifyHostList);
409 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
410 SPOOLMIME, NULL, process_notify, &Ctx);
411 CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: queue run completed\n");
413 if (Ctx.nNotifyHosts > 0)
415 for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
416 FreeStrBuf(&Ctx.NotifyHostList[i]);
417 free(Ctx.NotifyHostList);
423 /* Create the notify message queue. We use the exact same room
424 * as the Funambol module.
426 * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
427 * and sets as system room.
429 void create_extnotify_queue(void) {
430 struct ctdlroom qrbuf;
432 CtdlCreateRoom(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
434 CtdlFillSystemContext(&extnotify_queue_CC, "Extnotify");
437 * Make sure it's set to be a "system room" so it doesn't show up
438 * in the <K>nown rooms list for Aides.
440 if (CtdlGetRoomLock(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
441 qrbuf.QRflags2 |= QR2_SYSTEM;
442 CtdlPutRoomLock(&qrbuf);
446 CTDL_MODULE_INIT(extnotify)
450 create_extnotify_queue();
451 CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
453 /* return our Subversion id for the Log */