changeset 524:05c0e55c4bb1

Pass message body to external command Introduce 2 new options: event_log_files & event_log_dir
author Mikael Berthe <mikael@lilotux.net>
date Sun, 20 Nov 2005 00:08:49 +0100
parents fc6bc26f891e
children c7f94f6e51f0
files mcabber/src/hooks.c mcabber/src/utils.c mcabber/src/utils.h
diffstat 3 files changed, 53 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/hooks.c	Sat Nov 19 18:12:37 2005 +0100
+++ b/mcabber/src/hooks.c	Sun Nov 20 00:08:49 2005 +0100
@@ -21,6 +21,7 @@
 
 #include <sys/types.h>
 #include <unistd.h>
+#include <stdlib.h>
 
 #include "hooks.h"
 #include "screen.h"
@@ -28,6 +29,7 @@
 #include "histolog.h"
 #include "hbuf.h"
 #include "settings.h"
+#include "utils.h"
 
 static char *extcmd;
 
@@ -101,7 +103,7 @@
   // - We do not call hk_ext_cmd() for history lines in MUC
   // - We do call hk_ext_cmd() for private messages in a room
   if ((is_groupchat && !timestamp) || !is_groupchat)
-    hk_ext_cmd(jid, (is_groupchat ? 'G' : 'M'), 'R', NULL);
+    hk_ext_cmd(jid, (is_groupchat ? 'G' : 'M'), 'R', wmsg);
 
   // We need to rebuild the list if the sender is unknown or
   // if the sender is offline/invisible and hide_offline_buddies is set
@@ -233,6 +235,7 @@
   char *arg_info = NULL;
   char *arg_data = NULL;
   char status_str[2];
+  char *datafname = NULL;
 
   if (!extcmd) return;
 
@@ -263,8 +266,31 @@
 
   if (!arg_type || !arg_info) return;
 
+  if (strchr("MG", type) && data && settings_opt_get_int("event_log_files")) {
+    int fd;
+    const char *prefix;
+    prefix = settings_opt_get("event_log_dir");
+    if (!prefix)
+      prefix = ut_get_tmpdir();
+    datafname = g_strdup_printf("%s/mcabber-%d.XXXXXX", prefix, getpid());
+    // XXX Some old systems may require us to set umask first.
+    fd = mkstemp(datafname);
+    if (fd == -1) {
+      g_free(datafname);
+      datafname = NULL;
+      scr_LogPrint(LPRINT_LOGNORM,
+                   "Unable to create temp file for external command.");
+    }
+    write(fd, data, strlen(data));
+    write(fd, "\n", 1);
+    close(fd);
+    arg_data = datafname;
+  }
+
   if ((pid=fork()) == -1) {
     scr_LogPrint(LPRINT_LOGNORM, "Fork error, cannot launch external command.");
+    if (datafname)
+      g_free(datafname);
     return;
   }
 
@@ -274,5 +300,6 @@
       exit(1);
     }
   }
+  if (datafname)
+    g_free(datafname);
 }
-
--- a/mcabber/src/utils.c	Sat Nov 19 18:12:37 2005 +0100
+++ b/mcabber/src/utils.c	Sun Nov 20 00:08:49 2005 +0100
@@ -145,6 +145,28 @@
   return 0;
 }
 
+const char *ut_get_tmpdir(void)
+{
+  static const char *tmpdir;
+  const char *tmpvars[] = { "MCABBERTMPDIR", "TMP", "TMPDIR", "TEMP" };
+  int i;
+
+  if (tmpdir)
+    return tmpdir;
+
+  for (i = 0; i < (sizeof(tmpvars) / sizeof(const char *)); i++) {
+    tmpdir = getenv(tmpvars[i]);
+    if (tmpdir && tmpdir[0] && tmpdir[0] == '/' && tmpdir[1]) {
+      // Looks ok.
+      return tmpdir;
+    }
+  }
+
+  // Default temporary directory
+  tmpdir = "/tmp";
+  return tmpdir;
+}
+
 //  to_iso8601(dststr, timestamp)
 // Convert timestamp to iso8601 format, and store it in dststr.
 // NOTE: dststr should be at last 19 chars long.
--- a/mcabber/src/utils.h	Sat Nov 19 18:12:37 2005 +0100
+++ b/mcabber/src/utils.h	Sun Nov 20 00:08:49 2005 +0100
@@ -9,6 +9,8 @@
 
 int checkset_perm(const char *name, unsigned int setmode);
 
+const char *ut_get_tmpdir(void);
+
 int    to_iso8601(char *dststr, time_t timestamp);
 time_t from_iso8601(const char *timestamp, int utc);