2 use CGI::Fast qw/:cgi/;
5 use POSIX qw/strftime/;
9 my $baseurl = Blerg::Database::constant('BASEURL');
19 sub decode_basic_auth {
23 my ($method, $base64) = split(/\s+/, $q->http('Authorization'));
24 if (!defined $method) {
26 } elsif ($method ne 'Basic') {
30 my ($username, $password) = split(':', decode_base64($base64), 2);
31 if (!defined $username) {
35 return ($username, $password);
39 print header(-type => 'text/html',
40 -status => '401 Unauthorized',
41 -WWW_Authenticate => 'Basic realm="blerg"');
44 <h1>401 Unauthorized</h1>
50 print header(-type => 'text/html',
51 -status => '404 Not Found');
54 <h1>404 Not Found</h1>
60 my ($type, $name, @items) = @_;
63 if ($type eq 'user') {
64 $title = "${name}'s blërg";
65 $link = "${baseurl}#$name";
66 } elsif ($type eq 'feed') {
67 $title = "${name}'s stalking feed";
68 $link = "${baseurl}#/feed";
69 } elsif ($type eq 'tag' || $type eq 'ref') {
73 $link = "${baseurl}#/$type/$basename";
77 <?xml version="1.0" encoding="utf-8" ?>
82 <description>Textual vomit</description>
86 my $author = defined $i->{author} ? $i->{author} : $name;
87 my $data = xml_escape(qq{<a href="${baseurl}#$author"><strong>\@$author</strong></a><br> $i->{data}});
88 my $post_time = strftime("%a, %d %b %Y %H:%M:%S %Z", localtime($i->{timestamp}));
91 <pubDate>$post_time</pubDate>
92 <guid>${baseurl}get/$author/$i->{record}</guid>
93 <link>${baseurl}#$author/$i->{record}</link>
94 <description>$data</description>
106 while (my $q = new CGI::Fast) {
108 my @path = split('/', $ENV{PATH_INFO});
111 if ($path[0] eq 'feed') {
112 my ($username, $password) = decode_basic_auth($q);
113 if (!defined $username) {
117 if (!Blerg::Database::auth_check_password($username, $password)) {
122 my $bb = Blerg::Database->open_existing($username);
123 my @list = $bb->subscription_list();
126 my $b = Blerg::Database->open_existing($_->{author});
127 my $data = $b->fetch($_->{record});
128 my $timestamp = $b->timestamp($_->{record});
131 author => $_->{author},
132 record => $_->{record},
134 timestamp => $timestamp,
138 print header(-type => 'application/rss+xml');
139 print_rss(feed => $username, @list);
140 } elsif (@path == 1) {
141 # Assume this is a username; redirect to /user/<username>
142 my $username = $path[0];
143 my $b = Blerg::Database->open_existing($username);
148 print header(-type => 'application/rss+xml',
150 -status => '301 Moved Permanently',
151 -location => "${baseurl}rss/user/$username");
152 # And present the content in case their client is broken
154 record => '?failed_redirect',
156 data => qq{Your RSS aggregator is dumb and isn't following 301 redirects. Please manually redirect it here: ${baseurl}rss/user/$username}
158 print_rss(user => $username, $i);
159 } elsif ($path[0] eq 'user') {
160 my $username = $path[1];
161 my $b = Blerg::Database->open_existing($username);
166 print header(-type => 'application/rss+xml');
167 my $n = $b->record_count - 1;
168 my @list = reverse map {
171 data => $b->fetch($_),
172 timestamp => $b->timestamp($_),
174 } ($n > 50 ? $n - 50 : 0)..$n;
176 print_rss(user => $username, @list);
177 } elsif ($path[0] eq 'tag' || $path[0] eq 'ref') {
180 if ($path[0] eq 'tag') {
186 my @list = Blerg::Database::tag_list($atag, 0, -1);
188 my $b = Blerg::Database->open_existing($_->{author});
189 my $data = $b->fetch($_->{record});
190 my $timestamp = $b->timestamp($_->{record});
193 author => $_->{author},
194 record => $_->{record},
196 timestamp => $timestamp,
200 print header(-type => 'application/rss+xml');
201 print_rss($path[0] => $atag, @list);