Software /
code /
prosody
Annotate
plugins/mod_vcard_legacy.lua @ 9250:9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 20 Aug 2018 07:36:03 +0200 |
parent | 9249:506aabdb13bc |
child | 9251:3f31e10e8256 |
rev | line source |
---|---|
9249
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local st = require "util.stanza" |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local jid_split = require "util.jid".split; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local mod_pep = module:depends("pep"); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 module:add_feature("vcard-temp"); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 module:add_feature("urn:xmpp:pep-vcard-conversion:0"); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 -- Simple translations |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 -- <foo><text>hey</text></foo> -> <FOO>hey</FOO> |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local simple_map = { |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 nickname = "text"; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 title = "text"; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 role = "text"; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 categories = "text"; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 note = "text"; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 url = "uri"; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 bday = "date"; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 } |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 module:hook("iq-get/bare/vcard-temp:vCard", function (event) |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local origin, stanza = event.origin, event.stanza; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 local pep_service = mod_pep.get_pep_service(jid_split(stanza.attr.to) or origin.username); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 local ok, id, vcard4_item = pep_service:get_last_item("urn:xmpp:vcard4", stanza.attr.from); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local vcard_temp = st.stanza("vCard", { xmlns = "vcard-temp" }); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 if ok and vcard4_item then |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 local vcard4 = vcard4_item.tags[1]; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 local fn = vcard4:get_child("fn"); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 vcard_temp:text_tag("FN", fn and fn:get_child_text("text")); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 local v4n = vcard4:get_child("n"); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 vcard_temp:tag("N") |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 :text_tag("FAMILY", v4n and v4n:get_child_text("surname")) |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 :text_tag("GIVEN", v4n and v4n:get_child_text("given")) |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 :text_tag("MIDDLE", v4n and v4n:get_child_text("additional")) |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 :text_tag("PREFIX", v4n and v4n:get_child_text("prefix")) |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 :text_tag("SUFFIX", v4n and v4n:get_child_text("suffix")) |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 :up(); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 for tag in vcard4:childtags() do |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 local typ = simple_map[tag.name]; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 if typ then |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 local text = tag:get_child_text(typ); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 if text then |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 vcard_temp:text_tag(tag.name:upper(), text); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 end |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 end |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 end |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 end |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 origin.send(st.reply(stanza):add_child(vcard_temp)); |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 return true; |
506aabdb13bc
mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 end); |
9250
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
56 |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
57 local function inject_xep153(event) |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
58 local origin, stanza = event.origin, event.stanza; |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
59 local username = origin.username; |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
60 if not username then return end |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
61 local pep = mod_pep.get_pep_service(username); |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
62 |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
63 stanza:remove_children("x", "vcard-temp:x:update"); |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
64 local x_update = st.stanza("x", { xmlns = "vcard-temp:x:update" }); |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
65 local ok, avatar_hash = pep:get_last_item("urn:xmpp:avatar:metadata", true); |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
66 if ok and avatar_hash then |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
67 x_update:text_tag("photo", avatar_hash); |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
68 end |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
69 stanza:add_direct_child(x_update); |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
70 end |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
71 |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
72 module:hook("pre-presence/full", inject_xep153, 1); |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
73 module:hook("pre-presence/bare", inject_xep153, 1); |
9a8006f9e983
mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents:
9249
diff
changeset
|
74 module:hook("pre-presence/host", inject_xep153, 1); |