]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
view: simplify parent anchoring code
[public-inbox.git] / lib / PublicInbox / View.pm
1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::View;
4 use strict;
5 use warnings;
6 use URI::Escape qw/uri_escape_utf8/;
7 use Date::Parse qw/str2time/;
8 use Encode qw/find_encoding/;
9 use Encode::MIME::Header;
10 use Email::MIME::ContentType qw/parse_content_type/;
11 use PublicInbox::Hval;
12 use PublicInbox::MID qw/mid_clean mid_compress mid2path/;
13 use Digest::SHA qw/sha1_hex/;
14 my $SALT = rand;
15 require POSIX;
16
17 # TODO: make these constants tunable
18 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
19 use constant MAX_TRUNC_LEN => 72;
20 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
21 use constant T_ANCHOR => '#u';
22
23 *ascii_html = *PublicInbox::Hval::ascii_html;
24
25 my $enc_utf8 = find_encoding('UTF-8');
26
27 # public functions:
28 sub msg_html {
29         my ($ctx, $mime, $full_pfx, $footer) = @_;
30         if (defined $footer) {
31                 $footer = "\n" . $footer;
32         } else {
33                 $footer = '';
34         }
35         headers_to_html_header($mime, $full_pfx, $ctx) .
36                 multipart_text_as_html($mime, $full_pfx) .
37                 '</pre><hr />' . PRE_WRAP .
38                 html_footer($mime, 1, $full_pfx, $ctx) .
39                 $footer .
40                 '</pre></body></html>';
41 }
42
43 sub feed_entry {
44         my ($class, $mime, $full_pfx) = @_;
45
46         PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
47 }
48
49 sub in_reply_to {
50         my ($header_obj) = @_;
51         my $irt = $header_obj->header('In-Reply-To');
52
53         return mid_clean($irt) if (defined $irt);
54
55         my $refs = $header_obj->header('References');
56         if ($refs && $refs =~ /<([^>]+)>\s*\z/s) {
57                 return $1;
58         }
59         undef;
60 }
61
62 # this is already inside a <pre>
63 sub index_entry {
64         my ($fh, $mime, $level, $state) = @_;
65         my $midx = $state->{anchor_idx}++;
66         my $ctx = $state->{ctx};
67         my $srch = $ctx->{srch};
68         my ($prev, $next) = ($midx - 1, $midx + 1);
69         my $part_nr = 0;
70         my $enc = enc_for($mime->header("Content-Type"));
71         my $subj = $mime->header('Subject');
72         my $header_obj = $mime->header_obj;
73
74         my $mid_raw = $header_obj->header('Message-ID');
75         my $id = anchor_for($mid_raw);
76         my $seen = $state->{seen};
77         $seen->{$id} = "#$id"; # save the anchor for children, later
78
79         my $mid = PublicInbox::Hval->new_msgid($mid_raw);
80         my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
81         my @from = Email::Address->parse($from);
82         $from = $from[0]->name;
83
84         $from = PublicInbox::Hval->new_oneline($from)->as_html;
85         $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
86         my $more = 'permalink';
87         my $root_anchor = $state->{root_anchor};
88         my $path = $root_anchor ? '../../' : '';
89         my $href = $mid->as_href;
90         my $irt = in_reply_to($header_obj);
91         my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt;
92
93         if ($srch) {
94                 my $t = $ctx->{flat} ? 'T' : 't';
95                 $subj = "<a\nhref=\"${path}$href/$t/#u\">$subj</a>";
96         }
97         if ($root_anchor && $root_anchor eq $id) {
98                 $subj = "<u\nid=\"u\">$subj</u>";
99         }
100
101         my $ts = _msg_date($mime);
102         my $rv = "<table\nsummary=l$level><tr>";
103         if ($level) {
104                 $rv .= '<td><pre>' . ('  ' x $level) . '</pre></td>';
105         }
106         $rv .= "<td\nid=s$midx>" . PRE_WRAP;
107         $rv .= "<b\nid=\"$id\">$subj</b>\n";
108         $rv .= "- by $from @ $ts UTC - ";
109         $rv .= "<a\nhref=\"#s$next\">next</a>";
110         if ($prev >= 0) {
111                 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
112         }
113         $fh->write($rv .= "\n\n");
114
115         my ($fhref, $more_ref);
116         my $mhref = "${path}$href/";
117
118         # show full messages at level == 0 in threaded view
119         if ($level > 0 || ($ctx->{flat} && $root_anchor ne $id)) {
120                 $fhref = "${path}$href/f/";
121                 $more_ref = \$more;
122         }
123         # scan through all parts, looking for displayable text
124         $mime->walk_parts(sub {
125                 index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
126         });
127         $mime->body_set('');
128
129         my $txt = "${path}$href/raw";
130         $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
131         $rv .= html_footer($mime, 0, undef, $ctx);
132
133         if (defined $irt) {
134                 unless (defined $parent_anchor) {
135                         my $v = PublicInbox::Hval->new_msgid($irt);
136                         $v = $v->as_href;
137                         $parent_anchor = "${path}$v/";
138                 }
139                 $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
140         }
141         if ($srch) {
142                 if ($ctx->{flat}) {
143                         $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
144                                 "|<b>flat</b>]";
145                 } else {
146                         $rv .= " [<b>threaded</b>|" .
147                                 "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
148                 }
149         }
150
151         $fh->write($rv .= '</pre></td></tr></table>');
152 }
153
154 sub thread_html {
155         my ($ctx, $foot, $srch) = @_;
156         sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
157 }
158
159 # only private functions below.
160
161 sub emit_thread_html {
162         my ($cb, $ctx, $foot, $srch) = @_;
163         my $mid = mid_compress($ctx->{mid});
164         my $res = $srch->get_thread($mid);
165         my $msgs = load_results($res);
166         my $nr = scalar @$msgs;
167         return missing_thread($cb) if $nr == 0;
168         my $flat = $ctx->{flat};
169         my $orig_cb = $cb;
170         my $state = {
171                 ctx => $ctx,
172                 seen => {},
173                 root_anchor => anchor_for($mid),
174                 anchor_idx => 0,
175         };
176
177         require PublicInbox::GitCatFile;
178         my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
179         if ($flat) {
180                 __thread_entry(\$cb, $git, $state, $_, 0) for (@$msgs);
181         } else {
182                 my $th = thread_results($msgs);
183                 thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
184         }
185         $git = undef;
186         Email::Address->purge_cache;
187
188         # there could be a race due to a message being deleted in git
189         # but still being in the Xapian index:
190         return missing_thread($cb) if ($orig_cb eq $cb);
191
192         my $final_anchor = $state->{anchor_idx};
193         my $next = "<a\nid=\"s$final_anchor\">";
194         $next .= $final_anchor == 1 ? 'only message in' : 'end of';
195         $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>";
196         if ($flat) {
197                 $next .= " [<a\nhref=\"../t/#u\">threaded</a>|<b>flat</b>]";
198         } else {
199                 $next .= " [<b>threaded</b>|<a\nhref=\"../T/#u\">flat</a>]";
200         }
201         $next .= "\ndownload thread: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
202         $next .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>";
203         $cb->write("<hr />" . PRE_WRAP . $next . "\n\n". $foot .
204                    "</pre></body></html>");
205         $cb->close;
206 }
207
208 sub index_walk {
209         my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
210         my $s = add_text_body($enc, $part, $part_nr, $fhref);
211
212         if ($more) {
213                 # drop the remainder of git patches, they're usually better
214                 # to review when the full message is viewed
215                 $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
216
217                 # Drop signatures
218                 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
219         }
220
221         # kill any leading or trailing whitespace lines
222         $s =~ s/^\s*$//sgm;
223         $s =~ s/\s+\z//s;
224
225         if ($s ne '') {
226                 # kill per-line trailing whitespace
227                 $s =~ s/[ \t]+$//sgm;
228                 $s .= "\n" unless $s =~ /\n\z/s;
229         }
230         $fh->write($s);
231 }
232
233 sub enc_for {
234         my ($ct, $default) = @_;
235         $default ||= $enc_utf8;
236         defined $ct or return $default;
237         my $ct_parsed = parse_content_type($ct);
238         if ($ct_parsed) {
239                 if (my $charset = $ct_parsed->{attributes}->{charset}) {
240                         my $enc = find_encoding($charset);
241                         return $enc if $enc;
242                 }
243         }
244         $default;
245 }
246
247 sub multipart_text_as_html {
248         my ($mime, $full_pfx, $srch) = @_;
249         my $rv = "";
250         my $part_nr = 0;
251         my $enc = enc_for($mime->header("Content-Type"));
252
253         # scan through all parts, looking for displayable text
254         $mime->walk_parts(sub {
255                 my ($part) = @_;
256                 $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx);
257         });
258         $mime->body_set('');
259         $rv;
260 }
261
262 sub add_filename_line {
263         my ($enc, $fn) = @_;
264         my $len = 72;
265         my $pad = "-";
266         $fn = $enc->decode($fn);
267         $len -= length($fn);
268         $pad x= ($len/2) if ($len > 0);
269         "$pad " . ascii_html($fn) . " $pad\n";
270 }
271
272 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
273                  [\@:\w\.-]+/
274                  ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
275
276 sub linkify_1 {
277         my ($link_map, $s) = @_;
278         $s =~ s!$LINK_RE!
279                 my $url = $1;
280                 # salt this, as this could be exploited to show
281                 # links in the HTML which don't show up in the raw mail.
282                 my $key = sha1_hex($url . $SALT);
283                 $link_map->{$key} = $url;
284                 'PI-LINK-'. $key;
285         !ge;
286         $s;
287 }
288
289 sub linkify_2 {
290         my ($link_map, $s) = @_;
291
292         # Added "PI-LINK-" prefix to avoid false-positives on git commits
293         $s =~ s!\bPI-LINK-([a-f0-9]{40})\b!
294                 my $key = $1;
295                 my $url = $link_map->{$key};
296                 if (defined $url) {
297                         $url = ascii_html($url);
298                         "<a\nhref=\"$url\">$url</a>";
299                 } else {
300                         # false positive or somebody tried to mess with us
301                         $key;
302                 }
303         !ge;
304         $s;
305 }
306
307 sub flush_quote {
308         my ($quot, $n, $part_nr, $full_pfx, $final) = @_;
309
310         if ($full_pfx) {
311                 if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
312                         # show quote inline
313                         my %l;
314                         my $rv = join('', map { linkify_1(\%l, $_) } @$quot);
315                         @$quot = ();
316                         $rv = ascii_html($rv);
317                         return linkify_2(\%l, $rv);
318                 }
319
320                 # show a short snippet of quoted text and link to full version:
321                 @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
322                 my $cur = join(' ', @$quot);
323                 @$quot = split(/\s+/, $cur);
324                 $cur = '';
325                 do {
326                         my $tmp = shift(@$quot);
327                         my $len = length($tmp) + length($cur);
328                         if ($len > MAX_TRUNC_LEN) {
329                                 @$quot = ();
330                         } else {
331                                 $cur .= $tmp . ' ';
332                         }
333                 } while (@$quot && length($cur) < MAX_TRUNC_LEN);
334                 @$quot = ();
335                 $cur =~ s/ \z/ .../s;
336                 $cur = ascii_html($cur);
337                 my $nr = ++$$n;
338                 "&gt; [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
339         } else {
340                 # show everything in the full version with anchor from
341                 # short version (see above)
342                 my $nr = ++$$n;
343                 my $rv = "";
344                 my %l;
345                 $rv .= join('', map { linkify_1(\%l, $_) } @$quot);
346                 @$quot = ();
347                 $rv = ascii_html($rv);
348                 "<a\nid=q${part_nr}_$nr></a>" . linkify_2(\%l, $rv);
349         }
350 }
351
352 sub add_text_body {
353         my ($enc_msg, $part, $part_nr, $full_pfx) = @_;
354         return '' if $part->subparts;
355
356         my $ct = $part->content_type;
357         # account for filter bugs...
358         if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) {
359                 $part->body_set('');
360                 return '';
361         }
362         my $enc = enc_for($ct, $enc_msg);
363         my $n = 0;
364         my $nr = 0;
365         my $s = $part->body;
366         $part->body_set('');
367         $s = $enc->decode($s);
368         my @lines = split(/^/m, $s);
369         $s = '';
370
371         if ($$part_nr > 0) {
372                 my $fn = $part->filename;
373                 defined($fn) or $fn = "part #" . ($$part_nr + 1);
374                 $s .= add_filename_line($enc, $fn);
375         }
376
377         my @quot;
378         while (defined(my $cur = shift @lines)) {
379                 if ($cur !~ /^>/) {
380                         # show the previously buffered quote inline
381                         if (scalar @quot) {
382                                 $s .= flush_quote(\@quot, \$n, $$part_nr,
383                                                   $full_pfx, 0);
384                         }
385
386                         # regular line, OK
387                         my %l;
388                         $cur = linkify_1(\%l, $cur);
389                         $cur = ascii_html($cur);
390                         $s .= linkify_2(\%l, $cur);
391                 } else {
392                         push @quot, $cur;
393                 }
394         }
395         $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1) if scalar @quot;
396         $s .= "\n" unless $s =~ /\n\z/s;
397         ++$$part_nr;
398         $s;
399 }
400
401 sub headers_to_html_header {
402         my ($mime, $full_pfx, $ctx) = @_;
403         my $srch = $ctx->{srch} if $ctx;
404         my $rv = "";
405         my @title;
406         my $header_obj = $mime->header_obj;
407         my $mid = $header_obj->header('Message-ID');
408         $mid = PublicInbox::Hval->new_msgid($mid);
409         my $mid_href = $mid->as_href;
410         foreach my $h (qw(From To Cc Subject Date)) {
411                 my $v = $mime->header($h);
412                 defined($v) && ($v ne '') or next;
413                 $v = PublicInbox::Hval->new_oneline($v);
414
415                 if ($h eq 'From') {
416                         my @from = Email::Address->parse($v->raw);
417                         $title[1] = ascii_html($from[0]->name);
418                 } elsif ($h eq 'Subject') {
419                         $title[0] = $v->as_html;
420                         if ($srch) {
421                                 my $p = $full_pfx ? '' : '../';
422                                 $rv .= "$h: <a\nid=\"t\"\nhref=\"${p}t/#u\">";
423                                 $rv .= $v->as_html . "</a>\n";
424                                 next;
425                         }
426                 }
427                 $rv .= "$h: " . $v->as_html . "\n";
428
429         }
430         $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
431         my $raw_ref = $full_pfx ? 'raw' : '../raw';
432         $rv .= "(<a\nhref=\"$raw_ref\">raw</a>)\n";
433         if ($srch) {
434                 $rv .= "<a\nhref=\"#r\">References: [see below]</a>\n";
435         } else {
436                 $rv .= _parent_headers_nosrch($header_obj);
437         }
438         $rv .= "\n";
439
440         ("<html><head><title>".  join(' - ', @title) .
441          '</title></head><body>' . PRE_WRAP . $rv);
442 }
443
444 sub thread_inline {
445         my ($dst, $ctx, $cur, $full_pfx) = @_;
446         my $srch = $ctx->{srch};
447         my $mid = mid_compress(mid_clean($cur->header('Message-ID')));
448         my $res = $srch->get_thread($mid);
449         my $nr = $res->{total};
450
451         if ($nr <= 1) {
452                 $$dst .= "\n[no followups, yet]\n";
453                 return;
454         }
455         my $upfx = $full_pfx ? '' : '../';
456
457         $$dst .= "\n\n~$nr messages in thread: ".
458                  "(<a\nhref=\"${upfx}t/#u\">expand</a>)\n";
459         my $subj = $srch->subject_path($cur->header('Subject'));
460         my $state = {
461                 seen => { $subj => 1 },
462                 srch => $srch,
463                 cur => $mid,
464         };
465         for (thread_results(load_results($res))->rootset) {
466                 inline_dump($dst, $state, $upfx, $_, 0);
467         }
468         $state->{next_msg};
469 }
470
471 sub _parent_headers_nosrch {
472         my ($header_obj) = @_;
473         my $rv = '';
474
475         my $irt = in_reply_to($header_obj);
476         if (defined $irt) {
477                 my $v = PublicInbox::Hval->new_msgid($irt);
478                 my $html = $v->as_html;
479                 my $href = $v->as_href;
480                 $rv .= "In-Reply-To: &lt;";
481                 $rv .= "<a\nhref=\"../$href/\">$html</a>&gt;\n";
482         }
483
484         my $refs = $header_obj->header('References');
485         if ($refs) {
486                 # avoid redundant URLs wasting bandwidth
487                 my %seen;
488                 $seen{$irt} = 1 if defined $irt;
489                 my @refs;
490                 my @raw_refs = ($refs =~ /<([^>]+)>/g);
491                 foreach my $ref (@raw_refs) {
492                         next if $seen{$ref};
493                         $seen{$ref} = 1;
494                         push @refs, linkify_ref($ref);
495                 }
496
497                 if (@refs) {
498                         $rv .= 'References: '. join(' ', @refs) . "\n";
499                 }
500         }
501         $rv;
502 }
503
504 sub html_footer {
505         my ($mime, $standalone, $full_pfx, $ctx) = @_;
506         my %cc; # everyone else
507         my $to; # this is the From address
508
509         foreach my $h (qw(From To Cc)) {
510                 my $v = $mime->header($h);
511                 defined($v) && ($v ne '') or next;
512                 my @addrs = Email::Address->parse($v);
513                 foreach my $recip (@addrs) {
514                         my $address = $recip->address;
515                         my $dst = lc($address);
516                         $cc{$dst} ||= $address;
517                         $to ||= $dst;
518                 }
519         }
520         Email::Address->purge_cache if $standalone;
521
522         my $subj = $mime->header('Subject') || '';
523         $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
524         my $mid = $mime->header('Message-ID');
525         my $irt = uri_escape_utf8($mid);
526         delete $cc{$to};
527         $to = uri_escape_utf8($to);
528         $subj = uri_escape_utf8($subj);
529
530         my $cc = uri_escape_utf8(join(',', sort values %cc));
531         my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
532
533         my $srch = $ctx->{srch} if $ctx;
534         my $upfx = $full_pfx ? '../' : '../../';
535         my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
536         if ($idx && $srch) {
537                 my $next = thread_inline(\$idx, $ctx, $mime, $full_pfx);
538                 $irt = in_reply_to($mime->header_obj);
539                 if (defined $irt) {
540                         $irt = PublicInbox::Hval->new_msgid($irt);
541                         $irt = $irt->as_href;
542                         $irt = "<a\nhref=\"$upfx$irt/\">parent</a> ";
543                 } else {
544                         $irt = ' ' x length('parent ');
545                 }
546                 if ($next) {
547                         $irt .= "<a\nhref=\"$upfx$next/\">next</a> ";
548                 } else {
549                         $irt .= '     ';
550                 }
551         } else {
552                 $irt = '';
553         }
554
555         "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
556 }
557
558 sub linkify_ref {
559         my $v = PublicInbox::Hval->new_msgid($_[0]);
560         my $html = $v->as_html;
561         my $href = $v->as_href;
562         "&lt;<a\nhref=\"../$href/\">$html</a>&gt;";
563 }
564
565 sub anchor_for {
566         my ($msgid) = @_;
567         my $id = $msgid;
568         if ($id !~ /\A[a-f0-9]{40}\z/) {
569                 $id = mid_compress(mid_clean($id), 1);
570         }
571         'm' . $id;
572 }
573
574 sub thread_html_head {
575         my ($cb, $mime) = @_;
576         $$cb = $$cb->([200, ['Content-Type'=> 'text/html; charset=UTF-8']]);
577
578         my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
579         $s = $s->as_html;
580         $$cb->write("<html><head><title>$s</title></head><body>");
581 }
582
583 sub __thread_entry {
584         my ($cb, $git, $state, $mime, $level) = @_;
585
586         # lazy load the full message from mini_mime:
587         my $path = mid2path(mid_clean($mime->header('Message-ID')));
588         $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
589         if ($mime) {
590                 if ($state->{anchor_idx} == 0) {
591                         thread_html_head($cb, $mime);
592                 }
593                 index_entry($$cb, $mime, $level, $state);
594         }
595 }
596
597 sub thread_entry {
598         my ($cb, $git, $state, $node, $level) = @_;
599         return unless $node;
600         if (my $mime = $node->message) {
601                 __thread_entry($cb, $git, $state, $mime, $level);
602         }
603         thread_entry($cb, $git, $state, $node->child, $level + 1);
604         thread_entry($cb, $git, $state, $node->next, $level);
605 }
606
607 sub load_results {
608         my ($res) = @_;
609
610         [ map { $_->mini_mime } @{delete $res->{msgs}} ];
611 }
612
613 sub msg_timestamp {
614         my ($mime) = @_;
615         my $ts = eval { str2time($mime->header('Date')) };
616         defined($ts) ? $ts : 0;
617 }
618
619 sub thread_results {
620         my ($msgs) = @_;
621         require PublicInbox::Thread;
622         my $th = PublicInbox::Thread->new(@$msgs);
623         $th->thread;
624         no warnings 'once';
625         $th->order(*PublicInbox::Thread::sort_ts);
626         $th
627 }
628
629 sub missing_thread {
630         my ($cb) = @_;
631         my $title = 'Thread does not exist';
632         $cb->([404, ['Content-Type' => 'text/html']])->write(<<EOF);
633 <html><head><title>$title</title></head><body><pre>$title
634 <a href="../../">Return to index</a></pre></body></html>
635 EOF
636 }
637
638 sub _msg_date {
639         my ($mime) = @_;
640         my $ts = $mime->header('X-PI-TS') || msg_timestamp($mime);
641         POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
642 }
643
644 sub _inline_header {
645         my ($dst, $state, $upfx, $mime, $level) = @_;
646         my $pfx = '  ' x $level;
647
648         my $cur = $state->{cur};
649         my $mid = $mime->header('Message-ID');
650         my $f = $mime->header('X-PI-From');
651         my $d = _msg_date($mime);
652         $f = PublicInbox::Hval->new($f);
653         $d = PublicInbox::Hval->new($d);
654         $f = $f->as_html;
655         $d = $d->as_html . ' UTC';
656         my $midc = mid_compress(mid_clean($mid));
657         if ($cur) {
658                 if ($cur eq $midc) {
659                         delete $state->{cur};
660                         $$dst .= "$pfx` <b><a\nid=\"r\"\nhref=\"#t\">".
661                                  "[this message]</a></b> by $f @ $d\n";
662
663                         return;
664                 }
665         } else {
666                 $state->{next_msg} ||= $midc;
667         }
668
669         # Subject is never undef, this mail was loaded from
670         # our Xapian which would've resulted in '' if it were
671         # really missing (and Filter rejects empty subjects)
672         my $s = $mime->header('Subject');
673         my $h = $state->{srch}->subject_path($s);
674         if ($state->{seen}->{$h}) {
675                 $s = undef;
676         } else {
677                 $state->{seen}->{$h} = 1;
678                 $s = PublicInbox::Hval->new($s);
679                 $s = $s->as_html;
680         }
681         my $m = PublicInbox::Hval->new_msgid($mid);
682         $m = $upfx . '../' . $m->as_href . '/';
683         if (defined $s) {
684                 $$dst .= "$pfx` <a\nhref=\"$m\">$s</a>\n" .
685                          "$pfx  $f @ $d\n";
686         } else {
687                 $$dst .= "$pfx` <a\nhref=\"$m\">$f @ $d</a>\n";
688         }
689 }
690
691 sub inline_dump {
692         my ($dst, $state, $upfx, $node, $level) = @_;
693         return unless $node;
694         return if $state->{stopped};
695         if (my $mime = $node->message) {
696                 _inline_header($dst, $state, $upfx, $mime, $level);
697         }
698         inline_dump($dst, $state, $upfx, $node->child, $level+1);
699         inline_dump($dst, $state, $upfx, $node->next, $level);
700 }
701
702 1;