]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - scripts/get_maintainer.pl
scripts/get_maintainer.pl: add --git-blame
[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;
214 }
215 }
216 }
217 }
218
219 if (!$exclude) {
220 my $tvi = 0;
221 foreach my $line (@typevalue) {
290603c1 222 if ($line =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
223 my $type = $1;
224 my $value = $2;
225 if ($type eq 'F') {
226 if (file_match_pattern($file, $value)) {
227 add_categories($tvi);
228 }
229 }
230 }
231 $tvi++;
232 }
233 }
234
4a7fdb5f 235 if ($email && $email_git) {
cb7301c7
JP
236 recent_git_signoffs($file);
237 }
238
f5492666
JP
239 if ($email && $email_git_blame) {
240 git_assign_blame($file);
241 }
cb7301c7
JP
242}
243
f5f5078d 244if ($email) {
cb7301c7
JP
245 foreach my $chief (@penguin_chief) {
246 if ($chief =~ m/^(.*):(.*)/) {
f5f5078d 247 my $email_address;
cb7301c7 248 if ($email_usename) {
f5f5078d 249 $email_address = format_email($1, $2);
cb7301c7 250 } else {
f5f5078d
JP
251 $email_address = $2;
252 }
253 if ($email_git_penguin_chiefs) {
254 push(@email_to, $email_address);
255 } else {
256 @email_to = grep(!/${email_address}/, @email_to);
cb7301c7
JP
257 }
258 }
259 }
260}
261
290603c1
JP
262if ($email || $email_list) {
263 my @to = ();
264 if ($email) {
265 @to = (@to, @email_to);
cb7301c7 266 }
290603c1 267 if ($email_list) {
290603c1 268 @to = (@to, @list_to);
290603c1
JP
269 }
270 output(uniq(@to));
cb7301c7
JP
271}
272
273if ($scm) {
4a7fdb5f 274 @scm = sort_and_uniq(@scm);
cb7301c7
JP
275 output(@scm);
276}
277
278if ($status) {
4a7fdb5f 279 @status = sort_and_uniq(@status);
cb7301c7
JP
280 output(@status);
281}
282
283if ($subsystem) {
4a7fdb5f 284 @subsystem = sort_and_uniq(@subsystem);
cb7301c7
JP
285 output(@subsystem);
286}
287
288if ($web) {
4a7fdb5f 289 @web = sort_and_uniq(@web);
cb7301c7
JP
290 output(@web);
291}
292
293exit($exit);
294
295sub file_match_pattern {
296 my ($file, $pattern) = @_;
297 if (substr($pattern, -1) eq "/") {
298 if ($file =~ m@^$pattern@) {
299 return 1;
300 }
301 } else {
302 if ($file =~ m@^$pattern@) {
303 my $s1 = ($file =~ tr@/@@);
304 my $s2 = ($pattern =~ tr@/@@);
305 if ($s1 == $s2) {
306 return 1;
307 }
308 }
309 }
310 return 0;
311}
312
313sub usage {
314 print <<EOT;
315usage: $P [options] patchfile
870020f9 316 $P [options] -f file|directory
cb7301c7
JP
317version: $V
318
319MAINTAINER field selection options:
320 --email => print email address(es) if any
321 --git => include recent git \*-by: signers
322 --git-chief-penguins => include ${penguin_chiefs}
323 --git-min-signatures => number of signatures required (default: 1)
324 --git-max-maintainers => maximum maintainers to add (default: 5)
3d202aeb 325 --git-min-percent => minimum percentage of commits required (default: 5)
cb7301c7 326 --git-since => git history to use (default: 1-year-ago)
f5492666 327 --git-blame => use git blame to find modified commits for patch or file
cb7301c7
JP
328 --m => include maintainer(s) if any
329 --n => include name 'Full Name <addr\@domain.tld>'
330 --l => include list(s) if any
331 --s => include subscriber only list(s) if any
332 --scm => print SCM tree(s) if any
333 --status => print status if any
334 --subsystem => print subsystem name if any
335 --web => print website(s) if any
336
337Output type options:
338 --separator [, ] => separator for multiple entries on 1 line
339 --multiline => print 1 entry per line
340
341Default options:
290603c1 342 [--email --git --m --n --l --multiline]
cb7301c7
JP
343
344Other options:
f5f5078d 345 --version => show version
cb7301c7
JP
346 --help => show this help information
347
870020f9
JP
348Notes:
349 Using "-f directory" may give unexpected results:
f5492666
JP
350 Used with "--git", git signators for _all_ files in and below
351 directory are examined as git recurses directories.
352 Any specified X: (exclude) pattern matches are _not_ ignored.
353 Used with "--nogit", directory is used as a pattern match,
354 no individual file within the directory or subdirectory
355 is matched.
356 Used with "--git-blame", does not iterate all files in directory
357 Using "--git-blame" is slow and may add old committers and authors
358 that are no longer active maintainers to the output.
cb7301c7
JP
359EOT
360}
361
362sub top_of_kernel_tree {
363 my ($lk_path) = @_;
364
365 if ($lk_path ne "" && substr($lk_path,length($lk_path)-1,1) ne "/") {
366 $lk_path .= "/";
367 }
368 if ( (-f "${lk_path}COPYING")
369 && (-f "${lk_path}CREDITS")
370 && (-f "${lk_path}Kbuild")
371 && (-f "${lk_path}MAINTAINERS")
372 && (-f "${lk_path}Makefile")
373 && (-f "${lk_path}README")
374 && (-d "${lk_path}Documentation")
375 && (-d "${lk_path}arch")
376 && (-d "${lk_path}include")
377 && (-d "${lk_path}drivers")
378 && (-d "${lk_path}fs")
379 && (-d "${lk_path}init")
380 && (-d "${lk_path}ipc")
381 && (-d "${lk_path}kernel")
382 && (-d "${lk_path}lib")
383 && (-d "${lk_path}scripts")) {
384 return 1;
385 }
386 return 0;
387}
388
389sub format_email {
390 my ($name, $email) = @_;
391
392 $name =~ s/^\s+|\s+$//g;
d789504a 393 $name =~ s/^\"|\"$//g;
cb7301c7
JP
394 $email =~ s/^\s+|\s+$//g;
395
396 my $formatted_email = "";
397
398 if ($name =~ /[^a-z0-9 \.\-]/i) { ##has "must quote" chars
399 $name =~ s/(?<!\\)"/\\"/g; ##escape quotes
400 $formatted_email = "\"${name}\"\ \<${email}\>";
401 } else {
402 $formatted_email = "${name} \<${email}\>";
403 }
404 return $formatted_email;
405}
406
407sub add_categories {
408 my ($index) = @_;
409
410 $index = $index - 1;
411 while ($index >= 0) {
412 my $tv = $typevalue[$index];
290603c1 413 if ($tv =~ m/^(\C):\s*(.*)/) {
cb7301c7
JP
414 my $ptype = $1;
415 my $pvalue = $2;
416 if ($ptype eq "L") {
290603c1
JP
417 my $list_address = $pvalue;
418 my $list_additional = "";
419 if ($list_address =~ m/([^\s]+)\s+(.*)$/) {
420 $list_address = $1;
421 $list_additional = $2;
422 }
bdf7c685 423 if ($list_additional =~ m/subscribers-only/) {
cb7301c7 424 if ($email_subscriber_list) {
290603c1 425 push(@list_to, $list_address);
cb7301c7
JP
426 }
427 } else {
428 if ($email_list) {
290603c1 429 push(@list_to, $list_address);
cb7301c7
JP
430 }
431 }
432 } elsif ($ptype eq "M") {
5f2441e9
JP
433 my $p_used = 0;
434 if ($index >= 0) {
435 my $tv = $typevalue[$index - 1];
436 if ($tv =~ m/^(\C):\s*(.*)/) {
437 if ($1 eq "P") {
438 if ($email_usename) {
439 push_email_address(format_email($2, $pvalue));
440 $p_used = 1;
441 }
442 }
443 }
444 }
445 if (!$p_used) {
1b5e1cf6 446 push_email_addresses($pvalue);
cb7301c7
JP
447 }
448 } elsif ($ptype eq "T") {
449 push(@scm, $pvalue);
450 } elsif ($ptype eq "W") {
451 push(@web, $pvalue);
452 } elsif ($ptype eq "S") {
453 push(@status, $pvalue);
454 }
455
456 $index--;
457 } else {
458 push(@subsystem,$tv);
459 $index = -1;
460 }
461 }
462}
463
1b5e1cf6
JP
464sub push_email_address {
465 my ($email_address) = @_;
466
467 my $email_name = "";
1b5e1cf6 468
0a79c492 469 if ($email_maintainer) {
f5492666
JP
470 if ($email_address =~ m/([^<]+)<(.*\@.*)>$/) {
471 $email_name = $1;
472 $email_address = $2;
473 if ($email_usename) {
474 push(@email_to, format_email($email_name, $email_address));
475 } else {
476 push(@email_to, $email_address);
477 }
478 } elsif ($email_address =~ m/<(.+)>/) {
479 $email_address = $1;
480 push(@email_to, $email_address);
0a79c492
JP
481 } else {
482 push(@email_to, $email_address);
483 }
1b5e1cf6
JP
484 }
485}
486
487sub push_email_addresses {
488 my ($address) = @_;
489
490 my @address_list = ();
491
5f2441e9
JP
492 if (rfc822_valid($address)) {
493 push_email_address($address);
494 } elsif (@address_list = rfc822_validlist($address)) {
1b5e1cf6
JP
495 my $array_count = shift(@address_list);
496 while (my $entry = shift(@address_list)) {
497 push_email_address($entry);
498 }
5f2441e9
JP
499 } else {
500 warn("Invalid MAINTAINERS address: '" . $address . "'\n");
1b5e1cf6 501 }
1b5e1cf6
JP
502}
503
cb7301c7
JP
504sub which {
505 my ($bin) = @_;
506
f5f5078d 507 foreach my $path (split(/:/, $ENV{PATH})) {
cb7301c7
JP
508 if (-e "$path/$bin") {
509 return "$path/$bin";
510 }
511 }
512
513 return "";
514}
515
516sub recent_git_signoffs {
517 my ($file) = @_;
518
519 my $sign_offs = "";
520 my $cmd = "";
521 my $output = "";
522 my $count = 0;
523 my @lines = ();
afa81ee1 524 my $total_sign_offs;
cb7301c7
JP
525
526 if (which("git") eq "") {
de2fc492
JP
527 warn("$P: git not found. Add --nogit to options?\n");
528 return;
529 }
530 if (!(-d ".git")) {
5f2441e9
JP
531 warn("$P: .git directory not found. Use a git repository for better results.\n");
532 warn("$P: perhaps 'git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git'\n");
de2fc492 533 return;
cb7301c7
JP
534 }
535
536 $cmd = "git log --since=${email_git_since} -- ${file}";
de2fc492
JP
537 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
538 if (!$email_git_penguin_chiefs) {
539 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
540 }
4a7fdb5f 541 $cmd .= " | cut -f2- -d\":\"";
cb7301c7
JP
542 $cmd .= " | sort | uniq -c | sort -rn";
543
544 $output = `${cmd}`;
545 $output =~ s/^\s*//gm;
546
547 @lines = split("\n", $output);
afa81ee1
JP
548
549 $total_sign_offs = 0;
550 foreach my $line (@lines) {
551 if ($line =~ m/([0-9]+)\s+(.*)/) {
552 $total_sign_offs += $1;
553 } else {
554 die("$P: Unexpected git output: ${line}\n");
555 }
556 }
557
cb7301c7 558 foreach my $line (@lines) {
4a7fdb5f 559 if ($line =~ m/([0-9]+)\s+(.*)/) {
cb7301c7 560 my $sign_offs = $1;
4a7fdb5f 561 $line = $2;
cb7301c7
JP
562 $count++;
563 if ($sign_offs < $email_git_min_signatures ||
afa81ee1
JP
564 $count > $email_git_max_maintainers ||
565 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent) {
cb7301c7
JP
566 last;
567 }
cb7301c7 568 }
f5492666
JP
569 push_email_address($line);
570 }
571}
572
573sub save_commits {
574 my ($cmd, @commits) = @_;
575 my $output;
576 my @lines = ();
577
578 $output = `${cmd}`;
579
580 @lines = split("\n", $output);
581 foreach my $line (@lines) {
582 if ($line =~ m/^(\w+) /) {
583 push (@commits, $1);
584 }
585 }
586 return @commits;
587}
588
589sub git_assign_blame {
590 my ($file) = @_;
591
592 my @lines = ();
593 my @commits = ();
594 my $cmd;
595 my $output;
596 my %hash;
597 my $total_sign_offs;
598 my $count;
599
600 if (@range) {
601 foreach my $file_range_diff (@range) {
602 next if (!($file_range_diff =~ m/(.+):(.+):(.+)/));
603 my $diff_file = $1;
604 my $diff_start = $2;
605 my $diff_length = $3;
606 next if (!("$file" eq "$diff_file"));
607 $cmd = "git blame -l -L $diff_start,+$diff_length $file\n";
608 @commits = save_commits($cmd, @commits);
cb7301c7 609 }
f5492666
JP
610 } else {
611 if (-f $file) {
612 $cmd = "git blame -l $file\n";
613 @commits = save_commits($cmd, @commits);
614 }
615 }
616
617 $total_sign_offs = 0;
618 @commits = uniq(@commits);
619 foreach my $commit (@commits) {
620 $cmd = "git log -1 ${commit}";
621 $cmd .= " | grep -Ei \"^[-_ a-z]+by:.*\\\@.*\$\"";
622 if (!$email_git_penguin_chiefs) {
623 $cmd .= " | grep -Ev \"${penguin_chiefs}\"";
624 }
625 $cmd .= " | cut -f2- -d\":\"";
626
627 $output = `${cmd}`;
628 $output =~ s/^\s*//gm;
629 @lines = split("\n", $output);
630 $hash{$_}++ for @lines;
631 $total_sign_offs += @lines;
632 }
633
634 $count = 0;
635 foreach my $line (sort {$hash{$b} <=> $hash{$a}} keys %hash) {
636 my $sign_offs = $hash{$line};
637 $count++;
638 last if ($sign_offs < $email_git_min_signatures ||
639 $count > $email_git_max_maintainers ||
640 $sign_offs * 100 / $total_sign_offs < $email_git_min_percent);
641 push_email_address($line);
cb7301c7 642 }
cb7301c7
JP
643}
644
645sub uniq {
646 my @parms = @_;
647
648 my %saw;
649 @parms = grep(!$saw{$_}++, @parms);
650 return @parms;
651}
652
653sub sort_and_uniq {
654 my @parms = @_;
655
656 my %saw;
657 @parms = sort @parms;
658 @parms = grep(!$saw{$_}++, @parms);
659 return @parms;
660}
661
662sub output {
663 my @parms = @_;
664
665 if ($output_multiline) {
666 foreach my $line (@parms) {
667 print("${line}\n");
668 }
669 } else {
670 print(join($output_separator, @parms));
671 print("\n");
672 }
673}
1b5e1cf6
JP
674
675my $rfc822re;
676
677sub make_rfc822re {
678# Basic lexical tokens are specials, domain_literal, quoted_string, atom, and
679# comment. We must allow for rfc822_lwsp (or comments) after each of these.
680# This regexp will only work on addresses which have had comments stripped
681# and replaced with rfc822_lwsp.
682
683 my $specials = '()<>@,;:\\\\".\\[\\]';
684 my $controls = '\\000-\\037\\177';
685
686 my $dtext = "[^\\[\\]\\r\\\\]";
687 my $domain_literal = "\\[(?:$dtext|\\\\.)*\\]$rfc822_lwsp*";
688
689 my $quoted_string = "\"(?:[^\\\"\\r\\\\]|\\\\.|$rfc822_lwsp)*\"$rfc822_lwsp*";
690
691# Use zero-width assertion to spot the limit of an atom. A simple
692# $rfc822_lwsp* causes the regexp engine to hang occasionally.
693 my $atom = "[^$specials $controls]+(?:$rfc822_lwsp+|\\Z|(?=[\\[\"$specials]))";
694 my $word = "(?:$atom|$quoted_string)";
695 my $localpart = "$word(?:\\.$rfc822_lwsp*$word)*";
696
697 my $sub_domain = "(?:$atom|$domain_literal)";
698 my $domain = "$sub_domain(?:\\.$rfc822_lwsp*$sub_domain)*";
699
700 my $addr_spec = "$localpart\@$rfc822_lwsp*$domain";
701
702 my $phrase = "$word*";
703 my $route = "(?:\@$domain(?:,\@$rfc822_lwsp*$domain)*:$rfc822_lwsp*)";
704 my $route_addr = "\\<$rfc822_lwsp*$route?$addr_spec\\>$rfc822_lwsp*";
705 my $mailbox = "(?:$addr_spec|$phrase$route_addr)";
706
707 my $group = "$phrase:$rfc822_lwsp*(?:$mailbox(?:,\\s*$mailbox)*)?;\\s*";
708 my $address = "(?:$mailbox|$group)";
709
710 return "$rfc822_lwsp*$address";
711}
712
713sub rfc822_strip_comments {
714 my $s = shift;
715# Recursively remove comments, and replace with a single space. The simpler
716# regexps in the Email Addressing FAQ are imperfect - they will miss escaped
717# chars in atoms, for example.
718
719 while ($s =~ s/^((?:[^"\\]|\\.)*
720 (?:"(?:[^"\\]|\\.)*"(?:[^"\\]|\\.)*)*)
721 \((?:[^()\\]|\\.)*\)/$1 /osx) {}
722 return $s;
723}
724
725# valid: returns true if the parameter is an RFC822 valid address
726#
727sub rfc822_valid ($) {
728 my $s = rfc822_strip_comments(shift);
729
730 if (!$rfc822re) {
731 $rfc822re = make_rfc822re();
732 }
733
734 return $s =~ m/^$rfc822re$/so && $s =~ m/^$rfc822_char*$/;
735}
736
737# validlist: In scalar context, returns true if the parameter is an RFC822
738# valid list of addresses.
739#
740# In list context, returns an empty list on failure (an invalid
741# address was found); otherwise a list whose first element is the
742# number of addresses found and whose remaining elements are the
743# addresses. This is needed to disambiguate failure (invalid)
744# from success with no addresses found, because an empty string is
745# a valid list.
746
747sub rfc822_validlist ($) {
748 my $s = rfc822_strip_comments(shift);
749
750 if (!$rfc822re) {
751 $rfc822re = make_rfc822re();
752 }
753 # * null list items are valid according to the RFC
754 # * the '1' business is to aid in distinguishing failure from no results
755
756 my @r;
757 if ($s =~ m/^(?:$rfc822re)?(?:,(?:$rfc822re)?)*$/so &&
758 $s =~ m/^$rfc822_char*$/) {
5f2441e9 759 while ($s =~ m/(?:^|,$rfc822_lwsp*)($rfc822re)/gos) {
1b5e1cf6
JP
760 push @r, $1;
761 }
762 return wantarray ? (scalar(@r), @r) : 1;
763 }
764 else {
765 return wantarray ? () : 0;
766 }
767}