1 # Copyright (C) 2014-2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
4 # Used for displaying the HTML web interface.
5 # See Documentation/design_www.txt for this.
6 package PublicInbox::View;
9 use Date::Parse qw/str2time/;
10 use PublicInbox::Hval qw/ascii_html/;
11 use PublicInbox::Linkify;
12 use PublicInbox::MID qw/mid_clean id_compress mid_mime mid_escape/;
13 use PublicInbox::MsgIter;
14 use PublicInbox::Address;
15 use PublicInbox::WwwStream;
16 use PublicInbox::Reply;
19 use constant INDENT => ' ';
20 use constant TCHILD => '` ';
21 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
23 # public functions: (unstable)
25 my ($ctx, $mime) = @_;
26 my $hdr = $mime->header_obj;
27 my $tip = _msg_html_prepare($hdr, $ctx);
28 PublicInbox::WwwStream->response($ctx, 200, sub {
31 $tip . multipart_text_as_html($mime, '') . '</pre><hr>'
33 # fake an EOF if generating the footer fails;
34 # we want to at least show the message if something
37 '<pre>' . html_footer($hdr, 1, $ctx) .
38 '</pre>' . msg_reply($ctx, $hdr)
46 # /$INBOX/$MESSAGE_ID/#R
50 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
52 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
55 if (my $url = $ctx->{-inbox}->{infourl}) {
56 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
57 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
60 my ($arg, $link) = PublicInbox::Reply::mailto_arg_link($hdr);
61 push @$arg, '/path/to/YOUR_REPLY';
62 $arg = ascii_html(join(" \\\n ", '', @$arg));
65 id=R><b>Reply instructions:</b>
67 You may reply publically to <a
68 href=#t>this message</a> via plain-text email
69 using any one of the following methods:
71 * Save the following mbox file, import it into your mail client,
72 and reply-to-all from there: <a
75 Avoid top-posting and favor interleaved quoting:
77 href="$p_url">$p_url</a>
79 * Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,
80 and <b>--in-reply-to</b> switches of git-send-email(1):
85 href="$se_url">$se_url</a>
87 * If your mail client supports setting the <b>In-Reply-To</b> header
88 via mailto: links, try the <a
89 href="$link">mailto: link</a></pre>
95 my %mid = map { $_ => 1 } $hdr->header_raw('Message-ID');
96 my @refs = ($hdr->header_raw('References'),
97 $hdr->header_raw('In-Reply-To'));
98 @refs = ((join(' ', @refs)) =~ /<([^>]+)>/g);
99 while (defined(my $irt = pop @refs)) {
100 next if $mid{"<$irt>"};
106 sub _hdr_names ($$) {
107 my ($hdr, $field) = @_;
108 my $val = $hdr->header($field) or return '';
109 ascii_html(join(', ', PublicInbox::Address::names($val)));
113 my ($nr, $singular, $plural) = @_;
114 return "0 $plural" if $nr == 0;
115 $nr == 1 ? "$nr $singular" : "$nr $plural";
118 # this is already inside a <pre>
120 my ($mime, $ctx, $more) = @_;
121 my $srch = $ctx->{srch};
122 my $hdr = $mime->header_obj;
123 my $subj = $hdr->header('Subject');
125 my $mid_raw = mid_clean(mid_mime($mime));
126 my $id = id_compress($mid_raw, 1);
129 my $root_anchor = $ctx->{root_anchor} || '';
132 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
133 $subj = '<b>'.ascii_html($subj).'</b>';
134 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
136 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
138 foreach my $f (qw(To Cc)) {
139 my $dst = _hdr_names($hdr, $f);
140 push @tocc, "$f: $dst" if $dst ne '';
142 $rv .= "From: "._hdr_names($hdr, 'From').' @ '._msg_date($hdr)." UTC";
143 my $upfx = $ctx->{-upfx};
144 my $mhref = $upfx . mid_escape($mid_raw) . '/';
145 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
146 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
147 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
149 my $mapping = $ctx->{mapping};
150 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
151 my $mirt = PublicInbox::Hval->new_msgid($irt);
152 my $href = $upfx . $mirt->{href}. '/';
153 my $html = $mirt->as_html;
154 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
158 # scan through all parts, looking for displayable text
159 msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[0]) });
162 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
163 "<a\nhref=\"$mhref\">permalink</a>" .
164 " <a\nhref=\"${mhref}raw\">raw</a>" .
165 " <a\nhref=\"${mhref}#R\">reply</a>";
168 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
169 $rv .= "\t[relevance $pct->{$mid_raw}%]";
172 my $threaded = 'threaded';
177 $flat = "<b>$flat</b>";
179 $threaded = "<b>$threaded</b>";
181 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
182 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
183 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
188 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
192 sub pad_link ($$;$) {
193 my ($mid, $level, $s) = @_;
195 my $id = id_compress($mid, 1);
196 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
200 my ($mid_raw, $irt, $id, $ctx) = @_;
202 my $mapping = $ctx->{mapping} or return $rv;
204 my $mid_map = $mapping->{$mid_raw};
206 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
207 my ($attr, $node, $idx, $level) = @$mid_map;
208 my $children = $node->{children};
209 my $nr_c = scalar @$children;
212 if (my $smsg = $node->{smsg}) {
213 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
215 my $irt_map = $mapping->{$$irt} if defined $$irt;
216 if (defined $irt_map) {
217 $siblings = $irt_map->[1]->{children};
218 $nr_s = scalar(@$siblings) - 1;
219 $rv .= $pad . $irt_map->[0];
221 my $prev = $siblings->[$idx - 1];
222 my $pmid = $prev->{id};
224 my $s = ($idx - 1). ' preceding siblings ...';
225 $rv .= pad_link($pmid, $level, $s);
226 } elsif ($idx == 2) {
227 my $ppmid = $siblings->[0]->{id};
228 $rv .= $pad . $mapping->{$ppmid}->[0];
230 $rv .= $pad . $mapping->{$pmid}->[0];
233 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
234 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
235 $attr =~ s!\n\z!</b>\n!s;
236 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
237 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
240 my $cmid = $children->[0]->{id};
241 $rv .= $pad . $mapping->{$cmid}->[0];
243 my $s = ($nr_c - 1). ' more replies';
244 $rv .= pad_link($cmid, $level + 1, $s);
245 } elsif (my $cn = $children->[1]) {
246 $rv .= $pad . $mapping->{$cn->{id}}->[0];
250 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
252 my $nmid = $next->{id};
253 $rv .= $pad . $mapping->{$nmid}->[0];
254 my $nnext = $nr_s - $idx;
256 my $s = ($nnext - 1).' subsequent siblings';
257 $rv .= pad_link($nmid, $level, $s);
258 } elsif (my $nn = $siblings->[$idx + 2]) {
259 $rv .= $pad . $mapping->{$nn->{id}}->[0];
262 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
266 my ($rootset, $ctx, $cb) = @_;
267 my @q = map { (0, $_, -1) } @$rootset;
269 my ($level, $node, $i) = splice(@q, 0, 3);
270 defined $node or next;
271 $cb->($ctx, $level, $node, $i);
274 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
279 my ($ctx, $level, $node, $idx) = @_;
280 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
281 skel_dump($ctx, $level, $node);
284 sub thread_index_entry {
285 my ($ctx, $level, $mime) = @_;
286 my ($beg, $end) = thread_adj_level($ctx, $level);
287 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
290 sub stream_thread ($$) {
291 my ($rootset, $ctx) = @_;
292 my $inbox = $ctx->{-inbox};
294 my @q = map { (0, $_) } @$rootset;
298 my $node = shift @q or next;
300 unshift @q, map { ($cl, $_) } @{$node->{children}};
301 $mime = $inbox->msg_by_smsg($node->{smsg}) and last;
303 return missing_thread($ctx) unless $mime;
305 $mime = PublicInbox::MIME->new($mime);
306 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
307 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
308 PublicInbox::WwwStream->response($ctx, 200, sub {
312 my $node = shift @q or next;
314 unshift @q, map { ($cl, $_) } @{$node->{children}};
315 my $mid = $node->{id};
316 if ($mime = $inbox->msg_by_smsg($node->{smsg})) {
317 $mime = PublicInbox::MIME->new($mime);
318 return thread_index_entry($ctx, $level, $mime);
320 return ghost_index_entry($ctx, $level, $node);
323 my $ret = join('', thread_adj_level($ctx, 0));
324 $ret .= ${$ctx->{dst}}; # skel
332 my $mid = $ctx->{mid};
333 my $srch = $ctx->{srch};
334 my $sres = $srch->get_thread($mid);
335 my $msgs = load_results($srch, $sres);
336 my $nr = $sres->{total};
337 return missing_thread($ctx) if $nr == 0;
338 my $skel = '<hr><pre>';
339 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
340 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
341 $skel .= "<b\nid=t>Thread overview:</b> ";
342 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
343 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
344 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
345 $skel .= "-- links below jump to the message on this page --\n";
346 $ctx->{-upfx} = '../../';
347 $ctx->{cur_level} = 0;
348 $ctx->{dst} = \$skel;
349 $ctx->{prev_attr} = '';
350 $ctx->{prev_level} = 0;
351 $ctx->{root_anchor} = anchor_for($mid);
353 $ctx->{mapping} = {};
354 $ctx->{s_nr} = "$nr+ messages in thread";
356 my $rootset = thread_results($msgs);
357 walk_thread($rootset, $ctx, *pre_thread);
359 return stream_thread($rootset, $ctx) unless $ctx->{flat};
361 # flat display: lazy load the full message from smsg
362 my $inbox = $ctx->{-inbox};
364 while ($mime = shift @$msgs) {
365 $mime = $inbox->msg_by_smsg($mime) and last;
367 return missing_thread($ctx) unless $mime;
368 $mime = PublicInbox::MIME->new($mime);
369 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
370 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
372 PublicInbox::WwwStream->response($ctx, 200, sub {
374 while ($mime = shift @$msgs) {
375 $mime = $inbox->msg_by_smsg($mime) and last;
378 $mime = PublicInbox::MIME->new($mime);
379 return index_entry($mime, $ctx, scalar @$msgs);
386 sub multipart_text_as_html {
387 my ($mime, $upfx) = @_;
390 # scan through all parts, looking for displayable text
391 msg_iter($mime, sub {
393 $rv .= add_text_body($upfx, $p);
399 my ($s, $l, $quot) = @_;
401 # show everything in the full version with anchor from
402 # short version (see above)
403 my $rv = $l->linkify_1(join('', @$quot));
406 # we use a <span> here to allow users to specify their own
407 # color for quoted text
408 $rv = $l->linkify_2(ascii_html($rv));
409 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
412 sub attach_link ($$$$;$) {
413 my ($upfx, $ct, $p, $fn, $err) = @_;
414 my ($part, $depth, @idx) = @$p;
415 my $nl = $idx[-1] > 1 ? "\n" : '';
416 my $idx = join('.', @idx);
417 my $size = bytes::length($part->body);
419 # hide attributes normally, unless we want to aid users in
420 # spotting MUA problems:
421 $ct =~ s/;.*// unless $err;
422 $ct = ascii_html($ct);
423 my $desc = $part->header('Content-Description');
424 $desc = $fn unless defined $desc;
425 $desc = '' unless defined $desc;
427 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
429 } elsif ($ct eq 'text/plain') {
434 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
437 "[-- Warning: decoded text below may be mangled --]\n";
439 $ret .= "[-- Attachment #$idx: ";
440 my $ts = "Type: $ct, Size: $size bytes";
441 $desc = ascii_html($desc);
442 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
447 my ($upfx, $p) = @_; # from msg_iter: [ Email::MIME, depth, @idx ]
448 my ($part, $depth) = @$p; # attachment @idx is unused
449 my $ct = $part->content_type || 'text/plain';
450 my $fn = $part->filename;
452 if ($ct =~ m!\btext/x?html\b!i) {
453 return attach_link($upfx, $ct, $p, $fn);
456 my $s = eval { $part->body_str };
458 # badly-encoded message? tell the world about it!
461 if ($ct =~ m!\btext/plain\b!i) {
462 # Try to assume UTF-8 because Alpine seems to
463 # do wacky things and set charset=X-UNKNOWN
464 $part->charset_set('UTF-8');
465 $s = eval { $part->body_str };
467 # If forcing charset=UTF-8 failed,
468 # attach_link will warn further down...
469 $s = $part->body if $@;
471 return attach_link($upfx, $ct, $p, $fn);
475 my @lines = split(/^/m, $s);
477 if (defined($fn) || $depth > 0 || $err) {
478 $s .= attach_link($upfx, $ct, $p, $fn, $err);
482 my $l = PublicInbox::Linkify->new;
483 foreach my $cur (@lines) {
485 # show the previously buffered quote inline
486 flush_quote(\$s, $l, \@quot) if @quot;
490 $s .= $l->linkify_2(ascii_html($cur));
496 if (@quot) { # ugh, top posted
497 flush_quote(\$s, $l, \@quot);
498 } elsif ($s =~ /\n\z/s) { # common, last line ends with a newline
500 } else { # some editors don't do newlines...
505 sub _msg_html_prepare {
506 my ($hdr, $ctx) = @_;
507 my $srch = $ctx->{srch} if $ctx;
509 my $rv = "<pre\nid=b>"; # anchor for body start
512 $ctx->{-upfx} = '../';
515 my $mid = mid_clean($hdr->header_raw('Message-ID'));
516 $mid = PublicInbox::Hval->new_msgid($mid);
517 foreach my $h (qw(From To Cc Subject Date)) {
518 my $v = $hdr->header($h);
519 defined($v) && ($v ne '') or next;
520 $v = PublicInbox::Hval->new($v);
523 my @n = PublicInbox::Address::names($v->raw);
524 $title[1] = ascii_html(join(', ', @n));
525 } elsif ($h eq 'Subject') {
526 $title[0] = $v->as_html;
528 $rv .= qq($h: <a\nhref="#r"\nid=t>);
529 $rv .= $v->as_html . "</a>\n";
534 $v =~ s/(\@[^,]+,) /$1\n\t/g if ($h eq 'Cc' || $h eq 'To');
538 $title[0] ||= '(no subject)';
539 $ctx->{-title_html} = join(' - ', @title);
540 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
541 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
542 $rv .= _parent_headers($hdr, $srch);
547 my ($dst, $ctx, $hdr, $tpfx) = @_;
548 my $srch = $ctx->{srch};
549 my $mid = mid_clean($hdr->header_raw('Message-ID'));
550 my $sres = $srch->get_thread($mid);
551 my $nr = $sres->{total};
552 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
553 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
554 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
556 my $parent = in_reply_to($hdr);
557 $$dst .= "\n<b>Thread overview: </b>";
559 if (defined $parent) {
560 $$dst .= "($expand)\n ";
561 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
563 $$dst .= "[no followups, yet] ($expand)\n";
565 $ctx->{next_msg} = undef;
566 $ctx->{parent_msg} = $parent;
570 $$dst .= "$nr+ messages in thread ($expand";
571 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
573 my $subj = $srch->subject_path($hdr->header('Subject'));
574 $ctx->{seen} = { $subj => 1 };
576 $ctx->{prev_attr} = '';
577 $ctx->{prev_level} = 0;
579 $sres = load_results($srch, $sres);
580 walk_thread(thread_results($sres), $ctx, *skel_dump);
581 $ctx->{parent_msg} = $parent;
584 sub _parent_headers {
585 my ($hdr, $srch) = @_;
588 my $irt = in_reply_to($hdr);
590 my $v = PublicInbox::Hval->new_msgid($irt);
591 my $html = $v->as_html;
592 my $href = $v->{href};
593 $rv .= "In-Reply-To: <";
594 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
597 # do not display References: if search is present,
598 # we show the thread skeleton at the bottom, instead.
601 my $refs = $hdr->header_raw('References');
603 # avoid redundant URLs wasting bandwidth
605 $seen{$irt} = 1 if defined $irt;
607 my @raw_refs = ($refs =~ /<([^>]+)>/g);
608 foreach my $ref (@raw_refs) {
611 push @refs, linkify_ref_nosrch($ref);
615 $rv .= 'References: '. join("\n\t", @refs) . "\n";
622 my ($hdr, $standalone, $ctx, $rhref) = @_;
624 my $srch = $ctx->{srch} if $ctx;
627 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
631 thread_skel(\$idx, $ctx, $hdr, $tpfx);
636 if (my $n = $ctx->{next_msg}) {
637 $n = PublicInbox::Hval->new_msgid($n)->{href};
638 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
641 my $par = $ctx->{parent_msg};
643 $u = PublicInbox::Hval->new_msgid($par)->{href};
646 if (my $p = $ctx->{prev_msg}) {
647 $prev = PublicInbox::Hval->new_msgid($p)->{href};
648 if ($p && $par && $p eq $par) {
649 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
650 'rel=prev>prev parent</a>';
653 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
655 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
657 } elsif ($u) { # unlikely
658 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
660 $irt = "$next $prev$parent ";
665 $irt .= qq(<a\nhref="$rhref">reply</a>);
669 sub linkify_ref_nosrch {
670 my $v = PublicInbox::Hval->new_msgid($_[0]);
671 my $html = $v->as_html;
672 my $href = $v->{href};
673 "<<a\nhref=\"../$href/\">$html</a>>";
678 'm' . id_compress($msgid, 1);
682 my ($upfx, $mid) = @_;
684 $mid = PublicInbox::Hval->new_msgid($mid);
685 my $href = $mid->{href};
686 my $html = $mid->as_html;
687 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
692 INDENT x ($level - 1);
696 my ($srch, $sres) = @_;
697 my $msgs = delete $sres->{msgs};
698 $srch->retry_reopen(sub { [ map { $_->mid; $_ } @$msgs ] });
703 my $ts = eval { str2time($hdr->header('Date')) };
704 defined($ts) ? $ts : 0;
709 require PublicInbox::SearchThread;
710 PublicInbox::SearchThread::thread($msgs, *sort_ts);
715 require PublicInbox::ExtMsg;
716 PublicInbox::ExtMsg::ext_msg($ctx);
721 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
725 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
728 my ($ctx, $level, $node) = @_;
729 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
731 my $dst = $ctx->{dst};
732 my $cur = $ctx->{cur};
733 my $mid = $smsg->{mid};
734 my $f = ascii_html($smsg->from_name);
735 my $d = fmt_ts($smsg->{ts}) . ' ' . indent_for($level) . th_pfx($level);
737 $ctx->{first_level} ||= $level;
739 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
740 $ctx->{prev_attr} = $attr;
742 $ctx->{prev_level} = $level;
747 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
748 "$attr [this message]</a></b>\n";
751 $ctx->{prev_msg} = $mid;
754 $ctx->{next_msg} ||= $mid;
757 # Subject is never undef, this mail was loaded from
758 # our Xapian which would've resulted in '' if it were
759 # really missing (and Filter rejects empty subjects)
760 my $s = $smsg->subject;
761 my $h = $ctx->{srch}->subject_path($s);
762 if ($ctx->{seen}->{$h}) {
765 $ctx->{seen}->{$h} = 1;
766 $s = PublicInbox::Hval->new($s);
771 my $mapping = $ctx->{mapping};
772 my $end = defined($s) ? "$s</a> $f\n" : "$f</a>\n";
774 my $map = $mapping->{$mid};
775 $id = id_compress($mid, 1);
777 $map->[0] = "$d<a\nhref=\"$m\">$end";
780 $m = $ctx->{-upfx}.mid_escape($mid).'/';
782 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
786 my ($ctx, $level, $node) = @_;
788 my $mid = $node->{id};
789 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
791 $d .= indent_for($level) . th_pfx($level);
792 my $upfx = $ctx->{-upfx};
793 my $m = PublicInbox::Hval->new_msgid($mid);
794 my $href = $upfx . $m->{href} . '/';
795 my $html = $m->as_html;
797 my $mapping = $ctx->{mapping};
798 my $map = $mapping->{$mid} if $mapping;
800 my $id = id_compress($mid, 1);
801 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
802 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
804 $d .= qq{<<a\nhref="$href">$html</a>>\n};
806 my $dst = $ctx->{dst};
812 (eval { $a->topmost->{smsg}->ts } || 0) <=>
813 (eval { $b->topmost->{smsg}->ts } || 0)
817 # accumulate recent topics if search is supported
818 # returns 200 if done, 404 if not
820 my ($ctx, $level, $node) = @_;
821 my $srch = $ctx->{srch};
822 my $mid = $node->{id};
823 my $x = $node->{smsg} || $srch->lookup_mail($mid);
828 $subj = $srch->subject_normalized($subj);
831 $topic = [ $ts, 1, { $subj => $mid }, $subj ];
832 $ctx->{-cur_topic} = $topic;
833 push @{$ctx->{order}}, $topic;
837 $topic = $ctx->{-cur_topic}; # should never be undef
838 $topic->[0] = $ts if $ts > $topic->[0];
840 my $seen = $topic->[2];
841 if (scalar(@$topic) == 3) { # parent was a ghost
843 } elsif (!$seen->{$subj}) {
844 push @$topic, $level, $subj;
846 $seen->{$subj} = $mid; # latest for subject
847 } else { # ghost message
848 return if $level != 0; # ignore child ghosts
849 $topic = [ -666, 0, {} ];
850 $ctx->{-cur_topic} = $topic;
851 push @{$ctx->{order}}, $topic;
857 my $order = delete $ctx->{order}; # [ ts, subj1, subj2, subj3, ... ]
859 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
865 # sort by recency, this allows new posts to "bump" old topics...
866 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
867 my ($ts, $n, $seen, $top, @ex) = @$topic;
869 next unless defined $top; # ghost topic
870 my $mid = delete $seen->{$top};
871 my $href = mid_escape($mid);
872 $top = PublicInbox::Hval->new($top)->as_html;
875 # $n isn't the total number of posts on the topic,
876 # just the number of posts in the current results window
880 $anchor = '#u'; # top of only message
882 $n = " ($n+ messages)";
883 $anchor = '#t'; # thread skeleton
886 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
887 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
888 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
889 " $ts UTC $n - $mbox / $atom\n";
890 for (my $i = 0; $i < scalar(@ex); $i += 2) {
892 my $sub = $ex[$i + 1];
893 $mid = delete $seen->{$sub};
894 $sub = PublicInbox::Hval->new($sub)->as_html;
895 $href = mid_escape($mid);
896 $s .= indent_for($level) . TCHILD;
897 $s .= "<a\nhref=\"$href/T/#u\">$sub</a>\n";
901 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
905 sub index_nav { # callback for WwwStream
906 my (undef, $ctx) = @_;
907 delete $ctx->{qp} or return;
912 my $next_o = $ctx->{-next_o};
914 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
916 if (my $cur_o = $ctx->{-cur_o}) {
917 $latest = qq! <a\nhref=.>latest</a>!;
919 my $o = $cur_o - ($next_o - $cur_o);
921 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
923 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
926 "<hr><pre>page: $next $prev$latest</pre>";
931 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
932 my $opts = { offset => $off, limit => 200 };
935 my $srch = $ctx->{srch};
936 my $sres = $srch->query('', $opts);
937 my $nr = scalar @{$sres->{msgs}};
939 $sres = load_results($srch, $sres);
940 walk_thread(thread_results($sres), $ctx, *acc_topic);
942 $ctx->{-next_o} = $off+ $nr;
943 $ctx->{-cur_o} = $off;
944 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
947 sub thread_adj_level {
948 my ($ctx, $level) = @_;
950 my $max = $ctx->{cur_level};
952 return ('', '') if $max == 0; # flat output
954 # reset existing lists
955 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
956 $ctx->{cur_level} = 0;
958 } elsif ($level == $max) { # continue existing list
960 } elsif ($level < $max) {
961 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
962 $ctx->{cur_level} = $level;
963 ("$beg<li>", '</li>');
964 } else { # ($level > $max) # start a new level
965 $ctx->{cur_level} = $level;
966 my $beg = ($max ? '<li>' : '') . '<ul><li>';
971 sub ghost_index_entry {
972 my ($ctx, $level, $node) = @_;
973 my ($beg, $end) = thread_adj_level($ctx, $level);
974 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})