blib/lib/Email/Address/Classify.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 89 CC 8 SCAR 28.9
CC
8
Cov
0%
is_random
42.0
new
6.0
_parse_email
6.0
CRAP 18.0
1 package Email::Address::Classify;
2 use strict;
3 use warnings FATAL => 'all';
4 use File::Basename;
5  
6 =head1 NAME
7  
8 Email::Address::Classify - Classify email addresses
9  
10 =head1 SYNOPSIS
11  
12 use Email::Address::Classify;
13  
14 $email = Email::Address::Classify->new('a.johnson@example.com');
15  
16 print "Is valid: " . $email->is_valid() ? "Y\n" : "N\n"; # Y
17 print "Is random: " . $email->is_random() ? "Y\n" : "N\n"; # N
18  
19 =head1 DESCRIPTION
20  
21 This module provides a simple way to classify email addresses. At the moment, it only
22 provides two classifications is_valid() and is_random(). More classifications may be
23 added in the future.
24  
25 =head1 METHODS
26  
27 =over 4
28  
29 =item new($address)
30  
31 Creates a new Email::Address::Classify object. The only argument is the email address.
32  
33 =item is_valid()
34  
35 Performs a simple check to determine if the address is formatted properly.
36 Note that this method does not check if the domain exists or if the mailbox is valid.
37 Nor is it a complete RFC 2822 validator. For that, you should use a module such as
38 L<Email::Address>.
39  
40 If this method returns false, all other methods will return false as well.
41  
42 =item is_random()
43  
44 Returns true if the localpart is likely to be randomly generated, false otherwise.
45 Note that randomness is subjective and depends on the user's locale and other factors.
46 This method uses a list of common trigrams to determine if the localpart is random. The trigrams
47 were generated from a corpus of 30,000 email messages, mostly in English. The accuracy of this
48 method is about 95% for English email addresses.
49  
50 If you would like to generate your own list of trigrams, you can use the included
51 C<ngrams.pl> script in the C<tools> directory of the source repository.
52  
53 =back
54  
55 =head1 TODO
56  
57 Ideas for future classification methods:
58  
59 is_freemail()
60 is_disposable()
61 is_role_based()
62 is_bounce()
63 is_verp()
64 is_srs()
65 is_batv()
66  
67 =head1 AUTHOR
68  
69 Kent Oyer <kent@mxguardian.net>
70  
71 =head1 LICENSE AND COPYRIGHT
72  
73 Copyright (C) 2023 MXGuardian LLC
74  
75 This program is free software: you can redistribute it and/or modify
76 it under the terms of the GNU General Public License as published by
77 the Free Software Foundation, either version 3 of the License, or
78 (at your option) any later version.
79  
80 This program is distributed in the hope that it will be useful,
81 but WITHOUT ANY WARRANTY; without even the implied warranty of
82 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE
83 file included with this distribution for more information.
84  
85 You should have received a copy of the GNU General Public License
86 along with this program. If not, see https://www.gnu.org/licenses/.
87  
88 =cut
89  
90 my %ngrams;
91 our $min_length = 4;
92 our $VERSION = '0.01';
93  
94 sub _init {
95  
96 # read ngrams from Classify/ngrams.txt
97 my $filename = dirname($INC{'Email/Address/Classify.pm'}).'/Classify/ngrams.txt';
98 open(my $fh, '<', $filename) or die "Can't open $filename: $!";
99 while (my $line = <$fh>) {
100 chomp $line;
101 $ngrams{$line} = 1;
102 }
103 close($fh);
104  
105 }
106  
107 sub new {
108 my ($class,$address) = @_;
109 my $self = bless {
110 address => $address,
111 }, $class;
112 my $email = _parse_email($address);
113 if ( $email ) {
114 $self->{localpart} = $email->{localpart};
115 $self->{domain} = $email->{domain};
116 $self->{valid} = 1;
117 } else {
118 $self->{valid} = 0;
119 }
120  
121 return $self;
122 }
123  
124 sub is_valid {
125 my $self = shift;
126 return $self->{valid};
127 }
128  
129 sub _find_ngrams {
130 my $str = lc($_[0]);
131 my @ngrams;
132 for (my $i = 0; $i < length($str) - 2; $i++) {
133 push @ngrams, substr($str, $i, 3);
134 }
135 return @ngrams;
136 }
137  
138 sub is_random {
139 my $self = shift;
140  
141 return $self->{random} if exists $self->{random};
142  
143 return $self->{random} = 0 unless $self->{valid} && length($self->{localpart}) >= $min_length;
144  
145 _init() unless %ngrams;
146  
147 my ($common,$uncommon) = (0,0);
148 foreach (_find_ngrams($self->{localpart})) {
149 if (exists $ngrams{$_} ) {
150 $common++;
151 } else {
152 $uncommon++;
153 }
154 }
155 return $self->{random} = ($uncommon > $common ? 1 : 0);
156 }
157  
158 sub _parse_email {
159 my $email = shift;
160 return undef unless defined($email) &&
161 $email =~ /^((?:[a-zA-Z0-9\+\_\=\.\-])+)@((?:[a-zA-Z0-9\-])+(?:\.[a-zA-Z0-9\-]+)+)$/;
162  
163 return {
164 address => $email,
165 localpart => $1,
166 domain => $2,
167 };
168  
169 }
170  
171 1;