comparison mod_pep_vcard_png_avatar/mod_pep_vcard_png_avatar.lua @ 2216:7f36ec9c836e

mod_pep_vcard_png_avatar: Initial commit
author Michel Le Bihan <michel@lebihan.pl>
date Fri, 24 Jun 2016 19:16:01 +0200
parents
children 7be1ca7a51a4
comparison
equal deleted inserted replaced
2215:e276ed33bc1a 2216:7f36ec9c836e
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 jid = require "util.jid";
12 local base64 = require"util.encodings".base64;
13 local sha1 = require"util.hashes".sha1;
14
15 local mod_pep = module:depends"pep";
16 local pep_data = mod_pep.module.save().data;
17
18 module:add_feature("http://prosody.im/protocol/vcard-pep-integration");
19 module:depends"vcard";
20 local vcard_storage = module:open_store("vcard");
21
22 local function get_vcard(username)
23 local vcard, err = vcard_storage:get(username);
24 if vcard then
25 vcard = st.deserialize(vcard);
26 end
27 if not vcard then
28 vcard = st.stanza("vCard", { xmlns = "vcard-temp" });
29 end
30 return vcard, err;
31 end
32
33 local function replace_tag(s, replacement)
34 local once = false;
35 s:maptags(function (tag)
36 if tag.name == replacement.name and tag.attr.xmlns == replacement.attr.xmlns then
37 if not once then
38 once = true;
39 return replacement;
40 else
41 return nil;
42 end
43 end
44 return tag;
45 end);
46 if not once then
47 s:add_child(replacement);
48 end
49 end
50
51 local function set_vcard(username, vcard)
52 if vcard then
53 vcard = st.preserialize(st.clone(vcard));
54 end
55 return vcard_storage:set(username, vcard);
56 end
57
58 local function publish(session, node, id, item)
59 return module:fire_event("pep-publish-item", {
60 actor = true, user = jid.bare(session.full_jid), session = session, node = node, id = id, item = item;
61 });
62 end
63
64 -- vCard -> PEP
65 local function update_pep(session, vcard)
66 if not vcard then return end
67 local nickname = vcard:get_child_text("NICKNAME");
68 if nickname then
69 publish(session, "http://jabber.org/protocol/nick", "current", st.stanza("item", {id="current"})
70 :tag("nick", { xmlns="http://jabber.org/protocol/nick" }):text(nickname));
71 end
72
73 local photo = vcard:get_child("PHOTO");
74 if photo then
75 local photo_type = photo:get_child_text("TYPE");
76 local photo_b64 = photo:get_child_text("BINVAL");
77 local photo_raw = photo_b64 and base64.decode(photo_b64);
78 if photo_raw and photo_type then -- Else invalid data or encoding
79 local photo_hash = sha1(photo_raw, true);
80
81 publish(session, "urn:xmpp:avatar:data", photo_hash, st.stanza("item", {id=photo_hash})
82 :tag("data", { xmlns="urn:xmpp:avatar:data" }):text(photo_b64));
83 publish(session, "urn:xmpp:avatar:metadata", photo_hash, st.stanza("item", {id=photo_hash})
84 :tag("metadata", { xmlns="urn:xmpp:avatar:metadata" })
85 :tag("info", { id = photo_hash, bytes = tostring(#photo_raw), type = photo_type,}));
86 end
87 end
88 end
89
90 local function handle_vcard(event)
91 local session, stanza = event.origin, event.stanza;
92 if not stanza.attr.to and stanza.attr.type == "set" then
93 return update_pep(session, stanza:get_child("vCard", "vcard-temp"));
94 end
95 end
96
97 module:hook("iq/bare/vcard-temp:vCard", handle_vcard, 1);
98
99 -- PEP Avatar -> vCard
100 local function on_publish_metadata(event)
101 local username = event.session.username;
102 local metadata = event.item:find("{urn:xmpp:avatar:metadata}metadata/info");
103 if not metadata then
104 module:log("error", "No info found");
105 module:log("debug", event.item:top_tag());
106 return;
107 end
108 module:log("debug", metadata:top_tag());
109 local user_data = pep_data[username.."@"..module.host];
110 local pep_photo = user_data["urn:xmpp:avatar:data"];
111 pep_photo = pep_photo and pep_photo[1] == metadata.attr.id and pep_photo[2];
112 if not pep_photo then
113 module:log("error", "No photo found");
114 return;
115 end -- Publishing in the wrong order?
116 if pep_photo and metadata.attr.type == "image/webp" then
117 file_webp = io.open("/tmp/Prosody_temp_avatar.webp", "w");
118 file_webp:write(base64.decode(pep_photo:get_child_text("data", "urn:xmpp:avatar:data")));
119 file_webp:close();
120 os.execute("convert /tmp/Prosody_temp_avatar.webp /tmp/Prosody_temp_avatar.png");
121 file_png = io.open("/tmp/Prosody_temp_avatar.png", "r");
122 image=base64.encode(file_png:read("*a"));
123 file_png:close();
124 os.remove("/tmp/Prosody_temp_avatar.webp");
125 os.remove("/tmp/Prosody_temp_avatar.png");
126 else
127 image=pep_photo:get_child_text("data", "urn:xmpp:avatar:data");
128 end
129 local vcard = get_vcard(username);
130 local new_photo = st.stanza("PHOTO", { xmlns = "vcard-temp" })
131 :tag("TYPE"):text(metadata.attr.type):up()
132 :tag("BINVAL"):text(image);
133
134 replace_tag(vcard, new_photo);
135 set_vcard(username, vcard);
136 end
137
138 -- PEP Nickname -> vCard
139 local function on_publish_nick(event)
140 local username = event.session.username;
141 local vcard = get_vcard(username);
142 local new_nick = st.stanza("NICKNAME", { xmlns = "vcard-temp" })
143 :text(event.item:get_child_text("nick", "http://jabber.org/protocol/nick"));
144 replace_tag(vcard, new_nick);
145 set_vcard(username, vcard);
146 end
147
148 local function on_publish(event)
149 if event.actor == true then return end -- Not from a client
150 local node = event.node;
151 if node == "urn:xmpp:avatar:metadata" then
152 return on_publish_metadata(event);
153 elseif node == "http://jabber.org/protocol/nick" then
154 return on_publish_nick(event);
155 end
156 end
157
158 module:hook("pep-publish-item", on_publish, 1);