1 |
#!/usr/bin/suidperl |
2 |
|
3 |
# read e-mail from stdit (pipe from /etc/aliases) and call SAP rfc function |
4 |
# |
5 |
# 2002-11-18 Dobrica Pavlinusic <dpavlin@pliva.hr> |
6 |
|
7 |
require 5.001; |
8 |
|
9 |
use strict; |
10 |
use vars qw($Msgno); |
11 |
use SAP::Rfc; |
12 |
use Digest::MD5 qw(md5_hex); |
13 |
use MIME::Parser; |
14 |
use XML::Simple; |
15 |
use Data::Dumper; |
16 |
|
17 |
# read configuration from xml file |
18 |
my $config = XMLin(); |
19 |
|
20 |
# directory in which attachemnts will be left |
21 |
my $outdir = $config->{outdir} || die "config: on <outdir> defined"; |
22 |
# temporary directory (ON SAME FILESYSTEM AS $outdir -- we use rename!) |
23 |
# for extraction of *ALL* attachements |
24 |
my $msgdir = $config->{msgdir} || die "config: no <msgdir> defined"; |
25 |
$msgdir .= "$$"; # append PID to make it unique |
26 |
my $log = $config->{log} || die "config: no <log> defined"; |
27 |
|
28 |
# open log and redirect die to it... |
29 |
open(LOG,">> $log") || warn "open log $log: $!"; |
30 |
local $SIG{__DIE__} = sub { print LOG $_[0] ; die $_[0] }; |
31 |
|
32 |
umask 022; # world readable |
33 |
|
34 |
|
35 |
#------------------------------------------------------------ |
36 |
# dump_entity - dump an entity's file info |
37 |
#------------------------------------------------------------ |
38 |
sub dump_entity { |
39 |
my $ent = shift; |
40 |
my @parts = $ent->parts; |
41 |
|
42 |
if (@parts) { # multipart... |
43 |
map { dump_entity($_) } @parts; |
44 |
} else { # single part... |
45 |
# print " Part: ", $ent->bodyhandle->path, |
46 |
# " (", scalar($ent->head->mime_type), ")\n"; |
47 |
if ($ent->head->mime_type =~ m,text/plain,i) { |
48 |
# open(I,$ent->bodyhandle->path) || die "$ent->bodyhandle->path: $!"; |
49 |
# while(<I>) { print LOG $_; } |
50 |
# close(I); |
51 |
unlink $ent->bodyhandle->path; |
52 |
} else { |
53 |
my $file=$ent->bodyhandle->path; |
54 |
my $new=$file; |
55 |
$new=~s/ +/_/g; |
56 |
$new=~s/^.*\/([^\/]+)$/$1/g; |
57 |
$new=~s/[^a-zA-Z._0-9]//g; |
58 |
|
59 |
# never try to overwrite file |
60 |
my $suffix = ""; |
61 |
while (-e "$outdir/$new$suffix") { $suffix++; } |
62 |
$new .= $suffix; |
63 |
|
64 |
rename $file,"$outdir/$new" || die "move $file -> $outdir/$new: $!"; |
65 |
print LOG scalar localtime," $new\n"; |
66 |
|
67 |
# now, call SAP rfc |
68 |
|
69 |
# init SAP rfc |
70 |
# |
71 |
my $rfc = new SAP::Rfc( |
72 |
ASHOST => $config->{sap}->{ashost}, |
73 |
USER => $config->{sap}->{user}, |
74 |
PASSWD => $config->{sap}->{passwd}, |
75 |
LANG => $config->{sap}->{lang}, |
76 |
CLIENT => $config->{sap}->{client}, |
77 |
SYSNR => $config->{sap}->{sysnr}, |
78 |
TRACE => $config->{sap}->{trace} |
79 |
) || die "new: $!"; |
80 |
|
81 |
$rfc->is_connected || die "SAP rfc: not connected"; |
82 |
|
83 |
my $it = $rfc->discover($config->{sap}->{discover}) || die "discover: $!"; |
84 |
|
85 |
foreach my $p ($config->{sap}->{params}) { |
86 |
foreach my $p_name (keys %{$p}) { |
87 |
$it->$p_name($p->{$p_name}); |
88 |
} |
89 |
} |
90 |
|
91 |
$it->FILENAME( $new ); |
92 |
|
93 |
$rfc->callrfc( $it ); |
94 |
|
95 |
$rfc->close(); |
96 |
} |
97 |
} |
98 |
} |
99 |
|
100 |
|
101 |
#------------------------------------------------------------ |
102 |
# main |
103 |
#------------------------------------------------------------ |
104 |
sub main { |
105 |
my $file; |
106 |
my $entity; |
107 |
|
108 |
mkdir $msgdir,0755 || die "can't create $msgdir: $!"; |
109 |
die "problems with creation of directory $msgdir: $!" if (! -w $msgdir); |
110 |
|
111 |
my $parser = new MIME::Parser; |
112 |
### $parser->parse_nested_messages('REPLACE'); |
113 |
|
114 |
$parser->output_dir($msgdir); |
115 |
|
116 |
open FILE, "-" or die "couldn't open $file"; |
117 |
$entity = $parser->read(\*FILE) or |
118 |
print STDERR "Couldn't parse MIME in $file; continuing...\n"; |
119 |
close FILE; |
120 |
|
121 |
dump_entity($entity) if $entity; |
122 |
### $entity->dump_skeleton if $entity; |
123 |
|
124 |
rmdir $msgdir || die "can't remove $msgdir: $!"; |
125 |
|
126 |
1; |
127 |
} |
128 |
|
129 |
exit (&main ? 0 : -1); |
130 |
#------------------------------------------------------------ |
131 |
1; |
132 |
|