| 1 |
# This module is part of debbugs, and is released |
| 2 |
# under the terms of the GPL version 2, or any later version. See the |
| 3 |
# file README and COPYING for more information. |
| 4 |
# Copyright 2008 by Don Armstrong <[email protected]>. |
| 5 |
# $Id: perl_module_header.pm 1221 2008-05-19 15:00:40Z don $ |
| 6 |
|
| 7 |
package Debbugs::Recipients; |
| 8 |
|
| 9 |
=head1 NAME |
| 10 |
|
| 11 |
Debbugs::Recipients -- Determine recipients of messages from the bts |
| 12 |
|
| 13 |
=head1 SYNOPSIS |
| 14 |
|
| 15 |
|
| 16 |
=head1 DESCRIPTION |
| 17 |
|
| 18 |
|
| 19 |
=head1 BUGS |
| 20 |
|
| 21 |
None known. |
| 22 |
|
| 23 |
=cut |
| 24 |
|
| 25 |
use warnings; |
| 26 |
use strict; |
| 27 |
use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT); |
| 28 |
use Exporter qw(import); |
| 29 |
|
| 30 |
BEGIN{ |
| 31 |
($VERSION) = q$Revision: 1221 $ =~ /^Revision:\s+([^\s+])/; |
| 32 |
$DEBUG = 0 unless defined $DEBUG; |
| 33 |
|
| 34 |
@EXPORT = (); |
| 35 |
%EXPORT_TAGS = (add => [qw(add_recipients)], |
| 36 |
det => [qw(determine_recipients)], |
| 37 |
); |
| 38 |
@EXPORT_OK = (); |
| 39 |
Exporter::export_ok_tags(keys %EXPORT_TAGS); |
| 40 |
$EXPORT_TAGS{all} = [@EXPORT_OK]; |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
use Debbugs::Config qw(:config); |
| 45 |
use Params::Validate qw(:types validate_with); |
| 46 |
use Debbugs::Common qw(:misc :util); |
| 47 |
use Debbugs::Status qw(splitpackages isstrongseverity); |
| 48 |
|
| 49 |
use Debbugs::Packages qw(binary_to_source); |
| 50 |
|
| 51 |
use Debbugs::Mail qw(get_addresses); |
| 52 |
|
| 53 |
use Carp; |
| 54 |
|
| 55 |
=head2 add_recipients |
| 56 |
|
| 57 |
add_recipients(data => $data, |
| 58 |
recipients => \%recipients; |
| 59 |
); |
| 60 |
|
| 61 |
Given data (from read_bug or similar) (or an arrayref of data), |
| 62 |
calculates the addresses which need to receive mail involving this |
| 63 |
bug. |
| 64 |
|
| 65 |
=over |
| 66 |
|
| 67 |
=item data -- Data from read_bug or similar; can be an arrayref of data |
| 68 |
|
| 69 |
=item recipients -- hashref of recipient data structure; pass to |
| 70 |
subsequent calls of add_recipients or |
| 71 |
|
| 72 |
=item debug -- optional |
| 73 |
|
| 74 |
|
| 75 |
=back |
| 76 |
|
| 77 |
=cut |
| 78 |
|
| 79 |
|
| 80 |
sub add_recipients { |
| 81 |
# Data structure is: |
| 82 |
# maintainer email address &c -> assoc of packages -> assoc of bug#'s |
| 83 |
my %param = validate_with(params => \@_, |
| 84 |
spec => {data => {type => HASHREF|ARRAYREF, |
| 85 |
}, |
| 86 |
recipients => {type => HASHREF, |
| 87 |
}, |
| 88 |
debug => {type => HANDLE|SCALARREF, |
| 89 |
optional => 1, |
| 90 |
}, |
| 91 |
transcript => {type => HANDLE|SCALARREF, |
| 92 |
optional => 1, |
| 93 |
}, |
| 94 |
actions_taken => {type => HASHREF, |
| 95 |
default => {}, |
| 96 |
}, |
| 97 |
unknown_packages => {type => HASHREF, |
| 98 |
default => {}, |
| 99 |
}, |
| 100 |
received_at_quiet => {type => SCALAR, |
| 101 |
default => 0, |
| 102 |
}, |
| 103 |
additional_cc => {type => SCALAR|ARRAYREF, |
| 104 |
optional => 1, |
| 105 |
}, |
| 106 |
}, |
| 107 |
); |
| 108 |
|
| 109 |
$param{transcript} = globify_scalar($param{transcript}); |
| 110 |
$param{debug} = globify_scalar($param{debug}); |
| 111 |
if (ref ($param{data}) eq 'ARRAY') { |
| 112 |
for my $data (@{$param{data}}) { |
| 113 |
add_recipients(data => $data, |
| 114 |
map {exists $param{$_}?($_,$param{$_}):()} |
| 115 |
qw(recipients debug transcript actions_taken unknown_packages received_at_quiet) |
| 116 |
); |
| 117 |
} |
| 118 |
return; |
| 119 |
} |
| 120 |
my ($addmaint); |
| 121 |
my $ref = $param{data}{bug_num}; |
| 122 |
for my $p (splitpackages($param{data}{package})) { |
| 123 |
$p = lc($p); |
| 124 |
if (defined $config{subscription_domain}) { |
| 125 |
my @source_packages = binary_to_source(binary => $p, |
| 126 |
source_only => 1, |
| 127 |
); |
| 128 |
if (@source_packages) { |
| 129 |
for my $source (@source_packages) { |
| 130 |
_add_address(recipients => $param{recipients}, |
| 131 |
address => "$source\@".$config{subscription_domain}, |
| 132 |
reason => $source, |
| 133 |
type => 'bcc', |
| 134 |
); |
| 135 |
} |
| 136 |
} |
| 137 |
else { |
| 138 |
_add_address(recipients => $param{recipients}, |
| 139 |
address => "$p\@".$config{subscription_domain}, |
| 140 |
reason => $p, |
| 141 |
type => 'bcc', |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
if (not $param{received_at_quiet} and defined $param{data}{severity} and defined $config{strong_list} and |
| 146 |
isstrongseverity($param{data}{severity})) { |
| 147 |
_add_address(recipients => $param{recipients}, |
| 148 |
address => "$config{strong_list}\@".$config{list_domain}, |
| 149 |
reason => $param{data}{severity}, |
| 150 |
type => 'bcc', |
| 151 |
); |
| 152 |
} |
| 153 |
my @maints = package_maintainer(binary => $p); |
| 154 |
if (@maints) { |
| 155 |
print {$param{debug}} "MR|".join(',',@maints)."|$p|$ref|\n"; |
| 156 |
_add_address(recipients => $param{recipients}, |
| 157 |
address => \@maints, |
| 158 |
reason => $p, |
| 159 |
bug_num => $param{data}{bug_num}, |
| 160 |
type => 'cc', |
| 161 |
); |
| 162 |
print {$param{debug}} "maintainer add >$p|".join(',',@maints)."<\n"; |
| 163 |
} |
| 164 |
else { |
| 165 |
print {$param{debug}} "maintainer none >$p<\n"; |
| 166 |
if (not exists $param{unknown_packages}{$p}) { |
| 167 |
print {$param{transcript}} "Warning: Unknown package '$p'\n"; |
| 168 |
$param{unknown_packages}{$p} = 1; |
| 169 |
} |
| 170 |
print {$param{debug}} "MR|unknown-package|$p|$ref|\n"; |
| 171 |
_add_address(recipients => $param{recipients}, |
| 172 |
address => $config{unknown_maintainer_email}, |
| 173 |
reason => $p, |
| 174 |
bug_num => $param{data}{bug_num}, |
| 175 |
type => 'cc', |
| 176 |
) |
| 177 |
if defined $config{unknown_maintainer_email} and |
| 178 |
length $config{unknown_maintainer_email}; |
| 179 |
} |
| 180 |
} |
| 181 |
if (defined $config{bug_subscription_domain} and |
| 182 |
length $config{bug_subscription_domain}) { |
| 183 |
_add_address(recipients => $param{recipients}, |
| 184 |
address => 'bugs='.$param{data}{bug_num}.'@'. |
| 185 |
$config{bug_subscription_domain}, |
| 186 |
reason => "bug $param{data}{bug_num}", |
| 187 |
bug_num => $param{data}{bug_num}, |
| 188 |
type => 'bcc', |
| 189 |
); |
| 190 |
} |
| 191 |
if (defined $config{cc_all_mails_to_addr} and |
| 192 |
length $config{cc_all_mails_to_addr} |
| 193 |
) { |
| 194 |
_add_address(recipients => $param{recipients}, |
| 195 |
address => $config{cc_all_mails_to_addr}, |
| 196 |
reason => "cc_all_mails_to_addr", |
| 197 |
bug_num => $param{data}{bug_num}, |
| 198 |
type => 'bcc', |
| 199 |
); |
| 200 |
} |
| 201 |
|
| 202 |
if (length $param{data}{owner}) { |
| 203 |
$addmaint = $param{data}{owner}; |
| 204 |
print {$param{debug}} "MO|$addmaint|$param{data}{package}|$ref|\n"; |
| 205 |
_add_address(recipients => $param{recipients}, |
| 206 |
address => $addmaint, |
| 207 |
reason => "owner of $param{data}{bug_num}", |
| 208 |
bug_num => $param{data}{bug_num}, |
| 209 |
type => 'cc', |
| 210 |
); |
| 211 |
print {$param{debug}} "owner add >$param{data}{package}|$addmaint<\n"; |
| 212 |
} |
| 213 |
if (exists $param{actions_taken}) { |
| 214 |
if (exists $param{actions_taken}{done} and |
| 215 |
$param{actions_taken}{done} and |
| 216 |
length($config{done_list}) and |
| 217 |
length($config{list_domain}) |
| 218 |
) { |
| 219 |
_add_address(recipients => $param{recipients}, |
| 220 |
type => 'cc', |
| 221 |
address => $config{done_list}.'@'.$config{list_domain}, |
| 222 |
bug_num => $param{data}{bug_num}, |
| 223 |
reason => "bug $param{data}{bug_num} done", |
| 224 |
); |
| 225 |
} |
| 226 |
if (exists $param{actions_taken}{forwarded} and |
| 227 |
$param{actions_taken}{forwarded} and |
| 228 |
length($config{forward_list}) and |
| 229 |
length($config{list_domain}) |
| 230 |
) { |
| 231 |
_add_address(recipients => $param{recipients}, |
| 232 |
type => 'cc', |
| 233 |
address => $config{forward_list}.'@'.$config{list_domain}, |
| 234 |
bug_num => $param{data}{bug_num}, |
| 235 |
reason => "bug $param{data}{bug_num} forwarded", |
| 236 |
); |
| 237 |
} |
| 238 |
} |
| 239 |
if ( exists $param{additional_cc} ) { |
| 240 |
for my $address ( make_list( $param{additional_cc} ) ) { |
| 241 |
_add_address( |
| 242 |
recipients => $param{recipients}, |
| 243 |
type => 'cc', |
| 244 |
address => $address, |
| 245 |
bug_num => $param{data}{bug_num}, |
| 246 |
reason => "additional cc recipient", |
| 247 |
); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
=head2 determine_recipients |
| 254 |
|
| 255 |
my @recipients = determine_recipients(recipients => \%recipients, |
| 256 |
bcc => 1, |
| 257 |
); |
| 258 |
my %recipients => determine_recipients(recipients => \%recipients,); |
| 259 |
|
| 260 |
# or a crazy example: |
| 261 |
send_mail_message(message => $message, |
| 262 |
recipients => |
| 263 |
[make_list( |
| 264 |
values %{{determine_recipients( |
| 265 |
recipients => \%recipients) |
| 266 |
}}) |
| 267 |
], |
| 268 |
); |
| 269 |
|
| 270 |
Using the recipient hashref, determines the set of recipients. |
| 271 |
|
| 272 |
If you specify one of C<bcc>, C<cc>, or C<to>, you will receive only a |
| 273 |
LIST of recipients which the main should be Bcc'ed, Cc'ed, or To'ed |
| 274 |
respectively. By default, a LIST with keys bcc, cc, and to is returned |
| 275 |
with ARRAYREF values corresponding to the users to whom a message |
| 276 |
should be sent. |
| 277 |
|
| 278 |
=over |
| 279 |
|
| 280 |
=item address_only -- whether to only return mail addresses without reasons or realnamesq |
| 281 |
|
| 282 |
=back |
| 283 |
|
| 284 |
Passing more than one of bcc, cc or to is a fatal error. |
| 285 |
|
| 286 |
=cut |
| 287 |
|
| 288 |
sub determine_recipients { |
| 289 |
my %param = validate_with(params => \@_, |
| 290 |
spec => {recipients => {type => HASHREF, |
| 291 |
}, |
| 292 |
bcc => {type => BOOLEAN, |
| 293 |
default => 0, |
| 294 |
}, |
| 295 |
cc => {type => BOOLEAN, |
| 296 |
default => 0, |
| 297 |
}, |
| 298 |
to => {type => BOOLEAN, |
| 299 |
default => 0, |
| 300 |
}, |
| 301 |
address_only => {type => BOOLEAN, |
| 302 |
default => 0, |
| 303 |
} |
| 304 |
}, |
| 305 |
); |
| 306 |
|
| 307 |
if (1 < scalar grep {$param{$_}} qw(to cc bcc)) { |
| 308 |
croak "Passing more than one of to, cc, or bcc is non-sensical"; |
| 309 |
} |
| 310 |
|
| 311 |
my %final_recipients; |
| 312 |
# start with the to recipients |
| 313 |
for my $addr (keys %{$param{recipients}}) { |
| 314 |
my $level = 'bcc'; |
| 315 |
my @reasons; |
| 316 |
for my $reason (keys %{$param{recipients}{$addr}}) { |
| 317 |
my @bugs; |
| 318 |
for my $bug (keys %{$param{recipients}{$addr}{$reason}}) { |
| 319 |
push @bugs, $bug; |
| 320 |
my $t_level = $param{recipients}{$addr}{$reason}{$bug}; |
| 321 |
if ($level eq 'to' or |
| 322 |
$t_level eq 'to') { |
| 323 |
$level = 'to'; |
| 324 |
} |
| 325 |
elsif ($t_level eq 'cc') { |
| 326 |
$level = 'cc'; |
| 327 |
} |
| 328 |
} |
| 329 |
# RFC 2822 comments cannot contain specials and |
| 330 |
# unquoted () or \; there's no reason for us to allow |
| 331 |
# insane things here, though, so we restrict this even |
| 332 |
# more to 20-7E ( -~) |
| 333 |
$reason =~ s/\\/\\\\/g; |
| 334 |
$reason =~ s/([\)\(])/\\$1/g; |
| 335 |
$reason =~ s/[^\x20-\x7E]//g; |
| 336 |
push @reasons, $reason . ' for {'.join(',',@bugs).'}'; |
| 337 |
} |
| 338 |
if ($param{address_only}) { |
| 339 |
push @{$final_recipients{$level}}, get_addresses($addr); |
| 340 |
} |
| 341 |
else { |
| 342 |
push @{$final_recipients{$level}}, $addr . ' ('.join(', ',@reasons).')'; |
| 343 |
} |
| 344 |
} |
| 345 |
for (qw(to cc bcc)) { |
| 346 |
if ($param{$_}) { |
| 347 |
if (exists $final_recipients{$_}) { |
| 348 |
return @{$final_recipients{$_}||[]}; |
| 349 |
} |
| 350 |
return (); |
| 351 |
} |
| 352 |
} |
| 353 |
return %final_recipients; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
=head1 PRIVATE FUNCTIONS |
| 358 |
|
| 359 |
=head2 _add_address |
| 360 |
|
| 361 |
_add_address(recipients => $param{recipients}, |
| 362 |
address => $addmaint, |
| 363 |
reason => $param{data}{package}, |
| 364 |
bug_num => $param{data}{bug_num}, |
| 365 |
type => 'cc', |
| 366 |
); |
| 367 |
|
| 368 |
|
| 369 |
=cut |
| 370 |
|
| 371 |
|
| 372 |
sub _add_address { |
| 373 |
my %param = validate_with(params => \@_, |
| 374 |
spec => {recipients => {type => HASHREF, |
| 375 |
}, |
| 376 |
bug_num => {type => SCALAR, |
| 377 |
regex => qr/^\d*$/, |
| 378 |
default => '', |
| 379 |
}, |
| 380 |
reason => {type => SCALAR, |
| 381 |
default => '', |
| 382 |
}, |
| 383 |
address => {type => SCALAR|ARRAYREF, |
| 384 |
}, |
| 385 |
type => {type => SCALAR, |
| 386 |
default => 'cc', |
| 387 |
regex => qr/^(?:b?cc|to)$/i, |
| 388 |
}, |
| 389 |
}, |
| 390 |
); |
| 391 |
for my $addr (make_list($param{address})) { |
| 392 |
if (lc($param{type}) eq 'bcc' and |
| 393 |
exists $param{recipients}{$addr}{$param{reason}}{$param{bug_num}} |
| 394 |
) { |
| 395 |
next; |
| 396 |
} |
| 397 |
elsif (lc($param{type}) eq 'cc' and |
| 398 |
exists $param{recipients}{$addr}{$param{reason}}{$param{bug_num}} |
| 399 |
and $param{recipients}{$addr}{$param{reason}}{$param{bug_num}} eq 'to' |
| 400 |
) { |
| 401 |
next; |
| 402 |
} |
| 403 |
$param{recipients}{$addr}{$param{reason}}{$param{bug_num}} = lc($param{type}); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
1; |
| 408 |
|
| 409 |
|
| 410 |
__END__ |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|