mod_bookmarks/mod_bookmarks.lua
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Tue, 02 Apr 2019 16:22:40 +0200
changeset 3547 11629f04ddd0
parent 3491 e60933722248
child 3548 7893115bf382
permissions -rw-r--r--
mod_bookmarks: Check for item-not-found and return empty Private XML. Fixes #1265 (thanks gerald and thebigfrog!)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3242
786ba175f2e5 mod_bookmarks: Require forgotten util.jid module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3239
diff changeset
     1
local st = require "util.stanza";
786ba175f2e5 mod_bookmarks: Require forgotten util.jid module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3239
diff changeset
     2
local jid_split = require "util.jid".split;
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
     3
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
     4
local mod_pep = module:depends "pep";
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
     5
local private_storage = module:open_store("private", "map");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
     6
3298
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
     7
local default_options = {
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
     8
	["persist_items"] = true;
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
     9
	["access_model"] = "whitelist";
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
    10
};
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
    11
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    12
module:hook("account-disco-info", function (event)
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    13
	event.reply:tag("feature", { var = "urn:xmpp:bookmarks-conversion:0" }):up();
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    14
end);
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    15
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    16
local function on_retrieve_private_xml(event)
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    17
	local stanza, session = event.stanza, event.origin;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    18
	local query = stanza:get_child("query", "jabber:iq:private");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    19
	if query == nil then
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    20
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    21
	end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    22
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    23
	local bookmarks = query:get_child("storage", "storage:bookmarks");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    24
	if bookmarks == nil then
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    25
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    26
	end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    27
3234
ba0d444b64aa mod_bookmarks: Simplify last item retrieval thanks to Prosody b6ffd4f951b9.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
    28
	module:log("debug", "Getting private bookmarks: %s", bookmarks);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    29
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    30
	local username = session.username;
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
    31
	local jid = username.."@"..session.host;
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    32
	local service = mod_pep.get_pep_service(username);
3234
ba0d444b64aa mod_bookmarks: Simplify last item retrieval thanks to Prosody b6ffd4f951b9.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
    33
	local ok, id, item = service:get_last_item("storage:bookmarks", session.full_jid);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    34
	if not ok then
3547
11629f04ddd0 mod_bookmarks: Check for item-not-found and return empty Private XML. Fixes #1265 (thanks gerald and thebigfrog!)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3491
diff changeset
    35
		if id == "item-not-found" then
11629f04ddd0 mod_bookmarks: Check for item-not-found and return empty Private XML. Fixes #1265 (thanks gerald and thebigfrog!)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3491
diff changeset
    36
			module:log("debug", "Got no PEP bookmarks item for %s, returning empty private bookmarks", jid);
11629f04ddd0 mod_bookmarks: Check for item-not-found and return empty Private XML. Fixes #1265 (thanks gerald and thebigfrog!)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3491
diff changeset
    37
			session.send(st.reply(stanza):add_child(query));
11629f04ddd0 mod_bookmarks: Check for item-not-found and return empty Private XML. Fixes #1265 (thanks gerald and thebigfrog!)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3491
diff changeset
    38
		else
11629f04ddd0 mod_bookmarks: Check for item-not-found and return empty Private XML. Fixes #1265 (thanks gerald and thebigfrog!)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3491
diff changeset
    39
			module:log("error", "Failed to retrieve PEP bookmarks of %s: %s", jid, id);
11629f04ddd0 mod_bookmarks: Check for item-not-found and return empty Private XML. Fixes #1265 (thanks gerald and thebigfrog!)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3491
diff changeset
    40
			session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to retrive bookmarks from PEP"));
11629f04ddd0 mod_bookmarks: Check for item-not-found and return empty Private XML. Fixes #1265 (thanks gerald and thebigfrog!)
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3491
diff changeset
    41
		end
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    42
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    43
	end
