package Email;

# $Id$

use strict;
use vars qw($VERSION);
$VERSION = sprintf "%d.%02d", q$Revision$ =~ /(\d+)\.(\d+)/;

use overload '""' => \&as_string;

# Need qr//.
require 5.005;


my $esc         = '\\\\';               my $period      = '\.';
my $space       = '\040';
my $open_br     = '\[';                 my $close_br    = '\]';
my $nonASCII    = '\x80-\xff';          my $ctrl        = '\000-\037';
my $cr_list     = '\n\015';
my $qtext       = qq/[^$esc$nonASCII$cr_list\"]/;
my $dtext       = qq/[^$esc$nonASCII$cr_list$open_br$close_br]/;
my $quoted_pair = qq<$esc>.qq<[^$nonASCII]>;
my $atom_char   = qq/[^($space)<>\@,;:\".$esc$open_br$close_br$ctrl$nonASCII]/;
my $atom        = qq<$atom_char+(?!$atom_char)>;
my $quoted_str  = qq<\"$qtext*(?:$quoted_pair$qtext*)*\">; # "
my $word        = qq<(?:$atom|$quoted_str)>;
my $domain_ref  = $atom;
my $domain_lit  = qq<$open_br(?:$dtext|$quoted_pair)*$close_br>;
my $sub_domain  = qq<(?:$domain_ref|$domain_lit)>;
my $domain      = qq<$sub_domain(?:$period$sub_domain)*>;
my $local_part  = qq<$word(?:$period$word)*>;


# Finally, the address-spec regex (more or less)
$local_part = qr<$local_part>;
$domain     = qr<$domain>;

sub new {
    my $class = shift;
    my $addr  = shift;

    study $addr;

    my ($u, $h) = ($addr =~ /($local_part)\s*@\s*($domain)/);

    bless { full => $addr, user => $u, host => $h, } => $class;
}

sub user      { shift->{'user'} }
sub host      { shift->{'host'} }
sub as_string {
    my $self = shift;
    join '@', $self->user, $self->host;
}

1;

__END__

=pod

=head1 NAME

  Email - Class to encapsulate an email address
 
=head1 SYNOPSIS

  use Email;
  my $e = Email->new($addr);


=head1 SEE ALSO

L<Email::Find>

=cut
