将qmail邮件标题从base64转换为中文

just posted @ 2011年3月30日 11:09 in 脚本 , 1876 阅读

 

#!/usr/bin/perl

$sub=join(" ",@ARGV);  
    if ($sub=~ /=\?\S+\?\l(\S)\?/) {  
      if ($1 =~ /[Qq]/) {  
        $sub=decode_qp($sub);  
      }  
      elsif ($1 =~ /[Bb]/) {  
        $sub=decode_base64($sub);  
      }  
    }  
    elsif ($sub=~ /=[a-fA-F0-9][a-fA-F0-9]/) {  
      $sub=decode_qp($sub);  
    }  
    $subject.=$sub;  
    $subject=~s/\'/\\\'/g;  
    $subject=~s/\"/\\\"/g;  


print $subject; 
# for test 
#最好是将生成的subject.log copy一份,在这个基础上修改成为badsubject 主要是怕编码不对
#open(FILE,">> /tmp/subject.log");
#print FILE "$subject\n";
#close(FILE);



sub decode_qp {  
        my($string) = @_;  
        @buffer=split(/\?/,$string);  
        $string = $buffer[3] if ($buffer[3] ne "");  
        $string =~ s/=([\da-fA-F]{2})/pack("C", hex($1))/ge;  
        $string =~ /\?=(.*)/;  
        $string =~ tr/_/ /;  
        $buffer[4]=~s/^=//;  
        $buffer[0]=~s/=$//;  
        $string="$buffer[0]$string$buffer[4]";  
        return($string);  
}  
  
sub decode_base64 {  
        my($string) = @_;  
        my($string2);  
        @buffer=split(/\?/,$string);  
        $string = $buffer[3] if ($buffer[3] ne "");  
        $string =~ s/=+$//;  
        $string =~ tr|A-Za-z0-9+/| -_|;  
        while($string =~ /(.{1,60})/gs) {  
                my($string3) = chr(32+length($1)*3/4);  
                $string2 .= unpack("u",$string3 . $1 );  
        }  
        $buffer[4]=~s/^=//;  
        $buffer[0]=~s/=$//;  
        $string2="$buffer[0]$string2$buffer[4]";  
        return($string2);  
}

 

  • 无匹配
  • 无匹配
fireboy and watergir 说:
2019年1月22日 12:11

sub decode_qp {
my($string) = @_;
@buffer=split(/\?/,$string);
$string = $buffer[3] if ($buffer[3] ne "");
$string =~ s/=([\da-fA-F]{2})/pack("C", hex($1))/ge;
$string =~ /\?=(.*)/;
$string =~ tr/_/ /;
$buffer[4]=~s/^=//;
$buffer[0]=~s/=$//;
$string="$buffer[0]$string$buffer[4]";
return($string);
}

seo service london 说:
2024年1月14日 18:11

Easily, the article is actually the best topic on this registry related issue. I fit in with your conclusions and will eagerly look forward to your next updates


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter