--- mail2sap.pl 2002/11/18 14:23:39 1.1 +++ mail2sap.pl 2002/11/19 10:43:46 1.5 @@ -1,4 +1,4 @@ -#!/usr/bin/perl -w +#!/usr/bin/suidperl -w # read e-mail from stdit (pipe from /etc/aliases) and call SAP rfc function # @@ -8,41 +8,29 @@ use strict; use vars qw($Msgno); +use SAP::Rfc; use Digest::MD5 qw(md5_hex); use MIME::Parser; -use SAP::Rfc; +use XML::Simple; +use Data::Dumper; -# configuration +# read configuration from xml file +my $config = XMLin(); # directory in which attachemnts will be left -my $outdir = "/var/autofs/data/export_sigma"; +my $outdir = $config->{outdir} || die "config: on defined"; # temporary directory (ON SAME FILESYSTEM AS $outdir -- we use rename!) # for extraction of *ALL* attachements -my $msgdir = "/var/autofs/data/export_sigma/mime$$"; # temp -my $log = "/data/sap_sigma/orders.log"; - -my $rfc = new SAP::Rfc( - ASHOST => 'cipkg', - USER => 'rfctest', - PASSWD => 'dinamo', - LANG => 'HR', - CLIENT => '200', - SYSNR => '20', - TRACE => '0' - ) || die "new: $!"; - -my $it = $rfc->discover("Z_ZDMM0123_SIGMA_RFC") || die "discover: $!"; - -$it->FILEPATH( "ZSIGMA" ); -$it->PORT( "ZIDOC" ); - -# no user editable content beyond this point +my $msgdir = $config->{msgdir} || die "config: no defined"; +$msgdir .= "$$"; # append PID to make it unique +my $log = $config->{log} || die "config: no defined"; + +# open log and redirect die to it... +open(LOG,">> $log") || warn "open log $log: $!"; +local $SIG{__DIE__} = sub { print LOG $_[0] ; die $_[0] }; umask 022; # world readable -open(LOG,">> $log") || warn "log $log: $!"; - -$rfc->is_connected || die "not connected"; #------------------------------------------------------------ # dump_entity - dump an entity's file info @@ -67,18 +55,44 @@ $new=~s/ +/_/g; $new=~s/^.*\/([^\/]+)$/$1/g; $new=~s/[^a-zA-Z._0-9]//g; + + # never try to overwrite file + my $suffix = ""; + while (-e "$outdir/$new$suffix") { $suffix++; } + $new .= $suffix; + rename $file,"$outdir/$new" || die "move $file -> $outdir/$new: $!"; print LOG scalar localtime," $new\n"; # now, call SAP rfc - $it->FILENAME( "$new" ); + # init SAP rfc + # + my $rfc = new SAP::Rfc( + ASHOST => $config->{sap}->{ashost}, + USER => $config->{sap}->{user}, + PASSWD => $config->{sap}->{passwd}, + LANG => $config->{sap}->{lang}, + CLIENT => $config->{sap}->{client}, + SYSNR => $config->{sap}->{sysnr}, + TRACE => $config->{sap}->{trace} + ) || die "new: $!"; + + $rfc->is_connected || die "SAP rfc: not connected"; + + my $it = $rfc->discover("Z_ZDMM0123_SIGMA_RFC") || die "discover: $!"; + + $it->FILEPATH( "ZSIGMA" ); + $it->FILENAME( $new ); + $it->PORT( "ZIDOC" ); + $rfc->callrfc( $it ); + + $rfc->close(); } } } -$rfc->close(); #------------------------------------------------------------ # main