extsay-old/extsay_old.c
changeset 21 9798e3aaa851
parent 8 53b0091ecd47
--- /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... */