commit 7b18a7528ee0938b19f35bd40ea2a110c80eadc5
parent 02a05701a382e226b83d63bdad35b0a01209beec
Author: René Wagner <rwagner@rw-net.de>
Date: Sat, 28 Nov 2020 13:09:55 +0100
add recent feeds list
the 10 most recently visited feeds are stored and shown
on the recent page
Diffstat:
M | lib/orrg.pm | | | 45 | +++++++++++++++++++++++++++++++++++++++++++-- |
M | orrg.pl | | | 2 | ++ |
A | recent.pl | | | 44 | ++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 89 insertions(+), 2 deletions(-)
diff --git a/lib/orrg.pm b/lib/orrg.pm
@@ -7,13 +7,15 @@ package orrg;
use strict;
use Exporter;
our @ISA = qw(Exporter);
-our @EXPORT = qw(write_response %RC); # automatically exported subs
+our @EXPORT = qw(recent_get recent_add write_response %RC); # automatically exported subs
# enable UTF-8 mode for everything
use utf8;
binmode STDOUT, ':utf8';
binmode STDERR, ':utf8';
+my $recentfile = 'data/recent.txt';
+
# define return codes
our %RC = (
'INPUT', 10,
@@ -36,11 +38,50 @@ our %RC = (
'CERT_NOT_VALID', 62
);
+sub recent_get
+{
+ (-f $recentfile) or return undef;
+
+ my @recents = ();
+ open INFILE, $recentfile;
+ flock OUTFILE, 1;
+ while (<INFILE>) {
+ chomp($_);
+ push @recents, $_;
+ }
+ flock OUTFILE, 8;
+ close INFILE;
+
+ return \@recents;
+}
+
+sub recent_add
+{
+ my ( $uri, $name ) = @_;
+ my $recent = recent_get();
+
+ my $newline = "$uri $name";
+ open OUTFILE, '>', $recentfile;
+ flock OUTFILE, 1;
+ print OUTFILE "$newline\n";
+
+ my $c = 1;
+ foreach (@$recent) {
+ if ($newline ne $_) {
+ print OUTFILE "$_\n";
+ $c++;
+ }
+ ($c < 10) or last;
+ }
+ flock OUTFILE, 8;
+ close OUTFILE;
+}
+
sub write_response
{
my ($returncode, $meta, @content) = @_;
- if (!defined($RC{$returncode})) { die "Unknown response code!"; }
+ defined($RC{$returncode}) or die "Unknown response code!";
printf("%d %s\r\n", $RC{$returncode}, ($meta eq '') ? $returncode : $meta);
foreach (@content) {
diff --git a/orrg.pl b/orrg.pl
@@ -29,6 +29,7 @@ my $query = lc(uri_unescape($ENV{'QUERY_STRING'}));
if ($query eq '' || $query !~ /^https\:\/\//) {
write_response('INPUT', 'Paste the URI of the rss feed you want to read:', undef);
}
+
write_response('SUCCESS', 'text/gemini', create_response($query));
exit;
@@ -45,6 +46,7 @@ sub create_response
return @body;
}
+ recent_add($qs, $feed->title);
push @body, '# '. $feed->title;
push @body, 'fetched '. strftime('%Y-%m-%dT%H:%M:%SZ', gmtime());
$feed->description eq '' or push @body, ('', $feed->description);
diff --git a/recent.pl b/recent.pl
@@ -0,0 +1,44 @@
+#!/usr/bin/perl
+# Copyright René Wagner 2020
+# licenced under BSD 3-Clause licence
+# https://git.sr.ht/~rwa/orrg
+
+use strict;
+no warnings 'experimental';
+use URI::Escape;
+use lib 'lib/';
+use orrg;
+
+# enable UTF-8 mode for everything
+use utf8;
+binmode STDOUT, ':utf8';
+binmode STDERR, ':utf8';
+
+if (!defined($ENV{'SERVER_PROTOCOL'}) || $ENV{'SERVER_PROTOCOL'} ne 'GEMINI')
+{
+ write_response('CGI_ERROR', '', undef);
+}
+
+write_response('SUCCESS', 'text/gemini', create_response());
+
+exit;
+
+sub create_response
+{
+ my @body = ();
+
+ push @body, ('# recently visited feeds', '');
+
+ my $recents = recent_get();
+ if ( defined($recents) ) {
+ foreach (@$recents) {
+ my ($uri, $name) = split / /, $_, 2;
+ push @body, '=> orrg.pl?'. uri_escape($uri) .' '. $name;
+ }
+ } else {
+ push @body, 'No feeds found';
+ }
+
+ push @body, ('', '', '=> index.pl [home]');
+ return @body;
+}