]> git.donarmstrong.com Git - debbugs.git/blob - Debbugs/Recipients.pm
Drop more unused variables
[debbugs.git] / Debbugs / Recipients.pm
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 $
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                                          },
101                               );
102
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)
110                              );
111           }
112           return;
113      }
114      my ($addmaint);
115      my $ref = $param{data}{bug_num};
116      for my $p (splitpackages($param{data}{package})) {
117           $p = lc($p);
118           if (defined $config{subscription_domain}) {
119                my @source_packages = binary_to_source(binary => $p,
120                                                       source_only => 1,
121                                                      );
122                if (@source_packages) {
123                     for my $source (@source_packages) {
124                          _add_address(recipients => $param{recipients},
125                                       address => "$source\@".$config{subscription_domain},
126                                       reason => $source,
127                                       type  => 'bcc',
128                                      );
129                     }
130                }
131                else {
132                     _add_address(recipients => $param{recipients},
133                                  address => "$p\@".$config{subscription_domain},
134                                  reason => $p,
135                                  type  => 'bcc',
136                                 );
137                }
138           }
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},
144                             type  => 'bcc',
145                            );
146           }
147           my @maints = package_maintainer(binary => $p);
148           if (@maints) {
149               print {$param{debug}} "MR|".join(',',@maints)."|$p|$ref|\n";
150               _add_address(recipients => $param{recipients},
151                            address => \@maints,
152                            reason => $p,
153                            bug_num => $param{data}{bug_num},
154                            type  => 'cc',
155                           );
156               print {$param{debug}} "maintainer add >$p|".join(',',@maints)."<\n";
157           }
158           else {
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;
163                }
164                print {$param{debug}} "MR|unknown-package|$p|$ref|\n";
165                _add_address(recipients => $param{recipients},
166                             address => $config{unknown_maintainer_email},
167                             reason => $p,
168                             bug_num => $param{data}{bug_num},
169                             type  => 'cc',
170                            )
171                     if defined $config{unknown_maintainer_email} and
172                          length $config{unknown_maintainer_email};
173           }
174       }
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},
182                        type       => 'bcc',
183                       );
184      }
185
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},
193                        type  => 'cc',
194                       );
195         print {$param{debug}} "owner add >$param{data}{package}|$addmaint<\n";
196      }
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})
202              ) {
203                _add_address(recipients => $param{recipients},
204                             type       => 'cc',
205                             address    => $config{done_list}.'@'.$config{list_domain},
206                             bug_num    => $param{data}{bug_num},
207                             reason     => "bug $param{data}{bug_num} done",
208                            );
209           }
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})
214              ) {
215                _add_address(recipients => $param{recipients},
216                             type       => 'cc',
217                             address    => $config{forward_list}.'@'.$config{list_domain},
218                             bug_num    => $param{data}{bug_num},
219                             reason     => "bug $param{data}{bug_num} forwarded",
220                            );
221           }
222      }
223 }
224
225 =head2 determine_recipients
226
227      my @recipients = determine_recipients(recipients => \%recipients,
228                                            bcc => 1,
229                                           );
230      my %recipients => determine_recipients(recipients => \%recipients,);
231
232      # or a crazy example:
233      send_mail_message(message => $message,
234                        recipients =>
235                         [make_list(
236                           values %{{determine_recipients(
237                                 recipients => \%recipients)
238                                   }})
239                         ],
240                       );
241
242 Using the recipient hashref, determines the set of recipients.
243
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
248 should be sent.
249
250 =over
251
252 =item address_only -- whether to only return mail addresses without reasons or realnamesq
253
254 =back
255
256 Passing more than one of bcc, cc or to is a fatal error.
257
258 =cut
259
260 sub determine_recipients {
261      my %param = validate_with(params => \@_,
262                                spec   => {recipients => {type => HASHREF,
263                                                         },
264                                           bcc        => {type => BOOLEAN,
265                                                          default => 0,
266                                                         },
267                                           cc         => {type => BOOLEAN,
268                                                          default => 0,
269                                                         },
270                                           to         => {type => BOOLEAN,
271                                                          default => 0,
272                                                         },
273                                           address_only => {type => BOOLEAN,
274                                                            default => 0,
275                                                           }
276                                          },
277                               );
278
279      if (1 < scalar grep {$param{$_}} qw(to cc bcc)) {
280           croak "Passing more than one of to, cc, or bcc is non-sensical";
281      }
282
283      my %final_recipients;
284      # start with the to recipients
285      for my $addr (keys %{$param{recipients}}) {
286           my $level = 'bcc';
287           my @reasons;
288           for my $reason (keys %{$param{recipients}{$addr}}) {
289                my @bugs;
290                for my $bug (keys %{$param{recipients}{$addr}{$reason}}) {
291                     push @bugs, $bug;
292                     my $t_level = $param{recipients}{$addr}{$reason}{$bug};
293                     if ($level eq 'to' or
294                         $t_level eq 'to') {
295                          $level = 'to';
296                     }
297                     elsif ($t_level eq 'cc') {
298                          $level = 'cc';
299                     }
300                }
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).'}';
309           }
310           if ($param{address_only}) {
311                push @{$final_recipients{$level}}, get_addresses($addr);
312           }
313           else {
314                push @{$final_recipients{$level}}, $addr . ' ('.join(', ',@reasons).')';
315           }
316      }
317      for (qw(to cc bcc)) {
318           if ($param{$_}) {
319                if (exists $final_recipients{$_}) {
320                     return @{$final_recipients{$_}||[]};
321                }
322                return ();
323           }
324      }
325      return %final_recipients;
326 }
327
328
329 =head1 PRIVATE FUNCTIONS
330
331 =head2 _add_address
332
333           _add_address(recipients => $param{recipients},
334                        address => $addmaint,
335                        reason => $param{data}{package},
336                        bug_num => $param{data}{bug_num},
337                        type  => 'cc',
338                       );
339
340
341 =cut
342
343
344 sub _add_address {
345      my %param = validate_with(params => \@_,
346                                spec => {recipients => {type => HASHREF,
347                                                       },
348                                         bug_num    => {type => SCALAR,
349                                                        regex => qr/^\d*$/,
350                                                        default => '',
351                                                       },
352                                         reason     => {type => SCALAR,
353                                                        default => '',
354                                                       },
355                                         address    => {type => SCALAR|ARRAYREF,
356                                                       },
357                                         type       => {type => SCALAR,
358                                                        default => 'cc',
359                                                        regex   => qr/^(?:b?cc|to)$/i,
360                                                       },
361                                        },
362                               );
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}}
366              ) {
367                next;
368           }
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'
372                 ) {
373                next;
374           }
375           $param{recipients}{$addr}{$param{reason}}{$param{bug_num}} = lc($param{type});
376      }
377 }
378
379 1;
380
381
382 __END__
383
384
385
386
387
388