diff options
-rw-r--r-- | lib/TgLib.pm | 6 | ||||
-rw-r--r-- | lib/TgLib/Cache.pm | 34 | ||||
-rw-r--r-- | lib/TgLib/Env.pm | 10 | ||||
-rwxr-xr-x | tgrecv | 31 | ||||
-rwxr-xr-x | tgsend | 9 |
5 files changed, 65 insertions, 25 deletions
diff --git a/lib/TgLib.pm b/lib/TgLib.pm new file mode 100644 index 0000000..9be77d7 --- /dev/null +++ b/lib/TgLib.pm @@ -0,0 +1,6 @@ +package TgLib; + +use parent qw<Exporter>; +our @EXPORT = qw<>; + +1; diff --git a/lib/TgLib/Cache.pm b/lib/TgLib/Cache.pm new file mode 100644 index 0000000..d3723b7 --- /dev/null +++ b/lib/TgLib/Cache.pm @@ -0,0 +1,34 @@ +package TgLib::Cache; + +use Storable qw<store retrieve>; +use File::Basename qw<dirname>; + +use TgLib::Env qw<$CACHE_HOME>; + +use parent qw<Exporter>; +our @EXPORT = qw<new>; + +my $CACHE_FILE = "$CACHE_HOME/tgutils/cache"; + +sub new { + my $class = shift; + + # Fetch cache + return bless((-f $CACHE_FILE + ? retrieve($CACHE_FILE) + : {'version' => $main::VERSION}), $class); +} + +sub offset { + my $cache = shift; + $cache->{'offset'} = shift if @_; + return $cache->{'offset'}; +} + +sub save { + my $cache = shift; + mkdir dirname $CACHE_FILE; + store($cache, $CACHE_FILE); +} + +1; diff --git a/lib/TgLib/Env.pm b/lib/TgLib/Env.pm new file mode 100644 index 0000000..b1e94aa --- /dev/null +++ b/lib/TgLib/Env.pm @@ -0,0 +1,10 @@ +package TgLib::Env; + +use parent qw<Exporter>; +our @EXPORT = qw<$HOME $CONFIG_HOME $CACHE_HOME>; + +our $HOME = $ENV{'HOME'}; +our $CONFIG_HOME = $ENV{'XDG_CONFIG_HOME'} || "$HOME/.config"; +our $CACHE_HOME = $ENV{'XDG_CACHE_HOME'} || "$HOME/.cache"; + +1; @@ -13,18 +13,15 @@ use Getopt::Long qw(:config auto_version); use Pod::Usage qw<pod2usage>; use LWP::UserAgent; use Data::Dumper; -use Storable qw<store retrieve>; -use File::Basename qw<dirname>; use HTTP::Request; use JSON qw<encode_json decode_json>; - -my $HOME = $ENV{'HOME'}; -my $CONFIG_HOME = $ENV{'XDG_CONFIG_HOME'} || "$HOME/.config"; -my $CACHE_HOME = $ENV{'XDG_CACHE_HOME'} || "$HOME/.cache"; - -my $cache; +# Local modules +use FindBin; +use lib "$FindBin::Bin/lib"; +use TgLib::Env qw<$HOME $CONFIG_HOME $CACHE_HOME>; +use TgLib::Cache; my $TOKEN; my $OUTPUT; @@ -60,16 +57,12 @@ unless ($TOKEN) { # Sanity check $TOKEN =~ /^[0-9]+:[a-zA-Z0-9]+$/ or die "Invalid bot token ($TOKEN)"; -# Fetch cache (TODO move to module) -my $CACHE_FILE = "$CACHE_HOME/tgutils/cache"; -$cache = -f $CACHE_FILE - ? retrieve($CACHE_FILE) - : {'version' => $main::VERSION}; +my $cache = TgLib::Cache->new; print STDERR "Using cache:\n", Dumper($cache), "\n" if $VERBOSE > 1; # Get offset from cache if --auto-offset is enabled (and no --offset provided) if ($AUTO_OFFSET && ! $OFFSET) { - $OFFSET |= $cache->{'offset'}; + $OFFSET |= $cache->offset; } my $ua = LWP::UserAgent->new; @@ -93,13 +86,11 @@ if ($resp->is_error()) { # Store new offset in cache if ($AUTO_OFFSET && @$results) { - printf STDERR "Setting new offset to %s (--auto-offset)\n", - $cache->{'offset'} if $VERBOSE > 1; # Update cache: set offset to last update id +1 - $cache->{'offset'} = $results->[-1]->{'update_id'}+1; - # Save cache (TODO move to module) - mkdir dirname $CACHE_FILE; - store($cache, $CACHE_FILE); + $cache->offset($results->[-1]{'update_id'}+1); + printf STDERR "Cached offset %s (--auto-offset)\n", + $cache->offset if $VERBOSE > 1; + $cache->save; } print $out encode_json($results); @@ -17,10 +17,10 @@ use Data::Dumper; use HTTP::Request; use JSON qw<encode_json>; - -my $HOME = $ENV{'HOME'}; -my $CONFIG_HOME = $ENV{'XDG_CONFIG_HOME'} || "$HOME/.config"; -my $CACHE_HOME = $ENV{'XDG_CACHE_HOME'} || "$HOME/.cache"; +# Local modules +use FindBin; +use lib "$FindBin::Bin/lib"; +use TgLib::Env qw<$HOME $CONFIG_HOME $CACHE_HOME>; my $TOKEN; my $PRETEND; @@ -38,7 +38,6 @@ $TOKEN ||= $ENV{'TGUTILS_TOKEN'}; # If still no token, try to get it from ~/.config/tgutils_token unless ($TOKEN) { - my $CONFIG_HOME = $ENV{'XDG_CONFIG_HOME'} || $ENV{'HOME'} . "/.config"; my $CONFIG = "$CONFIG_HOME/tgutils_token"; open(my $cfg, "<", $CONFIG) or pod2usage(-message => "ERROR: Unable to get bot token ($CONFIG: $!).\n", |