__module_name__ = "bookmarks_lua" __module_author__ = "William D. Jones (cr1901)" __module_version__ = "0.1.0" __module_description__ = "Channel bookmarks in Lua." -- Utils -- https://stackoverflow.com/a/7615129 function split(inputstr, sep) if sep == nil then sep = "%s" end local t={} for str in string.gmatch(inputstr, "([^"..sep.."]+)") do table.insert(t, str) end return t end -- GUI function load_gui() -- For each channel in tree/tab view, add a bookmark GUI entry. hexchat.command('menu -p-2 add "$TAB/Bookmark" "bookmark %s"') -- Same, but for channels referenced inside the text area. hexchat.command('menu add "$CHAN/Bookmark Channel" "bookmark %s"') -- Same, but for menu bar. hexchat.command('menu -p-3 add "Bookmarks"') hexchat.command('menu add "Bookmarks/-"') hexchat.command('menu add "Bookmarks/Add or Remove Current Channel" "bookmark"') for k, v in pairs(hexchat.pluginprefs) do if k:sub(0,9) == "channels_" then chan = k:sub(10) pref = "channels_" .. chan network_str = hexchat.pluginprefs[pref] if network_str == nil then network_str = "" end network_tab = split(network_str, ",") for _, v in pairs(network_tab) do hexchat.command(string.format('menu -p-2 add "Bookmarks/%s', v)) hexchat.command(string.format('menu add "Bookmarks/%s/%s" "netjoin %s %s"', v, chan, chan, v)) end end end end function toggle_bookmark(chan, network) -- Sometimes, we don't have the channel, and need to get it back. if chan == nil then chan = hexchat.get_info('channel') end if chan == '' then return end -- Sometimes, we also don't have the network (from e.g. Tree View), and -- need to get it back. if network == nil then local ctx = hexchat.find_context(nil, chan) network = ctx:get_info('network') end -- Only store channels (type 2) in bookmarks. for c in hexchat.iterate("channels") do if c.channel == chan then if c.type ~= 2 then return end end end local pref = "channels_" .. chan local network_str = hexchat.pluginprefs[pref] or "" local network_tab = split(network_str, ",") local network_found = false for _, v in pairs(network_tab) do if network == v then network_found = true break end end if not network_found then -- Don't prepend comma if network string is being initialized. if network_str == "" then network_str = network else network_str = network_str .. "," .. network end hexchat.command(string.format('menu -p-2 add "Bookmarks/%s', network)) hexchat.command(string.format('menu add "Bookmarks/%s/%s" "netjoin %s %s"', network, chan, chan, network)) hexchat.pluginprefs[pref] = network_str else if #network_tab == 1 then hexchat.pluginprefs[pref] = nil else -- Remove only the current network from the bookmarks list. network_str = "" for _, v in pairs(network_tab) do if network ~= v then if network_str == "" then network_str = v else network_str = network_str .. "," .. v end end end hexchat.pluginprefs[pref] = network_str end hexchat.command(string.format('menu del "Bookmarks/%s/%s"', network, chan)) end end -- Callbacks function load_cbs() hexchat.hook_command("bookmark", bookmark_cb, 'Usage: bookmark [channel] [network]\n\tToggles a bookmark.') hexchat.hook_command("netjoin", netjoin_cb, 'Usage: netjoin ') hexchat.hook_unload(unload_cb) end function unload_cb() hexchat.command('menu del "Bookmarks"') hexchat.command('menu del "$TAB/Bookmark"') hexchat.command('menu del "$CHAN/Bookmark Channel"') print(__module_name__ .. ' version ' .. __module_version__ .. ' unloaded.') end function netjoin_cb(word, word_eol) joinchan = word[2] joinnet = word_eol[3] for c in hexchat.iterate("channels") do if c.network == joinnet then c.context:command(string.format('join %s', joinchan)) return hexchat.EAT_ALL end end -- Not found, connect to network automatically. hexchat.command(string.format('url irc://"%s"/%s', joinnet, joinchan)) return hexchat.EAT_ALL end -- No userdata for Lua API. function bookmark_cb(word, word_eol) local chan = word[2] local network = word[3] toggle_bookmark(chan, network) return hexchat.EAT_ALL end -- Init -- Without this, plugin unloads immediately. hexchat.register(__module_name__, __module_version__, __module_description__) load_gui() load_cbs() print(__module_name__ .. ' version ' .. __module_version__ .. ' loaded.')