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