Skip to content

Instantly share code, notes, and snippets.

@romuald
Created June 4, 2018 16:14
Show Gist options
  • Select an option

  • Save romuald/93547c5733cce35474645c1d9d80a2b5 to your computer and use it in GitHub Desktop.

Select an option

Save romuald/93547c5733cce35474645c1d9d80a2b5 to your computer and use it in GitHub Desktop.
evolution-in-addressbook
#!/usr/bin/perl
=head1 evolution-in-addressbook
=head2 Synopsis
Parse a mail and tell if the From: is coming from
a contact in one of your evolution address books
=head2 Usage
Program reads from STDIN only:
perl evolution-in-addressbook < mail-file
=head2 Exit codes
- 0 email in the From: has been found in one of the address books
- 3 email NOT found in address books
=cut
use strict;
use warnings;
use DBI;
use DB_File;
sub DBPATHS() {
my $BASE = $ENV{HOME} . "/.local/share/evolution/addressbook";
my @ret;
opendir(DIR, $BASE) or die $!;
while ( my $node = readdir(DIR) ) {
next unless -d "$BASE/$node";
next unless -f "$BASE/$node/addressbook.db";
push @ret, "$BASE/$node/addressbook.db";
}
closedir(DIR);
return @ret;
}
sub DBPATHS3() {
my $BASE = $ENV{HOME} . "/.local/share/evolution/addressbook";
my @ret;
opendir(DIR, $BASE) or die $!;
while ( my $node = readdir(DIR) ) {
next unless -d "$BASE/$node";
next unless -f "$BASE/$node/contacts.db";
push @ret, "$BASE/$node/contacts.db";
}
closedir(DIR);
return @ret;
}
my %address_db;
sub END {
untie %address_db;
}
sub whitelist_email {
my $email = lc $_[0];
# Tie Berkeley db format addressbook to hash
for my $PATH (DBPATHS()) {
tie %address_db, 'DB_File', $PATH;
my $key;
my $record;
# Walk through all records in addressbook
while ( ($key, $record) = each %address_db ) {
my @lines = split /^/, $record;
# Each record may contain multiple email address... check them all
foreach my $line ( @lines ) {
$line =~ /^EMAIL;.*?:(.*)/;
my $db_email;
chop ($db_email = $1);
if ( lc($db_email) eq $email ) {
# print "KEY: $key \nRECORD: \n$record \n";
untie %address_db;
return 1;
}
}
}
untie %address_db;
}
return 0;
}
sub whitelist_email3 {
my $email = lc $_[0];
my $sql = qq{SELECT uid
FROM folder_id
WHERE
? IN (email_1, email_2, email_3, email_4)
COLLATE NOCASE
OR
vcard LIKE '%' || char(13) || char(10) || 'EMAIL;%' || ? || char(13) || char(10) || '%'
COLLATE NOCASE
LIMIT 1;
};
for my $PATH (DBPATHS3()) {
# print "check: $PATH\n";
my $dbh = DBI->connect("DBI:SQLite:dbname=$PATH");
my @exists = $dbh->selectrow_array($sql, {}, $email, $email);
return 1 if @exists;
}
}
# Scan through email from STDIN
while ( <> ) {
if ( /^From:/ ) {
# Hookey attempt at matching most email addresses
# Will miss some RFC822 compliant addresses
if ( /.*<(\S+@\S+)>/ or /(\S+@\S+)/ ) {
my $email = $1;
print "Found: $email ";
my $ret = whitelist_email($email) || whitelist_email3($email);
print "not " if not $ret;
print "in addressbook\n";
exit 0 if $ret;
} else {
# print "EEEEEEE No mail found\n";
}
}
}
exit 3;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment