Debian Codegraph @

ytnef/ytnefprocess

raw
1 #!/usr/bin/perl -w
2
3 use strict;
4 use MIME::Parser;
5 use MIME::Entity;
6 use Mail::Mailer;
7 use File::Path;
8 use Data::Dumper;
9
10 my $mail_dir = "$ENV{HOME}/.ytnef/$$";
11 my $reader = "/usr/bin/ytnef";
12 my $output_dir = "$mail_dir/output";
13
14 mkdir $mail_dir,0700;
15 mkdir $output_dir,0700;
16 my $parser = new MIME::Parser;
17 my $filer = new MIME::Parser::FileInto;
18 $filer->init($mail_dir);
19
20 $parser->filer($filer);
21 $parser->output_dir($mail_dir);
22
23 my $entity = $parser->parse( \*STDIN );
24
25 processParts($entity);
26
27 open(LOG, ">/dev/null");
28
29 ### Delete the tnef parts of the multipart message:
30 my @tab;
31 foreach my $e ($entity->parts) {
32 print LOG "working on entity:\n";
33 print LOG Dumper($e);
34 if ((defined $e->parts) && ($e->parts > 0)) {
35 print LOG "Found subparts\n";
36 foreach my $e1 ($e->parts) {
37 if ( $e1->mime_type !~ /ms-tnef/i ) {
38 print LOG "pushing entity:\n";
39 print LOG Dumper($e1);
40 push @tab,$e1;
41 }
42 }
43 } else {
44 if ( $e->mime_type !~ /ms-tnef/i ) {
45 print LOG "pushing entity:\n";
46 print LOG Dumper($e);
47 push @tab,$e;
48 }
49 }
50 }
51 $entity->parts(\@tab);
52
53 close(LOG);
54
55 $entity->print( \*STDOUT );
56
57 $entity->purge;
58 $parser->filer->purge;
59
60 rmtree($mail_dir);
61
62 sub processParts {
63 my $entity = shift;
64 if ($entity->parts) {
65 for my $part ($entity->parts) {
66 processParts($part);
67 }
68 } else {
69 if ( $entity->mime_type =~ /ms-tnef/i ) {
70 if (my $bh = $entity->bodyhandle) {
71 my $io = $bh->open("r") || die "Unable to open msg body";
72 open(FPTR, ">$output_dir/winmail.dat");
73 while (defined($_ = $io->getline)) {
74 print FPTR $_;
75 }
76 close(FPTR);
77 $io->close;
78
79 `$reader -f $output_dir +F $output_dir/winmail.dat`;
80 unlink("$output_dir/winmail.dat");
81
82 opendir(DIR, $output_dir) || die "Can't open directory $output_dir: $!";
83 my @files = map { $output_dir."/".$_ }
84 grep { !/^\./ }
85 readdir DIR;
86 closedir DIR;
87
88 for my $file ( @files ) {
89 my $mimetype = "application/binary";
90 my $disposition = "attachment";
91
92 $mimetype = "text/calendar" if ($file =~ m/\.vcf$/);
93 $mimetype = "text/x-vcard" if ($file =~ m/\.vcard$/);
94 $mimetype = "text/html" if ($file =~ m/\.htm[l]*$/);
95
96 $disposition = "inline" if ($file =~ m/\.vcf$/);
97 $disposition = "inline" if ($file =~ m/\.vcard$/);
98 #$disposition = "inline" if ($file =~ m/\.htm[l]*$/);
99
100 if ($mimetype eq "application/binary") {
101 my $qfile = quotemeta $file;
102 my $filetype = `file -bi $qfile`;
103 chomp $filetype;
104 $mimetype = $filetype if ($filetype ne "");
105 }
106 $entity->attach(
107 Path => $file,
108 Type => $mimetype,
109 Disposition => $disposition,
110 Encoding => "-SUGGEST",
111 Top => 0);
112 }
113 }
114 }
115 }
116 }