1 # Copyright (C) 2013-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # Used for generating Atom feeds for web-accessible mailing list archives.
5 package PublicInbox::Feed;
10 use PublicInbox::WwwAtomStream;
16 each_recent_blob($ctx, sub { push @paths, $_[0] });
17 return _no_thread() unless @paths;
19 my $ibx = $ctx->{-inbox};
20 PublicInbox::WwwAtomStream->response($ctx, 200, sub {
21 while (my $path = shift @paths) {
22 my $mime = do_cat_mail($ibx, $path) or next;
28 sub generate_thread_atom {
30 my $mid = $ctx->{mid};
31 my $res = $ctx->{srch}->get_thread($mid);
32 return _no_thread() unless $res->{total};
34 my $ibx = $ctx->{-inbox};
35 my $html_url = $ibx->base_url($ctx->{env});
36 $html_url .= PublicInbox::Hval->new_msgid($mid)->{href};
37 $ctx->{-html_url} = $html_url;
38 my $msgs = $res->{msgs};
39 PublicInbox::WwwAtomStream->response($ctx, 200, sub {
40 while (my $msg = shift @$msgs) {
41 $msg = $ibx->msg_by_smsg($msg) and
42 return PublicInbox::MIME->new($msg);
47 sub generate_html_index {
49 # if the 'r' query parameter is given, it is a legacy permalink
50 # which we must continue supporting:
52 if ($qp && !$qp->{r} && $ctx->{srch}) {
53 return PublicInbox::View::index_topics($ctx);
56 my $env = $ctx->{env};
57 my $url = $ctx->{-inbox}->base_url($env) . 'new.html';
58 my $qs = $env->{QUERY_STRING};
59 $url .= "?$qs" if $qs ne '';
60 [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
61 [ "Redirecting to $url\n" ] ];
67 my (undef, $last) = each_recent_blob($ctx, sub {
68 my ($path, $commit, $ts, $u, $subj) = @_;
69 $ctx->{first} ||= $commit;
73 return [404, ['Content-Type', 'text/plain'],
74 ["No messages, yet\n"] ];
76 $ctx->{-html_tip} = '<pre>';
79 PublicInbox::WwwStream->response($ctx, 200, sub {
80 while (my $path = shift @paths) {
81 my $m = do_cat_mail($ctx->{-inbox}, $path) or next;
82 my $more = scalar @paths;
83 my $s = PublicInbox::View::index_entry($m, $ctx, $more);
86 new_html_footer($ctx, $last);
93 [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
97 my ($ctx, $last) = @_;
98 my $qp = delete $ctx->{qp} or return;
104 $next = qq!<a\nhref="?r=$last"\nrel=next>next</a>!;
107 $latest = qq! <a\nhref='./new.html'>latest</a>!;
109 "<hr><pre>page: $next$latest</pre>";
112 sub each_recent_blob {
114 my $max = $ctx->{-inbox}->{feedmax};
115 my $hex = '[a-f0-9]';
116 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
117 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
118 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
121 # revision ranges may be specified
123 my $r = $qp->{r} if $qp;
124 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
128 # get recent messages
129 # we could use git log -z, but, we already know ssoma will not
130 # leave us with filenames with spaces in them..
131 my $log = $ctx->{-inbox}->git->popen(qw/log
132 --no-notes --no-color --raw -r
133 --abbrev=16 --abbrev-commit/,
134 "--format=%h%x00%ct%x00%an%x00%s%x00",
136 my %deleted; # only an optimization at this point
139 my ($cur_commit, $first_commit, $last_commit);
142 while (defined(my $line = <$log>)) {
143 if ($line =~ /$addmsg/o) {
145 next if $deleted{$add}; # optimization-only
146 $cb->($add, $cur_commit, $ts, $u, $subj) and $nr++;
151 } elsif ($line =~ /$delmsg/o) {
153 } elsif ($line =~ /^${hex}{7,40}/o) {
154 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
155 unless (defined $first_commit) {
156 $first_commit = $cur_commit;
163 while (my $line = <$log>) {
164 if ($line =~ /^(${hex}{7,40})/o) {
172 ($first_commit, $last_commit);
176 my ($ibx, $path) = @_;
177 my $mime = eval { $ibx->msg_by_path($path) } or return;
178 PublicInbox::MIME->new($mime);