Straightn http client generation
[citadel.git] / citadel / modules / extnotify / extnotify_main.c
index b26284af51f8221b99722509004135c36f5b88c9..3029322950dc79f66fdd7a2909d8911cf31c8c44 100644 (file)
@@ -8,23 +8,24 @@
  * Based on bits of serv_funambol
  * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
  *
- * Copyright (c) 2008-2009
+ * Copyright (c) 2008-2011
  *
- *  This program is free 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 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.
+ * 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
+ * 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>
@@ -65,7 +66,6 @@
 #include "clientsocket.h"
 #include "event_client.h"
 #include "extnotify.h"
-
 #include "ctdl_module.h"
 
 struct CitContext extnotify_queue_CC;
@@ -92,7 +92,7 @@ StrBuf* GetNHBuf(int i, int allocit, StrBuf **NotifyHostList)
 }
 
 
-StrBuf** GetNotifyHosts(void)
+int GetNotifyHosts(NotifyContext *Ctx)
 {
        char NotifyHostsBuf[SIZ];
        StrBuf *Host;
@@ -101,39 +101,39 @@ StrBuf** GetNotifyHosts(void)
        int notify;
        const char *pchs, *pche;
        const char *NextHost = NULL;
-       StrBuf **NotifyHostList;
-       int num_notify;
 
        /* See if we have any Notification Hosts configured */
-       num_notify = get_hosts(NotifyHostsBuf, "notify");
-       if (num_notify < 1)
-               return(NULL);
+       Ctx->nNotifyHosts = get_hosts(NotifyHostsBuf, "notify");
+       if (Ctx->nNotifyHosts < 1)
+               return 0;
 
-       NotifyHostList = malloc(sizeof(StrBuf*) * 2 * (num_notify + 1));
-       memset(NotifyHostList, 0, sizeof(StrBuf*) * 2 * (num_notify + 1));
+       Ctx->NotifyHostList = malloc(sizeof(StrBuf*) * 2 * (Ctx->nNotifyHosts + 1));
+       memset(Ctx->NotifyHostList, 0, sizeof(StrBuf*) * 2 * (Ctx->nNotifyHosts + 1));
        
        NotifyBuf = NewStrBufPlain(NotifyHostsBuf, -1);
        /* get all configured notifiers's */
-        for (notify=0; notify<num_notify; notify++) {
+        for (notify=0; notify<Ctx->nNotifyHosts; notify++) {
                
-               Host = GetNHBuf(notify * 2, 1, NotifyHostList);
+               Host = GetNHBuf(notify * 2, 1, Ctx->NotifyHostList);
                StrBufExtract_NextToken(Host, NotifyBuf, &NextHost, '|');
                pchs = ChrPtr(Host);
                pche = strchr(pchs, ':');
                if (pche == NULL) {
-                       CtdlLogPrintf(CTDL_ERR, 
-                                     "extnotify: filename of notification template not found in %s.\n", 
-                                     pchs);
+                       syslog(LOG_ERR, 
+                              "extnotify: filename of notification template not found in %s.\n", 
+                              pchs);
                        continue;
                }
-               File = GetNHBuf(notify * 2 + 1, 1, NotifyHostList);
+               File = GetNHBuf(notify * 2 + 1, 1, Ctx->NotifyHostList);
                StrBufPlain(File, pchs, pche - pchs);
                StrBufCutLeft(Host, pche - pchs + 1);
        }
-       return NotifyHostList;
+       FreeStrBuf(&NotifyBuf);
+       return Ctx->nNotifyHosts;
 }
 
 
+
 /*! \brief Get configuration message for pager/funambol system from the
  *                     users "My Citadel Config" room
  */
@@ -167,7 +167,7 @@ eNotifyType extNotify_getConfigMessage(char *username, char **PagerNumber, char
                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\n");
                return eNone;   /* No messages at all?  No further action. */
        }
        for (a = 0; a < num_msgs; ++a) {
@@ -183,6 +183,7 @@ eNotifyType extNotify_getConfigMessage(char *username, char **PagerNumber, char
                }
        }
        
+       free(msglist);
        if (msg == NULL)
                return eNone;
 
@@ -266,9 +267,9 @@ void process_notify(long NotifyMsgnum, void *usrdata)
        Ctx = (NotifyContext*) usrdata;
 
        msg = CtdlFetchMessage(NotifyMsgnum, 1);
-       if ( msg->cm_fields['W'] != NULL) 
+       if ( msg->cm_fields['2'] != NULL) 
        {
-               Type = extNotify_getConfigMessage(msg->cm_fields['W'], &PagerNo, &FreeMe);
+               Type = extNotify_getConfigMessage(msg->cm_fields['2'], &PagerNo, &FreeMe);
        
                pch = strstr(msg->cm_fields['M'], "msgid|");
                if (pch != NULL) 
@@ -283,13 +284,10 @@ void process_notify(long NotifyMsgnum, void *usrdata)
                                 config.c_funambol_port,
                                 FUNAMBOL_WS);
 
-                       SubC = CloneContext (CC);
-                       SubC->session_specific_data = NULL;// (char*) DupNotifyContext(Ctx);
-                       
                        notify_http_server(remoteurl, 
                                           file_funambol_msg,
                                           strlen(file_funambol_msg),/*GNA*/
-                                          msg->cm_fields['W'], 
+                                          msg->cm_fields['2'], 
                                           msg->cm_fields['I'],
                                           msgnum, 
                                           NULL);
@@ -302,7 +300,7 @@ void process_notify(long NotifyMsgnum, void *usrdata)
                        StrBuf *File;
                        StrBuf *FileBuf = NewStrBuf();
                
-                       while(1)
+                       for (i = 0; i < Ctx->nNotifyHosts; i++)
                        {
 
                                URL = GetNHBuf(i*2, 0, Ctx->NotifyHostList);
@@ -318,16 +316,13 @@ void process_notify(long NotifyMsgnum, void *usrdata)
                                        FlushStrBuf(FileBuf);
                                memcpy(URLBuf, ChrPtr(URL), StrLength(URL) + 1);
 
-                               SubC = CloneContext (CC);
-                               SubC->session_specific_data = NULL;// (char*) DupNotifyContext(Ctx);
                                notify_http_server(URLBuf, 
                                                   ChrPtr(FileBuf),
                                                   StrLength(FileBuf),
-                                                  msg->cm_fields['W'], 
+                                                  msg->cm_fields['2'], 
                                                   msg->cm_fields['I'],
                                                   msgnum, 
                                                   NULL);
-                               i++;
                        }
                        FreeStrBuf(&FileBuf);
                } 
