#!/usr/local/bin/perl
########################################
# VERSION: 1.2
# DATE: 01/16/2002
# This script accepts a filename
# and a configuration file name as
# arguments, parses the file, and sends
# the URL-encoded parameters to the URL
# mentioned in the configuration file
########################################
use strict;
########VARIABLES SECTION STARTS########
# This is a name of the config file
my %second_url_configs = ();
$second_url_configs{'second_url_value'} = '
if($ARGV[1] eq 'lost'){
$second_url_configs{'second_url_value'} = '}
if($ARGV[1] eq 'batch'){
$second_url_configs{'second_url_value'} = '}
$second_url_configs{'second_url_enabled'} = 1;
$second_url_configs{'second_url_emails'} = 'aidin@inkjetorder.com';
my $directory = $ARGV[0];
alert("Invalid order directory specified on command line: $directory" unless ((-r $directory) && (-d $directory));
opendir(DIR, $directory) || alert("Can't open directory - $directory: $!"
my @files = grep { /$\.csv/ && -f "$directory/$_" } readdir(DIR);
closedir DIR;
foreach my $file (@files){
# This is a name of a CSV-file to be parsed and sent
my $file_name = "$directory/$file";
print("Processing: $file_name\n"
alert("Can't open/read CSV data file: $file_name" unless (-r $file_name);
my ($data, $dataend, $query_string, $user_agent, $request, $res, $vhost);
my $secondary_url = $second_url_configs{'second_url_value'};
my $CCN = "";
my @strings = ();
my @fields = ();
my @data;
my @dataend;
#
########VARIABLES SECTION ENDS##########
########MAIN PROGRAM SECTION STARTS#####
#
if($second_url_configs{'second_url_enabled'}){
use LWP::UserAgent;
use HTTP::Request;
open(DATA, "<$file_name" || alert("Can't open/read CSV data file: $file_name"
undef $/;
$data = <DATA>; # read in whole file
$/ = "\n"; # back to normal input delim
close(DATA);
@strings = split("\n", $data);
chomp $strings[0];
if($strings[0] =~ /^\"/){
#Regular orders section...
$strings[0] =~ s/^\"//;
$strings[0] =~ s/\"$//;
@fields = split(/\",\"/, $strings[0]);
for(my $i=0; $i<@fields; $i++){
# $fields[$i] =~ s/^\d\d?\-//;
# $fields[$i] =~ s/\.//g;
$fields[$i] =~ s/ /_/g;
}
$user_agent = new LWP::UserAgent;
for(my $i=1; $i<@strings; $i++){
chomp $strings[$i];
$strings[$i] =~ s/^\"//;
$strings[$i] =~ s/\"$//;
@data = split(/\",\"/, $strings[$i]);
if(@data){
$query_string = "";
for(my $j=0; $j<@fields; $j++){
$data[$j] = urlencode($data[$j]);
$query_string .= "$fields[$j]=$data[$j]&";
}
if($CCN){ $query_string .= "CCN=$CCN"; }
$query_string = ($secondary_url =~ /\?/)?
"$secondary_url&$query_string":
"$secondary_url?$query_string";
$request = HTTP::Request->new(GET => $query_string);
$res = $user_agent->request($request);
print "In1's response:\n\n".$res->content."\n\n----------------------------------------\n";
alert("This URL failed to work: $query_string." unless($res->is_success);
}
}
}elsif($strings[0] !~ /\"$/){
#Wrong CSV-format...
alert("Wrong CSV format."
}else{
#Lost or Batch orders section...
$user_agent = new LWP::UserAgent;
for(my $i=0; $i<@strings; $i++){
chomp $strings[$i];
$strings[$i] =~ s/^(.*?)\.\"//;
$vhost = $1;
$strings[$i] =~ s/\"$//;
@data = split(/\",\"/, $strings[$i]);
my $ct_fields = @data;
if($ct_fields < 50)
{
if($ct_fields == 45)
{
@dataend = splice(@data,45);
push(@data,@dataend);
$ct_fields = @data;
}
}
if(@data && $ct_fields > 49){
$query_string = "host=".urlencode($vhost)."&";
for(my $j=0; $j<@data; $j++){
$data[$j] = urlencode($data[$j]);
$query_string .= "field$j=$data[$j]&";
}
if($CCN){ $query_string .= "CCN=$CCN"; }
$query_string = ($secondary_url =~ /\?/)?
"$secondary_url&$query_string":
"$secondary_url?$query_string";
# print "\nquery_string=$query_string\n";
$request = HTTP::Request->new(GET => $query_string);
$res = $user_agent->request($request);
print "In1's response to lost or batch order:\n\n".$res->content."\n\n----------------------------------------\n";
alert("This URL failed to work: $query_string." unless($res->is_success);
}
}
}
}
}
#
########MAIN PROGRAM SECTION ENDS#######
########SUBROUTINES SECTION STARTS######
#
sub urlencode {
my($val)=@_;
$val=~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
return $val;
}
sub alert {
my ($message) = @_;
my $subject = "Secondary URL mechanism ERROR";
my @addresses = split(/\,/, $second_url_configs{'second_url_emails'});
foreach my $address (@addresses){
&send_mail($addresses[0], $address, $subject, $message."\n\nSecondary URL mechanism was aborted for the order.\n"
}
exit;
}
sub send_mail {
my ($from, $to, $subject, $message) = @_;
my ($found, $mail_prog);
if(-e "/usr/bin/sendmail"{
$found = 1;
$mail_prog = "/usr/bin/sendmail -t";
}elsif(-e "/usr/sbin/sendmail"{
$found = 1;
$mail_prog = "/usr/sbin/sendmail -t";
}elsif(-e "/sbin/sendmail"{
$found = 1;
$mail_prog = "/sbin/sendmail -t";
}elsif(-e "/bin/sendmail"{
$found = 1;
$mail_prog = "/bin/sendmail -t";
}elsif(-e "/usr/lib/sendmail"{
$found = 1;
$mail_prog = "/usr/lib/sendmail -t";
}
if(!$found){ die "Can't find a mailing program"; }
open (MAIL, "|$mail_prog" || die "Could Not Open Mail Program";
print MAIL <<__END_OF_MAIL__;
To: $to
From: $from
Subject: $subject
$message
__END_OF_MAIL__
close (MAIL);
}
#
########SUBROUTINES SECTION ENDS########
1;
########################################
# VERSION: 1.2
# DATE: 01/16/2002
# This script accepts a filename
# and a configuration file name as
# arguments, parses the file, and sends
# the URL-encoded parameters to the URL
# mentioned in the configuration file
########################################
use strict;
########VARIABLES SECTION STARTS########
# This is a name of the config file
my %second_url_configs = ();
$second_url_configs{'second_url_value'} = '
if($ARGV[1] eq 'lost'){
$second_url_configs{'second_url_value'} = '}
if($ARGV[1] eq 'batch'){
$second_url_configs{'second_url_value'} = '}
$second_url_configs{'second_url_enabled'} = 1;
$second_url_configs{'second_url_emails'} = 'aidin@inkjetorder.com';
my $directory = $ARGV[0];
alert("Invalid order directory specified on command line: $directory" unless ((-r $directory) && (-d $directory));
opendir(DIR, $directory) || alert("Can't open directory - $directory: $!"
my @files = grep { /$\.csv/ && -f "$directory/$_" } readdir(DIR);
closedir DIR;
foreach my $file (@files){
# This is a name of a CSV-file to be parsed and sent
my $file_name = "$directory/$file";
print("Processing: $file_name\n"
alert("Can't open/read CSV data file: $file_name" unless (-r $file_name);
my ($data, $dataend, $query_string, $user_agent, $request, $res, $vhost);
my $secondary_url = $second_url_configs{'second_url_value'};
my $CCN = "";
my @strings = ();
my @fields = ();
my @data;
my @dataend;
#
########VARIABLES SECTION ENDS##########
########MAIN PROGRAM SECTION STARTS#####
#
if($second_url_configs{'second_url_enabled'}){
use LWP::UserAgent;
use HTTP::Request;
open(DATA, "<$file_name" || alert("Can't open/read CSV data file: $file_name"
undef $/;
$data = <DATA>; # read in whole file
$/ = "\n"; # back to normal input delim
close(DATA);
@strings = split("\n", $data);
chomp $strings[0];
if($strings[0] =~ /^\"/){
#Regular orders section...
$strings[0] =~ s/^\"//;
$strings[0] =~ s/\"$//;
@fields = split(/\",\"/, $strings[0]);
for(my $i=0; $i<@fields; $i++){
# $fields[$i] =~ s/^\d\d?\-//;
# $fields[$i] =~ s/\.//g;
$fields[$i] =~ s/ /_/g;
}
$user_agent = new LWP::UserAgent;
for(my $i=1; $i<@strings; $i++){
chomp $strings[$i];
$strings[$i] =~ s/^\"//;
$strings[$i] =~ s/\"$//;
@data = split(/\",\"/, $strings[$i]);
if(@data){
$query_string = "";
for(my $j=0; $j<@fields; $j++){
$data[$j] = urlencode($data[$j]);
$query_string .= "$fields[$j]=$data[$j]&";
}
if($CCN){ $query_string .= "CCN=$CCN"; }
$query_string = ($secondary_url =~ /\?/)?
"$secondary_url&$query_string":
"$secondary_url?$query_string";
$request = HTTP::Request->new(GET => $query_string);
$res = $user_agent->request($request);
print "In1's response:\n\n".$res->content."\n\n----------------------------------------\n";
alert("This URL failed to work: $query_string." unless($res->is_success);
}
}
}elsif($strings[0] !~ /\"$/){
#Wrong CSV-format...
alert("Wrong CSV format."
}else{
#Lost or Batch orders section...
$user_agent = new LWP::UserAgent;
for(my $i=0; $i<@strings; $i++){
chomp $strings[$i];
$strings[$i] =~ s/^(.*?)\.\"//;
$vhost = $1;
$strings[$i] =~ s/\"$//;
@data = split(/\",\"/, $strings[$i]);
my $ct_fields = @data;
if($ct_fields < 50)
{
if($ct_fields == 45)
{
@dataend = splice(@data,45);
push(@data,@dataend);
$ct_fields = @data;
}
}
if(@data && $ct_fields > 49){
$query_string = "host=".urlencode($vhost)."&";
for(my $j=0; $j<@data; $j++){
$data[$j] = urlencode($data[$j]);
$query_string .= "field$j=$data[$j]&";
}
if($CCN){ $query_string .= "CCN=$CCN"; }
$query_string = ($secondary_url =~ /\?/)?
"$secondary_url&$query_string":
"$secondary_url?$query_string";
# print "\nquery_string=$query_string\n";
$request = HTTP::Request->new(GET => $query_string);
$res = $user_agent->request($request);
print "In1's response to lost or batch order:\n\n".$res->content."\n\n----------------------------------------\n";
alert("This URL failed to work: $query_string." unless($res->is_success);
}
}
}
}
}
#
########MAIN PROGRAM SECTION ENDS#######
########SUBROUTINES SECTION STARTS######
#
sub urlencode {
my($val)=@_;
$val=~s/([^a-zA-Z0-9_\-.])/uc sprintf("%%%02x",ord($1))/eg;
return $val;
}
sub alert {
my ($message) = @_;
my $subject = "Secondary URL mechanism ERROR";
my @addresses = split(/\,/, $second_url_configs{'second_url_emails'});
foreach my $address (@addresses){
&send_mail($addresses[0], $address, $subject, $message."\n\nSecondary URL mechanism was aborted for the order.\n"
}
exit;
}
sub send_mail {
my ($from, $to, $subject, $message) = @_;
my ($found, $mail_prog);
if(-e "/usr/bin/sendmail"{
$found = 1;
$mail_prog = "/usr/bin/sendmail -t";
}elsif(-e "/usr/sbin/sendmail"{
$found = 1;
$mail_prog = "/usr/sbin/sendmail -t";
}elsif(-e "/sbin/sendmail"{
$found = 1;
$mail_prog = "/sbin/sendmail -t";
}elsif(-e "/bin/sendmail"{
$found = 1;
$mail_prog = "/bin/sendmail -t";
}elsif(-e "/usr/lib/sendmail"{
$found = 1;
$mail_prog = "/usr/lib/sendmail -t";
}
if(!$found){ die "Can't find a mailing program"; }
open (MAIL, "|$mail_prog" || die "Could Not Open Mail Program";
print MAIL <<__END_OF_MAIL__;
To: $to
From: $from
Subject: $subject
$message
__END_OF_MAIL__
close (MAIL);
}
#
########SUBROUTINES SECTION ENDS########
1;