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;
12 MAX_PER_PAGE => 25, # this needs to be tunable
19 each_recent_blob($ctx, sub { push @paths, $_[0] });
20 return _no_thread() unless @paths;
22 my $ibx = $ctx->{-inbox};
23 PublicInbox::WwwAtomStream->response($ctx, 200, sub {
24 while (my $path = shift @paths) {
25 my $mime = do_cat_mail($ibx, $path) or next;
31 sub generate_thread_atom {
33 my $mid = $ctx->{mid};
34 my $res = $ctx->{srch}->get_thread($mid);
35 return _no_thread() unless $res->{total};
37 my $ibx = $ctx->{-inbox};
38 my $html_url = $ibx->base_url($ctx->{env});
39 $html_url .= PublicInbox::Hval->new_msgid($mid)->{href};
40 $ctx->{-html_url} = $html_url;
41 my $msgs = $res->{msgs};
42 PublicInbox::WwwAtomStream->response($ctx, 200, sub {
43 while (my $msg = shift @$msgs) {
44 $msg = $ibx->msg_by_smsg($msg) and
45 return Email::MIME->new($msg);
50 sub generate_html_index {
52 # if the 'r' query parameter is given, it is a legacy permalink
53 # which we must continue supporting:
55 if ($qp && !$qp->{r} && $ctx->{srch}) {
56 return PublicInbox::View::index_topics($ctx);
59 my $env = $ctx->{env};
60 my $url = $ctx->{-inbox}->base_url($env) . 'new.html';
61 my $qs = $env->{QUERY_STRING};
62 $url .= "?$qs" if $qs ne '';
63 [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
64 [ "Redirecting to $url\n" ] ];
70 my (undef, $last) = each_recent_blob($ctx, sub {
71 my ($path, $commit, $ts, $u, $subj) = @_;
72 $ctx->{first} ||= $commit;
76 return [404, ['Content-Type', 'text/plain'],
77 ["No messages, yet\n"] ];
79 $ctx->{-html_tip} = '<pre>';
82 PublicInbox::WwwStream->response($ctx, 200, sub {
83 while (my $path = shift @paths) {
84 my $m = do_cat_mail($ctx->{-inbox}, $path) or next;
85 my $more = scalar @paths;
86 my $s = PublicInbox::View::index_entry($m, $ctx, $more);
89 new_html_footer($ctx, $last);
96 [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
100 my ($ctx, $last) = @_;
101 my $qp = delete $ctx->{qp} or return;
102 my $old_r = $qp->{r};
107 $next = qq!<a\nhref="?r=$last"\nrel=next>next</a>!;
110 $latest = qq! <a\nhref='./new.html'>latest</a>!;
112 "<hr><pre>page: $next$latest</pre>";
115 sub each_recent_blob {
117 my $max = $ctx->{max} || MAX_PER_PAGE;
118 my $hex = '[a-f0-9]';
119 my $addmsg = qr!^:000000 100644 \S+ \S+ A\t(${hex}{2}/${hex}{38})$!;
120 my $delmsg = qr!^:100644 000000 \S+ \S+ D\t(${hex}{2}/${hex}{38})$!;
121 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
124 # revision ranges may be specified
126 my $r = $qp->{r} if $qp;
127 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
131 # get recent messages
132 # we could use git log -z, but, we already know ssoma will not
133 # leave us with filenames with spaces in them..
134 my $log = $ctx->{-inbox}->git->popen(qw/log
135 --no-notes --no-color --raw -r
136 --abbrev=16 --abbrev-commit/,
137 "--format=%h%x00%ct%x00%an%x00%s%x00",
139 my %deleted; # only an optimization at this point
142 my ($cur_commit, $first_commit, $last_commit);
145 while (defined(my $line = <$log>)) {
146 if ($line =~ /$addmsg/o) {
148 next if $deleted{$add}; # optimization-only
149 $cb->($add, $cur_commit, $ts, $u, $subj) and $nr++;
154 } elsif ($line =~ /$delmsg/o) {
156 } elsif ($line =~ /^${hex}{7,40}/o) {
157 ($cur_commit, $ts, $u, $subj) = split("\0", $line);
158 unless (defined $first_commit) {
159 $first_commit = $cur_commit;
166 while (my $line = <$log>) {
167 if ($line =~ /^(${hex}{7,40})/o) {
175 ($first_commit, $last_commit);
179 my ($ibx, $path) = @_;
180 my $mime = eval { $ibx->msg_by_path($path) } or return;
181 Email::MIME->new($mime);