@@ -337,9 +332,9 @@ void process_notify(long NotifyMsgnum, void *usrdata)
                        int commandSiz;
                        char *command;
 
-                       commandSiz = sizeof(config.c_pager_program) + strlen(PagerNo) + strlen(msg->cm_fields['W']) + 5;
+                       commandSiz = sizeof(config.c_pager_program) + strlen(PagerNo) + strlen(msg->cm_fields['2']) + 5;
                        command = malloc(commandSiz);
-                       snprintf(command, commandSiz, "%s %s -u %s", config.c_pager_program, PagerNo, msg->cm_fields['W']);
+                       snprintf(command, commandSiz, "%s %s -u %s", config.c_pager_program, PagerNo, msg->cm_fields['2']);
                        system(command);
                        free(command);
                }
@@ -355,21 +350,15 @@ void process_notify(long NotifyMsgnum, void *usrdata)
        CtdlDeleteMessages(FNBL_QUEUE_ROOM, todelete, 1, "");
 }
 
-
-
-
-
-
 /*!
  * \brief Run through the pager room queue
+ * Checks to see what notification option the user has set
  */
 void do_extnotify_queue(void) 
 {
-       CitContext *CCC = CC;
-
        NotifyContext Ctx;
        static int doing_queue = 0;
-       //int i = 0;
+       int i = 0;
     
        /*
         * This is a simple concurrency check to make sure only one queue run
@@ -377,35 +366,48 @@ void do_extnotify_queue(void)
         * don't really require extremely fine granularity here, we'll do it
         * with a static variable instead.
         */
-
        if (IsEmptyStr(config.c_pager_program) && 
            IsEmptyStr(config.c_funambol_host))
        {
-               CtdlLogPrintf(CTDL_DEBUG, "No external notifiers configured on system/user");
+               syslog(LOG_ERR, "No external notifiers configured on system/user\n");
                return;
        }
 
        if (doing_queue) return;
        doing_queue = 1;
 
-       citthread_setspecific(MyConKey, (void *)&extnotify_queue_CC);
+       become_session(&extnotify_queue_CC);
+
+       pthread_setspecific(MyConKey, (void *)&extnotify_queue_CC);
 
        /*
         * Go ahead and run the queue
         */
-       CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: processing notify queue\n");
+       syslog(LOG_DEBUG, "serv_extnotify: processing notify queue\n");
 
        memset(&Ctx, 0, sizeof(NotifyContext));
-       Ctx.NotifyHostList = GetNotifyHosts();
-       if (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0) {
-               CtdlLogPrintf(CTDL_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
-               CtdlClearSystemContext();
+       if ((GetNotifyHosts(&Ctx) > 0) && 
+           (CtdlGetRoom(&CC->room, FNBL_QUEUE_ROOM) != 0))
+       {
+               syslog(LOG_ERR, "Cannot find room <%s>\n", FNBL_QUEUE_ROOM);
+               if (Ctx.nNotifyHosts > 0)
+               {
+                       for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
+                               FreeStrBuf(&Ctx.NotifyHostList[i]);
+                       free(Ctx.NotifyHostList);
+               }
                return;
        }
        CtdlForEachMessage(MSGS_ALL, 0L, NULL,
                           SPOOLMIME, NULL, process_notify, &Ctx);
-       CtdlLogPrintf(CTDL_DEBUG, "serv_extnotify: queue run completed\n");
+       syslog(LOG_DEBUG, "serv_extnotify: queue run completed\n");
        doing_queue = 0;
+       if (Ctx.nNotifyHosts > 0)
+       {
+               for (i = 0; i < Ctx.nNotifyHosts * 2; i++)
+                       FreeStrBuf(&Ctx.NotifyHostList[i]);
+               free(Ctx.NotifyHostList);
+       }
 }
 
 
@@ -433,6 +435,7 @@ void create_extnotify_queue(void) {
        }
 }
 
+
 CTDL_MODULE_INIT(extnotify)
 {
        if (!threading)
@@ -440,6 +443,6 @@ CTDL_MODULE_INIT(extnotify)
                create_extnotify_queue();
                CtdlRegisterSessionHook(do_extnotify_queue, EVT_TIMER);
        }
-       /* return our Subversion id for the Log */
+       /* return our module name for the log */
         return "extnotify";
 }