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 |
|
1
|
36 |
static LmHandlerResult disco_handler (LmMessageHandler *handler, LmConnection *connection, LmMessage *message, gpointer udata) |
0
|
37 |
{ |
1
|
38 |
int info_request = (int) udata; |
|
39 |
|
0
|
40 |
switch (lm_message_get_sub_type (message)) { |
|
41 |
case LM_MESSAGE_SUB_TYPE_RESULT: |
|
42 |
|
|
43 |
{ |
1
|
44 |
LmMessageNode *node = lm_message_get_node (message); |
|
45 |
const gchar *from = lm_message_node_get_attribute (node, "from"); |
|
46 |
GString *info; |
|
47 |
|
0
|
48 |
node = lm_message_node_get_child (node, "query"); |
1
|
49 |
|
|
50 |
// check xmlns |
|
51 |
if (!node || strcmp (lm_message_node_get_attribute (node, "xmlns"), info_request ? NS_DISCO_INFO : NS_DISCO_ITEMS)) |
0
|
52 |
break; |
1
|
53 |
|
|
54 |
{ // header for user message |
|
55 |
const gchar *rnode = lm_message_node_get_attribute (node, "node"); |
|
56 |
|
|
57 |
// create user message string |
|
58 |
info = g_string_new (NULL); |
|
59 |
g_string_printf (info, "Service discovery %s results for %s", info_request ? "info" : "items", from); |
|
60 |
if (rnode) |
|
61 |
g_string_append_printf (info, " (%s):", rnode); |
|
62 |
else |
|
63 |
g_string_append (info, ":"); |
|
64 |
} |
|
65 |
|
|
66 |
if (node->children) { |
|
67 |
// parse request results |
|
68 |
if (info_request) { // info |
|
69 |
GString *identities = g_string_new (NULL); |
|
70 |
GString *features = g_string_new (NULL); |
|
71 |
|
|
72 |
for (node = node->children; node; node = node->next) { |
|
73 |
if (!strcasecmp (node->name, "identity")) { |
|
74 |
const gchar *category = lm_message_node_get_attribute (node, "category"); |
|
75 |
const gchar *type = lm_message_node_get_attribute (node, "type"); |
|
76 |
const gchar *name = lm_message_node_get_attribute (node, "name"); |
|
77 |
|
|
78 |
g_string_append_printf (identities, "\n [%s (%s)] %s", category ? category : "none", type ? type : "none", name ? name : ""); |
|
79 |
|
|
80 |
} else if (!strcasecmp (node->name, "feature")) { |
|
81 |
const gchar *var = lm_message_node_get_attribute (node, "var"); |
|
82 |
|
|
83 |
g_string_append_printf (features, "\n [%s]", var ? var : "none"); |
|
84 |
} |
|
85 |
} |
|
86 |
|
|
87 |
if (identities->str) |
|
88 |
g_string_append_printf (info, "\n Identities:%s", identities->str); |
|
89 |
if (features->str) |
|
90 |
g_string_append_printf (info, "\n Features:%s", features->str); |
|
91 |
|
|
92 |
g_string_free (identities, TRUE); |
|
93 |
g_string_free (features, TRUE); |
|
94 |
|
|
95 |
} else { // items |
|
96 |
for (node = node->children; node; node = node->next) { |
|
97 |
const gchar *name = lm_message_node_get_attribute (node, "name"); |
|
98 |
const gchar *jid = lm_message_node_get_attribute (node, "jid"); |
|
99 |
const gchar *inode = lm_message_node_get_attribute (node, "node"); |
|
100 |
|
|
101 |
if (inode) |
|
102 |
g_string_append_printf (info, "\n [%s (%s)] %s", jid ? jid : "none", inode, name ? name : ""); |
|
103 |
else |
|
104 |
g_string_append_printf (info, "\n [%s] %s", jid ? jid : "none", name ? name : ""); |
|
105 |
} |
0
|
106 |
} |
1
|
107 |
} else |
|
108 |
g_string_append (info, "\n Empty result."); |
|
109 |
|
|
110 |
{ // print to buddy's buffer |
|
111 |
gchar *jid = jidtodisp (from); |
|
112 |
|
|
113 |
// XXX check for message size? conference server lists may be huge... |
|
114 |
scr_WriteIncomingMessage (jid, info->str, 0, HBB_PREFIX_INFO, 0); // NO conversion from utf-8 |
|
115 |
|
|
116 |
g_free (jid); |
0
|
117 |
} |
1
|
118 |
|
|
119 |
g_string_free (info, TRUE); |
0
|
120 |
} |
|
121 |
|
|
122 |
break; |
|
123 |
|
|
124 |
case LM_MESSAGE_SUB_TYPE_ERROR: |
|
125 |
|
|
126 |
{ |
1
|
127 |
LmMessageNode *node = lm_message_get_node (message); |
|
128 |
const gchar *from = lm_message_node_get_attribute (node, "from"); |
|
129 |
const gchar *type; |
|
130 |
const gchar *reason; |
0
|
131 |
|
|
132 |
node = lm_message_node_get_child (node, "error"); |
|
133 |
type = lm_message_node_get_attribute (node, "type"); |
|
134 |
if (node->children) |
|
135 |
reason = node->children->name; |
|
136 |
else |
|
137 |
reason = "undefined"; |
|
138 |
|
1
|
139 |
{ // print to buddy's buffer |
|
140 |
gchar *jid = jidtodisp (from); |
|
141 |
gchar *mesg = g_strdup_printf ("Service %s discovery for %s failed: %s - %s", info_request ? "info" : "items", from, type, reason); |
0
|
142 |
|
1
|
143 |
scr_WriteIncomingMessage (jid, mesg, 0, HBB_PREFIX_INFO, 0); |
0
|
144 |
|
1
|
145 |
g_free (mesg); |
|
146 |
g_free (jid); |
|
147 |
} |
0
|
148 |
} |
|
149 |
|
|
150 |
break; |
|
151 |
|
|
152 |
default: |
|
153 |
return LM_HANDLER_RESULT_ALLOW_MORE_HANDLERS; |
|
154 |
break; |
|
155 |
} |
|
156 |
|
|
157 |
return LM_HANDLER_RESULT_REMOVE_MESSAGE; |
|
158 |
} |
|
159 |
|
|
160 |
static void do_disco (char *arg) |
|
161 |
{ |
|
162 |
char **args = split_arg (arg, 3, 0); |
|
163 |
int info = -1; |
|
164 |
|
1
|
165 |
if (!args[0] || !strcmp (args[0], "info")) |
0
|
166 |
info = 1; |
|
167 |
else if (!strcmp (args[0], "items")) |
|
168 |
info = 0; |
|
169 |
else |
|
170 |
scr_LogPrint (LPRINT_NORMAL, "Unknown subcomand."); |
|
171 |
|
|
172 |
if (info != -1) { |
|
173 |
LmMessageHandler *handler; |
|
174 |
LmMessage *request; |
1
|
175 |
char *to = NULL; |
0
|
176 |
char *dnode = NULL; |
|
177 |
|
1
|
178 |
if (args[0] && args[1]) { |
|
179 |
if (*args[1] == '.') { |
|
180 |
if (*(args[1] + 1) == JID_RESOURCE_SEPARATOR) { // allow "./resource" notation |
|
181 |
char *rest = to_utf8 (args[1] + 1); |
|
182 |
|
|
183 |
to = g_strdup_printf ("%s%s", CURRENT_JID, rest); |
|
184 |
g_free (rest); |
|
185 |
} |
|
186 |
} else |
|
187 |
to = to_utf8 (args[1]); |
0
|
188 |
|
|
189 |
if (args[2]) |
1
|
190 |
dnode = to_utf8 (args[2]); |
|
191 |
} |
|
192 |
// XXX send to all resources/current resource? |
|
193 |
|
|
194 |
handler = lm_message_handler_new (disco_handler, (gpointer) info, NULL); |
|
195 |
|
|
196 |
{ // create message |
|
197 |
LmMessageNode *node; |
|
198 |
|
|
199 |
request = lm_message_new_with_sub_type (to ? to : CURRENT_JID, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_GET); |
|
200 |
node = lm_message_get_node (request); |
|
201 |
node = lm_message_node_add_child (node, "query", NULL); |
|
202 |
lm_message_node_set_attribute (node, "xmlns", info ? NS_DISCO_INFO : NS_DISCO_ITEMS); |
|
203 |
if (dnode) |
|
204 |
lm_message_node_set_attribute (node, "node", dnode); |
0
|
205 |
} |
|
206 |
|
1
|
207 |
lm_connection_send_with_reply (lconnection, request, handler, NULL); |
0
|
208 |
|
1
|
209 |
g_free (to); |
|
210 |
g_free (dnode); |
0
|
211 |
} |
|
212 |
} |
|
213 |
|
|
214 |
const gchar *g_module_check_init(GModule *module) |
|
215 |
{ |
1
|
216 |
// TODO completion |
|
217 |
|
0
|
218 |
cmd_add ("disco", "", 0, COMPL_JID, do_disco, NULL); |
|
219 |
return NULL; |
|
220 |
} |
|
221 |
|
|
222 |
void g_module_unload(GModule *module) |
|
223 |
{ |
|
224 |
cmd_del ("disco"); |
|
225 |
} |
|
226 |
|
1
|
227 |
/* vim: se ts=4: */ |