blib/lib/Mail/IMAP2SMS.pm

filter: Statementstmt 25.8% 23 / 89 Branchbran 4.5% 1 / 22 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 7 / 14 Podpod 100% 6 / 6 totaltotal 27.0% 37 / 137 CC 15 SCAR 26.4
CC
15
Cov
21%
_connect
30.0
chunk
20.0
send
20.0
CRAP 14.0
↑ Summary
1 package Mail::IMAP2SMS;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Net::IMAP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Net::IMAP::Simple::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Email::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Mail::Sendmail;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use vars qw[$VERSION];
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶$VERSION = $1 if('$Id: IMAP2SMS.pm,v 1.2 2009/05/21 14:49:36 rcrowder Exp $' =~ /,v ([\d_.]+) /);
Branch: 1 branch
BranchTrueFalseTotal
if "\$Id: IMAP2SMS.pm,v 1.2 2009/05/21 14:49:36 rcrowder Exp \$" =~ /,v ([\d_.]+) / 2 0 2
12  
13 =head1 NAME
14  
15 Mail::IMAP2SMS - Perl extension for IMAP to SMS.
16  
17 =head1 SYNOPSIS
18  
19 # Import Module
20 use Mail::IMAP2SMS;
21  
22 # Instantiate the IMAP2SMS object.
23 my $sms = Mail::IMAP2SMS->new('imap.example.com', 'test@example.com', 'p4$$w0rd', 1);
24  
25 # Get unseen mail from inbox.
26 my $unseen = $sms->get_unseen('INBOX');
27  
28 # Chunk each message into specified size (160) SMSs.
29 foreach (@$unseen) {
30 my ($id) = $_;
31 $sms->chunk($id, 160);
32 } # foreach
33  
34 # Send chunked messages.
35 if ($sms->send('7895551234@vtext.com')) {
36 print 'All sent successfully';
37 } else {
38 print 'Send failed...';
39 } # if/else
40  
41 # Close connection
42 $sms->disconnect;
43  
44 =head1 DESCRIPTION
45  
46 This module is a quick and easy way to SMS your IMAP email.
47  
48 =head1 OBJECT CREATION METHOD
49  
50 =over 4
51  
52 =item new
53  
54 my $sms = Mail::IMAP2SMS->new( $server [ :port ], $username, $password, $ssl );
55  
56 This class method constructs a C<Mail::IMAP2SMS> object. It takes four required parameters. The server parameter may specify just the
57  
58 server, or both the server and the port. To specify an alternate port, seperate it from the server with a colon (C<:>), C<example.com:9876>.
59  
60 The ssl (BOOLEAN) parameter may specify whether to use or not use SSL to connect to the specified IMAP server.
61  
62 =back
63  
64 =cut
65  
66 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
67 0 my $class = shift;
68  
69 0 my $self = {
70 server => shift,
71 user => shift,
72 pass => shift,
73 ssl => shift
74 };
75  
76 0 $self->{sms} = [];
77  
78 0 bless $self, $class;
79  
80 0 $self->_connect;
81  
82 0 return $self;
83 } # new
84  
85 0 ▶sub _connect {
Subroutine _connect: not called CC=5 SCAR=34.0
86 0 my ($self) = @_;
87  
88 0 ▶ if ($self->{ssl} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'ssl'} == 1) { } 0 0 0
89 0 ▶ $self->{imap} = Net::IMAP::Simple::SSL->new($self->{server}) ||
Condition0%0/2
'Net::IMAP::Simple::SSL'->new($self->{'server'}) || croak("Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n")
l!l
00
MC/DC0%0/1
'Net::IMAP::Simple::SSL'->new($self->{'server'}) || croak("Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n")
'Net::IMAP::Simple::SSL'->new($self->{'server'})
Truth table0%0/2
A
A= 'Net::IMAP::Simple::SSL'->new($self->{'server'})
Aresult
11
00
90 croak "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
91 } else {
92 0 ▶ $self->{imap} = Net::IMAP::Simple->new($self->{server}) ||
Condition0%0/2
'Net::IMAP::Simple'->new($self->{'server'}) || croak("Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n")
l!l
00
MC/DC0%0/1
'Net::IMAP::Simple'->new($self->{'server'}) || croak("Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n")
'Net::IMAP::Simple'->new($self->{'server'})
Truth table0%0/2
A
A= 'Net::IMAP::Simple'->new($self->{'server'})
Aresult
11
00
93 croak "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
94 }
95  
96 # Log on
97 0 ▶ if(!$self->{imap}->login($self->{user}, $self->{pass})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"imap"}->login($self->{"user"}, $self->{"pass"})) 0 0 0
98 0 croak "Login failed: " . $self->{imap}->errstr . "\n";
99 }
100  
101 0 return 1;
102 } # connect
103  
104 =pod
105  
106 =head1 METHODS
107  
108 =over 4
109  
110 =item get_seen
111  
112 my $seen = $sms->get_seen('INBOX');
113  
114 This method takes one required parameter, an IMAP folder. The number of seen
115 messages is returned on success. On failure, nothing is returned.
116  
117 =cut
118  
119 0 ▶sub get_seen {
Subroutine get_seen: not called CC=3 SCAR=24.8
Pod documented
120 0 my ($self, $folder) = @_;
121  
122 0 my $nm = $self->{imap}->select($folder);
123  
124 0 my @seen;
125  
126 0 ▶ for (my $i = 1; $i <= $nm; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nm) 0 0 0
127 0 ▶ if ($self->{imap}->seen($i)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"imap"}->seen($i)) 0 0 0
128 0 push @seen, $i;
129 }
130 }
131  
132 0 return \@seen;
133 } # get_seen
134  
135 =pod
136  
137 =item get_unseen
138  
139 my $unseen = $sms->get_unseen('INBOX');
140  
141 This method takes one required parameter, an IMAP folder. The number of seen
142 messages is returned on success. On failure, nothing is returned.
143  
144 =cut
145  
146 0 ▶sub get_unseen {
Subroutine get_unseen: not called CC=3 SCAR=24.8
Pod documented
147 0 my ($self, $folder) = @_;
148  
149 0 my $nm = $self->{imap}->select($folder);
150  
151 0 my @unseen;
152  
153 0 ▶ for (my $i = 1; $i <= $nm; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $nm) 0 0 0
154 0 ▶ if (!$self->{imap}->seen($i)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"imap"}->seen($i)) 0 0 0
155 0 push @unseen, $i;
156 }
157 }
158  
159 0 return \@unseen;
160 } # get_unseen
161  
162 =pod
163  
164 =item chunk
165  
166 print 'Successfully chunked!' if $sms->chunk($id, 160);
167  
168 This method takes two require parameters, a message ID and a chunking size.
169 The message ID is used to get the message from the IMAP server. Upon getting
170 the message it is determined whether the subject and body can fit in one SMS
171 or if the message must be broken into segments of the proper SMS size. Carriers
172 differ on SMS size thus a size must be specified. On success, boolean true is
173 returned. On failure, nothing is returned.
174  
175 =cut
176  
177 0 ▶sub chunk {
Subroutine chunk: not called CC=4 SCAR=30.0
Pod documented
178 0 my ($self, $id, $size) = @_;
179  
180 0 my $es = Email::Simple->new(join '', @{ $self->{imap}->get($id) } );
181  
182 0 my $sub = $es->header('Subject');
183  
184 0 my $subl = length($sub);
185  
186 0 my $bodyl = length($es->body);
187  
188 0 ▶ if (($bodyl + $subl) < $size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bodyl + $subl < $size) { } 0 0 0
189 0 push @{ $self->{sms} }, $es;
190 } else {
191 0 my $msg_size = $subl;
192  
193 0 my @body = split(/ /, $es->body);
194  
195 0 my $str = "";
196  
197 0 foreach (@body) {
198 0 my $wordl = length($_);
199  
200 0 ▶ if (($wordl + $msg_size + 1) > $size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wordl + $msg_size + 1 > $size) { } 0 0 0
201 0 my $msg = Email::Simple->new(join '', @{ $self->{imap}->get($id) } );
202 0 $msg->body_set("$str");
203  
204 0 push @{ $self->{sms} }, $msg;
205  
206 0 $msg_size = $subl;
207 0 $str = "$_ ";
208 } else {
209 0 $msg_size += ($wordl + 1);
210 0 $str .= "$_ ";
211 } # if/else
212 } # foreach
213 } # if/else
214  
215 0 return 1;
216 } # chunk
217  
218 =pod
219  
220 =item send
221  
222 print 'Send successful...' if $sms->send('7895551234@vtext.com');
223  
224 This method requires one parameter, a wireless carrier phone number email address.
225 This method will send all SMS created to the specified email address. If more than
226 three SMS are readied to send, a sending sleep time between each SMS will be applied
227 to reduce the chance of the wireless carrier dropping any messages. On success, a
228 boolean true is returned. On failure, nothing is returned.
229  
230 =cut
231  
232 0 ▶sub send {
Subroutine send: not called CC=4 SCAR=30.0
Pod documented
233 0 my ($self, $email) = @_;
234  
235 0 my $count = 1;
236  
237 0 foreach (@{ $self->{sms} }) {
238 0 my %mail = ( To => $email, From => $_->header('From'),
239 'Content-Type' => 'text/plain; charset=us-ascii',
240 'Content-Transfer-Encoding' => '7bit',
241 Subject => "$count " . $_->header('Subject'), Message => $_->body);
242  
243 0 ▶ if (scalar @{ $self->{sms} } > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"sms"};} > 3) 0 0 0
244 0 sleep(3);
245 }
246  
247 0 ▶ sendmail(%mail) || croak "Unable to send email: $!";
Branch: 1 branch
BranchTrueFalseTotal
sendmail(%mail) or croak("Unable to send email: $!") 0 0 0
248  
249 0 $count++;
250 } # foreach
251  
252 0 return 1;
253 } # send
254  
255 =pod
256  
257 =item disconnect
258  
259 print 'Disconnected from IMAP...' if $sms->quit;
260  
261 This method requires no parameters. It simply closes the connection
262 to the IMAP server. On success, a boolean true is returned. On failure,
263 nothing is returned.
264  
265 =cut
266  
267 0 ▶sub disconnect {
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
268 0 my ($self) = @_;
269  
270 0 $self->{imap}->quit;
271  
272 0 return 1;
273 } # quit
274  
275 =pod
276  
277 =back
278  
279 =cut
280  
281 2 1;
282  
283 __END__
↑ Summary