mod_profile/mod_profile.lua
author Kim Alvefur <zash@zash.se>
Sat, 27 Sep 2014 19:11:04 +0200
changeset 1515 f367d7cbfaa6
parent 1503 c4c9ccc6e6c9
child 1516 48d95ab404c7
permissions -rw-r--r--
mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     1
-- mod_profile
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     2
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     3
local st = require"util.stanza";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     4
local jid_split, jid_bare = import("util.jid", "split", "bare");
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     5
local is_admin = require"core.usermanager".is_admin;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     6
local vcard = require"util.vcard";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     7
local base64 = require"util.encodings".base64;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
     8
local sha1 = require"util.hashes".sha1;
1451
843769eb40c3 mod_profile: Don't include photo in vCard4 version (use XEP-0084 instead)
Kim Alvefur <zash@zash.se>
parents: 1442
diff changeset
     9
local t_insert, t_remove = table.insert, table.remove;
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    10
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    11
local pep_plus;
1420
808950ab007b mod_profile: Integrate with mod_pep_plus by default
Kim Alvefur <zash@zash.se>
parents: 1419
diff changeset
    12
if module:get_host_type() == "local" and module:get_option_boolean("vcard_to_pep", true) then
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    13
	pep_plus = module:depends"pep_plus";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    14
