Add module extsayng, rename previous one to extsay_old
authorMikael Berthe <mikael@lilotux.net>
Fri, 02 Apr 2010 23:23:58 +0200
changeset 21 9798e3aaa851
parent 20 4fbed301c014
child 22 b6b45c2eb022
Add module extsayng, rename previous one to extsay_old This new module is based on the screen utility, but it should be much safer to use.
Makefile.am
configure.ac
extsay-ng/Makefile.am
extsay-ng/README
extsay-ng/extsay.sh
extsay-ng/extsayng.c
extsay-old/Makefile.am
extsay-old/extsay_old.c
extsay/Makefile.am
extsay/extsay.c
--- a/Makefile.am	Wed Mar 31 23:01:39 2010 +0200
+++ b/Makefile.am	Fri Apr 02 23:23:58 2010 +0200
@@ -1,1 +1,1 @@
-SUBDIRS = clock comment extsay info_msgcount killpresence lastmsg
+SUBDIRS = clock comment extsay-ng extsay-old info_msgcount killpresence lastmsg
--- a/configure.ac	Wed Mar 31 23:01:39 2010 +0200
+++ b/configure.ac	Fri Apr 02 23:23:58 2010 +0200
@@ -98,14 +98,21 @@
               enable_all_modules=$enableval)
 
 AC_ARG_ENABLE(module-clock,
-              AC_HELP_STRING([--enable-module-clock], [enable module clock]),
+              AC_HELP_STRING([--enable-module-clock],
+                             [enable module clock]),
               enable_module_clock=$enableval)
 AC_ARG_ENABLE(module-comment,
-              AC_HELP_STRING([--enable-module-comment], [enable module comment]),
+              AC_HELP_STRING([--enable-module-comment],
+                             [enable module comment]),
               enable_module_comment=$enableval)
-AC_ARG_ENABLE(module-extsay,
-              AC_HELP_STRING([--enable-module-extsay], [enable module extsay]),
-              enable_module_extsay=$enableval)
+AC_ARG_ENABLE(module-extsayng,
+              AC_HELP_STRING([--enable-module-extsayng],
+                             [enable module extsayng]),
+              enable_module_extsayng=$enableval)
+AC_ARG_ENABLE(module-extsay_old,
+              AC_HELP_STRING([--enable-module-extsay_old],
+                             [enable module extsay_old]),
+              enable_module_extsay_old=$enableval)
 AC_ARG_ENABLE(module-info_msgcount,
               AC_HELP_STRING([--enable-module-info_msgcount],
                              [enable module info_msgcount]),
@@ -115,7 +122,8 @@
                              [enable module killpresence]),
               enable_module_lastmsg=$enableval)
 AC_ARG_ENABLE(module-lastmsg,
-              AC_HELP_STRING([--enable-module-lastmsg], [enable module lastmsg]),
+              AC_HELP_STRING([--enable-module-lastmsg],
+                             [enable module lastmsg]),
               enable_module_lastmsg=$enableval)
 
 AM_CONDITIONAL([INSTALL_MODULE_CLOCK],
@@ -126,9 +134,13 @@
                [test x"${enable_all_modules}" = x"yes" -o \
                      x"${enable_module_comment}" = x"yes"])
 
