+ my ($class, $msgid) = @_;
+ $class->new($msgid, mid_escape($msgid));
+}
+
+# some of these overrides are standard C escapes so they're
+# easy-to-understand when rendered.
+my %escape_sequence = (
+ "\x00" => '\\0', # NUL
+ "\x07" => '\\a', # bell
+ "\x08" => '\\b', # backspace
+ "\x09" => "\t", # obvious to show as-is
+ "\x0a" => "\n", # obvious to show as-is
+ "\x0b" => '\\v', # vertical tab
+ "\x0c" => '\\f', # form feed
+ "\x0d" => '\\r', # carriage ret (not preceding \n)
+ "\x1b" => '^[', # ASCII escape (mutt seems to escape this way)
+ "\x7f" => '\\x7f', # DEL
+);
+
+my %xhtml_map = (
+ '"' => '"',
+ '&' => '&',
+ "'" => ''',
+ '<' => '<',
+ '>' => '>',
+);
+
+$xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
+%xhtml_map = (%xhtml_map, %escape_sequence);
+
+sub src_escape ($) {
+ $_[0] =~ s/\r\n/\n/sg;
+ $_[0] =~ s/([\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
+ $_[0] = $enc_ascii->encode($_[0], Encode::HTMLCREF);
+}
+
+sub ascii_html {
+ my ($s) = @_;
+ $s =~ s/\r\n/\n/sg; # fixup bad line endings
+ $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
+ $enc_ascii->encode($s, Encode::HTMLCREF);
+}
+
+sub as_html { ascii_html($_[0]->{raw}) }
+
+sub raw {
+ if (defined $_[1]) {
+ $_[0]->{raw} = $_[1];
+ } else {
+ $_[0]->{raw};
+ }