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 base qw(Exporter);
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 $anymaintfound=0; my $anymaintnotfound=0;
116 my $ref = $param{data}{bug_num};
117 for my $p (splitpackages($param{data}{package})) {
119 if (defined $config{subscription_domain}) {
120 my @source_packages = binary_to_source(binary => $p,
123 if (@source_packages) {
124 for my $source (@source_packages) {
125 _add_address(recipients => $param{recipients},
126 address => "$source\@".$config{subscription_domain},
133 _add_address(recipients => $param{recipients},
134 address => "$p\@".$config{subscription_domain},
140 if (defined $param{data}{severity} and defined $config{strong_list} and
141 isstrongseverity($param{data}{severity})) {
142 _add_address(recipients => $param{recipients},
143 address => "$config{strong_list}\@".$config{list_domain},
144 reason => $param{data}{severity},
148 my @maints = package_maintainer(binary => $p);
150 print {$param{debug}} "MR|".join(',',@maints)."|$p|$ref|\n";
151 _add_address(recipients => $param{recipients},
154 bug_num => $param{data}{bug_num},
157 print {$param{debug}} "maintainer add >$p|".join(',',@maints)."<\n";
160 print {$param{debug}} "maintainer none >$p<\n";
161 if (not exists $param{unknown_packages}{$p}) {
162 print {$param{transcript}} "Warning: Unknown package '$p'\n";
163 $param{unknown_packages}{$p} = 1;
165 print {$param{debug}} "MR|unknown-package|$p|$ref|\n";
166 _add_address(recipients => $param{recipients},
167 address => $config{unknown_maintainer_email},
169 bug_num => $param{data}{bug_num},
172 if defined $config{unknown_maintainer_email} and
173 length $config{unknown_maintainer_email};
176 if (defined $config{bug_subscription_domain} and
177 length $config{bug_subscription_domain}) {
178 _add_address(recipients => $param{recipients},
179 address => 'bugs='.$param{data}{bug_num}.'@'.
180 $config{bug_subscription_domain},
181 reason => "bug $param{data}{bug_num}",
182 bug_num => $param{data}{bug_num},
187 if (length $param{data}{owner}) {
188 $addmaint = $param{data}{owner};
189 print {$param{debug}} "MO|$addmaint|$param{data}{package}|$ref|\n";
190 _add_address(recipients => $param{recipients},
191 address => $addmaint,
192 reason => "owner of $param{data}{bug_num}",
193 bug_num => $param{data}{bug_num},
196 print {$param{debug}} "owner add >$param{data}{package}|$addmaint<\n";
198 if (exists $param{actions_taken}) {
199 if (exists $param{actions_taken}{done} and
200 $param{actions_taken}{done} and
201 length($config{done_list}) and
202 length($config{list_domain})
204 _add_address(recipients => $param{recipients},
206 address => $config{done_list}.'@'.$config{list_domain},
207 bug_num => $param{data}{bug_num},
208 reason => "bug $param{data}{bug_num} done",
211 if (exists $param{actions_taken}{forwarded} and
212 $param{actions_taken}{forwarded} and
213 length($config{forward_list}) and
214 length($config{list_domain})
216 _add_address(recipients => $param{recipients},
218 address => $config{forward_list}.'@'.$config{list_domain},
219 bug_num => $param{data}{bug_num},
220 reason => "bug $param{data}{bug_num} forwarded",
226 =head2 determine_recipients
228 my @recipients = determine_recipients(recipients => \%recipients,
231 my %recipients => determine_recipients(recipients => \%recipients,);
233 # or a crazy example:
234 send_mail_message(message => $message,
237 values %{{determine_recipients(
238 recipients => \%recipients)
243 Using the recipient hashref, determines the set of recipients.
245 If you specify one of C<bcc>, C<cc>, or C<to>, you will receive only a
246 LIST of recipients which the main should be Bcc'ed, Cc'ed, or To'ed
247 respectively. By default, a LIST with keys bcc, cc, and to is returned
248 with ARRAYREF values corresponding to the users to whom a message
253 =item address_only -- whether to only return mail addresses without reasons or realnamesq
257 Passing more than one of bcc, cc or to is a fatal error.
261 sub determine_recipients {
262 my %param = validate_with(params => \@_,
263 spec => {recipients => {type => HASHREF,
265 bcc => {type => BOOLEAN,
268 cc => {type => BOOLEAN,
271 to => {type => BOOLEAN,
274 address_only => {type => BOOLEAN,
280 if (1 < scalar grep {$param{$_}} qw(to cc bcc)) {
281 croak "Passing more than one of to, cc, or bcc is non-sensical";
284 my %final_recipients;
285 # start with the to recipients
286 for my $addr (keys %{$param{recipients}}) {
289 for my $reason (keys %{$param{recipients}{$addr}}) {
291 for my $bug (keys %{$param{recipients}{$addr}{$reason}}) {
293 my $t_level = $param{recipients}{$addr}{$reason}{$bug};
294 if ($level eq 'to' or
298 elsif ($t_level eq 'cc') {
302 # RFC 2822 comments cannot contain specials and
303 # unquoted () or \; there's no reason for us to allow
304 # insane things here, though, so we restrict this even
305 # more to 20-7E ( -~)
306 $reason =~ s/\\/\\\\/g;
307 $reason =~ s/([\)\(])/\\$1/g;
308 $reason =~ s/[^\x20-\x7E]//g;
309 push @reasons, $reason . ' for {'.join(',',@bugs).'}';
311 if ($param{address_only}) {
312 push @{$final_recipients{$level}}, get_addresses($addr);
315 push @{$final_recipients{$level}}, $addr . ' ('.join(', ',@reasons).')';
318 for (qw(to cc bcc)) {
320 if (exists $final_recipients{$_}) {
321 return @{$final_recipients{$_}||[]};
326 return %final_recipients;
330 =head1 PRIVATE FUNCTIONS
334 _add_address(recipients => $param{recipients},
335 address => $addmaint,
336 reason => $param{data}{package},
337 bug_num => $param{data}{bug_num},
346 my %param = validate_with(params => \@_,
347 spec => {recipients => {type => HASHREF,
349 bug_num => {type => SCALAR,
353 reason => {type => SCALAR,
356 address => {type => SCALAR|ARRAYREF,
358 type => {type => SCALAR,
360 regex => qr/^(?:b?cc|to)$/i,
364 for my $addr (make_list($param{address})) {
365 if (lc($param{type}) eq 'bcc' and
366 exists $param{recipients}{$addr}{$param{reason}}{$param{bug_num}}
370 elsif (lc($param{type}) eq 'cc' and
371 exists $param{recipients}{$addr}{$param{reason}}{$param{bug_num}}
372 and $param{recipients}{$addr}{$param{reason}}{$param{bug_num}} eq 'to'
376 $param{recipients}{$addr}{$param{reason}}{$param{bug_num}} = lc($param{type});