plugins/bookmarks.lua

Thu, 22 Oct 2020 15:37:43 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Thu, 22 Oct 2020 15:37:43 +0100
changeset 161
c4df517edbc1
parent 59
dcb0eb6c32b6
permissions
-rw-r--r--

config.docker.lua: Require RIDDIM_DEBUG=1 to enable debug mode

48
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local st = require "util.stanza";
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 -- set allow_add_bookmarks = true; in config to enable the bookmark command
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 function riddim.plugins.bookmarks(bot)
49
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
6 local my_bookmarks = {};
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
7
48
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 bot.stream:add_plugin("private");
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local function get_bookmarks(callback)
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 bot.stream:private_get("storage", "storage:bookmarks", function(storage)
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 if not storage then
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 storage = st.tag("storage", { xmlns = "storage:bookmarks" });
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 if callback and type(callback) == "function" then
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 callback(storage);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 end);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local function add_bookmark(bookmark, callback)
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 -- TODO Check if a room is bookmarked already
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 if not bookmark or type(bookmark) ~= "table" or not bookmark.jid then return end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 if not bookmark.name then
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 bookmark.name = jid.split(bookmark.jid);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 local nick = bot.config.nick;
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 if bookmark.nick then
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 nick = bookmark.nick;
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 bookmark.nick = nil;
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 get_bookmarks(function(storage)
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 storage:tag("conference", bookmark)
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 :tag("nick"):text(nick):up()
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 :up();
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 bot.stream:private_set("storage", "storage:bookmarks", storage, callback);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 local function join_bookmarked_rooms()
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 get_bookmarks(function(storage)
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 for i, room in ipairs(storage) do
49
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
43 if room.name == "conference" and room.attr.jid then
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
44 my_bookmarks[room.attr.jid] = true; -- to know which rooms are bookmarked
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
45 if room.attr.autojoin == "true" or room.attr.autojoin == "1" then
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
46 nick = room:get_child("nick");
59
dcb0eb6c32b6 plugins.bookmarks: Remove redundant config reference
Kim Alvefur <zash@zash.se>
parents: 50
diff changeset
47 nick = nick and nick[1] or nil;
49
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
48 bot:join_room(room.attr.jid, nick);
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
49 end
48
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 -- TODO Passwords
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 -- Maybe get the hook in before the groupchat is loaded
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 -- and add to the bot.config.autojoin variable?
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 bot:hook("started", join_bookmarked_rooms);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 if bot.config.allow_add_bookmarks then
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 bot:hook("commands/bookmark", function(command)
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 local room = command.param and jid.bare(command.param) or command.room.jid;
49
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
63 if my_bookmarks[room] then return "Already bookmarked" end
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
64 my_bookmarks[room] = true;
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
65
48
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 add_bookmark({ jid = room, autojoin = "true" }, function() command:reply("Bookmarked " .. room) end);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 end

mercurial