Comparison

mod_default_bookmarks/mod_default_bookmarks.lua @ 479:597c872d691e

mod_default_bookmarks: Serve a list of default bookmarks if the user has nil.
author Kim Alvefur <zash@zash.se>
date Fri, 25 Nov 2011 11:03:04 +0100
child 1324:853a382c9bd6
comparison
equal deleted inserted replaced
478:db0f065c4e09 479:597c872d691e
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 -- Copyright (C) 2011 Kim Alvefur
5 --
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
10
11 local st = require "util.stanza"
12
13 local dm_load = require "util.datamanager".load
14 local jid_split = require "util.jid".split
15
16 module:hook("iq/self/jabber:iq:private:query", function(event)
17 local origin, stanza = event.origin, event.stanza;
18 local typ = stanza.attr.type;
19 local from = stanza.attr.from;
20 local query = stanza.tags[1];
21 if #query.tags == 1 and typ == "get" then
22 local tag = query.tags[1];
23 local key = tag.name..":"..tag.attr.xmlns;
24 if key == "storage:storage:bookmarks" then
25 local data, err = dm_load(origin.username, origin.host, "private");
26 if not(data and data[key]) then
27 local bookmarks = module:get_option("default_bookmarks");
28 if bookmarks and #bookmarks > 0 then
29 local reply = st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"})
30 :tag("storage", { xmlns = "storage:bookmarks" });
31 local nick = jid_split(from);
32 for i=1,#bookmarks do
33 local bookmark = bookmarks[i];
34 if type(bookmark) ~= "table" then -- assume it's only a jid
35 bookmark = { jid = bookmark, name = jid_split(bookmark) };
36 end
37 reply:tag("conference", {
38 jid = bookmark.jid,
39 name = bookmark.name,
40 autojoin = "1",
41 }):tag("nick"):text(nick):up():up();
42 end
43 origin.send(reply);
44 return true;
45 end
46 end
47 end
48 end
49 end, 1);