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