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 $obfs = $ctx->{-inbox}->{obfuscate};
28 my $tip = _msg_html_prepare($hdr, $ctx, $obfs);
29 PublicInbox::WwwStream->response($ctx, 200, sub {
32 $tip . multipart_text_as_html($mime, '', $obfs) .
35 # fake an EOF if generating the footer fails;
36 # we want to at least show the message if something
39 '<pre>' . html_footer($hdr, 1, $ctx) .
40 '</pre>' . msg_reply($ctx, $hdr)
48 # /$INBOX/$MESSAGE_ID/#R
52 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
54 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
57 my $ibx = $ctx->{-inbox};
58 if (my $url = $ibx->{infourl}) {
59 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
60 $info = qq(\n List information: <a\nhref="$url">$url</a>\n);
63 my ($arg, $link) = PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
64 push @$arg, '/path/to/YOUR_REPLY';
65 $arg = ascii_html(join(" \\\n ", '', @$arg));
68 id=R><b>Reply instructions:</b>
70 You may reply publically to <a
71 href=#t>this message</a> via plain-text email
72 using any one of the following methods:
74 * Save the following mbox file, import it into your mail client,
75 and reply-to-all from there: <a
78 Avoid top-posting and favor interleaved quoting:
80 href="$p_url">$p_url</a>
82 * Reply to all the recipients using the <b>--to</b>, <b>--cc</b>,
83 and <b>--in-reply-to</b> switches of git-send-email(1):
88 href="$se_url">$se_url</a>
90 * If your mail client supports setting the <b>In-Reply-To</b> header
91 via mailto: links, try the <a
92 href="$link">mailto: link</a></pre>
98 my %mid = map { $_ => 1 } $hdr->header_raw('Message-ID');
99 my @refs = ($hdr->header_raw('References'),
100 $hdr->header_raw('In-Reply-To'));
101 @refs = ((join(' ', @refs)) =~ /<([^>]+)>/g);
102 while (defined(my $irt = pop @refs)) {
103 next if $mid{"<$irt>"};
109 sub _hdr_names_html ($$) {
110 my ($hdr, $field) = @_;
111 my $val = $hdr->header($field) or return '';
112 ascii_html(join(', ', PublicInbox::Address::names($val)));
116 my ($nr, $singular, $plural) = @_;
117 return "0 $plural" if $nr == 0;
118 $nr == 1 ? "$nr $singular" : "$nr $plural";
121 # this is already inside a <pre>
123 my ($mime, $ctx, $more) = @_;
124 my $srch = $ctx->{srch};
125 my $hdr = $mime->header_obj;
126 my $subj = $hdr->header('Subject');
128 my $mid_raw = mid_clean(mid_mime($mime));
129 my $id = id_compress($mid_raw, 1);
132 my $root_anchor = $ctx->{root_anchor} || '';
134 my $obfs = $ctx->{-obfuscate};
136 my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
137 $subj = '<b>'.ascii_html($subj).'</b>';
138 obfuscate_addrs($subj) if $obfs;
139 $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
141 $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
143 foreach my $f (qw(To Cc)) {
144 my $dst = _hdr_names_html($hdr, $f);
146 obfuscate_addrs($dst) if $obfs;
147 push @tocc, "$f: $dst";
150 my $from = _hdr_names_html($hdr, 'From');
151 obfuscate_addrs($from) if $obfs;
152 $rv .= "From: $from @ "._msg_date($hdr)." UTC";
153 my $upfx = $ctx->{-upfx};
154 my $mhref = $upfx . mid_escape($mid_raw) . '/';
155 $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
156 $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
157 $rv .= ' '.join('; +', @tocc) . "\n" if @tocc;
159 my $mapping = $ctx->{mapping};
160 if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
161 my $mirt = PublicInbox::Hval->new_msgid($irt);
162 my $href = $upfx . $mirt->{href}. '/';
163 my $html = $mirt->as_html;
164 $rv .= qq(In-Reply-To: <<a\nhref="$href">$html</a>>\n)
168 # scan through all parts, looking for displayable text
169 msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs, $_[0]) });
172 $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
173 "<a\nhref=\"$mhref\">permalink</a>" .
174 " <a\nhref=\"${mhref}raw\">raw</a>" .
175 " <a\nhref=\"${mhref}#R\">reply</a>";
178 if (my $pct = $ctx->{pct}) { # used by SearchView.pm
179 $rv .= "\t[relevance $pct->{$mid_raw}%]";
182 my $threaded = 'threaded';
187 $flat = "<b>$flat</b>";
189 $threaded = "<b>$threaded</b>";
191 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
192 $rv .= "|<a\nhref=\"${mhref}t/#u\">$threaded</a>]";
193 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
198 $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
202 sub pad_link ($$;$) {
203 my ($mid, $level, $s) = @_;
205 my $id = id_compress($mid, 1);
206 (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
210 my ($mid_raw, $irt, $id, $ctx) = @_;
212 my $mapping = $ctx->{mapping} or return $rv;
214 my $mid_map = $mapping->{$mid_raw};
216 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
217 my ($attr, $node, $idx, $level) = @$mid_map;
218 my $children = $node->{children};
219 my $nr_c = scalar @$children;
222 if (my $smsg = $node->{smsg}) {
223 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
225 my $irt_map = $mapping->{$$irt} if defined $$irt;
226 if (defined $irt_map) {
227 $siblings = $irt_map->[1]->{children};
228 $nr_s = scalar(@$siblings) - 1;
229 $rv .= $pad . $irt_map->[0];
231 my $prev = $siblings->[$idx - 1];
232 my $pmid = $prev->{id};
234 my $s = ($idx - 1). ' preceding siblings ...';
235 $rv .= pad_link($pmid, $level, $s);
236 } elsif ($idx == 2) {
237 my $ppmid = $siblings->[0]->{id};
238 $rv .= $pad . $mapping->{$ppmid}->[0];
240 $rv .= $pad . $mapping->{$pmid}->[0];
243 my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
244 my $s_c = nr_to_s($nr_c, 'reply', 'replies');
245 $attr =~ s!\n\z!</b>\n!s;
246 $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
247 $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
250 my $cmid = $children->[0]->{id};
251 $rv .= $pad . $mapping->{$cmid}->[0];
253 my $s = ($nr_c - 1). ' more replies';
254 $rv .= pad_link($cmid, $level + 1, $s);
255 } elsif (my $cn = $children->[1]) {
256 $rv .= $pad . $mapping->{$cn->{id}}->[0];
260 my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
262 my $nmid = $next->{id};
263 $rv .= $pad . $mapping->{$nmid}->[0];
264 my $nnext = $nr_s - $idx;
266 my $s = ($nnext - 1).' subsequent siblings';
267 $rv .= pad_link($nmid, $level, $s);
268 } elsif (my $nn = $siblings->[$idx + 2]) {
269 $rv .= $pad . $mapping->{$nn->{id}}->[0];
272 $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
276 my ($rootset, $ctx, $cb) = @_;
277 my @q = map { (0, $_, -1) } @$rootset;
279 my ($level, $node, $i) = splice(@q, 0, 3);
280 defined $node or next;
281 $cb->($ctx, $level, $node, $i);
284 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
289 my ($ctx, $level, $node, $idx) = @_;
290 $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
291 skel_dump($ctx, $level, $node);
294 sub thread_index_entry {
295 my ($ctx, $level, $mime) = @_;
296 my ($beg, $end) = thread_adj_level($ctx, $level);
297 $beg . '<pre>' . index_entry($mime, $ctx, 0) . '</pre>' . $end;
300 sub stream_thread ($$) {
301 my ($rootset, $ctx) = @_;
302 my $inbox = $ctx->{-inbox};
304 my @q = map { (0, $_) } @$rootset;
308 my $node = shift @q or next;
310 unshift @q, map { ($cl, $_) } @{$node->{children}};
311 $mime = $inbox->msg_by_smsg($node->{smsg}) and last;
313 return missing_thread($ctx) unless $mime;
315 $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
316 $mime = PublicInbox::MIME->new($mime);
317 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
318 $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime);
319 PublicInbox::WwwStream->response($ctx, 200, sub {
323 my $node = shift @q or next;
325 unshift @q, map { ($cl, $_) } @{$node->{children}};
326 my $mid = $node->{id};
327 if ($mime = $inbox->msg_by_smsg($node->{smsg})) {
328 $mime = PublicInbox::MIME->new($mime);
329 return thread_index_entry($ctx, $level, $mime);
331 return ghost_index_entry($ctx, $level, $node);
334 my $ret = join('', thread_adj_level($ctx, 0));
335 $ret .= ${$ctx->{dst}}; # skel
343 my $mid = $ctx->{mid};
344 my $srch = $ctx->{srch};
345 my $sres = $srch->get_thread($mid);
346 my $msgs = load_results($srch, $sres);
347 my $nr = $sres->{total};
348 return missing_thread($ctx) if $nr == 0;
349 my $skel = '<hr><pre>';
350 $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
351 $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
352 $skel .= "<b\nid=t>Thread overview:</b> ";
353 $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
354 $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
355 $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
356 $skel .= "-- links below jump to the message on this page --\n";
357 $ctx->{-upfx} = '../../';
358 $ctx->{cur_level} = 0;
359 $ctx->{dst} = \$skel;
360 $ctx->{prev_attr} = '';
361 $ctx->{prev_level} = 0;
362 $ctx->{root_anchor} = anchor_for($mid);
364 $ctx->{mapping} = {};
365 $ctx->{s_nr} = "$nr+ messages in thread";
367 my $rootset = thread_results($msgs);
369 # reduce hash lookups in pre_thread->skel_dump
370 $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
371 walk_thread($rootset, $ctx, *pre_thread);
374 return stream_thread($rootset, $ctx) unless $ctx->{flat};
376 # flat display: lazy load the full message from smsg
377 my $inbox = $ctx->{-inbox};
379 while ($mime = shift @$msgs) {
380 $mime = $inbox->msg_by_smsg($mime) and last;
382 return missing_thread($ctx) unless $mime;
383 $mime = PublicInbox::MIME->new($mime);
384 $ctx->{-title_html} = ascii_html($mime->header('Subject'));
385 $ctx->{-html_tip} = '<pre>'.index_entry($mime, $ctx, scalar @$msgs);
387 PublicInbox::WwwStream->response($ctx, 200, sub {
389 while ($mime = shift @$msgs) {
390 $mime = $inbox->msg_by_smsg($mime) and last;
393 $mime = PublicInbox::MIME->new($mime);
394 return index_entry($mime, $ctx, scalar @$msgs);
401 sub multipart_text_as_html {
402 my ($mime, $upfx, $obfs) = @_;
405 # scan through all parts, looking for displayable text
406 msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs, $_[0]) });
411 my ($s, $l, $quot) = @_;
413 # show everything in the full version with anchor from
414 # short version (see above)
415 my $rv = $l->linkify_1(join('', @$quot));
418 # we use a <span> here to allow users to specify their own
419 # color for quoted text
420 $rv = $l->linkify_2(ascii_html($rv));
421 $$s .= qq(<span\nclass="q">) . $rv . '</span>'
424 sub attach_link ($$$$;$) {
425 my ($upfx, $ct, $p, $fn, $err) = @_;
426 my ($part, $depth, @idx) = @$p;
427 my $nl = $idx[-1] > 1 ? "\n" : '';
428 my $idx = join('.', @idx);
429 my $size = bytes::length($part->body);
431 # hide attributes normally, unless we want to aid users in
432 # spotting MUA problems:
433 $ct =~ s/;.*// unless $err;
434 $ct = ascii_html($ct);
435 my $desc = $part->header('Content-Description');
436 $desc = $fn unless defined $desc;
437 $desc = '' unless defined $desc;
439 if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
441 } elsif ($ct eq 'text/plain') {
446 my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
449 "[-- Warning: decoded text below may be mangled --]\n";
451 $ret .= "[-- Attachment #$idx: ";
452 my $ts = "Type: $ct, Size: $size bytes";
453 $desc = ascii_html($desc);
454 $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
459 my ($upfx, $obfs, $p) = @_;
460 # $p - from msg_iter: [ Email::MIME, depth, @idx ]
461 my ($part, $depth) = @$p; # attachment @idx is unused
462 my $ct = $part->content_type || 'text/plain';
463 my $fn = $part->filename;
465 if ($ct =~ m!\btext/x?html\b!i) {
466 return attach_link($upfx, $ct, $p, $fn);
469 my $s = eval { $part->body_str };
471 # badly-encoded message? tell the world about it!
474 if ($ct =~ m!\btext/plain\b!i) {
475 # Try to assume UTF-8 because Alpine seems to
476 # do wacky things and set charset=X-UNKNOWN
477 $part->charset_set('UTF-8');
478 $s = eval { $part->body_str };
480 # If forcing charset=UTF-8 failed,
481 # attach_link will warn further down...
482 $s = $part->body if $@;
484 return attach_link($upfx, $ct, $p, $fn);
488 my @lines = split(/^/m, $s);
490 if (defined($fn) || $depth > 0 || $err) {
491 $s .= attach_link($upfx, $ct, $p, $fn, $err);
495 my $l = PublicInbox::Linkify->new;
496 foreach my $cur (@lines) {
498 # show the previously buffered quote inline
499 flush_quote(\$s, $l, \@quot) if @quot;
503 $s .= $l->linkify_2(ascii_html($cur));
509 if (@quot) { # ugh, top posted
510 flush_quote(\$s, $l, \@quot);
511 obfuscate_addrs($s) if $obfs;
514 obfuscate_addrs($s) if $obfs;
515 if ($s =~ /\n\z/s) { # common, last line ends with a newline
517 } else { # some editors don't do newlines...
523 sub _msg_html_prepare {
524 my ($hdr, $ctx, $obfs) = @_;
525 my $srch = $ctx->{srch} if $ctx;
527 my $rv = "<pre\nid=b>"; # anchor for body start
530 $ctx->{-upfx} = '../';
533 my $mid = mid_clean($hdr->header_raw('Message-ID'));
534 $mid = PublicInbox::Hval->new_msgid($mid);
535 foreach my $h (qw(From To Cc Subject Date)) {
536 my $v = $hdr->header($h);
537 defined($v) && ($v ne '') or next;
538 $v = PublicInbox::Hval->new($v);
541 my @n = PublicInbox::Address::names($v->raw);
542 $title[1] = ascii_html(join(', ', @n));
543 obfuscate_addrs($title[1]) if $obfs;
544 } elsif ($h eq 'Subject') {
545 $title[0] = $v->as_html;
547 $rv .= qq($h: <a\nhref="#r"\nid=t>);
548 $rv .= $v->as_html . "</a>\n";
553 obfuscate_addrs($v) if $obfs;
557 $title[0] ||= '(no subject)';
558 $ctx->{-title_html} = join(' - ', @title);
559 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
560 $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
561 $rv .= _parent_headers($hdr, $srch);
566 my ($dst, $ctx, $hdr, $tpfx) = @_;
567 my $srch = $ctx->{srch};
568 my $mid = mid_clean($hdr->header_raw('Message-ID'));
569 my $sres = $srch->get_thread($mid);
570 my $nr = $sres->{total};
571 my $expand = qq(<a\nhref="${tpfx}T/#u">expand</a> ) .
572 qq(/ <a\nhref="${tpfx}t.mbox.gz">mbox.gz</a> ) .
573 qq(/ <a\nhref="${tpfx}t.atom">Atom feed</a>);
575 my $parent = in_reply_to($hdr);
576 $$dst .= "\n<b>Thread overview: </b>";
578 if (defined $parent) {
579 $$dst .= "($expand)\n ";
580 $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
582 $$dst .= "[no followups, yet] ($expand)\n";
584 $ctx->{next_msg} = undef;
585 $ctx->{parent_msg} = $parent;
589 $$dst .= "$nr+ messages in thread ($expand";
590 $$dst .= qq! / <a\nhref="#b">[top]</a>)\n!;
592 my $subj = $srch->subject_path($hdr->header('Subject'));
593 $ctx->{seen} = { $subj => 1 };
595 $ctx->{prev_attr} = '';
596 $ctx->{prev_level} = 0;
598 $sres = load_results($srch, $sres);
600 # reduce hash lookups in skel_dump
601 $ctx->{-obfuscate} = $ctx->{-inbox}->{obfuscate};
602 walk_thread(thread_results($sres), $ctx, *skel_dump);
604 $ctx->{parent_msg} = $parent;
607 sub _parent_headers {
608 my ($hdr, $srch) = @_;
611 my $irt = in_reply_to($hdr);
613 my $v = PublicInbox::Hval->new_msgid($irt);
614 my $html = $v->as_html;
615 my $href = $v->{href};
616 $rv .= "In-Reply-To: <";
617 $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
620 # do not display References: if search is present,
621 # we show the thread skeleton at the bottom, instead.
624 my $refs = $hdr->header_raw('References');
626 # avoid redundant URLs wasting bandwidth
628 $seen{$irt} = 1 if defined $irt;
630 my @raw_refs = ($refs =~ /<([^>]+)>/g);
631 foreach my $ref (@raw_refs) {
634 push @refs, linkify_ref_nosrch($ref);
638 $rv .= 'References: '. join("\n\t", @refs) . "\n";
645 my ($hdr, $standalone, $ctx, $rhref) = @_;
647 my $srch = $ctx->{srch} if $ctx;
650 my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
654 thread_skel(\$idx, $ctx, $hdr, $tpfx);
659 if (my $n = $ctx->{next_msg}) {
660 $n = PublicInbox::Hval->new_msgid($n)->{href};
661 $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
664 my $par = $ctx->{parent_msg};
666 $u = PublicInbox::Hval->new_msgid($par)->{href};
669 if (my $p = $ctx->{prev_msg}) {
670 $prev = PublicInbox::Hval->new_msgid($p)->{href};
671 if ($p && $par && $p eq $par) {
672 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
673 'rel=prev>prev parent</a>';
676 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
678 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
680 } elsif ($u) { # unlikely
681 $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
683 $irt = "$next $prev$parent ";
688 $irt .= qq(<a\nhref="$rhref">reply</a>);
692 sub linkify_ref_nosrch {
693 my $v = PublicInbox::Hval->new_msgid($_[0]);
694 my $html = $v->as_html;
695 my $href = $v->{href};
696 "<<a\nhref=\"../$href/\">$html</a>>";
701 'm' . id_compress($msgid, 1);
705 my ($upfx, $mid) = @_;
707 $mid = PublicInbox::Hval->new_msgid($mid);
708 my $href = $mid->{href};
709 my $html = $mid->as_html;
710 qq{[parent not found: <<a\nhref="$upfx$href/">$html</a>>]};
715 INDENT x ($level - 1);
719 my ($srch, $sres) = @_;
720 my $msgs = delete $sres->{msgs};
721 $srch->retry_reopen(sub { [ map { $_->mid; $_ } @$msgs ] });
726 my $ts = eval { str2time($hdr->header('Date')) };
727 defined($ts) ? $ts : 0;
732 require PublicInbox::SearchThread;
733 PublicInbox::SearchThread::thread($msgs, *sort_ts);
738 require PublicInbox::ExtMsg;
739 PublicInbox::ExtMsg::ext_msg($ctx);
744 my $ts = $hdr->header('X-PI-TS') || msg_timestamp($hdr);
748 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
751 my ($ctx, $level, $node) = @_;
752 my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
754 my $dst = $ctx->{dst};
755 my $cur = $ctx->{cur};
756 my $mid = $smsg->{mid};
758 my $f = ascii_html($smsg->from_name);
759 my $obfs = $ctx->{-obfuscate};
760 obfuscate_addrs($f) if $obfs;
762 my $d = fmt_ts($smsg->{ts}) . ' ' . indent_for($level) . th_pfx($level);
764 $ctx->{first_level} ||= $level;
766 if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
767 $ctx->{prev_attr} = $attr;
769 $ctx->{prev_level} = $level;
774 $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
775 "$attr [this message]</a></b>\n";
778 $ctx->{prev_msg} = $mid;
781 $ctx->{next_msg} ||= $mid;
784 # Subject is never undef, this mail was loaded from
785 # our Xapian which would've resulted in '' if it were
786 # really missing (and Filter rejects empty subjects)
787 my $subj = $smsg->subject;
788 my $h = $ctx->{srch}->subject_path($subj);
789 if ($ctx->{seen}->{$h}) {
792 $ctx->{seen}->{$h} = 1;
793 $subj = PublicInbox::Hval->new($subj);
794 $subj = $subj->as_html;
795 obfuscate_addrs($subj) if $obfs;
799 my $mapping = $ctx->{mapping};
800 my $end = defined($subj) ? "$subj</a> $f\n" : "$f</a>\n";
802 my $map = $mapping->{$mid};
803 $id = id_compress($mid, 1);
805 $map->[0] = "$d<a\nhref=\"$m\">$end";
808 $m = $ctx->{-upfx}.mid_escape($mid).'/';
810 $$dst .= $d . "<a\nhref=\"$m\"$id>" . $end;
814 my ($ctx, $level, $node) = @_;
816 my $mid = $node->{id};
817 my $d = $ctx->{pct} ? ' [irrelevant] ' # search result
819 $d .= indent_for($level) . th_pfx($level);
820 my $upfx = $ctx->{-upfx};
821 my $m = PublicInbox::Hval->new_msgid($mid);
822 my $href = $upfx . $m->{href} . '/';
823 my $html = $m->as_html;
825 my $mapping = $ctx->{mapping};
826 my $map = $mapping->{$mid} if $mapping;
828 my $id = id_compress($mid, 1);
829 $map->[0] = $d . qq{<<a\nhref=#r$id>$html</a>>\n};
830 $d .= qq{<<a\nhref="$href"\nid=r$id>$html</a>>\n};
832 $d .= qq{<<a\nhref="$href">$html</a>>\n};
834 my $dst = $ctx->{dst};
840 (eval { $a->topmost->{smsg}->ts } || 0) <=>
841 (eval { $b->topmost->{smsg}->ts } || 0)
845 # accumulate recent topics if search is supported
846 # returns 200 if done, 404 if not
848 my ($ctx, $level, $node) = @_;
849 my $srch = $ctx->{srch};
850 my $mid = $node->{id};
851 my $x = $node->{smsg} || $srch->lookup_mail($mid);
856 $subj = $srch->subject_normalized($subj);
859 $topic = [ $ts, 1, { $subj => $mid }, $subj ];
860 $ctx->{-cur_topic} = $topic;
861 push @{$ctx->{order}}, $topic;
865 $topic = $ctx->{-cur_topic}; # should never be undef
866 $topic->[0] = $ts if $ts > $topic->[0];
868 my $seen = $topic->[2];
869 if (scalar(@$topic) == 3) { # parent was a ghost
871 } elsif (!$seen->{$subj}) {
872 push @$topic, $level, $subj;
874 $seen->{$subj} = $mid; # latest for subject
875 } else { # ghost message
876 return if $level != 0; # ignore child ghosts
877 $topic = [ -666, 0, {} ];
878 $ctx->{-cur_topic} = $topic;
879 push @{$ctx->{order}}, $topic;
885 my $order = delete $ctx->{order}; # [ ts, subj1, subj2, subj3, ... ]
887 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
892 my $obfs = $ctx->{-inbox}->{obfuscate};
894 # sort by recency, this allows new posts to "bump" old topics...
895 foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
896 my ($ts, $n, $seen, $top, @ex) = @$topic;
898 next unless defined $top; # ghost topic
899 my $mid = delete $seen->{$top};
900 my $href = mid_escape($mid);
901 $top = PublicInbox::Hval->new($top)->as_html;
904 # $n isn't the total number of posts on the topic,
905 # just the number of posts in the current results window
909 $anchor = '#u'; # top of only message
911 $n = " ($n+ messages)";
912 $anchor = '#t'; # thread skeleton
915 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
916 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
917 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
918 " $ts UTC $n - $mbox / $atom\n";
919 for (my $i = 0; $i < scalar(@ex); $i += 2) {
921 my $subj = $ex[$i + 1];
922 $mid = delete $seen->{$subj};
923 $subj = ascii_html($subj);
924 obfuscate_addrs($subj) if $obfs;
925 $href = mid_escape($mid);
926 $s .= indent_for($level) . TCHILD;
927 $s .= "<a\nhref=\"$href/T/#u\">$subj</a>\n";
931 $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
935 sub index_nav { # callback for WwwStream
936 my (undef, $ctx) = @_;
937 delete $ctx->{qp} or return;
942 my $next_o = $ctx->{-next_o};
944 $next = qq!<a\nhref="?o=$next_o"\nrel=next>next</a>!;
946 if (my $cur_o = $ctx->{-cur_o}) {
947 $latest = qq! <a\nhref=.>latest</a>!;
949 my $o = $cur_o - ($next_o - $cur_o);
951 $prev = qq!<a\nhref="?o=$o"\nrel=prev>prev</a>!;
953 $prev = qq!<a\nhref=.\nrel=prev>prev</a>!;
956 "<hr><pre>page: $next $prev$latest</pre>";
961 my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/);
962 my $opts = { offset => $off, limit => 200 };
965 my $srch = $ctx->{srch};
966 my $sres = $srch->query('', $opts);
967 my $nr = scalar @{$sres->{msgs}};
969 $sres = load_results($srch, $sres);
970 walk_thread(thread_results($sres), $ctx, *acc_topic);
972 $ctx->{-next_o} = $off+ $nr;
973 $ctx->{-cur_o} = $off;
974 PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
977 sub thread_adj_level {
978 my ($ctx, $level) = @_;
980 my $max = $ctx->{cur_level};
982 return ('', '') if $max == 0; # flat output
984 # reset existing lists
985 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
986 $ctx->{cur_level} = 0;
988 } elsif ($level == $max) { # continue existing list
990 } elsif ($level < $max) {
991 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
992 $ctx->{cur_level} = $level;
993 ("$beg<li>", '</li>');
994 } else { # ($level > $max) # start a new level
995 $ctx->{cur_level} = $level;
996 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1001 sub ghost_index_entry {
1002 my ($ctx, $level, $node) = @_;
1003 my ($beg, $end) = thread_adj_level($ctx, $level);
1004 $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})