Mercurial > prosody-modules
annotate mod_pep_vcard_avatar/mod_pep_vcard_avatar.lua @ 3181:1f2272cda0d7
mod_profile: Ignore shadowed variable name
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 12 Jul 2018 21:09:41 +0200 |
parents | 4eff14ac6853 |
children | b98c7c33550e |
rev | line source |
---|---|
1591
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- Prosody IM |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- Copyright (C) 2008-2014 Matthew Wild |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- Copyright (C) 2008-2014 Waqas Hussain |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- Copyright (C) 2014 Kim Alvefur |
2862
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
5 -- Copyright (c) 2018 Daniel Gultsch |
1591
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 -- |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 -- This project is MIT/X11 licensed. Please see the |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 -- COPYING file in the source package for more information. |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 -- |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local st = require "util.stanza" |
2206
724809023c92
mod_pep_vcard_avatar: Include user field in event
Kim Alvefur <zash@zash.se>
parents:
2151
diff
changeset
|
12 local jid = require "util.jid"; |
1591
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local base64 = require"util.encodings".base64; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local sha1 = require"util.hashes".sha1; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local mod_pep = module:depends"pep"; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local pep_data = mod_pep.module.save().data; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 |
3177
4eff14ac6853
mod_pep_vcard_avatar: Advertise XEP-0398 feature
Kim Alvefur <zash@zash.se>
parents:
2864
diff
changeset
|
19 module:hook("account-disco-info", function (event) |
4eff14ac6853
mod_pep_vcard_avatar: Advertise XEP-0398 feature
Kim Alvefur <zash@zash.se>
parents:
2864
diff
changeset
|
20 event.reply:tag("feature", { var = "urn:xmpp:pep-vcard-conversion:0" }):up(); |
4eff14ac6853
mod_pep_vcard_avatar: Advertise XEP-0398 feature
Kim Alvefur <zash@zash.se>
parents:
2864
diff
changeset
|
21 end); |
4eff14ac6853
mod_pep_vcard_avatar: Advertise XEP-0398 feature
Kim Alvefur <zash@zash.se>
parents:
2864
diff
changeset
|
22 |
1591
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 module:depends"vcard"; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 local vcard_storage = module:open_store("vcard"); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 local function get_vcard(username) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 local vcard, err = vcard_storage:get(username); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 if vcard then |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 vcard = st.deserialize(vcard); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 if not vcard then |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 vcard = st.stanza("vCard", { xmlns = "vcard-temp" }); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 return vcard, err; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 |
2150
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
37 local function replace_tag(s, replacement) |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
38 local once = false; |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
39 s:maptags(function (tag) |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
40 if tag.name == replacement.name and tag.attr.xmlns == replacement.attr.xmlns then |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
41 if not once then |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
42 once = true; |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
43 return replacement; |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
44 else |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
45 return nil; |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
46 end |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
47 end |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
48 return tag; |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
49 end); |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
50 if not once then |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
51 s:add_child(replacement); |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
52 end |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
53 end |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
54 |
1591
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 local function set_vcard(username, vcard) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 if vcard then |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 vcard = st.preserialize(st.clone(vcard)); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 return vcard_storage:set(username, vcard); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 local function publish(session, node, id, item) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 return module:fire_event("pep-publish-item", { |
2206
724809023c92
mod_pep_vcard_avatar: Include user field in event
Kim Alvefur <zash@zash.se>
parents:
2151
diff
changeset
|
64 actor = true, user = jid.bare(session.full_jid), session = session, node = node, id = id, item = item; |
1591
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 }); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 -- vCard -> PEP |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 local function update_pep(session, vcard) |
2149
d9e91240a2dd
mod_pep_vcard_avatar: Return early if no vcard is given
Kim Alvefur <zash@zash.se>
parents:
1591
diff
changeset
|
70 if not vcard then return end |
d9e91240a2dd
mod_pep_vcard_avatar: Return early if no vcard is given
Kim Alvefur <zash@zash.se>
parents:
1591
diff
changeset
|
71 local nickname = vcard:get_child_text("NICKNAME"); |
1591
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 if nickname then |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 publish(session, "http://jabber.org/protocol/nick", "current", st.stanza("item", {id="current"}) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 :tag("nick", { xmlns="http://jabber.org/protocol/nick" }):text(nickname)); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 |
2149
d9e91240a2dd
mod_pep_vcard_avatar: Return early if no vcard is given
Kim Alvefur <zash@zash.se>
parents:
1591
diff
changeset
|
77 local photo = vcard:get_child("PHOTO"); |
1591
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 if photo then |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 local photo_type = photo:get_child_text("TYPE"); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 local photo_b64 = photo:get_child_text("BINVAL"); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 local photo_raw = photo_b64 and base64.decode(photo_b64); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 if photo_raw and photo_type then -- Else invalid data or encoding |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 local photo_hash = sha1(photo_raw, true); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 publish(session, "urn:xmpp:avatar:data", photo_hash, st.stanza("item", {id=photo_hash}) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 :tag("data", { xmlns="urn:xmpp:avatar:data" }):text(photo_b64)); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 publish(session, "urn:xmpp:avatar:metadata", photo_hash, st.stanza("item", {id=photo_hash}) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 :tag("metadata", { xmlns="urn:xmpp:avatar:metadata" }) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 :tag("info", { id = photo_hash, bytes = tostring(#photo_raw), type = photo_type,})); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 local function handle_vcard(event) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 local session, stanza = event.origin, event.stanza; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 if not stanza.attr.to and stanza.attr.type == "set" then |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 return update_pep(session, stanza:get_child("vCard", "vcard-temp")); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 module:hook("iq/bare/vcard-temp:vCard", handle_vcard, 1); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 -- PEP Avatar -> vCard |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 local function on_publish_metadata(event) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 local username = event.session.username; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 local metadata = event.item:find("{urn:xmpp:avatar:metadata}metadata/info"); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 if not metadata then |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 module:log("error", "No info found"); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 module:log("debug", event.item:top_tag()); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 return; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 module:log("debug", metadata:top_tag()); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 local user_data = pep_data[username.."@"..module.host]; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 local pep_photo = user_data["urn:xmpp:avatar:data"]; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 pep_photo = pep_photo and pep_photo[1] == metadata.attr.id and pep_photo[2]; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 if not pep_photo then |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 module:log("error", "No photo found"); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 return; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 end -- Publishing in the wrong order? |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 local vcard = get_vcard(username); |
2150
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
121 local new_photo = st.stanza("PHOTO", { xmlns = "vcard-temp" }) |
2151
96aca307964b
mod_pep_vcard_avatar: Fix typo [luacheck]
Kim Alvefur <zash@zash.se>
parents:
2150
diff
changeset
|
122 :tag("TYPE"):text(metadata.attr.type):up() |
2150
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
123 :tag("BINVAL"):text(pep_photo:get_child_text("data", "urn:xmpp:avatar:data")); |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
124 |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
125 replace_tag(vcard, new_photo); |
1591
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 set_vcard(username, vcard); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
128 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
129 -- PEP Nickname -> vCard |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 local function on_publish_nick(event) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 local username = event.session.username; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 local vcard = get_vcard(username); |
2150
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
133 local new_nick = st.stanza("NICKNAME", { xmlns = "vcard-temp" }) |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
134 :text(event.item:get_child_text("nick", "http://jabber.org/protocol/nick")); |
c1ecc3bc88fa
mod_pep_vcard_avatar: Replace PHOTO and NICKNAME tags instead of modifying them in-place
Kim Alvefur <zash@zash.se>
parents:
2149
diff
changeset
|
135 replace_tag(vcard, new_nick); |
1591
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 set_vcard(username, vcard); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 local function on_publish(event) |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 if event.actor == true then return end -- Not from a client |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 local node = event.node; |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 if node == "urn:xmpp:avatar:metadata" then |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 return on_publish_metadata(event); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 elseif node == "http://jabber.org/protocol/nick" then |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 return on_publish_nick(event); |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 end |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 |
21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 module:hook("pep-publish-item", on_publish, 1); |
2862
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
150 |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
151 local function calculate_avatar_hash(username) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
152 local vcard = get_vcard(username) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
153 local photo = vcard and vcard:get_child("PHOTO") |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
154 if photo then |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
155 local photo_type = photo:get_child_text("TYPE"); |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
156 local photo_b64 = photo:get_child_text("BINVAL"); |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
157 local photo_raw = photo_b64 and base64.decode(photo_b64); |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
158 if photo_raw and photo_type then |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
159 return sha1(photo_raw, true); |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
160 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
161 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
162 return "" |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
163 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
164 |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
165 local function get_avatar_hash(username, host) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
166 local session = prosody.hosts[host] and prosody.hosts[host].sessions[username] |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
167 if not session then |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
168 return nil |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
169 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
170 if not session.avatar_hash then |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
171 session.avatar_hash = calculate_avatar_hash(username) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
172 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
173 return session.avatar_hash |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
174 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
175 |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
176 local function on_send_presence(event) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
177 local stanza, session = event.stanza, event.origin |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
178 if stanza.attr.type then |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
179 return |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
180 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
181 local hash = get_avatar_hash(session.username, session.host) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
182 if hash and hash ~= "" then |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
183 local x_vcard_update = stanza:get_child("x","vcard-temp:x:update") |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
184 if not x_vcard_update then |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
185 x_vcard_update = st.stanza("x",{xmlns="vcard-temp:x:update"}) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
186 stanza:add_child(x_vcard_update) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
187 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
188 local photo = x_vcard_update:get_child("photo") |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
189 if not photo then |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
190 photo = st.stanza("photo") |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
191 photo:text(hash) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
192 x_vcard_update:add_child(photo) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
193 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
194 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
195 end |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
196 |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
197 module:hook("pre-presence/full", on_send_presence) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
198 module:hook("pre-presence/bare", on_send_presence) |
933049a60ce6
mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents:
2206
diff
changeset
|
199 module:hook("pre-presence/host", on_send_presence) |