Created
May 31, 2017 18:37
-
-
Save facboy/c137da65f9a072c35b665d4e02299eab to your computer and use it in GitHub Desktop.
Modification of https://github.com/TingPing/plugins/blob/master/HexChat/playback.lua to store state locally
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local lgi = require('lgi') | |
local GLib = lgi.require('GLib') | |
hexchat.register('Playback', '1', "Integration with ZNC's Playback module") | |
--[[ | |
This should behave like this: | |
On connect (end of MOTD): | |
if new server, play all | |
if old server, play all after latest timestamp | |
On close query: | |
clear all | |
On new message: | |
update latest timestamp | |
]] | |
local CAP_NAME = 'znc.in/playback' | |
local servers = hexchat.pluginprefs -- Table of id to timestamp | |
-- Request capability | |
hexchat.hook_print('Capability List', function (args) | |
if args[2]:find(CAP_NAME) then | |
hexchat.command('quote CAP REQ :' .. CAP_NAME) | |
local ctx = hexchat.props.context | |
hexchat.hook_timer(1, function () | |
-- Emit this event right after the current one | |
if ctx:set() then | |
hexchat.emit_print('Capability Request', CAP_NAME) | |
end | |
end) | |
end | |
end) | |
-- Capability supported | |
hexchat.hook_print('Capability Acknowledgement', function (args) | |
local server_id = hexchat.props.context:get_info('host') | |
hexchat.emit_print('Capability Request', CAP_NAME) | |
if args[2]:find(CAP_NAME) and not servers[server_id] then | |
servers[server_id] = 0 -- New server | |
end | |
end) | |
function connect_hook(word, word_eol) | |
local server_id = hexchat.props.context:get_info('host') | |
local timestamp = servers[server_id] | |
if timestamp then | |
hexchat.command('quote PRIVMSG *playback :play * ' .. tostring(timestamp)) | |
end | |
end | |
-- On successful connection play history | |
hexchat.hook_server('376', connect_hook) | |
hexchat.hook_server('422', connect_hook) | |
-- Store the timestamp of the latest message on the server | |
hexchat.hook_server_attrs('PRIVMSG', function (word, word_eol, attrs) | |
local server_id = hexchat.props.context:get_info('host') | |
if servers[server_id] then | |
timestamp = GLib.get_real_time() / 1000000 -- epoch in seconds with milisecond precision UTC | |
servers[server_id] = timestamp | |
end | |
end, hexchat.PRI_LOWEST) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment