7 mail-extract-raw-headers - Get named headers from RFC822-format input.
11 mail-extract-raw-headers [OPTIONS] <NAME> [<NAME> [...]]
17 =item -k, --keep-newlines, --keep-linefeeds
19 Keep linefeeds in multiline text.
21 =item -n, --header-names
23 Output the header name(s) too, not only the contents.
30 use Getopt
::Long qw
/:config no_ignore_case bundling no_getopt_compat/;
33 no if ($] >= 5.018), 'warnings' => 'experimental::smartmatch';
39 'k|keep-newlines|keep-linefeeds' => \
$OptKeepLF,
40 'n|header-names' => \
$OptNames,
41 'help|?' => sub { pod2usage
(-exitval
=>0, -verbose
=>99); },
42 ) or pod2usage
(-exitval
=>2, -verbose
=>99);
46 pod2usage
(-exitval
=>2, -verbose
=>99);
49 @asked_headers = map {lc} @ARGV;
57 for my $header_ref (@Headers)
59 if(lc $header_ref->{"name"} eq $hname)
61 push @return, $header_ref->{"content"};
78 # does not care line ending
81 if(my($header_name, $content) = /^(\S+?):[ ]?(.*)/)
83 if(lc $header_name ~~ @asked_headers)
85 my $header_name_pretty = $header_name =~ s/[^-]*/\L\u$&/gr;
86 my $header_hash = { "name" => $header_name, "pretty_name" => $header_name_pretty, "content" => $content, };
87 push @
{$found_headers{lc $header_name}}, $header_hash;
88 $last_header_ref = $header_hash;
92 $last_header_ref = undef;
97 # it is a folded header
98 if(defined $last_header_ref)
100 $last_header_ref->{"content"} .= "\n" if $OptKeepLF;
101 $last_header_ref->{"content"} .= $1;
106 die "$0: can not parse line $.\n";
110 for my $asked_header (@asked_headers)
112 for my $header (@
{$found_headers{$asked_header}})
114 # avoid non-whitespace character at the beginning of lines in the header content,
115 # so header names can not be spoofed.
116 my $safe_content = $header->{'content'} =~ s/\n([^\t ])/\n $1/gr;
120 printf "%s: %s\n", $header->{'name'}, $safe_content;
124 printf "%s\n", $safe_content;