0
|
1 |
/* |
|
2 |
Copyright 2009,2010 Andreas Fett |
|
3 |
Copyright 2010 Mikael Berthe |
|
4 |
|
|
5 |
Module "extsay" -- adds a /extsay command |
|
6 |
Spawns an external editor |
|
7 |
Original code from Andreas Fett. |
|
8 |
|
|
9 |
This module is free software: you can redistribute it and/or modify |
|
10 |
it under the terms of the GNU General Public License as published by |
|
11 |
the Free Software Foundation, either version 2 of the License, or |
|
12 |
(at your option) any later version. |
|
13 |
|
|
14 |
This program is distributed in the hope that it will be useful, |
|
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
GNU General Public License for more details. |
|
18 |
|
|
19 |
You should have received a copy of the GNU General Public License |
|
20 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
*/ |
|
22 |
|
|
23 |
#include <unistd.h> |
|
24 |
#include <stdlib.h> |
|
25 |
#include <sys/wait.h> |
|
26 |
#include <glib/gstdio.h> |
|
27 |
|
|
28 |
#include <mcabber/modules.h> |
|
29 |
#include <mcabber/commands.h> |
|
30 |
#include <mcabber/screen.h> |
|
31 |
|
|
32 |
static void extsay_init(void); |
|
33 |
static void extsay_uninit(void); |
|
34 |
|
|
35 |
/* Module description */ |
|
36 |
module_info_t info_extsay = { |
|
37 |
.mcabber_version = "0.10.0", |
|
38 |
.requires = NULL, |
|
39 |
.init = extsay_init, |
|
40 |
.uninit = extsay_uninit, |
|
41 |
}; |
|
42 |
|
|
43 |
// XXX Not very clean, internal function... |
|
44 |
char *load_message_from_file(const char *filename); |
|
45 |
|
|
46 |
static char* spawn_editor() { |
|
47 |
GError *err = NULL; |
|
48 |
gchar *argv[] = { NULL, NULL }; |
|
49 |
gchar *tmpfile = NULL; |
|
50 |
gchar *tmpl = "mcabber-XXXXXX"; |
|
51 |
gchar *msg; |
|
52 |
gboolean ret; |
|
53 |
gint fd; |
|
54 |
gint exit_status = 0; |
|
55 |
|
|
56 |
argv[0] = (gchar*)g_getenv("EDITOR"); |
|
57 |
if (argv[0] == NULL) { |
|
58 |
scr_LogPrint(LPRINT_NORMAL, "Environment variable EDITOR not set."); |
|
59 |
return NULL; |
|
60 |
} |
|
61 |
|
|
62 |
fd = g_file_open_tmp(tmpl, &tmpfile, &err); |
|
63 |
if ( fd < 0 ) { |
|
64 |
scr_LogPrint(LPRINT_NORMAL, err->message); |
|
65 |
g_error_free(err); |
|
66 |
return NULL; |
|
67 |
} |
|
68 |
close(fd); |
|
69 |
|
|
70 |
argv[1] = tmpfile; |
|
71 |
|
|
72 |
ret = g_spawn_sync(NULL, argv, NULL, |
|
73 |
G_SPAWN_CHILD_INHERITS_STDIN|G_SPAWN_SEARCH_PATH, |
|
74 |
NULL, NULL, NULL, NULL, &exit_status, &err); |
|
75 |
|
|
76 |
readline_refresh_screen(); |
|
77 |
|
|
78 |
if (!ret) { |
|
79 |
scr_LogPrint(LPRINT_NORMAL, err->message); |
|
80 |
return NULL; |
|
81 |
} |
|
82 |
|
|
83 |
if (WEXITSTATUS(exit_status) != EXIT_SUCCESS) { |
|
84 |
scr_LogPrint(LPRINT_NORMAL, |
|
85 |
"Editor exited with error, discarding message."); |
|
86 |
return NULL; |
|
87 |
} |
|
88 |
|
|
89 |
msg = load_message_from_file(tmpfile); |
|
90 |
|
|
91 |
if (g_unlink(tmpfile) != 0) { |
|
92 |
scr_LogPrint(LPRINT_NORMAL,"Warning, could not remove temp file."); |
|
93 |
} |
|
94 |
|
|
95 |
return msg; |
|
96 |
} |
|
97 |
|
|
98 |
static void do_extsay(char *args) |
|
99 |
{ |
|
100 |
char *msg = spawn_editor(); |
|
101 |
if (msg) { |
|
102 |
say_cmd(msg, 0); |
|
103 |
g_free(msg); |
|
104 |
} |
|
105 |
} |
|
106 |
|
|
107 |
/* Initialization */ |
|
108 |
static void extsay_init(void) |
|
109 |
{ |
|
110 |
/* Add command */ |
|
111 |
cmd_add("extsay", "Use external editor to write a message", |
|
112 |
0, 0, do_extsay, NULL); |
|
113 |
} |
|
114 |
|
|
115 |
/* Deinitialization */ |
|
116 |
static void extsay_uninit(void) |
|
117 |
{ |
|
118 |
/* Unregister command */ |
|
119 |
cmd_del("extsay"); |
|
120 |
} |
|
121 |
|
|
122 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |