2 use CGI::Fast qw/:cgi/;
5 use POSIX qw/strftime/;
9 my $baseurl = Blerg::Database::constant('BASEURL');
19 sub decode_basic_auth {
22 my ($method, $base64) = split(/\s+/, $q->http('Authorization'));
23 if (!defined $method) {
25 } elsif ($method ne 'Basic') {
29 my ($username, $password) = split(':', decode_base64($base64), 2);
30 if (!defined $username) {
34 return ($username, $password);
38 print header(-type => 'text/html',
39 -status => '401 Unauthorized',
40 -WWW_Authenticate => 'Basic realm="blerg"');
43 <h1>401 Unauthorized</h1>
49 print header(-type => 'text/html',
50 -status => '404 Not Found');
53 <h1>404 Not Found</h1>
63 my $b = Blerg::Database->open_existing($_->{author});
64 my $data = $b->fetch($_->{record});
69 my $timestamp = $b->timestamp($_->{record});
72 author => $_->{author},
73 record => $_->{record},
75 timestamp => $timestamp,
83 my ($type, $name, @items) = @_;
86 if ($type eq 'user') {
87 $title = "${name}'s blërg";
88 $link = "${baseurl}#$name";
89 } elsif ($type eq 'feed') {
90 $title = "${name}'s stalking feed";
91 $link = "${baseurl}#/feed";
92 } elsif ($type eq 'tag' || $type eq 'ref') {
96 $link = "${baseurl}#/$type/$basename";
100 <?xml version="1.0" encoding="utf-8" ?>
103 <title>$title</title>
105 <description>Textual vomit</description>
109 my $author = defined $i->{author} ? $i->{author} : $name;
110 my $data = xml_escape(qq{<a href="${baseurl}#$author"><strong>\@$author</strong></a><br> $i->{data}});
111 my $post_time = strftime("%a, %d %b %Y %H:%M:%S %Z", localtime($i->{timestamp}));
114 <pubDate>$post_time</pubDate>
115 <guid>${baseurl}get/$author/$i->{record}</guid>
116 <link>${baseurl}#$author/$i->{record}</link>
117 <description>$data</description>
129 while (my $q = new CGI::Fast) {
131 my @path = split('/', $ENV{PATH_INFO});
134 if ($path[0] eq 'feed') {
135 my ($username, $password) = decode_basic_auth($q);
136 if (!defined $username) {
140 if (!Blerg::Database::auth_check_password($username, $password)) {
145 my $b = Blerg::Database->open_existing($username);
146 my @list = fetch_records($b->subscription_list());
149 print header(-type => 'application/rss+xml');
150 print_rss(feed => $username, @list);
151 } elsif (@path == 1) {
152 # Assume this is a username; redirect to /user/<username>
153 my $username = $path[0];
154 my $b = Blerg::Database->open_existing($username);
159 print header(-type => 'application/rss+xml',
161 -status => '301 Moved Permanently',
162 -location => "${baseurl}rss/user/$username");
163 # And present the content in case their client is broken
165 record => '?failed_redirect',
167 data => qq{Your RSS aggregator is dumb and isn't following 301 redirects. Please manually redirect it here: ${baseurl}rss/user/$username}
169 print_rss(user => $username, $i);
170 } elsif ($path[0] eq 'user') {
171 my $username = $path[1];
172 my $b = Blerg::Database->open_existing($username);
177 print header(-type => 'application/rss+xml');
178 my $n = $b->record_count - 1;
179 my @list = reverse map {
182 data => $b->fetch($_),
183 timestamp => $b->timestamp($_),
185 } ($n > 50 ? $n - 50 : 0)..$n;
187 print_rss(user => $username, @list);
188 } elsif ($path[0] eq 'tag' || $path[0] eq 'ref') {
191 if ($path[0] eq 'tag') {
197 my @list = fetch_records(Blerg::Database::tag_list($atag, 0, -1));
199 print header(-type => 'application/rss+xml');
200 print_rss($path[0] => $atag, @list);