2 use CGI::Fast qw/:cgi/;
5 use POSIX qw/strftime/;
9 my $baseurl = Blerg::Database::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 $title = xml_escape(substr($i->{data}, 0, 27)) . "...";
112 my $post_time = strftime("%a, %d %b %Y %H:%M:%S %Z", localtime($i->{timestamp}));
115 <title>$title</title>
116 <pubDate>$post_time</pubDate>
117 <guid>${baseurl}get/$author/$i->{record}</guid>
118 <link>${baseurl}#$author/$i->{record}</link>
119 <description>$data</description>
131 while (my $q = new CGI::Fast) {
133 my @path = split('/', $ENV{PATH_INFO});
136 if ($path[0] eq 'feed') {
137 my ($username, $password) = decode_basic_auth($q);
138 if (!defined $username) {
142 if (!Blerg::Database::auth_check_password($username, $password)) {
147 my $b = Blerg::Database->open_existing($username);
148 my @list = fetch_records($b->subscription_list());
151 print header(-type => 'application/rss+xml');
152 print_rss(feed => $username, @list);
153 } elsif (@path == 1) {
154 # Assume this is a username; redirect to /user/<username>
155 my $username = $path[0];
156 my $b = Blerg::Database->open_existing($username);
161 print header(-type => 'application/rss+xml',
163 -status => '301 Moved Permanently',
164 -location => "${baseurl}rss/user/$username");
165 # And present the content in case their client is broken
167 record => '?failed_redirect',
169 data => qq{Your RSS aggregator is dumb and isn't following 301 redirects. Please manually redirect it here: ${baseurl}rss/user/$username}
171 print_rss(user => $username, $i);
172 } elsif ($path[0] eq 'user') {
173 my $username = $path[1];
174 my $b = Blerg::Database->open_existing($username);
179 print header(-type => 'application/rss+xml');
180 my $n = $b->record_count - 1;
181 my @list = reverse map {
184 data => $b->fetch($_),
185 timestamp => $b->timestamp($_),
187 } ($n > 50 ? $n - 50 : 0)..$n;
189 print_rss(user => $username, @list);
190 } elsif ($path[0] eq 'tag' || $path[0] eq 'ref') {
193 if ($path[0] eq 'tag') {
199 my @list = fetch_records(Blerg::Database::tag_list($atag, 0, -1));
201 print header(-type => 'application/rss+xml');
202 print_rss($path[0] => $atag, @list);