809
|
1 -- vim:sts=4 sw=4 |
|
2 |
|
3 -- Prosody IM |
|
4 -- Copyright (C) 2008-2010 Matthew Wild |
|
5 -- Copyright (C) 2008-2010 Waqas Hussain |
|
6 -- Copyright (C) 2012 Rob Hoelz |
|
7 -- |
|
8 -- This project is MIT/X11 licensed. Please see the |
|
9 -- COPYING file in the source package for more information. |
|
10 -- |
|
11 |
|
12 local st = require 'util.stanza'; |
|
13 |
|
14 local VCARD_NS = 'vcard-temp'; |
|
15 |
|
16 local builder_methods = {}; |
|
17 |
|
18 function builder_methods:addvalue(key, value) |
|
19 self.vcard:tag(key):text(value):up(); |
|
20 end |
|
21 |
|
22 function builder_methods:addregularfield(tagname, format_section) |
|
23 local record = self.record; |
|
24 local format = self.format; |
|
25 local vcard = self.vcard; |
|
26 |
|
27 if not format[format_section] then |
|
28 return; |
|
29 end |
|
30 |
|
31 local tag = vcard:tag(tagname); |
|
32 |
|
33 for k, v in pairs(format[format_section]) do |
|
34 tag:tag(string.upper(k)):text(record[v]):up(); |
|
35 end |
|
36 |
|
37 vcard:up(); |
|
38 end |
|
39 |
|
40 function builder_methods:addmultisectionedfield(tagname, format_section) |
|
41 local record = self.record; |
|
42 local format = self.format; |
|
43 local vcard = self.vcard; |
|
44 |
|
45 if not format[format_section] then |
|
46 return; |
|
47 end |
|
48 |
|
49 for k, v in pairs(format[format_section]) do |
|
50 local tag = vcard:tag(tagname); |
|
51 |
|
52 if type(k) == 'string' then |
|
53 tag:tag(string.upper(k)):up(); |
|
54 end |
|
55 |
|
56 for k2, v2 in pairs(v) do |
|
57 if type(v2) == 'boolean' then |
|
58 tag:tag(string.upper(k2)):up(); |
|
59 else |
|
60 tag:tag(string.upper(k2)):text(record[v2]):up(); |
|
61 end |
|
62 end |
|
63 |
|
64 vcard:up(); |
|
65 end |
|
66 end |
|
67 |
|
68 function builder_methods:build() |
|
69 local record = self.record; |
|
70 local format = self.format; |
|
71 |
|
72 self:addvalue( 'VERSION', '2.0'); |
|
73 self:addvalue( 'FN', record[format.displayname]); |
|
74 self:addregularfield( 'N', 'name'); |
|
75 self:addvalue( 'NICKNAME', record[format.nickname]); |
|
76 self:addregularfield( 'PHOTO', 'photo'); |
|
77 self:addvalue( 'BDAY', record[format.birthday]); |
|
78 self:addmultisectionedfield('ADR', 'address'); |
|
79 self:addvalue( 'LABEL', nil); -- we don't support LABEL...yet. |
|
80 self:addmultisectionedfield('TEL', 'telephone'); |
|
81 self:addmultisectionedfield('EMAIL', 'email'); |
|
82 self:addvalue( 'JABBERID', record.jid); |
|
83 self:addvalue( 'MAILER', record[format.mailer]); |
|
84 self:addvalue( 'TZ', record[format.timezone]); |
|
85 self:addregularfield( 'GEO', 'geo'); |
|
86 self:addvalue( 'TITLE', record[format.title]); |
|
87 self:addvalue( 'ROLE', record[format.role]); |
|
88 self:addregularfield( 'LOGO', 'logo'); |
|
89 self:addvalue( 'AGENT', nil); -- we don't support AGENT...yet. |
|
90 self:addregularfield( 'ORG', 'org'); |
|
91 self:addvalue( 'CATEGORIES', nil); -- we don't support CATEGORIES...yet. |
|
92 self:addvalue( 'NOTE', record[format.note]); |
|
93 self:addvalue( 'PRODID', nil); -- we don't support PRODID...yet. |
|
94 self:addvalue( 'REV', record[format.rev]); |
|
95 self:addvalue( 'SORT-STRING', record[format.sortstring]); |
|
96 self:addregularfield( 'SOUND', 'sound'); |
|
97 self:addvalue( 'UID', record[format.uid]); |
|
98 self:addvalue( 'URL', record[format.url]); |
|
99 self:addvalue( 'CLASS', nil); -- we don't support CLASS...yet. |
|
100 self:addregularfield( 'KEY', 'key'); |
|
101 self:addvalue( 'DESC', record[format.description]); |
|
102 |
|
103 return self.vcard; |
|
104 end |
|
105 |
|
106 local function new_builder(params) |
|
107 local vcard_tag = st.stanza('vCard', { xmlns = VCARD_NS }); |
|
108 |
|
109 local object = { |
|
110 vcard = vcard_tag, |
|
111 __index = builder_methods, |
|
112 }; |
|
113 |
|
114 for k, v in pairs(params) do |
|
115 object[k] = v; |
|
116 end |
|
117 |
|
118 setmetatable(object, object); |
|
119 |
|
120 return object; |
|
121 end |
|
122 |
|
123 local _M = {}; |
|
124 |
|
125 function _M.create(params) |
|
126 local builder = new_builder(params); |
|
127 |
|
128 return builder:build(); |
|
129 end |
|
130 |
|
131 return _M; |