Annotate

mod_pep_vcard_avatar/mod_pep_vcard_avatar.lua @ 4746:88f5e12c8351

mod_rest: Imply type=set for archive queries This is what makes GET /archive/{to} do the query instead of retrieve the form only.
author Kim Alvefur <zash@zash.se>
date Thu, 04 Nov 2021 20:17:07 +0100
parent 3221:b98c7c33550e
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
3221
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
16 local mm = require "core.modulemanager";
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
17
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
18 -- COMPAT w/trunk
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
19 local pep_module_name = "pep";
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
20 if mm.get_modules_for_host then
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
21 if mm.get_modules_for_host(module.host):contains("pep_simple") then
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
22 pep_module_name = "pep_simple";
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
23 end
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
24 end
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
25
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
26 local mod_pep = module:depends(pep_module_name);
1591
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 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
28
3221
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
29 if not pep_data then
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
30 module:log("error", "This module is not compatible with your version of mod_pep");
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
31 if mm.get_modules_for_host then
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
32 module:log("error", "Please use mod_pep_simple instead of mod_pep to continue using this module");
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
33 end
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
34 return false;
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
35 end
b98c7c33550e mod_pep_vcard_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 3177
diff changeset
36
3177
4eff14ac6853 mod_pep_vcard_avatar: Advertise XEP-0398 feature
Kim Alvefur <zash@zash.se>
parents: 2864
diff changeset
37 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
38 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
39 end);
4eff14ac6853 mod_pep_vcard_avatar: Advertise XEP-0398 feature
Kim Alvefur <zash@zash.se>
parents: 2864
diff changeset
40
1591
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 module:depends"vcard";
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 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
43
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 local function get_vcard(username)
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 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
46 if vcard then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 vcard = st.deserialize(vcard);
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 if not vcard then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 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
51 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 return vcard, err;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54
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
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68 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
69 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
70 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
71 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
72
1591
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 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
74 if vcard then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
75 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
76 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 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
78 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 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
81 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
82 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
83 });
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 -- vCard -> PEP
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 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
88 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
89 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
90 if nickname then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 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
92 :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
93 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94
2149
d9e91240a2dd mod_pep_vcard_avatar: Return early if no vcard is given
Kim Alvefur <zash@zash.se>
parents: 1591
diff changeset
95 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
96 if photo then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 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
98 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
99 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
100 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
101 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
102
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 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
104 :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
105 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
106 :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
107 :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
108 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
109 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 local function handle_vcard(event)
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 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
114 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
115 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
116 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 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
120
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 -- PEP Avatar -> vCard
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 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
123 local username = event.session.username;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 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
125 if not metadata then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 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
127 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
128 return;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
129 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
130 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
131 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
132 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
133 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
134 if not pep_photo then
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
135 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
136 return;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
137 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
138 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
139 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
140 :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
141 :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
142
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
143 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
144 set_vcard(username, vcard);
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 -- PEP Nickname -> vCard
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
148 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
149 local username = event.session.username;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
150 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
151 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
152 :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
153 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
154 set_vcard(username, vcard);
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
155 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
156
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
157 local function on_publish(event)
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
158 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
159 local node = event.node;
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
160 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
161 return on_publish_metadata(event);
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 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
163 return on_publish_nick(event);
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 end
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166
21e439ca7208 mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 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
168
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
169 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
170 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
171 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
172 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
173 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
174 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
175 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
176 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
177 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
178 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
179 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
180 return ""
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
181 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
182
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 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
184 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
185 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
186 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
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 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
189 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
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 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
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 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
195 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
196 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
197 return
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
198 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
199 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
200 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
201 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
202 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
203 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
204 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
205 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
206 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
207 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
208 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
209 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
210 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
211 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
212 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
213 end
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
214
933049a60ce6 mod_pep_vcard_avatar: annouce avatar hash in presence if client does not
Daniel Gultsch <daniel@gultsch.de>
parents: 2206
diff changeset
215 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
216 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
217 module:hook("pre-presence/host", on_send_presence)