comparison mod_default_bookmarks/mod_default_bookmarks.lua @ 3236:73906187f964

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