0
|
1 |
/* |
|
2 |
* cmd.c -- Send shell command output as messages |
|
3 |
* |
|
4 |
* Copyrigth (C) 2009 Myhailo Danylenko <isbear@ukrpost.net> |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; either version 2 of the License, or (at |
|
9 |
* your option) any later version. |
|
10 |
* |
|
11 |
* This program is distributed in the hope that it will be useful, but |
|
12 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
* General Public License for more details. |
|
15 |
* |
|
16 |
* You should have received a copy of the GNU General Public License |
|
17 |
* along with this program; if not, write to the Free Software |
|
18 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|
19 |
* USA |
|
20 |
*/ |
|
21 |
|
|
22 |
#include <stdlib.h> |
|
23 |
#include <glib.h> |
|
24 |
#include <gmodule.h> |
|
25 |
#include <unistd.h> |
|
26 |
#include <errno.h> |
|
27 |
|
|
28 |
#include "commands.h" |
|
29 |
#include "logprint.h" |
|
30 |
#include "utils.h" |
|
31 |
#include "settings.h" |
|
32 |
#include "hbuf.h" |
|
33 |
#include "xmpp.h" |
|
34 |
#include "roster.h" |
|
35 |
|
|
36 |
typedef struct { |
|
37 |
gchar *jid; |
|
38 |
GString *input; |
5
|
39 |
gchar *subject; |
|
40 |
guint seq; |
0
|
41 |
guint source; |
|
42 |
GIOChannel *channel; |
|
43 |
} cmd_cb_t; |
|
44 |
|
5
|
45 |
static GSList *cmd_channels = NULL; |
|
46 |
|
|
47 |
static gboolean is_room (const gchar *jid) |
|
48 |
{ |
|
49 |
return roster_find (jid, jidsearch, ROSTER_TYPE_ROOM) ? TRUE : FALSE; |
|
50 |
} |
|
51 |
|
|
52 |
static gboolean cmd_send_msg (const gchar *to, const gchar *subject, const gchar *body) |
|
53 |
{ |
|
54 |
gboolean room = is_room (to); |
|
55 |
gpointer xep184 = NULL; |
|
56 |
gint crypted; |
|
57 |
|
|
58 |
xmpp_send_msg (to, body, room ? ROSTER_TYPE_ROOM : ROSTER_TYPE_USER, room ? NULL : subject, FALSE /* ? */, &crypted, LM_MESSAGE_SUB_TYPE_NOT_SET, &xep184); |
|
59 |
|
|
60 |
if (crypted == -1) { |
|
61 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Encryption error. Message not sent."); |
|
62 |
return FALSE; |
|
63 |
} |
|
64 |
|
|
65 |
if (!room) |
|
66 |
hk_message_out (to, subject, 0, body, crypted, xep184); |
|
67 |
|
|
68 |
return TRUE; |
|
69 |
} |
0
|
70 |
|
|
71 |
static gboolean cmd_reader (GIOChannel *channel, GIOCondition condition, gpointer data) |
|
72 |
{ |
|
73 |
cmd_cb_t *cb = (cmd_cb_t *) data; |
|
74 |
|
|
75 |
if (condition & (G_IO_IN|G_IO_PRI)) { |
5
|
76 |
GIOStatus chstat; |
|
77 |
static gchar buf[HBB_BLOCKSIZE]; |
|
78 |
gsize endpos; |
|
79 |
GError *error = NULL; |
0
|
80 |
|
5
|
81 |
chstat = g_io_channel_read_chars (channel, buf, HBB_BLOCKSIZE, &endpos, &error); |
|
82 |
|
|
83 |
if (error) { |
|
84 |
scr_LogPrint (LPRINT_DEBUG, "cmd: Reading error: %s.", error -> message); |
|
85 |
g_clear_error (&error); |
|
86 |
} |
0
|
87 |
|
|
88 |
if (chstat == G_IO_STATUS_ERROR || chstat == G_IO_STATUS_EOF) { |
|
89 |
cb->source = 0; |
|
90 |
return FALSE; // XXX |
|
91 |
} |
|
92 |
|
|
93 |
if (endpos) { |
|
94 |
GString *input = cb->input; |
|
95 |
gsize bread = 0; |
|
96 |
gsize written = 0; |
|
97 |
gchar *utf8 = NULL; |
|
98 |
|
|
99 |
g_string_append_len (input, buf, endpos); |
|
100 |
|
|
101 |
if (!lm_connection_is_authenticated (lconnection)) { |
|
102 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Connection is not ready, delaying data"); |
|
103 |
return TRUE; |
|
104 |
} |
|
105 |
|
|
106 |
// usual g_locale_to_utf8 seem to be unable to detect locale charset |
|
107 |
// maybe, proper solution will be to call setlocale on module loading, |
|
108 |
// but mcabber already does this, and I do not want to mess with it |
5
|
109 |
utf8 = g_convert (input->str, input->len, LocaleCharSet, "UTF-8", &bread, &written, &error); |
0
|
110 |
|
5
|
111 |
if (error && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE && bread) { |
0
|
112 |
written = 0; |
5
|
113 |
g_clear_error (&error); |
|
114 |
utf8 = g_convert (input->str, bread, LocaleCharSet, "UTF-8", &bread, &written, &error); |
0
|
115 |
} |
|
116 |
|
|
117 |
if (written) { |
|
118 |
gsize sent = 0; |
|
119 |
|
|
120 |
while (sent < written) { |
|
121 |
gsize len = 0; |
|
122 |
gchar *bbuf = NULL; |
5
|
123 |
gchar *subject = NULL; |
|
124 |
gboolean ret; |
0
|
125 |
|
|
126 |
if (written - sent > HBB_BLOCKSIZE) { |
|
127 |
gchar *c = utf8 + sent + HBB_BLOCKSIZE; |
|
128 |
c = g_utf8_find_prev_char (utf8 + sent, c); |
|
129 |
|
|
130 |
if (!c) { |
|
131 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Cannot determine utf8 character end! End of data chunk will be discarded!"); |
|
132 |
break; |
|
133 |
} |
|
134 |
|
|
135 |
len = c - utf8 - sent; |
|
136 |
bbuf = g_strndup (utf8 + sent, len); |
|
137 |
} |
|
138 |
|
5
|
139 |
cb -> seq += 1; |
0
|
140 |
|
5
|
141 |
if (cb -> subject) |
|
142 |
subject = g_strdup_printf (cb -> subject, cb -> seq); |
0
|
143 |
|
5
|
144 |
if (!cmd_send_msg (cb->jid, subject, len ? bbuf : (utf8 + sent))) |
0
|
145 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Encryption error. Message not sent."); |
|
146 |
|
5
|
147 |
if (subject) |
|
148 |
g_free (subject); |
0
|
149 |
|
|
150 |
if (!len) |
|
151 |
break; |
|
152 |
|
|
153 |
g_free (bbuf); |
|
154 |
|
|
155 |
sent += len; |
|
156 |
} |
|
157 |
|
|
158 |
g_free (utf8); |
|
159 |
|
|
160 |
g_string_erase (input, 0, bread); |
|
161 |
|
|
162 |
} else { |
|
163 |
|
5
|
164 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Character conversion error: %s", error->message); |
|
165 |
g_error_free (error); |
0
|
166 |
cb->source = 0; |
|
167 |
return FALSE; |
|
168 |
} |
|
169 |
} |
|
170 |
|
|
171 |
} else if (condition & (G_IO_ERR|G_IO_NVAL|G_IO_HUP)) { |
|
172 |
cb->source = 0; |
|
173 |
return FALSE; // XXX |
|
174 |
} |
|
175 |
|
|
176 |
return TRUE; |
|
177 |
} |
|
178 |
|
|
179 |
static void cmd_destroy_data (gpointer data) |
|
180 |
{ |
|
181 |
cmd_cb_t *cb = (cmd_cb_t *) data; |
|
182 |
|
|
183 |
cmd_channels = g_slist_remove (cmd_channels, data); |
|
184 |
|
5
|
185 |
if (cb->source) |
|
186 |
g_source_remove (cb->source); |
0
|
187 |
if (cb->channel) |
|
188 |
g_io_channel_unref (cb->channel); |
5
|
189 |
if (cb -> subject) |
|
190 |
g_free (cb -> subject); |
0
|
191 |
g_free (cb->jid); |
|
192 |
g_free (cb); |
|
193 |
} |
|
194 |
|
|
195 |
static void do_cmd (char *arg) |
|
196 |
{ |
|
197 |
int fd[2]; |
|
198 |
const char *jid = CURRENT_JID; |
|
199 |
|
|
200 |
if (!*arg) |
|
201 |
return; |
|
202 |
|
|
203 |
if (!jid) { |
5
|
204 |
scr_LogPrint (LPRINT_LOGNORM, "Unsuitable buddy selected."); |
0
|
205 |
return; |
|
206 |
} |
|
207 |
|
|
208 |
if (pipe (fd)) { |
5
|
209 |
scr_LogPrint (LPRINT_LOGNORM, "Cannot create pipe: %s.", strerror (errno)); |
0
|
210 |
return; |
|
211 |
} |
|
212 |
|
|
213 |
{ |
5
|
214 |
GIOChannel *channel; |
|
215 |
int res = fork (); |
0
|
216 |
|
|
217 |
if (!res) { |
|
218 |
|
|
219 |
close (fd[0]); |
|
220 |
dup2 (fd[1], STDOUT_FILENO); |
|
221 |
if (settings_opt_get_int ("cmd_redirect_stderr")) |
|
222 |
dup2 (fd[1], STDERR_FILENO); |
|
223 |
else |
|
224 |
close (STDERR_FILENO); |
|
225 |
close (STDIN_FILENO); |
|
226 |
close (fd[1]); |
|
227 |
|
|
228 |
{ |
|
229 |
const char *shell = settings_opt_get ("cmd_shell"); |
|
230 |
if (!shell) |
|
231 |
shell = getenv ("SHELL"); |
|
232 |
if (!shell) |
|
233 |
shell = "sh"; |
|
234 |
execl (shell, shell, "-c", arg, NULL); |
|
235 |
} |
|
236 |
} |
|
237 |
|
|
238 |
if (res == -1) { |
5
|
239 |
scr_LogPrint (LPRINT_NORMAL, "Cannot fork child: %s.", strerror (errno)); |
0
|
240 |
close (fd[0]); |
|
241 |
close (fd[1]); |
|
242 |
return; |
|
243 |
} |
|
244 |
|
|
245 |
close (fd[1]); |
|
246 |
|
5
|
247 |
{ |
|
248 |
GError *error = NULL; |
|
249 |
|
|
250 |
channel = g_io_channel_unix_new (fd[0]); |
0
|
251 |
|
5
|
252 |
g_io_channel_set_encoding (channel, NULL, &error); |
|
253 |
if (error) { |
|
254 |
scr_LogPrint (LPRINT_DEBUG, "cmd: Cannot unset channel encoding: %s.", error -> message); |
|
255 |
g_clear_error (&error); |
|
256 |
} |
|
257 |
g_io_channel_set_buffered (channel, FALSE); |
|
258 |
g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, &error); |
|
259 |
if (error) { |
|
260 |
scr_LogPrint (LPRINT_DEBUG, "cmd: Cannot set nonblocking flag on channel: %s.", error -> message); |
|
261 |
g_error_free (error); |
|
262 |
} |
|
263 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
264 |
} |
0
|
265 |
|
|
266 |
{ |
|
267 |
cmd_cb_t *cb = g_new (cmd_cb_t, 1); |
|
268 |
|
5
|
269 |
if (settings_opt_get_int ("cmd_header")) { |
|
270 |
if (is_room (jid)) { |
|
271 |
gchar *mesg = g_strdup_printf ("$ %s", arg); |
|
272 |
gchar *utf = to_utf8 (mesg); |
|
273 |
g_free (mesg); |
|
274 |
cmd_send_msg (jid, NULL, utf); |
|
275 |
g_free (utf); |
|
276 |
cb -> subject = NULL; |
|
277 |
} else { |
|
278 |
gchar *header = g_strdup_printf ("[%%02d] $ %s", arg); |
|
279 |
cb -> subject = to_utf8 (header); |
|
280 |
g_free (header); |
|
281 |
} |
|
282 |
} |
|
283 |
|
|
284 |
cb -> jid = g_strdup (jid); |
|
285 |
cb -> input = g_string_new (NULL); |
|
286 |
cb -> seq = 0; |
|
287 |
cb -> channel = channel; |
|
288 |
cb -> source = g_io_add_watch_full (channel, 0, G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL, |
|
289 |
cmd_reader, (gpointer) cb, cmd_destroy_data); |
0
|
290 |
|
|
291 |
cmd_channels = g_slist_append (cmd_channels, cb); |
|
292 |
} |
|
293 |
} |
|
294 |
} |
|
295 |
|
|
296 |
const gchar *g_module_check_init(GModule *module) |
|
297 |
{ |
|
298 |
cmd_add ("cmd", "", 0, 0, do_cmd, NULL); |
|
299 |
|
|
300 |
return NULL; |
|
301 |
} |
|
302 |
|
|
303 |
void g_module_unload(GModule *module) |
|
304 |
{ |
|
305 |
GSList *sel; |
|
306 |
|
|
307 |
cmd_del ("cmd"); |
|
308 |
|
|
309 |
for (sel = cmd_channels; sel; sel = sel->next) { |
|
310 |
cmd_cb_t *cb = (cmd_cb_t *) sel->data; |
|
311 |
if (cb->source) |
|
312 |
g_source_remove (cb->source); |
|
313 |
if (cb->channel) |
|
314 |
g_io_channel_unref (cb->channel); |
|
315 |
g_free (cb->jid); |
|
316 |
g_free (cb); |
|
317 |
} |
|
318 |
|
|
319 |
g_slist_free (cmd_channels); |
|
320 |
} |
|
321 |
|
|
322 |
/* vim: se ts=4 sw=4: */ |