plugins/bookmarks.lua

Sun, 21 Nov 2010 00:37:29 +0100

author
Kim Alvefur <zash@zash.se>
date
Sun, 21 Nov 2010 00:37:29 +0100
changeset 49
45d31a4f9c5a
parent 48
222b0b70baf3
child 50
cd9b25249098
permissions
-rw-r--r--

plugins.bookmarks: Don't allow to bookmark the same room twice

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 local dump = require "util.serialization".serialize;
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- 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
5
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 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
7 local my_bookmarks = {};
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
8
48
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 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
10
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 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
12 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
13 if not storage then
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 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
15 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 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
17 callback(storage);
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 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 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
23 -- 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
24 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
25 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
26 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
27 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 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
29 if bookmark.nick then
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 nick = bookmark.nick;
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 bookmark.nick = nil;
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 get_bookmarks(function(storage)
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 storage:tag("conference", bookmark)
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 :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
36 :up();
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 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
38 end);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 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
42 get_bookmarks(function(storage)
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 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
44 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
45 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
46 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
47 nick = room:get_child("nick");
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
48 nick = nick and nick[1] or bot.config.nick;
45d31a4f9c5a plugins.bookmarks: Don't allow to bookmark the same room twice
Kim Alvefur <zash@zash.se>
parents: 48
diff changeset
49 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
50 end
48
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 -- TODO Passwords
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 -- 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
53 -- 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
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 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 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
60
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 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
62 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
63 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
64 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
65 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
66
48
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 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
68 end);
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69 end
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70
222b0b70baf3 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 end

mercurial