plugins/bookmarks.lua

changeset 48
222b0b70baf3
child 49
45d31a4f9c5a
equal deleted inserted replaced
47:b84572a2e6cb 48:222b0b70baf3
1 local st = require "util.stanza";
2 local dump = require "util.serialization".serialize;
3
4 -- set allow_add_bookmarks = true; in config to enable the bookmark command
5
6 function riddim.plugins.bookmarks(bot)
7 bot.stream:add_plugin("private");
8
9 local function get_bookmarks(callback)
10 bot.stream:private_get("storage", "storage:bookmarks", function(storage)
11 if not storage then
12 storage = st.tag("storage", { xmlns = "storage:bookmarks" });
13 end
14 if callback and type(callback) == "function" then
15 callback(storage);
16 end
17 end);
18 end
19
20 local function add_bookmark(bookmark, callback)
21 -- TODO Check if a room is bookmarked already
22 if not bookmark or type(bookmark) ~= "table" or not bookmark.jid then return end
23 if not bookmark.name then
24 bookmark.name = jid.split(bookmark.jid);
25 end
26 local nick = bot.config.nick;
27 if bookmark.nick then
28 nick = bookmark.nick;
29 bookmark.nick = nil;
30 end
31 get_bookmarks(function(storage)
32 storage:tag("conference", bookmark)
33 :tag("nick"):text(nick):up()
34 :up();
35 bot.stream:private_set("storage", "storage:bookmarks", storage, callback);
36 end);
37 end
38
39 local function join_bookmarked_rooms()
40 get_bookmarks(function(storage)
41 for i, room in ipairs(storage) do
42 if room.name == "conference" and room.attr.jid and room.attr.autojoin == "true" or room.attr.autojoin == "1" then
43 nick = room:get_child("nick");
44 nick = nick and nick[1] or bot.config.nick;
45 bot:join_room(room.attr.jid, nick);
46 -- TODO Passwords
47 -- Maybe get the hook in before the groupchat is loaded
48 -- and add to the bot.config.autojoin variable?
49 end
50 end
51 end);
52 end
53
54 bot:hook("started", join_bookmarked_rooms);
55
56 if bot.config.allow_add_bookmarks then
57 bot:hook("commands/bookmark", function(command)
58 local room = command.param and jid.bare(command.param) or command.room.jid;
59 add_bookmark({ jid = room, autojoin = "true" }, function() command:reply("Bookmarked " .. room) end);
60 end);
61 end
62
63 end

mercurial