# HG changeset patch # User Kim Alvefur # Date 1290275516 -3600 # Node ID 222b0b70baf322ba734e5aa58ccdeb918f90a241 # Parent b84572a2e6cb79c818fe0bd81e0e9cbbcc7e3157 plugins.bookmarks: New plugin that makes the bot autojoin bookmarked rooms. diff -r b84572a2e6cb -r 222b0b70baf3 plugins/bookmarks.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/bookmarks.lua Sat Nov 20 18:51:56 2010 +0100 @@ -0,0 +1,63 @@ +local st = require "util.stanza"; +local dump = require "util.serialization".serialize; + +-- set allow_add_bookmarks = true; in config to enable the bookmark command + +function riddim.plugins.bookmarks(bot) + bot.stream:add_plugin("private"); + + local function get_bookmarks(callback) + bot.stream:private_get("storage", "storage:bookmarks", function(storage) + if not storage then + storage = st.tag("storage", { xmlns = "storage:bookmarks" }); + end + if callback and type(callback) == "function" then + callback(storage); + end + end); + end + + local function add_bookmark(bookmark, callback) + -- TODO Check if a room is bookmarked already + if not bookmark or type(bookmark) ~= "table" or not bookmark.jid then return end + if not bookmark.name then + bookmark.name = jid.split(bookmark.jid); + end + local nick = bot.config.nick; + if bookmark.nick then + nick = bookmark.nick; + bookmark.nick = nil; + end + get_bookmarks(function(storage) + storage:tag("conference", bookmark) + :tag("nick"):text(nick):up() + :up(); + bot.stream:private_set("storage", "storage:bookmarks", storage, callback); + end); + end + + local function join_bookmarked_rooms() + get_bookmarks(function(storage) + for i, room in ipairs(storage) do + if room.name == "conference" and room.attr.jid and room.attr.autojoin == "true" or room.attr.autojoin == "1" then + nick = room:get_child("nick"); + nick = nick and nick[1] or bot.config.nick; + bot:join_room(room.attr.jid, nick); + -- TODO Passwords + -- Maybe get the hook in before the groupchat is loaded + -- and add to the bot.config.autojoin variable? + end + end + end); + end + + bot:hook("started", join_bookmarked_rooms); + + if bot.config.allow_add_bookmarks then + bot:hook("commands/bookmark", function(command) + local room = command.param and jid.bare(command.param) or command.room.jid; + add_bookmark({ jid = room, autojoin = "true" }, function() command:reply("Bookmarked " .. room) end); + end); + end + +end