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>
64 my $b = Blerg::Database->open_existing($_->{author});
65 my $data = $b->fetch($_->{record});
70 my $timestamp = $b->timestamp($_->{record});
73 author => $_->{author},
74 record => $_->{record},
76 timestamp => $timestamp,
84 my ($type, $name, @items) = @_;
87 if ($type eq 'user') {
88 $title = "${name}'s blërg";
89 $link = "${baseurl}#$name";
90 } elsif ($type eq 'feed') {
91 $title = "${name}'s stalking feed";
92 $link = "${baseurl}#/feed";
93 } elsif ($type eq 'tag' || $type eq 'ref') {
97 $link = "${baseurl}#/$type/$basename";
101 <?xml version="1.0" encoding="utf-8" ?>
104 <title>$title</title>
106 <description>Textual vomit</description>
110 my $author = defined $i->{author} ? $i->{author} : $name;
111 my $data = xml_escape(qq{<a href="${baseurl}#$author"><strong>\@$author</strong></a><br> $i->{data}});
112 my $post_time = strftime("%a, %d %b %Y %H:%M:%S %Z", localtime($i->{timestamp}));
115 <pubDate>$post_time</pubDate>
116 <guid>${baseurl}get/$author/$i->{record}</guid>
117 <link>${baseurl}#$author/$i->{record}</link>
118 <description>$data</description>
130 while (my $q = new CGI::Fast) {
132 my @path = split('/', $ENV{PATH_INFO});
135 if ($path[0] eq 'feed') {
136 my ($username, $password) = decode_basic_auth($q);
137 if (!defined $username) {
141 if (!Blerg::Database::auth_check_password($username, $password)) {
146 my $b = Blerg::Database->open_existing($username);
147 my @list = fetch_records($b->subscription_list());
150 print header(-type => 'application/rss+xml');
151 print_rss(feed => $username, @list);
152 } elsif (@path == 1) {
153 # Assume this is a username; redirect to /user/<username>
154 my $username = $path[0];
155 my $b = Blerg::Database->open_existing($username);
160 print header(-type => 'application/rss+xml',
162 -status => '301 Moved Permanently',
163 -location => "${baseurl}rss/user/$username");
164 # And present the content in case their client is broken
166 record => '?failed_redirect',
168 data => qq{Your RSS aggregator is dumb and isn't following 301 redirects. Please manually redirect it here: ${baseurl}rss/user/$username}
170 print_rss(user => $username, $i);
171 } elsif ($path[0] eq 'user') {
172 my $username = $path[1];
173 my $b = Blerg::Database->open_existing($username);
178 print header(-type => 'application/rss+xml');
179 my $n = $b->record_count - 1;
180 my @list = reverse map {
183 data => $b->fetch($_),
184 timestamp => $b->timestamp($_),
186 } ($n > 50 ? $n - 50 : 0)..$n;
188 print_rss(user => $username, @list);
189 } elsif ($path[0] eq 'tag' || $path[0] eq 'ref') {
192 if ($path[0] eq 'tag') {
198 my @list = fetch_records(Blerg::Database::tag_list($atag, 0, -1));
200 print header(-type => 'application/rss+xml');
201 print_rss($path[0] => $atag, @list);