62
|
1 |
|
|
2 |
-- VCARD-TEMP (XEP-0054) |
|
3 |
|
|
4 |
-- library |
|
5 |
|
|
6 |
require 'lm' |
|
7 |
require 'iq' |
|
8 |
require 'x_data' |
|
9 |
|
|
10 |
-- public |
|
11 |
|
|
12 |
vcard = { } |
|
13 |
|
|
14 |
local function vcard_parse ( node, path, ret ) |
|
15 |
local item = node:child () |
|
16 |
if item then |
|
17 |
while item do |
|
18 |
vcard_parse ( item, ( path and path .. '/' or '' ) .. item:name (), ret ) |
|
19 |
item = item:next () |
|
20 |
end |
|
21 |
elseif path then |
|
22 |
table.insert ( ret, { var = path, type = 'text-single', value = node:value () } ) |
|
23 |
end |
|
24 |
end |
|
25 |
|
|
26 |
function vcard.parse ( card ) |
|
27 |
local form = { xmlns = 'vcard-temp', type = 'form' } |
|
28 |
vcard_parse ( card, nil, form ) |
|
29 |
form.format = |
|
30 |
function ( form, root ) |
|
31 |
root.vCard = { xmlns = 'vcard-temp' } |
|
32 |
for k, field in ipairs ( form ) do |
|
33 |
local el = root.vCard |
|
34 |
for k in field.var:gmatch ( '[^/]+' ) do |
|
35 |
if not el[k] then |
|
36 |
el[k] = { } |
|
37 |
end |
|
38 |
el = el[k] |
|
39 |
end |
|
40 |
el[1] = field.value |
|
41 |
end |
|
42 |
return root |
|
43 |
end |
|
44 |
return form |
|
45 |
end |
|
46 |
|
|
47 |
function vcard.get ( conn, from, success, fail ) |
|
48 |
iq.send ( conn, from, 'get', |
|
49 |
{ |
|
50 |
vCard = { xmlns = 'vcard-temp' }, |
|
51 |
}, |
|
52 |
function ( mess ) |
|
53 |
local card = mess:child ( 'vCard' ) |
|
54 |
if card and card:attribute ( 'xmlns' ) == 'vcard-temp' then |
|
55 |
success ( vcard.parse ( mess:child ( 'vCard' ) ), |
|
56 |
function ( form, success, fail ) |
|
57 |
form.type = 'submit' -- :) |
|
58 |
iq.send ( conn, from, 'set', form.format ( form, { } ), success, fail ) |
|
59 |
end, |
|
60 |
function ( form, success, fail ) |
|
61 |
success () |
|
62 |
end ) |
|
63 |
else |
|
64 |
fail ( mess:xml () ) -- XXX |
|
65 |
end |
|
66 |
end, fail ) |
|
67 |
end |
|
68 |
|
|
69 |
-- mcabber |
|
70 |
|
|
71 |
main.command ( 'vcard-temp', |
|
72 |
function ( args ) |
|
73 |
vcard.get ( lm.connection.bless ( main.connection () ), args[1], |
|
74 |
function ( form, submit, reject ) |
|
75 |
local id = #forms + 1 |
|
76 |
forms[id] = { |
|
77 |
form = form, |
|
78 |
submit = |
|
79 |
function ( form ) |
|
80 |
submit ( form, |
|
81 |
function () |
|
82 |
print ( 'Vcard changed' ) |
|
83 |
end, |
|
84 |
function ( mesg ) |
|
85 |
print ( 'Vcard changing error: ' .. mesg ) |
|
86 |
end ) |
|
87 |
end, |
|
88 |
reject = |
|
89 |
function ( form ) |
|
90 |
reject ( form, |
|
91 |
function () |
|
92 |
print ( 'Vcard changing cancelled' ) |
|
93 |
end, |
|
94 |
function ( mesg ) |
|
95 |
print ( 'Vcard changing cancellation error: ' .. mesg ) |
|
96 |
end ) |
|
97 |
end, |
|
98 |
} |
|
99 |
print ( 'You have new form ' .. id ) |
|
100 |
end, |
|
101 |
function ( mesg ) |
|
102 |
print ( 'Vcard obtaining error: ' .. mesg ) |
|
103 |
end ) |
|
104 |
end, true, 'jid' ) |
|
105 |
|
|
106 |
-- vim: se ts=4: -- |