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;
11 use PublicInbox::SearchMsg; # this loads w/o Search::Xapian
16 my $msgs = recent_msgs($ctx);
17 return _no_thread() unless @$msgs;
19 my $ibx = $ctx->{-inbox};
20 PublicInbox::WwwAtomStream->response($ctx, 200, sub {
21 while (my $smsg = shift @$msgs) {
22 $ibx->smsg_mime($smsg) and return $smsg;
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 $smsg = shift @$msgs) {
40 $ibx->smsg_mime($smsg) and return $smsg;
45 sub generate_html_index {
47 # if the 'r' query parameter is given, it is a legacy permalink
48 # which we must continue supporting:
50 if ($qp && !$qp->{r} && $ctx->{srch}) {
51 return PublicInbox::View::index_topics($ctx);
54 my $env = $ctx->{env};
55 my $url = $ctx->{-inbox}->base_url($env) . 'new.html';
56 my $qs = $env->{QUERY_STRING};
57 $url .= "?$qs" if $qs ne '';
58 [302, [ 'Location', $url, 'Content-Type', 'text/plain'],
59 [ "Redirecting to $url\n" ] ];
64 my $msgs = recent_msgs($ctx);
66 return [404, ['Content-Type', 'text/plain'],
67 ["No messages, yet\n"] ];
69 $ctx->{-html_tip} = '<pre>';
72 my $ibx = $ctx->{-inbox};
73 PublicInbox::WwwStream->response($ctx, 200, sub {
74 while (my $smsg = shift @$msgs) {
75 my $m = $ibx->smsg_mime($smsg) or next;
76 my $more = scalar @$msgs;
77 return PublicInbox::View::index_entry($m, $ctx, $more);
79 new_html_footer($ctx);
86 [404, ['Content-Type', 'text/plain'], ["No feed found for thread\n"]];
91 my $qp = delete $ctx->{qp} or return;
93 my $next = delete $ctx->{next_page} || '';
95 $next = qq!<a\nhref="?$next"\nrel=next>next</a>!;
98 $latest = qq! <a\nhref='./new.html'>latest</a>!;
101 "<hr><pre>page: $next$latest</pre>";
106 my $ibx = $ctx->{-inbox};
107 my $max = $ibx->{feedmax};
109 my $v = $ibx->{version} || 1;
111 die "BUG: unsupported inbox version: $v\n";
113 if (my $srch = $ibx->search) {
114 my $o = $qp ? $qp->{o} : 0;
117 my $res = $srch->query('', { limit => $max, offset => $o });
118 my $next = $o + $max;
119 $ctx->{next_page} = "o=$next" if $res->{total} >= $next;
123 my $hex = '[a-f0-9]';
124 my $addmsg = qr!^:000000 100644 \S+ (\S+) A\t${hex}{2}/${hex}{38}$!;
125 my $delmsg = qr!^:100644 000000 (\S+) \S+ D\t(${hex}{2}/${hex}{38})$!;
126 my $refhex = qr/(?:HEAD|${hex}{4,40})(?:~\d+)?/;
128 # revision ranges may be specified
130 my $r = $qp->{r} if $qp;
131 if ($r && ($r =~ /\A(?:$refhex\.\.)?$refhex\z/o)) {
135 # get recent messages
136 # we could use git log -z, but, we already know ssoma will not
137 # leave us with filenames with spaces in them..
138 my $log = $ibx->git->popen(qw/log
139 --no-notes --no-color --raw -r
140 --no-abbrev --abbrev-commit/,
141 "--format=%h", $range);
142 my %deleted; # only an optimization at this point
147 while (defined(my $line = <$log>)) {
148 if ($line =~ /$addmsg/o) {
150 next if $deleted{$add}; # optimization-only
152 if (scalar(@oids) >= $max) {
156 } elsif ($line =~ /$delmsg/o) {
163 while (my $line = <$log>) {
164 if ($line =~ /^(${hex}{7,40})/) {
171 $ctx->{next_page} = "r=$last_commit" if $last_commit;
172 [ map { bless {blob => $_ }, 'PublicInbox::SearchMsg' } @oids ];