Sat, 20 Nov 2010 18:51:56 +0100
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
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) |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 | 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
|
8 | |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 | 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
|
10 | 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
|
11 | if not storage then |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 | 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
|
13 | end |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 | 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
|
15 | callback(storage); |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 | end |
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 | |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 | 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
|
21 | -- 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
|
22 | 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
|
23 | 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
|
24 | 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
|
25 | end |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 | 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
|
27 | if bookmark.nick then |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 | nick = bookmark.nick; |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 | bookmark.nick = nil; |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 | end |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 | get_bookmarks(function(storage) |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 | storage:tag("conference", bookmark) |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 | :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
|
34 | :up(); |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 | 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
|
36 | end); |
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 | |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 | 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
|
40 | get_bookmarks(function(storage) |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 | for i, room in ipairs(storage) do |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 | if room.name == "conference" and room.attr.jid and room.attr.autojoin == "true" or room.attr.autojoin == "1" then |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 | nick = room:get_child("nick"); |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 | nick = nick and nick[1] or bot.config.nick; |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 | bot:join_room(room.attr.jid, nick); |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 | -- TODO Passwords |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 | -- 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
|
48 | -- 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
|
49 | end |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 | end |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 | end); |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 | end |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 | |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 | 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
|
55 | |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 | 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
|
57 | 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
|
58 | local room = command.param and jid.bare(command.param) or command.room.jid; |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 | 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
|
60 | end); |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 | end |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 | |
222b0b70baf3
plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 | end |