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 obfuscate_addrs/;
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 $ibx = $ctx->{-inbox};
28 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
29 my $tip = _msg_html_prepare($hdr, $ctx, $obfs_ibx);
30 PublicInbox::WwwStream->response($ctx, 200, sub {
33 $tip . multipart_text_as_html($mime, '', $obfs_ibx) .
36 # fake an EOF if generating the footer fails;
37 # we want to at least show the message if something
40 '<pre>' . html_footer($hdr, 1, $ctx) .
41 '</pre>' . msg_reply($ctx, $hdr)
49 # /$INBOX/$MESSAGE_ID/#R
53 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
55 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
58 my $ibx = $ctx->{-inbox};
59 if (my $url = $ibx->{infourl}) {
60 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
61 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
64 my ($arg, $link) = PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
66 # mailto: link only works if address obfuscation is disabled
70 * If your mail client supports setting the <b>In-Reply-To</b> header
71 via mailto: links, try the <a
72 href="$link">mailto: link</a></pre>
75 push @$arg, '/path/to/YOUR_REPLY';
76 $arg = ascii_html(join(" \\\n ", '', @$arg));
79 id=R><b>Reply instructions:</b>
81 You may reply publically to <a
82 href=#t>this message</a> via plain-text email
83 using any one of the following methods:
85 * Save the following mbox file, import it into your mail client,
86 and reply-to-all from there: <a
89 Avoid top-posting and favor interleaved quoting:
91 href="$p_url">$p_url</a>
93 * Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,
94 and <b>--in-reply-to</b> switches of git-send-email(1):
99 href="$se_url">$se_url</a>
106 my %mid = map { $_ => 1 } $hdr->header_raw('Message-ID');
107 my @refs = ($hdr->header_raw('References'),
108 $hdr->header_raw('In-Reply-To'));
109 @refs = ((join(' ', @refs)) =~ /<([^>]+)>/g);
110 while (defined(my $irt = pop @refs)) {
111 next if $mid{"<$irt>"};
117 sub _hdr_names_html ($$) {
118 my ($hdr, $field) = @_;
119 my $val = $hdr->header($field) or return '';
120 ascii_html(join(', ', PublicInbox::Address::names($val)));
124 my ($nr, $singular, $plural) = @_;
125 return "0 $plural" if $nr == 0;
126 $nr == 1 ? "$nr $singular" : "$nr $plural";
129 # this is already inside a <pre>
131 my ($mime, $ctx, $more) = @_;
132 my $srch = $ctx->{srch};
133 my $hdr = $mime->header_obj;
134 my $subj = $hdr->header('Subject');
136 my $mid_raw = mid_clean(mid_mime($mime));
137 my $id = id_compress($mid_raw, 1);
140 my $root_anchor = $ctx->{root_anchor} || '';
142 my $obfs_ibx = $ctx->{-obfs_ibx};
144 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
145 $subj = '<b>'.ascii_html($subj).'</b>';
146 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
147 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
149 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
151 foreach my $f (qw(To Cc)) {
152 my $dst = _hdr_names_html($hdr, $f);
154 obfuscate_addrs($obfs_ibx, $dst) if $obfs_ibx;
155 push @tocc, "$f: $dst";
158 my $from = _hdr_names_html($hdr, 'From');
159 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
160 $rv .= "From: $from @ "._msg_date($hdr)." UTC";
161 my $upfx = $ctx->{-upfx};
162 my $mhref = $upfx . mid_escape($mid_raw) . '/';
163 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
164 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
165 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
167 my $mapping = $ctx->{mapping};
168 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
169 my $mirt = PublicInbox::Hval->new_msgid($irt);
170 my $href = $upfx . $mirt->{href}. '/';
171 my $html = $mirt->as_html;
172 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
176 # scan through all parts, looking for displayable text
177 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) });
180 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
181 "<a\nhref=\"$mhref\">permalink</a>" .
182 " <a\nhref=\"${mhref}raw\">raw</a>" .
183 " <a\nhref=\"${mhref}#R\">reply</a>";
186 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
187 $rv .= "\t[relevance $pct->{$mid_raw}%]";
190 my $threaded = 'threaded';
195 $flat = "<b>$flat</b>";
197 $threaded = "<b>$threaded</b>";
199 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
200 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
201 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
206 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
210 sub pad_link ($$;$) {
211 my ($mid, $level, $s) = @_;
213 my $id = id_compress($mid, 1);
214 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
218 my ($mid_raw, $irt, $id, $ctx) = @_;
220 my $mapping = $ctx->{mapping} or return $rv;
222 my $mid_map = $mapping->{$mid_raw};
224 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
225 my ($attr, $node, $idx, $level) = @$mid_map;
226 my $children = $node->{children};
227 my $nr_c = scalar @$children;
230 if (my $smsg = $node->{smsg}) {
231 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
233 my $irt_map = $mapping->{$$irt} if defined $$irt;
234 if (defined $irt_map) {
235 $siblings = $irt_map->[1]->{children};
236 $nr_s = scalar(@$siblings) - 1;
237 $rv .= $pad . $irt_map->[0];
239 my $prev = $siblings->[$idx - 1];
240 my $pmid = $prev->{id};
242 my $s = ($idx - 1). ' preceding siblings ...';
243 $rv .= pad_link($pmid, $level, $s);
244 } elsif ($idx == 2) {
245 my $ppmid = $siblings->[0]->{id};
246 $rv .= $pad . $mapping->{$ppmid}->[0];
248 $rv .= $pad . $mapping->{$pmid}->[0];
251 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
252 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
253 $attr =~ s!\n\z!</b>\n!s;
254 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
255 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
258 my $cmid = $children->[0]->{id};
259 $rv .= $pad . $mapping->{$cmid}->[0];
261 my $s = ($nr_c - 1). ' more replies';
262 $rv .= pad_link($cmid, $level + 1, $s);
263 } elsif (my $cn = $children->[1]) {
264 $rv .= $pad . $mapping->{$cn->{id}}->[0];
268 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
270 my $nmid = $next->{id};
271 $rv .= $pad . $mapping->{$nmid}->[0];
272 my $nnext = $nr_s - $idx;
274 my $s = ($nnext - 1).' subsequent siblings';
275 $rv .= pad_link($nmid, $level, $s);
276 } elsif (my $nn = $siblings->[$idx + 2]) {
277 $rv .= $pad . $mapping->{$nn->{id}}->[0];
280 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
284 my ($rootset, $ctx, $cb) = @_;
285 my @q = map { (0, $_, -1) } @$rootset;
287 my ($level, $node, $i) = splice(@q, 0, 3);
288 defined $node or next;
289 $cb->($ctx, $level, $node, $i);
292 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
297 my ($ctx, $level, $node, $idx) = @_;
298 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
299 skel_dump($ctx, $level, $node);
302 sub thread_index_entry {
303 my ($ctx, $level, $mime) = @_;
304 my ($beg, $end) = thread_adj_level($ctx, $level);
305 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
308 sub stream_thread ($$) {
309 my ($rootset, $ctx) = @_;
310 my $inbox = $ctx->{-inbox};
312 my @q = map { (0, $_) } @$rootset;
316 my $node = shift @q or next;
318 unshift @q, map { ($cl, $_) } @{$node->{children}};
319 $mime = $inbox->msg_by_smsg($node->{smsg}) and last;
321 return missing_thread($ctx) unless $mime;
323 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
324 $mime = PublicInbox::MIME->new($mime);
325 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
326 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
327 PublicInbox::WwwStream->response($ctx, 200, sub {
331 my $node = shift @q or next;
333 unshift @q, map { ($cl, $_) } @{$node->{children}};
334 my $mid = $node->{id};
335 if ($mime = $inbox->msg_by_smsg($node->{smsg})) {
336 $mime = PublicInbox::MIME->new($mime);
337 return thread_index_entry($ctx, $level, $mime);
339 return ghost_index_entry($ctx, $level, $node);
342 my $ret = join('', thread_adj_level($ctx, 0));
343 $ret .= ${$ctx->{dst}}; # skel
351 my $mid = $ctx->{mid};
352 my $srch = $ctx->{srch};
353 my $sres = $srch->get_thread($mid);
354 my $msgs = load_results($srch, $sres);
355 my $nr = $sres->{total};
356 return missing_thread($ctx) if $nr == 0;
357 my $skel = '<hr><pre>';
358 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
359 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
360 $skel .= "<b\nid=t>Thread overview:</b> ";
361 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
362 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
363 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
364 $skel .= "-- links below jump to the message on this page --\n";
365 $ctx->{-upfx} = '../../';
366 $ctx->{cur_level} = 0;
367 $ctx->{dst} = \$skel;
368 $ctx->{prev_attr} = '';
369 $ctx->{prev_level} = 0;
370 $ctx->{root_anchor} = anchor_for($mid);
372 $ctx->{mapping} = {};
373 $ctx->{s_nr} = "$nr+ messages in thread";
375 my $rootset = thread_results($msgs);
377 # reduce hash lookups in pre_thread->skel_dump
378 my $inbox = $ctx->{-inbox};
379 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
380 walk_thread($rootset, $ctx, *pre_thread);
383 return stream_thread($rootset, $ctx) unless $ctx->{flat};
385 # flat display: lazy load the full message from smsg
387 while ($mime = shift @$msgs) {
388 $mime = $inbox->msg_by_smsg($mime) and last;
390 return missing_thread($ctx) unless $mime;
391 $mime = PublicInbox::MIME->new($mime);
392 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
393 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
395 PublicInbox::WwwStream->response($ctx, 200, sub {
397 while ($mime = shift @$msgs) {
398 $mime = $inbox->msg_by_smsg($mime) and last;
401 $mime = PublicInbox::MIME->new($mime);
402 return index_entry($mime, $ctx, scalar @$msgs);
409 sub multipart_text_as_html {
410 my ($mime, $upfx, $obfs_ibx) = @_;
413 # scan through all parts, looking for displayable text
414 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs_ibx, $_[0]) });
419 my ($s, $l, $quot) = @_;
421 # show everything in the full version with anchor from
422 # short version (see above)
423 my $rv = $l->linkify_1(join('', @$quot));
426 # we use a <span> here to allow users to specify their own
427 # color for quoted text
428 $rv = $l->linkify_2(ascii_html($rv));
429 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
432 sub attach_link ($$$$;$) {
433 my ($upfx, $ct, $p, $fn, $err) = @_;
434 my ($part, $depth, @idx) = @$p;
435 my $nl = $idx[-1] > 1 ? "\n" : '';
436 my $idx = join('.', @idx);
437 my $size = bytes::length($part->body);
439 # hide attributes normally, unless we want to aid users in
440 # spotting MUA problems:
441 $ct =~ s/;.*// unless $err;
442 $ct = ascii_html($ct);
443 my $desc = $part->header('Content-Description');
444 $desc = $fn unless defined $desc;
445 $desc = '' unless defined $desc;
447 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
449 } elsif ($ct eq 'text/plain') {
454 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
457 "[-- Warning: decoded text below may be mangled --]\n";
459 $ret .= "[-- Attachment #$idx: ";
460 my $ts = "Type: $ct, Size: $size bytes";
461 $desc = ascii_html($desc);
462 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
467 my ($upfx, $obfs_ibx, $p) = @_;
468 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
469 my ($part, $depth) = @$p; # attachment @idx is unused
470 my $ct = $part->content_type || 'text/plain';
471 my $fn = $part->filename;
473 if ($ct =~ m!\btext/x?html\b!i) {
474 return attach_link($upfx, $ct, $p, $fn);
477 my $s = eval { $part->body_str };
479 # badly-encoded message? tell the world about it!
482 if ($ct =~ m!\btext/plain\b!i) {
483 # Try to assume UTF-8 because Alpine seems to
484 # do wacky things and set charset=X-UNKNOWN
485 $part->charset_set('UTF-8');
486 $s = eval { $part->body_str };
488 # If forcing charset=UTF-8 failed,
489 # attach_link will warn further down...
490 $s = $part->body if $@;
492 return attach_link($upfx, $ct, $p, $fn);
496 my @lines = split(/^/m, $s);
498 if (defined($fn) || $depth > 0 || $err) {
499 $s .= attach_link($upfx, $ct, $p, $fn, $err);
503 my $l = PublicInbox::Linkify->new;
504 foreach my $cur (@lines) {
506 # show the previously buffered quote inline
507 flush_quote(\$s, $l, \@quot) if @quot;
511 $s .= $l->linkify_2(ascii_html($cur));
517 if (@quot) { # ugh, top posted
518 flush_quote(\$s, $l, \@quot);
519 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
522 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
523 if ($s =~ /\n\z/s) { # common, last line ends with a newline
525 } else { # some editors don't do newlines...
531 sub _msg_html_prepare {
532 my ($hdr, $ctx, $obfs_ibx) = @_;
533 my $srch = $ctx->{srch} if $ctx;
535 my $rv = "<pre\nid=b>"; # anchor for body start
538 $ctx->{-upfx} = '../';
541 my $mid = mid_clean($hdr->header_raw('Message-ID'));
542 $mid = PublicInbox::Hval->new_msgid($mid);
543 foreach my $h (qw(From To Cc Subject Date)) {
544 my $v = $hdr->header($h);
545 defined($v) && ($v ne '') or next;
546 $v = PublicInbox::Hval->new($v);
549 my @n = PublicInbox::Address::names($v->raw);
550 $title[1] = ascii_html(join(', ', @n));
551 obfuscate_addrs($obfs_ibx, $title[1]) if $obfs_ibx;
552 } elsif ($h eq 'Subject') {
553 $title[0] = $v->as_html;
555 $rv .= qq($h: <a\nhref="#r"\nid=t>);
556 $rv .= $v->as_html . "</a>\n";
561 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
565 $title[0] ||= '(no subject)';
566 $ctx->{-title_html} = join(' - ', @title);
567 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
568 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
569 $rv .= _parent_headers($hdr, $srch);
574 my ($dst, $ctx, $hdr, $tpfx) = @_;
575 my $srch = $ctx->{srch};
576 my $mid = mid_clean($hdr->header_raw('Message-ID'));
577 my $sres = $srch->get_thread($mid);
578 my $nr = $sres->{total};
579 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
580 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
581 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
583 my $parent = in_reply_to($hdr);
584 $$dst .= "\n<b>Thread overview: </b>";
586 if (defined $parent) {
587 $$dst .= "($expand)\n ";
588 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
590 $$dst .= "[no followups, yet] ($expand)\n";
592 $ctx->{next_msg} = undef;
593 $ctx->{parent_msg} = $parent;
597 $$dst .= "$nr+ messages in thread ($expand";
598 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
600 my $subj = $srch->subject_path($hdr->header('Subject'));
601 $ctx->{seen} = { $subj => 1 };
603 $ctx->{prev_attr} = '';
604 $ctx->{prev_level} = 0;
606 $sres = load_results($srch, $sres);
608 # reduce hash lookups in skel_dump
609 my $ibx = $ctx->{-inbox};
610 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
611 walk_thread(thread_results($sres), $ctx, *skel_dump);
613 $ctx->{parent_msg} = $parent;
616 sub _parent_headers {
617 my ($hdr, $srch) = @_;
620 my $irt = in_reply_to($hdr);
622 my $v = PublicInbox::Hval->new_msgid($irt);
623 my $html = $v->as_html;
624 my $href = $v->{href};
625 $rv .= "In-Reply-To: <";
626 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
629 # do not display References: if search is present,
630 # we show the thread skeleton at the bottom, instead.
633 my $refs = $hdr->header_raw('References');
635 # avoid redundant URLs wasting bandwidth
637 $seen{$irt} = 1 if defined $irt;
639 my @raw_refs = ($refs =~ /<([^>]+)>/g);
640 foreach my $ref (@raw_refs) {
643 push @refs, linkify_ref_nosrch($ref);
647 $rv .= 'References: '. join("\n\t", @refs) . "\n";
654 my ($hdr, $standalone, $ctx, $rhref) = @_;
656 my $srch = $ctx->{srch} if $ctx;
659 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
663 thread_skel(\$idx, $ctx, $hdr, $tpfx);
668 if (my $n = $ctx->{next_msg}) {
669 $n = PublicInbox::Hval->new_msgid($n)->{href};
670 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
673 my $par = $ctx->{parent_msg};
675 $u = PublicInbox::Hval->new_msgid($par)->{href};
678 if (my $p = $ctx->{prev_msg}) {
679 $prev = PublicInbox::Hval->new_msgid($p)->{href};
680 if ($p && $par && $p eq $par) {
681 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
682 'rel=prev>prev parent</a>';
685 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
687 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
689 } elsif ($u) { # unlikely
690 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
692 $irt = "$next $prev$parent ";
697 $irt .= qq(<a\nhref="$rhref">reply</a>);
701 sub linkify_ref_nosrch {
702 my $v = PublicInbox::Hval->new_msgid($_[0]);
703 my $html = $v->as_html;
704 my $href = $v->{href};
705 "<<a\nhref=\"../$href/\">$html</a>>";
710 'm' . id_compress($msgid, 1);
714 my ($upfx, $mid) = @_;
716 $mid = PublicInbox::Hval->new_msgid($mid);
717 my $href = $mid->{href};
718 my $html = $mid->as_html;
719 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
724 INDENT x ($level - 1);
728 my ($srch, $sres) = @_;
729 my $msgs = delete $sres->{msgs};
730 $srch->retry_reopen(sub { [ map { $_->mid; $_ } @$msgs ] });
735 my $ts = eval { str2time($hdr->header('Date')) };
736 defined($ts) ? $ts : 0;
741 require PublicInbox::SearchThread;
742 PublicInbox::SearchThread::thread($msgs, *sort_ts);
747 require PublicInbox::ExtMsg;
748 PublicInbox::ExtMsg::ext_msg($ctx);
753 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
757 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
760 my ($ctx, $level, $node) = @_;
761 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
763 my $dst = $ctx->{dst};
764 my $cur = $ctx->{cur};
765 my $mid = $smsg->{mid};
767 my $f = ascii_html($smsg->from_name);
768 my $obfs_ibx = $ctx->{-obfs_ibx};
769 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
771 my $d = fmt_ts($smsg->{ts}) . ' ' . indent_for($level) . th_pfx($level);
773 $ctx->{first_level} ||= $level;
775 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
776 $ctx->{prev_attr} = $attr;
778 $ctx->{prev_level} = $level;
783 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
784 "$attr [this message]</a></b>\n";
787 $ctx->{prev_msg} = $mid;
790 $ctx->{next_msg} ||= $mid;
793 # Subject is never undef, this mail was loaded from
794 # our Xapian which would've resulted in '' if it were
795 # really missing (and Filter rejects empty subjects)
796 my $subj = $smsg->subject;
797 my $h = $ctx->{srch}->subject_path($subj);
798 if ($ctx->{seen}->{$h}) {
801 $ctx->{seen}->{$h} = 1;
802 $subj = PublicInbox::Hval->new($subj);
803 $subj = $subj->as_html;
804 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
808 my $mapping = $ctx->{mapping};
809 my $end = defined($subj) ? "$subj</a> $f\n" : "$f</a>\n";
811 my $map = $mapping->{$mid};
812 $id = id_compress($mid, 1);
814 $map->[0] = "$d<a\nhref=\"$m\">$end";
817 $m = $ctx->{-upfx}.mid_escape($mid).'/';
819 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
823 my ($ctx, $level, $node) = @_;
825 my $mid = $node->{id};
826 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
828 $d .= indent_for($level) . th_pfx($level);
829 my $upfx = $ctx->{-upfx};
830 my $m = PublicInbox::Hval->new_msgid($mid);
831 my $href = $upfx . $m->{href} . '/';
832 my $html = $m->as_html;
834 my $mapping = $ctx->{mapping};
835 my $map = $mapping->{$mid} if $mapping;
837 my $id = id_compress($mid, 1);
838 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
839 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
841 $d .= qq{<<a\nhref="$href">$html</a>>\n};
843 my $dst = $ctx->{dst};
849 (eval { $a->topmost->{smsg}->ts } || 0) <=>
850 (eval { $b->topmost->{smsg}->ts } || 0)
854 # accumulate recent topics if search is supported
855 # returns 200 if done, 404 if not
857 my ($ctx, $level, $node) = @_;
858 my $srch = $ctx->{srch};
859 my $mid = $node->{id};
860 my $x = $node->{smsg} || $srch->lookup_mail($mid);
865 $subj = $srch->subject_normalized($subj);
868 $topic = [ $ts, 1, { $subj => $mid }, $subj ];
869 $ctx->{-cur_topic} = $topic;
870 push @{$ctx->{order}}, $topic;
874 $topic = $ctx->{-cur_topic}; # should never be undef
875 $topic->[0] = $ts if $ts > $topic->[0];
877 my $seen = $topic->[2];
878 if (scalar(@$topic) == 3) { # parent was a ghost
880 } elsif (!$seen->{$subj}) {
881 push @$topic, $level, $subj;
883 $seen->{$subj} = $mid; # latest for subject
884 } else { # ghost message
885 return if $level != 0; # ignore child ghosts
886 $topic = [ -666, 0, {} ];
887 $ctx->{-cur_topic} = $topic;
888 push @{$ctx->{order}}, $topic;
894 my $order = delete $ctx->{order}; # [ ts, subj1, subj2, subj3, ... ]
896 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
901 my $ibx = $ctx->{-inbox};
902 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
904 # sort by recency, this allows new posts to "bump" old topics...
905 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
906 my ($ts, $n, $seen, $top, @ex) = @$topic;
908 next unless defined $top; # ghost topic
909 my $mid = delete $seen->{$top};
910 my $href = mid_escape($mid);
911 $top = PublicInbox::Hval->new($top)->as_html;
914 # $n isn't the total number of posts on the topic,
915 # just the number of posts in the current results window
919 $anchor = '#u'; # top of only message
921 $n = " ($n+ messages)";
922 $anchor = '#t'; # thread skeleton
925 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
926 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
927 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
928 " $ts UTC $n - $mbox / $atom\n";
929 for (my $i = 0; $i < scalar(@ex); $i += 2) {
931 my $subj = $ex[$i + 1];
932 $mid = delete $seen->{$subj};
933 $subj = ascii_html($subj);
934 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
935 $href = mid_escape($mid);
936 $s .= indent_for($level) . TCHILD;
937 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>\n";
941 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
945 sub index_nav { # callback for WwwStream
946 my (undef, $ctx) = @_;
947 delete $ctx->{qp} or return;
952 my $next_o = $ctx->{-next_o};
954 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
956 if (my $cur_o = $ctx->{-cur_o}) {
957 $latest = qq! <a\nhref=.>latest</a>!;
959 my $o = $cur_o - ($next_o - $cur_o);
961 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
963 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
966 "<hr><pre>page: $next $prev$latest</pre>";
971 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
972 my $opts = { offset => $off, limit => 200 };
975 my $srch = $ctx->{srch};
976 my $sres = $srch->query('', $opts);
977 my $nr = scalar @{$sres->{msgs}};
979 $sres = load_results($srch, $sres);
980 walk_thread(thread_results($sres), $ctx, *acc_topic);
982 $ctx->{-next_o} = $off+ $nr;
983 $ctx->{-cur_o} = $off;
984 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
987 sub thread_adj_level {
988 my ($ctx, $level) = @_;
990 my $max = $ctx->{cur_level};
992 return ('', '') if $max == 0; # flat output
994 # reset existing lists
995 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
996 $ctx->{cur_level} = 0;
998 } elsif ($level == $max) { # continue existing list
1000 } elsif ($level < $max) {
1001 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1002 $ctx->{cur_level} = $level;
1003 ("$beg<li>", '</li>');
1004 } else { # ($level > $max) # start a new level
1005 $ctx->{cur_level} = $level;
1006 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1011 sub ghost_index_entry {
1012 my ($ctx, $level, $node) = @_;
1013 my ($beg, $end) = thread_adj_level($ctx, $level);
1014 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})