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) = @_;
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 $tip . multipart_text_as_html($mime, '', $obfs_ibx) .
38 } elsif ($more && @$more) {
40 msg_html_more($ctx, $more, $nr);
41 } elsif ($nr == $end) {
42 # fake an EOF if generating the footer fails;
43 # we want to at least show the message if something
46 '<pre>' . html_footer($hdr, 1, $ctx) .
47 '</pre>' . msg_reply($ctx, $hdr)
57 my $mid = $ctx->{mid};
58 my $ibx = $ctx->{-inbox};
59 my ($first, $more, $head, $tail, $db);
60 if (my $srch = $ibx->search) {
61 $srch->retry_reopen(sub {
62 ($head, $tail, $db) = $srch->each_smsg_by_mid($mid);
63 for (; !defined($first) && $head != $tail; $head++) {
64 my @args = ($head, $db, $mid);
65 my $smsg = PublicInbox::SearchMsg->get(@args);
66 next if $smsg->type ne 'mail';
67 $first = $ibx->msg_by_smsg($smsg);
70 $more = [ $head, $tail, $db ];
75 $first = $ibx->msg_by_mid($mid) or return;
77 msg_html($ctx, PublicInbox::MIME->new($first), $more);
81 my ($ctx, $more, $nr) = @_;
84 my ($head, $tail, $db) = @$more;
85 my $mid = $ctx->{mid};
86 for (; !defined($smsg) && $head != $tail; $head++) {
87 my $m = PublicInbox::SearchMsg->get($head, $db, $mid);
88 next if $m->type ne 'mail';
89 $smsg = $ctx->{-inbox}->smsg_mime($m);
91 if ($head == $tail) { # done
97 my $mime = $smsg->{mime};
98 _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
99 multipart_text_as_html($mime, '',
107 warn "Error lookup up additional messages: $@\n";
108 $str = '<pre>Error looking up additional messages</pre>';
113 # /$INBOX/$MESSAGE_ID/#R
115 my ($ctx, $hdr) = @_;
117 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
119 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
122 my $ibx = $ctx->{-inbox};
123 if (my $url = $ibx->{infourl}) {
124 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
125 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
128 my ($arg, $link, $reply_to_all) =
129 PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
131 # mailto: link only works if address obfuscation is disabled
135 * If your mail client supports setting the <b>In-Reply-To</b> header
136 via mailto: links, try the <a
137 href="$link">mailto: link</a>
141 push @$arg, '/path/to/YOUR_REPLY';
142 $arg = ascii_html(join(" \\\n ", '', @$arg));
145 id=R><b>Reply instructions:</b>
147 You may reply publically to <a
148 href=#t>this message</a> via plain-text email
149 using any one of the following methods:
151 * Save the following mbox file, import it into your mail client,
152 and $reply_to_all from there: <a
155 Avoid top-posting and favor interleaved quoting:
157 href="$p_url">$p_url</a>
159 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
160 switches of git-send-email(1):
165 href="$se_url">$se_url</a>
172 my $refs = references($hdr);
176 sub _hdr_names_html ($$) {
177 my ($hdr, $field) = @_;
178 my $val = $hdr->header($field) or return '';
179 ascii_html(join(', ', PublicInbox::Address::names($val)));
183 my ($nr, $singular, $plural) = @_;
184 return "0 $plural" if $nr == 0;
185 $nr == 1 ? "$nr $singular" : "$nr $plural";
188 # this is already inside a <pre>
190 my ($smsg, $ctx, $more) = @_;
191 my $srch = $ctx->{srch};
192 my $subj = $smsg->subject;
193 my $mid_raw = $smsg->mid;
194 my $id = id_compress($mid_raw, 1);
197 my $root_anchor = $ctx->{root_anchor} || '';
199 my $obfs_ibx = $ctx->{-obfs_ibx};
201 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
202 $subj = '<b>'.ascii_html($subj).'</b>';
203 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
204 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
206 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
208 my $mime = $smsg->{mime};
209 my $hdr = $mime->header_obj;
210 foreach my $f (qw(To Cc)) {
211 my $dst = _hdr_names_html($hdr, $f);
213 obfuscate_addrs($obfs_ibx, $dst) if $obfs_ibx;
214 push @tocc, "$f: $dst";
217 my $from = _hdr_names_html($hdr, 'From');
218 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
219 $rv .= "From: $from @ ".fmt_ts($smsg->ds)." UTC";
220 my $upfx = $ctx->{-upfx};
221 my $mhref = $upfx . mid_escape($mid_raw) . '/';
222 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
223 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
224 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
226 my $mapping = $ctx->{mapping};
227 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
228 my $mirt = PublicInbox::Hval->new_msgid($irt);
229 my $href = $upfx . $mirt->{href}. '/';
230 my $html = $mirt->as_html;
231 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
235 # scan through all parts, looking for displayable text
236 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) });
239 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
240 "<a\nhref=\"$mhref\">permalink</a>" .
241 " <a\nhref=\"${mhref}raw\">raw</a>" .
242 " <a\nhref=\"${mhref}#R\">reply</a>";
245 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
246 $rv .= "\t[relevance $pct->{$mid_raw}%]";
249 my $nested = 'nested';
254 $flat = "<b>$flat</b>";
256 $nested = "<b>$nested</b>";
258 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
259 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
260 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
265 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
269 sub pad_link ($$;$) {
270 my ($mid, $level, $s) = @_;
272 my $id = id_compress($mid, 1);
273 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
277 my ($mid_raw, $irt, $id, $ctx) = @_;
279 my $mapping = $ctx->{mapping} or return $rv;
281 my $mid_map = $mapping->{$mid_raw};
283 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
284 my ($attr, $node, $idx, $level) = @$mid_map;
285 my $children = $node->{children};
286 my $nr_c = scalar @$children;
289 if (my $smsg = $node->{smsg}) {
290 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
292 my $irt_map = $mapping->{$$irt} if defined $$irt;
293 if (defined $irt_map) {
294 $siblings = $irt_map->[1]->{children};
295 $nr_s = scalar(@$siblings) - 1;
296 $rv .= $pad . $irt_map->[0];
298 my $prev = $siblings->[$idx - 1];
299 my $pmid = $prev->{id};
301 my $s = ($idx - 1). ' preceding siblings ...';
302 $rv .= pad_link($pmid, $level, $s);
303 } elsif ($idx == 2) {
304 my $ppmid = $siblings->[0]->{id};
305 $rv .= $pad . $mapping->{$ppmid}->[0];
307 $rv .= $pad . $mapping->{$pmid}->[0];
310 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
311 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
312 $attr =~ s!\n\z!</b>\n!s;
313 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
314 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
317 my $cmid = $children->[0]->{id};
318 $rv .= $pad . $mapping->{$cmid}->[0];
320 my $s = ($nr_c - 1). ' more replies';
321 $rv .= pad_link($cmid, $level + 1, $s);
322 } elsif (my $cn = $children->[1]) {
323 $rv .= $pad . $mapping->{$cn->{id}}->[0];
327 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
329 my $nmid = $next->{id};
330 $rv .= $pad . $mapping->{$nmid}->[0];
331 my $nnext = $nr_s - $idx;
333 my $s = ($nnext - 1).' subsequent siblings';
334 $rv .= pad_link($nmid, $level, $s);
335 } elsif (my $nn = $siblings->[$idx + 2]) {
336 $rv .= $pad . $mapping->{$nn->{id}}->[0];
339 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
343 my ($rootset, $ctx, $cb) = @_;
344 my @q = map { (0, $_, -1) } @$rootset;
346 my ($level, $node, $i) = splice(@q, 0, 3);
347 defined $node or next;
348 $cb->($ctx, $level, $node, $i);
351 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
356 my ($ctx, $level, $node, $idx) = @_;
357 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
358 skel_dump($ctx, $level, $node);
361 sub thread_index_entry {
362 my ($ctx, $level, $smsg) = @_;
363 my ($beg, $end) = thread_adj_level($ctx, $level);
364 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
367 sub stream_thread ($$) {
368 my ($rootset, $ctx) = @_;
369 my $inbox = $ctx->{-inbox};
370 my @q = map { (0, $_) } @$rootset;
375 my $node = shift @q or next;
377 unshift @q, map { ($cl, $_) } @{$node->{children}};
378 $smsg = $inbox->smsg_mime($node->{smsg}) and last;
380 return missing_thread($ctx) unless $smsg;
382 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
383 $ctx->{-title_html} = ascii_html($smsg->subject);
384 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
386 PublicInbox::WwwStream->response($ctx, 200, sub {
390 my $node = shift @q or next;
392 unshift @q, map { ($cl, $_) } @{$node->{children}};
393 if ($smsg = $inbox->smsg_mime($node->{smsg})) {
394 return thread_index_entry($ctx, $level, $smsg);
396 return ghost_index_entry($ctx, $level, $node);
399 my $ret = join('', thread_adj_level($ctx, 0));
400 $ret .= ${$ctx->{dst}}; # skel
408 my $mid = $ctx->{mid};
409 my $srch = $ctx->{srch};
410 my $sres = $srch->get_thread($mid);
411 my $msgs = load_results($srch, $sres);
412 my $nr = $sres->{total};
413 return missing_thread($ctx) if $nr == 0;
414 my $skel = '<hr><pre>';
415 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
416 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
417 $skel .= "<b\nid=t>Thread overview:</b> ";
418 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
419 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
420 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
421 $skel .= "-- links below jump to the message on this page --\n";
422 $ctx->{-upfx} = '../../';
423 $ctx->{cur_level} = 0;
424 $ctx->{dst} = \$skel;
425 $ctx->{prev_attr} = '';
426 $ctx->{prev_level} = 0;
427 $ctx->{root_anchor} = anchor_for($mid);
428 $ctx->{mapping} = {};
429 $ctx->{s_nr} = "$nr+ messages in thread";
431 my $rootset = thread_results($msgs, $srch);
433 # reduce hash lookups in pre_thread->skel_dump
434 my $inbox = $ctx->{-inbox};
435 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
436 walk_thread($rootset, $ctx, *pre_thread);
439 return stream_thread($rootset, $ctx) unless $ctx->{flat};
441 # flat display: lazy load the full message from smsg
443 while (my $m = shift @$msgs) {
444 $smsg = $inbox->smsg_mime($m) and last;
446 return missing_thread($ctx) unless $smsg;
447 $ctx->{-title_html} = ascii_html($smsg->subject);
448 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
450 PublicInbox::WwwStream->response($ctx, 200, sub {
453 while (my $m = shift @$msgs) {
454 $smsg = $inbox->smsg_mime($m) and last;
456 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
462 sub multipart_text_as_html {
463 my ($mime, $upfx, $obfs_ibx) = @_;
466 # scan through all parts, looking for displayable text
467 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs_ibx, $_[0]) });
472 my ($s, $l, $quot) = @_;
474 # show everything in the full version with anchor from
475 # short version (see above)
476 my $rv = $l->linkify_1(join('', @$quot));
479 # we use a <span> here to allow users to specify their own
480 # color for quoted text
481 $rv = $l->linkify_2(ascii_html($rv));
482 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
485 sub attach_link ($$$$;$) {
486 my ($upfx, $ct, $p, $fn, $err) = @_;
487 my ($part, $depth, @idx) = @$p;
488 my $nl = $idx[-1] > 1 ? "\n" : '';
489 my $idx = join('.', @idx);
490 my $size = bytes::length($part->body);
492 # hide attributes normally, unless we want to aid users in
493 # spotting MUA problems:
494 $ct =~ s/;.*// unless $err;
495 $ct = ascii_html($ct);
496 my $desc = $part->header('Content-Description');
497 $desc = $fn unless defined $desc;
498 $desc = '' unless defined $desc;
500 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
502 } elsif ($ct eq 'text/plain') {
507 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
510 "[-- Warning: decoded text below may be mangled --]\n";
512 $ret .= "[-- Attachment #$idx: ";
513 my $ts = "Type: $ct, Size: $size bytes";
514 $desc = ascii_html($desc);
515 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
520 my ($upfx, $obfs_ibx, $p) = @_;
521 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
522 my ($part, $depth) = @$p; # attachment @idx is unused
523 my $ct = $part->content_type || 'text/plain';
524 my $fn = $part->filename;
526 if ($ct =~ m!\btext/x?html\b!i) {
527 return attach_link($upfx, $ct, $p, $fn);
530 my $s = eval { $part->body_str };
532 # badly-encoded message? tell the world about it!
535 if ($ct =~ m!\btext/plain\b!i) {
536 # Try to assume UTF-8 because Alpine seems to
537 # do wacky things and set charset=X-UNKNOWN
538 $part->charset_set('UTF-8');
539 $s = eval { $part->body_str };
541 # If forcing charset=UTF-8 failed,
542 # attach_link will warn further down...
543 $s = $part->body if $@;
545 return attach_link($upfx, $ct, $p, $fn);
549 my @lines = split(/^/m, $s);
551 if (defined($fn) || $depth > 0 || $err) {
552 $s .= attach_link($upfx, $ct, $p, $fn, $err);
556 my $l = PublicInbox::Linkify->new;
557 foreach my $cur (@lines) {
559 # show the previously buffered quote inline
560 flush_quote(\$s, $l, \@quot) if @quot;
564 $s .= $l->linkify_2(ascii_html($cur));
570 if (@quot) { # ugh, top posted
571 flush_quote(\$s, $l, \@quot);
572 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
575 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
576 if ($s =~ /\n\z/s) { # common, last line ends with a newline
578 } else { # some editors don't do newlines...
584 sub _msg_html_prepare {
585 my ($hdr, $ctx, $more, $nr) = @_;
586 my $srch = $ctx->{srch} if $ctx;
588 my $obfs_ibx = $ctx->{-obfs_ibx};
590 my $mids = mids($hdr);
591 my $multiple = scalar(@$mids) > 1; # zero, one, infinity
595 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
597 $rv .= "<pre\nid=b>"; # anchor for body start
602 $ctx->{-upfx} = '../';
605 foreach my $h (qw(From To Cc Subject Date)) {
606 my $v = $hdr->header($h);
607 defined($v) && ($v ne '') or next;
608 $v = PublicInbox::Hval->new($v);
611 my @n = PublicInbox::Address::names($v->raw);
612 $title[1] = ascii_html(join(', ', @n));
613 obfuscate_addrs($obfs_ibx, $title[1]) if $obfs_ibx;
614 } elsif ($h eq 'Subject') {
615 $title[0] = $v->as_html;
617 $rv .= qq($h: <a\nhref="#r"\nid=t>);
618 $rv .= $v->as_html . "</a>\n";
623 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
627 $title[0] ||= '(no subject)';
628 $ctx->{-title_html} = join(' - ', @title);
630 my $mid = PublicInbox::Hval->new_msgid($_) ;
631 my $mhtml = $mid->as_html;
633 my $href = $mid->{href};
634 $rv .= "Message-ID: ";
635 $rv .= "<a\nhref=\"../$href/\">";
636 $rv .= "<$mhtml></a> ";
637 $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
639 $rv .= "Message-ID: <$mhtml> ";
640 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
643 $rv .= _parent_headers($hdr, $srch);
648 my ($dst, $ctx, $hdr, $tpfx) = @_;
649 my $srch = $ctx->{srch};
650 my $mid = mids($hdr)->[0];
651 my $sres = $srch->get_thread($mid);
652 my $nr = $sres->{total};
653 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
654 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
655 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
656 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
658 my $parent = in_reply_to($hdr);
659 $$dst .= "\n<b>Thread overview: </b>";
661 if (defined $parent) {
662 $$dst .= "$expand\n ";
663 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
665 $$dst .= "[no followups] $expand\n";
667 $ctx->{next_msg} = undef;
668 $ctx->{parent_msg} = $parent;
672 $$dst .= "$nr+ messages / $expand";
673 $$dst .= qq! <a\nhref="#b">top</a>\n!;
675 my $subj = $hdr->header('Subject');
676 defined $subj or $subj = '';
677 $ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
679 $ctx->{prev_attr} = '';
680 $ctx->{prev_level} = 0;
682 $sres = load_results($srch, $sres);
684 # reduce hash lookups in skel_dump
685 my $ibx = $ctx->{-inbox};
686 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
687 walk_thread(thread_results($sres, $srch), $ctx, *skel_dump);
689 $ctx->{parent_msg} = $parent;
692 sub _parent_headers {
693 my ($hdr, $srch) = @_;
696 my $irt = in_reply_to($hdr);
698 my $v = PublicInbox::Hval->new_msgid($irt);
699 my $html = $v->as_html;
700 my $href = $v->{href};
701 $rv .= "In-Reply-To: <";
702 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
705 # do not display References: if search is present,
706 # we show the thread skeleton at the bottom, instead.
709 my $refs = $hdr->header_raw('References');
711 # avoid redundant URLs wasting bandwidth
713 $seen{$irt} = 1 if defined $irt;
715 my @raw_refs = ($refs =~ /<([^>]+)>/g);
716 foreach my $ref (@raw_refs) {
719 push @refs, linkify_ref_nosrch($ref);
723 $rv .= 'References: '. join("\n\t", @refs) . "\n";
730 my ($hdr, $standalone, $ctx, $rhref) = @_;
732 my $srch = $ctx->{srch} if $ctx;
735 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
739 thread_skel(\$idx, $ctx, $hdr, $tpfx);
744 if (my $n = $ctx->{next_msg}) {
745 $n = PublicInbox::Hval->new_msgid($n)->{href};
746 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
749 my $par = $ctx->{parent_msg};
751 $u = PublicInbox::Hval->new_msgid($par)->{href};
754 if (my $p = $ctx->{prev_msg}) {
755 $prev = PublicInbox::Hval->new_msgid($p)->{href};
756 if ($p && $par && $p eq $par) {
757 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
758 'rel=prev>prev parent</a>';
761 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
763 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
765 } elsif ($u) { # unlikely
766 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
768 $irt = "$next $prev$parent ";
773 $irt .= qq(<a\nhref="$rhref">reply</a>);
777 sub linkify_ref_nosrch {
778 my $v = PublicInbox::Hval->new_msgid($_[0]);
779 my $html = $v->as_html;
780 my $href = $v->{href};
781 "<<a\nhref=\"../$href/\">$html</a>>";
786 'm' . id_compress($msgid, 1);
790 my ($upfx, $mid) = @_;
792 $mid = PublicInbox::Hval->new_msgid($mid);
793 my $href = $mid->{href};
794 my $html = $mid->as_html;
795 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
800 $level ? INDENT x ($level - 1) : '';
804 my ($srch, $sres) = @_;
805 my $msgs = delete $sres->{msgs};
806 $srch->retry_reopen(sub { [ map { $_->mid; $_ } @$msgs ] });
810 my ($msgs, $srch) = @_;
811 require PublicInbox::SearchThread;
812 PublicInbox::SearchThread::thread($msgs, *sort_ds, $srch);
817 require PublicInbox::ExtMsg;
818 PublicInbox::ExtMsg::ext_msg($ctx);
823 fmt_ts(msg_datestamp($hdr));
826 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
829 my ($prev_subj, $subj, $val) = @_;
831 my $omit = ''; # '"' denotes identical text omitted
832 my (@prev_pop, @curr_pop);
833 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
834 push(@prev_pop, pop(@$prev_subj));
835 push(@curr_pop, pop(@$subj));
838 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
839 if (scalar(@curr_pop) == 1) {
841 push @$prev_subj, @prev_pop;
842 push @$subj, @curr_pop;
848 my ($ctx, $level, $node) = @_;
849 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
851 my $dst = $ctx->{dst};
852 my $cur = $ctx->{cur};
853 my $mid = $smsg->{mid};
855 my $f = ascii_html($smsg->from_name);
856 my $obfs_ibx = $ctx->{-obfs_ibx};
857 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
859 my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
861 $ctx->{first_level} ||= $level;
863 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
864 $ctx->{prev_attr} = $attr;
866 $ctx->{prev_level} = $level;
871 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
872 "$attr [this message]</a></b>\n";
875 $ctx->{prev_msg} = $mid;
878 $ctx->{next_msg} ||= $mid;
881 # Subject is never undef, this mail was loaded from
882 # our Xapian which would've resulted in '' if it were
883 # really missing (and Filter rejects empty subjects)
884 my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject));
886 # remove common suffixes from the subject if it matches the previous,
887 # so we do not show redundant text at the end.
888 my $prev_subj = $ctx->{prev_subj} || [];
889 $ctx->{prev_subj} = [ @subj ];
890 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
893 my $subj = join(' ', @subj);
894 $subj = ascii_html($subj);
895 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
896 $end = "$subj</a> $omit$f\n"
902 my $mapping = $ctx->{mapping};
904 my $map = $mapping->{$mid};
905 $id = id_compress($mid, 1);
907 $map->[0] = "$d<a\nhref=\"$m\">$end";
910 $m = $ctx->{-upfx}.mid_escape($mid).'/';
912 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
916 my ($ctx, $level, $node) = @_;
918 my $mid = $node->{id};
919 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
921 $d .= indent_for($level) . th_pfx($level);
922 my $upfx = $ctx->{-upfx};
923 my $m = PublicInbox::Hval->new_msgid($mid);
924 my $href = $upfx . $m->{href} . '/';
925 my $html = $m->as_html;
927 my $mapping = $ctx->{mapping};
928 my $map = $mapping->{$mid} if $mapping;
930 my $id = id_compress($mid, 1);
931 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
932 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
934 $d .= qq{<<a\nhref="$href">$html</a>>\n};
936 my $dst = $ctx->{dst};
942 (eval { $a->topmost->{smsg}->ds } || 0) <=>
943 (eval { $b->topmost->{smsg}->ds } || 0)
947 # accumulate recent topics if search is supported
948 # returns 200 if done, 404 if not
950 my ($ctx, $level, $node) = @_;
951 my $srch = $ctx->{srch};
952 my $mid = $node->{id};
953 my $x = $node->{smsg} || $srch->lookup_mail($mid);
958 $subj = $srch->subject_normalized($subj);
961 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
962 $ctx->{-cur_topic} = $topic;
963 push @{$ctx->{order}}, $topic;
967 $topic = $ctx->{-cur_topic}; # should never be undef
968 $topic->[0] = $ds if $ds > $topic->[0];
970 my $seen = $topic->[2];
971 if (scalar(@$topic) == 3) { # parent was a ghost
973 } elsif (!$seen->{$subj}) {
974 push @$topic, $level, $subj;
976 $seen->{$subj} = $mid; # latest for subject
977 } else { # ghost message
978 return if $level != 0; # ignore child ghosts
979 $topic = [ -666, 0, {} ];
980 $ctx->{-cur_topic} = $topic;
981 push @{$ctx->{order}}, $topic;
987 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
989 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
994 my $ibx = $ctx->{-inbox};
995 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
996 my $srch = $ctx->{srch};
998 # sort by recency, this allows new posts to "bump" old topics...
999 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
1000 my ($ds, $n, $seen, $top, @ex) = @$topic;
1002 next unless defined $top; # ghost topic
1003 my $mid = delete $seen->{$top};
1004 my $href = mid_escape($mid);
1005 my $prev_subj = [ split(/ /, $top) ];
1006 $top = PublicInbox::Hval->new($top)->as_html;
1009 # $n isn't the total number of posts on the topic,
1010 # just the number of posts in the current results window
1014 $anchor = '#u'; # top of only message
1016 $n = " ($n+ messages)";
1017 $anchor = '#t'; # thread skeleton
1020 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1021 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1022 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
1023 " $ds UTC $n - $mbox / $atom\n";
1024 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1025 my $level = $ex[$i];
1026 my $subj = $ex[$i + 1];
1027 $mid = delete $seen->{$subj};
1028 my @subj = split(/ /, $srch->subject_normalized($subj));
1029 my @next_prev = @subj; # full copy
1030 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1031 $prev_subj = \@next_prev;
1032 $subj = ascii_html(join(' ', @subj));
1033 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1034 $href = mid_escape($mid);
1035 $s .= indent_for($level) . TCHILD;
1036 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1040 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1044 sub index_nav { # callback for WwwStream
1045 my (undef, $ctx) = @_;
1046 delete $ctx->{qp} or return;
1048 $next = $prev = ' ';
1051 my $next_o = $ctx->{-next_o};
1053 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
1055 if (my $cur_o = $ctx->{-cur_o}) {
1056 $latest = qq! <a\nhref=.>latest</a>!;
1058 my $o = $cur_o - ($next_o - $cur_o);
1060 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
1062 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
1065 "<hr><pre>page: $next $prev$latest</pre>";
1070 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
1071 my $opts = { offset => $off, limit => 200 };
1074 my $srch = $ctx->{srch};
1075 my $sres = $srch->query('', $opts);
1076 my $nr = scalar @{$sres->{msgs}};
1078 $sres = load_results($srch, $sres);
1079 walk_thread(thread_results($sres, $srch), $ctx, *acc_topic);
1081 $ctx->{-next_o} = $off+ $nr;
1082 $ctx->{-cur_o} = $off;
1083 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1086 sub thread_adj_level {
1087 my ($ctx, $level) = @_;
1089 my $max = $ctx->{cur_level};
1091 return ('', '') if $max == 0; # flat output
1093 # reset existing lists
1094 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1095 $ctx->{cur_level} = 0;
1097 } elsif ($level == $max) { # continue existing list
1099 } elsif ($level < $max) {
1100 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1101 $ctx->{cur_level} = $level;
1102 ("$beg<li>", '</li>');
1103 } else { # ($level > $max) # start a new level
1104 $ctx->{cur_level} = $level;
1105 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1110 sub ghost_index_entry {
1111 my ($ctx, $level, $node) = @_;
1112 my ($beg, $end) = thread_adj_level($ctx, $level);
1113 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})