-AM_CONDITIONAL([INSTALL_MODULE_EXTSAY],
+AM_CONDITIONAL([INSTALL_MODULE_EXTSAYNG],
                [test x"${enable_all_modules}" = x"yes" -o \
-                     x"${enable_module_extsay}" = x"yes"])
+                     x"${enable_module_extsayng}" = x"yes"])
+
+AM_CONDITIONAL([INSTALL_MODULE_EXTSAY_OLD],
+               [test x"${enable_all_modules}" = x"yes" -o \
+                     x"${enable_module_extsay_old}" = x"yes"])
 
 AM_CONDITIONAL([INSTALL_MODULE_INFO_MSGCOUNT],
                [test x"${enable_all_modules}" = x"yes" -o \
@@ -144,7 +156,8 @@
 
 AC_CONFIG_FILES([clock/Makefile
                  comment/Makefile
-                 extsay/Makefile
+                 extsay-ng/Makefile
+                 extsay-old/Makefile
                  info_msgcount/Makefile
                  killpresence/Makefile
                  lastmsg/Makefile
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extsay-ng/Makefile.am	Fri Apr 02 23:23:58 2010 +0200
@@ -0,0 +1,11 @@
+
+if INSTALL_MODULE_EXTSAYNG
+
+pkglib_LTLIBRARIES = libextsayng.la
+libextsayng_la_SOURCES = extsayng.c
+libextsayng_la_LDFLAGS = -module -avoid-version -shared
+
+LDADD = $(GLIB_LIBS) $(MCABBER_LIBS)
+AM_CPPFLAGS = -I$(top_srcdir) $(GLIB_CFLAGS) $(MCABBER_CFLAGS)
+
+endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extsay-ng/README	Fri Apr 02 23:23:58 2010 +0200
@@ -0,0 +1,22 @@
+
+    ** Module extsay-ng **
+
+To use this module, you have to be running the screen utility.
+
+Copy the script extsay.sh to some place on your system and set the mcabber
+option 'extsay_script_path' to the file path.  You can set it in your mcabber
+configuration file.
+
+ /EXTSAY [jid]
+
+When you type /extsay, the module will send a command to screen to open a
+new window and run the helper script in it.
+
+WARNING: If you do not provide a JID, the selected user _at the time you
+close the editor_ will be used.  Be careful not to select another contact
+while you're editing your message!
+
+Hope you find this module useful, do not hesitate to send patches for
+improvements!
+
+Mikael
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extsay-ng/extsay.sh	Fri Apr 02 23:23:58 2010 +0200
@@ -0,0 +1,34 @@
+#! /bin/sh
+#
+# Copyright (C) 2010 Mikael Berthe <mikael@lilotux.net>
+#
+# Copy this script on your system and specify the path for mcabber
+# by setting the 'extsay_script_path' option.
+#
+# This script is free software.
+# MiKael, 2010-04-02
+
+FIFOPATH="$HOME/.mcabber/mcabber.fifo"
+tmpdir=${TMPDIR:="/tmp"}
+editor=${EDITOR:="vi"}
+jid="."
+
+# Use argument as a recipient JID, if it is provided
+[ $# -eq 1 ] && jid=$1
+
+# Leave if the FIFO is not available
+[ -p $FIFOPATH ] || exit 255
+
+tf=$(mktemp --tmpdir=$tmpdir extsay-XXXXXX) || exit 255
+
+# This will not work if the editor runs in the background!
+$editor $tf
+
+# Send the message using MCabber's pipe
+if [ -s $tf ]; then
+    cmd="say_to -f $tf $jid"
+    echo $cmd >> $FIFOPATH
+fi
+
+rm $tf
+exit 0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extsay-ng/extsayng.c	Fri Apr 02 23:23:58 2010 +0200
@@ -0,0 +1,95 @@
+/*
+   Copyright 2010 Mikael Berthe
+
+  Module "extsayng"   -- adds a /extsay command
+                         Spawns an external editor, using screen
+
+This module 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 2 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <glib/gstdio.h>
+
+#include <mcabber/modules.h>
+#include <mcabber/commands.h>
+#include <mcabber/settings.h>
+#include <mcabber/compl.h>
+#include <mcabber/logprint.h>
+
+static void extsayng_init(void);
+static void extsayng_uninit(void);
+
+/* Module description */
+module_info_t info_extsayng = {
+        .branch         = MCABBER_BRANCH,
+        .api            = MCABBER_API_VERSION,
+        .version        = "0.01",
+        .description    = "Use external editor to send a message",
+        .requires       = NULL,
+        .init           = extsayng_init,
+        .uninit         = extsayng_uninit,
+        .next           = NULL,
+};
+
+// Forks and run the external helper script
+static void screen_run_script(const char *args)
+{
+  GError *err = NULL;
+  gchar *argv[] = { "screen", "-r", "-X", "screen", NULL, NULL, NULL };
+  gboolean ret;
+
+  // screen -r -X screen $path/extsay.sh
+  argv[4] = (gchar*)settings_opt_get("extsay_script_path");
+
+  if (!argv[4] || !argv[4][0]) {
+    scr_log_print(LPRINT_NORMAL, "Please set option 'extsay_script_path'.");
+    return;
+  }
+
+  if (args && *args)
+    argv[5] = (gchar*)args;
+  else
+    argv[5] = ".";
+
+  ret = g_spawn_async(NULL, argv, NULL,
+                      G_SPAWN_SEARCH_PATH, //|
+                      //  G_SPAWN_STDOUT_TO_DEV_NULL|G_SPAWN_STDERR_TO_DEV_NULL,
+                      NULL, NULL, NULL, &err);
+
+  if (!ret)
+    scr_LogPrint(LPRINT_NORMAL, err->message);
+}
+
+static void do_extsayng(char *args)
+{
+  // TODO - check the selected item is a real contact
+  // (not a special buffer, nor a group...)
+  // TODO provide JID if it isn't provided
+  screen_run_script(args);
+}
+
+static void extsayng_init(void)
+{
+  cmd_add("extsay", "Use external editor to write a message",
+          COMPL_JID, 0, do_extsayng, NULL);
+}
+
+static void extsayng_uninit(void)
+{
+  cmd_del("extsay");
+}
+
+/* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extsay-old/Makefile.am	Fri Apr 02 23:23:58 2010 +0200
@@ -0,0 +1,11 @@
+
+if INSTALL_MODULE_EXTSAY_OLD
+
+pkglib_LTLIBRARIES = libextsay_old.la
+libextsay_old_la_SOURCES = extsay_old.c
+libextsay_old_la_LDFLAGS = -module -avoid-version -shared
+
+LDADD = $(GLIB_LIBS) $(MCABBER_LIBS)
+AM_CPPFLAGS = -I$(top_srcdir) $(GLIB_CFLAGS) $(MCABBER_CFLAGS)
+
+endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/extsay-old/extsay_old.c	Fri Apr 02 23:23:58 2010 +0200
@@ -0,0 +1,135 @@
+/*
+   Copyright 2009,2010 Andreas Fett
+   Copyright 2010 Mikael Berthe
+
+  Module "extsay_old" -- adds a /extsay_old command
+                         Spawns an external editor
+  Original code from Andreas Fett.
+
+This module 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 2 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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/wait.h>
+#include <glib/gstdio.h>
+
+#include <mcabber/modules.h>
+#include <mcabber/commands.h>
+#include <mcabber/screen.h>
+
+static void extsay_old_init(void);
+static void extsay_old_uninit(void);
+
+/* Module description */
+module_info_t info_extsay_old = {
+        .branch         = MCABBER_BRANCH,
+        .api            = MCABBER_API_VERSION,
+        .version        = "0.01",
+        .description    = "Use external editor to send a message\n"
+                          "WARNING: use at your own risk - "
+                          "mcabber is stuck while you write a message, "
+                          "this is not recommended!",
+        .requires       = NULL,
+        .init           = extsay_old_init,
+        .uninit         = extsay_old_uninit,
+        .next           = NULL,
+};
+
+// XXX Not very clean, internal function...
+char *load_message_from_file(const char *filename);
+
+static char* spawn_editor() {
+  GError *err = NULL;
+  gchar *argv[] = { NULL, NULL };
+  gchar *tmpfile = NULL;
+  gchar *tmpl = "mcabber-XXXXXX";
+  gchar *msg;
+  gboolean ret;
+  gint fd;
+  gint exit_status = 0;
+
+  argv[0] = (gchar*)g_getenv("EDITOR");
+  if (argv[0] == NULL) {
+    scr_LogPrint(LPRINT_NORMAL, "Environment variable EDITOR not set.");
+    return NULL;
+  }
+
+  fd = g_file_open_tmp(tmpl, &tmpfile, &err);
+  if ( fd < 0 ) {
+    scr_LogPrint(LPRINT_NORMAL, err->message);
+    g_error_free(err);
+    return NULL;
+  }
+  close(fd);
+
+  argv[1] = tmpfile;
+
+  endwin();
+  ret = g_spawn_sync(NULL, argv, NULL,
+                     G_SPAWN_CHILD_INHERITS_STDIN|G_SPAWN_SEARCH_PATH,
+                     NULL, NULL, NULL, NULL, &exit_status, &err);
+
+  raw();
+  readline_refresh_screen();
+
+  if (!ret) {
+    scr_LogPrint(LPRINT_NORMAL, err->message);
+    return NULL;
+  }
+
+  if (WEXITSTATUS(exit_status) != EXIT_SUCCESS) {
+    scr_LogPrint(LPRINT_NORMAL,
+                 "Editor exited with error, discarding message.");
+    return NULL;
+  }
+
+  msg = load_message_from_file(tmpfile);
+
+  if (g_unlink(tmpfile) != 0)
+    scr_LogPrint(LPRINT_NORMAL, "Warning, could not remove temp file.");
+
+  return msg;
+}
+
+static void do_extsay_old(char *args)
+{
+  char *msg = spawn_editor();
+  if (msg) {
+    say_cmd(msg, 0);
+    g_free(msg);
+  }
+}
+
+/* Initialization */
+static void extsay_old_init(void)
+{
+  /* Add command */
+  cmd_add("extsay_old", "Use external editor to write a message",
+          0, 0, do_extsay_old, NULL);
+  scr_LogPrint(LPRINT_NORMAL, "Loading module extsay_old...\n"
+               "** Be careful, everything is on hold while you run the "
+               "external editor.\n"
+               "** Do not run it for too long!"
+               );
+}
+
+/* Deinitialization */
+static void extsay_old_uninit(void)
+{
+  /* Unregister command */
+  cmd_del("extsay_old");
+}
+
+/* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */
--- a/extsay/Makefile.am	Wed Mar 31 23:01:39 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,11 +0,0 @@
-
-if INSTALL_MODULE_EXTSAY
-
-pkglib_LTLIBRARIES = libextsay.la
-libextsay_la_SOURCES = extsay.c
-libextsay_la_LDFLAGS = -module -avoid-version -shared
-
-LDADD = $(GLIB_LIBS) $(MCABBER_LIBS)
-AM_CPPFLAGS = -I$(top_srcdir) $(GLIB_CFLAGS) $(MCABBER_CFLAGS)
-
-endif
--- a/extsay/extsay.c	Wed Mar 31 23:01:39 2010 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,135 +0,0 @@
-/*
-   Copyright 2009,2010 Andreas Fett
-   Copyright 2010 Mikael Berthe
-
-  Module "extsay"     -- adds a /extsay command
-                         Spawns an external editor
-  Original code from Andreas Fett.
-
-This module 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 2 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, see <http://www.gnu.org/licenses/>.
-*/
-
-#include <unistd.h>
-#include <stdlib.h>
-#include <sys/wait.h>
-#include <glib/gstdio.h>
-
-#include <mcabber/modules.h>
-#include <mcabber/commands.h>
-#include <mcabber/screen.h>
-
-static void extsay_init(void);
-static void extsay_uninit(void);
-
-/* Module description */
-module_info_t info_extsay = {
-        .branch         = MCABBER_BRANCH,
-        .api            = MCABBER_API_VERSION,
-        .version        = "0.01",
-        .description    = "Use external editor to send a message\n"
-                          "WARNING: use at your own risk - "
-                          "mcabber is stuck while you write a message, "
-                          "this is not recommended!",
-        .requires       = NULL,
-        .init           = extsay_init,
-        .uninit         = extsay_uninit,
-        .next           = NULL,
-};
-
-// XXX Not very clean, internal function...
-char *load_message_from_file(const char *filename);
-
-static char* spawn_editor() {
-  GError *err = NULL;
-  gchar *argv[] = { NULL, NULL };
-  gchar *tmpfile = NULL;
-  gchar *tmpl = "mcabber-XXXXXX";
-  gchar *msg;
-  gboolean ret;
-  gint fd;
-  gint exit_status = 0;
-
-  argv[0] = (gchar*)g_getenv("EDITOR");
-  if (argv[0] == NULL) {
-    scr_LogPrint(LPRINT_NORMAL, "Environment variable EDITOR not set.");
-    return NULL;
-  }
-
-  fd = g_file_open_tmp(tmpl, &tmpfile, &err);
-  if ( fd < 0 ) {
-    scr_LogPrint(LPRINT_NORMAL, err->message);
-    g_error_free(err);
-    return NULL;
-  }
-  close(fd);
-
-  argv[1] = tmpfile;
-
-  endwin();
-  ret = g_spawn_sync(NULL, argv, NULL,
-                     G_SPAWN_CHILD_INHERITS_STDIN|G_SPAWN_SEARCH_PATH,
-                     NULL, NULL, NULL, NULL, &exit_status, &err);
-
-  raw();
-  readline_refresh_screen();
-
-  if (!ret) {
-    scr_LogPrint(LPRINT_NORMAL, err->message);
-    return NULL;
-  }
-
-  if (WEXITSTATUS(exit_status) != EXIT_SUCCESS) {
-    scr_LogPrint(LPRINT_NORMAL,
-                 "Editor exited with error, discarding message.");
-    return NULL;
-  }
-
-  msg = load_message_from_file(tmpfile);
-
-  if (g_unlink(tmpfile) != 0)
-    scr_LogPrint(LPRINT_NORMAL, "Warning, could not remove temp file.");
-
-  return msg;
-}
-
-static void do_extsay(char *args)
-{
-  char *msg = spawn_editor();
-  if (msg) {
-    say_cmd(msg, 0);
-    g_free(msg);
-  }
-}
-
-/* Initialization */
-static void extsay_init(void)
-{
-  /* Add command */
-  cmd_add("extsay", "Use external editor to write a message",
-          0, 0, do_extsay, NULL);
-  scr_LogPrint(LPRINT_NORMAL, "Loading module extsay...\n"
-               "** Be careful, everything is on hold while you run the "
-               "external editor.\n"
-               "** Do not run it for too long!"
-               );
-}
-
-/* Deinitialization */
-static void extsay_uninit(void)
-{
-  /* Unregister command */
-  cmd_del("extsay");
-}
-
-/* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */