File Coverage

blib/lib/Email/MIME/MobileJP/Parser.pm
Criterion Covered Total %
statement 13 15 86.6
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 18 20 90.0


line stmt bran cond sub pod time code
1             package Email::MIME::MobileJP::Parser;
2 1     1   21550 use strict;
  1         3  
  1         38  
3 1     1   4 use warnings;
  1         1  
  1         24  
4 1     1   3 use utf8;
  1         3  
  1         6  
5              
6 1     1   577 use Email::MIME;
  1         83485  
  1         31  
7 1     1   2873 use Email::Address::JP::Mobile; # provides Email::Address->carrier() method for carrier detection.
  0            
  0            
8             use Email::Address::Loose -override;
9             use Carp ();
10              
11             sub new {
12             my $class = shift;
13             my $mail = Email::MIME->new(@_);
14             bless { mail => $mail }, $class;
15             }
16              
17             sub mail { shift->{mail} }
18              
19             sub subject {
20             my $self = shift;
21             my $carrier = $self->carrier;
22             return $carrier && $carrier->is_mobile ? $carrier->mime_encoding->decode($self->mail->header_obj->header_raw('Subject')) : $self->mail->header('Subject');
23             }
24              
25             sub from {
26             my $self = shift;
27              
28             $self->{__jpmobile_from} ||= do {
29             my ($from) = $self->mail->header('From');
30             return unless $from;
31             my ($addr) = Email::Address::Loose->parse($from);
32             return $addr;
33             };
34             }
35              
36             sub to {
37             my $self = shift;
38              
39             my @addr;
40             for my $to ($self->mail->header('To')) {
41             push @addr, Email::Address::Loose->parse($to);
42             }
43             return @addr;
44             }
45              
46             sub carrier {
47             my ($self, ) = @_;
48             my $from = $self->from;
49             Carp::croak("Missing 'From' field in headers") unless $from;
50             return $from->carrier();
51             }
52              
53             sub get_texts {
54             my ($self, $content_type) = @_;
55             $content_type ||= qr{^text/plain};
56              
57             if ($self->carrier->is_mobile) {
58             my $encoding = $self->carrier->parse_encoding;
59              
60             return map {
61             my $enc="utf-8";
62             if( $_->content_type =~/utf-8/i ){
63             $enc="utf-8";
64             }elsif( $_->content_type =~/iso-2022-jp/i ){
65             $enc="iso-2022-jp";
66             }elsif( $_->content_type =~/shift[-_]jis/i ){
67             $enc="shift_jis";
68             }
69             Encode::decode($enc, $_->body);
70             } $self->get_parts($content_type);
71             } else {
72             return map { $_->body_str } $self->get_parts($content_type);
73             }
74             }
75              
76             sub get_parts {
77             my ($self, $content_type) = @_;
78             Carp::croak("missing content-type") unless defined $content_type;
79              
80             my @parts;
81             $self->mail->walk_parts(sub {
82             my $part = shift;
83             return if $part->parts > 1; # multipart
84              
85             if ($part->content_type =~ $content_type) {
86             push @parts, $part;
87             }
88             });
89             return @parts;
90             }
91              
92             1;
93             __END__