comparison mod_default_bookmarks/mod_default_bookmarks.lua @ 4860:4a34ed2bb9a2

mod_default_bookmarks: Improve validation
author Kim Alvefur <zash@zash.se>
date Sat, 08 Jan 2022 02:28:16 +0100
parents 33208c3ae586
children 62006f4022e9
comparison
equal deleted inserted replaced
4859:33208c3ae586 4860:4a34ed2bb9a2
8 -- COPYING file in the source package for more information. 8 -- COPYING file in the source package for more information.
9 -- 9 --
10 10
11 local st = require "util.stanza" 11 local st = require "util.stanza"
12 local dm_load = require "util.datamanager".load 12 local dm_load = require "util.datamanager".load
13 local jid_split = require "util.jid".split 13 local jid = require "util.jid"
14 14
15 -- COMPAT w/trunk 15 -- COMPAT w/trunk
16 local mod_bookmarks_available = false; 16 local mod_bookmarks_available = false;
17 local mm = require "core.modulemanager"; 17 local mm = require "core.modulemanager";
18 if mm.get_modules_for_host then 18 if mm.get_modules_for_host then
23 mod_bookmarks_available = "bookmarks2"; 23 mod_bookmarks_available = "bookmarks2";
24 end 24 end
25 end 25 end
26 26
27 local function get_default_bookmarks(nickname) 27 local function get_default_bookmarks(nickname)
28 local bookmarks = module:get_option("default_bookmarks"); 28 local bookmarks = module:get_option_array("default_bookmarks");
29 if not bookmarks or #bookmarks == 0 then 29 if not bookmarks or #bookmarks == 0 then
30 return false; 30 return false;
31 end 31 end
32 local reply = st.stanza("storage", { xmlns = "storage:bookmarks" }); 32 local reply = st.stanza("storage", { xmlns = "storage:bookmarks" });
33 local nick = nickname and st.stanza("nick"):text(nickname); 33 local nick = nickname and st.stanza("nick"):text(nickname);
34 for _, bookmark in ipairs(bookmarks) do 34 for _, bookmark in ipairs(bookmarks) do
35 if type(bookmark) ~= "table" then -- assume it's only a jid 35 if type(bookmark) ~= "table" then -- assume it's only a jid
36 bookmark = { jid = bookmark, name = jid_split(bookmark) }; 36 bookmark = { jid = bookmark, name = jid.split(bookmark) };
37 end 37 end
38 reply:tag("conference", { 38 reply:tag("conference", {
39 jid = bookmark.jid, 39 jid = bookmark.jid,
40 name = bookmark.name, 40 name = bookmark.name,
41 autojoin = "1", 41 autojoin = "1",
77 -- 0.11 or earlier not supporting max_items="max" trows an error here 77 -- 0.11 or earlier not supporting max_items="max" trows an error here
78 module:log("debug", "Setting max_items=pep_max_items because 'max' is not supported in this version"); 78 module:log("debug", "Setting max_items=pep_max_items because 'max' is not supported in this version");
79 publish_options["max_items"] = module:get_option_number("pep_max_items", 256); 79 publish_options["max_items"] = module:get_option_number("pep_max_items", 256);
80 end 80 end
81 local service = mod_pep.get_pep_service(session.username); 81 local service = mod_pep.get_pep_service(session.username);
82 local bookmarks = module:get_option("default_bookmarks"); 82 local bookmarks = module:get_option_array("default_bookmarks");
83 local ns = event.version or "urn:xmpp:bookmarks:1"; 83 local ns = event.version or "urn:xmpp:bookmarks:1";
84 for i, bookmark in ipairs(bookmarks) do 84 for i, bookmark in ipairs(bookmarks) do
85 local item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub"; id = bookmark.jid }); 85 local bm_jid = jid.prep(bookmark.jid);
86 item:tag("conference", { xmlns = ns; name = bookmark.name; autojoin = bookmark.autojoin and "true" or nil }); 86 if not bm_jid then
87 if bookmark.nick then item:text_tag("nick", bookmarks.nick); end 87 module:log("error", "Invalid JID in default_bookmarks[%d].jid = %q", i, bookmark.jid);
88 if bookmark.password then item:text_tag("password", bookmarks.password); end 88 else
89 service:publish("urn:xmpp:bookmarks:1", session.full_jid, bookmark.jid, item, publish_options); 89 local item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub"; id = bm_jid });
90 item:tag("conference", { xmlns = ns; name = bookmark.name; autojoin = bookmark.autojoin and "true" or nil });
91 if bookmark.nick then item:text_tag("nick", bookmarks.nick); end
92 if bookmark.password then item:text_tag("password", bookmarks.password); end
93 local ok, err = service:publish("urn:xmpp:bookmarks:1", session.full_jid, bm_jid, item, publish_options);
94 if not ok then
95 module:log("error", "Could not add default bookmark %s to %s: %s", bm_jid, session.username, err);
96 end
97 end
90 end 98 end
91 end 99 end
92 module:hook("bookmarks/empty", publish_bookmarks2); 100 module:hook("bookmarks/empty", publish_bookmarks2);
93 end 101 end
94 else 102 else