Update MANIFEST for perl lib
[blerg.git] / rss.cgi
diff --git a/rss.cgi b/rss.cgi
index ca2a779..d7abe3d 100755 (executable)
--- a/rss.cgi
+++ b/rss.cgi
@@ -3,6 +3,7 @@ use CGI::Fast qw/:cgi/;
 use Blerg::Database;
 use URI::Escape;
 use POSIX qw/strftime/;
+use MIME::Base64;
 use strict;
 
 my $baseurl = Blerg::Database::constant('BASEURL');
@@ -15,6 +16,36 @@ sub xml_escape {
     return $_;
 }
 
+sub decode_basic_auth {
+    my ($q) = @_;
+
+    my $authorization = 
+    my ($method, $base64) = split(/\s+/, $q->http('Authorization'));
+    if (!defined $method) {
+        return;
+    } elsif ($method ne 'Basic') {
+        return;
+    }
+
+    my ($username, $password) = split(':', decode_base64($base64), 2);
+    if (!defined $username) {
+        return;
+    }
+
+    return ($username, $password);
+}
+
+sub print_401 {
+    print header(-type => 'text/html',
+                 -status => '401 Unauthorized',
+                 -WWW_Authenticate => 'Basic realm="blerg"');
+    print <<DOC;
+<!DOCTYPE html>
+<h1>401 Unauthorized</h1>
+Please log in.
+DOC
+}
+
 sub print_404 {
     print header(-type => 'text/html',
                  -status => '404 Not Found');
@@ -32,6 +63,9 @@ sub print_rss {
     if ($type eq 'user') {
         $title = "${name}'s blĂ«rg";
         $link = "${baseurl}#$name";
+    } elsif ($type eq 'feed') {
+        $title = "${name}'s stalking feed";
+        $link = "${baseurl}#/feed";
     } elsif ($type eq 'tag' || $type eq 'ref') {
         $title = $name;
         my $basename = $name;
@@ -49,13 +83,14 @@ sub print_rss {
 HEADER
 
     for my $i (@items) {
-        my $data = xml_escape($i->{data});
-        my $post_time = strftime("%a, %d %b %Y %H:%M:%S %Z", localtime($i->{timestamp}));
         my $author = defined $i->{author} ? $i->{author} : $name;
+        my $data = xml_escape(qq{<a href="${baseurl}#$author"><strong>\@$author</strong></a><br> $i->{data}});
+        my $post_time = strftime("%a, %d %b %Y %H:%M:%S %Z", localtime($i->{timestamp}));
         print <<ITEM;
     <item>
       <pubDate>$post_time</pubDate>
       <guid>${baseurl}get/$author/$i->{record}</guid>
+      <link>${baseurl}#$author/$i->{record}</link>
       <description>$data</description>
     </item>
 ITEM
@@ -69,10 +104,40 @@ FOOTER
 
 REQUEST:
 while (my $q = new CGI::Fast) {
+    $q->charset('utf8');
     my @path = split('/', $ENV{PATH_INFO});
     shift @path;
 
-    if (@path == 1) {
+    if ($path[0] eq 'feed') {
+        my ($username, $password) = decode_basic_auth($q);
+        if (!defined $username) {
+            print_401;
+            next REQUEST;
+        }
+        if (!Blerg::Database::auth_check_password($username, $password)) {
+            print_401;
+            next REQUEST;
+        }
+
+        my $bb = Blerg::Database->open_existing($username);
+        my @list = $bb->subscription_list();
+        $bb->close;
+        @list = map {
+            my $b = Blerg::Database->open_existing($_->{author});
+            my $data = $b->fetch($_->{record});
+            my $timestamp = $b->timestamp($_->{record});
+            $b->close;
+            {
+                author => $_->{author},
+                record => $_->{record},
+                data => $data,
+                timestamp => $timestamp,
+            };
+        } @list;
+
+        print header(-type => 'application/rss+xml');
+        print_rss(feed => $username, @list);
+    } elsif (@path == 1) {
         # Assume this is a username; redirect to /user/<username>
         my $username = $path[0];
         my $b = Blerg::Database->open_existing($username);
@@ -109,10 +174,16 @@ while (my $q = new CGI::Fast) {
         } ($n > 50 ? $n - 50 : 0)..$n;
         $b->close;
         print_rss(user => $username, @list);
-    } elsif ($path[0] eq 'tag') {
+    } elsif ($path[0] eq 'tag' || $path[0] eq 'ref') {
         my $tag = $path[1];
-        $tag =~ s/^H/#/;
-        my @list = Blerg::Database::tag_list($tag, 0, -1);
+        my $atag;
+        if ($path[0] eq 'tag') {
+            $atag = '#' . $tag;
+        } else {
+            $atag = '@' . $tag;
+        }
+
+        my @list = Blerg::Database::tag_list($atag, 0, -1);
         @list = map {
             my $b = Blerg::Database->open_existing($_->{author});
             my $data = $b->fetch($_->{record});
@@ -125,13 +196,8 @@ while (my $q = new CGI::Fast) {
                 timestamp => $timestamp,
             };
         } @list;
+
         print header(-type => 'application/rss+xml');
-        if (substr($tag, 0, 1) eq '#') {
-            print_rss(tag => $tag, @list);
-        } else {
-            print_rss(ref => $tag, @list);
-        }
-    } elsif ($path[0] eq 'ref') {
-    } elsif ($path[0] eq 'feed') {
+        print_rss($path[0] => $atag, @list);
     }
 }