2 * \file extnotify_main.c
3 * @author Mathew McBride
5 * This module implements an external pager hook for when notifcation
6 * of a new email is wanted.
7 * Based on bits of serv_funambol
8 * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
19 #include <sys/types.h>
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
35 #include <sys/socket.h>
36 #include <libcitadel.h>
39 #include "citserver.h"
48 #include "internet_addressing.h"
50 #include "clientsocket.h"
51 #include "extnotify.h"
53 #include "ctdl_module.h"
55 StrBuf* GetNHBuf(int i, int allocit, StrBuf **NotifyHostList)
57 if ((NotifyHostList[i] == NULL) && (allocit != 0))
58 NotifyHostList[i] = NewStrBuf();
59 return NotifyHostList[i];
63 StrBuf** GetNotifyHosts(void)
65 char NotifyHostsBuf[SIZ];
70 const char *pchs, *pche;
71 const char *NextHost = NULL;
72 StrBuf **NotifyHostList;
75 /* See if we have any Notification Hosts configured */
76 num_notify = get_hosts(NotifyHostsBuf, "notify");
80 NotifyHostList = malloc(sizeof(StrBuf*) * 2 * (num_notify + 1));
81 memset(NotifyHostList, 0, sizeof(StrBuf*) * 2 * (num_notify + 1));
83 NotifyBuf = NewStrBufPlain(NotifyHostsBuf, -1);
84 /* get all configured notifiers's */
85 for (notify=0; notify<num_notify; notify++) {
87 Host = GetNHBuf(notify * 2, 1, NotifyHostList);
88 StrBufExtract_NextToken(Host, NotifyBuf, &NextHost, '|');
90 pche = strchr(pchs, ':');
92 CtdlLogPrintf(CTDL_ERR,
93 __FILE__": filename not found in %s.\n",
97 File = GetNHBuf(notify * 2 + 1, 1, NotifyHostList);
98 StrBufPlain(File, pchs, pche - pchs);
99 StrBufCutLeft(Host, pche - pchs + 1);
101 return NotifyHostList;
105 /*! \brief Create the notify message queue. We use the exact same room
106 * as the Funambol module.
108 * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
109 * and sets as system room.
111 void create_extnotify_queue(void) {
112 struct ctdlroom qrbuf;
114 create_room(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
117 * Make sure it's set to be a "system room" so it doesn't show up
118 * in the <K>nown rooms list for Aides.
120 if (lgetroom(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
121 qrbuf.QRflags2 |= QR2_SYSTEM;
126 * \brief Run through the pager room queue
128 void do_extnotify_queue(void) {
129 static int doing_queue = 0;
130 StrBuf **NotifyHosts;
134 * This is a simple concurrency check to make sure only one queue run
135 * is done at a time. We could do this with a mutex, but since we
136 * don't really require extremely fine granularity here, we'll do it
137 * with a static variable instead.
139 if (doing_queue) return;
143 * Go ahead and run the queue
145 CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: processing notify queue\n");
147 NotifyHosts = GetNotifyHosts();
148 if (getroom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
149 CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
152 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
153 SPOOLMIME, NULL, process_notify, NotifyHosts);
155 while ((NotifyHosts != NULL) && (NotifyHosts[i] != NULL))
156 FreeStrBuf(&NotifyHosts[i]);
158 CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: queue run completed\n");
162 * \brief Process messages in the external notification queue
164 void process_notify(long NotifyMsgnum, void *usrdata)
169 int extPagerAllowedHttp;
170 int extPagerAllowedSystem;
174 StrBuf **NotifyHostList;
175 struct CtdlMessage *msg;
178 NotifyHostList = (StrBuf**) usrdata;
179 msg = CtdlFetchMessage(NotifyMsgnum, 1);
180 if ( msg->cm_fields['W'] == NULL) {
184 configMsgNum = extNotify_getConfigMessage(msg->cm_fields['W']);
186 extNotify_getPrefs(configMsgNum, &configMsg[0]);
189 * 1. The user has configured paging / They have and disabled it
190 * AND 2. There is an external pager program
191 * 3. A Funambol server has been entered
194 if ((configMsgNum == -1) ||
195 ((strncasecmp(configMsg, "none", 4) == 0) &&
196 IsEmptyStr(config.c_pager_program) &&
197 IsEmptyStr(config.c_funambol_host))) {
198 CtdlLogPrintf(CTDL_DEBUG, "No external notifiers configured on system/user");
202 // Can Funambol take the message?
203 pch = strchr(configMsg, '\n');
206 fnblAllowed = strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT));
207 extPagerAllowedHttp = strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP));
208 extPagerAllowedSystem = strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM));
209 pch = strstr(msg->cm_fields['M'], "msgid|");
211 msgnum = atol(pch + sizeof("msgid"));
212 if (fnblAllowed == 0) {
214 snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
215 config.c_funambol_auth,
216 config.c_funambol_host,
217 config.c_funambol_port,
219 notify_http_server(remoteurl,
221 strlen(file_funambol_msg),/*GNA*/
225 } else if (extPagerAllowedHttp == 0) {
230 StrBuf *FileBuf = NewStrBuf();
235 URL = GetNHBuf(i*2, 0, NotifyHostList);
236 if (URL==NULL) break;
237 File = GetNHBuf(i*2 + 1, 0, NotifyHostList);
238 if (File==NULL) break;
240 if (StrLength(File)>0)
241 StrBufPrintf(FileBuf, "%s/%s",
245 FlushStrBuf(FileBuf);
246 memcpy(URLBuf, ChrPtr(URL), StrLength(URL) + 1);
248 notify_http_server(URLBuf,
256 FreeStrBuf(&FileBuf);
258 else if (extPagerAllowedSystem == 0) {
263 number = strtok(configMsg, "textmessage\n");
264 commandSiz = sizeof(config.c_pager_program) + strlen(number) + strlen(msg->cm_fields['W']) + 5;
265 command = malloc(commandSiz);
266 snprintf(command, commandSiz, "%s %s -u %s", config.c_pager_program, number, msg->cm_fields['W']);
271 CtdlFreeMessage(msg);
272 memset(configMsg, 0, sizeof(configMsg));
273 todelete[0] = NotifyMsgnum;
274 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
277 /*! \brief Checks to see what notification option the user has set
280 void extNotify_getPrefs(long configMsgNum, char *configMsg)
282 struct CtdlMessage *prefMsg;
283 // Do a simple string search to see if 'funambol' is selected as the
284 // type. This string would be at the very top of the message contents.
285 if (configMsgNum == -1) {
286 CtdlLogPrintf(CTDL_ERR, "extNotify_isAllowedByPrefs was passed a non-existant config message id\n");
289 prefMsg = CtdlFetchMessage(configMsgNum, 1);
290 strncpy(configMsg, prefMsg->cm_fields['M'], strlen(prefMsg->cm_fields['M']));
291 CtdlFreeMessage(prefMsg);
294 /*! \brief Get configuration message for pager/funambol system from the
295 * users "My Citadel Config" room
297 long extNotify_getConfigMessage(char *username) {
298 struct ctdlroom qrbuf; // scratch for room
299 struct ctdluser user; // ctdl user instance
300 char configRoomName[ROOMNAMELEN];
301 struct CtdlMessage *msg;
302 struct cdbdata *cdbfr;
303 long *msglist = NULL;
305 long confMsgNum = -1;
309 getuser(&user, username);
311 MailboxName(configRoomName, sizeof configRoomName, &user, USERCONFIGROOM);
313 getroom(&qrbuf, configRoomName);
314 /* Do something really, really stoopid here. Raid the room on ourselves,
315 * loop through the messages manually and find it. I don't want
316 * to use a CtdlForEachMessage callback here, as we would be
318 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
320 msglist = (long *) cdbfr->ptr;
321 cdbfr->ptr = NULL; /* CtdlForEachMessage() now owns this memory */
322 num_msgs = cdbfr->len / sizeof(long);
325 CtdlLogPrintf(CTDL_DEBUG, "extNotify_getConfigMessage: No config messages found\n");
326 return -1; /* No messages at all? No further action. */
328 for (a = 0; a < num_msgs; ++a) {
329 msg = CtdlFetchMessage(msglist[a], 1);
331 if ((msg->cm_fields['U'] != NULL) &&
332 (strncasecmp(msg->cm_fields['U'], PAGER_CONFIG_MESSAGE,
333 strlen(PAGER_CONFIG_MESSAGE)) == 0)) {
334 confMsgNum = msglist[a];
336 CtdlFreeMessage(msg);
343 CTDL_MODULE_INIT(extnotify)
347 create_extnotify_queue();
348 CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
350 /* return our Subversion id for the Log */