Software /
code /
prosody-modules
Comparison
mod_bookmarks2/mod_bookmarks2.lua @ 4837:1f1acb7f3c10
mod_bookmarks2: Use constants for namespaces
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Sun, 26 Dec 2021 14:51:02 +0100 |
parent | 4835:60b2dbe032c0 |
child | 4838:fd2e48d4ac94 |
comparison
equal
deleted
inserted
replaced
4836:5fc306239db3 | 4837:1f1acb7f3c10 |
---|---|
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 | 11 |
12 local namespace = "urn:xmpp:bookmarks:1"; | 12 local namespace = "urn:xmpp:bookmarks:1"; |
13 local namespace_private = "jabber:iq:private"; | |
14 local namespace_legacy = "storage:bookmarks"; | |
13 | 15 |
14 local default_options = { | 16 local default_options = { |
15 ["persist_items"] = true; | 17 ["persist_items"] = true; |
16 -- This should be much higher, the XEP recommends 10000 but mod_pep rejects that. | 18 -- This should be much higher, the XEP recommends 10000 but mod_pep rejects that. |
17 ["max_items"] = "max"; | 19 ["max_items"] = "max"; |
30 event.reply:tag("feature", { var = namespace.."#compat" }):up(); | 32 event.reply:tag("feature", { var = namespace.."#compat" }):up(); |
31 event.reply:tag("feature", { var = namespace.."#compat-pep" }):up(); | 33 event.reply:tag("feature", { var = namespace.."#compat-pep" }):up(); |
32 end); | 34 end); |
33 | 35 |
34 local function generate_legacy_storage(items) | 36 local function generate_legacy_storage(items) |
35 local storage = st.stanza("storage", { xmlns = "storage:bookmarks" }); | 37 local storage = st.stanza("storage", { xmlns = namespace_legacy }); |
36 for _, item_id in ipairs(items) do | 38 for _, item_id in ipairs(items) do |
37 local item = items[item_id]; | 39 local item = items[item_id]; |
38 local conference = st.stanza("conference"); | |
39 conference.attr.jid = item.attr.id; | |
40 local bookmark = item:get_child("conference", namespace); | 40 local bookmark = item:get_child("conference", namespace); |
41 conference.attr.name = bookmark.attr.name; | 41 local conference = st.stanza("conference", { |
42 conference.attr.autojoin = bookmark.attr.autojoin; | 42 jid = item.attr.id, |
43 name = bookmark.attr.name, | |
44 autojoin = bookmark.attr.autojoin, | |
45 }); | |
43 local nick = bookmark:get_child_text("nick"); | 46 local nick = bookmark:get_child_text("nick"); |
44 if nick ~= nil then | 47 if nick ~= nil then |
45 conference:text_tag("nick", nick, { xmlns = "storage:bookmarks" }):up(); | 48 conference:text_tag("nick", nick):up(); |
46 end | 49 end |
47 local password = bookmark:get_child_text("password"); | 50 local password = bookmark:get_child_text("password"); |
48 if password ~= nil then | 51 if password ~= nil then |
49 conference:text_tag("password", password):up(); | 52 conference:text_tag("password", password):up(); |
50 end | 53 end |
65 if items == nil then | 68 if items == nil then |
66 return; | 69 return; |
67 end | 70 end |
68 | 71 |
69 local node = items.attr.node; | 72 local node = items.attr.node; |
70 if node ~= "storage:bookmarks" then | 73 if node ~= namespace_legacy then |
71 return; | 74 return; |
72 end | 75 end |
73 | 76 |
74 local username = session.username; | 77 local username = session.username; |
75 local jid = username.."@"..session.host; | 78 local jid = username.."@"..session.host; |
89 local storage = generate_legacy_storage(ret); | 92 local storage = generate_legacy_storage(ret); |
90 | 93 |
91 module:log("debug", "Sending back legacy PEP for %s: %s", jid, storage); | 94 module:log("debug", "Sending back legacy PEP for %s: %s", jid, storage); |
92 session.send(st.reply(stanza) | 95 session.send(st.reply(stanza) |
93 :tag("pubsub", {xmlns = "http://jabber.org/protocol/pubsub"}) | 96 :tag("pubsub", {xmlns = "http://jabber.org/protocol/pubsub"}) |
94 :tag("items", {node = "storage:bookmarks"}) | 97 :tag("items", {node = namespace_legacy}) |
95 :tag("item", {id = "current"}) | 98 :tag("item", {id = "current"}) |
96 :add_child(storage)); | 99 :add_child(storage)); |
97 return true; | 100 return true; |
98 end | 101 end |
99 | 102 |
100 local function on_retrieve_private_xml(event) | 103 local function on_retrieve_private_xml(event) |
101 local stanza, session = event.stanza, event.origin; | 104 local stanza, session = event.stanza, event.origin; |
102 local query = stanza:get_child("query", "jabber:iq:private"); | 105 local query = stanza:get_child("query", namespace_private); |
103 if query == nil then | 106 if query == nil then |
104 return; | 107 return; |
105 end | 108 end |
106 | 109 |
107 local bookmarks = query:get_child("storage", "storage:bookmarks"); | 110 local bookmarks = query:get_child("storage", namespace_legacy); |
108 if bookmarks == nil then | 111 if bookmarks == nil then |
109 return; | 112 return; |
110 end | 113 end |
111 | 114 |
112 module:log("debug", "Getting private bookmarks: %s", bookmarks); | 115 module:log("debug", "Getting private bookmarks: %s", bookmarks); |
127 end | 130 end |
128 | 131 |
129 local storage = generate_legacy_storage(ret); | 132 local storage = generate_legacy_storage(ret); |
130 | 133 |
131 module:log("debug", "Sending back private for %s: %s", jid, storage); | 134 module:log("debug", "Sending back private for %s: %s", jid, storage); |
132 session.send(st.reply(stanza):query("jabber:iq:private"):add_child(storage)); | 135 session.send(st.reply(stanza):query(namespace_private):add_child(storage)); |
133 return true; | 136 return true; |
134 end | 137 end |
135 | 138 |
136 local function compare_bookmark2(a, b) | 139 local function compare_bookmark2(a, b) |
137 if a == nil or b == nil then | 140 if a == nil or b == nil then |
187 local to_remove = {}; | 190 local to_remove = {}; |
188 for i in ipairs(bookmarks2) do | 191 for i in ipairs(bookmarks2) do |
189 to_remove[bookmarks2[i]] = true; | 192 to_remove[bookmarks2[i]] = true; |
190 end | 193 end |
191 | 194 |
192 for bookmark in bookmarks:childtags("conference", "storage:bookmarks") do | 195 for bookmark in bookmarks:childtags("conference", namespace_legacy) do |
193 -- Create the new conference element by copying everything from the legacy one. | 196 -- Create the new conference element by copying everything from the legacy one. |
194 local conference = st.stanza("conference", { xmlns = namespace }); | 197 local conference = st.stanza("conference", { |
195 conference.attr.name = bookmark.attr.name; | 198 xmlns = namespace, |
196 conference.attr.autojoin = bookmark.attr.autojoin; | 199 name = bookmark.attr.name, |
197 local nick = bookmark:get_child_text("nick", "storage:bookmarks"); | 200 autojoin = bookmark.attr.autojoin, |
201 }); | |
202 local nick = bookmark:get_child_text("nick"); | |
198 if nick ~= nil then | 203 if nick ~= nil then |
199 conference:text_tag("nick", nick):up(); | 204 conference:text_tag("nick", nick):up(); |
200 end | 205 end |
201 local password = bookmark:get_child_text("password", "storage:bookmarks"); | 206 local password = bookmark:get_child_text("password"); |
202 if password ~= nil then | 207 if password ~= nil then |
203 conference:text_tag("password", password):up(); | 208 conference:text_tag("password", password):up(); |
204 end | 209 end |
205 | 210 |
206 -- Create its wrapper. | 211 -- Create its wrapper. |
247 if pubsub == nil then | 252 if pubsub == nil then |
248 return; | 253 return; |
249 end | 254 end |
250 | 255 |
251 local publish = pubsub:get_child("publish"); | 256 local publish = pubsub:get_child("publish"); |
252 if publish == nil or publish.attr.node ~= "storage:bookmarks" then | 257 if publish == nil or publish.attr.node ~= namespace_legacy then |
253 return; | 258 return; |
254 end | 259 end |
255 | 260 |
256 local item = publish:get_child("item"); | 261 local item = publish:get_child("item"); |
257 if item == nil then | 262 if item == nil then |
258 return; | 263 return; |
259 end | 264 end |
260 | 265 |
261 -- Here we ignore the item id, it’ll be generated as 'current' anyway. | 266 -- Here we ignore the item id, it’ll be generated as 'current' anyway. |
262 | 267 |
263 local bookmarks = item:get_child("storage", "storage:bookmarks"); | 268 local bookmarks = item:get_child("storage", namespace_legacy); |
264 if bookmarks == nil then | 269 if bookmarks == nil then |
265 return; | 270 return; |
266 end | 271 end |
267 | 272 |
268 -- We also ignore the publish-options. | 273 -- We also ignore the publish-options. |
281 end | 286 end |
282 | 287 |
283 -- Synchronise Private XML to PEP. | 288 -- Synchronise Private XML to PEP. |
284 local function on_publish_private_xml(event) | 289 local function on_publish_private_xml(event) |
285 local stanza, session = event.stanza, event.origin; | 290 local stanza, session = event.stanza, event.origin; |
286 local query = stanza:get_child("query", "jabber:iq:private"); | 291 local query = stanza:get_child("query", namespace_private); |
287 if query == nil then | 292 if query == nil then |
288 return; | 293 return; |
289 end | 294 end |
290 | 295 |
291 local bookmarks = query:get_child("storage", "storage:bookmarks"); | 296 local bookmarks = query:get_child("storage", namespace_legacy); |
292 if bookmarks == nil then | 297 if bookmarks == nil then |
293 return; | 298 return; |
294 end | 299 end |
295 | 300 |
296 module:log("debug", "Private bookmarks set by client, publishing to PEP."); | 301 module:log("debug", "Private bookmarks set by client, publishing to PEP."); |
310 local session = event.session; | 315 local session = event.session; |
311 local username = session.username; | 316 local username = session.username; |
312 local service = mod_pep.get_pep_service(username); | 317 local service = mod_pep.get_pep_service(username); |
313 local jid = username.."@"..session.host; | 318 local jid = username.."@"..session.host; |
314 | 319 |
315 local ok, ret = service:get_items("storage:bookmarks", session.full_jid); | 320 local ok, ret = service:get_items(namespace_legacy, session.full_jid); |
316 if ok then | 321 if ok then |
317 module:log("debug", "Legacy PEP bookmarks found for %s, migrating.", jid); | 322 module:log("debug", "Legacy PEP bookmarks found for %s, migrating.", jid); |
318 local failed = false; | 323 local failed = false; |
319 for _, item_id in ipairs(ret) do | 324 for _, item_id in ipairs(ret) do |
320 local item = ret[item_id]; | 325 local item = ret[item_id]; |
321 if item.attr.id ~= "current" then | 326 if item.attr.id ~= "current" then |
322 module:log("warn", "Legacy PEP bookmarks for %s isn’t using 'current' as its id: %s", jid, item.attr.id); | 327 module:log("warn", "Legacy PEP bookmarks for %s isn’t using 'current' as its id: %s", jid, item.attr.id); |
323 end | 328 end |
324 local bookmarks = item:get_child("storage", "storage:bookmarks"); | 329 local bookmarks = item:get_child("storage", namespace_legacy); |
325 module:log("debug", "Got legacy PEP bookmarks of %s: %s", jid, bookmarks); | 330 module:log("debug", "Got legacy PEP bookmarks of %s: %s", jid, bookmarks); |
326 | 331 |
327 local ok, err = publish_to_pep(session.full_jid, bookmarks, false); | 332 local ok, err = publish_to_pep(session.full_jid, bookmarks, false); |
328 if not ok then | 333 if not ok then |
329 module:log("error", "Failed to store legacy PEP bookmarks to bookmarks 2 for %s, aborting migration: %s", jid, err); | 334 module:log("error", "Failed to store legacy PEP bookmarks to bookmarks 2 for %s, aborting migration: %s", jid, err); |
331 break; | 336 break; |
332 end | 337 end |
333 end | 338 end |
334 if not failed then | 339 if not failed then |
335 module:log("debug", "Successfully migrated legacy PEP bookmarks of %s to bookmarks 2, attempting deletion of the node.", jid); | 340 module:log("debug", "Successfully migrated legacy PEP bookmarks of %s to bookmarks 2, attempting deletion of the node.", jid); |
336 local ok, err = service:delete("storage:bookmarks", jid); | 341 local ok, err = service:delete(namespace_legacy, jid); |
337 if not ok then | 342 if not ok then |
338 module:log("error", "Failed to delete legacy PEP bookmarks for %s: %s", jid, err); | 343 module:log("error", "Failed to delete legacy PEP bookmarks for %s: %s", jid, err); |
339 end | 344 end |
340 end | 345 end |
341 end | 346 end |
369 module:log("debug", "Removed legacy bookmarks of %s, migration done!", jid); | 374 module:log("debug", "Removed legacy bookmarks of %s, migration done!", jid); |
370 end | 375 end |
371 | 376 |
372 local function on_node_created(event) | 377 local function on_node_created(event) |
373 local service, node, actor = event.service, event.node, event.actor; | 378 local service, node, actor = event.service, event.node, event.actor; |
374 if node ~= "storage:bookmarks" then | 379 if node ~= namespace_legacy then |
375 return; | 380 return; |
376 end | 381 end |
377 | 382 |
378 module:log("debug", "Something tried to create legacy PEP bookmarks for %s.", actor); | 383 module:log("debug", "Something tried to create legacy PEP bookmarks for %s.", actor); |
379 local ok, err = service:delete("storage:bookmarks", actor); | 384 local ok, err = service:delete(namespace_legacy, actor); |
380 if not ok then | 385 if not ok then |
381 module:log("error", "Failed to delete legacy PEP bookmarks for %s: %s", actor, err); | 386 module:log("error", "Failed to delete legacy PEP bookmarks for %s: %s", actor, err); |
382 end | 387 end |
383 module:log("debug", "Legacy PEP bookmarks node of %s deleted.", actor); | 388 module:log("debug", "Legacy PEP bookmarks node of %s deleted.", actor); |
384 end | 389 end |