1 |
dpavlin |
1.1 |
#!/usr/bin/perl -w |
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 Digest::MD5 qw(md5_hex); |
12 |
|
|
use MIME::Parser; |
13 |
|
|
use SAP::Rfc; |
14 |
|
|
|
15 |
|
|
# configuration |
16 |
|
|
|
17 |
|
|
# directory in which attachemnts will be left |
18 |
|
|
my $outdir = "/var/autofs/data/export_sigma"; |
19 |
|
|
# temporary directory (ON SAME FILESYSTEM AS $outdir -- we use rename!) |
20 |
|
|
# for extraction of *ALL* attachements |
21 |
|
|
my $msgdir = "/var/autofs/data/export_sigma/mime$$"; # temp |
22 |
|
|
my $log = "/data/sap_sigma/orders.log"; |
23 |
|
|
|
24 |
|
|
my $rfc = new SAP::Rfc( |
25 |
|
|
ASHOST => 'cipkg', |
26 |
|
|
USER => 'rfctest', |
27 |
|
|
PASSWD => 'dinamo', |
28 |
|
|
LANG => 'HR', |
29 |
|
|
CLIENT => '200', |
30 |
|
|
SYSNR => '20', |
31 |
|
|
TRACE => '0' |
32 |
|
|
) || die "new: $!"; |
33 |
|
|
|
34 |
|
|
my $it = $rfc->discover("Z_ZDMM0123_SIGMA_RFC") || die "discover: $!"; |
35 |
|
|
|
36 |
|
|
$it->FILEPATH( "ZSIGMA" ); |
37 |
|
|
$it->PORT( "ZIDOC" ); |
38 |
|
|
|
39 |
|
|
# no user editable content beyond this point |
40 |
|
|
|
41 |
|
|
umask 022; # world readable |
42 |
|
|
|
43 |
|
|
open(LOG,">> $log") || warn "log $log: $!"; |
44 |
|
|
|
45 |
|
|
$rfc->is_connected || die "not connected"; |
46 |
|
|
|
47 |
|
|
#------------------------------------------------------------ |
48 |
|
|
# dump_entity - dump an entity's file info |
49 |
|
|
#------------------------------------------------------------ |
50 |
|
|
sub dump_entity { |
51 |
|
|
my $ent = shift; |
52 |
|
|
my @parts = $ent->parts; |
53 |
|
|
|
54 |
|
|
if (@parts) { # multipart... |
55 |
|
|
map { dump_entity($_) } @parts; |
56 |
|
|
} else { # single part... |
57 |
|
|
# print " Part: ", $ent->bodyhandle->path, |
58 |
|
|
# " (", scalar($ent->head->mime_type), ")\n"; |
59 |
|
|
if ($ent->head->mime_type =~ m,text/plain,i) { |
60 |
|
|
# open(I,$ent->bodyhandle->path) || die "$ent->bodyhandle->path: $!"; |
61 |
|
|
# while(<I>) { print LOG $_; } |
62 |
|
|
# close(I); |
63 |
|
|
unlink $ent->bodyhandle->path; |
64 |
|
|
} else { |
65 |
|
|
my $file=$ent->bodyhandle->path; |
66 |
|
|
my $new=$file; |
67 |
|
|
$new=~s/ +/_/g; |
68 |
|
|
$new=~s/^.*\/([^\/]+)$/$1/g; |
69 |
|
|
$new=~s/[^a-zA-Z._0-9]//g; |
70 |
|
|
rename $file,"$outdir/$new" || die "move $file -> $outdir/$new: $!"; |
71 |
|
|
print LOG scalar localtime," $new\n"; |
72 |
|
|
|
73 |
|
|
# now, call SAP rfc |
74 |
|
|
|
75 |
|
|
$it->FILENAME( "$new" ); |
76 |
|
|
$rfc->callrfc( $it ); |
77 |
|
|
} |
78 |
|
|
} |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
$rfc->close(); |
82 |
|
|
|
83 |
|
|
#------------------------------------------------------------ |
84 |
|
|
# main |
85 |
|
|
#------------------------------------------------------------ |
86 |
|
|
sub main { |
87 |
|
|
my $file; |
88 |
|
|
my $entity; |
89 |
|
|
|
90 |
|
|
mkdir $msgdir,0755 || die "can't create $msgdir: $!"; |
91 |
|
|
die "problems with creation of directory $msgdir: $!" if (! -w $msgdir); |
92 |
|
|
|
93 |
|
|
my $parser = new MIME::Parser; |
94 |
|
|
### $parser->parse_nested_messages('REPLACE'); |
95 |
|
|
|
96 |
|
|
$parser->output_dir($msgdir); |
97 |
|
|
|
98 |
|
|
open FILE, "-" or die "couldn't open $file"; |
99 |
|
|
$entity = $parser->read(\*FILE) or |
100 |
|
|
print STDERR "Couldn't parse MIME in $file; continuing...\n"; |
101 |
|
|
close FILE; |
102 |
|
|
|
103 |
|
|
dump_entity($entity) if $entity; |
104 |
|
|
### $entity->dump_skeleton if $entity; |
105 |
|
|
|
106 |
|
|
rmdir $msgdir || die "can't remove $msgdir: $!"; |
107 |
|
|
|
108 |
|
|
1; |
109 |
|
|
} |
110 |
|
|
|
111 |
|
|
exit (&main ? 0 : -1); |
112 |
|
|
#------------------------------------------------------------ |
113 |
|
|
1; |
114 |
|
|
|