plugins/mod_vcard_legacy.lua
author Kim Alvefur <zash@zash.se>
Sat, 01 Sep 2018 22:06:25 +0200
changeset 9263 0fc6ffc57dc0
parent 9262 926a88dd5c54
child 9270 37e2cace1f2a
permissions -rw-r--r--
mod_vcard_legacy: Announce feature on bare JID per XEP
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
9252
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
local st = require "util.stanza"
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
local jid_split = require "util.jid".split;
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
local mod_pep = module:depends("pep");
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
9258
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
     6
local sha1 = require "util.hashes".sha1;
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
     7
local base64_decode = require "util.encodings".base64.decode;
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
     8
9259
12d3d96e3918 mod_vcard_legacy: Respond with old vcard
Kim Alvefur <zash@zash.se>
parents: 9258
diff changeset
     9
local vcards = module:open_store("vcard");
12d3d96e3918 mod_vcard_legacy: Respond with old vcard
Kim Alvefur <zash@zash.se>
parents: 9258
diff changeset
    10
9252
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
module:add_feature("vcard-temp");
9263
0fc6ffc57dc0 mod_vcard_legacy: Announce feature on bare JID per XEP
Kim Alvefur <zash@zash.se>
parents: 9262
diff changeset
    12
module:hook("account-disco-info", function (event)
0fc6ffc57dc0 mod_vcard_legacy: Announce feature on bare JID per XEP
Kim Alvefur <zash@zash.se>
parents: 9262
diff changeset
    13
	event.reply:tag("feature", { var = "urn:xmpp:pep-vcard-conversion:0" }):up();
0fc6ffc57dc0 mod_vcard_legacy: Announce feature on bare JID per XEP
Kim Alvefur <zash@zash.se>
parents: 9262
diff changeset
    14
end);
9252
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
-- Simple translations
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
-- <foo><text>hey</text></foo> -> <FOO>hey</FOO>
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
local simple_map = {
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
	nickname = "text";
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
	title = "text";
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
	role = "text";
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
	categories = "text";
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
	note = "text";
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
	url = "uri";
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
	bday = "date";
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
}
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
module:hook("iq-get/bare/vcard-temp:vCard", function (event)
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
	local origin, stanza = event.origin, event.stanza;
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
	local pep_service = mod_pep.get_pep_service(jid_split(stanza.attr.to) or origin.username);
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
	local ok, id, vcard4_item = pep_service:get_last_item("urn:xmpp:vcard4", stanza.attr.from);
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
	local vcard_temp = st.stanza("vCard", { xmlns = "vcard-temp" });
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
	if ok and vcard4_item then
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
		local vcard4 = vcard4_item.tags[1];
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
		local fn = vcard4:get_child("fn");
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    38
		vcard_temp:text_tag("FN", fn and fn:get_child_text("text"));
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    39
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    40
		local v4n = vcard4:get_child("n");
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
		vcard_temp:tag("N")
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    42
			:text_tag("FAMILY", v4n and v4n:get_child_text("surname"))
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    43
			:text_tag("GIVEN", v4n and v4n:get_child_text("given"))
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    44
			:text_tag("MIDDLE", v4n and v4n:get_child_text("additional"))
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    45
			:text_tag("PREFIX", v4n and v4n:get_child_text("prefix"))
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    46
			:text_tag("SUFFIX", v4n and v4n:get_child_text("suffix"))
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    47
			:up();
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    49
		for tag in vcard4:childtags() do
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    50
			local typ = simple_map[tag.name];
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    51
			if typ then
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    52
				local text = tag:get_child_text(typ);
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    53
				if text then
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    54
					vcard_temp:text_tag(tag.name:upper(), text);
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    55
				end
9254
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    56
			elseif tag.name == "email" then
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    57
				local text = tag:get_child_text("text");
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    58
				if text then
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    59
					vcard_temp:tag("EMAIL")
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    60
						:text_tag("USERID", text)
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    61
						:tag("INTERNET"):up();
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    62
					if tag:find"parameters/type/text#" == "home" then
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    63
						vcard_temp:tag("HOME"):up();
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    64
					elseif tag:find"parameters/type/text#" == "work" then
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    65
						vcard_temp:tag("WORK"):up();
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    66
					end
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    67
					vcard_temp:up();
3f31e10e8256 mod_vcard_legacy: Add translation of email field
Kim Alvefur <zash@zash.se>
parents: 9253
diff changeset
    68
				end
9255
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    69
			elseif tag.name == "tel" then
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    70
				local text = tag:get_child_text("uri");
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    71
				if text then
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    72
					if text:sub(1, 4) == "tel:" then
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    73
						text = text:sub(5)
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    74
					end
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    75
					vcard_temp:tag("TEL"):text_tag("NUMBER", text);
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    76
					if tag:find"parameters/type/text#" == "home" then
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    77
						vcard_temp:tag("HOME"):up();
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    78
					elseif tag:find"parameters/type/text#" == "work" then
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    79
						vcard_temp:tag("WORK"):up();
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    80
					end
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    81
					vcard_temp:up();
292d283c7694 mod_vcard_legacy: Add translation of telephone field
Kim Alvefur <zash@zash.se>
parents: 9254
diff changeset
    82
				end
9257
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    83
			elseif tag.name == "adr" then
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    84
				vcard_temp:tag("ADR")
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    85
					:text_tag("POBOX", tag:get_child_text("pobox"))
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    86
					:text_tag("EXTADD", tag:get_child_text("ext"))
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    87
					:text_tag("STREET", tag:get_child_text("street"))
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    88
					:text_tag("LOCALITY", tag:get_child_text("locality"))
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    89
					:text_tag("REGION", tag:get_child_text("region"))
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    90
					:text_tag("PCODE", tag:get_child_text("code"))
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    91
					:text_tag("CTRY", tag:get_child_text("country"));
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    92
				if tag:find"parameters/type/text#" == "home" then
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    93
					vcard_temp:tag("HOME"):up();
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    94
				elseif tag:find"parameters/type/text#" == "work" then
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    95
					vcard_temp:tag("WORK"):up();
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    96
				end
2ffbcad8ec50 mod_vcard_legacy: Add support for address field
Kim Alvefur <zash@zash.se>
parents: 9256
diff changeset
    97
				vcard_temp:up();
9252
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    98
			end
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    99
		end
9259
12d3d96e3918 mod_vcard_legacy: Respond with old vcard
Kim Alvefur <zash@zash.se>
parents: 9258
diff changeset
   100
	else
12d3d96e3918 mod_vcard_legacy: Respond with old vcard
Kim Alvefur <zash@zash.se>
parents: 9258
diff changeset
   101
		local legacy_vcard = st.deserialize(vcards:get(jid_split(stanza.attr.to) or origin.username));
12d3d96e3918 mod_vcard_legacy: Respond with old vcard
Kim Alvefur <zash@zash.se>
parents: 9258
diff changeset
   102
		origin.send(st.reply(stanza):add_child(legacy_vcard or vcard_temp));
12d3d96e3918 mod_vcard_legacy: Respond with old vcard
Kim Alvefur <zash@zash.se>
parents: 9258
diff changeset
   103
		return true;
9252
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   104
	end
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   105
9256
dbe3ae6f9746 mod_vcard_legacy: Include avatar data from XEP-0084 PEP node
Kim Alvefur <zash@zash.se>
parents: 9255
diff changeset
   106
	local meta_ok, avatar_meta = pep_service:get_items("urn:xmpp:avatar:metadata", stanza.attr.from);
dbe3ae6f9746 mod_vcard_legacy: Include avatar data from XEP-0084 PEP node
Kim Alvefur <zash@zash.se>
parents: 9255
diff changeset
   107
	local data_ok, avatar_data = pep_service:get_items("urn:xmpp:avatar:data", stanza.attr.from);
dbe3ae6f9746 mod_vcard_legacy: Include avatar data from XEP-0084 PEP node
Kim Alvefur <zash@zash.se>
parents: 9255
diff changeset
   108
	if meta_ok and data_ok then
dbe3ae6f9746 mod_vcard_legacy: Include avatar data from XEP-0084 PEP node
Kim Alvefur <zash@zash.se>
parents: 9255
diff changeset
   109
		for _, hash in ipairs(avatar_meta) do
dbe3ae6f9746 mod_vcard_legacy: Include avatar data from XEP-0084 PEP node
Kim Alvefur <zash@zash.se>
parents: 9255
diff changeset
   110
			local meta = avatar_meta[hash];
dbe3ae6f9746 mod_vcard_legacy: Include avatar data from XEP-0084 PEP node
Kim Alvefur <zash@zash.se>
parents: 9255
diff changeset
   111
			local data = avatar_data[hash];
9260
05bd21c122ae mod_vcard_legacy: Handle incomplete avatar info
Kim Alvefur <zash@zash.se>
parents: 9259
diff changeset
   112
			local info = meta and meta.tags[1]:get_child("info");
05bd21c122ae mod_vcard_legacy: Handle incomplete avatar info
Kim Alvefur <zash@zash.se>
parents: 9259
diff changeset
   113
			vcard_temp:tag("PHOTO");
05bd21c122ae mod_vcard_legacy: Handle incomplete avatar info
Kim Alvefur <zash@zash.se>
parents: 9259
diff changeset
   114
			if info and info.attr.type then
05bd21c122ae mod_vcard_legacy: Handle incomplete avatar info
Kim Alvefur <zash@zash.se>
parents: 9259
diff changeset
   115
				vcard_temp:text_tag("TYPE", info.attr.type);
05bd21c122ae mod_vcard_legacy: Handle incomplete avatar info
Kim Alvefur <zash@zash.se>
parents: 9259
diff changeset
   116
			end
05bd21c122ae mod_vcard_legacy: Handle incomplete avatar info
Kim Alvefur <zash@zash.se>
parents: 9259
diff changeset
   117
			if data then
05bd21c122ae mod_vcard_legacy: Handle incomplete avatar info
Kim Alvefur <zash@zash.se>
parents: 9259
diff changeset
   118
				vcard_temp:text_tag("BINVAL", data.tags[1]:get_text());
9261
0cc535668fce mod_vcard_legacy: Support external avatars
Kim Alvefur <zash@zash.se>
parents: 9260
diff changeset
   119
			elseif info and info.attr.url then
0cc535668fce mod_vcard_legacy: Support external avatars
Kim Alvefur <zash@zash.se>
parents: 9260
diff changeset
   120
				vcard_temp:text_tag("EXTVAL", info.attr.uri);
9260
05bd21c122ae mod_vcard_legacy: Handle incomplete avatar info
Kim Alvefur <zash@zash.se>
parents: 9259
diff changeset
   121
			end
05bd21c122ae mod_vcard_legacy: Handle incomplete avatar info
Kim Alvefur <zash@zash.se>
parents: 9259
diff changeset
   122
			vcard_temp:up();
9256
dbe3ae6f9746 mod_vcard_legacy: Include avatar data from XEP-0084 PEP node
Kim Alvefur <zash@zash.se>
parents: 9255
diff changeset
   123
		end
dbe3ae6f9746 mod_vcard_legacy: Include avatar data from XEP-0084 PEP node
Kim Alvefur <zash@zash.se>
parents: 9255
diff changeset
   124
	end
dbe3ae6f9746 mod_vcard_legacy: Include avatar data from XEP-0084 PEP node
Kim Alvefur <zash@zash.se>
parents: 9255
diff changeset
   125
9252
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   126
	origin.send(st.reply(stanza):add_child(vcard_temp));
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   127
	return true;
506aabdb13bc mod_vcard_legacy: Responds to vcard-temp queries with translated vcard4 data
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   128
end);
9253
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   129
9258
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   130
module:hook("iq-set/self/vcard-temp:vCard", function (event)
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   131
	local origin, stanza = event.origin, event.stanza;
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   132
	local pep_service = mod_pep.get_pep_service(origin.username);
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   133
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   134
	local vcard_temp = stanza.tags[1];
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   135
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   136
	local vcard4 = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = "current" })
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   137
		:tag("vcard", { xmlns = 'urn:ietf:params:xml:ns:vcard-4.0' });
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   138
9262
926a88dd5c54 mod_vcard_legacy: Clear avatars on setting a vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9261
diff changeset
   139
	if pep_service:purge("urn:xmpp:avatar:metadata", origin.full_jid) then