3237
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3236
diff changeset
    44
	if not id or not item then
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
    45
		module:log("debug", "Got no PEP bookmarks item for %s, returning empty private bookmarks", jid);
3237
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3236
diff changeset
    46
		session.send(st.reply(stanza):add_child(query));
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3236
diff changeset
    47
		return
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3236
diff changeset
    48
	end
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3236
diff changeset
    49
	module:log("debug", "Got item %s: %s", id, item);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    50
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    51
	local content = item.tags[1];
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
    52
	module:log("debug", "Sending back private for %s: %s", jid, content);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    53
	session.send(st.reply(stanza):query("jabber:iq:private"):add_child(content));
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    54
	return true;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    55
end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    56
3239
bd8e94ff726b mod_bookmarks: Expose publish_to_pep() to other modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3238
diff changeset
    57
function publish_to_pep(jid, bookmarks)
bd8e94ff726b mod_bookmarks: Expose publish_to_pep() to other modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3238
diff changeset
    58
	local service = mod_pep.get_pep_service(jid_split(jid));
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    59
	local item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = "current" })
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    60
		:add_child(bookmarks);
3298
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
    61
	return service:publish("storage:bookmarks", jid, "current", item, default_options);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    62
end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    63
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    64
-- Synchronise Private XML to PEP.
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    65
local function on_publish_private_xml(event)
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    66
	local stanza, session = event.stanza, event.origin;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    67
	local query = stanza:get_child("query", "jabber:iq:private");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    68
	if query == nil then
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    69
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    70
	end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    71
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    72
	local bookmarks = query:get_child("storage", "storage:bookmarks");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    73
	if bookmarks == nil then
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    74
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    75
	end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    76
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    77
	module:log("debug", "Private bookmarks set by client, publishing to pep");
3239
bd8e94ff726b mod_bookmarks: Expose publish_to_pep() to other modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3238
diff changeset
    78
	local ok, err = publish_to_pep(session.full_jid, bookmarks);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    79
	if not ok then
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
    80
		module:log("error", "Failed to publish to PEP bookmarks for %s@%s: %s", session.username, session.host, err);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    81
		session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to store bookmarks to PEP"));
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    82
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    83
	end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    84
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    85
	session.send(st.reply(stanza));
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    86
	return true;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    87
end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    88
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    89
local function on_resource_bind(event)
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    90
	local session = event.session;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    91
	local username = session.username;
3294
87769f53fdc8 mod_bookmarks: Delete the node before attempting migration, to make sure its config is correct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3286
diff changeset
    92
	local service = mod_pep.get_pep_service(username);
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
    93
	local jid = username.."@"..session.host;
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    94
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    95
	local data, err = private_storage:get(username, "storage:storage:bookmarks");
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    96
	if not data then
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
    97
		module:log("debug", "No existing Private XML bookmarks for %s, migration already done: %s", jid, err);
3238
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3237
diff changeset
    98
		local ok, id = service:get_last_item("storage:bookmarks", session.full_jid);
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3237
diff changeset
    99
		if not ok or not id then
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
   100
			module:log("debug", "Additionally, no PEP bookmarks were existing for %s", jid);
3238
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3237
diff changeset
   101
			module:fire_event("bookmarks/empty", { session = session });
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3237
diff changeset
   102
		end
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   103
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   104
	end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   105
	local bookmarks = st.deserialize(data);
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
   106
	module:log("debug", "Got private bookmarks of %s: %s", jid, bookmarks);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   107
3294
87769f53fdc8 mod_bookmarks: Delete the node before attempting migration, to make sure its config is correct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3286
diff changeset
   108
	-- We don’t care if deleting succeeds or not, we only want to start with a non-existent node.
87769f53fdc8 mod_bookmarks: Delete the node before attempting migration, to make sure its config is correct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3286
diff changeset
   109
	module:log("debug", "Deleting possibly existing PEP item for %s", jid);
