1 # Copyright (C) 2013-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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;
15 my $oids = recent_blobs($ctx);
16 return _no_thread() unless @$oids;
18 my $git = $ctx->{-inbox}->git;
19 PublicInbox::WwwAtomStream->response($ctx, 200, sub {
20 while (my $oid = shift @$oids) {
21 my $msg = $git->cat_file($oid) or next;
22 return PublicInbox::MIME->new($msg);
27 sub generate_thread_atom {
29 my $mid = $ctx->{mid};
30 my $res = $ctx->{srch}->get_thread($mid);
31 return _no_thread() unless $res->{total};
33 my $ibx = $ctx->{-inbox};
34 my $html_url = $ibx->base_url($ctx->{env});
35 $html_url .= PublicInbox::Hval->new_msgid($mid)->{href};
36 $ctx->{-html_url} = $html_url;
37 my $msgs = $res->{msgs};
38 PublicInbox::WwwAtomStream->response($ctx, 200, sub {
39 while (my $msg = shift @$msgs) {
40 $msg = $ibx->msg_by_smsg($msg) and
41 return PublicInbox::MIME->new($msg);
46 sub generate_html_index {
48 # if the 'r' query parameter is given, it is a legacy permalink
49 # which we must continue supporting:
51 if ($qp && !$qp->{r} && $ctx->{srch}) {
52 return PublicInbox::View::index_topics($ctx);
55 my $env = $ctx->{env};
56 my $url = $ctx->{-inbox}->base_url($env) . 'new.html';
57 my $qs = $env->{QUERY_STRING};
58 $url .= "?$qs" if $qs ne '';
59 [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
60 [ "Redirecting to $url\n" ] ];
65 my $oids = recent_blobs($ctx);
67 return [404, ['Content-Type', 'text/plain'],
68 ["No messages, yet\n"] ];
70 $ctx->{-html_tip} = '<pre>';
73 my $git = $ctx->{-inbox}->git;
74 PublicInbox::WwwStream->response($ctx, 200, sub {
75 while (my $oid = shift @$oids) {
76 my $msg = $git->cat_file($oid) or next;
77 my $m = PublicInbox::MIME->new($msg);
78 my $more = scalar @$oids;
79 return PublicInbox::View::index_entry($m, $ctx, $more);
81 new_html_footer($ctx);
88 [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
93 my $qp = delete $ctx->{qp} or return;
95 my $next = delete $ctx->{next_page} || '';
97 $next = qq!<a\nhref="?$next"\nrel=next>next</a>!;
100 $latest = qq! <a\nhref='./new.html'>latest</a>!;
103 "<hr><pre>page: $next$latest</pre>";
108 my $ibx = $ctx->{-inbox};
109 my $max = $ibx->{feedmax};
111 my $v = $ibx->{version} || 1;
113 die "BUG: unsupported inbox version: $v\n";
115 if (my $srch = $ibx->search) {
116 my $o = $qp ? $qp->{o} : 0;
119 my $res = $srch->query('', { limit => $max, offset => $o });
120 my $next = $o + $max;
121 $ctx->{next_page} = "o=$next" if $res->{total} >= $next;
122 return [ map { $_->{blob} } @{$res->{msgs}} ];
125 my $hex = '[a-f0-9]';
126 my $addmsg = qr!^:000000 100644 \S+ (\S+) A\t${hex}{2}/${hex}{38}$!;
127 my $delmsg = qr!^:100644 000000 (\S+) \S+ D\t(${hex}{2}/${hex}{38})$!;
128 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
130 # revision ranges may be specified
132 my $r = $qp->{r} if $qp;
133 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
137 # get recent messages
138 # we could use git log -z, but, we already know ssoma will not
139 # leave us with filenames with spaces in them..
140 my $log = $ibx->git->popen(qw/log
141 --no-notes --no-color --raw -r
142 --no-abbrev --abbrev-commit/,
143 "--format=%h", $range);
144 my %deleted; # only an optimization at this point
149 while (defined(my $line = <$log>)) {
150 if ($line =~ /$addmsg/o) {
152 next if $deleted{$add}; # optimization-only
154 if (scalar(@oids) >= $max) {
158 } elsif ($line =~ /$delmsg/o) {
165 while (my $line = <$log>) {
166 if ($line =~ /^(${hex}{7,40})/) {
173 $ctx->{next_page} = "r=$last_commit" if $last_commit;