1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <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 PublicInbox::MsgTime qw(msg_datestamp);
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 mids
14 use PublicInbox::MsgIter;
15 use PublicInbox::Address;
16 use PublicInbox::WwwStream;
17 use PublicInbox::Reply;
20 use constant INDENT => ' ';
21 use constant TCHILD => '` ';
22 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
24 # public functions: (unstable)
27 my ($ctx, $mime, $more, $smsg) = @_;
28 my $hdr = $mime->header_obj;
29 my $ibx = $ctx->{-inbox};
30 my $obfs_ibx = $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
31 my $tip = _msg_html_prepare($hdr, $ctx, $more, 0);
33 PublicInbox::WwwStream->response($ctx, 200, sub {
36 # $more cannot be true w/o $smsg being defined:
37 my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : '';
38 $tip . multipart_text_as_html($mime, $upfx, $obfs_ibx) .
40 } elsif ($more && @$more) {
42 msg_html_more($ctx, $more, $nr);
43 } elsif ($nr == $end) {
44 # fake an EOF if generating the footer fails;
45 # we want to at least show the message if something
48 '<pre>' . html_footer($hdr, 1, $ctx) .
49 '</pre>' . msg_reply($ctx, $hdr)
59 my $mid = $ctx->{mid};
60 my $ibx = $ctx->{-inbox};
61 my ($first, $more, $head, $tail, $db);
63 if (my $srch = $ibx->search) {
64 $srch->retry_reopen(sub {
65 ($head, $tail, $db) = $srch->each_smsg_by_mid($mid);
66 for (; !defined($first) && $head != $tail; $head++) {
67 my @args = ($head, $db, $mid);
68 $smsg = PublicInbox::SearchMsg->get(@args);
69 $first = $ibx->msg_by_smsg($smsg);
72 $more = [ $head, $tail, $db ];
77 $first = $ibx->msg_by_mid($mid) or return;
79 msg_html($ctx, PublicInbox::MIME->new($first), $more, $smsg);
83 my ($ctx, $more, $nr) = @_;
86 my ($head, $tail, $db) = @$more;
87 my $mid = $ctx->{mid};
88 for (; !defined($smsg) && $head != $tail; $head++) {
89 my $m = PublicInbox::SearchMsg->get($head, $db, $mid);
90 $smsg = $ctx->{-inbox}->smsg_mime($m);
92 if ($head == $tail) { # done
98 my $mime = $smsg->{mime};
99 my $upfx = '../' . mid_escape($smsg->mid) . '/';
100 _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
101 multipart_text_as_html($mime, $upfx,
109 warn "Error lookup up additional messages: $@\n";
110 $str = '<pre>Error looking up additional messages</pre>';
115 # /$INBOX/$MESSAGE_ID/#R
117 my ($ctx, $hdr) = @_;
119 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
121 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
124 my $ibx = $ctx->{-inbox};
125 if (my $url = $ibx->{infourl}) {
126 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
127 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
130 my ($arg, $link, $reply_to_all) =
131 PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
133 # mailto: link only works if address obfuscation is disabled
137 * If your mail client supports setting the <b>In-Reply-To</b> header
138 via mailto: links, try the <a
139 href="$link">mailto: link</a>
143 push @$arg, '/path/to/YOUR_REPLY';
144 $arg = ascii_html(join(" \\\n ", '', @$arg));
147 id=R><b>Reply instructions:</b>
149 You may reply publically to <a
150 href=#t>this message</a> via plain-text email
151 using any one of the following methods:
153 * Save the following mbox file, import it into your mail client,
154 and $reply_to_all from there: <a
157 Avoid top-posting and favor interleaved quoting:
159 href="$p_url">$p_url</a>
161 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
162 switches of git-send-email(1):
167 href="$se_url">$se_url</a>
174 my $refs = references($hdr);
178 sub _hdr_names_html ($$) {
179 my ($hdr, $field) = @_;
180 my $val = $hdr->header($field) or return '';
181 ascii_html(join(', ', PublicInbox::Address::names($val)));
185 my ($nr, $singular, $plural) = @_;
186 return "0 $plural" if $nr == 0;
187 $nr == 1 ? "$nr $singular" : "$nr $plural";
190 # this is already inside a <pre>
192 my ($smsg, $ctx, $more) = @_;
193 my $srch = $ctx->{srch};
194 my $subj = $smsg->subject;
195 my $mid_raw = $smsg->mid;
196 my $id = id_compress($mid_raw, 1);
199 my $root_anchor = $ctx->{root_anchor} || '';
201 my $obfs_ibx = $ctx->{-obfs_ibx};
203 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
204 $subj = '<b>'.ascii_html($subj).'</b>';
205 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
206 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
208 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
210 my $mime = $smsg->{mime};
211 my $hdr = $mime->header_obj;
212 foreach my $f (qw(To Cc)) {
213 my $dst = _hdr_names_html($hdr, $f);
215 obfuscate_addrs($obfs_ibx, $dst) if $obfs_ibx;
216 push @tocc, "$f: $dst";
219 my $from = _hdr_names_html($hdr, 'From');
220 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
221 $rv .= "From: $from @ ".fmt_ts($smsg->ds)." UTC";
222 my $upfx = $ctx->{-upfx};
223 my $mhref = $upfx . mid_escape($mid_raw) . '/';
224 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
225 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
226 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
228 my $mapping = $ctx->{mapping};
229 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
230 my $mirt = PublicInbox::Hval->new_msgid($irt);
231 my $href = $upfx . $mirt->{href}. '/';
232 my $html = $mirt->as_html;
233 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
237 # scan through all parts, looking for displayable text
238 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) });
241 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
242 "<a\nhref=\"$mhref\">permalink</a>" .
243 " <a\nhref=\"${mhref}raw\">raw</a>" .
244 " <a\nhref=\"${mhref}#R\">reply</a>";
247 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
248 $rv .= "\t[relevance $pct->{$mid_raw}%]";
251 my $nested = 'nested';
256 $flat = "<b>$flat</b>";
258 $nested = "<b>$nested</b>";
260 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
261 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
262 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
267 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
271 sub pad_link ($$;$) {
272 my ($mid, $level, $s) = @_;
274 my $id = id_compress($mid, 1);
275 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
279 my ($mid_raw, $irt, $id, $ctx) = @_;
281 my $mapping = $ctx->{mapping} or return $rv;
283 my $mid_map = $mapping->{$mid_raw};
285 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
286 my ($attr, $node, $idx, $level) = @$mid_map;
287 my $children = $node->{children};
288 my $nr_c = scalar @$children;
291 if (my $smsg = $node->{smsg}) {
292 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
294 my $irt_map = $mapping->{$$irt} if defined $$irt;
295 if (defined $irt_map) {
296 $siblings = $irt_map->[1]->{children};
297 $nr_s = scalar(@$siblings) - 1;
298 $rv .= $pad . $irt_map->[0];
300 my $prev = $siblings->[$idx - 1];
301 my $pmid = $prev->{id};
303 my $s = ($idx - 1). ' preceding siblings ...';
304 $rv .= pad_link($pmid, $level, $s);
305 } elsif ($idx == 2) {
306 my $ppmid = $siblings->[0]->{id};
307 $rv .= $pad . $mapping->{$ppmid}->[0];
309 $rv .= $pad . $mapping->{$pmid}->[0];
312 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
313 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
314 $attr =~ s!\n\z!</b>\n!s;
315 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
316 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
319 my $cmid = $children->[0]->{id};
320 $rv .= $pad . $mapping->{$cmid}->[0];
322 my $s = ($nr_c - 1). ' more replies';
323 $rv .= pad_link($cmid, $level + 1, $s);
324 } elsif (my $cn = $children->[1]) {
325 $rv .= $pad . $mapping->{$cn->{id}}->[0];
329 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
331 my $nmid = $next->{id};
332 $rv .= $pad . $mapping->{$nmid}->[0];
333 my $nnext = $nr_s - $idx;
335 my $s = ($nnext - 1).' subsequent siblings';
336 $rv .= pad_link($nmid, $level, $s);
337 } elsif (my $nn = $siblings->[$idx + 2]) {
338 $rv .= $pad . $mapping->{$nn->{id}}->[0];
341 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
345 my ($rootset, $ctx, $cb) = @_;
346 my @q = map { (0, $_, -1) } @$rootset;
348 my ($level, $node, $i) = splice(@q, 0, 3);
349 defined $node or next;
350 $cb->($ctx, $level, $node, $i);
353 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
358 my ($ctx, $level, $node, $idx) = @_;
359 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
360 skel_dump($ctx, $level, $node);
363 sub thread_index_entry {
364 my ($ctx, $level, $smsg) = @_;
365 my ($beg, $end) = thread_adj_level($ctx, $level);
366 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
369 sub stream_thread ($$) {
370 my ($rootset, $ctx) = @_;
371 my $inbox = $ctx->{-inbox};
372 my @q = map { (0, $_) } @$rootset;
377 my $node = shift @q or next;
379 unshift @q, map { ($cl, $_) } @{$node->{children}};
380 $smsg = $inbox->smsg_mime($node->{smsg}) and last;
382 return missing_thread($ctx) unless $smsg;
384 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
385 $ctx->{-title_html} = ascii_html($smsg->subject);
386 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
388 PublicInbox::WwwStream->response($ctx, 200, sub {
392 my $node = shift @q or next;
394 unshift @q, map { ($cl, $_) } @{$node->{children}};
395 if ($smsg = $inbox->smsg_mime($node->{smsg})) {
396 return thread_index_entry($ctx, $level, $smsg);
398 return ghost_index_entry($ctx, $level, $node);
401 my $ret = join('', thread_adj_level($ctx, 0));
402 $ret .= ${$ctx->{dst}}; # skel
410 my $mid = $ctx->{mid};
411 my $srch = $ctx->{srch};
412 my $sres = $srch->get_thread($mid);
413 my $msgs = load_results($srch, $sres);
414 my $nr = $sres->{total};
415 return missing_thread($ctx) if $nr == 0;
416 my $skel = '<hr><pre>';
417 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
418 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
419 $skel .= "<b\nid=t>Thread overview:</b> ";
420 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
421 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
422 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
423 $skel .= "-- links below jump to the message on this page --\n";
424 $ctx->{-upfx} = '../../';
425 $ctx->{cur_level} = 0;
426 $ctx->{dst} = \$skel;
427 $ctx->{prev_attr} = '';
428 $ctx->{prev_level} = 0;
429 $ctx->{root_anchor} = anchor_for($mid);
430 $ctx->{mapping} = {};
431 $ctx->{s_nr} = "$nr+ messages in thread";
433 my $rootset = thread_results($ctx, $msgs);
435 # reduce hash lookups in pre_thread->skel_dump
436 my $inbox = $ctx->{-inbox};
437 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
438 walk_thread($rootset, $ctx, *pre_thread);
441 return stream_thread($rootset, $ctx) unless $ctx->{flat};
443 # flat display: lazy load the full message from smsg
445 while (my $m = shift @$msgs) {
446 $smsg = $inbox->smsg_mime($m) and last;
448 return missing_thread($ctx) unless $smsg;
449 $ctx->{-title_html} = ascii_html($smsg->subject);
450 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
452 PublicInbox::WwwStream->response($ctx, 200, sub {
455 while (my $m = shift @$msgs) {
456 $smsg = $inbox->smsg_mime($m) and last;
458 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
464 sub multipart_text_as_html {
465 my ($mime, $upfx, $obfs_ibx) = @_;
468 # scan through all parts, looking for displayable text
469 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs_ibx, $_[0]) });
474 my ($s, $l, $quot) = @_;
476 # show everything in the full version with anchor from
477 # short version (see above)
478 my $rv = $l->linkify_1(join('', @$quot));
481 # we use a <span> here to allow users to specify their own
482 # color for quoted text
483 $rv = $l->linkify_2(ascii_html($rv));
484 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
487 sub attach_link ($$$$;$) {
488 my ($upfx, $ct, $p, $fn, $err) = @_;
489 my ($part, $depth, @idx) = @$p;
490 my $nl = $idx[-1] > 1 ? "\n" : '';
491 my $idx = join('.', @idx);
492 my $size = bytes::length($part->body);
494 # hide attributes normally, unless we want to aid users in
495 # spotting MUA problems:
496 $ct =~ s/;.*// unless $err;
497 $ct = ascii_html($ct);
498 my $desc = $part->header('Content-Description');
499 $desc = $fn unless defined $desc;
500 $desc = '' unless defined $desc;
502 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
504 } elsif ($ct eq 'text/plain') {
509 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
512 "[-- Warning: decoded text below may be mangled --]\n";
514 $ret .= "[-- Attachment #$idx: ";
515 my $ts = "Type: $ct, Size: $size bytes";
516 $desc = ascii_html($desc);
517 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
522 my ($upfx, $obfs_ibx, $p) = @_;
523 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
524 my ($part, $depth) = @$p; # attachment @idx is unused
525 my $ct = $part->content_type || 'text/plain';
526 my $fn = $part->filename;
528 if ($ct =~ m!\btext/x?html\b!i) {
529 return attach_link($upfx, $ct, $p, $fn);
532 my $s = eval { $part->body_str };
534 # badly-encoded message? tell the world about it!
537 if ($ct =~ m!\btext/plain\b!i) {
538 # Try to assume UTF-8 because Alpine seems to
539 # do wacky things and set charset=X-UNKNOWN
540 $part->charset_set('UTF-8');
541 $s = eval { $part->body_str };
543 # If forcing charset=UTF-8 failed,
544 # attach_link will warn further down...
545 $s = $part->body if $@;
547 return attach_link($upfx, $ct, $p, $fn);
551 my @lines = split(/^/m, $s);
553 if (defined($fn) || $depth > 0 || $err) {
554 $s .= attach_link($upfx, $ct, $p, $fn, $err);
558 my $l = PublicInbox::Linkify->new;
559 foreach my $cur (@lines) {
561 # show the previously buffered quote inline
562 flush_quote(\$s, $l, \@quot) if @quot;
566 $s .= $l->linkify_2(ascii_html($cur));
572 if (@quot) { # ugh, top posted
573 flush_quote(\$s, $l, \@quot);
574 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
577 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
578 if ($s =~ /\n\z/s) { # common, last line ends with a newline
580 } else { # some editors don't do newlines...
586 sub _msg_html_prepare {
587 my ($hdr, $ctx, $more, $nr) = @_;
588 my $srch = $ctx->{srch} if $ctx;
590 my $obfs_ibx = $ctx->{-obfs_ibx};
592 my $mids = mids($hdr);
593 my $multiple = scalar(@$mids) > 1; # zero, one, infinity
597 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
599 $rv .= "<pre\nid=b>"; # anchor for body start
604 $ctx->{-upfx} = '../';
607 foreach my $h (qw(From To Cc Subject Date)) {
608 my $v = $hdr->header($h);
609 defined($v) && ($v ne '') or next;
610 $v = PublicInbox::Hval->new($v);
613 my @n = PublicInbox::Address::names($v->raw);
614 $title[1] = ascii_html(join(', ', @n));
615 obfuscate_addrs($obfs_ibx, $title[1]) if $obfs_ibx;
616 } elsif ($h eq 'Subject') {
617 $title[0] = $v->as_html;
619 $rv .= qq($h: <a\nhref="#r"\nid=t>);
620 $rv .= $v->as_html . "</a>\n";
625 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
629 $title[0] ||= '(no subject)';
630 $ctx->{-title_html} = join(' - ', @title);
632 my $mid = PublicInbox::Hval->new_msgid($_) ;
633 my $mhtml = $mid->as_html;
635 my $href = $mid->{href};
636 $rv .= "Message-ID: ";
637 $rv .= "<a\nhref=\"../$href/\">";
638 $rv .= "<$mhtml></a> ";
639 $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
641 $rv .= "Message-ID: <$mhtml> ";
642 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
645 $rv .= _parent_headers($hdr, $srch);
650 my ($dst, $ctx, $hdr, $tpfx) = @_;
651 my $srch = $ctx->{srch};
652 my $mid = mids($hdr)->[0];
653 my $sres = $srch->get_thread($mid);
654 my $nr = $sres->{total};
655 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
656 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
657 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
658 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
660 my $parent = in_reply_to($hdr);
661 $$dst .= "\n<b>Thread overview: </b>";
663 if (defined $parent) {
664 $$dst .= "$expand\n ";
665 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
667 $$dst .= "[no followups] $expand\n";
669 $ctx->{next_msg} = undef;
670 $ctx->{parent_msg} = $parent;
674 $$dst .= "$nr+ messages / $expand";
675 $$dst .= qq! <a\nhref="#b">top</a>\n!;
677 my $subj = $hdr->header('Subject');
678 defined $subj or $subj = '';
679 $ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
681 $ctx->{prev_attr} = '';
682 $ctx->{prev_level} = 0;
684 $sres = load_results($srch, $sres);
686 # reduce hash lookups in skel_dump
687 my $ibx = $ctx->{-inbox};
688 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
689 walk_thread(thread_results($ctx, $sres), $ctx, *skel_dump);
691 $ctx->{parent_msg} = $parent;
694 sub _parent_headers {
695 my ($hdr, $srch) = @_;
698 my $irt = in_reply_to($hdr);
700 my $v = PublicInbox::Hval->new_msgid($irt);
701 my $html = $v->as_html;
702 my $href = $v->{href};
703 $rv .= "In-Reply-To: <";
704 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
707 # do not display References: if search is present,
708 # we show the thread skeleton at the bottom, instead.
711 my $refs = $hdr->header_raw('References');
713 # avoid redundant URLs wasting bandwidth
715 $seen{$irt} = 1 if defined $irt;
717 my @raw_refs = ($refs =~ /<([^>]+)>/g);
718 foreach my $ref (@raw_refs) {
721 push @refs, linkify_ref_nosrch($ref);
725 $rv .= 'References: '. join("\n\t", @refs) . "\n";
732 my ($hdr, $standalone, $ctx, $rhref) = @_;
734 my $srch = $ctx->{srch} if $ctx;
737 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
741 thread_skel(\$idx, $ctx, $hdr, $tpfx);
746 if (my $n = $ctx->{next_msg}) {
747 $n = PublicInbox::Hval->new_msgid($n)->{href};
748 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
751 my $par = $ctx->{parent_msg};
753 $u = PublicInbox::Hval->new_msgid($par)->{href};
756 if (my $p = $ctx->{prev_msg}) {
757 $prev = PublicInbox::Hval->new_msgid($p)->{href};
758 if ($p && $par && $p eq $par) {
759 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
760 'rel=prev>prev parent</a>';
763 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
765 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
767 } elsif ($u) { # unlikely
768 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
770 $irt = "$next $prev$parent ";
775 $irt .= qq(<a\nhref="$rhref">reply</a>);
779 sub linkify_ref_nosrch {
780 my $v = PublicInbox::Hval->new_msgid($_[0]);
781 my $html = $v->as_html;
782 my $href = $v->{href};
783 "<<a\nhref=\"../$href/\">$html</a>>";
788 'm' . id_compress($msgid, 1);
792 my ($upfx, $mid) = @_;
794 $mid = PublicInbox::Hval->new_msgid($mid);
795 my $href = $mid->{href};
796 my $html = $mid->as_html;
797 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
802 $level ? INDENT x ($level - 1) : '';
806 my ($srch, $sres) = @_;
807 my $msgs = delete $sres->{msgs};
808 $srch->retry_reopen(sub { [ map { $_->mid; $_ } @$msgs ] });
812 my ($ctx, $msgs) = @_;
813 require PublicInbox::SearchThread;
814 PublicInbox::SearchThread::thread($msgs, *sort_ds, $ctx->{-inbox});
819 require PublicInbox::ExtMsg;
820 PublicInbox::ExtMsg::ext_msg($ctx);
825 fmt_ts(msg_datestamp($hdr));
828 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
831 my ($prev_subj, $subj, $val) = @_;
833 my $omit = ''; # '"' denotes identical text omitted
834 my (@prev_pop, @curr_pop);
835 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
836 push(@prev_pop, pop(@$prev_subj));
837 push(@curr_pop, pop(@$subj));
840 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
841 if (scalar(@curr_pop) == 1) {
843 push @$prev_subj, @prev_pop;
844 push @$subj, @curr_pop;
850 my ($ctx, $level, $node) = @_;
851 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
853 my $dst = $ctx->{dst};
854 my $cur = $ctx->{cur};
855 my $mid = $smsg->{mid};
857 my $f = ascii_html($smsg->from_name);
858 my $obfs_ibx = $ctx->{-obfs_ibx};
859 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
861 my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
863 $ctx->{first_level} ||= $level;
865 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
866 $ctx->{prev_attr} = $attr;
868 $ctx->{prev_level} = $level;
873 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
874 "$attr [this message]</a></b>\n";
877 $ctx->{prev_msg} = $mid;
880 $ctx->{next_msg} ||= $mid;
883 # Subject is never undef, this mail was loaded from
884 # our Xapian which would've resulted in '' if it were
885 # really missing (and Filter rejects empty subjects)
886 my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject));
888 # remove common suffixes from the subject if it matches the previous,
889 # so we do not show redundant text at the end.
890 my $prev_subj = $ctx->{prev_subj} || [];
891 $ctx->{prev_subj} = [ @subj ];
892 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
895 my $subj = join(' ', @subj);
896 $subj = ascii_html($subj);
897 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
898 $end = "$subj</a> $omit$f\n"
904 my $mapping = $ctx->{mapping};
906 my $map = $mapping->{$mid};
907 $id = id_compress($mid, 1);
909 $map->[0] = "$d<a\nhref=\"$m\">$end";
912 $m = $ctx->{-upfx}.mid_escape($mid).'/';
914 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
918 my ($ctx, $level, $node) = @_;
920 my $mid = $node->{id};
921 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
923 $d .= indent_for($level) . th_pfx($level);
924 my $upfx = $ctx->{-upfx};
925 my $m = PublicInbox::Hval->new_msgid($mid);
926 my $href = $upfx . $m->{href} . '/';
927 my $html = $m->as_html;
929 my $mapping = $ctx->{mapping};
930 my $map = $mapping->{$mid} if $mapping;
932 my $id = id_compress($mid, 1);
933 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
934 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
936 $d .= qq{<<a\nhref="$href">$html</a>>\n};
938 my $dst = $ctx->{dst};
944 (eval { $a->topmost->{smsg}->ds } || 0) <=>
945 (eval { $b->topmost->{smsg}->ds } || 0)
949 # accumulate recent topics if search is supported
950 # returns 200 if done, 404 if not
952 my ($ctx, $level, $node) = @_;
953 my $srch = $ctx->{srch};
954 my $mid = $node->{id};
955 my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
960 $subj = $srch->subject_normalized($subj);
963 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
964 $ctx->{-cur_topic} = $topic;
965 push @{$ctx->{order}}, $topic;
969 $topic = $ctx->{-cur_topic}; # should never be undef
970 $topic->[0] = $ds if $ds > $topic->[0];
972 my $seen = $topic->[2];
973 if (scalar(@$topic) == 3) { # parent was a ghost
975 } elsif (!$seen->{$subj}) {
976 push @$topic, $level, $subj;
978 $seen->{$subj} = $mid; # latest for subject
979 } else { # ghost message
980 return if $level != 0; # ignore child ghosts
981 $topic = [ -666, 0, {} ];
982 $ctx->{-cur_topic} = $topic;
983 push @{$ctx->{order}}, $topic;
989 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
991 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
996 my $ibx = $ctx->{-inbox};
997 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
998 my $srch = $ctx->{srch};
1000 # sort by recency, this allows new posts to "bump" old topics...
1001 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
1002 my ($ds, $n, $seen, $top, @ex) = @$topic;
1004 next unless defined $top; # ghost topic
1005 my $mid = delete $seen->{$top};
1006 my $href = mid_escape($mid);
1007 my $prev_subj = [ split(/ /, $top) ];
1008 $top = PublicInbox::Hval->new($top)->as_html;
1011 # $n isn't the total number of posts on the topic,
1012 # just the number of posts in the current results window
1016 $anchor = '#u'; # top of only message
1018 $n = " ($n+ messages)";
1019 $anchor = '#t'; # thread skeleton
1022 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1023 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1024 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
1025 " $ds UTC $n - $mbox / $atom\n";
1026 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1027 my $level = $ex[$i];
1028 my $subj = $ex[$i + 1];
1029 $mid = delete $seen->{$subj};
1030 my @subj = split(/ /, $srch->subject_normalized($subj));
1031 my @next_prev = @subj; # full copy
1032 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1033 $prev_subj = \@next_prev;
1034 $subj = ascii_html(join(' ', @subj));
1035 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1036 $href = mid_escape($mid);
1037 $s .= indent_for($level) . TCHILD;
1038 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1042 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1046 sub index_nav { # callback for WwwStream
1047 my (undef, $ctx) = @_;
1048 delete $ctx->{qp} or return;
1050 $next = $prev = ' ';
1053 my $next_o = $ctx->{-next_o};
1055 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
1057 if (my $cur_o = $ctx->{-cur_o}) {
1058 $latest = qq! <a\nhref=.>latest</a>!;
1060 my $o = $cur_o - ($next_o - $cur_o);
1062 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
1064 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
1067 "<hr><pre>page: $next $prev$latest</pre>";
1072 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
1073 my $opts = { offset => $off, limit => 200 };
1076 my $srch = $ctx->{srch};
1077 my $sres = $srch->query('', $opts);
1078 my $nr = scalar @{$sres->{msgs}};
1080 $sres = load_results($srch, $sres);
1081 walk_thread(thread_results($ctx, $sres), $ctx, *acc_topic);
1083 $ctx->{-next_o} = $off+ $nr;
1084 $ctx->{-cur_o} = $off;
1085 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1088 sub thread_adj_level {
1089 my ($ctx, $level) = @_;
1091 my $max = $ctx->{cur_level};
1093 return ('', '') if $max == 0; # flat output
1095 # reset existing lists
1096 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1097 $ctx->{cur_level} = 0;
1099 } elsif ($level == $max) { # continue existing list
1101 } elsif ($level < $max) {
1102 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1103 $ctx->{cur_level} = $level;
1104 ("$beg<li>", '</li>');
1105 } else { # ($level > $max) # start a new level
1106 $ctx->{cur_level} = $level;
1107 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1112 sub ghost_index_entry {
1113 my ($ctx, $level, $node) = @_;
1114 my ($beg, $end) = thread_adj_level($ctx, $level);
1115 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})