mod_pep_vcard_png_avatar/mod_pep_vcard_png_avatar.lua
author Matthew Wild <mwild1@gmail.com>
Sat, 24 Sep 2022 08:06:11 +0100
changeset 5060 2583bd7eb5d1
parent 3226 c22b6283d226
permissions -rw-r--r--
mod_cloud_notify: Add warnings about options including real body/sender
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
3226
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    15
local mm = require "core.modulemanager";
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    16
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    17
-- COMPAT w/trunk
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    18
local pep_module_name = "pep";
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    19
if mm.get_modules_for_host then
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    20
	if mm.get_modules_for_host(module.host):contains("pep_simple") then
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    21
		pep_module_name = "pep_simple";
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    22
	end
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    23
end
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    24
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    25
local mod_pep = module:depends(pep_module_name);
2220
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    26
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
    27
3226
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    28
if not pep_data then
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    29
	module:log("error", "This module is not compatible with your version of mod_pep");
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    30
	if mm.get_modules_for_host then
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    31
		module:log("error", "Please use mod_pep_simple instead of mod_pep to continue using this module");
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    32
	end
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    33
	return false;
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    34
end
c22b6283d226 mod_pep_vcard_png_avatar: Log error when used with new mod_pep, add compat with mod_pep_simple
Matthew Wild <mwild1@gmail.com>
parents: 2500
diff changeset
    35
2220
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    36
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
    37
module:depends"vcard";
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    38
local vcard_storage = module:open_store("vcard");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    39
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    40
local function get_vcard(username)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    41
	local vcard, err = vcard_storage:get(username);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    42
	if vcard then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    43
		vcard = st.deserialize(vcard);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    44
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    45
	if not vcard then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    46
		vcard = st.stanza("vCard", { xmlns = "vcard-temp" });
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    47
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    48
	return vcard, err;
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 replace_tag(s, replacement)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    52
	local once = false;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    53
	s:maptags(function (tag)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    54
		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
    55
			if not once then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    56
				once = true;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    57
				return replacement;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    58
			else
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    59
				return nil;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    60
			end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    61
		end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    62
		return tag;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    63
	end);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    64
	if not once then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    65
		s:add_child(replacement);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    66
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    67
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    68
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    69
local function set_vcard(username, vcard)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    70
	if vcard then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    71
		vcard = st.preserialize(st.clone(vcard));
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    72
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    73
	return vcard_storage:set(username, vcard);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    74
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    75
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    76
local function publish(session, node, id, item)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    77
	return module:fire_event("pep-publish-item", {
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    78
		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
    79
	});
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    80
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    81
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    82
-- vCard -> PEP
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    83
local function update_pep(session, vcard)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    84
	if not vcard then return end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    85
	local nickname = vcard:get_child_text("NICKNAME");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    86
	if nickname then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    87
		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
    88
			: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
    89
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    90
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    91
	local photo = vcard:get_child("PHOTO");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    92
	if photo then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
    93
		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
    94
		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
    95
		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
    96
		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
    97
			local photo_hash = sha1(photo_raw, true);
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
			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
   100
				: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
   101
			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
   102
				:tag("metadata", { xmlns="urn:xmpp:avatar:metadata" })
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   103
					: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
   104
		end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   105
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   106
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   107
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   108
local function handle_vcard(event)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   109
	local session, stanza = event.origin, event.stanza;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   110
	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
   111
		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
   112
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   113
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   114
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   115
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
   116
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   117
-- PEP Avatar -> vCard
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   118
local function on_publish_metadata(event)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   119
	local username = event.session.username;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   120
	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
   121
	if not metadata then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   122
		module:log("error", "No info found");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   123
		module:log("debug", event.item:top_tag());
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   124
		return;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   125
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   126
	module:log("debug", metadata:top_tag());
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   127
	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
   128
	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
   129
	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
   130
	if not pep_photo then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   131
		module:log("error", "No photo found");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   132
		return;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   133
	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
   134
	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
   135
	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
   136
		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
   137
		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
   138
		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
   139
		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
   140
		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
   141
		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
   142
			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
   143
			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
   144
		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
   145
			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
   146
		end
2220
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   147
		os.remove("/tmp/Prosody_temp_avatar.webp");
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   148
		os.remove("/tmp/Prosody_temp_avatar.png");
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
	local vcard = get_vcard(username);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   151
	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
   152
		:tag("TYPE"):text(metadata.attr.type):up()
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   153
		:tag("BINVAL"):text(image);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   154
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   155
	replace_tag(vcard, new_photo);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   156
	set_vcard(username, vcard);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   157
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   158
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   159
-- PEP Nickname -> vCard
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   160
local function on_publish_nick(event)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   161
	local username = event.session.username;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   162
	local vcard = get_vcard(username);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   163
	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
   164
		: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
   165
	replace_tag(vcard, new_nick);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   166
	set_vcard(username, vcard);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   167
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   168
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   169
local function on_publish(event)
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   170
	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
   171
	local node = event.node;
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   172
	if node == "urn:xmpp:avatar:metadata" then
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   173
		return on_publish_metadata(event);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   174
	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
   175
		return on_publish_nick(event);
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   176
	end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   177
end
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   178
7f36ec9c836e mod_pep_vcard_png_avatar: Initial commit
Michel Le Bihan <michel@lebihan.pl>
parents:
diff changeset
   179
module:hook("pep-publish-item", on_publish, 1);