diff --git a/autosave.pl b/autosave.pl new file mode 100644 index 0000000..c9146ca --- /dev/null +++ b/autosave.pl @@ -0,0 +1,80 @@ +# +# autosave.pl +# Automatically performs a '/layout save' followed by a +# '/save' at a configurable interval +# +# +# Settings: +# /SET autosave_interval (0 to disable, 60 by default) +# +# Commands: +# /AUTOSAVE Execute save commands +# + +use strict; +use Irssi; + +use vars qw($VERSION %IRSSI); + +$VERSION = '1.0.0'; +%IRSSI = ( + authors => 'John R. Dennison', + contact => 'jrd@gerdesas.com', + name => 'AutoSave', + description => 'Save configuration on set intervals. Based on autoclearinput.pl by Trevor "tee" Slocum', + license => 'GPL V2+', + url => 'https://git.gerdesas.com/jrd/irssi-autosave', + changed => '2023-07-03' +); + +my ($autosave_timer, $server); + +# set up timer +sub autosave_init_timer { + if (Irssi::settings_get_int("autosave_interval") <= 0) { + Irssi::print ($IRSSI{name} . ": Timer unset."); + return; + } + $autosave_timer = Irssi::timeout_add_once(Irssi::settings_get_int("autosave_interval") * 1000 * 60, "autosave_execute", ""); + Irssi::print ($IRSSI{name} . ": Timer set for %9" . Irssi::settings_get_int("autosave_interval") . "%9 minutes."); +} + +# save layout and general configuaration +sub autosave_execute { + $server -> command ("LAYOUT SAVE"); + $server -> command ("SAVE"); + # remove the timer if it already exists + if (defined($autosave_timer)) { + Irssi::timeout_remove($autosave_timer); + } + if (Irssi::settings_get_int("autosave_interval") > 0) { + Irssi::print ($IRSSI{name} . ": Configuration saved. Resetting time for %9" . Irssi::settings_get_int("autosave_interval") . "%9 minutes."); + $autosave_timer = Irssi::timeout_add_once(Irssi::settings_get_int("autosave_interval") * 1000 * 60, "autosave_execute", ""); + } +} + +# config has changed; reset timer if it exists +sub autosave_reread_config { + Irssi::print ($IRSSI{name} . ": Configuration has changed - resetting timer"); + if (defined($autosave_timer)) { + Irssi::timeout_remove($autosave_timer); + } + autosave_init_timer +} + +Irssi::settings_add_int("autosave", "autosave_interval", 60); +Irssi::signal_add('setup changed' => 'autosave_reread_config'); +Irssi::command_bind("autosave", "autosave_execute"); + +print $IRSSI{name} . ': version %9' . $VERSION . '%9 ready. Save commands ' . + (Irssi::settings_get_int("autosave_interval") > 0 + ? ('will be executed every %9' . Irssi::settings_get_int("autosave_interval") . '%9 minutes.') + : 'are currently %9disabled%9.'); +print $IRSSI{name} . ': Configure this delay with: /SET autosave_interval [0 to disable]'; +print $IRSSI{name} . ': Manually save with /AUTOSAVE'; + +# snag server context for the current active server +$server = Irssi::servers(); + +# initialize timer and call it a day +autosave_init_timer