1 -- Copyright (C) 2011-2014 Kim Alvefur
3 -- This project is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information.
10 local st = require "util.stanza";
11 local t_insert, t_concat = table.insert, table.concat;
13 local next, pairs, ipairs = next, pairs, ipairs;
15 local from_text, to_text, from_xep54, to_xep54;
17 local line_sep = "\n";
19 local vCard_dtd; -- See end of file
22 local function fold_line()
23 error "Not implemented" --TODO
25 local function unfold_line()
26 error "Not implemented"
27 -- gsub("\r?\n[ \t]([^\r\n])", "%1");
30 local function vCard_esc(s)
31 return s:gsub("[,:;\\]", "\\%1"):gsub("\n","\\n");
34 local function vCard_unesc(s)
35 return s:gsub("\\?[\\nt:;,]", {
40 ["\\:"] = ":", -- FIXME Shouldn't need to espace : in values, just params
49 local function item_to_xep54(item)
50 local t = st.stanza(item.name, { xmlns = "vcard-temp" });
52 local prop_def = vCard_dtd[item.name];
53 if prop_def == "text" then
55 elseif type(prop_def) == "table" then
56 if prop_def.types and item.TYPE then
57 if type(item.TYPE) == "table" then
58 for _,v in pairs(prop_def.types) do
59 for _,typ in pairs(item.TYPE) do
60 if typ:upper() == v then
67 t:tag(item.TYPE:upper()):up();
71 if prop_def.props then
72 for _,v in pairs(prop_def.props) do
79 if prop_def.value then
80 t:tag(prop_def.value):text(item[1]):up();
81 elseif prop_def.values then
82 local prop_def_values = prop_def.values;
83 local repeat_last = prop_def_values.behaviour == "repeat-last" and prop_def_values[#prop_def_values];
85 t:tag(prop_def.values[i] or repeat_last):text(item[i]):up();
93 local function vcard_to_xep54(vCard)
94 local t = st.stanza("vCard", { xmlns = "vcard-temp" });
96 t:add_child(item_to_xep54(vCard[i]));
101 function to_xep54(vCards)
102 if not vCards[1] or vCards[1].name then
103 return vcard_to_xep54(vCards)
105 local t = st.stanza("xCard", { xmlns = "vcard-temp" });
107 t:add_child(vcard_to_xep54(vCards[i]));
113 function from_text(data)
114 data = data -- unfold and remove empty lines
119 local c; -- current item
120 for line in data:gmatch("[^\n]+") do
121 local line = vCard_unesc(line);
122 local name, params, value = line:match("^([-%a]+)(\30?[^\29]*)\29(.*)$");
123 value = value:gsub("\29",":");
126 for k,isval,v in params:gmatch("\30([^=]+)(=?)([^\30]*)") do
129 for _p in v:gmatch("[^\31]+") do
141 if name == "BEGIN" and value == "VCARD" then
143 vCards[#vCards+1] = c;
144 elseif name == "END" and value == "VCARD" then
146 elseif c and vCard_dtd[name] then
147 local dtd = vCard_dtd[name];
148 local p = { name = name };
154 for _, t in ipairs(dtd.types) do
156 if ( params.TYPE and params.TYPE[t] == true)
157 or params[t] == true then
163 for _, p in ipairs(dtd.props) do
165 if params[p] == true then
168 for _, prop in ipairs(params[p]) do
175 if dtd == "text" or dtd.value then
177 elseif dtd.values then
178 local value = "\30"..value;
179 for p in value:gmatch("\30([^\30]*)") do
189 local function item_to_text(item)
192 value[i] = vCard_esc(item[i]);
194 value = t_concat(value, ";");
197 for k,v in pairs(item) do
198 if type(k) == "string" and k ~= "name" then
199 params = params .. (";%s=%s"):format(k, type(v) == "table" and t_concat(v,",") or v);
203 return ("%s%s:%s"):format(item.name, params, value)
206 local function vcard_to_text(vcard)
208 t_insert(t, "BEGIN:VCARD")
210 t_insert(t, item_to_text(vcard[i]));
212 t_insert(t, "END:VCARD")
213 return t_concat(t, line_sep);
216 function to_text(vCards)
217 if vCards[1] and vCards[1].name then
218 return vcard_to_text(vCards)
222 t[i]=vcard_to_text(vCards[i]);
224 return t_concat(t, line_sep);
228 local function from_xep54_item(item)
229 local prop_name = item.name;
230 local prop_def = vCard_dtd[prop_name];
232 local prop = { name = prop_name };
234 if prop_def == "text" then
235 prop[1] = item:get_text();
236 elseif type(prop_def) == "table" then
237 if prop_def.value then --single item
238 prop[1] = item:get_child_text(prop_def.value) or "";
239 elseif prop_def.values then --array
240 local value_names = prop_def.values;
241 if value_names.behaviour == "repeat-last" then
242 for i=1,#item.tags do
243 t_insert(prop, item.tags[i]:get_text() or "");
246 for i=1,#value_names do
247 t_insert(prop, item:get_child_text(value_names[i]) or "");
250 elseif prop_def.names then
251 local names = prop_def.names;
253 if item:get_child(names[i]) then
260 if prop_def.props_verbatim then
261 for k,v in pairs(prop_def.props_verbatim) do
266 if prop_def.types then
267 local types = prop_def.types;
270 if item:get_child(types[i]) then
271 t_insert(prop.TYPE, types[i]:lower());
274 if #prop.TYPE == 0 then
279 -- A key-value pair, within a key-value pair?
280 if prop_def.props then
281 local params = prop_def.props;
283 local name = params[i]
284 local data = item:get_child_text(name);
286 prop[name] = prop[name] or {};
287 t_insert(prop[name], data);
298 local function from_xep54_vCard(vCard)
299 local tags = vCard.tags;
302 t_insert(t, from_xep54_item(tags[i]));
307 function from_xep54(vCard)
308 if vCard.attr.xmlns ~= "vcard-temp" then
309 return nil, "wrong-xmlns";
311 if vCard.name == "xCard" then -- A collection of vCards
313 local vCards = vCard.tags;
315 t[i] = from_xep54_vCard(vCards[i]);
318 elseif vCard.name == "vCard" then -- A single vCard
319 return from_xep54_vCard(vCard)
325 function vcard4:text(node, params, value)
326 self:tag(node:lower())
328 if type(value) == "string" then
329 self:tag("text"):text(value):up()
330 elseif vcard4[node] then
336 function vcard4.N(value)
337 for i, k in ipairs(vCard_dtd.N.values) do
338 value:tag(k):text(value[i]):up();
342 local xmlns_vcard4 = "urn:ietf:params:xml:ns:vcard-4.0"
344 local function item_to_vcard4(item)
345 local typ = item.name:lower();
346 local t = st.stanza(typ, { xmlns = xmlns_vcard4 });
348 local prop_def = vCard4_dtd[typ];
349 if prop_def == "text" then
350 t:tag("text"):text(item[1]):up();
351 elseif prop_def == "uri" then
352 if item.ENCODING and item.ENCODING[1] == 'b' then
353 t:tag("uri"):text("data:;base64,"):text(item[1]):up();
355 t:tag("uri"):text(item[1]):up();
357 elseif type(prop_def) == "table" then
358 if prop_def.values then
359 for i, v in ipairs(prop_def.values) do
360 t:tag(v:lower()):text(item[i] or ""):up();
363 t:tag("unsupported",{xmlns="http://zash.se/protocol/vcardlib"})
366 t:tag("unsupported",{xmlns="http://zash.se/protocol/vcardlib"})
371 local function vcard_to_vcard4xml(vCard)
372 local t = st.stanza("vcard", { xmlns = xmlns_vcard4 });
374 t:add_child(item_to_vcard4(vCard[i]));
379 local function vcards_to_vcard4xml(vCards)
380 if not vCards[1] or vCards[1].name then
381 return vcard_to_vcard4xml(vCards)
383 local t = st.stanza("vcards", { xmlns = xmlns_vcard4 });
385 t:add_child(vcard_to_vcard4xml(vCards[i]));
391 -- This was adapted from http://xmpp.org/extensions/xep-0054.html#dtd
393 VERSION = "text", --MUST be 3.0, so parsing is redundant
406 props_verbatim = { ENCODING = { "b" } },
408 value = "BINVAL", --{ "EXTVAL", },
482 LOGO = "copy of PHOTO",
486 behaviour = "repeat-last",
498 SOUND = "copy of PHOTO",
502 names = { -- The item.name is the value if it's one of these.
514 vCard_dtd.LOGO = vCard_dtd.PHOTO;
515 vCard_dtd.SOUND = vCard_dtd.PHOTO;
533 bday = "date-and-or-time",
534 anniversary = "date-and-or-time",
550 lang = "language-tag",
565 clientpidmap = "number, uuid",
575 from_text = from_text;
578 from_xep54 = from_xep54;
581 to_vcard4 = vcards_to_vcard4xml;