#!/usr/netmax/bin/perl use strict; local $::domail = 0; # Set this in case customer has not purchased any # downloadable items local $::mailto = ''; local $::ordernum = ''; local %::Licenses = (); local @::INPUT; ###### MAIN ###### while (my $line = <>) { chomp($line); push(@::INPUT,$line); } # Get the Email address,and order number &ParseEmail(); # If downloadables, generate licenses. &GenLicenses(); # Email all licenses back to purchaser &Respond(); ###### END MAIN ###### # Slurp the email into an array # This is acceptable, because the message is of a fixed (and small size) sub ParseEmail { my $in_items = 0; my @FIELDS; for (my $i=0;$i<@::INPUT;$i++) { # Remember the Order Number if ( (! $in_items) && ($::INPUT[$i] =~ /ORDER NUMBER:\s+(.*)/) ) { $::order_num = $1; } # Remember the email address if ( (! $in_items) && ($::INPUT[$i] =~ /^\s+Email address:\s+(.*)/) ) { ($::mailto = $1 ) =~ s/\s+.*//; $::mailto =~ s/\@/\\\@/g; } # Skip to line items if ($::INPUT[$i] =~ /^Quan\s+Item No\.\s+Description/) { $in_items = 1; my @COUNTS = split(/\s+/,$::INPUT[$i+1]); foreach my $count (@COUNTS) { push(@FIELDS,length($count)); } } elsif ($::INPUT[$i] =~ /^\s+SUBTOTAL/) { # Stop reading if line matches ^\s+SUBTOTAL $in_items = 0; } if ($in_items) { unless ($::INPUT[$i] =~ /^-+/) { if ($::INPUT[$i] =~ /^\s*\d+\s+/) { my $quantity = $::INPUT[$i]; my $sku= $::INPUT[$i]; my $description = $::INPUT[$i]; $quantity = substr($quantity,0,$FIELDS[0]); $sku = substr($sku,$FIELDS[0],$FIELDS[1]); $description = substr($description,$FIELDS[0]+$FIELDS[1],$FIELDS[2]); $sku =~ s/^\s+//g; $sku =~ s/\s+$//g; $quantity =~ s/^\s+//g; $quantity =~ s/\s+$//g; $description =~ s/^\s+//g; $description =~ s/\s+$//g; if ($quantity ne '' ){ if ($sku ne '' ) { $::domail = 1; $::Licenses{$sku}->{Quantity} = $quantity; } if ($description ne '') { $::Licenses{$sku}->{Description} = $description; } $::Licenses{$sku} } } } } } }; sub GenLicenses { # If no downloadable skus, do nothing. unless ($::domail) { return; } # Dummy routine, always returns a unique value, the date # You will need to replace this routine with something that will # generate valid licenses for your downloadable foreach my $product (keys %::Licenses) { my $quantity = $::Licenses{$product}->{Quantity}; for (my $i=$quantity;$i>0;$i--) { my $license = scalar(time()); push(@{$::Licenses{$product}->{Licenses}},$license); sleep 1; } } return; } # Send customer email with licenses sub Respond { # If no downloadable skus, do nothing. unless ($::domail) { return; } open (MAIL, "|/bin/mail -s 'Your Online Order has been processed.' $::mailto"); (my $message =<{Licenses}}) { print MAIL "$sku\t".$::Licenses{$sku}->{Description}.":\t".$license."\n"; } } close(MAIL); }