diff --git a/lib/orrg.pm b/lib/orrg.pm
index e4ca572f0b36c8b0eb3725c8f1f7f11dc0e3de4a..88575dd44ddce8718626f6c0c78be335616d11e7 100755
--- a/lib/orrg.pm
+++ b/lib/orrg.pm
@@ -7,12 +7,14 @@ 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 = (
@@ -36,11 +38,50 @@ 'CERT_NOT_AUTHORISED', 61,
'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
index 5cf31e2c5c5aaa1270cbeac8e19af7f7a44c0544..e9e258649242d929350b77364a977eded0563e1b 100755
--- a/orrg.pl
+++ b/orrg.pl
@@ -29,6 +29,7 @@
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 @@ push @body, ('# orrg error', '', 'The requested feed could not be loaded. :(', '', '=> '. $qs .' open feed in browser');
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
new file mode 100755
index 0000000000000000000000000000000000000000..bfc2c14271284eadf6dd7334b1982a69aceece11
--- /dev/null
+++ 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;
+}