Did away with lprintf all together now its called CtdlLogPrintf()
[citadel.git] / citadel / modules / extnotify / extnotify_main.c
1 /*
2  * \file extnotify_main.c
3  * @author Mathew McBride
4  *
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>
9  */
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <sys/types.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <sys/wait.h>
33 #include <string.h>
34 #include <limits.h>
35 #include <sys/socket.h>
36 #include <libcitadel.h>
37 #include "citadel.h"
38 #include "server.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "control.h"
43 #include "room_ops.h"
44 #include "user_ops.h"
45 #include "policy.h"
46 #include "database.h"
47 #include "msgbase.h"
48 #include "internet_addressing.h"
49 #include "domain.h"
50 #include "clientsocket.h"
51 #include "extnotify.h"
52
53 #include "ctdl_module.h"
54
55 /*! \brief Create the notify message queue. We use the exact same room
56  *                      as the Funambol module.
57  *
58  *      Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
59  *      and sets as system room.
60  */
61 void create_extnotify_queue(void) {
62     struct ctdlroom qrbuf;
63     
64     create_room(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
65     
66     /*
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.
69      */
70     if (lgetroom(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
71         qrbuf.QRflags2 |= QR2_SYSTEM;
72         lputroom(&qrbuf);
73     }
74 }
75 /*!
76  * \brief Run through the pager room queue
77  */
78 void do_extnotify_queue(void) {
79     static int doing_queue = 0;
80     
81     /*
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.
86      */
87     if (doing_queue) return;
88     doing_queue = 1;
89     
90     /*
91      * Go ahead and run the queue
92      */
93     CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: processing notify queue\n");
94     
95     if (getroom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
96         CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
97         return;
98     }
99     CtdlForEachMessage(MSGS_ALL, 0L, NULL,
100             SPOOLMIME, NULL, process_notify, NULL);
101     
102     CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: queue run completed\n");
103     doing_queue = 0;
104 }
105 /*!
106  * \brief Process messages in the external notification queue
107  */
108 void process_notify(long msgnum, void *usrdata) {
109     struct CtdlMessage *msg;
110     msg = CtdlFetchMessage(msgnum, 1);
111     if ( msg->cm_fields['W'] == NULL) {
112         goto nuke;
113     }
114     
115     long configMsgNum = extNotify_getConfigMessage(msg->cm_fields['W']);
116     char configMsg[SIZ];
117     
118     extNotify_getPrefs(configMsgNum, &configMsg);
119     
120     /* Check to see if:
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
124      *
125      */
126     if ((configMsgNum == -1) || (strncasecmp(configMsg, "none", 4) == 0) &&
127     IsEmptyStr(config.c_pager_program) && IsEmptyStr(config.c_funambol_host)) {
128         CtdlLogPrintf(CTDL_DEBUG, "No external notifiers configured on system/user");
129         goto nuke;
130     }
131     // Can Funambol take the message?
132     int fnblAllowed = strncasecmp(configMsg, FUNAMBOL_CONFIG_TEXT, strlen(FUNAMBOL_CONFIG_TEXT));
133     int extPagerAllowed = strncasecmp(configMsg, PAGER_CONFIG_TEXT, strlen(PAGER_CONFIG_TEXT)); 
134     if (fnblAllowed == 0) {
135         notify_funambol_server(msg->cm_fields['W']);
136     } else if (extPagerAllowed == 0) {
137         char *number = strtok(configMsg, "textmessage\n");
138         int commandSiz = sizeof(config.c_pager_program) + strlen(number) + strlen(msg->cm_fields['W']) + 5;
139         char *command = malloc(commandSiz);
140         snprintf(command, commandSiz, "%s %s -u %s", config.c_pager_program, number, msg->cm_fields['W']);
141         system(command);
142         free(command);
143     }
144     nuke:
145     CtdlFreeMessage(msg);
146     memset(configMsg, 0, sizeof(configMsg));
147     long todelete[1];
148     todelete[0] = msgnum;
149     CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
150 }
151
152 /*! \brief Checks to see what notification option the user has set
153  *
154  */
155 char *extNotify_getPrefs(long configMsgNum, char *configMsg) {
156     // Do a simple string search to see if 'funambol' is selected as the
157     // type. This string would be at the very top of the message contents.
158     if (configMsgNum == -1) {
159         CtdlLogPrintf(CTDL_ERR, "extNotify_isAllowedByPrefs was passed a non-existant config message id\n");
160         return "none";
161     }
162     struct CtdlMessage *prefMsg;
163     prefMsg = CtdlFetchMessage(configMsgNum, 1);
164     strncpy(configMsg, prefMsg->cm_fields['M'], strlen(prefMsg->cm_fields['M']));
165     CtdlFreeMessage(prefMsg);
166     return configMsg;
167 }
168 /*! \brief Get configuration message for pager/funambol system from the
169  *                      users "My Citadel Config" room
170  */
171 long extNotify_getConfigMessage(char *username) {
172     struct ctdlroom qrbuf; // scratch for room
173     struct ctdluser user; // ctdl user instance
174     char configRoomName[ROOMNAMELEN];
175     struct CtdlMessage *msg;
176     struct cdbdata *cdbfr;
177     long *msglist = NULL;
178     int num_msgs = 0;
179     long confMsgNum = -1;
180     // Get the user
181     getuser(&user, username);
182     
183     MailboxName(configRoomName, sizeof configRoomName, &user, USERCONFIGROOM);
184     // Fill qrbuf
185     getroom(&qrbuf, configRoomName);
186     /* Do something really, really stoopid here. Raid the room on ourselves,
187      * loop through the messages manually and find it. I don't want
188      * to use a CtdlForEachMessage callback here, as we would be
189      * already in one */
190     cdbfr = cdb_fetch(CDB_MSGLISTS, &qrbuf.QRnumber, sizeof(long));
191     if (cdbfr != NULL) {
192         msglist = (long *) cdbfr->ptr;
193         cdbfr->ptr = NULL;      /* CtdlForEachMessage() now owns this memory */
194         num_msgs = cdbfr->len / sizeof(long);
195         cdb_free(cdbfr);
196     } else {
197         CtdlLogPrintf(CTDL_DEBUG, "extNotify_getConfigMessage: No config messages found\n");
198         return -1;      /* No messages at all?  No further action. */
199     }
200     int a;
201     for (a = 0; a < num_msgs; ++a) {
202         msg = CtdlFetchMessage(msglist[a], 1);
203         if (msg != NULL) {
204             if (msg->cm_fields['U'] != NULL && strncasecmp(msg->cm_fields['U'], PAGER_CONFIG_MESSAGE,
205                     strlen(PAGER_CONFIG_MESSAGE)) == 0) {
206                 confMsgNum = msglist[a];
207             }
208             CtdlFreeMessage(msg);
209         }
210     }
211     return confMsgNum;
212     
213 }
214 CTDL_MODULE_INIT(extnotify)
215 {
216         if (!threading)
217         {
218                 create_extnotify_queue();
219                 CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
220         }
221         /* return our Subversion id for the Log */
222         return "$Id:  $";
223 }