Email/Message.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Email/Mbox.pm ↑ Summary Email/Send.pm »
1 #
2 # $Id$
3 #
4 # email::message Brik
5 #
6 package Metabrik::Email::Message;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 datadir => [ qw(datadir) ],
20 input => [ qw(file) ],
21 output => [ qw(file) ],
22 subject => [ qw(subject|OPTIONAL) ],
23 from => [ qw(from|OPTIONAL) ],
24 to => [ qw(to|OPTIONAL) ],
25 },
26 attributes_default => {
27 from => 'from@example.com',
28 to => 'to@example.com',
29 subject => 'Example.com subject',
30 },
31 commands => {
32 create => [ qw(content) ],
33 parse => [ qw(string) ],
34 save_attachments => [ qw(string) ],
35 },
36 require_modules => {
37 'Metabrik::System::File' => [ ],
38 'Metabrik::File::Base64' => [ ],
39 'Email::Simple' => [ ],
40 'Email::MIME' => [ ],
41 },
42 };
43 }
44  
45 sub create {
46 my $self = shift;
47 my ($content) = @_;
48  
49 $self->brik_help_run_undef_arg('create', $content) or return;
50  
51 my $from = $self->from;
52 my $to = $self->to;
53 my $subject = $self->subject;
54  
55 my $email = Email::Simple->create(
56 header => [
57 From => $from,
58 To => $to,
59 Subject => $subject,
60 ],
61 body => $content,
62 );
63  
64 return $email;
65 }
66  
67 sub parse {
68 my $self = shift;
69 my ($message) = @_;
70  
71 $self->brik_help_run_undef_arg('parse', $message) or return;
72  
73 my $parsed = Email::MIME->new($message);
74 if (! defined($parsed)) {
75 return $self->log->error("parse: MIME failed for message");
76 }
77  
78 my $simple = Email::Simple->new($message);
79  
80 my @headers = $simple->headers;
81 my %header = ();
82 for my $this (@headers) {
83 my @values = $simple->header($this);
84 if (@values == 1) {
85 $header{$this} = $values[0];
86 }
87 else {
88 $header{$this} = \@values;
89 }
90 }
91  
92 my @list = ();
93 push @list, \%header;
94  
95 my @parts = $parsed->parts;
96 for (@parts) {
97 my $this = { %$_ }; # unbless it.
98  
99 my $filename = $_->filename;
100 my $file_content = $_->body_raw;
101 if (defined($filename) && defined($file_content)) {
102 $this->{filename} = $filename;
103 $file_content =~ s{[\r\n]*$}{};
104 $this->{file_content} = $file_content;
105 }
106  
107 if (exists($this->{header}) && exists($this->{header}{headers})) {
108 my $headers = $this->{header}{headers};
109 my $new_headers = {};
110 my $name;
111 my $value;
112 for (@$headers) {
113 if (ref($_) eq '') { # This is a name
114 $name = $_;
115 }
116 elsif (ref($_) eq 'ARRAY') { # This is a value
117 $value = $_->[0]; # 0: has the header value, 1 has the header + value
118 }
119 if (defined($name) && defined($value)) {
120 $new_headers->{$name} = $value;
121 $name = undef;
122 $value = undef;
123 }
124 }
125 $this->{header} = $new_headers;
126 }
127 push @list, $this;
128 }
129  
130 return \@list;
131 }
132  
133 sub save_attachments {
134 my $self = shift;
135 my ($string) = @_;
136  
137 $self->brik_help_run_undef_arg('parse', $string) or return;
138  
139 my $datadir = $self->datadir;
140 my $message = $self->parse($string) or return;
141 my $headers = $message->[0];
142  
143 my $sf = Metabrik::System::File->new_from_brik_init($self) or return;
144 my $fb = Metabrik::File::Base64->new_from_brik_init($self) or return;
145  
146 my @files = ();
147 for my $part (@$message) {
148 if (exists($part->{filename}) && length($part->{filename})) {
149 my $from = $headers->{From};
150 my $to = $headers->{To};
151 my $subject = $headers->{Subject};
152 my $filename = $sf->basefile($part->{filename});
153 $filename =~ s{\s+}{_}g; # I hate spaces in filenames.
154 my $output = $fb->decode_from_string(
155 $part->{file_content}, $datadir."/$filename"
156 );
157 push @files, {
158 headers => $headers,
159 file => $output,
160 };
161 }
162 }
163  
164 return \@files;
165 }
166  
167 1;
168  
169 __END__
« Email/Mbox.pm ↑ Summary Email/Send.pm »