0
|
1 |
/* |
|
2 |
* disco.c -- Service discovery requests |
|
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 <glib.h> |
|
23 |
#include <gmodule.h> |
|
24 |
#include <loudmouth/loudmouth.h> |
|
25 |
#include <strings.h> |
|
26 |
|
|
27 |
#include "commands.h" |
|
28 |
#include "logprint.h" |
|
29 |
#include "utils.h" |
|
30 |
#include "xmpp.h" |
|
31 |
#include "compl.h" |
1
|
32 |
#include "xmpp_defines.h" |
|
33 |
#include "screen.h" |
|
34 |
#include "hbuf.h" |
0
|
35 |
|
3
|
36 |
static GSList *disco_handlers = NULL; |
|
37 |
static guint disco_cid = 0; |
|
38 |
|
1
|
39 |
static LmHandlerResult disco_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer udata) |
0
|
40 |
{ |
1
|
41 |
int info_request = (int) udata; |
|
42 |
|
3
|
43 |
disco_handlers = g_slist_remove (disco_handlers, handler); |
|
44 |
|
0
|
45 |
switch (lm_message_get_sub_type (message)) { |
|
46 |
case LM_MESSAGE_SUB_TYPE_RESULT: |
|
47 |
|
|
48 |
{ |
1
|
49 |
LmMessageNode *node = lm_message_get_node (message); |
|
50 |
const gchar *from = lm_message_node_get_attribute (node, "from"); |
|
51 |
GString *info; |
|
52 |
|
0
|
53 |
node = lm_message_node_get_child (node, "query"); |
1
|
54 |
|
|
55 |
// check xmlns |
|
56 |
if (!node || strcmp (lm_message_node_get_attribute (node, "xmlns"), info_request ? NS_DISCO_INFO : NS_DISCO_ITEMS)) |
0
|
57 |
break; |
1
|
58 |
|
|
59 |
{ // header for user message |
|
60 |
const gchar *rnode = lm_message_node_get_attribute (node, "node"); |
|
61 |
|
|
62 |
// create user message string |
|
63 |
info = g_string_new (NULL); |
|
64 |
g_string_printf (info, "Service discovery %s results for %s", info_request ? "info" : "items", from); |
|
65 |
if (rnode) |
|
66 |
g_string_append_printf (info, " (%s):", rnode); |
|
67 |
else |
|
68 |
g_string_append (info, ":"); |
|
69 |
} |
|
70 |
|
|
71 |
if (node->children) { |
|
72 |
// parse request results |
|
73 |
if (info_request) { // info |
|
74 |
GString *identities = g_string_new (NULL); |
|
75 |
GString *features = g_string_new (NULL); |
|
76 |
|
|
77 |
for (node = node->children; node; node = node->next) { |
|
78 |
if (!strcasecmp (node->name, "identity")) { |
|
79 |
const gchar *category = lm_message_node_get_attribute (node, "category"); |
|
80 |
const gchar *type = lm_message_node_get_attribute (node, "type"); |
|
81 |
const gchar *name = lm_message_node_get_attribute (node, "name"); |
|
82 |
|
|
83 |
g_string_append_printf (identities, "\n [%s (%s)] %s", category ? category : "none", type ? type : "none", name ? name : ""); |
|
84 |
|
|
85 |
} else if (!strcasecmp (node->name, "feature")) { |
|
86 |
const gchar *var = lm_message_node_get_attribute (node, "var"); |
|
87 |
|
|
88 |
g_string_append_printf (features, "\n [%s]", var ? var : "none"); |
|
89 |
} |
|
90 |
} |
|
91 |
|
3
|
92 |
if (identities->len) |
1
|
93 |
g_string_append_printf (info, "\n Identities:%s", identities->str); |
3
|
94 |
if (features->len) |
1
|
95 |
g_string_append_printf (info, "\n Features:%s", features->str); |
|
96 |
|
|
97 |
g_string_free (identities, TRUE); |
|
98 |
g_string_free (features, TRUE); |
|
99 |
|
|
100 |
} else { // items |
|
101 |
for (node = node->children; node; node = node->next) { |
|
102 |
const gchar *name = lm_message_node_get_attribute (node, "name"); |
|
103 |
const gchar *jid = lm_message_node_get_attribute (node, "jid"); |
|
104 |
const gchar *inode = lm_message_node_get_attribute (node, "node"); |
|
105 |
|
|
106 |
if (inode) |
|
107 |
g_string_append_printf (info, "\n [%s (%s)] %s", jid ? jid : "none", inode, name ? name : ""); |
|
108 |
else |
|
109 |
g_string_append_printf (info, "\n [%s] %s", jid ? jid : "none", name ? name : ""); |
|
110 |
} |
0
|
111 |
} |
1
|
112 |
} else |
|
113 |
g_string_append (info, "\n Empty result."); |
|
114 |
|
|
115 |
{ // print to buddy's buffer |
|
116 |
gchar *jid = jidtodisp (from); |
|
117 |
|
|
118 |
// XXX check for message size? conference server lists may be huge... |
|
119 |
scr_WriteIncomingMessage (jid, info->str, 0, HBB_PREFIX_INFO, 0); // NO conversion from utf-8 |
|
120 |
|
|
121 |
g_free (jid); |
0
|
122 |
} |
1
|
123 |
|
|
124 |
g_string_free (info, TRUE); |
0
|
125 |
} |
|
126 |
|
|
127 |
break; |
|
128 |
|
|
129 |
case LM_MESSAGE_SUB_TYPE_ERROR: |
|
130 |
|
|
131 |
{ |
1
|
132 |
LmMessageNode *node = lm_message_get_node (message); |
|
133 |
const gchar *from = lm_message_node_get_attribute (node, "from"); |
|
134 |
const gchar *type; |
|
135 |
const gchar *reason; |
0
|
136 |
|
|
137 |
node = lm_message_node_get_child (node, "error"); |
|
138 |
type = lm_message_node_get_attribute (node, "type"); |
|
139 |
if (node->children) |
|
140 |
reason = node->children->name; |
|
141 |
else |
|
142 |
reason = "undefined"; |
|
143 |
|
1
|
144 |
{ // print to buddy's buffer |
|
145 |
gchar *jid = jidtodisp (from); |
|
146 |
gchar *mesg = g_strdup_printf ("Service %s discovery for %s failed: %s - %s", info_request ? "info" : "items", from, type, reason); |
0
|
147 |
|
1
|
148 |
scr_WriteIncomingMessage (jid, mesg, 0, HBB_PREFIX_INFO, 0); |
0
|
149 |
|
1
|
150 |
g_free (mesg); |
|
151 |
g_free (jid); |
|
152 |
} |
0
|
153 |
} |
|
154 |
|
|
155 |
break; |
|
156 |
|
|
157 |
default: |
|
158 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
159 |
break; |
|
160 |
} |
|
161 |
|
|
162 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
163 |
} |
|
164 |
|
|
165 |
static void do_disco (char *arg) |
|
166 |
{ |
|
167 |
char **args = split_arg (arg, 3, 0); |
|
168 |
int info = -1; |
|
169 |
|
1
|
170 |
if (!args[0] || !strcmp (args[0], "info")) |
0
|
171 |
info = 1; |
|
172 |
else if (!strcmp (args[0], "items")) |
|
173 |
info = 0; |
|
174 |
else |
|
175 |
scr_LogPrint (LPRINT_NORMAL, "Unknown subcomand."); |
|
176 |
|
|
177 |
if (info != -1) { |
|
178 |
LmMessageHandler *handler; |
|
179 |
LmMessage *request; |
1
|
180 |
char *to = NULL; |
0
|
181 |
char *dnode = NULL; |
|
182 |
|
1
|
183 |
if (args[0] && args[1]) { |
|
184 |
if (*args[1] == '.') { |
|
185 |
if (*(args[1] + 1) == JID_RESOURCE_SEPARATOR) { // allow "./resource" notation |
|
186 |
char *rest = to_utf8 (args[1] + 1); |
|
187 |
|
|
188 |
to = g_strdup_printf ("%s%s", CURRENT_JID, rest); |
|
189 |
g_free (rest); |
|
190 |
} |
|
191 |
} else |
|
192 |
to = to_utf8 (args[1]); |
0
|
193 |
|
|
194 |
if (args[2]) |
1
|
195 |
dnode = to_utf8 (args[2]); |
|
196 |
} |
|
197 |
// XXX send to all resources/current resource? |
|
198 |
|
|
199 |
handler = lm_message_handler_new (disco_handler, (gpointer) info, NULL); |
|
200 |
|
|
201 |
{ // create message |
|
202 |
LmMessageNode *node; |
|
203 |
|
|
204 |
request = lm_message_new_with_sub_type (to ? to : CURRENT_JID, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET); |
|
205 |
node = lm_message_get_node (request); |
|
206 |
node = lm_message_node_add_child (node, "query", NULL); |
|
207 |
lm_message_node_set_attribute (node, "xmlns", info ? NS_DISCO_INFO : NS_DISCO_ITEMS); |
|
208 |
if (dnode) |
|
209 |
lm_message_node_set_attribute (node, "node", dnode); |
0
|
210 |
} |
|
211 |
|
1
|
212 |
lm_connection_send_with_reply (lconnection, request, handler, NULL); |
0
|
213 |
|
3
|
214 |
lm_message_handler_unref (handler); |
|
215 |
lm_message_unref (request); |
|
216 |
g_free (dnode); |
1
|
217 |
g_free (to); |
0
|
218 |
} |
|
219 |
} |
|
220 |
|
|
221 |
const gchar *g_module_check_init(GModule *module) |
|
222 |
{ |
3
|
223 |
disco_handlers = NULL; |
1
|
224 |
|
3
|
225 |
// completion |
|
226 |
disco_cid = compl_new_category (); |
|
227 |
if (disco_cid) { |
|
228 |
compl_add_category_word (disco_cid, "info"); |
|
229 |
compl_add_category_word (disco_cid, "items"); |
|
230 |
} |
|
231 |
|
|
232 |
// command |
|
233 |
cmd_add ("disco", "", disco_cid, COMPL_JID, do_disco, NULL); |
|
234 |
|
0
|
235 |
return NULL; |
|
236 |
} |
|
237 |
|
|
238 |
void g_module_unload(GModule *module) |
|
239 |
{ |
3
|
240 |
GSList *hel; |
|
241 |
|
|
242 |
// let's hope, that after invalidation, lm will remove and free unreffed by us handler |
|
243 |
for (hel = disco_handlers; hel; hel = hel->next) { |
|
244 |
LmMessageHandler *handler = (LmMessageHandler *) hel->data; |
|
245 |
lm_message_handler_invalidate (handler); |
|
246 |
} |
|
247 |
|
|
248 |
// free list |
|
249 |
g_slist_free (disco_handlers); |
|
250 |
disco_handlers = NULL; |
|
251 |
|
|
252 |
// command |
0
|
253 |
cmd_del ("disco"); |
3
|
254 |
|
|
255 |
// completion |
|
256 |
if (disco_cid) |
|
257 |
compl_del_category (disco_cid); |
0
|
258 |
} |
|
259 |
|
1
|
260 |
/* vim: se ts=4: */ |