author | Myhailo Danylenko <isbear@ukrpost.net> |
Thu, 04 Mar 2010 19:50:39 +0200 | |
changeset 15 | 891fc0779d50 |
parent 13 | 88315b883803 |
child 16 | 19d13d97b3a1 |
permissions | -rw-r--r-- |
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 <unistd.h> |
|
25 |
#include <errno.h> |
|
26 |
||
8
5771827a4976
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
7
diff
changeset
|
27 |
#include <mcabber/commands.h> |
5771827a4976
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
7
diff
changeset
|
28 |
#include <mcabber/logprint.h> |
5771827a4976
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
7
diff
changeset
|
29 |
#include <mcabber/utils.h> |
5771827a4976
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
7
diff
changeset
|
30 |
#include <mcabber/settings.h> |
5771827a4976
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
7
diff
changeset
|
31 |
#include <mcabber/hbuf.h> |
5771827a4976
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
7
diff
changeset
|
32 |
#include <mcabber/xmpp.h> |
5771827a4976
Change mcabber headers naming convention
Myhailo Danylenko <isbear@ukrpost.net>
parents:
7
diff
changeset
|
33 |
#include <mcabber/roster.h> |
15
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
34 |
#include <mcabber/modules.h> |
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
35 |
|
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
36 |
static void mcmd_init (void); |
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
37 |
static void mcmd_uninit (void); |
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
38 |
|
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
39 |
module_info_t info_cmd = { |
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
40 |
.mcabber_version = "0.10.0", |
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
41 |
.requires = NULL, |
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
42 |
.init = mcmd_init, |
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
43 |
.uninit = mcmd_uninit, |
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
44 |
}; |
0 | 45 |
|
46 |
typedef struct { |
|
47 |
gchar *jid; |
|
48 |
GString *input; |
|
5 | 49 |
gchar *subject; |
50 |
guint seq; |
|
0 | 51 |
guint source; |
52 |
GIOChannel *channel; |
|
53 |
} cmd_cb_t; |
|
54 |
||
5 | 55 |
static GSList *cmd_channels = NULL; |
56 |
||
57 |
static gboolean is_room (const gchar *jid) |
|
58 |
{ |
|
59 |
return roster_find (jid, jidsearch, ROSTER_TYPE_ROOM) ? TRUE : FALSE; |
|
60 |
} |
|
61 |
||
62 |
static gboolean cmd_send_msg (const gchar *to, const gchar *subject, const gchar *body) |
|
63 |
{ |
|
64 |
gboolean room = is_room (to); |
|
65 |
gpointer xep184 = NULL; |
|
66 |
gint crypted; |
|
67 |
||
68 |
xmpp_send_msg (to, body, room ? ROSTER_TYPE_ROOM : ROSTER_TYPE_USER, room ? NULL : subject, FALSE /* ? */, &crypted, LM_MESSAGE_SUB_TYPE_NOT_SET, &xep184); |
|
69 |
||
70 |
if (crypted == -1) { |
|
71 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Encryption error. Message not sent."); |
|
72 |
return FALSE; |
|
73 |
} |
|
74 |
||
6
8e64b6d3302e
Fix outgoing message header displaying
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
75 |
if (!room) { |
8e64b6d3302e
Fix outgoing message header displaying
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
76 |
if (subject) { |
8e64b6d3302e
Fix outgoing message header displaying
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
77 |
gchar *hbody = g_strdup_printf ("[%s]\n%s", subject, body); |
8e64b6d3302e
Fix outgoing message header displaying
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
78 |
hk_message_out (to, NULL, 0, hbody, crypted, xep184); |
8e64b6d3302e
Fix outgoing message header displaying
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
79 |
g_free (hbody); |
8e64b6d3302e
Fix outgoing message header displaying
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
80 |
} else |
8e64b6d3302e
Fix outgoing message header displaying
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
81 |
hk_message_out (to, NULL, 0, body, crypted, xep184); |
8e64b6d3302e
Fix outgoing message header displaying
Myhailo Danylenko <isbear@ukrpost.net>
parents:
5
diff
changeset
|
82 |
} |
5 | 83 |
|
84 |
return TRUE; |
|
85 |
} |
|
0 | 86 |
|
87 |
static gboolean cmd_reader (GIOChannel *channel, GIOCondition condition, gpointer data) |
|
88 |
{ |
|
89 |
cmd_cb_t *cb = (cmd_cb_t *) data; |
|
90 |
||
91 |
if (condition & (G_IO_IN|G_IO_PRI)) { |
|
5 | 92 |
GIOStatus chstat; |
93 |
static gchar buf[HBB_BLOCKSIZE]; |
|
94 |
gsize endpos; |
|
95 |
GError *error = NULL; |
|
0 | 96 |
|
5 | 97 |
chstat = g_io_channel_read_chars (channel, buf, HBB_BLOCKSIZE, &endpos, &error); |
98 |
||
99 |
if (error) { |
|
100 |
scr_LogPrint (LPRINT_DEBUG, "cmd: Reading error: %s.", error -> message); |
|
101 |
g_clear_error (&error); |
|
102 |
} |
|
0 | 103 |
|
104 |
if (chstat == G_IO_STATUS_ERROR || chstat == G_IO_STATUS_EOF) { |
|
105 |
cb->source = 0; |
|
106 |
return FALSE; // XXX |
|
107 |
} |
|
108 |
||
109 |
if (endpos) { |
|
110 |
GString *input = cb->input; |
|
111 |
gsize bread = 0; |
|
112 |
gsize written = 0; |
|
113 |
gchar *utf8 = NULL; |
|
114 |
||
115 |
g_string_append_len (input, buf, endpos); |
|
116 |
||
11
fafc922963f5
Use xmpp_is_online if available
Myhailo Danylenko <isbear@ukrpost.net>
parents:
8
diff
changeset
|
117 |
if (!xmpp_is_online ()) { |
0 | 118 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Connection is not ready, delaying data"); |
119 |
return TRUE; |
|
120 |
} |
|
121 |
||
122 |
// usual g_locale_to_utf8 seem to be unable to detect locale charset |
|
123 |
// maybe, proper solution will be to call setlocale on module loading, |
|
124 |
// but mcabber already does this, and I do not want to mess with it |
|
5 | 125 |
utf8 = g_convert (input->str, input->len, LocaleCharSet, "UTF-8", &bread, &written, &error); |
0 | 126 |
|
5 | 127 |
if (error && error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE && bread) { |
0 | 128 |
written = 0; |
5 | 129 |
g_clear_error (&error); |
130 |
utf8 = g_convert (input->str, bread, LocaleCharSet, "UTF-8", &bread, &written, &error); |
|
0 | 131 |
} |
132 |
||
133 |
if (written) { |
|
134 |
gsize sent = 0; |
|
135 |
||
136 |
while (sent < written) { |
|
137 |
gsize len = 0; |
|
138 |
gchar *bbuf = NULL; |
|
5 | 139 |
gchar *subject = NULL; |
140 |
gboolean ret; |
|
0 | 141 |
|
142 |
if (written - sent > HBB_BLOCKSIZE) { |
|
143 |
gchar *c = utf8 + sent + HBB_BLOCKSIZE; |
|
144 |
c = g_utf8_find_prev_char (utf8 + sent, c); |
|
145 |
||
146 |
if (!c) { |
|
147 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Cannot determine utf8 character end! End of data chunk will be discarded!"); |
|
148 |
break; |
|
149 |
} |
|
150 |
||
151 |
len = c - utf8 - sent; |
|
152 |
bbuf = g_strndup (utf8 + sent, len); |
|
153 |
} |
|
154 |
||
5 | 155 |
cb -> seq += 1; |
0 | 156 |
|
5 | 157 |
if (cb -> subject) |
158 |
subject = g_strdup_printf (cb -> subject, cb -> seq); |
|
0 | 159 |
|
5 | 160 |
if (!cmd_send_msg (cb->jid, subject, len ? bbuf : (utf8 + sent))) |
0 | 161 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Encryption error. Message not sent."); |
162 |
||
5 | 163 |
if (subject) |
164 |
g_free (subject); |
|
0 | 165 |
|
166 |
if (!len) |
|
167 |
break; |
|
168 |
||
169 |
g_free (bbuf); |
|
170 |
||
171 |
sent += len; |
|
172 |
} |
|
173 |
||
174 |
g_free (utf8); |
|
175 |
||
176 |
g_string_erase (input, 0, bread); |
|
177 |
||
178 |
} else { |
|
179 |
||
5 | 180 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Character conversion error: %s", error->message); |
181 |
g_error_free (error); |
|
0 | 182 |
cb->source = 0; |
183 |
return FALSE; |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
} else if (condition & (G_IO_ERR|G_IO_NVAL|G_IO_HUP)) { |
|
188 |
cb->source = 0; |
|
189 |
return FALSE; // XXX |
|
190 |
} |
|
191 |
||
192 |
return TRUE; |
|
193 |
} |
|
194 |
||
195 |
static void cmd_destroy_data (gpointer data) |
|
196 |
{ |
|
197 |
cmd_cb_t *cb = (cmd_cb_t *) data; |
|
198 |
||
199 |
cmd_channels = g_slist_remove (cmd_channels, data); |
|
200 |
||
5 | 201 |
if (cb->source) |
202 |
g_source_remove (cb->source); |
|
0 | 203 |
if (cb->channel) |
204 |
g_io_channel_unref (cb->channel); |
|
5 | 205 |
if (cb -> subject) |
206 |
g_free (cb -> subject); |
|
0 | 207 |
g_free (cb->jid); |
208 |
g_free (cb); |
|
209 |
} |
|
210 |
||
211 |
static void do_cmd (char *arg) |
|
212 |
{ |
|
213 |
int fd[2]; |
|
214 |
const char *jid = CURRENT_JID; |
|
215 |
||
216 |
if (!*arg) |
|
217 |
return; |
|
218 |
||
219 |
if (!jid) { |
|
5 | 220 |
scr_LogPrint (LPRINT_LOGNORM, "Unsuitable buddy selected."); |
0 | 221 |
return; |
222 |
} |
|
223 |
||
224 |
if (pipe (fd)) { |
|
5 | 225 |
scr_LogPrint (LPRINT_LOGNORM, "Cannot create pipe: %s.", strerror (errno)); |
0 | 226 |
return; |
227 |
} |
|
228 |
||
229 |
{ |
|
5 | 230 |
GIOChannel *channel; |
231 |
int res = fork (); |
|
0 | 232 |
|
233 |
if (!res) { |
|
234 |
||
235 |
close (fd[0]); |
|
236 |
dup2 (fd[1], STDOUT_FILENO); |
|
237 |
if (settings_opt_get_int ("cmd_redirect_stderr")) |
|
238 |
dup2 (fd[1], STDERR_FILENO); |
|
239 |
else |
|
240 |
close (STDERR_FILENO); |
|
241 |
close (STDIN_FILENO); |
|
242 |
close (fd[1]); |
|
243 |
||
244 |
{ |
|
245 |
const char *shell = settings_opt_get ("cmd_shell"); |
|
246 |
if (!shell) |
|
247 |
shell = getenv ("SHELL"); |
|
248 |
if (!shell) |
|
249 |
shell = "sh"; |
|
250 |
execl (shell, shell, "-c", arg, NULL); |
|
251 |
} |
|
252 |
} |
|
253 |
||
254 |
if (res == -1) { |
|
5 | 255 |
scr_LogPrint (LPRINT_NORMAL, "Cannot fork child: %s.", strerror (errno)); |
0 | 256 |
close (fd[0]); |
257 |
close (fd[1]); |
|
258 |
return; |
|
259 |
} |
|
260 |
||
261 |
close (fd[1]); |
|
262 |
||
5 | 263 |
{ |
264 |
GError *error = NULL; |
|
265 |
||
266 |
channel = g_io_channel_unix_new (fd[0]); |
|
0 | 267 |
|
5 | 268 |
g_io_channel_set_encoding (channel, NULL, &error); |
269 |
if (error) { |
|
270 |
scr_LogPrint (LPRINT_DEBUG, "cmd: Cannot unset channel encoding: %s.", error -> message); |
|
271 |
g_clear_error (&error); |
|
272 |
} |
|
273 |
g_io_channel_set_buffered (channel, FALSE); |
|
274 |
g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, &error); |
|
275 |
if (error) { |
|
276 |
scr_LogPrint (LPRINT_DEBUG, "cmd: Cannot set nonblocking flag on channel: %s.", error -> message); |
|
277 |
g_error_free (error); |
|
278 |
} |
|
279 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
280 |
} |
|
0 | 281 |
|
282 |
{ |
|
283 |
cmd_cb_t *cb = g_new (cmd_cb_t, 1); |
|
284 |
||
5 | 285 |
if (settings_opt_get_int ("cmd_header")) { |
7 | 286 |
if (settings_opt_get_int ("cmd_header_inline") || is_room (jid)) { |
5 | 287 |
gchar *mesg = g_strdup_printf ("$ %s", arg); |
288 |
gchar *utf = to_utf8 (mesg); |
|
289 |
g_free (mesg); |
|
290 |
cmd_send_msg (jid, NULL, utf); |
|
291 |
g_free (utf); |
|
292 |
cb -> subject = NULL; |
|
293 |
} else { |
|
294 |
gchar *header = g_strdup_printf ("[%%02d] $ %s", arg); |
|
295 |
cb -> subject = to_utf8 (header); |
|
296 |
g_free (header); |
|
297 |
} |
|
298 |
} |
|
299 |
||
300 |
cb -> jid = g_strdup (jid); |
|
301 |
cb -> input = g_string_new (NULL); |
|
302 |
cb -> seq = 0; |
|
303 |
cb -> channel = channel; |
|
304 |
cb -> source = g_io_add_watch_full (channel, 0, G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL, |
|
305 |
cmd_reader, (gpointer) cb, cmd_destroy_data); |
|
0 | 306 |
|
307 |
cmd_channels = g_slist_append (cmd_channels, cb); |
|
308 |
} |
|
309 |
} |
|
310 |
} |
|
311 |
||
15
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
312 |
static void mcmd_init (void) |
0 | 313 |
{ |
314 |
cmd_add ("cmd", "", 0, 0, do_cmd, NULL); |
|
315 |
} |
|
316 |
||
15
891fc0779d50
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
317 |
static void mcmd_uninit (void) |
0 | 318 |
{ |
319 |
GSList *sel; |
|
320 |
||
321 |
cmd_del ("cmd"); |
|
322 |
||
323 |
for (sel = cmd_channels; sel; sel = sel->next) { |
|
324 |
cmd_cb_t *cb = (cmd_cb_t *) sel->data; |
|
325 |
if (cb->source) |
|
326 |
g_source_remove (cb->source); |
|
327 |
if (cb->channel) |
|
328 |
g_io_channel_unref (cb->channel); |
|
329 |
g_free (cb->jid); |
|
330 |
g_free (cb); |
|
331 |
} |
|
332 |
||
333 |
g_slist_free (cmd_channels); |
|
334 |
} |
|
335 |
||
336 |
/* vim: se ts=4 sw=4: */ |