Mercurial > prosody-modules
annotate mod_bookmarks2/mod_bookmarks2.lua @ 4048:64b7daa6c42c
mod_csi_battery_saver: Ignore CSI actions before resource bind
author | tmolitor <thilo@eightysoft.de> |
---|---|
date | Mon, 22 Jun 2020 21:35:57 +0200 |
parents | 5eab799b5fa4 |
children | 776eacd233b5 |
rev | line source |
---|---|
3678
7575399ae544
mod_bookmarks, mod_bookmarks2: Refuse to load if the other one is also loaded.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3677
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:
3677
diff
changeset
|
2 if mm.get_modules_for_host(module.host):contains("bookmarks") then |
7575399ae544
mod_bookmarks, mod_bookmarks2: Refuse to load if the other one is also loaded.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3677
diff
changeset
|
3 error("mod_bookmarks2 and mod_bookmarks 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:
3677
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:
3677
diff
changeset
|
5 |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
6 local st = require "util.stanza"; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
7 local jid_split = require "util.jid".split; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
8 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
9 local mod_pep = module:depends "pep"; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
10 local private_storage = module:open_store("private", "map"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
11 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
12 local default_options = { |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
13 ["persist_items"] = true; |
3678
7575399ae544
mod_bookmarks, mod_bookmarks2: Refuse to load if the other one is also loaded.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3677
diff
changeset
|
14 -- This should be much higher, the XEP recommends 10000 but mod_pep rejects that. |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
15 ["max_items"] = 255; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
16 ["send_last_published_item"] = "never"; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
17 ["access_model"] = "whitelist"; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
18 }; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
19 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
20 module:hook("account-disco-info", function (event) |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
21 -- This Time it’s Serious! |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
22 event.reply:tag("feature", { var = "urn:xmpp:bookmarks:0#compat" }):up(); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
23 end); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
24 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
25 local function on_retrieve_private_xml(event) |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
26 local stanza, session = event.stanza, event.origin; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
27 local query = stanza:get_child("query", "jabber:iq:private"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
28 if query == nil then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
29 return; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
30 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
31 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
32 local bookmarks = query:get_child("storage", "storage:bookmarks"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
33 if bookmarks == nil then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
34 return; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
35 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
36 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
37 module:log("debug", "Getting private bookmarks: %s", bookmarks); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
38 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
39 local username = session.username; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
40 local jid = username.."@"..session.host; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
41 local service = mod_pep.get_pep_service(username); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
42 local ok, ret = service:get_items("urn:xmpp:bookmarks:0", session.full_jid); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
43 if not ok then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
44 if ret == "item-not-found" then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
45 module:log("debug", "Got no PEP bookmarks item for %s, returning empty private bookmarks", jid); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
46 session.send(st.reply(stanza):add_child(query)); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
47 else |
3740
b3fb32bd1f01
mod_bookmarks2: Use the correct error when failing to retrieve bookmarks2 to reply to Private XML request.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3739
diff
changeset
|
48 module:log("error", "Failed to retrieve PEP bookmarks of %s: %s", jid, ret); |
b3fb32bd1f01
mod_bookmarks2: Use the correct error when failing to retrieve bookmarks2 to reply to Private XML request.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3739
diff
changeset
|
49 session.send(st.error_reply(stanza, "cancel", ret, "Failed to retrive bookmarks from PEP")); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
50 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
51 return true; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
52 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
53 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
54 local storage = st.stanza("storage", { xmlns = "storage:bookmarks" }); |
3738
9cd6d602ad1b
mod_bookmarks2: Simplify for loop (thanks MattJ!).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3696
diff
changeset
|
55 for _, item_id in ipairs(ret) do |
9cd6d602ad1b
mod_bookmarks2: Simplify for loop (thanks MattJ!).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3696
diff
changeset
|
56 local item = ret[item_id]; |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
57 local conference = st.stanza("conference"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
58 conference.attr.jid = item.attr.id; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
59 local bookmark = item:get_child("conference", "urn:xmpp:bookmarks:0"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
60 conference.attr.name = bookmark.attr.name; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
61 conference.attr.autojoin = bookmark.attr.autojoin; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
62 local nick = bookmark:get_child_text("nick", "urn:xmpp:bookmarks:0"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
63 if nick ~= nil then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
64 conference:text_tag("nick", nick, { xmlns = "storage:bookmarks" }):up(); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
65 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
66 local password = bookmark:get_child_text("password", "urn:xmpp:bookmarks:0"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
67 if password ~= nil then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
68 conference:text_tag("password", password):up(); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
69 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
70 storage:add_child(conference); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
71 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
72 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
73 module:log("debug", "Sending back private for %s: %s", jid, storage); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
74 session.send(st.reply(stanza):query("jabber:iq:private"):add_child(storage)); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
75 return true; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
76 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
77 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
78 local function compare_bookmark2(a, b) |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
79 if a == nil or b == nil then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
80 return false; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
81 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
82 local a_conference = a:get_child("conference", "urn:xmpp:bookmarks:0"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
83 local b_conference = b:get_child("conference", "urn:xmpp:bookmarks:0"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
84 local a_nick = a:get_child_text("nick", "urn:xmpp:bookmarks:0"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
85 local b_nick = b:get_child_text("nick", "urn:xmpp:bookmarks:0"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
86 local a_password = a:get_child_text("password", "urn:xmpp:bookmarks:0"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
87 local b_password = b:get_child_text("password", "urn:xmpp:bookmarks:0"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
88 return (a.attr.id == b.attr.id and |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
89 a_conference.attr.name == b_conference.attr.name and |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
90 a_conference.attr.autojoin == b_conference.attr.autojoin and |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
91 a_nick == b_nick and |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
92 a_password == b_password); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
93 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
94 |
3739
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
95 local function publish_to_pep(jid, bookmarks, synchronise) |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
96 local service = mod_pep.get_pep_service(jid_split(jid)); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
97 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
98 if #bookmarks.tags == 0 then |
3739
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
99 if synchronise then |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
100 -- If we set zero legacy bookmarks, purge the bookmarks 2 node. |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
101 module:log("debug", "No bookmark in the set, purging instead."); |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
102 return service:purge("urn:xmpp:bookmarks:0", jid, true); |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
103 else |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
104 return true; |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
105 end |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
106 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
107 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
108 -- Retrieve the current bookmarks2. |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
109 module:log("debug", "Retrieving the current bookmarks 2."); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
110 local has_bookmarks2, ret = service:get_items("urn:xmpp:bookmarks:0", jid); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
111 local bookmarks2; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
112 if not has_bookmarks2 and ret == "item-not-found" then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
113 module:log("debug", "Got item-not-found, assuming it was empty until now, creating."); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
114 local ok, err = service:create("urn:xmpp:bookmarks:0", jid, default_options); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
115 if not ok then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
116 module:log("error", "Creating bookmarks 2 node failed: %s", err); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
117 return ok, err; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
118 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
119 bookmarks2 = {}; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
120 elseif not has_bookmarks2 then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
121 module:log("debug", "Got %s error, aborting.", ret); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
122 return false, ret; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
123 else |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
124 module:log("debug", "Got existing bookmarks2."); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
125 bookmarks2 = ret; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
126 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
127 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
128 -- Get a list of all items we may want to remove. |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
129 local to_remove = {}; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
130 for i in ipairs(bookmarks2) do |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
131 to_remove[bookmarks2[i]] = true; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
132 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
133 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
134 for bookmark in bookmarks:childtags("conference", "storage:bookmarks") do |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
135 -- Create the new conference element by copying everything from the legacy one. |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
136 local conference = st.stanza("conference", { xmlns = "urn:xmpp:bookmarks:0" }); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
137 conference.attr.name = bookmark.attr.name; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
138 conference.attr.autojoin = bookmark.attr.autojoin; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
139 local nick = bookmark:get_child_text("nick", "storage:bookmarks"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
140 if nick ~= nil then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
141 conference:text_tag("nick", nick, { xmlns = "urn:xmpp:bookmarks:0" }):up(); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
142 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
143 local password = bookmark:get_child_text("password", "storage:bookmarks"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
144 if password ~= nil then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
145 conference:text_tag("password", password, { xmlns = "urn:xmpp:bookmarks:0" }):up(); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
146 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
147 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
148 -- Create its wrapper. |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
149 local item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = bookmark.attr.jid }) |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
150 :add_child(conference); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
151 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
152 -- Then publish it only if it’s a new one or updating a previous one. |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
153 if compare_bookmark2(item, bookmarks2[bookmark.attr.jid]) then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
154 module:log("debug", "Item %s identical to the previous one, skipping.", item.attr.id); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
155 to_remove[bookmark.attr.jid] = nil; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
156 else |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
157 if bookmarks2[bookmark.attr.jid] == nil then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
158 module:log("debug", "Item %s not existing previously, publishing.", item.attr.id); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
159 else |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
160 module:log("debug", "Item %s different from the previous one, publishing.", item.attr.id); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
161 to_remove[bookmark.attr.jid] = nil; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
162 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
163 local ok, err = service:publish("urn:xmpp:bookmarks:0", jid, bookmark.attr.jid, item, default_options); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
164 if not ok then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
165 module:log("error", "Publishing item %s failed: %s", item.attr.id, err); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
166 return ok, err; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
167 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
168 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
169 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
170 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
171 -- Now handle retracting items that have been removed. |
3739
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
172 if synchronise then |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
173 for id in pairs(to_remove) do |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
174 module:log("debug", "Item %s removed from bookmarks.", id); |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
175 local ok, err = service:retract("urn:xmpp:bookmarks:0", jid, id, st.stanza("retract", { id = id })); |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
176 if not ok then |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
177 module:log("error", "Retracting item %s failed: %s", id, err); |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
178 return ok, err; |
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
179 end |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
180 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
181 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
182 return true; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
183 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
184 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
185 -- Synchronise Private XML to PEP. |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
186 local function on_publish_private_xml(event) |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
187 local stanza, session = event.stanza, event.origin; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
188 local query = stanza:get_child("query", "jabber:iq:private"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
189 if query == nil then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
190 return; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
191 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
192 |
3742
05bca7a771d6
mod_bookmarks2: Remove unused variables.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3741
diff
changeset
|
193 local bookmarks = query:get_child("storage", "storage:bookmarks"); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
194 if bookmarks == nil then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
195 return; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
196 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
197 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
198 module:log("debug", "Private bookmarks set by client, publishing to pep."); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
199 |
3739
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
200 local ok, err = publish_to_pep(session.full_jid, bookmarks, true); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
201 if not ok then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
202 module:log("error", "Failed to publish to PEP bookmarks for %s@%s: %s", session.username, session.host, err); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
203 session.send(st.error_reply(stanza, "cancel", "internal-server-error", "Failed to store bookmarks to PEP")); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
204 return true; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
205 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
206 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
207 session.send(st.reply(stanza)); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
208 return true; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
209 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
210 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
211 local function migrate_legacy_bookmarks(event) |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
212 local session = event.session; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
213 local username = session.username; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
214 local service = mod_pep.get_pep_service(username); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
215 local jid = username.."@"..session.host; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
216 |
3741
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
217 local ok, ret = service:get_items("storage:bookmarks", session.full_jid); |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
218 if ok then |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
219 module:log("debug", "Legacy PEP bookmarks found for %s, migrating.", jid); |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
220 local failed = false; |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
221 for _, item_id in ipairs(ret) do |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
222 local item = ret[item_id]; |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
223 if item.attr.id ~= "current" then |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
224 module:log("warn", "Legacy PEP bookmarks for %s isn’t using 'current' as its id: %s", jid, item.attr.id); |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
225 end |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
226 local bookmarks = item:get_child("storage", "storage:bookmarks"); |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
227 module:log("debug", "Got legacy PEP bookmarks of %s: %s", jid, bookmarks); |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
228 |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
229 local ok, err = publish_to_pep(session.full_jid, bookmarks, false); |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
230 if not ok then |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
231 module:log("error", "Failed to store legacy PEP bookmarks to bookmarks 2 for %s, aborting migration: %s", jid, err); |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
232 failed = true; |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
233 break; |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
234 end |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
235 end |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
236 if not failed then |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
237 module:log("debug", "Successfully migrated legacy PEP bookmarks of %s to bookmarks 2, attempting deletion of the node.", jid); |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
238 local ok, err = service:delete("storage:bookmarks", jid); |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
239 if not ok then |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
240 module:log("error", "Failed to delete legacy PEP bookmarks for %s: %s", jid, err); |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
241 end |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
242 end |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
243 end |
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
244 |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
245 local data, err = private_storage:get(username, "storage:storage:bookmarks"); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
246 if not data then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
247 module:log("debug", "No existing legacy bookmarks for %s, migration already done: %s", jid, err); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
248 local ok, ret = service:get_items("urn:xmpp:bookmarks:0", session.full_jid); |
3696
310061f6e1df
mod_bookmarks2: Fix traceback on updating from no legacy bookmarks to bookmarks 2.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3678
diff
changeset
|
249 if not ok or not ret then |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
250 module:log("debug", "Additionally, no bookmarks 2 were existing for %s, assuming empty.", jid); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
251 module:fire_event("bookmarks/empty", { session = session }); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
252 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
253 return; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
254 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
255 local bookmarks = st.deserialize(data); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
256 module:log("debug", "Got legacy bookmarks of %s: %s", jid, bookmarks); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
257 |
3741
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
258 module:log("debug", "Going to store legacy bookmarks to bookmarks 2 %s.", jid); |
3739
1ed3d86eeaed
mod_bookmarks2: Don’t purge or remove inexistant items when synchronising existing Private XML data.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3738
diff
changeset
|
259 local ok, err = publish_to_pep(session.full_jid, bookmarks, false); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
260 if not ok then |
3741
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
261 module:log("error", "Failed to store legacy bookmarks to bookmarks 2 for %s, aborting migration: %s", jid, err); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
262 return; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
263 end |
3741
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
264 module:log("debug", "Stored legacy bookmarks to bookmarks 2 for %s.", jid); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
265 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
266 local ok, err = private_storage:set(username, "storage:storage:bookmarks", nil); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
267 if not ok then |
3741
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
268 module:log("error", "Failed to remove legacy bookmarks of %s: %s", jid, err); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
269 return; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
270 end |
3741
33b3e2387a10
mod_bookmarks2: Also migrate legacy PEP bookmarks (current XEP-0048).
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3740
diff
changeset
|
271 module:log("debug", "Removed legacy bookmarks of %s, migration done!", jid); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
272 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
273 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
274 local function on_node_created(event) |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
275 local service, node, actor = event.service, event.node, event.actor; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
276 if node ~= "storage:bookmarks" then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
277 return; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
278 end |
3743
5eab799b5fa4
mod_bookmarks2: Delete legacy PEP bookmarks whenever they get recreated, to prevent them from being used.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3742
diff
changeset
|
279 |
5eab799b5fa4
mod_bookmarks2: Delete legacy PEP bookmarks whenever they get recreated, to prevent them from being used.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3742
diff
changeset
|
280 module:log("debug", "Something tried to create legacy PEP bookmarks for %s.", actor); |
5eab799b5fa4
mod_bookmarks2: Delete legacy PEP bookmarks whenever they get recreated, to prevent them from being used.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3742
diff
changeset
|
281 local ok, err = service:delete("storage:bookmarks", actor); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
282 if not ok then |
3743
5eab799b5fa4
mod_bookmarks2: Delete legacy PEP bookmarks whenever they get recreated, to prevent them from being used.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3742
diff
changeset
|
283 module:log("error", "Failed to delete legacy PEP bookmarks for %s: %s", actor, err); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
284 end |
3743
5eab799b5fa4
mod_bookmarks2: Delete legacy PEP bookmarks whenever they get recreated, to prevent them from being used.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3742
diff
changeset
|
285 module:log("debug", "Legacy PEP bookmarks node of %s deleted.", actor); |
3677
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
286 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
287 |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
288 module:hook("iq/bare/jabber:iq:private:query", function (event) |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
289 if event.stanza.attr.type == "get" then |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
290 return on_retrieve_private_xml(event); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
291 else |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
292 return on_publish_private_xml(event); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
293 end |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
294 end, 1); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
295 module:hook("resource-bind", migrate_legacy_bookmarks); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
296 module:handle_items("pep-service", function (event) |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
297 local service = event.item.service; |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
298 module:hook_object_event(service.events, "node-created", on_node_created); |
90f88a643973
mod_bookmarks2: Add new module.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff
changeset
|
299 end, function () end, true); |