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