2 local dump = require "util.serialization".serialize; |
2 local dump = require "util.serialization".serialize; |
3 |
3 |
4 -- set allow_add_bookmarks = true; in config to enable the bookmark command |
4 -- set allow_add_bookmarks = true; in config to enable the bookmark command |
5 |
5 |
6 function riddim.plugins.bookmarks(bot) |
6 function riddim.plugins.bookmarks(bot) |
|
7 local my_bookmarks = {}; |
|
8 |
7 bot.stream:add_plugin("private"); |
9 bot.stream:add_plugin("private"); |
8 |
10 |
9 local function get_bookmarks(callback) |
11 local function get_bookmarks(callback) |
10 bot.stream:private_get("storage", "storage:bookmarks", function(storage) |
12 bot.stream:private_get("storage", "storage:bookmarks", function(storage) |
11 if not storage then |
13 if not storage then |
37 end |
39 end |
38 |
40 |
39 local function join_bookmarked_rooms() |
41 local function join_bookmarked_rooms() |
40 get_bookmarks(function(storage) |
42 get_bookmarks(function(storage) |
41 for i, room in ipairs(storage) do |
43 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 |
44 if room.name == "conference" and room.attr.jid then |
43 nick = room:get_child("nick"); |
45 my_bookmarks[room.attr.jid] = true; -- to know which rooms are bookmarked |
44 nick = nick and nick[1] or bot.config.nick; |
46 if room.attr.autojoin == "true" or room.attr.autojoin == "1" then |
45 bot:join_room(room.attr.jid, nick); |
47 nick = room:get_child("nick"); |
|
48 nick = nick and nick[1] or bot.config.nick; |
|
49 bot:join_room(room.attr.jid, nick); |
|
50 end |
46 -- TODO Passwords |
51 -- TODO Passwords |
47 -- Maybe get the hook in before the groupchat is loaded |
52 -- Maybe get the hook in before the groupchat is loaded |
48 -- and add to the bot.config.autojoin variable? |
53 -- and add to the bot.config.autojoin variable? |
49 end |
54 end |
50 end |
55 end |
54 bot:hook("started", join_bookmarked_rooms); |
59 bot:hook("started", join_bookmarked_rooms); |
55 |
60 |
56 if bot.config.allow_add_bookmarks then |
61 if bot.config.allow_add_bookmarks then |
57 bot:hook("commands/bookmark", function(command) |
62 bot:hook("commands/bookmark", function(command) |
58 local room = command.param and jid.bare(command.param) or command.room.jid; |
63 local room = command.param and jid.bare(command.param) or command.room.jid; |
|
64 if my_bookmarks[room] then return "Already bookmarked" end |
|
65 my_bookmarks[room] = true; |
|
66 |
59 add_bookmark({ jid = room, autojoin = "true" }, function() command:reply("Bookmarked " .. room) end); |
67 add_bookmark({ jid = room, autojoin = "true" }, function() command:reply("Bookmarked " .. room) end); |
60 end); |
68 end); |
61 end |
69 end |
62 |
70 |
63 end |
71 end |