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 <don@donarmstrong.com>.
5 # $Id: perl_module_header.pm 1221 2008-05-19 15:00:40Z don $
7 package Debbugs::Recipients;
11 Debbugs::Recipients -- Determine recipients of messages from the bts
27 use vars qw($VERSION $DEBUG %EXPORT_TAGS @EXPORT_OK @EXPORT);
28 use Exporter qw(import);
31 ($VERSION) = q$Revision: 1221 $ =~ /^Revision:\s+([^\s+])/;
32 $DEBUG = 0 unless defined $DEBUG;
35 %EXPORT_TAGS = (add => [qw(add_recipients)],
36 det => [qw(determine_recipients)],
39 Exporter::export_ok_tags(keys %EXPORT_TAGS);
40 $EXPORT_TAGS{all} = [@EXPORT_OK];
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);
49 use Debbugs::Packages qw(binary_to_source);
51 use Debbugs::Mail qw(get_addresses);
57 add_recipients(data => $data,
58 recipients => \%recipients;
61 Given data (from read_bug or similar) (or an arrayref of data),
62 calculates the addresses which need to receive mail involving this
67 =item data -- Data from read_bug or similar; can be an arrayref of data
69 =item recipients -- hashref of recipient data structure; pass to
70 subsequent calls of add_recipients or
72 =item debug -- optional
82 # maintainer email address &c -> assoc of packages -> assoc of bug#'s
83 my %param = validate_with(params => \@_,
84 spec => {data => {type => HASHREF|ARRAYREF,
86 recipients => {type => HASHREF,
88 debug => {type => HANDLE|SCALARREF,
91 transcript => {type => HANDLE|SCALARREF,
94 actions_taken => {type => HASHREF,
97 unknown_packages => {type => HASHREF,
103 $param{transcript} = globify_scalar($param{transcript});
104 $param{debug} = globify_scalar($param{debug});
105 if (ref ($param{data}) eq 'ARRAY') {
106 for my $data (@{$param{data}}) {
107 add_recipients(data => $data,
108 map {exists $param{$_}?($_,$param{$_}):()}
109 qw(recipients debug transcript actions_taken unknown_packages)
115 my $ref = $param{data}{bug_num};
116 for my $p (splitpackages($param{data}{package})) {
118 if (defined $config{subscription_domain}) {
119 my @source_packages = binary_to_source(binary => $p,
122 if (@source_packages) {
123 for my $source (@source_packages) {
124 _add_address(recipients => $param{recipients},
125 address => "$source\@".$config{subscription_domain},
132 _add_address(recipients => $param{recipients},
133 address => "$p\@".$config{subscription_domain},
139 if (defined $param{data}{severity} and defined $config{strong_list} and
140 isstrongseverity($param{data}{severity})) {
141 _add_address(recipients => $param{recipients},
142 address => "$config{strong_list}\@".$config{list_domain},
143 reason => $param{data}{severity},
147 my @maints = package_maintainer(binary => $p);
149 print {$param{debug}} "MR|".join(',',@maints)."|$p|$ref|\n";
150 _add_address(recipients => $param{recipients},
153 bug_num => $param{data}{bug_num},
156 print {$param{debug}} "maintainer add >$p|".join(',',@maints)."<\n";
159 print {$param{debug}} "maintainer none >$p<\n";
160 if (not exists $param{unknown_packages}{$p}) {
161 print {$param{transcript}} "Warning: Unknown package '$p'\n";
162 $param{unknown_packages}{$p} = 1;
164 print {$param{debug}} "MR|unknown-package|$p|$ref|\n";
165 _add_address(recipients => $param{recipients},
166 address => $config{unknown_maintainer_email},
168 bug_num => $param{data}{bug_num},
171 if defined $config{unknown_maintainer_email} and
172 length $config{unknown_maintainer_email};
175 if (defined $config{bug_subscription_domain} and
176 length $config{bug_subscription_domain}) {
177 _add_address(recipients => $param{recipients},
178 address => 'bugs='.$param{data}{bug_num}.'@'.
179 $config{bug_subscription_domain},
180 reason => "bug $param{data}{bug_num}",
181 bug_num => $param{data}{bug_num},
186 if (length $param{data}{owner}) {
187 $addmaint = $param{data}{owner};
188 print {$param{debug}} "MO|$addmaint|$param{data}{package}|$ref|\n";
189 _add_address(recipients => $param{recipients},
190 address => $addmaint,
191 reason => "owner of $param{data}{bug_num}",
192 bug_num => $param{data}{bug_num},
195 print {$param{debug}} "owner add >$param{data}{package}|$addmaint<\n";
197 if (exists $param{actions_taken}) {
198 if (exists $param{actions_taken}{done} and
199 $param{actions_taken}{done} and
200 length($config{done_list}) and
201 length($config{list_domain})
203 _add_address(recipients => $param{recipients},
205 address => $config{done_list}.'@'.$config{list_domain},
206 bug_num => $param{data}{bug_num},
207 reason => "bug $param{data}{bug_num} done",
210 if (exists $param{actions_taken}{forwarded} and
211 $param{actions_taken}{forwarded} and
212 length($config{forward_list}) and
213 length($config{list_domain})
215 _add_address(recipients => $param{recipients},
217 address => $config{forward_list}.'@'.$config{list_domain},
218 bug_num => $param{data}{bug_num},
219 reason => "bug $param{data}{bug_num} forwarded",
225 =head2 determine_recipients
227 my @recipients = determine_recipients(recipients => \%recipients,
230 my %recipients => determine_recipients(recipients => \%recipients,);
232 # or a crazy example:
233 send_mail_message(message => $message,
236 values %{{determine_recipients(
237 recipients => \%recipients)
242 Using the recipient hashref, determines the set of recipients.
244 If you specify one of C<bcc>, C<cc>, or C<to>, you will receive only a
245 LIST of recipients which the main should be Bcc'ed, Cc'ed, or To'ed
246 respectively. By default, a LIST with keys bcc, cc, and to is returned
247 with ARRAYREF values corresponding to the users to whom a message
252 =item address_only -- whether to only return mail addresses without reasons or realnamesq
256 Passing more than one of bcc, cc or to is a fatal error.
260 sub determine_recipients {
261 my %param = validate_with(params => \@_,
262 spec => {recipients => {type => HASHREF,
264 bcc => {type => BOOLEAN,
267 cc => {type => BOOLEAN,
270 to => {type => BOOLEAN,
273 address_only => {type => BOOLEAN,
279 if (1 < scalar grep {$param{$_}} qw(to cc bcc)) {
280 croak "Passing more than one of to, cc, or bcc is non-sensical";
283 my %final_recipients;
284 # start with the to recipients
285 for my $addr (keys %{$param{recipients}}) {
288 for my $reason (keys %{$param{recipients}{$addr}}) {
290 for my $bug (keys %{$param{recipients}{$addr}{$reason}}) {
292 my $t_level = $param{recipients}{$addr}{$reason}{$bug};
293 if ($level eq 'to' or
297 elsif ($t_level eq 'cc') {
301 # RFC 2822 comments cannot contain specials and
302 # unquoted () or \; there's no reason for us to allow
303 # insane things here, though, so we restrict this even
304 # more to 20-7E ( -~)
305 $reason =~ s/\\/\\\\/g;
306 $reason =~ s/([\)\(])/\\$1/g;
307 $reason =~ s/[^\x20-\x7E]//g;
308 push @reasons, $reason . ' for {'.join(',',@bugs).'}';
310 if ($param{address_only}) {
311 push @{$final_recipients{$level}}, get_addresses($addr);
314 push @{$final_recipients{$level}}, $addr . ' ('.join(', ',@reasons).')';
317 for (qw(to cc bcc)) {
319 if (exists $final_recipients{$_}) {
320 return @{$final_recipients{$_}||[]};
325 return %final_recipients;
329 =head1 PRIVATE FUNCTIONS
333 _add_address(recipients => $param{recipients},
334 address => $addmaint,
335 reason => $param{data}{package},
336 bug_num => $param{data}{bug_num},
345 my %param = validate_with(params => \@_,
346 spec => {recipients => {type => HASHREF,
348 bug_num => {type => SCALAR,
352 reason => {type => SCALAR,
355 address => {type => SCALAR|ARRAYREF,
357 type => {type => SCALAR,
359 regex => qr/^(?:b?cc|to)$/i,
363 for my $addr (make_list($param{address})) {
364 if (lc($param{type}) eq 'bcc' and
365 exists $param{recipients}{$addr}{$param{reason}}{$param{bug_num}}
369 elsif (lc($param{type}) eq 'cc' and
370 exists $param{recipients}{$addr}{$param{reason}}{$param{bug_num}}
371 and $param{recipients}{$addr}{$param{reason}}{$param{bug_num}} eq 'to'
375 $param{recipients}{$addr}{$param{reason}}{$param{bug_num}} = lc($param{type});