This commit is contained in:
Nick Bebout 2011-03-12 02:45:04 +00:00
parent 3f8607bd96
commit d88bf4b46a
90 changed files with 13227 additions and 504 deletions

13
paypal_reply/8859_utf8 Executable file
View file

@ -0,0 +1,13 @@
#!/usr/bin/perl -w
# $Id: 8859_utf8,v 1.1 2010/10/01 13:00:09 gilles Exp gilles $
use Unicode::MapUTF8 qw(to_utf8 from_utf8 utf8_supported_charset);
die unless (utf8_supported_charset('ISO-8859-1'));
while (<>) {
print to_utf8({ -string => $_, -charset => 'ISO-8859-1' });
}

6
paypal_reply/TODO Normal file
View file

@ -0,0 +1,6 @@
Rewrite all with less scripts
use Email::Simple module

140
paypal_reply/paypal_build_reply Executable file
View file

@ -0,0 +1,140 @@
#!/usr/bin/perl
# $Id: paypal_build_reply,v 1.8 2010/12/29 23:51:23 gilles Exp gilles $
use warnings;
use strict;
my ($msg_id_file, $msg_id);
my ($amount, $name, $email);
my (
$buyer, $object, $support, $description,
$url_source, $url_exe, $url, $release,
);
$msg_id_file = $ARGV[1];
$msg_id = firstline($msg_id_file);
while(<>) {
next if ( ! /^Vous avez re.*paiement d'un montant de (.*) de la part de (.*) \((.*)\)/);
($amount, $name, $email) = ($1, $2, $3);
last;
}
$url_source = firstline('/g/var/paypal_reply/url_source');
$url_exe = firstline('/g/var/paypal_reply/url_exe');
$release = firstline('/g/var/paypal_reply/url_release');
# source code wanted
if (('$35,00 USD' eq $amount) or ('$50,00 USD' eq $amount)) {
$object = 'imapsync source code';
$support = '';
$url = $url_source;
}
# win32 binary wanted
if (('$25,00 USD' eq $amount) or ('$15,00 USD' eq $amount)) {
$object = 'imapsync.exe binary';
$support = '';
$url = $url_exe;
}
# source code + technical support wanted
if ('$135,00 USD' eq $amount) {
$object = 'imapsync source code';
$support = "\nI'm ready to help you by email until success (I hope).\n";
$url = $url_source;
}
# win32 binary + technical support wanted
if ('$125,00 USD' eq $amount) {
$object = 'imapsync.exe binary';
$support = "\nI'm ready to help you by email until success (I hope).\n";
$url = $url_exe;
}
while(<>) {
next if ( ! /^Acheteur/ );
$buyer .= "===== Acheteur =====\n";
last;
}
while(<>) {
$buyer .= $_ if ( ! /^Instructions/ );
last if ( /^Instructions/ );
}
while(<>) {
next if ( ! /^Description :(.*)/ );
$description = "===== Details =====\n";
$description .= $_;
last;
}
while(<>) {
$description .= $_;
last if ( /^Paiement envoy/ );
}
my $address = 'gilles.lamiral@laposte.net';
my $address2 = 'gilles@lamiral.info';
my $rcstag = '$Id: paypal_build_reply,v 1.8 2010/12/29 23:51:23 gilles Exp gilles $';
my $message = <<EOM
X-Comment: $rcstag
In-Reply-To: $msg_id
From: Gilles LAMIRAL <$address>
To: <$email>
Bcc: Gilles LAMIRAL <$address>, <$address2>
Subject: [imapsync download] $object release $release [$email]
Hello $name,
You will find the latest $object release $release at the following link:
$url
I thank you for buying and using imapsync,
I wish you successful transfers!
$support
$buyer
$description
==== Vendeur ====
Gilles LAMIRAL
4 La Billais
35580 Baulon
FRANCE
Tel: +33 951 84 42 42
Mob: +33 620 79 76 06
Fax: +33 956 84 42 42
email: $address
--
Au revoir, 09 51 84 42 42
Gilles Lamiral. France, Baulon (35580) 06 20 79 76 06
EOM
;
=pod
=cut
print $message;
#print "[$amount] [$name] [$email] [$object]\n";
sub firstline {
# extract the first line of a file (without \n)
my($file) = @_;
my $line = "";
open FILE, $file or die("error [$file]: $! ");
chomp($line = <FILE>);
close FILE;
$line = ($line) ? $line: "error !EMPTY! [$file]";
return $line;
}

182
paypal_reply/paypal_functions Executable file
View file

@ -0,0 +1,182 @@
#!/bin/sh
# $Id: paypal_functions,v 1.10 2011/01/11 01:41:31 gilles Exp gilles $
paypal_prerequisites() {
perl -mMIME::Lite -e '' || echo 'sudo aptitude install libmime-lite-perl'
perl -mMIME::Parser -e '' || echo 'sudo aptitude install libmime-tools-perl'
perl -mUnicode::MapUTF8 -e '' || echo 'sudo aptitude install libunicode-maputf8-perl'
}
paypal_init_laposte() {
user=gilles.lamiral
passfile=/g/var/pass/secret.gilles_laposte
host=imap.laposte.net
tmpdir=/g/var/paypal_reply
folder=INBOX
}
paypal_init_petite() {
user=gilles@est.belle
passfile=/g/var/pass/secret.gilles_mbox
host=p
tmpdir=/g/var/paypal_reply
folder='INBOX.03_imapsync.imapsync_paypal'
}
paypal_init_petite_INBOX() {
user=gilles@est.belle
passfile=/g/var/pass/secret.gilles_mbox
host=p
tmpdir=/g/var/paypal_reply
folder='INBOX'
}
paypal_init_test() {
user=gilles@est.belle
passfile=/g/var/pass/secret.gilles_mbox
host=p
tmpdir=/g/var/paypal_reply_test
folder='INBOX.03_imapsync.imapsync_paypal'
}
get_mail() {
# creation des répertoires
mkdir -p $tmpdir/msg_in/
mkdir -p $tmpdir/msg_id/
(
cd $tmpdir/msg_in/
# recuperation des messages de la boite sans destruction des messages
# transférés
paypal_imapget --host $host --user $user --passfile $passfile \
--folder $folder
)
}
extract_mail() {
test -z "`ls $tmpdir/msg_in/`" && echo no mail && return
mkdir -p $tmpdir/msg_out/
(
cd $tmpdir/msg_out/
test -z "`ls .`" || rm -rf *_d
paypal_mimeexplode ../msg_in/*
)
#ls -d $tmpdir/msg_out/
}
extract_mail_test() {
test -z "`ls $tmpdir/msg_in/`" && echo no mail && return
mkdir -p $tmpdir/msg_out/
(
cd $tmpdir/msg_out/
test -z "`ls .`" || rm -rf *_d
paypal_mimeexplode ../msg_in/*
)
#ls -d $tmpdir/msg_out/
}
convert_utf8() {
test -z "`ls $tmpdir/msg_out/`" && echo no mail && return
mkdir -p $tmpdir/msg_out_utf8/
for f in $tmpdir/msg_out/*_d/*.txt; do
b=`basename "$f"`
d=`dirname "$f"`
bd=`basename "$d"`
d_utf8="$tmpdir/msg_out_utf8/$bd"
f_utf8="$d_utf8/$b"
test -d "$d_utf8" && continue
echo converting "$f" to "$f_utf8"
mkdir "$d_utf8"
8859_utf8 "$f" > "$f_utf8"
done
}
troncate_last_2_chars() {
length=`expr length "$1"`
length_2=`expr $length - 2`
expr substr "$1" 1 $length_2
}
build_reply() {
mkdir -p $tmpdir/msg_reply/
for f in $tmpdir/msg_out_utf8/*/*.txt; do
#echo "$f"
d=`dirname "$f"`
bd=`basename "$d"`
file_id=`troncate_last_2_chars $bd`
d_reply="$tmpdir/msg_reply/$file_id"
test -f "$d_reply/$file_id.txt" && continue
mkdir -p "$d_reply"
echo building "$d_reply/$file_id.txt"
paypal_build_reply "$f" "$tmpdir/msg_id/$file_id" > "$d_reply/$file_id.txt"
done
}
send_reply() {
mkdir -p $tmpdir/msg_sent/
for f in $tmpdir/msg_reply/*/*.txt; do
b=`basename "$f"`
d=`dirname "$f"`
bd=`basename "$d"`
d_sent="$tmpdir/msg_sent/$bd"
test -f "$d_sent/$b" && continue
mkdir -p "$d_sent"
test X"--send" = X"$1" && paypal_send --send "$f" && touch "$d_sent/$b"
test X"" = X"$1" && paypal_send "$f"
done
}
paypal_all() {
paypal_prerequisites
echo "Will get messages in $tmpdir/msg_in/"
get_mail
echo "Done get messages in $tmpdir/msg_in/"
echo "Will extract_mail in $tmpdir/msg_out/"
extract_mail
echo "Done extract_mail in $tmpdir/msg_out/"
echo "Will converting to utf8 in $tmpdir/msg_out_utf8/"
convert_utf8
echo "Done converting to utf8 in $tmpdir/msg_out_utf8/"
echo "Will build_reply in $tmpdir/msg_reply/"
build_reply
echo "Done build_reply in $tmpdir/msg_reply/"
echo "Will send_reply $@"
send_reply "$@"
echo "Done send_reply $@"
}
#echo 'paypal_reply_petite'
paypal_reply_petite() {
echo "Doing paypal_reply_petite"
echo paypal_init_petite
paypal_init_petite
paypal_all "$@"
echo paypal_init_petite_INBOX
paypal_init_petite_INBOX
paypal_all "$@"
echo "Done paypal_reply_petite"
}
#echo 'paypal_reply_laposte'
paypal_reply_laposte() {
echo "Doing paypal_reply_laposte"
echo paypal_init_laposte
paypal_init_laposte
paypal_all "$@"
echo "Done paypal_reply_laposte"
}

129
paypal_reply/paypal_imapget Executable file
View file

@ -0,0 +1,129 @@
#!/usr/bin/perl -w
# $Id: paypal_imapget,v 1.6 2010/12/29 23:50:45 gilles Exp gilles $
use Getopt::Long;
use Mail::IMAPClient;
use FileHandle;
my $host;
my $port = 143;
my $debugimap = 0;
my $debug = 0;
my $user;
my $password;
my $passfile;
my $folder = 'INBOX';
my $help;
my $numopt = scalar(@ARGV);
my $opt_ret = GetOptions(
"host=s" => \$host,
"user=s" => \$user,
"password=s" => \$password,
"passfile=s" => \$passfile,
"folder=s" => \$folder,
"help" => \$help,
"delete!" => \$delete,
"expunge!" => \$expunge,
"debugimap!" => \$debugimap,
"debug!" => \$debug,
);
usage() and exit if ($help or ! $numopt) ;
$password = (defined($passfile)) ? firstline ($passfile) : $password;
my $imap = Mail::IMAPClient->new();
$imap->Server($host);
$imap->Port($port);
$imap->Uid(1);
$imap->Peek(1);
$imap->Debug($debugimap);
$imap->connect()
or die "Can not open imap connection on [$host] with user [$user] : $@\n";
$imap->User($user);
$imap->Password($password);
$imap->login() or die "Error login : [$host] with user [$user] : $@";
$imap->select($folder) or die "Error select folder [$folder] host [$host] user [$user] : $@";
#my @uids = $imap->search('HEADER', 'SUBJECT',"=?windows-1252?Q?Avis_de_r=E9ception_d=27un_paiement?=");
#my @uids = $imap->search('HEADER', 'Sender','sendmail@paypal.com');
my @uids = $imap->search('TEXT', 'PP341');
print "Search: [@uids]\n";
foreach $msg (@uids) {
my $msg_id = $imap->get_header( $msg, "Message-Id" );
$debug and print "$msg_id\n";
my $msg_code = format_msg_id($msg_id);
my $file = "$msg_code";
if (-f $msg_code and -f "../msg_id/$msg_code") {
$debug and print "Already have $msg_code $msg\n";
next;
}
print "writing message $msg to $file\n";
unlink($file);
if ($imap->message_to_file($file, $msg)) {
$imap->delete_message($msg) if $delete;
$imap->expunge() if $expunge;
}else{
print "Error writing $file: $@\n";
}
write_to_file("../msg_id/$msg_code", $msg_id);
}
$imap->logout();
sub usage {
print <<EOF;
usage: $0 [options]
Several options are mandatory.
--host <string> : imap server. Mandatory.
--user <string> : user to login. Mandatory.
--password <string> : password for the user1. Mandatory.
--delete : mark messages well dumped as deleted
--expunge : expunge folder.
Example:
$0 \\
--host imap.troc.org --user foo --password secret
EOF
}
sub firstline {
# extract the first line of a file (without \n)
my($file) = @_;
my $line = "";
open FILE, $file or die("error [$file]: $! ");
chomp($line = <FILE>);
close FILE;
$line = ($line) ? $line: "error !EMPTY! [$file]";
return $line;
}
sub format_msg_id {
my $msg_id = shift;
$msg_id =~ tr/a-zA-Z0-9/_/cs;
$debug and print "$msg_id\n";
return($msg_id);
}
sub write_to_file {
my $file = shift;
my $string = shift;
$fh = FileHandle->new("> $file");
if (defined $fh) {
print $fh $string;
$fh->close;
}
}

187
paypal_reply/paypal_mimeexplode Executable file
View file

@ -0,0 +1,187 @@
#!/usr/bin/perl -w
# $Id: paypal_mimeexplode,v 1.1 2010/11/23 01:26:24 gilles Exp gilles $
=head1 NAME
mimeexplode - explode one or more MIME messages
=head1 SYNOPSIS
mimeexplode <mime-msg-file> <mime-msg-file> ...
someprocess | mimeexplode -
=head1 DESCRIPTION
Takes one or more files from the command line that contain MIME
messages, and explodes their contents out into subdirectories
of the current working directory. The subdirectories are
just called C<msg0>, C<msg1>, C<msg2>, etc. Existing directories are
skipped over.
The message information is output to the stdout, like this:
Message: msg3 (inputfile1.msg)
Part: msg3/filename-1.dat (text/plain)
Part: msg3/filename-2.dat (text/plain)
Message: msg5 (input-file2.msg)
Part: msg5/dir.gif (image/gif)
Part: msg5/face.jpg (image/jpeg)
Message: msg6 (infile3)
Part: msg6/filename-1.dat (text/plain)
This was written as an example of the MIME:: modules in the
MIME-parser package I wrote. It may prove useful as a quick-and-dirty
way of splitting a MIME message if you need to decode something, and
you don't have a MIME mail reader on hand.
=head1 COMMAND LINE OPTIONS
None yet.
=head1 AUTHOR
Eryq C<eryq@zeegee.com>, in a big hurry...
=cut
BEGIN { unshift @INC, ".." } # to test MIME:: stuff before installing it!
require 5.001;
use strict;
use Getopt::Long;
use vars qw($Msgno);
use MIME::Parser;
use Getopt::Std;
use File::Basename;
my $numopt = scalar(@ARGV);
my $help;
my $debug;
my $opt_ret = GetOptions(
"help" => \$help,
"debug!" => \$debug,
);
usage() and exit if ($help or ! $numopt) ;
sub usage {
print <<EOF;
Usage: $0 [options] email_1 email_2 ...
Options:
--help : print this message
--debug : verbose output
Example:
$0 email_1 email_2
EOF
}
#------------------------------------------------------------
# make_msg - make and return the name of a msgXXX directory
#------------------------------------------------------------
$Msgno = 1;
sub make_msg {
while (-d "msg$Msgno") {
++$Msgno;
die "self-imposed limit reached" if $Msgno == 256;
}
mkdir "msg$Msgno",0755 or die "couldn't make msg$Msgno: $!";
"msg$Msgno";
}
#------------------------------------------------------------
# make_msg_dir - make and return the name of a output directory
#------------------------------------------------------------
sub make_msg_dir {
my ($file) = @_;
if ("-" ne "$file") {
my $basefile = basename($file) . "_d";
-d $basefile and return($basefile);
mkdir $basefile or do {
warn "can not create directory $basefile: $!";
return undef;
};
return($basefile);
}else{
return(make_msg());
}
}
#------------------------------------------------------------
# dump_entity - dump an entity's file info
#------------------------------------------------------------
sub dump_entity {
my $ent = shift;
my @parts = $ent->parts;
if (@parts) { # multipart...
map { dump_entity($_) } @parts;
}
else { # single part...
$debug and print " Part: ", $ent->bodyhandle->path,
" (", scalar($ent->head->mime_type), ")\n";
}
}
#------------------------------------------------------------
# main
#------------------------------------------------------------
sub main {
my $file;
my $entity;
# Sanity:
(-w ".") or die "cwd not writable, you naughty boy...";
# Go through messages:
@ARGV or unshift @ARGV, "-";
while (defined($file = shift @ARGV)) {
my $msgdir = make_msg_dir($file);
next if not $msgdir;
$debug and print "Message: $msgdir ($file)\n";
# Create a new parser object:
my $parser = new MIME::Parser;
### $parser->parse_nested_messages('REPLACE');
# Optional: set up parameters that will affect how it extracts
# documents from the input stream:
$parser->output_dir($msgdir);
# Parse an input stream:
open FILE, $file or die "couldn't open $file";
$entity = $parser->read(\*FILE) or
print STDERR "Couldn't parse MIME in $file; continuing...\n";
close FILE;
# Congratulations: you now have a (possibly multipart) MIME entity!
dump_entity($entity) if $entity;
### $entity->dump_skeleton if $entity;
}
1;
}
exit (&main ? 0 : -1);
#------------------------------------------------------------
1;

30
paypal_reply/paypal_run_laposte Executable file
View file

@ -0,0 +1,30 @@
#!/bin/sh
# $Id: paypal_run_laposte,v 1.2 2010/11/28 05:27:12 gilles Exp gilles $
set -e
#set -x
# Add path to commands at home
PATH=$PATH:/g/public_html/imapsync/paypal_reply
PERL5LIB=/g/public_html/imapsync/Mail-IMAPClient-3.25/lib
export PERL5LIB
test -f /g/public_html/imapsync/paypal_reply/paypal_functions \
&& . /g/public_html/imapsync/paypal_reply/paypal_functions
DATE_1=`date`
echo "==== paypal_reply_laposte ===="
paypal_reply_laposte "$@"
echo
DATE_2=`date`
echo "Debut : $DATE_1"
echo "Fin : $DATE_2"
echo "Yo Bery GOOD !"

30
paypal_reply/paypal_run_petite Executable file
View file

@ -0,0 +1,30 @@
#!/bin/sh
# $Id: paypal_run_petite,v 1.4 2010/12/14 15:40:13 gilles Exp gilles $
set -e
#set -x
# Add path to commands at home
PATH=$PATH:/g/public_html/imapsync/paypal_reply
PERL5LIB=/g/public_html/imapsync/Mail-IMAPClient-3.25/lib
export PERL5LIB
test -f /g/public_html/imapsync/paypal_reply/paypal_functions \
&& . /g/public_html/imapsync/paypal_reply/paypal_functions
DATE_1=`date`
echo "==== paypal_reply_petite ===="
paypal_reply_petite "$@"
echo
DATE_2=`date`
echo "Debut : $DATE_1"
echo "Fin : $DATE_2"
echo "Yo Bery GOOD !"

30
paypal_reply/paypal_run_test Executable file
View file

@ -0,0 +1,30 @@
#!/bin/sh
# $Id: paypal_run_test,v 1.1 2010/11/28 01:00:45 gilles Exp gilles $
set -e
#set -x
# Add path to commands at home
PATH=$PATH:/g/public_html/imapsync/paypal_reply
PERL5LIB=/g/public_html/imapsync/Mail-IMAPClient-3.25/lib
export PERL5LIB
test -f /g/public_html/imapsync/paypal_reply/paypal_functions \
&& . /g/public_html/imapsync/paypal_reply/paypal_functions
DATE_1=`date`
echo "==== paypal_reply_test ===="
paypal_reply_test
echo
DATE_2=`date`
echo "Debut : $DATE_1"
echo "Fin : $DATE_2"
echo "Yo Bery GOOD !"

71
paypal_reply/paypal_send Executable file
View file

@ -0,0 +1,71 @@
#!/usr/bin/perl
# $Id: paypal_send,v 1.3 2010/12/29 23:50:24 gilles Exp gilles $
use strict;
use warnings;
use Getopt::Long;
use MIME::Lite;
my (
$help,
$debug,
$send,
);
my $numopt = scalar(@ARGV);
my $opt_ret = GetOptions(
"help" => \$help,
"debug!" => \$debug,
"send!" => \$send,
);
usage() and exit if ($help or ! $numopt or ! $opt_ret) ;
my @reply = <>;
my %header;
while (my $line = shift @reply) {
#print $line;
chomp($line);
last if ($line =~ /^$/) ;
my($blank, $key, $value) = split /^(.+?:)\s*/, $line;
#print "[$key] [$value]\n";
$header{$key} = $value;
}
my $data = join('', @reply);
#print "[", $data, "]\n";
my $message = MIME::Lite->new();
$message->attr("content-type" => "text/plain");
$message->attr("content-type.charset" => "UTF-8");
$message->build(%header);
$message->build(Data => $data);
$message->print(\*STDOUT);
if ($send) {
$message->send;
print "Sent to ", $header{'To:'},"\n";
}
sub usage {
print <<EOF;
usage: $0 [options] file
--help : print this help message
--debug : verbose output
--send : send message
Examples:
$0 file
$0 --send file
EOF
}