mod_pep_vcard_png_avatar/mod_pep_vcard_png_avatar.lua
author Michel Le Bihan <michel@lebihan.pl>
Mon, 13 Feb 2017 21:50:53 +0100
changeset 2500 e6a3bdbce7b9
parent 2221 7be1ca7a51a4
child 3226 c22b6283d226
permissions -rw-r--r--
mod_pep_vcard_png_avatar: Move to dwebp, handle errors on opening file_png.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2220
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
     1
-- Prosody IM
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
     2
-- Copyright (C) 2008-2014 Matthew Wild
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
     3
-- Copyright (C) 2008-2014 Waqas Hussain
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
     4
-- Copyright (C) 2014 Kim Alvefur
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
     5
--
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
     6
-- This project is MIT/X11 licensed. Please see the
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
     7
-- COPYING file in the source package for more information.
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
     8
--
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
     9
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    10
local st = require "util.stanza"
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    11
local jid = require "util.jid";
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    12
local base64 = require"util.encodings".base64;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    13
local sha1 = require"util.hashes".sha1;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    14
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    15
local mod_pep = module:depends"pep";
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    16
local pep_data = mod_pep.module.save().data;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    17
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    18
module:add_feature("http://prosody.im/protocol/vcard-pep-integration");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    19
module:depends"vcard";
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    20
local vcard_storage = module:open_store("vcard");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    21
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    22
local function get_vcard(username)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    23
	local vcard, err = vcard_storage:get(username);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    24
	if vcard then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    25
		vcard = st.deserialize(vcard);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    26
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    27
	if not vcard then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    28
		vcard = st.stanza("vCard", { xmlns = "vcard-temp" });
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    29
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    30
	return vcard, err;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    31
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    32
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    33
local function replace_tag(s, replacement)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    34
	local once = false;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    35
	s:maptags(function (tag)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    36
		if tag.name == replacement.name and tag.attr.xmlns == replacement.attr.xmlns then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    37
			if not once then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    38
				once = true;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    39
				return replacement;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    40
			else
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    41
				return nil;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    42
			end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    43
		end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    44
		return tag;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    45
	end);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    46
	if not once then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    47
		s:add_child(replacement);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    48
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    49
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    50
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    51
local function set_vcard(username, vcard)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    52
	if vcard then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    53
		vcard = st.preserialize(st.clone(vcard));
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    54
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    55
	return vcard_storage:set(username, vcard);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    56
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    57
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    58
local function publish(session, node, id, item)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    59
	return module:fire_event("pep-publish-item", {
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    60
		actor = true, user = jid.bare(session.full_jid), session = session, node = node, id = id, item = item;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    61
	});
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    62
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    63
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    64
-- vCard -> PEP
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    65
local function update_pep(session, vcard)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    66
	if not vcard then return end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    67
	local nickname = vcard:get_child_text("NICKNAME");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    68
	if nickname then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    69
		publish(session, "http://jabber.org/protocol/nick", "current", st.stanza("item", {id="current"})
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    70
			:tag("nick", { xmlns="http://jabber.org/protocol/nick" }):text(nickname));
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    71
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    72
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    73
	local photo = vcard:get_child("PHOTO");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    74
	if photo then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    75
		local photo_type = photo:get_child_text("TYPE");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    76
		local photo_b64 = photo:get_child_text("BINVAL");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    77
		local photo_raw = photo_b64 and base64.decode(photo_b64);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    78
		if photo_raw and photo_type then -- Else invalid data or encoding
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    79
			local photo_hash = sha1(photo_raw, true);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    80
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    81
			publish(session, "urn:xmpp:avatar:data", photo_hash, st.stanza("item", {id=photo_hash})
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    82
				:tag("data", { xmlns="urn:xmpp:avatar:data" }):text(photo_b64));
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    83
			publish(session, "urn:xmpp:avatar:metadata", photo_hash, st.stanza("item", {id=photo_hash})
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    84
				:tag("metadata", { xmlns="urn:xmpp:avatar:metadata" })
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    85
					:tag("info", { id = photo_hash, bytes = tostring(#photo_raw), type = photo_type,}));
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    86
		end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    87
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    88
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    89
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    90
local function handle_vcard(event)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    91
	local session, stanza = event.origin, event.stanza;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    92
	if not stanza.attr.to and stanza.attr.type == "set" then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    93
		return update_pep(session, stanza:get_child("vCard", "vcard-temp"));
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    94
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    95
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    96
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    97
module:hook("iq/bare/vcard-temp:vCard", handle_vcard, 1);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    98
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    99
-- PEP Avatar -> vCard
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   100
local function on_publish_metadata(event)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   101
	local username = event.session.username;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   102
	local metadata = event.item:find("{urn:xmpp:avatar:metadata}metadata/info");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   103
	if not metadata then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   104
		module:log("error", "No info found");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   105
		module:log("debug", event.item:top_tag());
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   106
		return;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   107
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   108
	module:log("debug", metadata:top_tag());
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   109
	local user_data = pep_data[username.."@"..module.host];
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   110
	local pep_photo = user_data["urn:xmpp:avatar:data"];
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   111
	pep_photo = pep_photo and pep_photo[1] == metadata.attr.id and pep_photo[2];
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   112
	if not pep_photo then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   113
		module:log("error", "No photo found");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   114
		return;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   115
	end -- Publishing in the wrong order?
2221
7be1ca7a51a4 mod_pep_vcard_png_avatar: Fix Lua warnings
Michel Le Bihan <michel@lebihan.pl>
parents: 2220
diff changeset
   116
	local image=pep_photo:get_child_text("data", "urn:xmpp:avatar:data");
2220
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   117
	if pep_photo and metadata.attr.type == "image/webp" then
2221
7be1ca7a51a4 mod_pep_vcard_png_avatar: Fix Lua warnings
Michel Le Bihan <michel@lebihan.pl>
parents: 2220
diff changeset
   118
		local file_webp = io.open("/tmp/Prosody_temp_avatar.webp", "w");
2220
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   119
		file_webp:write(base64.decode(pep_photo:get_child_text("data", "urn:xmpp:avatar:data")));
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   120
		file_webp:close();
2500
e6a3bdbce7b9 mod_pep_vcard_png_avatar: Move to dwebp, handle errors on opening file_png.
Michel Le Bihan <michel@lebihan.pl>
parents: 2221
diff changeset
   121
		os.execute("dwebp /tmp/Prosody_temp_avatar.webp -o /tmp/Prosody_temp_avatar.png");
2221
7be1ca7a51a4 mod_pep_vcard_png_avatar: Fix Lua warnings
Michel Le Bihan <michel@lebihan.pl>
parents: 2220
diff changeset
   122
		local file_png = io.open("/tmp/Prosody_temp_avatar.png", "r");
2500
e6a3bdbce7b9 mod_pep_vcard_png_avatar: Move to dwebp, handle errors on opening file_png.
Michel Le Bihan <michel@lebihan.pl>
parents: 2221
diff changeset
   123
		if file_png ~= nil then
e6a3bdbce7b9 mod_pep_vcard_png_avatar: Move to dwebp, handle errors on opening file_png.
Michel Le Bihan <michel@lebihan.pl>
parents: 2221
diff changeset
   124
			image=base64.encode(file_png:read("*a"));
e6a3bdbce7b9 mod_pep_vcard_png_avatar: Move to dwebp, handle errors on opening file_png.
Michel Le Bihan <michel@lebihan.pl>
parents: 2221
diff changeset
   125
			file_png:close();
e6a3bdbce7b9 mod_pep_vcard_png_avatar: Move to dwebp, handle errors on opening file_png.
Michel Le Bihan <michel@lebihan.pl>
parents: 2221
diff changeset
   126
		else
e6a3bdbce7b9 mod_pep_vcard_png_avatar: Move to dwebp, handle errors on opening file_png.
Michel Le Bihan <michel@lebihan.pl>
parents: 2221
diff changeset
   127
			module:log("error", "Couldn't access /tmp/Prosody_temp_avatar.png. Are you sure that /tmp is readable and writable and that Prosody can execute the dwebp command?");
e6a3bdbce7b9 mod_pep_vcard_png_avatar: Move to dwebp, handle errors on opening file_png.
Michel Le Bihan <michel@lebihan.pl>
parents: 2221
diff changeset
   128
		end
2220
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   129
		os.remove("/tmp/Prosody_temp_avatar.webp");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   130
		os.remove("/tmp/Prosody_temp_avatar.png");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   131
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   132
	local vcard = get_vcard(username);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   133
	local new_photo = st.stanza("PHOTO", { xmlns = "vcard-temp" })
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   134
		:tag("TYPE"):text(metadata.attr.type):up()
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   135
		:tag("BINVAL"):text(image);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   136
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   137
	replace_tag(vcard, new_photo);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   138
	set_vcard(username, vcard);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   139
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   140
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   141
-- PEP Nickname -> vCard
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   142
local function on_publish_nick(event)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   143
	local username = event.session.username;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   144
	local vcard = get_vcard(username);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   145
	local new_nick = st.stanza("NICKNAME", { xmlns = "vcard-temp" })
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   146
		:text(event.item:get_child_text("nick", "http://jabber.org/protocol/nick"));
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   147
	replace_tag(vcard, new_nick);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   148
	set_vcard(username, vcard);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   149
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   150
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   151
local function on_publish(event)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   152
	if event.actor == true then return end -- Not from a client
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   153
	local node = event.node;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   154
	if node == "urn:xmpp:avatar:metadata" then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   155
		return on_publish_metadata(event);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   156
	elseif node == "http://jabber.org/protocol/nick" then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   157
		return on_publish_nick(event);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   158
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   159
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   160
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   161
module:hook("pep-publish-item", on_publish, 1);