|
1 |
|
2 function disco_items ( callback, what, node ) |
|
3 local request = |
|
4 lm.message.create { to = what, mtype = 'iq-get', |
|
5 query = { xmlns = 'http://jabber.org/protocol/disco#items' } |
|
6 } |
|
7 if node then |
|
8 request:child( 'query' ):attribute ( 'node', node ) |
|
9 end |
|
10 return lm.connection.bless( main.connection () ):send ( request, |
|
11 function ( conn, message ) |
|
12 if message:child ( 'error' ) then |
|
13 callback ( message:child( 'error' ):children():name () ) |
|
14 else |
|
15 local item = message:child( 'query' ):children () |
|
16 local items = { } |
|
17 while item do |
|
18 if item:name () == 'item' then |
|
19 table.insert ( items, { jid = item:attribute ( 'jid' ), node = item:attribute ( 'node' ), name = item:attribute ( 'name' ) } ) |
|
20 end |
|
21 item = item:next () |
|
22 end |
|
23 callback ( items ) |
|
24 end |
|
25 return true |
|
26 end ) |
|
27 end |
|
28 |
|
29 function disco_info ( callback, what ) |
|
30 return lm.connection.bless( main.connection () ):send ( |
|
31 lm.message.create { to = what, mtype = 'iq-get', |
|
32 query = { xmlns='http://jabber.org/protocol/disco#info' } |
|
33 }, |
|
34 function ( conn, message ) |
|
35 local items_supported = false |
|
36 if message:child ( 'error' ) then |
|
37 callback ( message:child( 'error' ):children():name () ) |
|
38 else |
|
39 local item = message:child( 'query' ):children () |
|
40 local identities = { } |
|
41 local features = { } |
|
42 while item do |
|
43 if item:name () == 'identity' then |
|
44 table.insert ( identities, { category = item:attribute ( 'category' ), type = item:attribute ( 'type' ), name = item:attribute ( 'name' ) } ) |
|
45 elseif item:name () == 'feature' then |
|
46 table.insert ( features, item:attribute ( 'var' ) ) |
|
47 end |
|
48 item = item:next () |
|
49 end |
|
50 callback ( identities, features ) |
|
51 end |
|
52 return true |
|
53 end ) |
|
54 end |
|
55 |
|
56 main.add_command ( 'disco', |
|
57 function ( args ) |
|
58 args = parse_args ( args ) |
|
59 local who |
|
60 if args.t then |
|
61 who = args.t |
|
62 args.t = nil |
|
63 else |
|
64 who = full_current_jid () |
|
65 end |
|
66 if args[1] == 'items' then |
|
67 local node = nil |
|
68 if args[2] then |
|
69 args[1] = nil |
|
70 node = rebuild_args_string ( args ) |
|
71 end |
|
72 disco_items ( |
|
73 function ( items ) |
|
74 if type ( items ) == 'string' then |
|
75 main.print_info ( who, string.format ( "Service items discovery for %s (%s) failed: %s", who, node or '', items ) ) |
|
76 else |
|
77 main.print_info ( who, string.format ( "Service items discovery result for %s (%s):", who, node or '' ) ) |
|
78 for index, item in ipairs ( items ) do |
|
79 main.print_info ( who, string.format ( " [%s (%s)] %s", item.jid or '', item.node or '', item.name or '' ) ) |
|
80 end |
|
81 end |
|
82 end, who, node ) |
|
83 else |
|
84 disco_info ( |
|
85 function ( identities, features ) |
|
86 if type ( identities ) == 'string' then |
|
87 main.print_info ( who, string.format ( "Service info discovery for %s failed: %s", who, identities ) ) |
|
88 else |
|
89 main.print_info ( who, string.format ( "Service info discovery result for %s:", who ) ) |
|
90 main.print_info ( who, " Identities:" ) |
|
91 for index, identity in ipairs ( identities ) do |
|
92 main.print_info ( who, string.format ( " [%s (%s)] %s", identity.category or '', identity.type or '', identity.name or '' ) ) |
|
93 end |
|
94 main.print_info ( who, " Features:" ) |
|
95 for index, feature in ipairs ( features ) do |
|
96 main.print_info ( who, string.format ( " [%s]", feature or '' ) ) |
|
97 end |
|
98 end |
|
99 end, who ) |
|
100 end |
|
101 end ) |
|
102 |
|
103 commands_help['disco'] = "[-t target_jid] [info | items] [node]\n\nService discovery request.\nInfo is sent if omitted.\nIf info reveals, that buddy can do items, items request also will be sent." |
|
104 |
|
105 -- vim: se ts=4: -- |