Comparison

mod_bookmarks2/mod_bookmarks2.lua @ 4639:3da7cd77aca9

mod_bookmarks2: Factor namespace string into a variable
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 30 Jul 2021 21:13:16 +0200
parent 4279:776eacd233b5
child 4640:d835cb7d2b47
comparison
equal deleted inserted replaced
4638:3c4cef6be45b 4639:3da7cd77aca9
6 local st = require "util.stanza"; 6 local st = require "util.stanza";
7 local jid_split = require "util.jid".split; 7 local jid_split = require "util.jid".split;
8 8
9 local mod_pep = module:depends "pep"; 9 local mod_pep = module:depends "pep";
10 local private_storage = module:open_store("private", "map"); 10 local private_storage = module:open_store("private", "map");
11
12 local namespace = "urn:xmpp:bookmarks:0";
11 13
12 local default_options = { 14 local default_options = {
13 ["persist_items"] = true; 15 ["persist_items"] = true;
14 -- This should be much higher, the XEP recommends 10000 but mod_pep rejects that. 16 -- This should be much higher, the XEP recommends 10000 but mod_pep rejects that.
15 ["max_items"] = 255; 17 ["max_items"] = 255;
17 ["access_model"] = "whitelist"; 19 ["access_model"] = "whitelist";
18 }; 20 };
19 21
20 module:hook("account-disco-info", function (event) 22 module:hook("account-disco-info", function (event)
21 -- This Time it’s Serious! 23 -- This Time it’s Serious!
22 event.reply:tag("feature", { var = "urn:xmpp:bookmarks:0#compat" }):up(); 24 event.reply:tag("feature", { var = namespace.."#compat" }):up();
23 end); 25 end);
24 26
25 local function on_retrieve_private_xml(event) 27 local function on_retrieve_private_xml(event)
26 local stanza, session = event.stanza, event.origin; 28 local stanza, session = event.stanza, event.origin;
27 local query = stanza:get_child("query", "jabber:iq:private"); 29 local query = stanza:get_child("query", "jabber:iq:private");
37 module:log("debug", "Getting private bookmarks: %s", bookmarks); 39 module:log("debug", "Getting private bookmarks: %s", bookmarks);
38 40
39 local username = session.username; 41 local username = session.username;
40 local jid = username.."@"..session.host; 42 local jid = username.."@"..session.host;
41 local service = mod_pep.get_pep_service(username); 43 local service = mod_pep.get_pep_service(username);
42 local ok, ret = service:get_items("urn:xmpp:bookmarks:0", session.full_jid); 44 local ok, ret = service:get_items(namespace, session.full_jid);
43 if not ok then 45 if not ok then
44 if ret == "item-not-found" then 46 if ret == "item-not-found" then
45 module:log("debug", "Got no PEP bookmarks item for %s, returning empty private bookmarks", jid); 47 module:log("debug", "Got no PEP bookmarks item for %s, returning empty private bookmarks", jid);
46 session.send(st.reply(stanza):add_child(query)); 48 session.send(st.reply(stanza):add_child(query));
47 else 49 else
54 local storage = st.stanza("storage", { xmlns = "storage:bookmarks" }); 56 local storage = st.stanza("storage", { xmlns = "storage:bookmarks" });
55 for _, item_id in ipairs(ret) do 57 for _, item_id in ipairs(ret) do
56 local item = ret[item_id]; 58 local item = ret[item_id];
57 local conference = st.stanza("conference"); 59 local conference = st.stanza("conference");
58 conference.attr.jid = item.attr.id; 60 conference.attr.jid = item.attr.id;
59 local bookmark = item:get_child("conference", "urn:xmpp:bookmarks:0"); 61 local bookmark = item:get_child("conference", namespace);
60 conference.attr.name = bookmark.attr.name; 62 conference.attr.name = bookmark.attr.name;
61 conference.attr.autojoin = bookmark.attr.autojoin; 63 conference.attr.autojoin = bookmark.attr.autojoin;
62 local nick = bookmark:get_child_text("nick", "urn:xmpp:bookmarks:0"); 64 local nick = bookmark:get_child_text("nick", namespace);
63 if nick ~= nil then 65 if nick ~= nil then
64 conference:text_tag("nick", nick, { xmlns = "storage:bookmarks" }):up(); 66 conference:text_tag("nick", nick, { xmlns = "storage:bookmarks" }):up();
65 end 67 end
66 local password = bookmark:get_child_text("password", "urn:xmpp:bookmarks:0"); 68 local password = bookmark:get_child_text("password", namespace);
67 if password ~= nil then 69 if password ~= nil then
68 conference:text_tag("password", password):up(); 70 conference:text_tag("password", password):up();
69 end 71 end
70 storage:add_child(conference); 72 storage:add_child(conference);
71 end 73 end
77 79
78 local function compare_bookmark2(a, b) 80 local function compare_bookmark2(a, b)
79 if a == nil or b == nil then 81 if a == nil or b == nil then
80 return false; 82 return false;
81 end 83 end
82 local a_conference = a:get_child("conference", "urn:xmpp:bookmarks:0"); 84 local a_conference = a:get_child("conference", namespace);
83 local b_conference = b:get_child("conference", "urn:xmpp:bookmarks:0"); 85 local b_conference = b:get_child("conference", namespace);
84 local a_nick = a:get_child_text("nick", "urn:xmpp:bookmarks:0"); 86 local a_nick = a:get_child_text("nick", namespace);
85 local b_nick = b:get_child_text("nick", "urn:xmpp:bookmarks:0"); 87 local b_nick = b:get_child_text("nick", namespace);
86 local a_password = a:get_child_text("password", "urn:xmpp:bookmarks:0"); 88 local a_password = a:get_child_text("password", namespace);
87 local b_password = b:get_child_text("password", "urn:xmpp:bookmarks:0"); 89 local b_password = b:get_child_text("password", namespace);
88 return (a.attr.id == b.attr.id and 90 return (a.attr.id == b.attr.id and
89 a_conference.attr.name == b_conference.attr.name and 91 a_conference.attr.name == b_conference.attr.name and
90 a_conference.attr.autojoin == b_conference.attr.autojoin and 92 a_conference.attr.autojoin == b_conference.attr.autojoin and
91 a_nick == b_nick and 93 a_nick == b_nick and
92 a_password == b_password); 94 a_password == b_password);
97 99
98 if #bookmarks.tags == 0 then 100 if #bookmarks.tags == 0 then
99 if synchronise then 101 if synchronise then
100 -- If we set zero legacy bookmarks, purge the bookmarks 2 node. 102 -- If we set zero legacy bookmarks, purge the bookmarks 2 node.
101 module:log("debug", "No bookmark in the set, purging instead."); 103 module:log("debug", "No bookmark in the set, purging instead.");
102 return service:purge("urn:xmpp:bookmarks:0", jid, true); 104 return service:purge(namespace, jid, true);
103 else 105 else
104 return true; 106 return true;
105 end 107 end
106 end 108 end
107 109
108 -- Retrieve the current bookmarks2. 110 -- Retrieve the current bookmarks2.
109 module:log("debug", "Retrieving the current bookmarks 2."); 111 module:log("debug", "Retrieving the current bookmarks 2.");
110 local has_bookmarks2, ret = service:get_items("urn:xmpp:bookmarks:0", jid); 112 local has_bookmarks2, ret = service:get_items(namespace, jid);
111 local bookmarks2; 113 local bookmarks2;
112 if not has_bookmarks2 and ret == "item-not-found" then 114 if not has_bookmarks2 and ret == "item-not-found" then
113 module:log("debug", "Got item-not-found, assuming it was empty until now, creating."); 115 module:log("debug", "Got item-not-found, assuming it was empty until now, creating.");
114 local ok, err = service:create("urn:xmpp:bookmarks:0", jid, default_options); 116 local ok, err = service:create(namespace, jid, default_options);
115 if not ok then 117 if not ok then
116 module:log("error", "Creating bookmarks 2 node failed: %s", err); 118 module:log("error", "Creating bookmarks 2 node failed: %s", err);
117 return ok, err; 119 return ok, err;
118 end 120 end
119 bookmarks2 = {}; 121 bookmarks2 = {};
131 to_remove[bookmarks2[i]] = true; 133 to_remove[bookmarks2[i]] = true;
132 end 134 end
133 135
134 for bookmark in bookmarks:childtags("conference", "storage:bookmarks") do 136 for bookmark in bookmarks:childtags("conference", "storage:bookmarks") do
135 -- Create the new conference element by copying everything from the legacy one. 137 -- Create the new conference element by copying everything from the legacy one.
136 local conference = st.stanza("conference", { xmlns = "urn:xmpp:bookmarks:0" }); 138 local conference = st.stanza("conference", { xmlns = namespace });
137 conference.attr.name = bookmark.attr.name; 139 conference.attr.name = bookmark.attr.name;
138 conference.attr.autojoin = bookmark.attr.autojoin; 140 conference.attr.autojoin = bookmark.attr.autojoin;
139 local nick = bookmark:get_child_text("nick", "storage:bookmarks"); 141 local nick = bookmark:get_child_text("nick", "storage:bookmarks");
140 if nick ~= nil then 142 if nick ~= nil then
141 conference:text_tag("nick", nick, { xmlns = "urn:xmpp:bookmarks:0" }):up(); 143 conference:text_tag("nick", nick):up();
142 end 144 end
143 local password = bookmark:get_child_text("password", "storage:bookmarks"); 145 local password = bookmark:get_child_text("password", "storage:bookmarks");
144 if password ~= nil then 146 if password ~= nil then
145 conference:text_tag("password", password, { xmlns = "urn:xmpp:bookmarks:0" }):up(); 147 conference:text_tag("password", password):up();
146 end 148 end
147 149
148 -- Create its wrapper. 150 -- Create its wrapper.
149 local item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = bookmark.attr.jid }) 151 local item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = bookmark.attr.jid })
150 :add_child(conference); 152 :add_child(conference);
158 module:log("debug", "Item %s not existing previously, publishing.", item.attr.id); 160 module:log("debug", "Item %s not existing previously, publishing.", item.attr.id);
159 else 161 else
160 module:log("debug", "Item %s different from the previous one, publishing.", item.attr.id); 162 module:log("debug", "Item %s different from the previous one, publishing.", item.attr.id);
161 to_remove[bookmark.attr.jid] = nil; 163 to_remove[bookmark.attr.jid] = nil;
162 end 164 end
163 local ok, err = service:publish("urn:xmpp:bookmarks:0", jid, bookmark.attr.jid, item, default_options); 165 local ok, err = service:publish(namespace, jid, bookmark.attr.jid, item, default_options);
164 if not ok then 166 if not ok then
165 module:log("error", "Publishing item %s failed: %s", item.attr.id, err); 167 module:log("error", "Publishing item %s failed: %s", item.attr.id, err);
166 return ok, err; 168 return ok, err;
167 end 169 end
168 end 170 end
170 172
171 -- Now handle retracting items that have been removed. 173 -- Now handle retracting items that have been removed.
172 if synchronise then 174 if synchronise then
173 for id in pairs(to_remove) do 175 for id in pairs(to_remove) do
174 module:log("debug", "Item %s removed from bookmarks.", id); 176 module:log("debug", "Item %s removed from bookmarks.", id);
175 local ok, err = service:retract("urn:xmpp:bookmarks:0", jid, id, st.stanza("retract", { id = id })); 177 local ok, err = service:retract(namespace, jid, id, st.stanza("retract", { id = id }));
176 if not ok then 178 if not ok then
177 module:log("error", "Retracting item %s failed: %s", id, err); 179 module:log("error", "Retracting item %s failed: %s", id, err);
178 return ok, err; 180 return ok, err;
179 end 181 end
180 end 182 end
243 end 245 end
244 246
245 local data, err = private_storage:get(username, "storage:storage:bookmarks"); 247 local data, err = private_storage:get(username, "storage:storage:bookmarks");
246 if not data then 248 if not data then
247 module:log("debug", "No existing legacy bookmarks for %s, migration already done: %s", jid, err); 249 module:log("debug", "No existing legacy bookmarks for %s, migration already done: %s", jid, err);
248 local ok, ret2 = service:get_items("urn:xmpp:bookmarks:0", session.full_jid); 250 local ok, ret2 = service:get_items(namespace, session.full_jid);
249 if not ok or not ret2 then 251 if not ok or not ret2 then
250 module:log("debug", "Additionally, no bookmarks 2 were existing for %s, assuming empty.", jid); 252 module:log("debug", "Additionally, no bookmarks 2 were existing for %s, assuming empty.", jid);
251 module:fire_event("bookmarks/empty", { session = session }); 253 module:fire_event("bookmarks/empty", { session = session });
252 end 254 end
253 return; 255 return;