926a88dd5c54 mod_vcard_legacy: Clear avatars on setting a vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9261
diff changeset
   140
		pep_service:publish("urn:xmpp:avatar:data", origin.full_jid);
926a88dd5c54 mod_vcard_legacy: Clear avatars on setting a vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9261
diff changeset
   141
	end
926a88dd5c54 mod_vcard_legacy: Clear avatars on setting a vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9261
diff changeset
   142
9258
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   143
	vcard4:tag("fn"):text_tag("text", vcard_temp:get_child_text("FN")):up();
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   144
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   145
	local N = vcard_temp:get_child("N");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   146
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   147
	vcard4:tag("n")
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   148
		:text_tag("surname", N and N:get_child_text("FAMILY"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   149
		:text_tag("given", N and N:get_child_text("GIVEN"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   150
		:text_tag("additional", N and N:get_child_text("MIDDLe"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   151
		:text_tag("prefix", N and N:get_child_text("PREFIX"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   152
		:text_tag("suffix", N and N:get_child_text("SUFFIX"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   153
	:up();
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   154
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   155
	for tag in vcard_temp:childtags() do
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   156
		local typ = simple_map[tag.name:lower()];
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   157
		if typ then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   158
			local text = tag:get_text();
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   159
			if text then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   160
				vcard4:tag(tag.name:lower()):text_tag(typ, text):up();
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   161
			end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   162
		elseif tag.name == "EMAIL" then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   163
			local text = tag:get_child_text("USERID");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   164
			if text then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   165
				vcard4:tag("email")
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   166
				vcard4:text_tag("text", text)
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   167
				vcard4:tag("parameters"):tag("type");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   168
				if tag:get_child("HOME") then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   169
					vcard4:text_tag("text", "home");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   170
				elseif tag:get_child("WORK") then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   171
					vcard4:text_tag("text", "work");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   172
				end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   173
				vcard4:up():up():up();
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   174
			end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   175
		elseif tag.name == "TEL" then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   176
			local text = tag:get_child_text("NUMBER");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   177
			if text then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   178
				vcard4:tag("tel"):text_tag("uri", "tel:"..text);
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   179
			end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   180
			vcard4:tag("parameters"):tag("type");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   181
			if tag:get_child("HOME") then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   182
				vcard4:text_tag("text", "home");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   183
			elseif tag:get_child("WORK") then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   184
				vcard4:text_tag("text", "work");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   185
			end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   186
			vcard4:up():up():up();
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   187
		elseif tag.name == "ORG" then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   188
			local text = tag:get_child_text("ORGNAME");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   189
			if text then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   190
				vcard4:tag("org"):text_tag("text", text):up();
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   191
			end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   192
		elseif tag.name == "DESC" then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   193
			local text = tag:get_text();
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   194
			if text then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   195
				vcard4:tag("note"):text_tag("text", text):up();
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   196
			end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   197
		elseif tag.name == "ADR" then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   198
			vcard4:tag("adr")
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   199
				:text_tag("pobox", tag:get_child_text("POBOX"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   200
				:text_tag("ext", tag:get_child_text("EXTADD"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   201
				:text_tag("street", tag:get_child_text("STREET"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   202
				:text_tag("locality", tag:get_child_text("LOCALITY"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   203
				:text_tag("region", tag:get_child_text("REGION"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   204
				:text_tag("code", tag:get_child_text("PCODE"))
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   205
				:text_tag("country", tag:get_child_text("CTRY"));
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   206
			vcard4:tag("parameters"):tag("type");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   207
			if tag:get_child("HOME") then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   208
				vcard4:text_tag("text", "home");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   209
			elseif tag:get_child("WORK") then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   210
				vcard4:text_tag("text", "work");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   211
			end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   212
			vcard4:up():up():up();
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   213
		elseif tag.name == "PHOTO" then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   214
			local avatar_type = tag:get_child_text("TYPE");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   215
			local avatar_payload = tag:get_child_text("BINVAL");
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   216
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   217
			if avatar_payload then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   218
				local avatar_raw = base64_decode(avatar_payload);
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   219
				local avatar_hash = sha1(avatar_raw, true);
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   220
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   221
				local avatar_meta = st.stanza("item", { id = avatar_hash, xmlns = "http://jabber.org/protocol/pubsub" })
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   222
					:tag("metadata", { xmlns="urn:xmpp:avatar:metadata" })
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   223
						:tag("info", {
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   224
							bytes = tostring(#avatar_raw),
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   225
							id = avatar_hash,
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   226
							type = avatar_type,
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   227
						});
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   228
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   229
				local avatar_data = st.stanza("item", { id = avatar_hash, xmlns = "http://jabber.org/protocol/pubsub" })
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   230
					:tag("data", { xmlns="urn:xmpp:avatar:data" })
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   231
						:text(avatar_payload);
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   232
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   233
				if pep_service:publish("urn:xmpp:avatar:data", origin.full_jid, avatar_hash, avatar_data) then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   234
					pep_service:publish("urn:xmpp:avatar:metadata", origin.full_jid, avatar_hash, avatar_meta);
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   235
				end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   236
			end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   237
		end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   238
	end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   239
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   240
	local ok, err = pep_service:publish("urn:xmpp:vcard4", origin.full_jid, "current", vcard4);
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   241
	if ok then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   242
		origin.send(st.reply(stanza));
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   243
	elseif err == "forbidden" then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   244
		origin.send(st.error_reply(stanza, "auth", "forbidden"));
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   245
	elseif err == "internal-server-error" then
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   246
		origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   247
	else
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   248
		origin.send(st.error_reply(stanza, "modify", "undefined-condition", err));
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   249
	end
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   250
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   251
	return true;
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   252
end);
2aa40526df7b mod_vcard_legacy: Respond to attempts to set the legacy vcard-temp
Kim Alvefur <zash@zash.se>
parents: 9257
diff changeset
   253
9253
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   254
local function inject_xep153(event)
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   255
	local origin, stanza = event.origin, event.stanza;
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   256
	local username = origin.username;
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   257
	if not username then return end
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   258
	local pep = mod_pep.get_pep_service(username);
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   259
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   260
	stanza:remove_children("x", "vcard-temp:x:update");
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   261
	local x_update = st.stanza("x", { xmlns = "vcard-temp:x:update" });
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   262
	local ok, avatar_hash = pep:get_last_item("urn:xmpp:avatar:metadata", true);
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   263
	if ok and avatar_hash then
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   264
		x_update:text_tag("photo", avatar_hash);
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   265
	end
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   266
	stanza:add_direct_child(x_update);
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   267
end
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   268
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   269
module:hook("pre-presence/full", inject_xep153, 1);
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   270
module:hook("pre-presence/bare", inject_xep153, 1);
9a8006f9e983 mod_vcard_legacy: Attach vcard-temp avatar hash to outgoing presence
Kim Alvefur <zash@zash.se>
parents: 9252
diff changeset
   271
module:hook("pre-presence/host", inject_xep153, 1);