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/id_compress mid_escape mids references/;
13 use PublicInbox::MsgIter;
14 use PublicInbox::Address;
15 use PublicInbox::WwwStream;
16 use PublicInbox::Reply;
18 use Time::Local qw(timegm);
20 use constant COLS => 72;
21 use constant INDENT => ' ';
22 use constant TCHILD => '` ';
23 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
25 # public functions: (unstable)
28 my ($ctx, $mime, $more, $smsg) = @_;
29 my $hdr = $mime->header_obj;
30 my $ibx = $ctx->{-inbox};
31 my $obfs_ibx = $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
32 my $tip = _msg_html_prepare($hdr, $ctx, $more, 0);
34 PublicInbox::WwwStream->response($ctx, 200, sub {
37 # $more cannot be true w/o $smsg being defined:
38 my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : '';
39 $tip . multipart_text_as_html($mime, $upfx, $obfs_ibx) .
41 } elsif ($more && @$more) {
43 msg_html_more($ctx, $more, $nr);
44 } elsif ($nr == $end) {
45 # fake an EOF if generating the footer fails;
46 # we want to at least show the message if something
49 '<pre>' . html_footer($hdr, 1, $ctx) .
50 '</pre>' . msg_reply($ctx, $hdr)
60 my $mid = $ctx->{mid};
61 my $ibx = $ctx->{-inbox};
64 if (my $srch = $ibx->search) {
66 $smsg = $srch->next_by_mid($mid, \$id, \$prev);
67 $first = $ibx->msg_by_smsg($smsg) if $smsg;
69 my $next = $srch->next_by_mid($mid, \$id, \$prev);
70 $more = [ $id, $prev, $next ] if $next;
74 $first = $ibx->msg_by_mid($mid) or return;
76 msg_html($ctx, PublicInbox::MIME->new($first), $more, $smsg);
80 my ($ctx, $more, $nr) = @_;
82 my ($id, $prev, $smsg) = @$more;
83 my $mid = $ctx->{mid};
84 $smsg = $ctx->{-inbox}->smsg_mime($smsg);
85 my $next = $ctx->{srch}->next_by_mid($mid, \$id, \$prev);
86 @$more = $next ? ($id, $prev, $next) : ();
88 my $mime = $smsg->{mime};
89 my $upfx = '../' . mid_escape($smsg->mid) . '/';
90 _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
91 multipart_text_as_html($mime, $upfx,
99 warn "Error lookup up additional messages: $@\n";
100 $str = '<pre>Error looking up additional messages</pre>';
105 # /$INBOX/$MESSAGE_ID/#R
107 my ($ctx, $hdr) = @_;
109 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
111 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
114 my $ibx = $ctx->{-inbox};
115 if (my $url = $ibx->{infourl}) {
116 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
117 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
120 my ($arg, $link, $reply_to_all) =
121 PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
122 if (ref($arg) eq 'SCALAR') {
123 return '<pre id=R>'.ascii_html($$arg).'</pre>';
126 # mailto: link only works if address obfuscation is disabled
130 * If your mail client supports setting the <b>In-Reply-To</b> header
131 via mailto: links, try the <a
132 href="$link">mailto: link</a>
136 push @$arg, '/path/to/YOUR_REPLY';
137 $arg = ascii_html(join(" \\\n ", '', @$arg));
140 id=R><b>Reply instructions:</b>
142 You may reply publically to <a
143 href=#t>this message</a> via plain-text email
144 using any one of the following methods:
146 * Save the following mbox file, import it into your mail client,
147 and $reply_to_all from there: <a
150 Avoid top-posting and favor interleaved quoting:
152 href="$p_url">$p_url</a>
154 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
155 switches of git-send-email(1):
160 href="$se_url">$se_url</a>
167 my $refs = references($hdr);
171 sub fold_addresses ($) {
172 return $_[0] if length($_[0]) <= COLS;
173 # try to fold on commas after non-word chars before $lim chars,
174 # Try to get the "," preceeded by ">" or ")", but avoid folding
175 # on the comma where somebody uses "Lastname, Firstname".
176 # We also try to keep the last and penultimate addresses in
177 # the list on the same line if possible, hence the extra \z
178 # Fall back to folding on spaces at $lim + 1 chars
179 my $lim = COLS - 8; # 8 = "\t" display width
180 my $too_long = $lim + 1;
181 $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
183 ($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
186 .{$too_long,}?)(?:\s|\z)/xgo));
189 sub _hdr_names_html ($$) {
190 my ($hdr, $field) = @_;
191 my $val = $hdr->header($field) or return '';
192 ascii_html(join(', ', PublicInbox::Address::names($val)));
196 my ($nr, $singular, $plural) = @_;
197 return "0 $plural" if $nr == 0;
198 $nr == 1 ? "$nr $singular" : "$nr $plural";
201 # this is already inside a <pre>
203 my ($smsg, $ctx, $more) = @_;
204 my $srch = $ctx->{srch};
205 my $subj = $smsg->subject;
206 my $mid_raw = $smsg->mid;
207 my $id = id_compress($mid_raw, 1);
210 my $root_anchor = $ctx->{root_anchor} || '';
212 my $obfs_ibx = $ctx->{-obfs_ibx};
214 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
215 $subj = '<b>'.ascii_html($subj).'</b>';
216 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
217 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
219 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
221 my $ds = $smsg->ds; # for v1 non-Xapian/SQLite users
222 my $mime = delete $smsg->{mime}; # critical to memory use
223 my $hdr = $mime->header_obj;
224 my $from = _hdr_names_html($hdr, 'From');
225 obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
226 $rv .= "From: $from @ ".fmt_ts($ds)." UTC";
227 my $upfx = $ctx->{-upfx};
228 my $mhref = $upfx . mid_escape($mid_raw) . '/';
229 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
230 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
231 my $to = fold_addresses(_hdr_names_html($hdr, 'To'));
232 my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc'));
233 my ($tlen, $clen) = (length($to), length($cc));
235 if (($tlen + $clen) > COLS) {
236 $to_cc .= ' To: '.$to."\n" if $tlen;
237 $to_cc .= ' Cc: '.$cc."\n" if $clen;
240 $to_cc .= ' To: '.$to;
241 $to_cc .= '; <b>+Cc:</b> '.$cc if $clen;
243 $to_cc .= ' Cc: '.$cc if $clen;
247 obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx;
250 my $mapping = $ctx->{mapping};
251 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
252 my $mirt = PublicInbox::Hval->new_msgid($irt);
253 my $href = $upfx . $mirt->{href}. '/';
254 my $html = $mirt->as_html;
255 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
259 # scan through all parts, looking for displayable text
260 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) });
263 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
264 "<a\nhref=\"$mhref\">permalink</a>" .
265 " <a\nhref=\"${mhref}raw\">raw</a>" .
266 " <a\nhref=\"${mhref}#R\">reply</a>";
269 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
270 $rv .= "\t[relevance $pct->{$mid_raw}%]";
273 my $nested = 'nested';
278 $flat = "<b>$flat</b>";
280 $nested = "<b>$nested</b>";
282 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
283 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
284 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
289 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
293 sub pad_link ($$;$) {
294 my ($mid, $level, $s) = @_;
296 my $id = id_compress($mid, 1);
297 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
301 my ($mid_raw, $irt, $id, $ctx) = @_;
303 my $mapping = $ctx->{mapping} or return $rv;
305 my $mid_map = $mapping->{$mid_raw};
307 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
308 my ($attr, $node, $idx, $level) = @$mid_map;
309 my $children = $node->{children};
310 my $nr_c = scalar @$children;
313 if (my $smsg = $node->{smsg}) {
314 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
316 my $irt_map = $mapping->{$$irt} if defined $$irt;
317 if (defined $irt_map) {
318 $siblings = $irt_map->[1]->{children};
319 $nr_s = scalar(@$siblings) - 1;
320 $rv .= $pad . $irt_map->[0];
322 my $prev = $siblings->[$idx - 1];
323 my $pmid = $prev->{id};
325 my $s = ($idx - 1). ' preceding siblings ...';
326 $rv .= pad_link($pmid, $level, $s);
327 } elsif ($idx == 2) {
328 my $ppmid = $siblings->[0]->{id};
329 $rv .= $pad . $mapping->{$ppmid}->[0];
331 $rv .= $pad . $mapping->{$pmid}->[0];
334 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
335 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
336 $attr =~ s!\n\z!</b>\n!s;
337 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
338 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
341 my $cmid = $children->[0]->{id};
342 $rv .= $pad . $mapping->{$cmid}->[0];
344 my $s = ($nr_c - 1). ' more replies';
345 $rv .= pad_link($cmid, $level + 1, $s);
346 } elsif (my $cn = $children->[1]) {
347 $rv .= $pad . $mapping->{$cn->{id}}->[0];
351 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
353 my $nmid = $next->{id};
354 $rv .= $pad . $mapping->{$nmid}->[0];
355 my $nnext = $nr_s - $idx;
357 my $s = ($nnext - 1).' subsequent siblings';
358 $rv .= pad_link($nmid, $level, $s);
359 } elsif (my $nn = $siblings->[$idx + 2]) {
360 $rv .= $pad . $mapping->{$nn->{id}}->[0];
363 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
367 my ($rootset, $ctx, $cb) = @_;
368 my @q = map { (0, $_, -1) } @$rootset;
370 my ($level, $node, $i) = splice(@q, 0, 3);
371 defined $node or next;
372 $cb->($ctx, $level, $node, $i) or return;
375 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
380 my ($ctx, $level, $node, $idx) = @_;
381 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
382 skel_dump($ctx, $level, $node);
385 sub thread_index_entry {
386 my ($ctx, $level, $smsg) = @_;
387 my ($beg, $end) = thread_adj_level($ctx, $level);
388 $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
391 sub stream_thread ($$) {
392 my ($rootset, $ctx) = @_;
393 my $inbox = $ctx->{-inbox};
394 my @q = map { (0, $_) } @$rootset;
399 my $node = shift @q or next;
401 unshift @q, map { ($cl, $_) } @{$node->{children}};
402 $smsg = $inbox->smsg_mime($node->{smsg}) and last;
404 return missing_thread($ctx) unless $smsg;
406 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
407 $ctx->{-title_html} = ascii_html($smsg->subject);
408 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
410 PublicInbox::WwwStream->response($ctx, 200, sub {
414 my $node = shift @q or next;
416 unshift @q, map { ($cl, $_) } @{$node->{children}};
417 if ($smsg = $inbox->smsg_mime($node->{smsg})) {
418 return thread_index_entry($ctx, $level, $smsg);
420 return ghost_index_entry($ctx, $level, $node);
423 my $ret = join('', thread_adj_level($ctx, 0));
424 $ret .= ${$ctx->{dst}}; # skel
432 my $mid = $ctx->{mid};
433 my $srch = $ctx->{srch};
434 my ($nr, $msgs) = $srch->get_thread($mid);
435 return missing_thread($ctx) if $nr == 0;
436 my $skel = '<hr><pre>';
437 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
438 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
439 $skel .= "<b\nid=t>Thread overview:</b> ";
440 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
441 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
442 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
443 $skel .= "-- links below jump to the message on this page --\n";
444 $ctx->{-upfx} = '../../';
445 $ctx->{cur_level} = 0;
446 $ctx->{dst} = \$skel;
447 $ctx->{prev_attr} = '';
448 $ctx->{prev_level} = 0;
449 $ctx->{root_anchor} = anchor_for($mid);
450 $ctx->{mapping} = {};
451 $ctx->{s_nr} = "$nr+ messages in thread";
453 my $rootset = thread_results($ctx, $msgs);
455 # reduce hash lookups in pre_thread->skel_dump
456 my $inbox = $ctx->{-inbox};
457 $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
458 walk_thread($rootset, $ctx, *pre_thread);
461 return stream_thread($rootset, $ctx) unless $ctx->{flat};
463 # flat display: lazy load the full message from smsg
465 while (my $m = shift @$msgs) {
466 $smsg = $inbox->smsg_mime($m) and last;
468 return missing_thread($ctx) unless $smsg;
469 $ctx->{-title_html} = ascii_html($smsg->subject);
470 $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
472 PublicInbox::WwwStream->response($ctx, 200, sub {
475 while (my $m = shift @$msgs) {
476 $smsg = $inbox->smsg_mime($m) and last;
478 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
484 sub multipart_text_as_html {
485 my ($mime, $upfx, $obfs_ibx) = @_;
488 # scan through all parts, looking for displayable text
489 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs_ibx, $_[0]) });
494 my ($s, $l, $quot) = @_;
496 # show everything in the full version with anchor from
497 # short version (see above)
498 my $rv = $l->linkify_1(join('', @$quot));
501 # we use a <span> here to allow users to specify their own
502 # color for quoted text
503 $rv = $l->linkify_2(ascii_html($rv));
504 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
507 sub attach_link ($$$$;$) {
508 my ($upfx, $ct, $p, $fn, $err) = @_;
509 my ($part, $depth, @idx) = @$p;
510 my $nl = $idx[-1] > 1 ? "\n" : '';
511 my $idx = join('.', @idx);
512 my $size = bytes::length($part->body);
514 # hide attributes normally, unless we want to aid users in
515 # spotting MUA problems:
516 $ct =~ s/;.*// unless $err;
517 $ct = ascii_html($ct);
518 my $desc = $part->header('Content-Description');
519 $desc = $fn unless defined $desc;
520 $desc = '' unless defined $desc;
522 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
524 } elsif ($ct eq 'text/plain') {
529 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
532 "[-- Warning: decoded text below may be mangled --]\n";
534 $ret .= "[-- Attachment #$idx: ";
535 my $ts = "Type: $ct, Size: $size bytes";
536 $desc = ascii_html($desc);
537 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
542 my ($upfx, $obfs_ibx, $p) = @_;
543 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
544 my ($part, $depth) = @$p; # attachment @idx is unused
545 my $ct = $part->content_type || 'text/plain';
546 my $fn = $part->filename;
547 my ($s, $err) = msg_part_text($part, $ct);
549 return attach_link($upfx, $ct, $p, $fn) unless defined $s;
551 my @lines = split(/^/m, $s);
553 if (defined($fn) || $depth > 0 || $err) {
554 # badly-encoded message with $err? tell the world about it!
555 $s .= attach_link($upfx, $ct, $p, $fn, $err);
559 my $l = PublicInbox::Linkify->new;
560 foreach my $cur (@lines) {
562 # show the previously buffered quote inline
563 flush_quote(\$s, $l, \@quot) if @quot;
567 $s .= $l->linkify_2(ascii_html($cur));
573 if (@quot) { # ugh, top posted
574 flush_quote(\$s, $l, \@quot);
575 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
578 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
579 if ($s =~ /\n\z/s) { # common, last line ends with a newline
581 } else { # some editors don't do newlines...
587 sub _msg_html_prepare {
588 my ($hdr, $ctx, $more, $nr) = @_;
589 my $srch = $ctx->{srch} if $ctx;
591 my $obfs_ibx = $ctx->{-obfs_ibx};
593 my $mids = mids($hdr);
594 my $multiple = scalar(@$mids) > 1; # zero, one, infinity
598 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
600 $rv .= "<pre\nid=b>"; # anchor for body start
605 $ctx->{-upfx} = '../';
609 if (defined($v = $hdr->header('From'))) {
610 $v = PublicInbox::Hval->new($v);
611 my @n = PublicInbox::Address::names($v->raw);
612 $title[1] = ascii_html(join(', ', @n));
615 obfuscate_addrs($obfs_ibx, $v);
616 obfuscate_addrs($obfs_ibx, $title[1]);
618 $rv .= "From: $v\n" if $v ne '';
620 foreach my $h (qw(To Cc)) {
621 defined($v = $hdr->header($h)) or next;
624 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
625 $rv .= "$h: $v\n" if $v ne '';
627 if (defined($v = $hdr->header('Subject')) && ($v ne '')) {
629 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
631 $rv .= qq(Subject: <a\nhref="#r"\nid=t>$v</a>\n);
633 $rv .= "Subject: $v\n";
636 } else { # dummy anchor for thread skeleton at bottom of page
637 $rv .= qq(<a\nhref="#r"\nid=t></a>) if $srch;
638 $title[0] = '(no subject)';
640 if (defined($v = $hdr->header('Date'))) {
642 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
645 $ctx->{-title_html} = join(' - ', @title);
647 my $mid = PublicInbox::Hval->new_msgid($_) ;
648 my $mhtml = $mid->as_html;
650 my $href = $mid->{href};
651 $rv .= "Message-ID: ";
652 $rv .= "<a\nhref=\"../$href/\">";
653 $rv .= "<$mhtml></a> ";
654 $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
656 $rv .= "Message-ID: <$mhtml> ";
657 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
660 $rv .= _parent_headers($hdr, $srch);
665 my ($dst, $ctx, $hdr, $tpfx) = @_;
666 my $srch = $ctx->{srch};
667 my $mid = mids($hdr)->[0];
668 my ($nr, $msgs) = $srch->get_thread($mid);
669 my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
670 qq(|<a\nhref="${tpfx}t/#u">nested</a>] ) .
671 qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
672 qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
674 my $parent = in_reply_to($hdr);
675 $$dst .= "\n<b>Thread overview: </b>";
677 if (defined $parent) {
678 $$dst .= "$expand\n ";
679 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
681 $$dst .= "[no followups] $expand\n";
683 $ctx->{next_msg} = undef;
684 $ctx->{parent_msg} = $parent;
688 $$dst .= "$nr+ messages / $expand";
689 $$dst .= qq! <a\nhref="#b">top</a>\n!;
691 my $subj = $hdr->header('Subject');
692 defined $subj or $subj = '';
693 $ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
695 $ctx->{prev_attr} = '';
696 $ctx->{prev_level} = 0;
699 # reduce hash lookups in skel_dump
700 my $ibx = $ctx->{-inbox};
701 $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
702 walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
704 $ctx->{parent_msg} = $parent;
707 sub _parent_headers {
708 my ($hdr, $srch) = @_;
711 my $refs = references($hdr);
712 my $irt = pop @$refs;
714 my $v = PublicInbox::Hval->new_msgid($irt);
715 my $html = $v->as_html;
716 my $href = $v->{href};
717 $rv .= "In-Reply-To: <";
718 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
721 # do not display References: if search is present,
722 # we show the thread skeleton at the bottom, instead.
726 @$refs = map { linkify_ref_nosrch($_) } @$refs;
727 $rv .= 'References: '. join("\n\t", @$refs) . "\n";
733 my ($hdr, $standalone, $ctx, $rhref) = @_;
735 my $srch = $ctx->{srch} if $ctx;
738 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
742 thread_skel(\$idx, $ctx, $hdr, $tpfx);
747 if (my $n = $ctx->{next_msg}) {
748 $n = PublicInbox::Hval->new_msgid($n)->{href};
749 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
752 my $par = $ctx->{parent_msg};
754 $u = PublicInbox::Hval->new_msgid($par)->{href};
757 if (my $p = $ctx->{prev_msg}) {
758 $prev = PublicInbox::Hval->new_msgid($p)->{href};
759 if ($p && $par && $p eq $par) {
760 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
761 'rel=prev>prev parent</a>';
764 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
766 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
768 } elsif ($u) { # unlikely
769 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
771 $irt = "$next $prev$parent ";
776 $irt .= qq(<a\nhref="$rhref">reply</a>);
780 sub linkify_ref_nosrch {
781 my $v = PublicInbox::Hval->new_msgid($_[0]);
782 my $html = $v->as_html;
783 my $href = $v->{href};
784 "<<a\nhref=\"../$href/\">$html</a>>";
789 'm' . id_compress($msgid, 1);
793 my ($upfx, $mid) = @_;
795 $mid = PublicInbox::Hval->new_msgid($mid);
796 my $href = $mid->{href};
797 my $html = $mid->as_html;
798 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
803 $level ? INDENT x ($level - 1) : '';
807 my ($ctx, $level, $node, $idx) = @_;
808 ++$ctx->{root_idx} if $level == 0;
809 if ($node->{id} eq $ctx->{mid}) {
810 $ctx->{found_mid_at} = $ctx->{root_idx};
816 sub strict_loose_note ($) {
819 " -- strict thread matches above, loose matches on Subject: below --\n";
821 if ($nr > PublicInbox::Over::DEFAULT_LIMIT()) {
823 " -- use mbox.gz link to download all $nr messages --\n";
829 my ($ctx, $msgs) = @_;
830 require PublicInbox::SearchThread;
831 my $ibx = $ctx->{-inbox};
832 my $rootset = PublicInbox::SearchThread::thread($msgs, *sort_ds, $ibx);
834 # FIXME: `tid' is broken on --reindex, so that needs to be fixed
835 # and preserved in the future. This bug is hidden by `sid' matches
836 # in get_thread, so we never noticed it until now. And even when
837 # reindexing is fixed, we'll keep this code until a SCHEMA_VERSION
838 # bump since reindexing is expensive and users may not do it
840 # loose threading could've returned too many results,
841 # put the root the message we care about at the top:
842 my $mid = $ctx->{mid};
843 if (defined($mid) && scalar(@$rootset) > 1) {
844 $ctx->{root_idx} = -1;
845 my $nr = scalar @$msgs;
846 walk_thread($rootset, $ctx, *find_mid_root);
847 my $idx = $ctx->{found_mid_at};
848 if (defined($idx) && $idx != 0) {
849 my $tip = splice(@$rootset, $idx, 1);
850 @$rootset = reverse @$rootset;
851 unshift @$rootset, $tip;
852 $ctx->{sl_note} = strict_loose_note($nr);
860 require PublicInbox::ExtMsg;
861 PublicInbox::ExtMsg::ext_msg($ctx);
866 fmt_ts(msg_datestamp($hdr));
869 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
872 my ($prev_subj, $subj, $val) = @_;
874 my $omit = ''; # '"' denotes identical text omitted
875 my (@prev_pop, @curr_pop);
876 while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
877 push(@prev_pop, pop(@$prev_subj));
878 push(@curr_pop, pop(@$subj));
881 pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
882 if (scalar(@curr_pop) == 1) {
884 push @$prev_subj, @prev_pop;
885 push @$subj, @curr_pop;
891 my ($ctx, $level, $node) = @_;
892 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
894 my $dst = $ctx->{dst};
895 my $cur = $ctx->{cur};
896 my $mid = $smsg->{mid};
898 if ($level == 0 && $ctx->{skel_dump_roots}++) {
899 $$dst .= delete $ctx->{sl_note} || '';
902 my $f = ascii_html($smsg->from_name);
903 my $obfs_ibx = $ctx->{-obfs_ibx};
904 obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
906 my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
908 $ctx->{first_level} ||= $level;
910 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
911 $ctx->{prev_attr} = $attr;
913 $ctx->{prev_level} = $level;
918 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
919 "$attr [this message]</a></b>\n";
922 $ctx->{prev_msg} = $mid;
925 $ctx->{next_msg} ||= $mid;
928 # Subject is never undef, this mail was loaded from
929 # our Xapian which would've resulted in '' if it were
930 # really missing (and Filter rejects empty subjects)
931 my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject));
933 # remove common suffixes from the subject if it matches the previous,
934 # so we do not show redundant text at the end.
935 my $prev_subj = $ctx->{prev_subj} || [];
936 $ctx->{prev_subj} = [ @subj ];
937 my $omit = dedupe_subject($prev_subj, \@subj, '" ');
940 my $subj = join(' ', @subj);
941 $subj = ascii_html($subj);
942 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
943 $end = "$subj</a> $omit$f\n"
949 my $mapping = $ctx->{mapping};
951 my $map = $mapping->{$mid};
952 $id = id_compress($mid, 1);
954 $map->[0] = "$d<a\nhref=\"$m\">$end";
957 $m = $ctx->{-upfx}.mid_escape($mid).'/';
959 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
964 my ($ctx, $level, $node) = @_;
966 my $mid = $node->{id};
967 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
969 $d .= indent_for($level) . th_pfx($level);
970 my $upfx = $ctx->{-upfx};
971 my $m = PublicInbox::Hval->new_msgid($mid);
972 my $href = $upfx . $m->{href} . '/';
973 my $html = $m->as_html;
975 my $mapping = $ctx->{mapping};
976 my $map = $mapping->{$mid} if $mapping;
978 my $id = id_compress($mid, 1);
979 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
980 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
982 $d .= qq{<<a\nhref="$href">$html</a>>\n};
984 my $dst = $ctx->{dst};
991 (eval { $a->topmost->{smsg}->ds } || 0) <=>
992 (eval { $b->topmost->{smsg}->ds } || 0)
996 # accumulate recent topics if search is supported
997 # returns 200 if done, 404 if not
999 my ($ctx, $level, $node) = @_;
1000 my $srch = $ctx->{srch};
1001 my $mid = $node->{id};
1002 my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
1006 $subj = $x->subject;
1007 $subj = $srch->subject_normalized($subj);
1010 $topic = [ $ds, 1, { $subj => $mid }, $subj ];
1011 $ctx->{-cur_topic} = $topic;
1012 push @{$ctx->{order}}, $topic;
1016 $topic = $ctx->{-cur_topic}; # should never be undef
1017 $topic->[0] = $ds if $ds > $topic->[0];
1019 my $seen = $topic->[2];
1020 if (scalar(@$topic) == 3) { # parent was a ghost
1021 push @$topic, $subj;
1022 } elsif (!$seen->{$subj}) {
1023 push @$topic, $level, $subj;
1025 $seen->{$subj} = $mid; # latest for subject
1026 } else { # ghost message
1027 return 1 if $level != 0; # ignore child ghosts
1028 $topic = [ -666, 0, {} ];
1029 $ctx->{-cur_topic} = $topic;
1030 push @{$ctx->{order}}, $topic;
1037 my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
1039 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
1044 my $ibx = $ctx->{-inbox};
1045 my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
1046 my $srch = $ctx->{srch};
1048 # sort by recency, this allows new posts to "bump" old topics...
1049 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
1050 my ($ds, $n, $seen, $top, @ex) = @$topic;
1052 next unless defined $top; # ghost topic
1053 my $mid = delete $seen->{$top};
1054 my $href = mid_escape($mid);
1055 my $prev_subj = [ split(/ /, $top) ];
1056 $top = PublicInbox::Hval->new($top)->as_html;
1059 # $n isn't the total number of posts on the topic,
1060 # just the number of posts in the current results window
1064 $anchor = '#u'; # top of only message
1066 $n = " ($n+ messages)";
1067 $anchor = '#t'; # thread skeleton
1070 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1071 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1072 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
1073 " $ds UTC $n - $mbox / $atom\n";
1074 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1075 my $level = $ex[$i];
1076 my $subj = $ex[$i + 1];
1077 $mid = delete $seen->{$subj};
1078 my @subj = split(/ /, $srch->subject_normalized($subj));
1079 my @next_prev = @subj; # full copy
1080 my $omit = dedupe_subject($prev_subj, \@subj, ' "');
1081 $prev_subj = \@next_prev;
1082 $subj = ascii_html(join(' ', @subj));
1083 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1084 $href = mid_escape($mid);
1085 $s .= indent_for($level) . TCHILD;
1086 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1090 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1096 POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
1100 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
1101 timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
1104 sub pagination_footer ($$) {
1105 my ($ctx, $latest) = @_;
1106 delete $ctx->{qp} or return;
1107 my $next = $ctx->{next_page} || '';
1108 my $prev = $ctx->{prev_page} || '';
1110 $next = $next ? "$next " : ' ';
1111 $prev .= qq! <a\nhref='$latest'>latest</a>!;
1113 "<hr><pre>page: $next$prev</pre>";
1116 sub index_nav { # callback for WwwStream
1117 my (undef, $ctx) = @_;
1118 pagination_footer($ctx, '.')
1121 sub paginate_recent ($$) {
1122 my ($ctx, $lim) = @_;
1123 my $t = $ctx->{qp}->{t} || '';
1124 my $opts = { limit => $lim };
1125 my ($after, $before);
1127 # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers
1128 # if only $after exists "YYYYMMDD.." because "." could be skipped
1129 # if interpreted as an end-of-sentence
1130 $t =~ s/\A(\d{8,14})-// and $after = str2ts($1);
1131 $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1);
1133 my $ibx = $ctx->{-inbox};
1134 my $msgs = $ibx->recent($opts, $after, $before);
1135 my $nr = scalar @$msgs;
1136 if ($nr < $lim && defined($after)) {
1137 $after = $before = undef;
1138 $msgs = $ibx->recent($opts);
1139 $nr = scalar @$msgs;
1141 my $more = $nr == $lim;
1142 my ($newest, $oldest);
1144 $newest = $msgs->[0]->{ts};
1145 $oldest = $msgs->[-1]->{ts};
1146 # if we only had $after, our SQL query in ->recent ordered
1147 if ($newest < $oldest) {
1148 ($oldest, $newest) = ($newest, $oldest);
1149 $more = 0 if defined($after) && $after < $oldest;
1152 if (defined($oldest) && $more) {
1153 my $s = ts2str($oldest);
1154 $ctx->{next_page} = qq!<a\nhref="?t=$s"\nrel=next>next</a>!;
1156 if (defined($newest) && (defined($before) || defined($after))) {
1157 my $s = ts2str($newest);
1158 $ctx->{prev_page} = qq!<a\nhref="?t=$s-"\nrel=prev>prev</a>!;
1165 my $msgs = paginate_recent($ctx, 200); # 200 is our window
1167 walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
1169 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1172 sub thread_adj_level {
1173 my ($ctx, $level) = @_;
1175 my $max = $ctx->{cur_level};
1177 return ('', '') if $max == 0; # flat output
1179 # reset existing lists
1180 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1181 $ctx->{cur_level} = 0;
1183 } elsif ($level == $max) { # continue existing list
1185 } elsif ($level < $max) {
1186 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1187 $ctx->{cur_level} = $level;
1188 ("$beg<li>", '</li>');
1189 } else { # ($level > $max) # start a new level
1190 $ctx->{cur_level} = $level;
1191 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1196 sub ghost_index_entry {
1197 my ($ctx, $level, $node) = @_;
1198 my ($beg, $end) = thread_adj_level($ctx, $level);
1199 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})