MUC: Fix delay@from to be room JID (fixes #1416)
[prosody.git] / plugins / mod_vcard_legacy.lua
blobab2c44902403e23d8e35e959299295d38eea17ec
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();
14 end);
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"));
21 else
22 origin.send(st.error_reply(stanza, "modify", "undefined-condition", err));
23 end
24 end
26 -- Simple translations
27 -- <foo><text>hey</text></foo> -> <FOO>hey</FOO>
28 local simple_map = {
29 nickname = "text";
30 title = "text";
31 role = "text";
32 categories = "text";
33 note = "text";
34 url = "uri";
35 bday = "date";
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");
51 vcard_temp:tag("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"))
57 :up();
59 for tag in vcard4:childtags() do
60 local typ = simple_map[tag.name];
61 if typ then
62 local text = tag:get_child_text(typ);
63 if text then
64 vcard_temp:text_tag(tag.name:upper(), text);
65 end
66 elseif tag.name == "email" then
67 local text = tag:get_child_text("text");
68 if text then
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();
76 end
77 vcard_temp:up();
78 end
79 elseif tag.name == "tel" then
80 local text = tag:get_child_text("uri");
81 if text then
82 if text:sub(1, 4) == "tel:" then
83 text = text:sub(5)
84 end
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();
90 end
91 vcard_temp:up();
92 end
93 elseif tag.name == "adr" then
94 vcard_temp:tag("ADR")
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();
107 vcard_temp: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);
115 if data_ok then
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);
124 if data then
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);
129 vcard_temp:up();
133 origin.send(st.reply(stanza):add_child(vcard_temp));
134 return true;
135 end);
137 local node_defaults = {
138 access_model = "open";
139 _defaults_only = true;
142 function vcard_to_pep(vcard_temp)
143 local avatars = {};
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");
152 vcard4:tag("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"))
158 :up();
160 for tag in vcard_temp:childtags() do
161 local typ = simple_map[tag.name:lower()];
162 if typ then
163 local text = tag:get_text();
164 if text then
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");
169 if text then
170 vcard4:tag("email")
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");
182 if text then
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");
194 if text then
195 vcard4:tag("org"):text_tag("text", text):up();
197 elseif tag.name == "DESC" then
198 local text = tag:get_text();
199 if text then
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
204 vcard4:tag("adr")
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" })
230 :tag("info", {
231 bytes = tostring(#avatar_raw),
232 id = avatar_hash,
233 type = avatar_type,
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)
248 if avatars then
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;
255 if #avatars > 1 then
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);
264 if ok then
265 ok, err = pep_service:publish("urn:xmpp:avatar:metadata", actor, avatar.hash, avatar.meta, avatar_defaults);
267 if not ok then
268 return ok, err;
273 if vcard4 then
274 return pep_service:publish("urn:xmpp:vcard4", actor, "current", vcard4, node_defaults);
277 return true;
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));
287 if ok then
288 origin.send(st.reply(stanza));
289 else
290 handle_error(origin, stanza, err);
293 return true;
294 end);
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");
323 return;
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
329 vcard4 = nil;
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
333 avatars = nil;
335 if not (vcard4 or avatars) then
336 session.log("debug", "Already PEP data, not overwriting with migrated data");
337 vcards:set(username, nil);
338 return;
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");
343 else
344 session.log("info", "Failed to migrate vCard-temp to PEP: %s", err or "problem emptying 'vcard' store");
346 end);