|
1 |
|
2 -- SERVICE DISCOVERY (XEP-0030) |
|
3 |
|
4 -- library |
|
5 |
|
6 require 'lm' |
|
7 require 'iq' |
|
8 |
|
9 -- public |
|
10 |
|
11 disco = { } |
|
12 |
|
13 function disco.items ( conn, to, success, fail, node ) |
|
14 iq.send ( conn, to, 'get', |
|
15 { |
|
16 query = { xmlns = 'http://jabber.org/protocol/disco#items', node = node } |
|
17 }, |
|
18 function ( mess ) |
|
19 local item = mess:child( 'query' ):children () |
|
20 local items = { } |
|
21 while item do |
|
22 if item:name () == 'item' then |
|
23 table.insert ( items, { jid = item:attribute ( 'jid' ), node = item:attribute ( 'node' ), name = item:attribute ( 'name' ) } ) |
|
24 end |
|
25 item = item:next () |
|
26 end |
|
27 success ( items ) |
|
28 end, |
|
29 fail ) |
|
30 end |
|
31 |
|
32 function disco.info ( conn, to, success, fail ) |
|
33 iq.send ( conn, to, 'get', |
|
34 { |
|
35 query = { xmlns='http://jabber.org/protocol/disco#info' } |
|
36 }, |
|
37 function ( mess ) |
|
38 local identities = { } |
|
39 local features = { } |
|
40 local item = mess:child( 'query' ):children () |
|
41 while item do |
|
42 local name = item:name () |
|
43 if name == 'identity' then |
|
44 table.insert ( identities, { category = item:attribute ( 'category' ), type = item:attribute ( 'type' ), name = item:attribute ( 'name' ) } ) |
|
45 elseif name == 'feature' then |
|
46 table.insert ( features, item:attribute ( 'var' ) ) |
|
47 end |
|
48 item = item:next () |
|
49 end |
|
50 success ( identities, features ) |
|
51 end, |
|
52 fail ) |
|
53 end |
|
54 |
|
55 -- mcabber |
|
56 |
|
57 main.command ( 'disco', |
|
58 function ( args ) |
|
59 local who |
|
60 local conn = lm.connection.bless ( main.connection () ) |
|
61 if args.t then |
|
62 who = args.t |
|
63 else |
|
64 who = main.full_jid () |
|
65 end |
|
66 if args[1] == 'items' then |
|
67 local node = args[2] |
|
68 disco.items ( conn, who, |
|
69 function ( items ) |
|
70 local text = '' |
|
71 for index, item in ipairs ( items ) do |
|
72 text = text .. ("\n [%s (%s)] %s"):format ( item.jid or '', item.node or '', item.name or '' ) |
|
73 end |
|
74 if text ~= '' then |
|
75 main.print_info ( who, ("Items service discovery result for %s (%s):%s"):format ( who, node or '', text ) ) |
|
76 else |
|
77 main.print_info ( who, ("No items in discovery result for %s (%s)"):format ( who, node or '' ) ) |
|
78 end |
|
79 end, |
|
80 function ( mesg ) |
|
81 main.print_info ( who, ("Items service discovery for %s (%s) failed: %s"):format ( who, node or '', mesg ) ) |
|
82 end, node ) |
|
83 else |
|
84 disco.info ( conn, who, |
|
85 function ( identities, features ) |
|
86 main.print_info ( who, ("Service info discovery result for %s:"):format ( who ) ) |
|
87 local text = '' |
|
88 for index, identity in ipairs ( identities ) do |
|
89 text = text .. ("\n [%s (%s)] %s"):format ( identity.category or '', identity.type or '', identity.name or '' ) |
|
90 end |
|
91 if text ~= '' then |
|
92 main.print_info ( who, " Identities:" .. text ) |
|
93 else |
|
94 main.print_info ( who, " No identities" ) |
|
95 end |
|
96 text = '' |
|
97 for index, feature in ipairs ( features ) do |
|
98 text = text .. ("\n [%s]"):format ( feature or '' ) |
|
99 end |
|
100 if text ~= '' then |
|
101 main.print_info ( who, " Features:" .. text ) |
|
102 else |
|
103 main.print_info ( who, " No features" ) |
|
104 end |
|
105 end, |
|
106 function ( mesg ) |
|
107 main.print_info ( who, ("Info service discovery for %s failed: %s"):format ( who, mesg ) ) |
|
108 end ) |
|
109 end |
|
110 end, true, 'jid' ) |
|
111 |
|
112 commands_help['disco'] = "[-t target_jid] [info | items] [node]\n\nService discovery request.\nInfo is sent if omitted." |
|
113 |
|
114 -- vim: se ts=4: -- |