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