]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/extnotify/funambol65.c
Merge branch 'master' of ssh://git.citadel.org/appl/gitroot/citadel
[citadel.git] / citadel / modules / extnotify / funambol65.c
index 77a2dacee667e9c8e0e1badd1c7acdd28379539c..1a109971988185f4b4ebeb44a1a3604fbdc5042c 100644 (file)
@@ -8,21 +8,15 @@
  * Based on bits of the previous serv_funambol
  * Contact: <matt@mcbridematt.dhs.org> / <matt@comalies>
  *
- * Copyright (c) 2008-2010
+ * Copyright (c) 2008-2015
  *
- * 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 version 3.
  *
  * 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 <stdio.h>
@@ -36,6 +30,7 @@
 #include <curl/curl.h>
 
 #include "citadel.h"
+#include "citserver.h"
 #include "citadel_dirs.h"
 #include "clientsocket.h"
 #include "sysdep.h"
@@ -49,6 +44,7 @@
 
 eNextState EvaluateResult(AsyncIO *IO);
 eNextState ExtNotifyTerminate(AsyncIO *IO);
+eNextState ExtNotifyTerminateDB(AsyncIO *IO);
 eNextState ExtNotifyShutdownAbort(AsyncIO *IO);
 
 /*
@@ -81,6 +77,7 @@ int notify_http_server(char *remoteurl,
                               "Citadel ExtNotify",
                               EvaluateResult,
                               ExtNotifyTerminate,
+                              ExtNotifyTerminateDB,
                               ExtNotifyShutdownAbort))
        {
                syslog(LOG_ALERT, "Unable to initialize libcurl.\n");
@@ -91,11 +88,15 @@ int notify_http_server(char *remoteurl,
 
        if (tlen > 0) {
                /* Load the template message. Get mallocs done too */
-               FILE *Ftemplate = NULL;
+               int fd;
+               struct stat statbuf;
                const char *mimetype;
+               const char *Err = NULL;
 
-               Ftemplate = fopen(template, "r");
-               if (Ftemplate == NULL) {
+               fd = open(template, O_RDONLY);
+               if ((fd < 0) ||
+                   (fstat(fd, &statbuf) == -1))
+               {
                        char buf[SIZ];
 
                        snprintf(buf, SIZ,
@@ -108,38 +109,38 @@ int notify_http_server(char *remoteurl,
                        CtdlAideMessage(
                                buf,
                                "External notifier: "
-                               "unable to find message template!");
+                               "unable to find/stat message template!");
                        goto abort;
                }
-               mimetype = GuessMimeByFilename(template, tlen);
-
-               buf = malloc(SIZ);
-               memset(buf, 0, SIZ);
-               SOAPMessage = malloc(3072);
-               memset(SOAPMessage, 0, 3072);
-
-               while(fgets(buf, SIZ, Ftemplate) != NULL) {
-                       strcat(SOAPMessage, buf);
-               }
-               fclose(Ftemplate);
 
-               if (strlen(SOAPMessage) < 0) {
+               Buf = NewStrBufPlain(NULL, statbuf.st_size + 1);
+               if (StrBufReadBLOB(Buf, &fd, 1, statbuf.st_size, &Err) < 0) {
                        char buf[SIZ];
 
+                       close(fd);
+
                        snprintf(buf, SIZ,
-                                "Cannot load template file %s;"
-                                " won't send notification\r\n",
-                                file_funambol_msg);
+                                "Cannot load template file %s [%s] "
+                                "won't send notification\r\n",
+                                file_funambol_msg,
+                                Err);
                        syslog(LOG_ERR, "%s", buf);
-
-                       CtdlAideMessage(buf, "External notifier: "
-                                       "unable to load message template!");
+                       // TODO: once an hour!
+                       CtdlAideMessage(
+                               buf,
+                               "External notifier: "
+                               "unable to load message template!");
                        goto abort;
                }
+               close(fd);
+
+               mimetype = GuessMimeByFilename(template, tlen);
+
+               SOAPMessage = SmashStrBuf(&Buf);
+
                // Do substitutions
                help_subst(SOAPMessage, "^notifyuser", user);
-               help_subst(SOAPMessage, "^syncsource",
-                          config.c_funambol_source);
+               help_subst(SOAPMessage, "^syncsource", CtdlGetConfigStr("c_funambol_source"));
                help_subst(SOAPMessage, "^msgid", msgid);
                help_subst(SOAPMessage, "^msgnum", msgnumstr);
 
@@ -157,7 +158,8 @@ int notify_http_server(char *remoteurl,
                IO->HttpReq.headers = curl_slist_append(
                        IO->HttpReq.headers,
                        contenttype);
-
+               free(contenttype);
+               contenttype = NULL;
                IO->HttpReq.headers = curl_slist_append(
                        IO->HttpReq.headers,
                        "Accept: application/soap+xml, "
@@ -173,7 +175,7 @@ int notify_http_server(char *remoteurl,
        }
        else {
                help_subst(remoteurl, "^notifyuser", user);
-               help_subst(remoteurl, "^syncsource", config.c_funambol_source);
+               help_subst(remoteurl, "^syncsource", CtdlGetConfigStr("c_funambol_source"));
                help_subst(remoteurl, "^msgid", msgid);
                help_subst(remoteurl, "^msgnum", msgnumstr);
 
@@ -256,6 +258,7 @@ eNextState EvaluateResult(AsyncIO *IO)
                CtdlAideMessage(ChrPtr(ErrMsg),
                                "External notifier: "
                                "unable to contact notification host!");
+               FreeStrBuf(&ErrMsg);
        }
 
        syslog(LOG_DEBUG, "Funambol notified\n");
@@ -292,9 +295,14 @@ eNextState EvaluateResult(AsyncIO *IO)
        ///if (SOAPMessage != NULL) free(SOAPMessage);
        ///if (buf != NULL) free(buf);
        ///FreeStrBuf (&ReplyBuf);
-       return 0;
+       return eTerminateConnection;
 }
 
+eNextState ExtNotifyTerminateDB(AsyncIO *IO)
+{
+       free(IO);
+       return eAbort;
+}
 eNextState ExtNotifyTerminate(AsyncIO *IO)
 {
        free(IO);