1 local st
= require
"util.stanza"
2 local jid_split
= require
"util.jid".split
;
4 local mod_pep
= module
:depends("pep");
6 local sha1
= require
"util.hashes".sha1
;
7 local base64_decode
= require
"util.encodings".base64
.decode
;
9 local vcards
= module
:open_store("vcard");
11 module
:add_feature("vcard-temp");
12 module
:hook("account-disco-info", function (event
)
13 event
.reply
:tag("feature", { var
= "urn:xmpp:pep-vcard-conversion:0" }):up();
16 local function handle_error(origin
, stanza
, err
)
17 if err
== "forbidden" then
18 origin
.send(st
.error_reply(stanza
, "auth", "forbidden"));
19 elseif err
== "internal-server-error" then
20 origin
.send(st
.error_reply(stanza
, "wait", "internal-server-error"));
22 origin
.send(st
.error_reply(stanza
, "modify", "undefined-condition", err
));
26 -- Simple translations
27 -- <foo><text>hey</text></foo> -> <FOO>hey</FOO>
38 module
:hook("iq-get/bare/vcard-temp:vCard", function (event
)
39 local origin
, stanza
= event
.origin
, event
.stanza
;
40 local pep_service
= mod_pep
.get_pep_service(jid_split(stanza
.attr
.to
) or origin
.username
);
41 local ok
, id
, vcard4_item
= pep_service
:get_last_item("urn:xmpp:vcard4", stanza
.attr
.from
);
43 local vcard_temp
= st
.stanza("vCard", { xmlns
= "vcard-temp" });
44 if ok
and vcard4_item
then
45 local vcard4
= vcard4_item
.tags
[1];
47 local fn
= vcard4
:get_child("fn");
48 vcard_temp
:text_tag("FN", fn
and fn
:get_child_text("text"));
50 local v4n
= vcard4
:get_child("n");
52 :text_tag("FAMILY", v4n
and v4n
:get_child_text("surname"))
53 :text_tag("GIVEN", v4n
and v4n
:get_child_text("given"))
54 :text_tag("MIDDLE", v4n
and v4n
:get_child_text("additional"))
55 :text_tag("PREFIX", v4n
and v4n
:get_child_text("prefix"))
56 :text_tag("SUFFIX", v4n
and v4n
:get_child_text("suffix"))
59 for tag in vcard4
:childtags() do
60 local typ
= simple_map
[tag.name
];
62 local text
= tag:get_child_text(typ
);
64 vcard_temp
:text_tag(tag.name
:upper(), text
);
66 elseif tag.name
== "email" then
67 local text
= tag:get_child_text("text");
69 vcard_temp
:tag("EMAIL")
70 :text_tag("USERID", text
)
71 :tag("INTERNET"):up();
72 if tag:find
"parameters/type/text#" == "home" then
73 vcard_temp
:tag("HOME"):up();
74 elseif tag:find
"parameters/type/text#" == "work" then
75 vcard_temp
:tag("WORK"):up();
79 elseif tag.name
== "tel" then
80 local text
= tag:get_child_text("uri");
82 if text
:sub(1, 4) == "tel:" then
85 vcard_temp
:tag("TEL"):text_tag("NUMBER", text
);
86 if tag:find
"parameters/type/text#" == "home" then
87 vcard_temp
:tag("HOME"):up();
88 elseif tag:find
"parameters/type/text#" == "work" then
89 vcard_temp
:tag("WORK"):up();
93 elseif tag.name
== "adr" then
95 :text_tag("POBOX", tag:get_child_text("pobox"))
96 :text_tag("EXTADD", tag:get_child_text("ext"))
97 :text_tag("STREET", tag:get_child_text("street"))
98 :text_tag("LOCALITY", tag:get_child_text("locality"))
99 :text_tag("REGION", tag:get_child_text("region"))
100 :text_tag("PCODE", tag:get_child_text("code"))
101 :text_tag("CTRY", tag:get_child_text("country"));
102 if tag:find
"parameters/type/text#" == "home" then
103 vcard_temp
:tag("HOME"):up();
104 elseif tag:find
"parameters/type/text#" == "work" then
105 vcard_temp
:tag("WORK"):up();
112 local meta_ok
, avatar_meta
= pep_service
:get_items("urn:xmpp:avatar:metadata", stanza
.attr
.from
);
113 local data_ok
, avatar_data
= pep_service
:get_items("urn:xmpp:avatar:data", stanza
.attr
.from
);
116 for _
, hash
in ipairs(avatar_data
) do
117 local meta
= meta_ok
and avatar_meta
[hash
];
118 local data
= avatar_data
[hash
];
119 local info
= meta
and meta
.tags
[1]:get_child("info");
120 vcard_temp
:tag("PHOTO");
121 if info
and info
.attr
.type then
122 vcard_temp
:text_tag("TYPE", info
.attr
.type);
125 vcard_temp
:text_tag("BINVAL", data
.tags
[1]:get_text());
126 elseif info
and info
.attr
.url
then
127 vcard_temp
:text_tag("EXTVAL", info
.attr
.url
);
133 if not vcard_temp
.tags
[1] then
134 vcard_temp
= st
.deserialize(vcards
:get(jid_split(stanza
.attr
.to
) or origin
.username
)) or vcard_temp
;
137 origin
.send(st
.reply(stanza
):add_child(vcard_temp
));
141 local node_defaults
= {
142 access_model
= "open";
143 _defaults_only
= true;
146 module
:hook("iq-set/self/vcard-temp:vCard", function (event
)
147 local origin
, stanza
= event
.origin
, event
.stanza
;
148 local pep_service
= mod_pep
.get_pep_service(origin
.username
);
150 local vcard_temp
= stanza
.tags
[1];
152 local vcard4
= st
.stanza("item", { xmlns
= "http://jabber.org/protocol/pubsub", id
= "current" })
153 :tag("vcard", { xmlns
= 'urn:ietf:params:xml:ns:vcard-4.0' });
155 if pep_service
:purge("urn:xmpp:avatar:metadata", origin
.full_jid
) then
156 pep_service
:purge("urn:xmpp:avatar:data", origin
.full_jid
);
159 vcard4
:tag("fn"):text_tag("text", vcard_temp
:get_child_text("FN")):up();
161 local N
= vcard_temp
:get_child("N");
164 :text_tag("surname", N
and N
:get_child_text("FAMILY"))
165 :text_tag("given", N
and N
:get_child_text("GIVEN"))
166 :text_tag("additional", N
and N
:get_child_text("MIDDLE"))
167 :text_tag("prefix", N
and N
:get_child_text("PREFIX"))
168 :text_tag("suffix", N
and N
:get_child_text("SUFFIX"))
171 for tag in vcard_temp
:childtags() do
172 local typ
= simple_map
[tag.name
:lower()];
174 local text
= tag:get_text();
176 vcard4
:tag(tag.name
:lower()):text_tag(typ
, text
):up();
178 elseif tag.name
== "EMAIL" then
179 local text
= tag:get_child_text("USERID");
182 vcard4
:text_tag("text", text
)
183 vcard4
:tag("parameters"):tag("type");
184 if tag:get_child("HOME") then
185 vcard4
:text_tag("text", "home");
186 elseif tag:get_child("WORK") then
187 vcard4
:text_tag("text", "work");
189 vcard4
:up():up():up();
191 elseif tag.name
== "TEL" then
192 local text
= tag:get_child_text("NUMBER");
194 vcard4
:tag("tel"):text_tag("uri", "tel:"..text
);
196 vcard4
:tag("parameters"):tag("type");
197 if tag:get_child("HOME") then
198 vcard4
:text_tag("text", "home");
199 elseif tag:get_child("WORK") then
200 vcard4
:text_tag("text", "work");
202 vcard4
:up():up():up();
203 elseif tag.name
== "ORG" then
204 local text
= tag:get_child_text("ORGNAME");
206 vcard4
:tag("org"):text_tag("text", text
):up();
208 elseif tag.name
== "DESC" then
209 local text
= tag:get_text();
211 vcard4
:tag("note"):text_tag("text", text
):up();
213 -- <note> gets mapped into <NOTE> in the other direction
214 elseif tag.name
== "ADR" then
216 :text_tag("pobox", tag:get_child_text("POBOX"))
217 :text_tag("ext", tag:get_child_text("EXTADD"))
218 :text_tag("street", tag:get_child_text("STREET"))
219 :text_tag("locality", tag:get_child_text("LOCALITY"))
220 :text_tag("region", tag:get_child_text("REGION"))
221 :text_tag("code", tag:get_child_text("PCODE"))
222 :text_tag("country", tag:get_child_text("CTRY"));
223 vcard4
:tag("parameters"):tag("type");
224 if tag:get_child("HOME") then
225 vcard4
:text_tag("text", "home");
226 elseif tag:get_child("WORK") then
227 vcard4
:text_tag("text", "work");
229 vcard4
:up():up():up();
230 elseif tag.name
== "PHOTO" then
231 local avatar_type
= tag:get_child_text("TYPE");
232 local avatar_payload
= tag:get_child_text("BINVAL");
233 -- Can EXTVAL be translated? No way to know the sha1 of the data?
235 if avatar_payload
then
236 local avatar_raw
= base64_decode(avatar_payload
);
237 local avatar_hash
= sha1(avatar_raw
, true);
239 local avatar_meta
= st
.stanza("item", { id
= avatar_hash
, xmlns
= "http://jabber.org/protocol/pubsub" })
240 :tag("metadata", { xmlns
="urn:xmpp:avatar:metadata" })
242 bytes
= tostring(#avatar_raw
),
247 local avatar_data
= st
.stanza("item", { id
= avatar_hash
, xmlns
= "http://jabber.org/protocol/pubsub" })
248 :tag("data", { xmlns
="urn:xmpp:avatar:data" })
249 :text(avatar_payload
);
251 local ok
, err
= pep_service
:publish("urn:xmpp:avatar:data", origin
.full_jid
, avatar_hash
, avatar_data
, node_defaults
)
253 ok
, err
= pep_service
:publish("urn:xmpp:avatar:metadata", origin
.full_jid
, avatar_hash
, avatar_meta
, node_defaults
);
256 handle_error(origin
, stanza
, err
);
263 local ok
, err
= pep_service
:publish("urn:xmpp:vcard4", origin
.full_jid
, "current", vcard4
, node_defaults
);
265 origin
.send(st
.reply(stanza
));
267 handle_error(origin
, stanza
, err
);
273 local function inject_xep153(event
)
274 local origin
, stanza
= event
.origin
, event
.stanza
;
275 local username
= origin
.username
;
276 if not username
then return end
277 if stanza
.attr
.type then return end
278 local pep_service
= mod_pep
.get_pep_service(username
);
280 stanza
:remove_children("x", "vcard-temp:x:update");
281 local x_update
= st
.stanza("x", { xmlns
= "vcard-temp:x:update" });
282 local ok
, avatar_hash
= pep_service
:get_last_item("urn:xmpp:avatar:metadata", true);
283 if ok
and avatar_hash
then
284 x_update
:text_tag("photo", avatar_hash
);
286 stanza
:add_direct_child(x_update
);
289 module
:hook("pre-presence/full", inject_xep153
, 1);
290 module
:hook("pre-presence/bare", inject_xep153
, 1);
291 module
:hook("pre-presence/host", inject_xep153
, 1);