Comparison

mod_group_bookmarks/mod_group_bookmarks.lua @ 289:415034fd38c2

mod_group_bookmarks: Module to inject room bookmarks to user private XML stores
author Matthew Wild <mwild1@gmail.com>
date Mon, 20 Dec 2010 16:24:13 +0000
child 290:ee416b285802
comparison
equal deleted inserted replaced
288:9233d7ee3c09 289:415034fd38c2
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local st = require "util.stanza"
11
12 local datamanager = require "util.datamanager"
13 local jid, datamanager = require "util.jid", require "util.datamanager";
14 local jid_bare, jid_prep, jid_split = jid.bare, jid.prep, jid.split;
15
16 local module_host = module:get_host();
17
18 local rooms;
19 local members;
20
21 local bookmarks_file;
22
23 module:add_feature("jabber:iq:private");
24
25 function inject_bookmarks(username, host, data)
26 local jid = username.."@"..host;
27 data:reset();
28 if members[jid] then
29 for _, room in ipairs(members[jid]) do
30 data:tag("conference", {
31 name = room;
32 jid = room;
33 autojoin = "1";
34 });
35 local nick = rooms[room][jid];
36 if nick then
37 data:tag("nick"):text(nick):up();
38 end
39 data:up();
40 end
41 end
42 return data;
43 end
44
45 module:hook("iq/self/jabber:iq:private:query", function(event)
46 local origin, stanza = event.origin, event.stanza;
47 local type = stanza.attr.type;
48 local query = stanza.tags[1];
49 if #query.tags == 1 then
50 local tag = query.tags[1];
51 local key = tag.name..":"..tag.attr.xmlns;
52 local data, err = datamanager.load(origin.username, origin.host, "private");
53 if err then
54 origin.send(st.error_reply(stanza, "wait", "internal-server-error"));
55 return true;
56 end
57 if stanza.attr.type == "get" then
58 local data = data and data[key];
59 if data then
60 data = st.deserialize(data);
61 if key == "storage:storage:bookmarks" then
62 data = inject_bookmarks(origin.username, origin.host, data);
63 end
64 origin.send(st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"})
65 :add_child(data));
66 else
67 origin.send(st.reply(stanza):add_child(stanza.tags[1]));
68 end
69 return true;
70 end
71 end
72 end, 1);
73
74 function module.load()
75 bookmarks_file = config.get(module:get_host(), "core", "group_bookmarks_file");
76 if not bookmarks_file then return; end
77
78 rooms = { default = {} };
79 members = { };
80 local curr_room;
81 for line in io.lines(bookmarks_file) do
82 if line:match("^%s*%[.-%]%s*$") then
83 curr_room = line:match("^%s*%[(.-)%]%s*$");
84 if curr_room:match("^%+") then
85 curr_room = curr_room:gsub("^%+", "");
86 if not members[false] then
87 members[false] = {};
88 end
89 members[false][#members[false]+1] = curr_room; -- Is a public group
90 end
91 module:log("debug", "New group: %s", tostring(curr_room));
92 rooms[curr_room] = rooms[curr_room] or {};
93 elseif curr_room then
94 -- Add JID
95 local entryjid, name = line:match("([^=]*)=?(.*)");
96 module:log("debug", "entryjid = '%s', name = '%s'", entryjid, name);
97 local jid;
98 jid = jid_prep(entryjid:match("%S+"));
99 if jid then
100 module:log("debug", "New member of %s: %s", tostring(curr_room), tostring(jid));
101 rooms[curr_room][jid] = name or false;
102 members[jid] = members[jid] or {};
103 members[jid][#members[jid]+1] = curr_room;
104 end
105 end
106 end
107 module:log("info", "Group bookmarks loaded successfully");
108 end
109