1 # Copyrights 1995-2017 by [Mark Overmeer <perl@overmeer.net>].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
13 # use locale; removed in version 1.78, because it causes taint problems
15 sub Version { our $VERSION }
19 # given a comment, attempt to extract a person's name
21 { # This function can be called as method as well
22 my $self = @_ && ref $_[0] ? shift : undef;
27 # Using encodings, too hard. See Mail::Message::Field::Full.
28 return '' if m/\=\?.*?\?\=/;
35 # Disregard numeric names (e.g. 123456.1234@compuserve.com)
36 return "" if /^[\d ]+$/;
38 s/^\((.*)\)$/$1/; # remove outermost parenthesis
39 s/^"(.*)"$/$1/; # remove outer quotation marks
40 s/\(.*?\)//g; # remove minimal embedded comments
41 s/\\//g; # remove all escapes
42 s/^"(.*)"$/$1/; # remove internal quotation marks
43 s/^([^\s]+) ?, ?(.*)$/$2 $1/; # reverse "Last, First M." if applicable
46 # Change casing only when the name contains only upper or only
47 # lower cased characters.
48 unless( m/[A-Z]/ && m/[a-z]/ )
49 { # Set the case of the name to first char upper rest lower
50 s/\b(\w+)/\L\u$1/igo; # Upcase first letter on name
51 s/\bMc(\w)/Mc\u$1/igo; # Scottish names such as 'McLeod'
52 s/\bo'(\w)/O'\u$1/igo; # Irish names such as 'O'Malley, O'Reilly'
53 s/\b(x*(ix)?v*(iv)?i*)\b/\U$1/igo; # Roman numerals, eg 'Level III Support'
58 s/(^[\s'"]+|[\s'"]+$)//g;
65 { local $_ = join ',', @_;
66 my (@words,$snippet,$field);
73 if(s/^\s*\(/(/ ) # (...)
76 PAREN: while(s/^(\(([^\(\)\\]|\\.)*)//)
79 while(s/^(([^\(\)\\]|\\.)*\)\s*)//)
81 last PAREN unless --$depth;
82 $field .= $1 if s/^(([^\(\)\\]|\\.)+)//;
86 carp "Unmatched () '$field' '$_'"
95 if( s/^("(?:[^"\\]+|\\.)*")\s*// # "..."
96 || s/^(\[(?:[^\]\\]+|\\.)*\])\s*// # [...]
97 || s/^([^\s()<>\@,;:\\".[\]]+)\s*//
98 || s/^([()<>\@,;:\\".[\]])\s*//
104 croak "Unrecognised line: $_";
112 { my ($idx, $tokens, $len) = @_;
115 { my $c = $tokens->[$idx];
116 return $c if $c eq ',' || $c eq ';' || $c eq '<';
124 { my ($class, $phrase, $address, $comment) = @_;
126 @$phrase || @$comment || @$address
129 my $o = $class->new(join(" ",@$phrase), join("",@$address), join(" ",@$comment));
130 @$phrase = @$address = @$comment = ();
144 my @line = grep {defined} @_;
145 my $line = join '', @line;
147 my (@phrase, @comment, @address, @objs);
148 my ($depth, $idx) = (0, 0);
150 my $tokens = _tokenise @line;
152 my $next = _find_next $idx, $tokens, $len;
155 for(my $idx = 0; $idx < $len; $idx++)
156 { $_ = $tokens->[$idx];
158 if(substr($_,0,1) eq '(') { push @comment, $_ }
159 elsif($_ eq '<') { $depth++ }
160 elsif($_ eq '>') { $depth-- if $depth }
161 elsif($_ eq ',' || $_ eq ';')
162 { warn "Unmatched '<>' in $line" if $depth;
163 my $o = $class->_complete(\@phrase, \@address, \@comment);
164 push @objs, $o if defined $o;
166 $next = _find_next $idx+1, $tokens, $len;
168 elsif($depth) { push @address, $_ }
169 elsif($next eq '<') { push @phrase, $_ }
170 elsif( /^[.\@:;]$/ || !@address || $address[-1] =~ /^[.\@:;]$/ )
171 { push @address, $_ }
173 { warn "Unmatched '<>' in $line" if $depth;
174 my $o = $class->_complete(\@phrase, \@address, \@comment);
175 push @objs, $o if defined $o;
185 sub phrase { shift->set_or_get(0, @_) }
186 sub address { shift->set_or_get(1, @_) }
187 sub comment { shift->set_or_get(2, @_) }
190 { my ($self, $i) = (shift, shift);
191 @_ or return $self->[$i];
193 my $val = $self->[$i];
194 $self->[$i] = shift if @_;
199 my $atext = '[\-\w !#$%&\'*+/=?^`{|}~]';
204 { my ($phrase, $email, $comment) = @$_;
207 if(defined $phrase && length $phrase)
209 , $phrase =~ /^(?:\s*$atext\s*)+$/o ? $phrase
210 : $phrase =~ /(?<!\\)"/ ? $phrase
213 push @addr, "<$email>"
214 if defined $email && length $email;
216 elsif(defined $email && length $email)
217 { push @addr, $email;
220 if(defined $comment && $comment =~ /\S/)
221 { $comment =~ s/^\s*\(?/(/;
222 $comment =~ s/\)?\s*$/)/;
226 if defined $comment && length $comment;
228 push @addrs, join(" ", @addr)
239 my $phrase = $self->phrase;
240 my $addr = $self->address;
242 $phrase = $self->comment
243 unless defined $phrase && length $phrase;
245 my $name = $self->_extract_name($phrase);
247 # first.last@domain address
248 if($name eq '' && $addr =~ /([^\%\.\@_]+([\._][^\%\.\@_]+)+)[\@\%]/)
249 { ($name = $1) =~ s/[\._]+/ /g;
250 $name = _extract_name $name;
253 if($name eq '' && $addr =~ m#/g=#i) # X400 style address
254 { my ($f) = $addr =~ m#g=([^/]*)#i;
255 my ($l) = $addr =~ m#s=([^/]*)#i;
256 $name = _extract_name "$f $l";
259 length $name ? $name : undef;
264 { my $addr = shift->address || '';
265 my $i = rindex $addr, '@';
266 $i >= 0 ? substr($addr, $i+1) : undef;
271 { my $addr = shift->address || '';
272 my $i = rindex $addr, '@';
273 $i >= 0 ? substr($addr,0,$i) : $addr;