]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/extnotify/extnotify_main.c
Removed references to Subversion in the code
[citadel.git] / citadel / modules / extnotify / extnotify_main.c
index d15b3b07bab9b6a7540a16dcecbd4f3fe10c3a9f..e848df490d8231691a2522130d794c980edb43aa 100644 (file)
@@ -1,13 +1,31 @@
 /*
- * \file extnotify_main.c
- * @author Mathew McBride
+ * extnotify_main.c
+ * Mathew McBride
  *
  * This module implements an external pager hook for when notifcation
  * of a new email is wanted.
+ *
  * Based on bits of serv_funambol
  * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
+ *
+ * Copyright (c) 2008-2011
+ *
+ * This program is open source software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  */
 
+
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
 #include "support.h"
 #include "config.h"
 #include "control.h"
-#include "room_ops.h"
 #include "user_ops.h"
-#include "policy.h"
 #include "database.h"
 #include "msgbase.h"
 #include "internet_addressing.h"
 #include "domain.h"
 #include "clientsocket.h"
 #include "extnotify.h"
-
 #include "ctdl_module.h"
 
+
+void ExtNotify_PutErrorMessage(NotifyContext *Ctx, StrBuf *ErrMsg)
+{
+       int nNext;
+       if (Ctx->NotifyErrors == NULL)
+               Ctx->NotifyErrors = NewHash(1, Flathash);
+
+       nNext = GetCount(Ctx->NotifyErrors) + 1;
+       Put(Ctx->NotifyErrors, 
+           (char*)&nNext, 
+           sizeof(int), 
+           ErrMsg, 
+           HFreeStrBuf);
+}
+
+
 StrBuf* GetNHBuf(int i, int allocit, StrBuf **NotifyHostList)
 {
        if ((NotifyHostList[i] == NULL) && (allocit != 0))
@@ -89,9 +120,7 @@ StrBuf** GetNotifyHosts(void)
                pchs = ChrPtr(Host);
                pche = strchr(pchs, ':');
                if (pche == NULL) {
-                       CtdlLogPrintf(CTDL_ERR, 
-                                     __FILE__": filename not found in %s.\n", 
-                                     pchs);
+                       syslog(LOG_ERR, __FILE__": filename not found in %s.", pchs);
                        continue;
                }
                File = GetNHBuf(notify * 2 + 1, 1, NotifyHostList);
@@ -102,32 +131,36 @@ StrBuf** GetNotifyHosts(void)
 }
 
 
