sub keep_7bit { local $_ = shift; tr/\x00-\x7F/#/c; $_; } sub decode_qp { my $res = shift; $res =~ s/\r\n/\n/g; # normalize newlines $res =~ s/[ \t]+\n/\n/g; # rule #3 (trailing space must be deleted) $res =~ s/=\n//g; # rule #5 (soft line breaks) if (ord('A') == 193) { # EBCDIC style machine if (ord('[') == 173) { $res =~ s/=([\da-fA-F]{2})/Encode::encode('cp1047',Encode::decode('iso-8859-1',pack("C", hex($1))))/ge; } elsif (ord('[') == 187) { $res =~ s/=([\da-fA-F]{2})/Encode::encode('posix-bc',Encode::decode('iso-8859-1',pack("C", hex($1))))/ge; } elsif (ord('[') == 186) { $res =~ s/=([\da-fA-F]{2})/Encode::encode('cp37',Encode::decode('iso-8859-1',pack("C", hex($1))))/ge; } } else { # ASCII style machine $res =~ s/=([\da-fA-F]{2})/pack("C", hex($1))/ge; } $res; } sub decode_words { my $value = shift || ""; my $wd = default MIME::WordDecoder; eval "require Encode; $value = Encode::decode('MIME-Header', $value)"; $wd->handler("BIG5" => "KEEP"); $wd->handler("WINDOWS-1250" => "KEEP"); $wd->handler("WINDOWS-1251" => "KEEP"); $wd->handler("WINDOWS-1252" => "KEEP"); $wd->handler("WINDOWS-1253" => "KEEP"); $wd->handler("WINDOWS-1254" => "KEEP"); $wd->handler("WINDOWS-1255" => "KEEP"); $wd->handler("WINDOWS-1256" => "KEEP"); $wd->handler("WINDOWS-1257" => "KEEP"); $wd->handler("WINDOWS-1258" => "KEEP"); $wd->handler("ISO-8859-1" => \&decode_qp); $wd->handler("ISO-8859-2" => \&decode_qp); my $retval = $value ? $wd->decode($value) : ""; return ($retval == 1 ? $value : $retval); }