end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    15
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    16
local storage = module:open_store();
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    17
local legacy_storage = module:open_store("vcard");
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    18
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    19
local function get_item(vcard, name)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    20
	local item;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    21
	for i=1, #vcard do
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    22
		item=vcard[i];
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    23
		if item.name == name then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    24
			return item, i;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    25
		end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    26
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    27
end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    28
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    29
local magic_mime = {
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    30
	["\137PNG\r\n\026\n"] = "image/png";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    31
	["\255\216"] = "image/jpeg";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    32
	["GIF87a"] = "image/gif";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    33
	["GIF89a"] = "image/gif";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    34
	["<?xml"] = "image/svg+xml";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    35
}
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    36
local function identify(data)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    37
	for magic, mime in pairs(magic_mime) do
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    38
		if data:sub(1, #magic) == magic then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    39
			return mime;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    40
		end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    41
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    42
	return "application/octet-stream";
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    43
end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    44
1515
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
    45
local function update_pep(username, data, pep)
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
    46
	pep = pep or pep_plus.get_pep_service(username.."@"..module.host);
1451
843769eb40c3 mod_profile: Don't include photo in vCard4 version (use XEP-0084 instead)
Kim Alvefur <zash@zash.se>
parents: 1442
diff changeset
    47
	local photo, p = get_item(data, "PHOTO");
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    48
	if vcard.to_vcard4 then
1451
843769eb40c3 mod_profile: Don't include photo in vCard4 version (use XEP-0084 instead)
Kim Alvefur <zash@zash.se>
parents: 1442
diff changeset
    49
		if p then t_remove(data, p); end
1422
c3882787ae06 mod_profile: Purge nodes before publishing new items
Kim Alvefur <zash@zash.se>
parents: 1420
diff changeset
    50
		pep:purge("urn:xmpp:vcard4", true);
1424
9892a537e7fc mod_profile: Add id to item tag too.
Kim Alvefur <zash@zash.se>
parents: 1423
diff changeset
    51
		pep:publish("urn:xmpp:vcard4", true, "current", st.stanza("item", {id="current"})
9892a537e7fc mod_profile: Add id to item tag too.
Kim Alvefur <zash@zash.se>
parents: 1423
diff changeset
    52
			:add_child(vcard.to_vcard4(data)));
1451
843769eb40c3 mod_profile: Don't include photo in vCard4 version (use XEP-0084 instead)
Kim Alvefur <zash@zash.se>
parents: 1442
diff changeset
    53
		if p then t_insert(data, p, photo); end
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    54
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    55
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    56
	local nickname = get_item(data, "NICKNAME");
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    57
	if nickname and nickname[1] then
1422
c3882787ae06 mod_profile: Purge nodes before publishing new items
Kim Alvefur <zash@zash.se>
parents: 1420
diff changeset
    58
		pep:purge("http://jabber.org/protocol/nick", true);
1424
9892a537e7fc mod_profile: Add id to item tag too.
Kim Alvefur <zash@zash.se>
parents: 1423
diff changeset
    59
		pep:publish("http://jabber.org/protocol/nick", true, "current", st.stanza("item", {id="current"})
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    60
			:tag("nick", { xmlns="http://jabber.org/protocol/nick" }):text(nickname[1]));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    61
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    62
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    63
	if photo and photo[1] then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    64
		local photo_raw = base64.decode(photo[1]);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    65
		local photo_hash = sha1(photo_raw, true);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    66
1422
c3882787ae06 mod_profile: Purge nodes before publishing new items
Kim Alvefur <zash@zash.se>
parents: 1420
diff changeset
    67
		pep:purge("urn:xmpp:avatar:metadata", true);
c3882787ae06 mod_profile: Purge nodes before publishing new items
Kim Alvefur <zash@zash.se>
parents: 1420
diff changeset
    68
		pep:purge("urn:xmpp:avatar:data", true);
1503
c4c9ccc6e6c9 mod_profile: Set metadata nodes ItemID to the same hash as the data node
Kim Alvefur <zash@zash.se>
parents: 1499
diff changeset
    69
		pep:publish("urn:xmpp:avatar:metadata", true, photo_hash, st.stanza("item", {id=photo_hash})
1469
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1451
diff changeset
    70
			:tag("metadata", { xmlns="urn:xmpp:avatar:metadata" })
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1451
diff changeset
    71
				:tag("info", {
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1451
diff changeset
    72
					bytes = tostring(#photo_raw),
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1451
diff changeset
    73
					id = photo_hash,
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1451
diff changeset
    74
					type = identify(photo_raw),
019746bba318 mod_profile: Add the missing <info> child element and move info attributes there
Kim Alvefur <zash@zash.se>
parents: 1451
diff changeset
    75
				}));
1470
b291a9423e0f mod_profile: Fix wrong use of "current" as id for avatar data
Kim Alvefur <zash@zash.se>
parents: 1469
diff changeset
    76
		pep:publish("urn:xmpp:avatar:data", true, photo_hash, st.stanza("item", {id=photo_hash})
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    77
			:tag("data", { xmlns="urn:xmpp:avatar:data" }):text(photo[1]));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    78
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    79
end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    80
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    81
-- The "temporary" vCard XEP-0054 part
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    82
module:add_feature("vcard-temp");
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    83
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    84
local function handle_get(event)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    85
	local origin, stanza = event.origin, event.stanza;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    86
	local username = origin.username;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    87
	local to = stanza.attr.to;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    88
	if to then username = jid_split(to); end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    89
	local data = storage:get(username);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    90
	if not data then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    91
		data = legacy_storage:get(username);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    92
		data = data and st.deserialize(data);
1441
07c9306c2c1f mod_profile: Don't pass old vcard data trough vcard lib
Kim Alvefur <zash@zash.se>
parents: 1424
diff changeset
    93
		if data then
07c9306c2c1f mod_profile: Don't pass old vcard data trough vcard lib
Kim Alvefur <zash@zash.se>
parents: 1424
diff changeset
    94
			return origin.send(st.reply(stanza):add_child(data));
07c9306c2c1f mod_profile: Don't pass old vcard data trough vcard lib
Kim Alvefur <zash@zash.se>
parents: 1424
diff changeset
    95
		end
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    96
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    97
	if not data then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    98
		return origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
    99
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   100
	return origin.send(st.reply(stanza):add_child(vcard.to_xep54(data)));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   101
end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   102
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   103
local function handle_set(event)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   104
	local origin, stanza = event.origin, event.stanza;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   105
	local data = vcard.from_xep54(stanza.tags[1]);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   106
	local username = origin.username;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   107
	local to = stanza.attr.to;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   108
	if to then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   109
		if not is_admin(jid_bare(stanza.attr.from), module.host) then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   110
			return origin.send(st.error_reply(stanza, "auth", "forbidden"));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   111
		end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   112
		username = jid_split(to);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   113
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   114
	local ok, err = storage:set(username, data);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   115
	if not ok then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   116
		return origin.send(st.error_reply(stanza, "cancel", "internal-server-error", err));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   117
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   118
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   119
	if pep_plus and username then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   120
		update_pep(username, data);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   121
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   122
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   123
	return origin.send(st.reply(stanza));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   124
end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   125
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   126
module:hook("iq-get/bare/vcard-temp:vCard", handle_get);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   127
module:hook("iq-get/host/vcard-temp:vCard", handle_get);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   128
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   129
module:hook("iq-set/bare/vcard-temp:vCard", handle_set);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   130
module:hook("iq-set/host/vcard-temp:vCard", handle_set);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   131
1515
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   132
local function pep_service_added(event)
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   133
	local item = event.item;
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   134
	local service, username = item.service, jid_split(item.jid);
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   135
	local data = storage:get(username);
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   136
	if data then
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   137
		update_pep(username, data, service);
1442
253e374824a8 mod_profile: Load profile into PEP on initial presence
Kim Alvefur <zash@zash.se>
parents: 1441
diff changeset
   138
	end
1515
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   139
end
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   140
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   141
local function pep_service_removed()
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   142
	-- This would happen when mod_pep_plus gets unloaded, but this module gets unloaded before that
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   143
end
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   144
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   145
function module.load()
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   146
	module:handle_items("pep-service", pep_service_added, pep_service_removed, true);
f367d7cbfaa6 mod_profile: Use module:handle_items() for PEP node bootstrapping (see trunk 388786af0dd2)
Kim Alvefur <zash@zash.se>
parents: 1503
diff changeset
   147
end
1442
253e374824a8 mod_profile: Load profile into PEP on initial presence
Kim Alvefur <zash@zash.se>
parents: 1441
diff changeset
   148
1419
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   149
-- The vCard4 part
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   150
if vcard.to_vcard4 then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   151
	module:add_feature("urn:ietf:params:xml:ns:vcard-4.0");
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   152
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   153
	module:hook("iq-get/bare/urn:ietf:params:xml:ns:vcard-4.0:vcard", function(event)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   154
		local origin, stanza = event.origin, event.stanza;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   155
		local username = jid_split(stanza.attr.to) or origin.username;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   156
		local data = storage:get(username);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   157
		if not data then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   158
			return origin.send(st.error_reply(stanza, "cancel", "item-not-found"));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   159
		end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   160
		return origin.send(st.reply(stanza):add_child(vcard.to_vcard4(data)));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   161
	end);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   162
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   163
	if vcard.from_vcard4 then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   164
		module:hook("iq-set/self/urn:ietf:params:xml:ns:vcard-4.0:vcard", function(event)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   165
			local origin, stanza = event.origin, event.stanza;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   166
			local ok, err = storage:set(origin.username, vcard.from_vcard4(stanza.tags[1]));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   167
			if not ok then
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   168
				return origin.send(st.error_reply(stanza, "cancel", "internal-server-error", err));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   169
			end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   170
			return origin.send(st.reply(stanza));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   171
		end);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   172
	else
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   173
		module:hook("iq-set/self/urn:ietf:params:xml:ns:vcard-4.0:vcard", function(event)
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   174
			local origin, stanza = event.origin, event.stanza;
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   175
			return origin.send(st.error_reply(stanza, "cancel", "feature-not-implemented"));
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   176
		end);
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   177
	end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   178
end
877dc87539ff mod_profile: Replacement for mod_vcard with vcard4 support and integration with mod_pep_plus
Kim Alvefur <zash@zash.se>
parents:
diff changeset
   179