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