Revision: 5481
          http://ipcop.svn.sourceforge.net/ipcop/?rev=5481&view=rev
Author:   dotzball
Date:     2011-02-23 06:28:56 +0000 (Wed, 23 Feb 2011)

Log Message:
-----------
Replace common placeholders (__HOSTNAME__, __CURRENT_DATE__) in given 
messagefile.

Modified Paths:
--------------
    ipcop/trunk/src/misc-progs/emailhelper.c

Modified: ipcop/trunk/src/misc-progs/emailhelper.c
===================================================================
--- ipcop/trunk/src/misc-progs/emailhelper.c    2011-02-23 06:28:13 UTC (rev 
5480)
+++ ipcop/trunk/src/misc-progs/emailhelper.c    2011-02-23 06:28:56 UTC (rev 
5481)
@@ -27,6 +27,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <syslog.h>
+#include <time.h>
 #include <unistd.h>
 #include "common.h"
 #include "setuid.h"
@@ -47,6 +48,7 @@
 #define EMAIL_ERR_HOSTNAME      11      // invalid hostname
 #define EMAIL_ERR_DOMAINNAME    12      // invalid domainname
 #define EMAIL_ERR_SENDEMAIL     13      // error in sendEmail
+#define EMAIL_ERR_SED           14      // error while replaciing placeholders 
in messagefile
 
 
 static int flag_subject = 0;
@@ -54,6 +56,8 @@
 static int flag_delete_messagefile  = 0;
 static int flag_attachment  = 0;
 static char command[STRING_SIZE_LARGE];
+static char sed[STRING_SIZE_LARGE];
+static char tmpEmailFile[STRING_SIZE] = "/tmp/email.msg.tmp";
 
 
 void usage(char *prg, int exit_code)
@@ -90,11 +94,13 @@
     char *opt_subject = NULL;
     char *opt_messagefile = NULL;
     char *opt_attachment = NULL;
+    char date[STRING_SIZE];
     NODEKV *kv = NULL;
     NODEKV *main_kv = NULL;
     int rc;
+    time_t curtime;
+    struct tm *loctime;
 
-
     static struct option long_options[] = {
         { "subject", required_argument, 0, 's'},
         { "messagefile", required_argument, 0, 'm'},
@@ -350,7 +356,22 @@
         }
         else {
             verbose_printf(1, "Use messagefile: %s \n", opt_messagefile);
-            snprintf(message, STRING_SIZE, "-o 
message-file=%s",opt_messagefile);
+            snprintf(message, STRING_SIZE, "-o message-file=%s", tmpEmailFile);
+
+            verbose_printf(2, "Replace placeholders in messagefile\n");
+
+           curtime = time (NULL);
+           loctime = localtime (&curtime);
+           strftime (date, STRING_SIZE, "%Y-%m-%d, %H:%M", loctime);
+
+            snprintf(sed, STRING_SIZE_LARGE, "/bin/sed -e 
's/__HOSTNAME__/%s.%s/;s/__CURRENT_DATE__/%s/;'  %s > %s", hostname, 
domainname, date, opt_messagefile, tmpEmailFile);
+
+            verbose_printf(2, "sed: %s\n", sed);
+            rc = safe_system(sed);
+            if (rc) {
+                fprintf(stderr, "Couldn't replace placeholders in messagefile: 
%d\n", rc);
+                exit(EMAIL_ERR_SED);
+            }
         }
     }
     else {
@@ -373,6 +394,7 @@
         strcpy(attachment, "");
     }
 
+
     snprintf(command, STRING_SIZE_LARGE,
             "/usr/bin/sendEmail -s %s%s %s %s -f %s -t %s %s %s %s ", server, 
port, user, password, from, to, subject, message, attachment);
 
@@ -383,6 +405,8 @@
         exit(EMAIL_ERR_SENDEMAIL);
     }
 
+    unlink(tmpEmailFile);
+
     if (flag_messagefile && flag_delete_messagefile) {
         verbose_printf(1, "Delete messagefile: %s \n", opt_messagefile);
         unlink(opt_messagefile);


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
Free Software Download: Index, Search & Analyze Logs and other IT data in 
Real-Time with Splunk. Collect, index and harness all the fast moving IT data 
generated by your applications, servers and devices whether physical, virtual
or in the cloud. Deliver compliance at lower cost and gain new business 
insights. http://p.sf.net/sfu/splunk-dev2dev 
_______________________________________________
Ipcop-svn mailing list
Ipcop-svn@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ipcop-svn

Reply via email to