]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - scripts/get_maintainer.pl
scripts/get_maintainer.pl: add sections in pattern match depth order
[mirror_ubuntu-bionic-kernel.git] / scripts / get_maintainer.pl
CommitLineData
cb7301c7
JP
1#!/usr/bin/perl -w
2# (c) 2007, Joe Perches <joe@perches.com>
3# created from checkpatch.pl
4#
5# Print selected MAINTAINERS information for
6# the files modified in a patch or for a file
7#
8# usage: perl scripts/get_maintainers.pl [OPTIONS] <patch>
9# perl scripts/get_maintainers.pl [OPTIONS] -f <file>
10#
11# Licensed under the terms of the GNU GPL License version 2
12
13use strict;
14
15my $P = $0;
f5492666 16my $V = '0.18beta2';
cb7301c7
JP
17
18use Getopt::Long qw(:config no_auto_abbrev);
19
20my $lk_path = "./";
21my $email = 1;
22my $email_usename = 1;
23my $email_maintainer = 1;
24my $email_list = 1;
25my $email_subscriber_list = 0;
26my $email_git = 1;
27my $email_git_penguin_chiefs = 0;
28my $email_git_min_signatures = 1;
29my $email_git_max_maintainers = 5;
afa81ee1 30my $email_git_min_percent = 5;
cb7301c7 31my $email_git_since = "1-year-ago";
f5492666 32my $email_git_blame = 0;
cb7301c7
JP
33my $output_multiline = 1;
34my $output_separator = ", ";
35my $scm = 0;
36my $web = 0;
37my $subsystem = 0;
38my $status = 0;
4a7fdb5f 39my $from_filename = 0;
cb7301c7
JP
40my $version = 0;
41my $help = 0;
42
43my $exit = 0;
44
45my @penguin_chief = ();
46push(@penguin_chief,"Linus Torvalds:torvalds\@linux-foundation.org");
47#Andrew wants in on most everything - 2009/01/14
48#push(@penguin_chief,"Andrew Morton:akpm\@linux-foundation.org");
49
50my @penguin_chief_names = ();
51foreach my $chief (@penguin_chief) {
52 if ($chief =~ m/^(.*):(.*)/) {
53 my $chief_name = $1;
54 my $chief_addr = $2;
55 push(@penguin_chief_names, $chief_name);
56 }
57}
58my $penguin_chiefs = "\(" . join("|",@penguin_chief_names) . "\)";
59
5f2441e9 60# rfc822 email address - preloaded methods go here.
1b5e1cf6 61my $rfc822_lwsp = "(?:(?:\\r\\n)?[ \\t])";
df4cc036 62my $rfc822_char = '[\\000-\\377]';
1b5e1cf6 63
cb7301c7
JP
64if (!GetOptions(
65 'email!' => \$email,
66 'git!' => \$email_git,
67 'git-chief-penguins!' => \$email_git_penguin_chiefs,
68 'git-min-signatures=i' => \$email_git_min_signatures,
69 'git-max-maintainers=i' => \$email_git_max_maintainers,
afa81ee1 70 'git-min-percent=i' => \$email_git_min_percent,
cb7301c7 71 'git-since=s' => \$email_git_since,
f5492666 72 'git-blame!' => \$email_git_blame,
cb7301c7
JP
73 'm!' => \$email_maintainer,
74 'n!' => \$email_usename,
75 'l!' => \$email_list,
76 's!' => \$email_subscriber_list,
77 'multiline!' => \$output_multiline,
78 'separator=s' => \$output_separator,
79 'subsystem!' => \$subsystem,
80 'status!' => \$status,
81 'scm!' => \$scm,
82 'web!' => \$web,
4a7fdb5f 83 'f|file' => \$from_filename,
cb7301c7
JP
84 'v|version' => \$version,
85 'h|help' => \$help,
86 )) {
87 usage();
88 die "$P: invalid argument\n";
89}
90
91if ($help != 0) {
92 usage();
93 exit 0;
94}
95
96if ($version != 0) {
97 print("${P} ${V}\n");
98 exit 0;
99}
100
cb7301c7
JP
101if ($#ARGV < 0) {
102 usage();
103 die "$P: argument missing: patchfile or -f file please\n";
104}
105
106my $selections = $email + $scm + $status + $subsystem + $web;
107if ($selections == 0) {
108 usage();
109 die "$P: Missing required option: email, scm, status, subsystem or web\n";
110}
111
f5492666
JP
112if ($email &&
113 ($email_maintainer + $email_list + $email_subscriber_list +
114 $email_git + $email_git_penguin_chiefs + $email_git_blame) == 0) {
cb7301c7
JP
115 usage();
116 die "$P: Please select at least 1 email option\n";
117}
118
119if (!top_of_kernel_tree($lk_path)) {
120 die "$P: The current directory does not appear to be "
121 . "a linux kernel source tree.\n";
122}
123
124## Read MAINTAINERS for type/value pairs
125
126my @typevalue = ();
127open(MAINT, "<${lk_path}MAINTAINERS") || die "$P: Can't open MAINTAINERS\n";
128while (<MAINT>) {
129 my $line = $_;
130
131 if ($line =~ m/^(\C):\s*(.*)/) {
132 my $type = $1;
133 my $value = $2;
134
135 ##Filename pattern matching
136 if ($type eq "F" || $type eq "X") {
137 $value =~ s@\.@\\\.@g; ##Convert . to \.
138 $value =~ s/\*/\.\*/g; ##Convert * to .*
139 $value =~ s/\?/\./g; ##Convert ? to .
870020f9
JP
140 ##if pattern is a directory and it lacks a trailing slash, add one
141 if ((-d $value)) {
142 $value =~ s@([^/])$@$1/@;
143 }
cb7301c7
JP
144 }
145 push(@typevalue, "$type:$value");
146 } elsif (!/^(\s)*$/) {
147 $line =~ s/\n$//g;
148 push(@typevalue, $line);
149 }
150}
151close(MAINT);
152
4a7fdb5f 153## use the filenames on the command line or find the filenames in the patchfiles
cb7301c7
JP
154
155my @files = ();
f5492666 156my @range = ();
cb7301c7 157
4a7fdb5f 158foreach my $file (@ARGV) {
870020f9
JP
159 ##if $file is a directory and it lacks a trailing slash, add one
160 if ((-d $file)) {
161 $file =~ s@([^/])$@$1/@;
162 } elsif (!(-f $file)) {
4a7fdb5f 163 die "$P: file '${file}' not found\n";
cb7301c7 164 }
4a7fdb5f
JP
165 if ($from_filename) {
166 push(@files, $file);
167 } else {
168 my $file_cnt = @files;
f5492666 169 my $lastfile;
4a7fdb5f
JP
170 open(PATCH, "<$file") or die "$P: Can't open ${file}\n";
171 while (<PATCH>) {
172 if (m/^\+\+\+\s+(\S+)/) {
173 my $filename = $1;
174 $filename =~ s@^[^/]*/@@;
175 $filename =~ s@\n@@;
f5492666 176 $lastfile = $filename;
4a7fdb5f 177 push(@files, $filename);
f5492666
JP
178 } elsif (m/^\@\@ -(\d+),(\d+)/) {
179 if ($email_git_blame) {
180 push(@range, "$lastfile:$1:$2");
181 }
4a7fdb5f 182 }
cb7301c7 183 }
4a7fdb5f
JP
184 close(PATCH);
185 if ($file_cnt == @files) {
7f29fd27 186 warn "$P: file '${file}' doesn't appear to be a patch. "
4a7fdb5f
JP
187 . "Add -f to options?\n";
188 }
189 @files = sort_and_uniq(@files);
cb7301c7 190 }
cb7301c7
JP
191}
192
193my @email_to = ();
290603c1 194my @list_to = ();
cb7301c7
JP
195my @scm = ();
196my @web = ();
197my @subsystem = ();
198my @status = ();
199
200# Find responsible parties
201
202foreach my $file (@files) {
203
204#Do not match excluded file patterns
205
206 my $exclude = 0;
207 foreach my $line (@typevalue) {
290603c1 208 if ($line =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
209 my $type = $1;
210 my $value = $2;
211 if ($type eq 'X') {
212 if (file_match_pattern($file, $value)) {
213 $exclude = 1;
1d606b4e 214 last;
cb7301c7
JP
215 }
216 }
217 }
218 }
219
220 if (!$exclude) {
221 my $tvi = 0;
1d606b4e 222 my %hash;
cb7301c7 223 foreach my $line (@typevalue) {
290603c1 224 if ($line =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
225 my $type = $1;
226 my $value = $2;
227 if ($type eq 'F') {
228 if (file_match_pattern($file, $value)) {
1d606b4e
JP
229 my $pattern_depth = ($value =~ tr@/@@);
230 $pattern_depth++ if (!(substr($value,-1,1) eq "/"));
231 $hash{$tvi} = $pattern_depth;
cb7301c7
JP
232 }
233 }
234 }
235 $tvi++;
236 }
1d606b4e
JP
237 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
238 add_categories($line);
239 }
cb7301c7
JP
240 }
241
4a7fdb5f 242 if ($email && $email_git) {
cb7301c7
JP
243 recent_git_signoffs($file);
244 }
245
f5492666
JP
246 if ($email && $email_git_blame) {
247 git_assign_blame($file);
248 }
cb7301c7
JP
249}
250
f5f5078d 251if ($email) {
cb7301c7
JP
252 foreach my $chief (@penguin_chief) {
253 if ($chief =~ m/^(.*):(.*)/) {
f5f5078d 254 my $email_address;
cb7301c7 255 if ($email_usename) {
f5f5078d 256 $email_address = format_email($1, $2);
cb7301c7 257 } else {
f5f5078d
JP
258 $email_address = $2;
259 }
260 if ($email_git_penguin_chiefs) {
261 push(@email_to, $email_address);
262 } else {
263 @email_to = grep(!/${email_address}/, @email_to);
cb7301c7
JP
264 }
265 }
266 }
267}
268
290603c1
JP
269if ($email || $email_list) {
270 my @to = ();
271 if ($email) {
272 @to = (@to, @email_to);
cb7301c7 273 }
290603c1 274 if ($email_list) {
290603c1 275 @to = (@to, @list_to);
290603c1
JP
276 }
277 output(uniq(@to));
cb7301c7
JP
278}
279
280if ($scm) {
4a7fdb5f 281 @scm = sort_and_uniq(@scm);
cb7301c7
JP
282 output(@scm);
283}
284
285if ($status) {
4a7fdb5f 286 @status = sort_and_uniq(@status);
cb7301c7
JP
287 output(@status);
288}
289
290if ($subsystem) {
4a7fdb5f 291 @subsystem = sort_and_uniq(@subsystem);
cb7301c7
JP
292 output(@subsystem);
293}
294
295if ($web) {
4a7fdb5f 296 @web = sort_and_uniq(@web);
cb7301c7
JP
297 output(@web);
298}
299
300exit($exit);
301
302sub file_match_pattern {
303 my ($file, $pattern) = @_;
304 if (substr($pattern, -1) eq "/") {
305 if ($file =~ m@^$pattern@) {
306 return 1;
307 }
308 } else {
309 if ($file =~ m@^$pattern@) {
310 my $s1 = ($file =~ tr@/@@);
311 my $s2 = ($pattern =~ tr@/@@);
312 if ($s1 == $s2) {
313 return 1;
314 }
315 }
316 }
317 return 0;
318}
319
320sub usage {
321 print <<EOT;
322usage: $P [options] patchfile
870020f9 323 $P [options] -f file|directory
cb7301c7
JP
324version: $V
325
326MAINTAINER field selection options:
327 --email => print email address(es) if any
328 --git => include recent git \*-by: signers
329 --git-chief-penguins => include ${penguin_chiefs}
330 --git-min-signatures => number of signatures required (default: 1)
331 --git-max-maintainers => maximum maintainers to add (default: 5)
3d202aeb 332 --git-min-percent => minimum percentage of commits required (default: 5)
cb7301c7 333 --git-since => git history to use (default: 1-year-ago)
f5492666 334 --git-blame => use git blame to find modified commits for patch or file
cb7301c7
JP
335 --m => include maintainer(s) if any
336 --n => include name 'Full Name <addr\@domain.tld>'
337 --l => include list(s) if any
338 --s => include subscriber only list(s) if any
339 --scm => print SCM tree(s) if any
340 --status => print status if any
341 --subsystem => print subsystem name if any
342 --web => print website(s) if any
343
344Output type options:
345 --separator [, ] => separator for multiple entries on 1 line
346 --multiline => print 1 entry per line
347
348Default options:
290603c1 349 [--email --git --m --n --l --multiline]
cb7301c7
JP
350
351Other options:
f5f5078d 352 --version => show version
cb7301c7
JP
353 --help => show this help information
354
870020f9
JP
355Notes:
356 Using "-f directory" may give unexpected results:
f5492666
JP
357 Used with "--git", git signators for _all_ files in and below
358 directory are examined as git recurses directories.
359 Any specified X: (exclude) pattern matches are _not_ ignored.
360 Used with "--nogit", directory is used as a pattern match,
361 no individual file within the directory or subdirectory
362 is matched.
363 Used with "--git-blame", does not iterate all files in directory
364 Using "--git-blame" is slow and may add old committers and authors
365 that are no longer active maintainers to the output.
cb7301c7
JP
366EOT
367}
368
369sub top_of_kernel_tree {
370 my ($lk_path) = @_;
371
372 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
373 $lk_path .= "/";
374 }
375 if ( (-f "${lk_path}COPYING")
376 && (-f "${lk_path}CREDITS")
377 && (-f "${lk_path}Kbuild")
378 && (-f "${lk_path}MAINTAINERS")
379 && (-f "${lk_path}Makefile")
380 && (-f "${lk_path}README")
381 && (-d "${lk_path}Documentation")
382 && (-d "${lk_path}arch")
383 && (-d "${lk_path}include")
384 && (-d "${lk_path}drivers")
385 && (-d "${lk_path}fs")
386 && (-d "${lk_path}init")
387 && (-d "${lk_path}ipc")
388 && (-d "${lk_path}kernel")
389 && (-d "${lk_path}lib")
390 && (-d "${lk_path}scripts")) {
391 return 1;
392 }
393 return 0;
394}
395
396sub format_email {
397 my ($name, $email) = @_;
398
399 $name =~ s/^\s+|\s+$//g;
d789504a 400 $name =~ s/^\"|\"$//g;
cb7301c7
JP
401 $email =~ s/^\s+|\s+$//g;
402
403 my $formatted_email = "";
404
405 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
406 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
407 $formatted_email = "\"${name}\"\ \<${email}\>";
408 } else {
409 $formatted_email = "${name} \<${email}\>";
410 }
411 return $formatted_email;
412}
413
414sub add_categories {
415 my ($index) = @_;
416
417 $index = $index - 1;
418 while ($index >= 0) {
419 my $tv = $typevalue[$index];
290603c1 420 if ($tv =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
421 my $ptype = $1;
422 my $pvalue = $2;
423 if ($ptype eq "L") {
290603c1
JP
424 my $list_address = $pvalue;
425 my $list_additional = "";
426 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
427 $list_address = $1;
428 $list_additional = $2;
429 }
bdf7c685 430 if ($list_additional =~ m/subscribers-only/) {
cb7301c7 431 if ($email_subscriber_list) {
290603c1 432 push(@list_to, $list_address);
cb7301c7
JP
433 }
434 } else {
435 if ($email_list) {
290603c1 436 push(@list_to, $list_address);
cb7301c7
JP
437 }
438 }
439 } elsif ($ptype eq "M") {
5f2441e9
JP
440 my $p_used = 0;
441 if ($index >= 0) {
442 my $tv = $typevalue[$index - 1];
443 if ($tv =~ m/^(\C):\s*(.*)/) {
444 if ($1 eq "P") {
445 if ($email_usename) {
446 push_email_address(format_email($2, $pvalue));
447 $p_used = 1;
448 }
449 }
450 }
451 }
452 if (!$p_used) {
1b5e1cf6 453 push_email_addresses($pvalue);
cb7301c7
JP
454 }
455 } elsif ($ptype eq "T") {
456 push(@scm, $pvalue);
457 } elsif ($ptype eq "W") {
458 push(@web, $pvalue);
459 } elsif ($ptype eq "S") {
460 push(@status, $pvalue);
461 }
462
463 $index--;
464 } else {
465 push(@subsystem,$tv);
466 $index = -1;
467 }
468 }
469}
470
1b5e1cf6
JP
471sub push_email_address {
472 my ($email_address) = @_;
473
474 my $email_name = "";
1b5e1cf6 475
0a79c492 476 if ($email_maintainer) {
f5492666
JP
477 if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
478 $email_name = $1;
479 $email_address = $2;
480 if ($email_usename) {
481 push(@email_to, format_email($email_name, $email_address));
482 } else {
483 push(@email_to, $email_address);
484 }
485 } elsif ($email_address =~ m/<(.+)>/) {
486 $email_address = $1;
487 push(@email_to, $email_address);
0a79c492
JP
488 } else {
489 push(@email_to, $email_address);
490 }
1b5e1cf6
JP
491 }
492}
493
494sub push_email_addresses {
495 my ($address) = @_;
496
497 my @address_list = ();
498
5f2441e9
JP
499 if (rfc822_valid($address)) {
500 push_email_address($address);
501 } elsif (@address_list = rfc822_validlist($address)) {
1b5e1cf6
JP
502 my $array_count = shift(@address_list);
503 while (my $entry = shift(@address_list)) {
504 push_email_address($entry);
505 }
5f2441e9
JP
506 } else {
507 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
1b5e1cf6 508 }
1b5e1cf6
JP
509}
510
cb7301c7
JP
511sub which {
512 my ($bin) = @_;
513
f5f5078d 514 foreach my $path (split(/:/, $ENV{PATH})) {
cb7301c7
JP
515 if (-e "$path/$bin") {
516 return "$path/$bin";
517 }
518 }
519
520 return "";
521}
522
523sub recent_git_signoffs {
524 my ($file) = @_;
525
526 my $sign_offs = "";
527 my $cmd = "";
528 my $output = "";
529 my $count = 0;
530 my @lines = ();
afa81ee1 531 my $total_sign_offs;
cb7301c7
JP
532
533 if (which("git") eq "") {
de2fc492
JP
534 warn("$P: git not found. Add --nogit to options?\n");
535 return;
536 }
537 if (!(-d ".git")) {
5f2441e9
JP
538 warn("$P: .git directory not found. Use a git repository for better results.\n");
539 warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
de2fc492 540 return;
cb7301c7
JP
541 }
542
543 $cmd = "git log --since=${email_git_since} -- ${file}";
de2fc492
JP
544 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
545 if (!$email_git_penguin_chiefs) {
546 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
547 }
4a7fdb5f 548 $cmd .= " | cut -f2- -d\":\"";
cb7301c7
JP
549 $cmd .= " | sort | uniq -c | sort -rn";
550
551 $output = `${cmd}`;
552 $output =~ s/^\s*//gm;
553
554 @lines = split("\n", $output);
afa81ee1
JP
555
556 $total_sign_offs = 0;
557 foreach my $line (@lines) {
558 if ($line =~ m/([0-9]+)\s+(.*)/) {
559 $total_sign_offs += $1;
560 } else {
561 die("$P: Unexpected git output: ${line}\n");
562 }
563 }
564
cb7301c7 565 foreach my $line (@lines) {
4a7fdb5f 566 if ($line =~ m/([0-9]+)\s+(.*)/) {
cb7301c7 567 my $sign_offs = $1;
4a7fdb5f 568 $line = $2;
cb7301c7
JP
569 $count++;
570 if ($sign_offs < $email_git_min_signatures ||
afa81ee1
JP
571 $count > $email_git_max_maintainers ||
572 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
cb7301c7
JP
573 last;
574 }
cb7301c7 575 }
f5492666
JP
576 push_email_address($line);
577 }
578}
579
580sub save_commits {
581 my ($cmd, @commits) = @_;
582 my $output;
583 my @lines = ();
584
585 $output = `${cmd}`;
586
587 @lines = split("\n", $output);
588 foreach my $line (@lines) {
589 if ($line =~ m/^(\w+) /) {
590 push (@commits, $1);
591 }
592 }
593 return @commits;
594}
595
596sub git_assign_blame {
597 my ($file) = @_;
598
599 my @lines = ();
600 my @commits = ();
601 my $cmd;
602 my $output;
603 my %hash;
604 my $total_sign_offs;
605 my $count;
606
607 if (@range) {
608 foreach my $file_range_diff (@range) {
609 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
610 my $diff_file = $1;
611 my $diff_start = $2;
612 my $diff_length = $3;
613 next if (!("$file" eq "$diff_file"));
614 $cmd = "git blame -l -L $diff_start,+$diff_length $file\n";
615 @commits = save_commits($cmd, @commits);
cb7301c7 616 }
f5492666
JP
617 } else {
618 if (-f $file) {
619 $cmd = "git blame -l $file\n";
620 @commits = save_commits($cmd, @commits);
621 }
622 }
623
624 $total_sign_offs = 0;
625 @commits = uniq(@commits);
626 foreach my $commit (@commits) {
627 $cmd = "git log -1 ${commit}";
628 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
629 if (!$email_git_penguin_chiefs) {
630 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
631 }
632 $cmd .= " | cut -f2- -d\":\"";
633
634 $output = `${cmd}`;
635 $output =~ s/^\s*//gm;
636 @lines = split("\n", $output);
637 $hash{$_}++ for @lines;
638 $total_sign_offs += @lines;
639 }
640
641 $count = 0;
642 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
643 my $sign_offs = $hash{$line};
644 $count++;
645 last if ($sign_offs < $email_git_min_signatures ||
646 $count > $email_git_max_maintainers ||
647 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
648 push_email_address($line);
cb7301c7 649 }
cb7301c7
JP
650}
651
652sub uniq {
653 my @parms = @_;
654
655 my %saw;
656 @parms = grep(!$saw{$_}++, @parms);
657 return @parms;
658}
659
660sub sort_and_uniq {
661 my @parms = @_;
662
663 my %saw;
664 @parms = sort @parms;
665 @parms = grep(!$saw{$_}++, @parms);
666 return @parms;
667}
668
669sub output {
670 my @parms = @_;
671
672 if ($output_multiline) {
673 foreach my $line (@parms) {
674 print("${line}\n");
675 }
676 } else {
677 print(join($output_separator, @parms));
678 print("\n");
679 }
680}
1b5e1cf6
JP
681
682my $rfc822re;
683
684sub make_rfc822re {
685# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
686# comment. We must allow for rfc822_lwsp (or comments) after each of these.
687# This regexp will only work on addresses which have had comments stripped
688# and replaced with rfc822_lwsp.
689
690 my $specials = '()<>@,;:\\\\".\\[\\]';
691 my $controls = '\\000-\\037\\177';
692
693 my $dtext = "[^\\[\\]\\r\\\\]";
694 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
695
696 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
697
698# Use zero-width assertion to spot the limit of an atom. A simple
699# $rfc822_lwsp* causes the regexp engine to hang occasionally.
700 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
701 my $word = "(?:$atom|$quoted_string)";
702 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
703
704 my $sub_domain = "(?:$atom|$domain_literal)";
705 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
706
707 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
708
709 my $phrase = "$word*";
710 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
711 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
712 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
713
714 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
715 my $address = "(?:$mailbox|$group)";
716
717 return "$rfc822_lwsp*$address";
718}
719
720sub rfc822_strip_comments {
721 my $s = shift;
722# Recursively remove comments, and replace with a single space. The simpler
723# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
724# chars in atoms, for example.
725
726 while ($s =~ s/^((?:[^"\\]|\\.)*
727 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
728 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
729 return $s;
730}
731
732# valid: returns true if the parameter is an RFC822 valid address
733#
734sub rfc822_valid ($) {
735 my $s = rfc822_strip_comments(shift);
736
737 if (!$rfc822re) {
738 $rfc822re = make_rfc822re();
739 }
740
741 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
742}
743
744# validlist: In scalar context, returns true if the parameter is an RFC822
745# valid list of addresses.
746#
747# In list context, returns an empty list on failure (an invalid
748# address was found); otherwise a list whose first element is the
749# number of addresses found and whose remaining elements are the
750# addresses. This is needed to disambiguate failure (invalid)
751# from success with no addresses found, because an empty string is
752# a valid list.
753
754sub rfc822_validlist ($) {
755 my $s = rfc822_strip_comments(shift);
756
757 if (!$rfc822re) {
758 $rfc822re = make_rfc822re();
759 }
760 # * null list items are valid according to the RFC
761 # * the '1' business is to aid in distinguishing failure from no results
762
763 my @r;
764 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
765 $s =~ m/^$rfc822_char*$/) {
5f2441e9 766 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
1b5e1cf6
JP
767 push @r, $1;
768 }
769 return wantarray ? (scalar(@r), @r) : 1;
770 }
771 else {
772 return wantarray ? () : 0;
773 }
774}