1 # Copyrights 1995-2018 by [Mark Overmeer].
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.
5 # This code is part of the bundle MailTools. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md for Copyright.
7 # Licensed under the same terms as Perl itself.
17 # use locale; removed in version 1.78, because it causes taint problems
19 sub Version { our $VERSION }
23 # given a comment, attempt to extract a person's name
25 { # This function can be called as method as well
26 my $self = @_ && ref $_[0] ? shift : undef;
31 # Using encodings, too hard. See Mail::Message::Field::Full.
32 return '' if m/\=\?.*?\?\=/;
39 # Disregard numeric names (e.g. 123456.1234@compuserve.com)
40 return "" if /^[\d ]+$/;
42 s/^\((.*)\)$/$1/; # remove outermost parenthesis
43 s/^"(.*)"$/$1/; # remove outer quotation marks
44 s/\(.*?\)//g; # remove minimal embedded comments
45 s/\\//g; # remove all escapes
46 s/^"(.*)"$/$1/; # remove internal quotation marks
47 s/^([^\s]+) ?, ?(.*)$/$2 $1/; # reverse "Last, First M." if applicable
50 # Change casing only when the name contains only upper or only
51 # lower cased characters.
52 unless( m/[A-Z]/ && m/[a-z]/ )
53 { # Set the case of the name to first char upper rest lower
54 s/\b(\w+)/\L\u$1/igo; # Upcase first letter on name
55 s/\bMc(\w)/Mc\u$1/igo; # Scottish names such as 'McLeod'
56 s/\bo'(\w)/O'\u$1/igo; # Irish names such as 'O'Malley, O'Reilly'
57 s/\b(x*(ix)?v*(iv)?i*)\b/\U$1/igo; # Roman numerals, eg 'Level III Support'
62 s/(^[\s'"]+|[\s'"]+$)//g;
69 { local $_ = join ',', @_;
70 my (@words,$snippet,$field);
77 if(s/^\s*\(/(/ ) # (...)
80 PAREN: while(s/^(\(([^\(\)\\]|\\.)*)//)
83 while(s/^(([^\(\)\\]|\\.)*\)\s*)//)
85 last PAREN unless --$depth;
86 $field .= $1 if s/^(([^\(\)\\]|\\.)+)//;
90 carp "Unmatched () '$field' '$_'"
99 if( s/^("(?:[^"\\]+|\\.)*")\s*// # "..."
100 || s/^(\[(?:[^\]\\]+|\\.)*\])\s*// # [...]
101 || s/^([^\s()<>\@,;:\\".[\]]+)\s*//
102 || s/^([()<>\@,;:\\".[\]])\s*//
108 croak "Unrecognised line: $_";
116 { my ($idx, $tokens, $len) = @_;
119 { my $c = $tokens->[$idx];
120 return $c if $c eq ',' || $c eq ';' || $c eq '<';
128 { my ($class, $phrase, $address, $comment) = @_;
130 @$phrase || @$comment || @$address
133 my $o = $class->new(join(" ",@$phrase), join("",@$address), join(" ",@$comment));
134 @$phrase = @$address = @$comment = ();
148 my @line = grep {defined} @_;
149 my $line = join '', @line;
151 my (@phrase, @comment, @address, @objs);
152 my ($depth, $idx) = (0, 0);
154 my $tokens = _tokenise @line;
156 my $next = _find_next $idx, $tokens, $len;
159 for(my $idx = 0; $idx < $len; $idx++)
160 { $_ = $tokens->[$idx];
162 if(substr($_,0,1) eq '(') { push @comment, $_ }
163 elsif($_ eq '<') { $depth++ }
164 elsif($_ eq '>') { $depth-- if $depth }
165 elsif($_ eq ',' || $_ eq ';')
166 { warn "Unmatched '<>' in $line" if $depth;
167 my $o = $class->_complete(\@phrase, \@address, \@comment);
168 push @objs, $o if defined $o;
170 $next = _find_next $idx+1, $tokens, $len;
172 elsif($depth) { push @address, $_ }
173 elsif($next eq '<') { push @phrase, $_ }
174 elsif( /^[.\@:;]$/ || !@address || $address[-1] =~ /^[.\@:;]$/ )
175 { push @address, $_ }
177 { warn "Unmatched '<>' in $line" if $depth;
178 my $o = $class->_complete(\@phrase, \@address, \@comment);
179 push @objs, $o if defined $o;
189 sub phrase { shift->set_or_get(0, @_) }
190 sub address { shift->set_or_get(1, @_) }
191 sub comment { shift->set_or_get(2, @_) }
194 { my ($self, $i) = (shift, shift);
195 @_ or return $self->[$i];
197 my $val = $self->[$i];
198 $self->[$i] = shift if @_;
203 my $atext = '[\-\w !#$%&\'*+/=?^`{|}~]';
208 { my ($phrase, $email, $comment) = @$_;
211 if(defined $phrase && length $phrase)
213 , $phrase =~ /^(?:\s*$atext\s*)+$/o ? $phrase
214 : $phrase =~ /(?<!\\)"/ ? $phrase
217 push @addr, "<$email>"
218 if defined $email && length $email;
220 elsif(defined $email && length $email)
221 { push @addr, $email;
224 if(defined $comment && $comment =~ /\S/)
225 { $comment =~ s/^\s*\(?/(/;
226 $comment =~ s/\)?\s*$/)/;
230 if defined $comment && length $comment;
232 push @addrs, join(" ", @addr)
243 my $phrase = $self->phrase;
244 my $addr = $self->address;
246 $phrase = $self->comment
247 unless defined $phrase && length $phrase;
249 my $name = $self->_extract_name($phrase);
251 # first.last@domain address
252 if($name eq '' && $addr =~ /([^\%\.\@_]+([\._][^\%\.\@_]+)+)[\@\%]/)
253 { ($name = $1) =~ s/[\._]+/ /g;
254 $name = _extract_name $name;
257 if($name eq '' && $addr =~ m#/g=#i) # X400 style address
258 { my ($f) = $addr =~ m#g=([^/]*)#i;
259 my ($l) = $addr =~ m#s=([^/]*)#i;
260 $name = _extract_name "$f $l";
263 length $name ? $name : undef;
268 { my $addr = shift->address || '';
269 my $i = rindex $addr, '@';
270 $i >= 0 ? substr($addr, $i+1) : undef;
275 { my $addr = shift->address || '';
276 my $i = rindex $addr, '@';
277 $i >= 0 ? substr($addr,0,$i) : $addr;