-/*! \brief Create the notify message queue. We use the exact same room
- *                     as the Funambol module.
+/* Create the notify message queue. We use the exact same room
+ * as the Funambol module.
  *
- *     Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
- *     and sets as system room.
+ * Run at server startup, creates FNBL_QUEUE_ROOM if it doesn't exist
+ * and sets as system room.
  */
 void create_extnotify_queue(void) {
        struct ctdlroom qrbuf;
     
-       create_room(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
+       CtdlCreateRoom(FNBL_QUEUE_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
     
        /*
         * Make sure it's set to be a "system room" so it doesn't show up
         * in the <K>nown rooms list for Aides.
         */
-       if (lgetroom(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
+       if (CtdlGetRoomLock(&qrbuf, FNBL_QUEUE_ROOM) == 0) {
                qrbuf.QRflags2 |= QR2_SYSTEM;
-               lputroom(&qrbuf);
+               CtdlPutRoomLock(&qrbuf);
        }
 }
-/*!
- * \brief Run through the pager room queue
+
+
+/*
+ * Run through the pager room queue
  */
-void do_extnotify_queue(void) {
+void do_extnotify_queue(void) 
+{
+
+       NotifyContext Ctx;
        static int doing_queue = 0;
-       StrBuf **NotifyHosts;
        int i = 0;
     
        /*
@@ -138,31 +171,56 @@ void do_extnotify_queue(void) {
         */
        if (doing_queue) return;
        doing_queue = 1;
-    
+
        /*
         * Go ahead and run the queue
         */
-       CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: processing notify queue\n");
+       syslog(LOG_DEBUG, "serv_extnotify: processing notify queue");
     
-       NotifyHosts = GetNotifyHosts();
-       if (getroom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
-               CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
+       memset(&Ctx, 0, sizeof(NotifyContext));
+       Ctx.NotifyHostList = GetNotifyHosts();
+       if (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
+               syslog(LOG_ERR, "Cannot find room <%s>", FNBL_QUEUE_ROOM);
                return;
        }
        CtdlForEachMessage(MSGS_ALL, 0L, NULL,
-                          SPOOLMIME, NULL, process_notify, NotifyHosts);
+                          SPOOLMIME, NULL, process_notify, &Ctx);
 
-    while ((NotifyHosts != NULL) && (NotifyHosts[i] != NULL))
-           FreeStrBuf(&NotifyHosts[i]);
+       while ((Ctx.NotifyHostList != NULL) && (Ctx.NotifyHostList[i] != NULL))
+               FreeStrBuf(&Ctx.NotifyHostList[i]);
 
-    CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: queue run completed\n");
-    doing_queue = 0;
+       if (Ctx.NotifyErrors != NULL)
+       {
+               long len;
+               const char *Key;
+               HashPos *It;
+               void *vErr;
+               StrBuf *ErrMsg;
+
+               It = GetNewHashPos(Ctx.NotifyErrors, 0);
+               while (GetNextHashPos(Ctx.NotifyErrors, It, &len, &Key, &vErr) && 
+                      (vErr != NULL)) {
+                       ErrMsg = (StrBuf*) vErr;
+                       quickie_message("Citadel", NULL, NULL, AIDEROOM, ChrPtr(ErrMsg), FMT_FIXED, 
+                                       "Failed to notify external service about inbound mail");
+               }
+
+               DeleteHashPos(&It);
+               DeleteHash(&Ctx.NotifyErrors);
+       }
+
+       syslog(LOG_DEBUG, "serv_extnotify: queue run completed");
+       doing_queue = 0;
 }
-/*!
- * \brief Process messages in the external notification queue
+
+
+/*
+ * Process messages in the external notification queue
  */
-void process_notify(long msgnum, void *usrdata) 
+void process_notify(long NotifyMsgnum, void *usrdata) 
 {
+       NotifyContext *Ctx;
+       long msgnum = 0;
        long todelete[1];
        int fnblAllowed;
        int extPagerAllowedHttp;
@@ -170,12 +228,11 @@ void process_notify(long msgnum, void *usrdata)
        char *pch;
        long configMsgNum;
        char configMsg[SIZ];
-       StrBuf **NotifyHostList;        
        struct CtdlMessage *msg;
 
+       Ctx = (NotifyContext*) usrdata;
 
-       NotifyHostList = (StrBuf**) usrdata;
-       msg = CtdlFetchMessage(msgnum, 1);
+       msg = CtdlFetchMessage(NotifyMsgnum, 1);
        if ( msg->cm_fields['W'] == NULL) {
                goto nuke;
        }
@@ -194,7 +251,7 @@ void process_notify(long msgnum, void *usrdata)
            ((strncasecmp(configMsg, "none", 4) == 0) &&
             IsEmptyStr(config.c_pager_program) && 
             IsEmptyStr(config.c_funambol_host))) {
-               CtdlLogPrintf(CTDL_DEBUG, "No external notifiers configured on system/user");
+               syslog(LOG_DEBUG, "No external notifiers configured on system/user");
                goto nuke;
        }
 
@@ -205,7 +262,9 @@ void process_notify(long msgnum, void *usrdata)
        fnblAllowed = strncasecmp(configMsg, HKEY(FUNAMBOL_CONFIG_TEXT));
        extPagerAllowedHttp = strncasecmp(configMsg, HKEY(PAGER_CONFIG_HTTP)); 
        extPagerAllowedSystem = strncasecmp(configMsg, HKEY(PAGER_CONFIG_SYSTEM));
-
+       pch = strstr(msg->cm_fields['M'], "msgid|");
+       if (pch != NULL) 
+               msgnum = atol(pch + sizeof("msgid"));
        if (fnblAllowed == 0) {
                char remoteurl[SIZ];
                snprintf(remoteurl, SIZ, "http://%s@%s:%d/%s",
@@ -218,7 +277,8 @@ void process_notify(long msgnum, void *usrdata)
                                   strlen(file_funambol_msg),/*GNA*/
                                   msg->cm_fields['W'], 
                                   msg->cm_fields['I'],
-                                  msgnum);
+                                  msgnum, 
+                                  Ctx);
        } else if (extPagerAllowedHttp == 0) {
                int i = 0;
                StrBuf *URL;
@@ -229,9 +289,9 @@ void process_notify(long msgnum, void *usrdata)
                while(1)
                {
 
-                       URL = GetNHBuf(i*2, 0, NotifyHostList);
+                       URL = GetNHBuf(i*2, 0, Ctx->NotifyHostList);
                        if (URL==NULL) break;
-                       File = GetNHBuf(i*2 + 1, 0, NotifyHostList);
+                       File = GetNHBuf(i*2 + 1, 0, Ctx->NotifyHostList);
                        if (File==NULL) break;
 
                        if (StrLength(File)>0)
@@ -247,7 +307,8 @@ void process_notify(long msgnum, void *usrdata)
                                           StrLength(FileBuf),
                                           msg->cm_fields['W'], 
                                           msg->cm_fields['I'],
-                                          msgnum);
+                                          msgnum, 
+                                          Ctx);
                        i++;
                }
                FreeStrBuf(&FileBuf);
@@ -267,12 +328,13 @@ void process_notify(long msgnum, void *usrdata)
 nuke:
        CtdlFreeMessage(msg);
        memset(configMsg, 0, sizeof(configMsg));
-       todelete[0] = msgnum;
+       todelete[0] = NotifyMsgnum;
        CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
 }
 
-/*! \brief Checks to see what notification option the user has set
- *
+
+/*
+ * Checks to see what notification option the user has set
  */
 void extNotify_getPrefs(long configMsgNum, char *configMsg) 
 {
@@ -280,7 +342,7 @@ void extNotify_getPrefs(long configMsgNum, char *configMsg)
        // Do a simple string search to see if 'funambol' is selected as the
        // type. This string would be at the very top of the message contents.
        if (configMsgNum == -1) {
-               CtdlLogPrintf(CTDL_ERR, "extNotify_isAllowedByPrefs was passed a non-existant config message id\n");
+               syslog(LOG_ERR, "extNotify_isAllowedByPrefs was passed a non-existant config message id");
                return;
        }
        prefMsg = CtdlFetchMessage(configMsgNum, 1);
@@ -288,8 +350,9 @@ void extNotify_getPrefs(long configMsgNum, char *configMsg)
        CtdlFreeMessage(prefMsg);
 }
 
-/*! \brief Get configuration message for pager/funambol system from the
- *                     users "My Citadel Config" room
+
+/*
+ * Get configuration message for pager/funambol system from the user's "My Citadel Config" room
  */
 long extNotify_getConfigMessage(char *username) {
        struct ctdlroom qrbuf; // scratch for room
@@ -303,11 +366,11 @@ long extNotify_getConfigMessage(char *username) {
        int a;
 
        // Get the user
-       getuser(&user, username);
+       CtdlGetUser(&user, username);
     
-       MailboxName(configRoomName, sizeof configRoomName, &user, USERCONFIGROOM);
+       CtdlMailboxName(configRoomName, sizeof configRoomName, &user, USERCONFIGROOM);
        // Fill qrbuf
-       getroom(&qrbuf, configRoomName);
+       CtdlGetRoom(&qrbuf, configRoomName);
        /* Do something really, really stoopid here. Raid the room on ourselves,
         * loop through the messages manually and find it. I don't want
         * to use a CtdlForEachMessage callback here, as we would be
@@ -319,7 +382,7 @@ long extNotify_getConfigMessage(char *username) {
                num_msgs = cdbfr->len / sizeof(long);
                cdb_free(cdbfr);
        } else {
-               CtdlLogPrintf(CTDL_DEBUG, "extNotify_getConfigMessage: No config messages found\n");
+               syslog(LOG_DEBUG, "extNotify_getConfigMessage: No config messages found");
                return -1;      /* No messages at all?  No further action. */
        }
        for (a = 0; a < num_msgs; ++a) {
@@ -337,6 +400,7 @@ long extNotify_getConfigMessage(char *username) {
     
 }
 
+
 CTDL_MODULE_INIT(extnotify)
 {
        if (!threading)
@@ -344,6 +408,6 @@ CTDL_MODULE_INIT(extnotify)
                create_extnotify_queue();
                CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
        }
-       /* return our Subversion id for the Log */
-        return "$Id$";
+       /* return our module name for the log */
+        return "extnotify";
 }