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