87769f53fdc8 mod_bookmarks: Delete the node before attempting migration, to make sure its config is correct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3286
diff changeset
   110
	service:delete("storage:bookmarks", jid);
87769f53fdc8 mod_bookmarks: Delete the node before attempting migration, to make sure its config is correct.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3286
diff changeset
   111
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
   112
	module:log("debug", "Going to store PEP item for %s", jid);
3239
bd8e94ff726b mod_bookmarks: Expose publish_to_pep() to other modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3238
diff changeset
   113
	local ok, err = publish_to_pep(session.full_jid, bookmarks);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   114
	if not ok then
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
   115
		module:log("error", "Failed to store bookmarks to PEP for %s, aborting migration: %s", jid, err);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   116
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   117
	end
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
   118
	module:log("debug", "Stored bookmarks to PEP for %s", jid);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   119
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   120
	local ok, err = private_storage:set(username, "storage:storage:bookmarks", nil);
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   121
	if not ok then
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
   122
		module:log("error", "Failed to remove private bookmarks of %s: %s", jid, err);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   123
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   124
	end
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
   125
	module:log("debug", "Removed private bookmarks of %s, migration done!", jid);
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   126
end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   127
3298
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   128
local function on_node_created(event)
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   129
	local service, node, actor = event.service, event.node, event.actor;
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   130
	if node ~= "storage:bookmarks" then
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   131
		return;
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   132
	end
3491
e60933722248 mod_bookmarks: Use correct variable, fixes a traceback (thanks dan)
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
   133
	local ok, node_config = service:get_node_config(node, actor);
3480
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   134
	if not ok then
3491
e60933722248 mod_bookmarks: Use correct variable, fixes a traceback (thanks dan)
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
   135
		module:log("error", "Failed to get node config of %s: %s", node, node_config);
3480
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   136
		return;
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   137
	end
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   138
	local changed = false;
3298
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   139
	for config_field, value in pairs(default_options) do
3480
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   140
		if node_config[config_field] ~= value then
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   141
			node_config[config_field] = value;
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   142
			changed = true;
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   143
		end
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   144
	end
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   145
	if not changed then
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   146
		return;
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   147
	end
3491
e60933722248 mod_bookmarks: Use correct variable, fixes a traceback (thanks dan)
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
   148
	local ok, err = service:set_node_config(node, actor, node_config);
3480
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   149
	if not ok then
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   150
		module:log("error", "Failed to set node config of %s: %s", node, err);
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   151
		return;
3298
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   152
	end
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   153
end
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   154
3312
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3311
diff changeset
   155
module:hook("iq/bare/jabber:iq:private:query", function (event)
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3311
diff changeset
   156
	if event.stanza.attr.type == "get" then
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3311
diff changeset
   157
		return on_retrieve_private_xml(event);
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3311
diff changeset
   158
	else
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3311
diff changeset
   159
		return on_publish_private_xml(event);
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3311
diff changeset
   160
	end
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3311
diff changeset
   161
end, 1);
3238
b1e25943a004 mod_bookmarks: Fire empty and updated events, for other modules to use.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3237
diff changeset
   162
module:hook("resource-bind", on_resource_bind);
3311
303b17ec8264 mod_bookmarks: Correctly hook events on PEP services
Kim Alvefur <zash@zash.se>
parents: 3298
diff changeset
   163
module:handle_items("pep-service", function (event)
303b17ec8264 mod_bookmarks: Correctly hook events on PEP services
Kim Alvefur <zash@zash.se>
parents: 3298
diff changeset
   164
	local service = event.item.service;
303b17ec8264 mod_bookmarks: Correctly hook events on PEP services
Kim Alvefur <zash@zash.se>
parents: 3298
diff changeset
   165
	module:hook_object_event(service.events, "node-created", on_node_created);
303b17ec8264 mod_bookmarks: Correctly hook events on PEP services
Kim Alvefur <zash@zash.se>
parents: 3298
diff changeset
   166
end, function () end, true);