-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_tracking_number
executable file
·71 lines (60 loc) · 1.62 KB
/
extract_tracking_number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/perl -w
#
# extract tracking number from e-mail, and invoke "ips" to add it to tracking database
# Started by Matija Nalis <mnalis-perl@voyager.hr> 2010-12-26, GPLv3+
#
use strict;
use MIME::Base64;
my $DEBUG = $ENV{DEBUG} || 0;
my %found=();
my $full_decoded='';
umask 002;
sub found_tracking($) {
my ($tracking) = @_;
print "Found Tracking# $tracking\n" if $DEBUG > 1;
$found{$tracking}++;
return '';
}
# checks if given line contains tracking number
# example tracking numbers: EA123456789HR, RT072868631HK, RA004359364CN
sub check_tracking($) {
my ($line) = @_;
$line =~ s/\b([A-Z]{2}[A-Z0-9]\d{8}[A-Z]{2})\b/found_tracking($1)/ge;
return undef;
}
my $header = 1;
my $from_continue = 0;
my $sender = '';
while (<STDIN>) {
print "line : $_" if $DEBUG > 2;
if ($header and !$sender) {
$header = 0 if /^\s*$/; # detect end of headers
if ($from_continue) {
$from_continue = 0;
print "Continuation line is: $_" if $DEBUG > 1;
if (/^\s.*?<(.+?)>/) {
$sender = $1;
print "Found sender(2): <$sender>\n" if $DEBUG > 1;
}
}
if (/^From:\s/) {
if (/^From:\s.*<(.+?)>/) {
$sender = $1;
print "Found sender: <$sender>\n" if $DEBUG > 1;
} else {
$from_continue = 1;
print "Found From: continuation\n" if $DEBUG > 1;
}
}
}
check_tracking ($_);
if (m!^([A-Za-z0-9+/=]{1,76})\s*$!) { # detected Base64
$full_decoded .= decode_base64($1);
}
}
check_tracking($full_decoded) if $full_decoded; # if it was base64 MIME e-mail...
foreach my $tn (keys %found) {
print "Found key: $tn -- $found{$tn} times (sender: $sender)\n" if $DEBUG > 0;
system 'ips', $tn, '', $sender;
}
exit 0;