Created
January 26, 2015 18:33
-
-
Save yanick/7d58933c8b8440dc8c7b to your computer and use it in GitHub Desktop.
anonymize/deanonymize emails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use 5.20.0; | |
use warnings; | |
use AnyEvent::SMTP::Server 'smtp_server'; | |
use CHI; | |
use Email::Simple; | |
my $email_forward_cache = CHI->new( | |
driver => 'File', | |
root_dir => 'cache', | |
); | |
my $email_reverse_cache = CHI->new( | |
driver => 'File', | |
root_dir => 'rev_cache', | |
); | |
smtp_server undef, 2525, sub { | |
my $mail = shift; | |
my $parsed = Email::Simple->new( $mail->{data} ); | |
my $anonymous = Email::Simple->create( | |
header => [ | |
From => anonymize( $parsed->header('From') ), | |
To => deanonymize( $parsed->header('To') ), | |
Subject => $parsed->header('Subject'), | |
], | |
body => $parsed->body, | |
); | |
warn $anonymous->as_string; | |
}; | |
sub anonymize { | |
my $email = shift; | |
$email = $1 if $email =~ /<(.*?)>/; | |
$email_forward_cache->compute( $email, undef, sub { | |
my $anon = $email =~ s/([^@]+)/reverse $1/er =~ s/(?<=@).*/anonymizer.com/r; | |
$email_reverse_cache->set( $anon => $email ); | |
return $anon; | |
}) | |
} | |
sub deanonymize { | |
my $email = shift; | |
$email = $1 if $email =~ /<(.*?)>/; | |
warn $email; | |
return $email_reverse_cache->get($email) || $email; | |
} | |
AnyEvent->condvar->recv; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment