Annotate

mod_pep_vcard_avatar/mod_pep_vcard_avatar.lua @ 3151:a36710acd9c8

mod_pubsub_post: Rename README so it gets picked up by the site generator
author Kim Alvefur <zash@zash.se>
date Wed, 27 Jun 2018 17:17:17 +0200
parent 2864:6f3859233515
child 3177:4eff14ac6853
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 module:add_feature("http://prosody.im/protocol/vcard-pep-integration");
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 module:depends"vcard";
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 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
22
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local function get_vcard(username)
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 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
25 if vcard then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 vcard = st.deserialize(vcard);
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 if not vcard then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 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
30 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 return vcard, err;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33
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
34 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
35 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
36 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
37 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
38 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
39 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
40 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
41 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
42 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
43 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
44 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
45 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
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 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
48 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
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 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
51
1591
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 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
53 if vcard then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 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
55 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 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
57 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 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
60 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
61 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
62 });
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 -- vCard -> PEP
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 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
67 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
68 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
69 if nickname then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 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
71 :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
72 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73
2149
d9e91240a2dd mod_pep_vcard_avatar: Return early if no vcard is given
Kim Alvefur <zash@zash.se>
parents: 1591
diff changeset
74 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
75 if photo then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
76 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
77 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
78 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
79 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
80 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
81
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 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
83 :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
84 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
85 :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
86 :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
87 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 local function handle_vcard(event)
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 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
93 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
94 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
95 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 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
99
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 -- PEP Avatar -> vCard
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 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
102 local username = event.session.username;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 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
104 if not metadata then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 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
106 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
107 return;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
108 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 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
110 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
111 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
112 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
113 if not pep_photo then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114 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
115 return;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 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
117 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
118 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
119 :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
120 :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
121
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
122 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
123 set_vcard(username, vcard);
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
125
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 -- PEP Nickname -> vCard
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 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
128 local username = event.session.username;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 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
130 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
131 :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
132 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
133 set_vcard(username, vcard);
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
134 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
136 local function on_publish(event)
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 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
138 local node = event.node;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139 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
140 return on_publish_metadata(event);
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
141 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
142 return on_publish_nick(event);
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
143 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
144 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 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
147
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
148 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
149 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
150 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
151 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
152 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
153 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
154 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
155 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
156 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
157 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
158 end
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 ""
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
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
162 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
163 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
164 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
165 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
166 end
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.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
168 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
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 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
171 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
172
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
173 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
174 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
175 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
176 return
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
177 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
178 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
179 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
180 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
181 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
182 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
183 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
184 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
185 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
186 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
187 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
188 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
189 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
190 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
191 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
192 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
193
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
194 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
195 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
196 module:hook("pre-presence/host", on_send_presence)