File: convmail.pl

package info (click to toggle)
zh-autoconvert 0.3.16-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 3,612 kB
  • sloc: ansic: 28,053; perl: 264; makefile: 84; python: 9
file content (166 lines) | stat: -rw-r--r-- 3,855 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/perl

#This is a GB<->Big5 Email convertsion script.
#It can handle the MIME encoding Subject and Attenchment file.
#It bases on Anthony Fok<foka@debian.org>'s debian-chinese-gateway.pl.

#By Yu Guanghui <ygh@dlut.edu.cn>,2000/6/6
#$Header: /home/prog/autoconvert/contrib/convmail.pl,v 1.2 2000/11/27 16:16:33 ygh Exp $

use strict;
use Fcntl;
use MIME::Parser;
use MIME::Entity;
use MIME::Words qw(:all);

use Getopt::Std;

#
# Variable definitions
#

my $home = "$ENV{HOME}";
my $bin = "/usr/bin";
my $mimemail= "$home/mimemail";
my $encoding = '8bit';

my $tmphdr = "$home/mimemail/msg-$$.hdr";
my $tmpbdy = "$home/mimemail/msg-$$.bdy";

my $sigstripper = "| perl -p -e '\$/ = \"\"; s/(--  )\\n.*\\n.*\\s*\$//;' ";
my $src_chasret;
my $dest;

my %big5 = ( charset => 'big5',
             converter => "$bin/autob5" );

my %gb2312 = ( charset => 'gb2312',
               converter => "$bin/autogb" );

my %opts;
#
# Parse the args

getopts('gb',\%opts);

if ($opts{"g"} && $opts{"b"}) {
	usage();
}elsif($opts{"g"}){
	$dest=\%gb2312;
}elsif($opts{"b"}){
	$dest=\%big5;
}else{
	usage();
}

#
# Parse the message
#

my $parser = new MIME::Parser   output_dir      => $mimemail,
                                output_prefix   => "part",
                                output_to_core  => 20000;

my $top = $parser->read(\*STDIN) or die "couldn't parse MIME stream";
my $head = $top->head;

#Convert the message
if (defined($dest)) {
 
    # Decode the header to 8Bit 
    $head->decode; 
 
    my $header = $head->as_string; 
 
    if ($header =~ /[\x80-\xff]/) { 
	# Convert the decoded header to Big5/GB and write to a file 
	open(TMPHDR, "| $dest->{converter} > $tmphdr") 
	    or die "$!"; 
	$head->print(\*TMPHDR); 
	close TMPHDR; 
 
	# Re-read header, encode it as quoted-printable, and write it back. 
	open(TMPHDR, "< $tmphdr") or die "$!"; 
	undef $/; 
	$header = <TMPHDR>; 
	$/ = "\n"; 
	close TMPHDR; 
	$header = encode_mimewords($header, Charset => $dest->{charset}); 
	open(TMPHDR, "> $tmphdr") or die "$!"; 
	print TMPHDR $header; 
	close TMPHDR; 
 
	# Read in the header as the new head. 
	$top->head(MIME::Head->from_file($tmphdr)) or die "$!"; 
	$head = $top->head; 
	unlink $tmphdr or die "$!"; 
    } 
 
 
    # Convert the message body and parts if "Content-Type: text/plain" 
    my $body = $top->bodyhandle; 
    recode_body($top) if defined($body); 
    $sigstripper = ""; 
    my $num_parts = $top->parts; 
    my $i; 
    for ($i = 0; $i < $num_parts; $i++) { 
	recode_body($top->parts($i)); 
    } 
 
} 

#
#out put the message 
#
my $recvdate=gmtime();
my $sender= $head->get('Return-Path',0);
chop($sender);
$sender=~ s/[<>]//g;
printf("From %s %s\n",$sender,$recvdate);
$top->print(\*STDOUT); 
printf("\n");
 
# Clean up 
$top->purge; 
 
# 
# Subroutines 
# 
 
sub recode_body($) { 
    my $entity = shift; 
    my $head = $entity->head; 
    my $body = $entity->bodyhandle; 
    my $mime_type = $head->mime_type; 
    my $charset = 'us-ascii'; 
    if (lc($mime_type) eq "text/plain" and defined($body)) { 
 
	# Convert the body to Big5/GB and write to a file 
	open(TMPBDY, "$sigstripper| $dest->{converter} > $tmpbdy") or die "$!"; 
	print TMPBDY $body->as_lines; 
	close TMPBDY; 
 
	# Read in the body 
	my $IO = $body->open("w") or die "open body: $!"; 
	open(TMPBDY, "< $tmpbdy") or die "$!"; 
	while (<TMPBDY>) { 
	    $IO->print($_); 
	} 
	close TMPBDY; 
	$IO->close or die "close I/O handle: $!"; 
 
	my $bodytext = $body->as_string; 
	$charset = $dest->{charset} if $bodytext =~ /[\x80-\xff]/; 
	$head->replace('Content-Type', $mime_type . '; charset=' . $charset); 
	$head->replace('Content-Transfer-Encoding', $encoding); 
 
	unlink $tmpbdy or die "$!"; 
    } 
} 

sub usage(){
	print "Usage: convmail.pl [-g|-b]\n";
	print "-g\tConvert mail to gb2312\n";
	print "-b\tConvert mail to big5\n";
	exit;
}