author | Myhailo Danylenko <isbear@ukrpost.net> |
Wed, 02 Dec 2009 21:05:12 +0200 | |
changeset 4 | e305d7e562c4 |
parent 0 | 72ffcc3c584e |
child 5 | 1b3263c96cbe |
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 |
||
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; |
|
39 |
guint source; |
|
40 |
GIOChannel *channel; |
|
41 |
} cmd_cb_t; |
|
42 |
||
43 |
static GSList *cmd_channels = NULL; |
|
44 |
||
45 |
static gboolean cmd_reader (GIOChannel *channel, GIOCondition condition, gpointer data) |
|
46 |
{ |
|
47 |
cmd_cb_t *cb = (cmd_cb_t *) data; |
|
48 |
||
49 |
if (condition & (G_IO_IN|G_IO_PRI)) { |
|
50 |
GIOStatus chstat; |
|
51 |
static gchar buf[HBB_BLOCKSIZE]; |
|
52 |
gsize endpos; |
|
53 |
||
54 |
chstat = g_io_channel_read_chars (channel, buf, HBB_BLOCKSIZE, &endpos, NULL); |
|
55 |
||
56 |
if (chstat == G_IO_STATUS_ERROR || chstat == G_IO_STATUS_EOF) { |
|
57 |
cb->source = 0; |
|
58 |
return FALSE; // XXX |
|
59 |
} |
|
60 |
||
61 |
if (endpos) { |
|
62 |
GString *input = cb->input; |
|
63 |
gsize bread = 0; |
|
64 |
gsize written = 0; |
|
65 |
GError *err = NULL; |
|
66 |
gchar *utf8 = NULL; |
|
67 |
||
68 |
g_string_append_len (input, buf, endpos); |
|
69 |
||
70 |
if (!lm_connection_is_authenticated (lconnection)) { |
|
71 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Connection is not ready, delaying data"); |
|
72 |
return TRUE; |
|
73 |
} |
|
74 |
||
75 |
// usual g_locale_to_utf8 seem to be unable to detect locale charset |
|
76 |
// maybe, proper solution will be to call setlocale on module loading, |
|
77 |
// but mcabber already does this, and I do not want to mess with it |
|
78 |
utf8 = g_convert (input->str, input->len, LocaleCharSet, "UTF-8", &bread, &written, &err); |
|
79 |
||
80 |
if (err && err->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE && bread) { |
|
81 |
err = NULL; |
|
82 |
written = 0; |
|
83 |
utf8 = g_convert (input->str, bread, LocaleCharSet, "UTF-8", &bread, &written, &err); |
|
84 |
} |
|
85 |
||
86 |
if (written) { |
|
87 |
gsize sent = 0; |
|
88 |
||
89 |
while (sent < written) { |
|
90 |
gint crypted; |
|
91 |
gpointer xep184 = NULL; |
|
92 |
gsize len = 0; |
|
93 |
gchar *bbuf = NULL; |
|
4
e305d7e562c4
Fix sending to MUC rooms
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
94 |
int type = roster_find (cb->jid, jidsearch, ROSTER_TYPE_ROOM) ? ROSTER_TYPE_ROOM : ROSTER_TYPE_USER; |
0 | 95 |
|
96 |
if (written - sent > HBB_BLOCKSIZE) { |
|
97 |
gchar *c = utf8 + sent + HBB_BLOCKSIZE; |
|
98 |
c = g_utf8_find_prev_char (utf8 + sent, c); |
|
99 |
||
100 |
if (!c) { |
|
101 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Cannot determine utf8 character end! End of data chunk will be discarded!"); |
|
102 |
break; |
|
103 |
} |
|
104 |
||
105 |
len = c - utf8 - sent; |
|
106 |
bbuf = g_strndup (utf8 + sent, len); |
|
107 |
} |
|
108 |
||
109 |
// XXX add command/sequence number as title? |
|
4
e305d7e562c4
Fix sending to MUC rooms
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
110 |
xmpp_send_msg (cb->jid, len ? bbuf : (utf8 + sent), type, NULL, FALSE /* ? */, &crypted, LM_MESSAGE_SUB_TYPE_NOT_SET, &xep184); |
0 | 111 |
|
112 |
if (crypted == -1) { |
|
113 |
||
114 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Encryption error. Message not sent."); |
|
115 |
||
116 |
if (!len) |
|
117 |
break; |
|
118 |
||
119 |
g_free (bbuf); |
|
120 |
||
121 |
continue; |
|
122 |
} |
|
123 |
||
4
e305d7e562c4
Fix sending to MUC rooms
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
124 |
if (type != ROSTER_TYPE_ROOM) |
e305d7e562c4
Fix sending to MUC rooms
Myhailo Danylenko <isbear@ukrpost.net>
parents:
0
diff
changeset
|
125 |
hk_message_out (cb->jid, NULL, 0, len ? bbuf : (utf8 + sent), crypted, xep184); |
0 | 126 |
|
127 |
if (!len) |
|
128 |
break; |
|
129 |
||
130 |
g_free (bbuf); |
|
131 |
||
132 |
sent += len; |
|
133 |
} |
|
134 |
||
135 |
g_free (utf8); |
|
136 |
||
137 |
g_string_erase (input, 0, bread); |
|
138 |
||
139 |
} else { |
|
140 |
||
141 |
scr_LogPrint (LPRINT_LOGNORM, "cmd: Character conversion error: %s", err->message); |
|
142 |
cb->source = 0; |
|
143 |
return FALSE; |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
} else if (condition & (G_IO_ERR|G_IO_NVAL|G_IO_HUP)) { |
|
148 |
cb->source = 0; |
|
149 |
return FALSE; // XXX |
|
150 |
} |
|
151 |
||
152 |
return TRUE; |
|
153 |
} |
|
154 |
||
155 |
static void cmd_destroy_data (gpointer data) |
|
156 |
{ |
|
157 |
cmd_cb_t *cb = (cmd_cb_t *) data; |
|
158 |
||
159 |
cmd_channels = g_slist_remove (cmd_channels, data); |
|
160 |
||
161 |
// May conflict - will be called during source removal? |
|
162 |
// if (cb->source) |
|
163 |
// g_source_remove (cb->source); |
|
164 |
if (cb->channel) |
|
165 |
g_io_channel_unref (cb->channel); |
|
166 |
g_free (cb->jid); |
|
167 |
g_free (cb); |
|
168 |
} |
|
169 |
||
170 |
static void do_cmd (char *arg) |
|
171 |
{ |
|
172 |
int fd[2]; |
|
173 |
const char *jid = CURRENT_JID; |
|
174 |
||
175 |
if (!*arg) |
|
176 |
return; |
|
177 |
||
178 |
if (!jid) { |
|
179 |
scr_LogPrint (LPRINT_LOGNORM, "Unsuitable buddy selected"); |
|
180 |
return; |
|
181 |
} |
|
182 |
||
183 |
if (pipe (fd)) { |
|
184 |
scr_LogPrint (LPRINT_LOGNORM, "Cannot create pipe: %s", strerror (errno)); |
|
185 |
return; |
|
186 |
} |
|
187 |
||
188 |
{ |
|
189 |
int res = fork (); |
|
190 |
||
191 |
if (!res) { |
|
192 |
||
193 |
close (fd[0]); |
|
194 |
dup2 (fd[1], STDOUT_FILENO); |
|
195 |
if (settings_opt_get_int ("cmd_redirect_stderr")) |
|
196 |
dup2 (fd[1], STDERR_FILENO); |
|
197 |
else |
|
198 |
close (STDERR_FILENO); |
|
199 |
close (STDIN_FILENO); |
|
200 |
close (fd[1]); |
|
201 |
||
202 |
{ |
|
203 |
const char *shell = settings_opt_get ("cmd_shell"); |
|
204 |
if (!shell) |
|
205 |
shell = getenv ("SHELL"); |
|
206 |
if (!shell) |
|
207 |
shell = "sh"; |
|
208 |
execl (shell, shell, "-c", arg, NULL); |
|
209 |
} |
|
210 |
} |
|
211 |
||
212 |
if (res == -1) { |
|
213 |
scr_LogPrint (LPRINT_NORMAL, "Cannot fork child: %s", strerror (errno)); |
|
214 |
close (fd[0]); |
|
215 |
close (fd[1]); |
|
216 |
return; |
|
217 |
} |
|
218 |
||
219 |
close (fd[1]); |
|
220 |
||
221 |
GIOChannel *channel = g_io_channel_unix_new (fd[0]); |
|
222 |
||
223 |
g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL); |
|
224 |
g_io_channel_set_encoding (channel, NULL, NULL); |
|
225 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
226 |
g_io_channel_set_buffered (channel, FALSE); |
|
227 |
||
228 |
{ |
|
229 |
cmd_cb_t *cb = g_new (cmd_cb_t, 1); |
|
230 |
||
231 |
cb->jid = g_strdup (jid); |
|
232 |
cb->input = g_string_new (NULL); |
|
233 |
cb->channel = channel; |
|
234 |
cb->source = g_io_add_watch_full (channel, 0, G_IO_IN|G_IO_PRI|G_IO_ERR|G_IO_HUP|G_IO_NVAL, |
|
235 |
cmd_reader, (gpointer) cb, cmd_destroy_data); |
|
236 |
||
237 |
cmd_channels = g_slist_append (cmd_channels, cb); |
|
238 |
} |
|
239 |
} |
|
240 |
} |
|
241 |
||
242 |
const gchar *g_module_check_init(GModule *module) |
|
243 |
{ |
|
244 |
cmd_add ("cmd", "", 0, 0, do_cmd, NULL); |
|
245 |
||
246 |
return NULL; |
|
247 |
} |
|
248 |
||
249 |
void g_module_unload(GModule *module) |
|
250 |
{ |
|
251 |
GSList *sel; |
|
252 |
||
253 |
cmd_del ("cmd"); |
|
254 |
||
255 |
for (sel = cmd_channels; sel; sel = sel->next) { |
|
256 |
cmd_cb_t *cb = (cmd_cb_t *) sel->data; |
|
257 |
if (cb->source) |
|
258 |
g_source_remove (cb->source); |
|
259 |
if (cb->channel) |
|
260 |
g_io_channel_unref (cb->channel); |
|
261 |
g_free (cb->jid); |
|
262 |
g_free (cb); |
|
263 |
} |
|
264 |
||
265 |
g_slist_free (cmd_channels); |
|
266 |
} |
|
267 |
||
268 |
/* vim: se ts=4 sw=4: */ |