mod_bookmarks/mod_bookmarks.lua
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Sat, 28 Sep 2019 14:46:18 +0200
changeset 3682 7575399ae544
parent 3548 7893115bf382
permissions -rw-r--r--
mod_bookmarks, mod_bookmarks2: Refuse to load if the other one is also loaded.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3682
7575399ae544 mod_bookmarks, mod_bookmarks2: Refuse to load if the other one is also loaded.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3548
diff changeset
     1
local mm = require "core.modulemanager";
7575399ae544 mod_bookmarks, mod_bookmarks2: Refuse to load if the other one is also loaded.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3548
diff changeset
     2
if mm.get_modules_for_host(module.host):contains("bookmarks2") then
7575399ae544 mod_bookmarks, mod_bookmarks2: Refuse to load if the other one is also loaded.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3548
diff changeset
     3
	error("mod_bookmarks and mod_bookmarks2 are conflicting, please disable one of them.", 0);
7575399ae544 mod_bookmarks, mod_bookmarks2: Refuse to load if the other one is also loaded.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3548
diff changeset
     4
end
7575399ae544 mod_bookmarks, mod_bookmarks2: Refuse to load if the other one is also loaded.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3548
diff changeset
     5
3242
786ba175f2e5 mod_bookmarks: Require forgotten util.jid module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3239
diff changeset
     6
local st = require "util.stanza";
786ba175f2e5 mod_bookmarks: Require forgotten util.jid module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3239
diff changeset
     7
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
     8
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
     9
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
    10
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
    11
3298
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
    12
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
    13
	["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
    14
	["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
    15
};
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
    16
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    17
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
    18
	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
    19
end);
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    20
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    21
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
    22
	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
    23
	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
    24
	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
    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
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    28
	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
    29
	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
    30
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    31
	end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    32
3234
ba0d444b64aa mod_bookmarks: Simplify last item retrieval thanks to Prosody b6ffd4f951b9.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3233
diff changeset
    33
	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
    34
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    35
	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
    36
	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
    37
	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
    38
	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
    39
	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
    40
		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
    41
			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
    42
			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
    43
		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
    44
			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
    45
			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
    46
		end
3548
7893115bf382 mod_bookmarks: Return true when a stanza has been sent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3547
diff changeset
    47
		return true;
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    48
	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
    49
	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
    50
		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
    51
		session.send(st.reply(stanza):add_child(query));
3548
7893115bf382 mod_bookmarks: Return true when a stanza has been sent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3547
diff changeset
    52
		return true;
3237
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3236
diff changeset
    53
	end
176b537a658c mod_bookmarks: Send back empty Private XML bookmarks on empty PEP bookmarks.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3236
diff changeset
    54
	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
    55
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    56
	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
    57
	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
    58
	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
    59
	return true;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    60
end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    61
3239
bd8e94ff726b mod_bookmarks: Expose publish_to_pep() to other modules.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3238
diff changeset
    62
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
    63
	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
    64
	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
    65
		: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
    66
	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
    67
end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    68
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    69
-- 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
    70
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
    71
	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
    72
	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
    73
	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
    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
	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
    78
	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
    79
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    80
	end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    81
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    82
	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
    83
	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
    84
	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
    85
		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
    86
		session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to store bookmarks to PEP"));
3548
7893115bf382 mod_bookmarks: Return true when a stanza has been sent.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3547
diff changeset
    87
		return true;
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    88
	end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    89
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    90
	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
    91
	return true;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    92
end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    93
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
    94
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
    95
	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
    96
	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
    97
	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
    98
	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
    99
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   100
	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
   101
	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
   102
		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
   103
		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
   104
		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
   105
			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
   106
			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
   107
		end
3233
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   108
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   109
	end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   110
	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
   111
	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
   112
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
   113
	-- 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
   114
	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
   115
	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
   116
3286
9346ed926842 mod_bookmarks: Display the bare JID instead of the username in logs.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3257
diff changeset
   117
	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
   118
	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
   119
	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
   120
		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
   121
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   122
	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
   123
	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
   124
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   125
	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
   126
	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
   127
		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
   128
		return;
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   129
	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
   130
	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
   131
end
e8963e328b26 mod_bookmarks: New module synchronising bookmarks to the new persistent mod_pep
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
   132
3298
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   133
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
   134
	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
   135
	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
   136
		return;
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   137
	end
3491
e60933722248 mod_bookmarks: Use correct variable, fixes a traceback (thanks dan)
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
   138
	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
   139
	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
   140
		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
   141
		return;
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   142
	end
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   143
	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
   144
	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
   145
		if node_config[config_field] ~= value then
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   146
			node_config[config_field] = value;
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   147
			changed = true;
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   148
		end
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   149
	end
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   150
	if not changed then
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   151
		return;
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   152
	end
3491
e60933722248 mod_bookmarks: Use correct variable, fixes a traceback (thanks dan)
Kim Alvefur <zash@zash.se>
parents: 3480
diff changeset
   153
	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
   154
	if not ok then
4ce945490a24 mod_bookmarks: Actually save node config
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3477
diff changeset
   155
		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
   156
		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
   157
	end
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   158
end
947790ec4406 mod_bookmarks: Ensure the configuration is correct even with create-on-subscribe.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3294
diff changeset
   159
3312
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3311
diff changeset
   160
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
   161
	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
   162
		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
   163
	else
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3311
diff changeset
   164
		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
   165
	end
7155cc1adf8f mod_bookmarks: Handle iq:private queries before mod_private
Kim Alvefur <zash@zash.se>
parents: 3311
diff changeset
   166
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
   167
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
   168
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
   169
	local service = event.item.service;
303b17ec8264 mod_bookmarks: Correctly hook events on PEP services
Kim Alvefur <zash@zash.se>
parents: 3298
diff changeset
   170
	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
   171
end, function () end, true);