mod_muc_webchat_url: Fix default url
[prosody-modules.git] / mod_default_bookmarks / mod_default_bookmarks.lua
blobfdd065b537fd4e1f79cf01e5082f56b11eb7166a
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2011 Kim Alvefur
5 -- Copyright (C) 2018 Emmanuel Gil Peyrot
6 --
7 -- This project is MIT/X11 licensed. Please see the
8 -- COPYING file in the source package for more information.
9 --
11 local st = require "util.stanza"
12 local dm_load = require "util.datamanager".load
13 local jid_split = require "util.jid".split
15 -- COMPAT w/trunk
16 local is_on_trunk = false;
17 local mm = require "core.modulemanager";
18 if mm.get_modules_for_host then
19 if mm.get_modules_for_host(module.host):contains("bookmarks") then
20 is_on_trunk = true;
21 end
22 end
24 local function get_default_bookmarks(nickname)
25 local bookmarks = module:get_option("default_bookmarks");
26 if not bookmarks or #bookmarks == 0 then
27 return false;
28 end
29 local reply = st.stanza("storage", { xmlns = "storage:bookmarks" });
30 local nick = nickname and st.stanza("nick"):text(nickname);
31 for _, bookmark in ipairs(bookmarks) do
32 if type(bookmark) ~= "table" then -- assume it's only a jid
33 bookmark = { jid = bookmark, name = jid_split(bookmark) };
34 end
35 reply:tag("conference", {
36 jid = bookmark.jid,
37 name = bookmark.name,
38 autojoin = "1",
39 });
40 if nick then
41 reply:add_child(nick):up();
42 end
43 if bookmark.password then
44 reply:tag("password"):text(bookmark.password):up();
45 end
46 reply:up();
47 end
48 return reply;
49 end
51 if is_on_trunk then
52 local mod_bookmarks = module:depends "bookmarks";
53 local function on_bookmarks_empty(event)
54 local session = event.session;
55 local bookmarks = get_default_bookmarks(session.username);
56 if bookmarks then
57 mod_bookmarks.publish_to_pep(session.full_jid, bookmarks);
58 end
59 end
60 module:hook("bookmarks/empty", on_bookmarks_empty);
61 else
62 local function on_private_xml_get(event)
63 local origin, stanza = event.origin, event.stanza;
64 local tag = stanza.tags[1].tags[1];
65 local key = tag.name..":"..tag.attr.xmlns;
66 if key ~= "storage:storage:bookmarks" then
67 return;
68 end
70 local data, err = dm_load(origin.username, origin.host, "private");
71 if data and data[key] then
72 return;
73 end
75 local bookmarks = get_default_bookmarks(origin.username);
76 if not bookmarks then
77 return;
78 end;
80 local reply = st.reply(stanza):tag("query", { xmlns = "jabber:iq:private" })
81 :add_child(bookmarks);
82 origin.send(reply);
83 return true;
84 end
85 module:hook("iq-get/self/jabber:iq:private:query", on_private_xml_get, 1);
86 end