Skip to content

Commit

Permalink
Decode RFC2047 header values
Browse files Browse the repository at this point in the history
Re-encode RFC2047 header values as utf-8 for display.

Add --raw switch to allow user to disable.
  • Loading branch information
tlhackque committed Feb 2, 2022
1 parent 8aee1b7 commit 1237944
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ allows you to move/remove messages in a queue, but does not facilitate
analyzing them.

Usage:
smqdecode --body --config --flags --headers --submission
smqdecode --body --config --flags --headers --raw --submission
--limit-body=[N,none] --message --metadata --skip-busy
--queue --warnings --type=[all,pended,quarantined,temporary]
--help --man --version
Expand Down
31 changes: 23 additions & 8 deletions smqdecode
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use warnings;
use strict;

my $copyright = << 'COPYRIGHT';
Copyright (C) 2019,2020, 2021 Timothe Litt <litt at acm.org>
Copyright (C) 2019,2020, 2021, 2022 Timothe Litt <litt at acm.org>
Use --man for license information.
COPYRIGHT

our $VERSION = 'V1.005';
our $VERSION = 'V1.006';

# License is in the POD at the end of this file.

Expand All @@ -36,6 +36,7 @@ use warnings;
use strict;

use Carp;
use Encode( qw/decode encode/ );
use Fcntl( qw/:flock :seek/ );
use POSIX( qw/strftime/ );

Expand Down Expand Up @@ -612,7 +613,7 @@ sub dumpmeta {

# Dump headers
#
# $qf->dumpheaders( [$ofh] )
# $qf->dumpheaders( $flags, [$ofh], $raw )

sub dumpheaders {
my $qf = shift;
Expand All @@ -625,7 +626,13 @@ sub dumpheaders {
# 0x81 indicates macro expansion.
# Present as '$' -- and quote any '$' as '\$'
my $txt = $h->{H};
$txt =~ s/([\$\x81])/$1 eq '$'? '\\$':'$'/ge unless( $raw );
unless( $raw ) {
$txt =~ s/([\$\x81])/$1 eq '$'? '\\$':'$'/ge;
if( $txt =~ /=\?.+\?.+\?=/ ) {
$txt =~ s/\n\s+//g;
$txt = encode( 'utf-8', decode( 'MIME-Header', $txt ) );
}
}
print $ofh ( "$txt\n" );

if( $flags && keys %{ $h->{F} } ) {
Expand Down Expand Up @@ -1051,7 +1058,7 @@ unless( __FILE__ =~ /\.pm$/ ) {
require Getopt::Long;
Getopt::Long->import( qw/GetOptions :config bundling/ );

my( $meta, $headers, $body, $transcript,
my( $meta, $headers, $body, $transcript, $raw,
$flags, $limit, $debug, $submit, $delete,
$help, $man, $version, );

Expand All @@ -1068,6 +1075,7 @@ unless( __FILE__ =~ /\.pm$/ ) {
'man' => \$man,
'metadata|m!' => \$meta,
'queue|q=s' => \$opts{qdir},
'raw|r!' => \$raw,
'skip-busy|s!' => \$opts{skip},
'submission|submit!' => \$submit,
'transcript|t!' => \$transcript,
Expand Down Expand Up @@ -1128,7 +1136,7 @@ unless( __FILE__ =~ /\.pm$/ ) {
$qf->dumpmeta if( $meta );
if( $headers ) {
print( "\n" ) if( $meta );
$qf->dumpheaders( $flags );
$qf->dumpheaders( $flags, undef, $raw );
}
if( $body ) {
print( "\n" ) if( $meta || $headers );
Expand Down Expand Up @@ -1197,7 +1205,7 @@ smqdecode - Decode and extract sendmail queue entries
=head1 SYNOPSIS
smqdecode --body --config --flags --headers --submission
smqdecode --body --config --flags --headers --raw --submission
--limit-body=[N,none] --message --metadata --skip-busy
--queue --warnings --type=[all,pended,quarantined,temporary]
--delete
Expand Down Expand Up @@ -1251,7 +1259,7 @@ The following options are provided. The short and long forms of
each option have the same effect. The long form of a boolean
option can be negated by specifying it as B<-->noB<OPTION>.
The usual defaults are --nometadata --headers --nobody --nowarn --limit-body=none.
The usual defaults are --nometadata --headers --nobody --nowarn --noraw --limit-body=none.
If --delete is specified, the default for --headers is --noheaders.
Expand Down Expand Up @@ -1322,6 +1330,13 @@ Output the (decoded) metadata for each queued message.
Look for the queue entries in F<DIR>. The default is described for B<--config>
=item B<-r> B<--raw>
When decoding headers, do not decode RFC2047-encoded values or macro codes.
The default is to decode RFC2047-encoded values and re-encode them as UTF-8. Macro
code are displayed as '$'.
=item B<-s> B<--skip-busy>
Ignore queue entries that are locked or can't be opened.
Expand Down

0 comments on commit 1237944

Please sign in to comment.