Software /
code /
prosody-modules
Comparison
mod_pep_vcard_avatar/mod_pep_vcard_avatar.lua @ 1591:21e439ca7208
mod_pep_vcard_avatar: Syncs avatars between PEP and vCard
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 18 Jan 2015 01:21:53 +0100 |
child | 2149:d9e91240a2dd |
comparison
equal
deleted
inserted
replaced
1590:fbb4cd2922a1 | 1591:21e439ca7208 |
---|---|
1 -- Prosody IM | |
2 -- Copyright (C) 2008-2014 Matthew Wild | |
3 -- Copyright (C) 2008-2014 Waqas Hussain | |
4 -- Copyright (C) 2014 Kim Alvefur | |
5 -- | |
6 -- This project is MIT/X11 licensed. Please see the | |
7 -- COPYING file in the source package for more information. | |
8 -- | |
9 | |
10 local st = require "util.stanza" | |
11 local base64 = require"util.encodings".base64; | |
12 local sha1 = require"util.hashes".sha1; | |
13 | |
14 local mod_pep = module:depends"pep"; | |
15 local pep_data = mod_pep.module.save().data; | |
16 | |
17 module:add_feature("http://prosody.im/protocol/vcard-pep-integration"); | |
18 module:depends"vcard"; | |
19 local vcard_storage = module:open_store("vcard"); | |
20 | |
21 local function get_vcard(username) | |
22 local vcard, err = vcard_storage:get(username); | |
23 if vcard then | |
24 vcard = st.deserialize(vcard); | |
25 end | |
26 if not vcard then | |
27 vcard = st.stanza("vCard", { xmlns = "vcard-temp" }); | |
28 end | |
29 return vcard, err; | |
30 end | |
31 | |
32 local function set_vcard(username, vcard) | |
33 if vcard then | |
34 vcard = st.preserialize(st.clone(vcard)); | |
35 end | |
36 return vcard_storage:set(username, vcard); | |
37 end | |
38 | |
39 local function publish(session, node, id, item) | |
40 return module:fire_event("pep-publish-item", { | |
41 actor = true, session = session, node = node, id = id, item = item; | |
42 }); | |
43 end | |
44 | |
45 -- vCard -> PEP | |
46 local function update_pep(session, vcard) | |
47 local nickname = vcard and vcard:get_child_text("NICKNAME"); | |
48 if nickname then | |
49 publish(session, "http://jabber.org/protocol/nick", "current", st.stanza("item", {id="current"}) | |
50 :tag("nick", { xmlns="http://jabber.org/protocol/nick" }):text(nickname)); | |
51 end | |
52 | |
53 local photo = vcard and vcard:get_child("PHOTO"); | |
54 if photo then | |
55 local photo_type = photo:get_child_text("TYPE"); | |
56 local photo_b64 = photo:get_child_text("BINVAL"); | |
57 local photo_raw = photo_b64 and base64.decode(photo_b64); | |
58 if photo_raw and photo_type then -- Else invalid data or encoding | |
59 local photo_hash = sha1(photo_raw, true); | |
60 | |
61 publish(session, "urn:xmpp:avatar:data", photo_hash, st.stanza("item", {id=photo_hash}) | |
62 :tag("data", { xmlns="urn:xmpp:avatar:data" }):text(photo_b64)); | |
63 publish(session, "urn:xmpp:avatar:metadata", photo_hash, st.stanza("item", {id=photo_hash}) | |
64 :tag("metadata", { xmlns="urn:xmpp:avatar:metadata" }) | |
65 :tag("info", { id = photo_hash, bytes = tostring(#photo_raw), type = photo_type,})); | |
66 end | |
67 end | |
68 end | |
69 | |
70 local function handle_vcard(event) | |
71 local session, stanza = event.origin, event.stanza; | |
72 if not stanza.attr.to and stanza.attr.type == "set" then | |
73 return update_pep(session, stanza:get_child("vCard", "vcard-temp")); | |
74 end | |
75 end | |
76 | |
77 module:hook("iq/bare/vcard-temp:vCard", handle_vcard, 1); | |
78 | |
79 -- PEP Avatar -> vCard | |
80 local function on_publish_metadata(event) | |
81 local username = event.session.username; | |
82 local metadata = event.item:find("{urn:xmpp:avatar:metadata}metadata/info"); | |
83 if not metadata then | |
84 module:log("error", "No info found"); | |
85 module:log("debug", event.item:top_tag()); | |
86 return; | |
87 end | |
88 module:log("debug", metadata:top_tag()); | |
89 local user_data = pep_data[username.."@"..module.host]; | |
90 local pep_photo = user_data["urn:xmpp:avatar:data"]; | |
91 pep_photo = pep_photo and pep_photo[1] == metadata.attr.id and pep_photo[2]; | |
92 if not pep_photo then | |
93 module:log("error", "No photo found"); | |
94 return; | |
95 end -- Publishing in the wrong order? | |
96 local vcard = get_vcard(username); | |
97 local old_photo = vcard:get_child("PHOTO"); | |
98 if old_photo then | |
99 local photo_type = old_photo:get_child("TYPE"); | |
100 photo_type[1] = metadata.attr.type; | |
101 local photo_b64 = old_photo:get_child("BINVAL"); | |
102 photo_b64[1] = pep_photo:get_child_text("data", "urn:xmpp:avatar:data"); | |
103 end | |
104 set_vcard(username, vcard); | |
105 end | |
106 | |
107 -- PEP Nickname -> vCard | |
108 local function on_publish_nick(event) | |
109 local username = event.session.username; | |
110 local vcard = get_vcard(username); | |
111 local new_nick = event.item:get_child_text("nick", "http://jabber.org/protocol/nick"); | |
112 local old_nick = vcard:get_child("NICKNAME"); | |
113 if not old_nick then | |
114 vcard:tag("NICKNAME"):text(new_nick); | |
115 elseif old_nick[1] ~= new_nick then | |
116 old_nick[1] = new_nick; | |
117 else | |
118 return -- no change | |
119 end | |
120 set_vcard(username, vcard); | |
121 end | |
122 | |
123 local function on_publish(event) | |
124 if event.actor == true then return end -- Not from a client | |
125 local node = event.node; | |
126 if node == "urn:xmpp:avatar:metadata" then | |
127 return on_publish_metadata(event); | |
128 elseif node == "http://jabber.org/protocol/nick" then | |
129 return on_publish_nick(event); | |
130 end | |
131 end | |
132 | |
133 module:hook("pep-publish-item", on_publish, 1); |