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 origin
.send(st
.reply(stanza
):add_child(vcard_temp
));
137 local node_defaults
= {
138 access_model
= "open";
139 _defaults_only
= true;
142 function vcard_to_pep(vcard_temp
)
145 local vcard4
= st
.stanza("item", { xmlns
= "http://jabber.org/protocol/pubsub", id
= "current" })
146 :tag("vcard", { xmlns
= 'urn:ietf:params:xml:ns:vcard-4.0' });
148 vcard4
:tag("fn"):text_tag("text", vcard_temp
:get_child_text("FN")):up();
150 local N
= vcard_temp
:get_child("N");
153 :text_tag("surname", N
and N
:get_child_text("FAMILY"))
154 :text_tag("given", N
and N
:get_child_text("GIVEN"))
155 :text_tag("additional", N
and N
:get_child_text("MIDDLE"))
156 :text_tag("prefix", N
and N
:get_child_text("PREFIX"))
157 :text_tag("suffix", N
and N
:get_child_text("SUFFIX"))
160 for tag in vcard_temp
:childtags() do
161 local typ
= simple_map
[tag.name
:lower()];
163 local text
= tag:get_text();
165 vcard4
:tag(tag.name
:lower()):text_tag(typ
, text
):up();
167 elseif tag.name
== "EMAIL" then
168 local text
= tag:get_child_text("USERID");
171 vcard4
:text_tag("text", text
)
172 vcard4
:tag("parameters"):tag("type");
173 if tag:get_child("HOME") then
174 vcard4
:text_tag("text", "home");
175 elseif tag:get_child("WORK") then
176 vcard4
:text_tag("text", "work");
178 vcard4
:up():up():up();
180 elseif tag.name
== "TEL" then
181 local text
= tag:get_child_text("NUMBER");
183 vcard4
:tag("tel"):text_tag("uri", "tel:"..text
);
185 vcard4
:tag("parameters"):tag("type");
186 if tag:get_child("HOME") then
187 vcard4
:text_tag("text", "home");
188 elseif tag:get_child("WORK") then
189 vcard4
:text_tag("text", "work");
191 vcard4
:up():up():up();
192 elseif tag.name
== "ORG" then
193 local text
= tag:get_child_text("ORGNAME");
195 vcard4
:tag("org"):text_tag("text", text
):up();
197 elseif tag.name
== "DESC" then
198 local text
= tag:get_text();
200 vcard4
:tag("note"):text_tag("text", text
):up();
202 -- <note> gets mapped into <NOTE> in the other direction
203 elseif tag.name
== "ADR" then
205 :text_tag("pobox", tag:get_child_text("POBOX"))
206 :text_tag("ext", tag:get_child_text("EXTADD"))
207 :text_tag("street", tag:get_child_text("STREET"))
208 :text_tag("locality", tag:get_child_text("LOCALITY"))
209 :text_tag("region", tag:get_child_text("REGION"))
210 :text_tag("code", tag:get_child_text("PCODE"))
211 :text_tag("country", tag:get_child_text("CTRY"));
212 vcard4
:tag("parameters"):tag("type");
213 if tag:get_child("HOME") then
214 vcard4
:text_tag("text", "home");
215 elseif tag:get_child("WORK") then
216 vcard4
:text_tag("text", "work");
218 vcard4
:up():up():up();
219 elseif tag.name
== "PHOTO" then
220 local avatar_type
= tag:get_child_text("TYPE");
221 local avatar_payload
= tag:get_child_text("BINVAL");
222 -- Can EXTVAL be translated? No way to know the sha1 of the data?
224 if avatar_payload
then
225 local avatar_raw
= base64_decode(avatar_payload
);
226 local avatar_hash
= sha1(avatar_raw
, true);
228 local avatar_meta
= st
.stanza("item", { id
= avatar_hash
, xmlns
= "http://jabber.org/protocol/pubsub" })
229 :tag("metadata", { xmlns
="urn:xmpp:avatar:metadata" })
231 bytes
= tostring(#avatar_raw
),
236 local avatar_data
= st
.stanza("item", { id
= avatar_hash
, xmlns
= "http://jabber.org/protocol/pubsub" })
237 :tag("data", { xmlns
="urn:xmpp:avatar:data" })
238 :text(avatar_payload
);
240 table.insert(avatars
, { hash
= avatar_hash
, meta
= avatar_meta
, data
= avatar_data
});
244 return vcard4
, avatars
;
247 function save_to_pep(pep_service
, actor
, vcard4
, avatars
)
250 if pep_service
:purge("urn:xmpp:avatar:metadata", actor
) then
251 pep_service
:purge("urn:xmpp:avatar:data", actor
);
254 local avatar_defaults
= node_defaults
;
256 avatar_defaults
= {};
257 for k
,v
in pairs(node_defaults
) do
258 avatar_defaults
[k
] = v
;
260 avatar_defaults
.max_items
= #avatars
;
262 for _
, avatar
in ipairs(avatars
) do
263 local ok
, err
= pep_service
:publish("urn:xmpp:avatar:data", actor
, avatar
.hash
, avatar
.data
, avatar_defaults
);
265 ok
, err
= pep_service
:publish("urn:xmpp:avatar:metadata", actor
, avatar
.hash
, avatar
.meta
, avatar_defaults
);
274 return pep_service
:publish("urn:xmpp:vcard4", actor
, "current", vcard4
, node_defaults
);
280 module
:hook("iq-set/self/vcard-temp:vCard", function (event
)
281 local origin
, stanza
= event
.origin
, event
.stanza
;
282 local pep_service
= mod_pep
.get_pep_service(origin
.username
);
284 local vcard_temp
= stanza
.tags
[1];
286 local ok
, err
= save_to_pep(pep_service
, origin
.full_jid
, vcard_to_pep(vcard_temp
));
288 origin
.send(st
.reply(stanza
));
290 handle_error(origin
, stanza
, err
);
296 local function inject_xep153(event
)
297 local origin
, stanza
= event
.origin
, event
.stanza
;
298 local username
= origin
.username
;
299 if not username
then return end
300 if stanza
.attr
.type then return end
301 local pep_service
= mod_pep
.get_pep_service(username
);
303 stanza
:remove_children("x", "vcard-temp:x:update");
304 local x_update
= st
.stanza("x", { xmlns
= "vcard-temp:x:update" });
305 local ok
, avatar_hash
= pep_service
:get_last_item("urn:xmpp:avatar:metadata", true);
306 if ok
and avatar_hash
then
307 x_update
:text_tag("photo", avatar_hash
);
309 stanza
:add_direct_child(x_update
);
312 module
:hook("pre-presence/full", inject_xep153
, 1);
313 module
:hook("pre-presence/bare", inject_xep153
, 1);
314 module
:hook("pre-presence/host", inject_xep153
, 1);
316 if module
:get_option_boolean("upgrade_legacy_vcards", true) then
317 module
:hook("resource-bind", function (event
)
318 local session
= event
.session
;
319 local username
= session
.username
;
320 local vcard_temp
= vcards
:get(username
);
321 if not vcard_temp
then
322 session
.log("debug", "No legacy vCard to migrate or already migrated");
325 local pep_service
= mod_pep
.get_pep_service(username
);
326 vcard_temp
= st
.deserialize(vcard_temp
);
327 local vcard4
, avatars
= vcard_to_pep(vcard_temp
);
328 if pep_service
:get_last_item("urn:xmpp:vcard4", true) then
331 if pep_service
:get_last_item("urn:xmpp:avatar:metadata", true)
332 or pep_service
:get_last_item("urn:xmpp:avatar:data", true) then
335 if not (vcard4
or avatars
) then
336 session
.log("debug", "Already PEP data, not overwriting with migrated data");
337 vcards
:set(username
, nil);
340 local ok
, err
= save_to_pep(pep_service
, true, vcard4
, avatars
);
341 if ok
and vcards
:set(username
, nil) then
342 session
.log("info", "Migrated vCard-temp to PEP");
344 session
.log("info", "Failed to migrate vCard-temp to PEP: %s", err
or "problem emptying 'vcard' store");