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