extsay-ng/extsayng.c
changeset 21 9798e3aaa851
child 23 cf95a475825c
equal deleted inserted replaced
20:4fbed301c014 21:9798e3aaa851
       
     1 /*
       
     2    Copyright 2010 Mikael Berthe
       
     3 
       
     4   Module "extsayng"   -- adds a /extsay command
       
     5                          Spawns an external editor, using screen
       
     6 
       
     7 This module is free software: you can redistribute it and/or modify
       
     8 it under the terms of the GNU General Public License as published by
       
     9 the Free Software Foundation, either version 2 of the License, or
       
    10 (at your option) any later version.
       
    11 
       
    12 This program is distributed in the hope that it will be useful,
       
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    15 GNU General Public License for more details.
       
    16 
       
    17 You should have received a copy of the GNU General Public License
       
    18 along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    19 */
       
    20 
       
    21 #include <unistd.h>
       
    22 #include <stdlib.h>
       
    23 #include <sys/wait.h>
       
    24 #include <glib/gstdio.h>
       
    25 
       
    26 #include <mcabber/modules.h>
       
    27 #include <mcabber/commands.h>
       
    28 #include <mcabber/settings.h>
       
    29 #include <mcabber/compl.h>
       
    30 #include <mcabber/logprint.h>
       
    31 
       
    32 static void extsayng_init(void);
       
    33 static void extsayng_uninit(void);
       
    34 
       
    35 /* Module description */
       
    36 module_info_t info_extsayng = {
       
    37         .branch         = MCABBER_BRANCH,
       
    38         .api            = MCABBER_API_VERSION,
       
    39         .version        = "0.01",
       
    40         .description    = "Use external editor to send a message",
       
    41         .requires       = NULL,
       
    42         .init           = extsayng_init,
       
    43         .uninit         = extsayng_uninit,
       
    44         .next           = NULL,
       
    45 };
       
    46 
       
    47 // Forks and run the external helper script
       
    48 static void screen_run_script(const char *args)
       
    49 {
       
    50   GError *err = NULL;
       
    51   gchar *argv[] = { "screen", "-r", "-X", "screen", NULL, NULL, NULL };
       
    52   gboolean ret;
       
    53 
       
    54   // screen -r -X screen $path/extsay.sh
       
    55   argv[4] = (gchar*)settings_opt_get("extsay_script_path");
       
    56 
       
    57   if (!argv[4] || !argv[4][0]) {
       
    58     scr_log_print(LPRINT_NORMAL, "Please set option 'extsay_script_path'.");
       
    59     return;
       
    60   }
       
    61 
       
    62   if (args && *args)
       
    63     argv[5] = (gchar*)args;
       
    64   else
       
    65     argv[5] = ".";
       
    66 
       
    67   ret = g_spawn_async(NULL, argv, NULL,
       
    68                       G_SPAWN_SEARCH_PATH, //|
       
    69                       //  G_SPAWN_STDOUT_TO_DEV_NULL|G_SPAWN_STDERR_TO_DEV_NULL,
       
    70                       NULL, NULL, NULL, &err);
       
    71 
       
    72   if (!ret)
       
    73     scr_LogPrint(LPRINT_NORMAL, err->message);
       
    74 }
       
    75 
       
    76 static void do_extsayng(char *args)
       
    77 {
       
    78   // TODO - check the selected item is a real contact
       
    79   // (not a special buffer, nor a group...)
       
    80   // TODO provide JID if it isn't provided
       
    81   screen_run_script(args);
       
    82 }
       
    83 
       
    84 static void extsayng_init(void)
       
    85 {
       
    86   cmd_add("extsay", "Use external editor to write a message",
       
    87           COMPL_JID, 0, do_extsayng, NULL);
       
    88 }
       
    89 
       
    90 static void extsayng_uninit(void)
       
    91 {
       
    92   cmd_del("extsay");
       
    93 }
       
    94 
       
    95 /* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */