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