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-2015
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.
33 #include <sys/types.h>
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
40 # include <sys/time.h>
49 #include <sys/socket.h>
50 #include <libcitadel.h>
53 #include "citserver.h"
60 #include "internet_addressing.h"
62 #include "clientsocket.h"
63 #include "event_client.h"
64 #include "extnotify.h"
65 #include "ctdl_module.h"
67 struct CitContext extnotify_queue_CC;
69 void ExtNotify_PutErrorMessage(NotifyContext *Ctx, StrBuf *ErrMsg)
72 if (Ctx->NotifyErrors == NULL)
73 Ctx->NotifyErrors = NewHash(1, Flathash);
75 nNext = GetCount(Ctx->NotifyErrors) + 1;
76 Put(Ctx->NotifyErrors,
83 StrBuf* GetNHBuf(int i, int allocit, StrBuf **NotifyHostList)
85 if ((NotifyHostList[i] == NULL) && (allocit != 0))
86 NotifyHostList[i] = NewStrBuf();
87 return NotifyHostList[i];
91 int GetNotifyHosts(NotifyContext *Ctx)
93 char NotifyHostsBuf[SIZ];
98 const char *pchs, *pche;
99 const char *NextHost = NULL;
101 /* See if we have any Notification Hosts configured */
102 Ctx->nNotifyHosts = get_hosts(NotifyHostsBuf, "notify");
103 if (Ctx->nNotifyHosts < 1)
106 Ctx->NotifyHostList = malloc(sizeof(StrBuf*) *
108 (Ctx->nNotifyHosts + 1));
109 memset(Ctx->NotifyHostList, 0,
110 sizeof(StrBuf*) * 2 * (Ctx->nNotifyHosts + 1));
112 NotifyBuf = NewStrBufPlain(NotifyHostsBuf, -1);
113 /* get all configured notifiers's */
114 for (notify=0; notify<Ctx->nNotifyHosts; notify++) {
116 Host = GetNHBuf(notify * 2, 1, Ctx->NotifyHostList);
117 StrBufExtract_NextToken(Host, NotifyBuf, &NextHost, '|');
119 pche = strchr(pchs, ':');
122 "extnotify: filename of notification "
123 "template not found in %s.",
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,
144 struct ctdlroom qrbuf; // scratch for room
145 struct ctdluser user; // ctdl user instance
146 char configRoomName[ROOMNAMELEN];
147 struct CtdlMessage *msg = NULL;
148 struct cdbdata *cdbfr;
149 long *msglist = NULL;
157 CtdlGetUser(&user, username);
159 CtdlMailboxName(configRoomName,
160 sizeof(configRoomName),
164 CtdlGetRoom(&qrbuf, configRoomName);
165 /* Do something really, really stoopid here. Raid the room on ourselves,
166 * loop through the messages manually and find it. I don't want
167 * to use a CtdlForEachMessage callback here, as we would be
169 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
171 msglist = (long *) cdbfr->ptr;
173 /* CtdlForEachMessage() now owns this memory */
174 num_msgs = cdbfr->len / sizeof(long);
177 MARKM_syslog(LOG_DEBUG,
178 "extNotify_getConfigMessage: "
179 "No config messages found");
180 return eNone; /* No messages at all? No further action. */
182 for (a = 0; a < num_msgs; ++a) {
183 msg = CtdlFetchMessage(msglist[a], 1, 1);
185 if (!CM_IsEmpty(msg, eMsgSubject) &&
186 (strncasecmp(msg->cm_fields[eMsgSubject],
187 PAGER_CONFIG_MESSAGE,
188 strlen(PAGER_CONFIG_MESSAGE)) == 0))
201 // Do a simple string search to see if 'funambol' is selected as the
202 // type. This string would be at the very top of the message contents.
204 CM_GetAsField(msg, eMesageText, &configMsg, &clen);
207 /* here we would find the pager number... */
208 pch = strchr(configMsg, '\n');
216 * 1. The user has configured paging / They have and disabled it
217 * AND 2. There is an external pager program
218 * 3. A Funambol server has been entered
221 if (!strncasecmp(configMsg, "none", 4))
227 if (!strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP)))
230 return eHttpMessages;
232 if (!strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT)))
237 else if (!strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM)))
239 // whats the pager number?
240 if (!pch || (*pch == '\0'))
246 while (isspace(*pch))
249 while (isdigit(*pch) || (*pch == '+'))
262 * Process messages in the external notification queue
264 void process_notify(long NotifyMsgnum, void *usrdata)
270 struct CtdlMessage *msg;
276 Ctx = (NotifyContext*) usrdata;
278 msg = CtdlFetchMessage(NotifyMsgnum, 1, 1);
279 if (!CM_IsEmpty(msg, eExtnotify))
281 Type = extNotify_getConfigMessage(
282 msg->cm_fields[eExtnotify],
286 pch = strstr(msg->cm_fields[eMesageText], "msgid|");
288 msgnum = atol(pch + sizeof("msgid"));
293 snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
294 CtdlGetConfigStr("c_funambol_auth"),
295 CtdlGetConfigStr("c_funambol_host"),
296 CtdlGetConfigInt("c_funambol_port"),
299 notify_http_server(remoteurl,
301 strlen(file_funambol_msg),/*GNA*/
302 msg->cm_fields[eExtnotify],
303 msg->cm_fields[emessageId],
313 StrBuf *FileBuf = NewStrBuf();
315 for (i = 0; i < Ctx->nNotifyHosts; i++)
318 URL = GetNHBuf(i*2, 0, Ctx->NotifyHostList);
319 if (URL==NULL) break;
320 File = GetNHBuf(i*2 + 1, 0,
321 Ctx->NotifyHostList);
322 if (File==NULL) break;
324 if (StrLength(File)>0)
325 StrBufPrintf(FileBuf, "%s/%s",
329 FlushStrBuf(FileBuf);
330 memcpy(URLBuf, ChrPtr(URL), StrLength(URL) + 1);
332 notify_http_server(URLBuf,
335 msg->cm_fields[eExtnotify],
336 msg->cm_fields[emessageId],
340 FreeStrBuf(&FileBuf);
348 commandSiz = sizeof(CtdlGetConfigStr("c_pager_program")) +
350 msg->cm_lengths[eExtnotify] + 5;
352 command = malloc(commandSiz);
357 CtdlGetConfigStr("c_pager_program"),
359 msg->cm_fields[eExtnotify]);
372 todelete[0] = NotifyMsgnum;
373 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
377 * \brief Run through the pager room queue
378 * Checks to see what notification option the user has set
380 void do_extnotify_queue(void)
383 static int doing_queue = 0;
387 * This is a simple concurrency check to make sure only one queue run
388 * is done at a time. We could do this with a mutex, but since we
389 * don't really require extremely fine granularity here, we'll do it
390 * with a static variable instead.
392 if (IsEmptyStr(CtdlGetConfigStr("c_pager_program")) &&
393 IsEmptyStr(CtdlGetConfigStr("c_funambol_host")))
395 MARKM_syslog(LOG_DEBUG,
396 "No external notifiers configured on system/user");
405 become_session(&extnotify_queue_CC);
407 pthread_setspecific(MyConKey, (void *)&extnotify_queue_CC);
410 * Go ahead and run the queue
412 MARKM_syslog(LOG_DEBUG, "serv_extnotify: processing notify queue");
414 memset(&Ctx, 0, sizeof(NotifyContext));
415 if ((GetNotifyHosts(&Ctx) > 0) &&
416 (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0))
418 syslog(LOG_ERR, "Cannot find room <%s>", FNBL_QUEUE_ROOM);
419 if (Ctx.nNotifyHosts > 0)
421 for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
422 FreeStrBuf(&Ctx.NotifyHostList[i]);
423 free(Ctx.NotifyHostList);
427 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
428 SPOOLMIME, NULL, process_notify, &Ctx);
429 MARKM_syslog(LOG_DEBUG, "serv_extnotify: queue run completed");
431 if (Ctx.nNotifyHosts > 0)
433 for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
434 FreeStrBuf(&Ctx.NotifyHostList[i]);
435 free(Ctx.NotifyHostList);
441 /* Create the notify message queue. We use the exact same room
442 * as the Funambol module.
444 * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
445 * and sets as system room.
447 void create_extnotify_queue(void) {
448 struct ctdlroom qrbuf;
450 CtdlCreateRoom(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_QUEUE);
452 CtdlFillSystemContext(&extnotify_queue_CC, "Extnotify");
455 * Make sure it's set to be a "system room" so it doesn't show up
456 * in the <K>nown rooms list for Aides.
458 if (CtdlGetRoomLock(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
459 qrbuf.QRflags2 |= QR2_SYSTEM;
460 CtdlPutRoomLock(&qrbuf);
464 int extnotify_after_mbox_save(struct CtdlMessage *msg,
468 /* If this is private, local mail, make a copy in the
469 * recipient's mailbox and bump the reference count.
471 if (!IsEmptyStr(CtdlGetConfigStr("c_funambol_host")) || !IsEmptyStr(CtdlGetConfigStr("c_pager_program")))
473 /* Generate a instruction message for the Funambol notification
474 * server, in the same style as the SMTP queue
477 struct CtdlMessage *imsg;
479 instr = NewStrBufPlain(NULL, 1024);
481 "Content-type: "SPOOLMIME"\n"
486 msg->cm_fields[eVltMsgNum],
487 (long)time(NULL), //todo: time() is expensive!
491 imsg = malloc(sizeof(struct CtdlMessage));
492 memset(imsg, 0, sizeof(struct CtdlMessage));
493 imsg->cm_magic = CTDLMESSAGE_MAGIC;
494 imsg->cm_anon_type = MES_NORMAL;
495 imsg->cm_format_type = FMT_RFC822;
496 CM_SetField(imsg, eMsgSubject, HKEY("QMSG"));
497 CM_SetField(imsg, eAuthor, HKEY("Citadel"));
498 CM_SetField(imsg, eJournal, HKEY("do not journal"));
499 CM_SetAsFieldSB(imsg, eMesageText, &instr);
500 CM_SetField(imsg, eExtnotify, recps->recp_local, strlen(recps->recp_local));
501 CtdlSubmitMsg(imsg, NULL, FNBL_QUEUE_ROOM, 0);
507 CTDL_MODULE_INIT(extnotify)
511 create_extnotify_queue();
512 CtdlRegisterMessageHook(extnotify_after_mbox_save, EVT_AFTERUSRMBOXSAVE);
514 CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER, PRIO_SEND + 10);
516 /* return our module name for the log */