author | Mikael Berthe <mikael@lilotux.net> |
Tue, 09 Mar 2010 22:17:15 +0100 | |
changeset 7 | ad87c711de84 |
parent 5 | fbfc64c5d6be |
child 8 | 53b0091ecd47 |
permissions | -rw-r--r-- |
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 |
||
5
fbfc64c5d6be
[extsay] Fix ncurses reinitialization
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
72 |
endwin(); |
0 | 73 |
ret = g_spawn_sync(NULL, argv, NULL, |
74 |
G_SPAWN_CHILD_INHERITS_STDIN|G_SPAWN_SEARCH_PATH, |
|
75 |
NULL, NULL, NULL, NULL, &exit_status, &err); |
|
76 |
||
5
fbfc64c5d6be
[extsay] Fix ncurses reinitialization
Mikael Berthe <mikael@lilotux.net>
parents:
0
diff
changeset
|
77 |
raw(); |
0 | 78 |
readline_refresh_screen(); |
79 |
||
80 |
if (!ret) { |
|
81 |
scr_LogPrint(LPRINT_NORMAL, err->message); |
|
82 |
return NULL; |
|
83 |
} |
|
84 |
||
85 |
if (WEXITSTATUS(exit_status) != EXIT_SUCCESS) { |
|
86 |
scr_LogPrint(LPRINT_NORMAL, |
|
87 |
"Editor exited with error, discarding message."); |
|
88 |
return NULL; |
|
89 |
} |
|
90 |
||
91 |
msg = load_message_from_file(tmpfile); |
|
92 |
||
7
ad87c711de84
Add a warning when loading module extsay
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
93 |
if (g_unlink(tmpfile) != 0) |
ad87c711de84
Add a warning when loading module extsay
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
94 |
scr_LogPrint(LPRINT_NORMAL, "Warning, could not remove temp file."); |
0 | 95 |
|
7
ad87c711de84
Add a warning when loading module extsay
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
96 |
return msg; |
0 | 97 |
} |
98 |
||
99 |
static void do_extsay(char *args) |
|
100 |
{ |
|
101 |
char *msg = spawn_editor(); |
|
102 |
if (msg) { |
|
103 |
say_cmd(msg, 0); |
|
104 |
g_free(msg); |
|
105 |
} |
|
106 |
} |
|
107 |
||
108 |
/* Initialization */ |
|
109 |
static void extsay_init(void) |
|
110 |
{ |
|
111 |
/* Add command */ |
|
112 |
cmd_add("extsay", "Use external editor to write a message", |
|
113 |
0, 0, do_extsay, NULL); |
|
7
ad87c711de84
Add a warning when loading module extsay
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
114 |
scr_LogPrint(LPRINT_NORMAL, "Loading module extsay...\n" |
ad87c711de84
Add a warning when loading module extsay
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
115 |
"** Be careful, everything is on hold while you run the " |
ad87c711de84
Add a warning when loading module extsay
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
116 |
"external editor.\n" |
ad87c711de84
Add a warning when loading module extsay
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
117 |
"** Do not run it for too long!" |
ad87c711de84
Add a warning when loading module extsay
Mikael Berthe <mikael@lilotux.net>
parents:
5
diff
changeset
|
118 |
); |
0 | 119 |
} |
120 |
||
121 |
/* Deinitialization */ |
|
122 |
static void extsay_uninit(void) |
|
123 |
{ |
|
124 |
/* Unregister command */ |
|
125 |
cmd_del("extsay"); |
|
126 |
} |
|
127 |
||
128 |
/* vim: set expandtab cindent cinoptions=>2\:2(0: For Vim users... */ |