util/vcard.lua

changeset 298
1897dc6a07bb
parent 227
31019cb93d59
child 306
c6183b218f77
equal deleted inserted replaced
297:447dffdaf46c 298:1897dc6a07bb
1 -- Copyright (C) 2011 Kim Alvefur 1 -- Copyright (C) 2011-2012 Kim Alvefur
2 -- 2 --
3 -- This project is MIT/X11 licensed. Please see the 3 -- This project is MIT/X11 licensed. Please see the
4 -- COPYING file in the source package for more information. 4 -- COPYING file in the source package for more information.
5 -- 5 --
6 6
12 local st = verse or require "util.stanza"; 12 local st = verse or require "util.stanza";
13 local t_insert, t_concat = table.insert, table.concat; 13 local t_insert, t_concat = table.insert, table.concat;
14 local type = type; 14 local type = type;
15 local next, pairs, ipairs = next, pairs, ipairs; 15 local next, pairs, ipairs = next, pairs, ipairs;
16 16
17 module "vcard" 17 local lua_to_text, lua_to_xep54, text_to_lua, text_to_xep54, xep54_to_lua, xep54_to_text;
18 --[[ TODO local from_text, to_text, from_xep54, to_xep54; --]]
19
18 20
19 local vCard_dtd; 21 local vCard_dtd;
20 22
21 local function vCard_esc(s) 23 local function vCard_esc(s)
22 return s:gsub("[,:;\\]", "\\%1"):gsub("\n","\\n"); 24 return s:gsub("[,:;\\]", "\\%1"):gsub("\n","\\n");
34 [";"] = "\30", 36 [";"] = "\30",
35 [","] = "\31", 37 [","] = "\31",
36 }); 38 });
37 end 39 end
38 40
39 function text_to_xep54(data) 41 local function text_to_xep54(data)
40 --[[ TODO 42 --[[ TODO
41 return lua_to_xep54(text_to_lua(data)); 43 return lua_to_xep54(text_to_lua(data));
42 --]] 44 --]]
43 data = data 45 data = data
44 :gsub("\r\n","\n") 46 :gsub("\r\n","\n")
109 end 111 end
110 end 112 end
111 return c; 113 return c;
112 end 114 end
113 115
114 function text_to_lua(data) --table 116 local function text_to_lua(data) --table
115 data = data 117 data = data
116 :gsub("\r\n","\n") 118 :gsub("\r\n","\n")
117 :gsub("\n ", "") 119 :gsub("\n ", "")
118 :gsub("\n\n+","\n"); 120 :gsub("\n\n+","\n");
119 local vCards = {}; 121 local vCards = {};
271 273
272 return ("%s%s:%s"):format(item.name, params, value) 274 return ("%s%s:%s"):format(item.name, params, value)
273 :gsub(("."):rep(75), "%0\r\n "):gsub("\r\n $",""); 275 :gsub(("."):rep(75), "%0\r\n "):gsub("\r\n $","");
274 end 276 end
275 277
276 function xep54_to_text(vCard) 278 local function xep54_to_text(vCard)
277 --[[ TODO 279 --[[ TODO
278 return lua_to_text(xep54_to_lua(vCard)) 280 return lua_to_text(xep54_to_lua(vCard))
279 --]] 281 --]]
280 local r = {}; 282 local r = {};
281 t_insert(r, "BEGIN:VCARD"); 283 t_insert(r, "BEGIN:VCARD");
370 t[i] = xep54_item_to_lua(tags[i]); 372 t[i] = xep54_item_to_lua(tags[i]);
371 end 373 end
372 return t 374 return t
373 end 375 end
374 376
375 function xep54_to_lua(vCard) 377 local function xep54_to_lua(vCard)
376 if vCard.attr.xmlns ~= "vcard-temp" then 378 if vCard.attr.xmlns ~= "vcard-temp" then
377 return false 379 return false
378 end 380 end
379 if vCard.name == "xCard" then 381 if vCard.name == "xCard" then
380 local t = {}; 382 local t = {};
513 }, 515 },
514 DESC = "text", 516 DESC = "text",
515 }; 517 };
516 vCard_dtd.LOGO = vCard_dtd.PHOTO; 518 vCard_dtd.LOGO = vCard_dtd.PHOTO;
517 vCard_dtd.SOUND = vCard_dtd.PHOTO; 519 vCard_dtd.SOUND = vCard_dtd.PHOTO;
518 return _M 520
521 return {
522 text_to_xep54 = text_to_xep54;
523 text_to_lua = text_to_lua;
524 xep54_to_text = xep54_to_text;
525 xep54_to_lua = xep54_to_lua;
526 --[[ TODO
527 from_text = from_text;
528 to_text = to_text;
529 from_xep54 = from_xep54;
530 to_xep54 = to_xep54;
531 --]]
532 };

mercurial