File Coverage

lib/Sisimai/Lhost/GoogleGroups.pm
Criterion Covered Total %
statement 45 47 95.7
branch 12 18 66.6
condition 4 6 66.6
subroutine 6 6 100.0
pod 2 2 100.0
total 69 79 87.3


line stmt bran cond sub pod time code
1             package Sisimai::Lhost::GoogleGroups;
2 22     22   5427 use parent 'Sisimai::Lhost';
  22         36  
  22         131  
3 22     22   1187 use feature ':5.10';
  22         40  
  22         1303  
4 22     22   136 use strict;
  22         38  
  22         458  
5 22     22   98 use warnings;
  22         46  
  22         11786  
6              
7 2     2 1 1075 sub description { 'Google Groups: https://groups.google.com' }
8             sub make {
9             # Detect an error from Google Groups
10             # @param [Hash] mhead Message headers of a bounce email
11             # @param [String] mbody Message body of a bounce email
12             # @return [Hash] Bounce data list and message/rfc822 part
13             # @return [Undef] failed to parse or the arguments are missing
14             # @since v4.25.6
15 567     567 1 1262 my $class = shift;
16 567   100     1283 my $mhead = shift // return undef;
17 566   50     1134 my $mbody = shift // return undef;
18              
19 566 100       1692 return undef unless rindex($mhead->{'from'}, '') > -1;
20 148 50       479 return undef unless index($mhead->{'subject'}, 'Delivery Status Notification') > -1;
21 148 100       390 return undef unless exists $mhead->{'x-failed-recipients'};
22 128 100       328 return undef unless index($mhead->{'x-failed-recipients'}, '@googlegroups.com') > -1;
23              
24             # Hello kijitora@libsisimai.org,
25             #
26             # We're writing to let you know that the group you tried to contact (group-name)
27             # may not exist, or you may not have permission to post messages to the group.
28             # A few more details on why you weren't able to post:
29             #
30             # * You might have spelled or formatted the group name incorrectly.
31             # * The owner of the group may have removed this group.
32             # * You may need to join the group before receiving permission to post.
33             # * This group may not be open to posting.
34             #
35             # If you have questions related to this or any other Google Group,
36             # visit the Help Center at https://groups.google.com/support/.
37             #
38             # Thanks,
39             #
40             # Google Groups
41 72         134 state $indicators = __PACKAGE__->INDICATORS;
42 72         116 state $rebackbone = qr/^-----[ ]Original[ ]message[ ]-----$/m;
43              
44 72         197 my $dscontents = [__PACKAGE__->DELIVERYSTATUS];
45 72         223 my $emailsteak = Sisimai::RFC5322->fillet($mbody, $rebackbone);
46 72         282 my $recordwide = { 'rhost' => '', 'reason' => '', 'diagnosis' => '' };
47 72         138 my $recipients = 0;
48 72         196 my $v = $dscontents->[-1];
49              
50             # * You might have spelled or formatted the group name incorrectly.
51             # * The owner of the group may have removed this group.
52             # * You may need to join the group before receiving permission to post.
53             # * This group may not be open to posting.
54 72   50     677 my $fewdetails = [$emailsteak->[0] =~ /^[ ]?[*][ ]?/gm] || [];
55 72 50       263 $recordwide->{'reason'} = scalar @$fewdetails == 4 ? 'rejected' : 'onhold';
56              
57 72         319 my @entiremesg = split(/\n\n/, $emailsteak->[0], 5); pop @entiremesg;
  72         154  
58 72         265 my $diagnostic = join(' ', @entiremesg); $diagnostic =~ y/\n/ /;
  72         212  
59 72         430 $recordwide->{'diagnosis'} = Sisimai::String->sweep($diagnostic);
60              
61 72         300 my $serverlist = Sisimai::RFC5322->received($mhead->{'received'}->[0]);
62 72         274 $recordwide->{'rhost'} = shift @$serverlist;
63              
64 72         288 for my $e ( split(',', $mhead->{'x-failed-recipients'}) ) {
65             # X-Failed-Recipients: neko@example.jp, nyaan@example.org, ...
66 72 50       236 next unless index($e, '@googlegroups.com') > -1;
67 72 50       200 next unless Sisimai::RFC5322->is_emailaddress($e);
68              
69 72 50       208 if( $v->{'recipient'} ) {
70             # There are multiple recipient addresses in the message body.
71 0         0 push @$dscontents, __PACKAGE__->DELIVERYSTATUS;
72 0         0 $v = $dscontents->[-1];
73             }
74 72         368 $v->{'recipient'} = Sisimai::Address->s3s4($e);
75 72         150 $recipients++;
76 72         487 $v->{ $_ } = $recordwide->{ $_ } for keys %$recordwide;
77             }
78 72 50       183 return undef unless $recipients;
79 72         424 return { 'ds' => $dscontents, 'rfc822' => $emailsteak->[1] };
80             }
81              
82             1;
83             __END__