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 /*! \brief Create the notify message queue. We use the exact same room
56 * as the Funambol module.
58 * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
59 * and sets as system room.
61 void create_extnotify_queue(void) {
62 struct ctdlroom qrbuf;
64 create_room(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
67 * Make sure it's set to be a "system room" so it doesn't show up
68 * in the <K>nown rooms list for Aides.
70 if (lgetroom(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
71 qrbuf.QRflags2 |= QR2_SYSTEM;
76 * \brief Run through the pager room queue
78 void do_extnotify_queue(void) {
79 static int doing_queue = 0;
82 * This is a simple concurrency check to make sure only one queue run
83 * is done at a time. We could do this with a mutex, but since we
84 * don't really require extremely fine granularity here, we'll do it
85 * with a static variable instead.
87 if (doing_queue) return;
91 * Go ahead and run the queue
93 CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: processing notify queue\n");
95 if (getroom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
96 CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
99 CtdlForEachMessage(MSGS_ALL, 0L, NULL,
100 SPOOLMIME, NULL, process_notify, NULL);
102 CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: queue run completed\n");
106 * \brief Process messages in the external notification queue
108 void process_notify(long msgnum, void *usrdata) {
109 struct CtdlMessage *msg;
110 msg = CtdlFetchMessage(msgnum, 1);
111 if ( msg->cm_fields['W'] == NULL) {
115 long configMsgNum = extNotify_getConfigMessage(msg->cm_fields['W']);
118 extNotify_getPrefs(configMsgNum, &configMsg[0]);
121 * 1. The user has configured paging / They have and disabled it
122 * AND 2. There is an external pager program
123 * 3. A Funambol server has been entered
126 if ((configMsgNum == -1) ||
127 ((strncasecmp(configMsg, "none", 4) == 0) &&
128 IsEmptyStr(config.c_pager_program) &&
129 IsEmptyStr(config.c_funambol_host))) {
130 CtdlLogPrintf(CTDL_DEBUG, "No external notifiers configured on system/user");
133 // Can Funambol take the message?
134 int fnblAllowed = strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT));
135 int extPagerAllowedHttp = strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP));
136 int extPagerAllowedSystem = strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM));
137 if (fnblAllowed == 0) {
139 snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
140 config.c_funambol_auth,
141 config.c_funambol_host,
142 config.c_funambol_port,
144 notify_http_server(remoteurl,
146 strlen(file_funambol_msg),/*GNA*/
150 } else if (extPagerAllowedHttp) {
152 notify_http_server(remoteurl,
154 strlen(file_funambol_msg),/ *GNA* /
159 } else if (extPagerAllowedSystem == 0) {
160 char *number = strtok(configMsg, "textmessage\n");
161 int commandSiz = sizeof(config.c_pager_program) + strlen(number) + strlen(msg->cm_fields['W']) + 5;
162 char *command = malloc(commandSiz);
163 snprintf(command, commandSiz, "%s %s -u %s", config.c_pager_program, number, msg->cm_fields['W']);
168 CtdlFreeMessage(msg);
169 memset(configMsg, 0, sizeof(configMsg));
171 todelete[0] = msgnum;
172 CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
175 /*! \brief Checks to see what notification option the user has set
178 void extNotify_getPrefs(long configMsgNum, char *configMsg) {
179 // Do a simple string search to see if 'funambol' is selected as the
180 // type. This string would be at the very top of the message contents.
181 if (configMsgNum == -1) {
182 CtdlLogPrintf(CTDL_ERR, "extNotify_isAllowedByPrefs was passed a non-existant config message id\n");
185 struct CtdlMessage *prefMsg;
186 prefMsg = CtdlFetchMessage(configMsgNum, 1);
187 strncpy(configMsg, prefMsg->cm_fields['M'], strlen(prefMsg->cm_fields['M']));
188 CtdlFreeMessage(prefMsg);
190 /*! \brief Get configuration message for pager/funambol system from the
191 * users "My Citadel Config" room
193 long extNotify_getConfigMessage(char *username) {
194 struct ctdlroom qrbuf; // scratch for room
195 struct ctdluser user; // ctdl user instance
196 char configRoomName[ROOMNAMELEN];
197 struct CtdlMessage *msg;
198 struct cdbdata *cdbfr;
199 long *msglist = NULL;
201 long confMsgNum = -1;
203 getuser(&user, username);
205 MailboxName(configRoomName, sizeof configRoomName, &user, USERCONFIGROOM);
207 getroom(&qrbuf, configRoomName);
208 /* Do something really, really stoopid here. Raid the room on ourselves,
209 * loop through the messages manually and find it. I don't want
210 * to use a CtdlForEachMessage callback here, as we would be
212 cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
214 msglist = (long *) cdbfr->ptr;
215 cdbfr->ptr = NULL; /* CtdlForEachMessage() now owns this memory */
216 num_msgs = cdbfr->len / sizeof(long);
219 CtdlLogPrintf(CTDL_DEBUG, "extNotify_getConfigMessage: No config messages found\n");
220 return -1; /* No messages at all? No further action. */
223 for (a = 0; a < num_msgs; ++a) {
224 msg = CtdlFetchMessage(msglist[a], 1);
226 if (msg->cm_fields['U'] != NULL && strncasecmp(msg->cm_fields['U'], PAGER_CONFIG_MESSAGE,
227 strlen(PAGER_CONFIG_MESSAGE)) == 0) {
228 confMsgNum = msglist[a];
230 CtdlFreeMessage(msg);
236 CTDL_MODULE_INIT(extnotify)
240 create_extnotify_queue();
241 CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
243 /* return our Subversion id for the Log */