Bulkmail/DummyServer.pm

filter: Statementstmt 98.2% 56 / 57 Branchbran 55.0% 11 / 20 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 90.9% 10 / 11 Podpod 100% 2 / 2 totaltotal 82.6% 81 / 98 CC 13 SCAR 11.7
CC
13
Cov
84%
disconnect
5.2
connect
4.1
PRINT
4.1
CRAP 3.2
« Bulkmail/Object.pm ↑ Summary
1 package Mail::Bulkmail::DummyServer;
2  
3 # Copyright and (c) 2003 James A Thomason III (jim@jimandkoka.com). All rights reserved.
4 # Mail::Bulkmail::DummyServer is distributed under the terms of the Perl Artistic License.
5  
6 =pod
7  
8 =head1 NAME
9  
10 Mail::Bulkmail::DummyServer - dummy class for dummy server objects
11  
12 =head1 AUTHOR
13  
14 Jim Thomason, jim@jimandkoka.com
15  
16 =head1 DESCRIPTION
17  
18 Mail::Bulkmail::DummyServer is a drop in replacement for Mail::Bulkmail::Server.
19  
20 Sometimes you just want to test things on your end - make sure your list iterates properly, make
21 sure your mail merge is functioning fine, make sure your logging functions are correct, whatever.
22 And in those cases, you probably don't want to worry about futzing around with your SMTP relay and
23 sending junk messages through it that you don't care about. Not to mention the fact that those
24 probably will need to be inspected and deleted later. A hassle for debugging.
25  
26 Enter DummyServer. This is a subclass of Mail::Bulkmail::Server that behaves exactly the same
27 except for the fact that it doesn't actually connect to a server. Instead, it sends all data
28 that would be going to the server to a file instead. This file should be specified in the conf file.
29  
30 #in your conf file
31 define package Mail::Bulkmail::DummyServer
32 dummy_file = ./my.dummy.file
33  
34 Now, instead of sending commands to your SMTP relay, they'll get sent to ./my.dummy.file for easy
35 inspection at a later date.
36  
37 =cut
38  
39 1 ▶use Mail::Bulkmail::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 1 @ISA = qw(Mail::Bulkmail::Server);
41  
42 1 $VERSION = '3.12';
43  
44 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 =pod
48  
49 =head1 CLASS ATTRIBUTES
50  
51 =over 11
52  
53 =item dummy_file
54  
55 Stores the dummy_file that you want to output your data to.
56  
57 =back
58  
59 =cut
60  
61 1 __PACKAGE__->add_attr('dummy_file');
62  
63 # this is used for tied filehandles to internally hold the dummy socket
64 1 __PACKAGE__->add_attr('_socket');
65  
66 =pod
67  
68 =head1 METHODS
69  
70 =over 11
71  
72 =item connect
73  
74 "connects" to your DummyServer. Actually, internally it ties a filehandle onto this package.
75 Yes, this thing has a (minimal) implementation of a tied handle class to accomplish this feat.
76  
77 This method is known to return
78  
79 MBDu001 - server won't say EHLO
80  
81 =cut
82  
83 1 ▶sub connect {
Subroutine connect: called CC=4 SCAR=14.2
Pod documented
84 1 my $self = shift;
85  
86 1 local $\ = "\015\012";
87 1 local $/ = "\015\012";
88  
89 1 my $h = $self->gen_handle();
90 1 tie *$h, "Mail::Bulkmail::DummyServer", $self;
91  
92 1 $self->socket($h);
93  
94 #We're either given a domain, or we'll build it based on who the message is from
95 1 my $domain = $self->Domain;
96  
97 1 print $h "EHLO $domain";
98  
99 1 ▶ my $response = <$h> || "";
Condition50%1/2
readline $h || ''
l!l
10
MC/DC0%0/1
readline $h || ''
readline $h
Truth table50%1/2
A
A= readline $h
Aresult
11
00
100 1 ▶ return $self->error("Server won't say EHLO: $response", "MBDu001") if ! $response || $response =~ /^[45]/;
Branch: 1 branch
BranchTrueFalseTotal
if not $response or $response =~ /^[45]/ 0 1 1
Condition33%1/3
not $response or $response =~ /^[45]/
l!l&&r!l&&!r
001
MC/DC0%0/2
not $response or $response =~ /^[45]/
not $response $response =~ /^[45]/
Truth table33%1/3
not(A) or B
A= not $responseB= $response =~ /^[45]/
ABresult
1X1
011
000
101  
102 1 $self->connected(1);
103 1 return $self;
104 };
105  
106 # TIEHANDLE, as usual, ties a filehandle onto this class. It reads the file that is defined
107 # _in_the_conf_file at Mail::Bulkmail::DummyServer->dummy_file, tries to open it (dies with an
108 # error if it can't), and then ties our filehandle to the just opened file.
109 1 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: called CC=2 SCAR=7.0
110  
111 1 my $class = shift;
112 1 my $self = shift;
113  
114 1 my $file = $self->dummy_file();
115  
116 1 my $handle = Mail::Bulkmail::Object->gen_handle();
117  
118 1 ▶ open ($handle, ">>$file") || die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $handle, ">>$file" or die $! 1 0 1
119  
120 1 return $class->new('_socket' => $handle);
121 };
122  
123 # in case our filehandle is fetched, just display some minimal information, namely the fact
124 # that we're in DummyServer, and the name of the dummy file
125 0 ▶sub FETCH {
Subroutine FETCH: not called CC=1 SCAR=6.9
126 0 return "DummyServer at file : " . shift->_socket;
127 };
128  
129 # prints to our dummy file. Uses sendmail crlfs, and tacks on a note that we're starting
130 # a new message if we get a RSET command
131 13 ▶sub PRINT {
Subroutine PRINT: called CC=4 SCAR=14.0
132  
133 13 my $f = shift->_socket;
134  
135 13 local $\ = "\015\012";
136 13 local $/ = "\015\012";
137  
138 13 ▶ if ($_[0] eq 'RSET'){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] eq "RSET") 3 10 13
139 3 ▶ print $f "--------NEW MESSAGE (connection reset)-------" if $f;
Branch: 1 branch
BranchTrueFalseTotal
if $f 3 0 3
140 };
141  
142 13 ▶ print $f @_ if $f;
Branch: 1 branch
BranchTrueFalseTotal
if $f 13 0 13
143  
144 13 return 1;
145 };
146  
147 12 ▶sub FILENO {
Subroutine FILENO: called CC=1 SCAR=0.0
148 12 my $f = shift->_socket;
149 12 my $n = fileno($f);
150 };
151  
152 # We can't read from this file, it's output only. However, we need to return something since
153 # talk_and_respond is expecting to read information from its SMTP socket
154  
155 13 ▶sub READLINE {
Subroutine READLINE: called CC=1 SCAR=0.0
156 13 return "250 bullshit all happy-happy" . scalar localtime() . "\015\012";
157 };
158  
159 # closes our filehandle
160  
161 1 ▶sub CLOSE {
Subroutine CLOSE: called CC=2 SCAR=7.1
162 1 my $f = shift->_socket;
163 1 ▶ close $f if $f;
Branch: 1 branch
BranchTrueFalseTotal
if $f 1 0 1
164 1 return 1;
165 };
166  
167 =pod
168  
169 =item disconnect
170  
171 overloaded disconnect method. Wipes out the internal socket as usual, but doesn't try
172 to say RSET or QUIT to the server.
173  
174 disconnect can also disconnect quietly, i.e., it won't try to issue a RSET and then quit before closing the socket.
175  
176 $server->disconnect(); #issues RSET and quit
177 $server->disconnect('quietly'); #issues nothing
178  
179 =back
180  
181 =cut
182  
183 1 ▶sub disconnect {
Subroutine disconnect: called CC=5 SCAR=16.6
Pod documented
184 1 my $self = shift;
185 1 my $quietly = shift;
186  
187 1 ▶ return $self unless $self->connected();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->connected 0 1 1
188  
189 1 ▶ $self->talk_and_respond('RSET') unless $quietly; #just to be polite
Branch: 1 branch
BranchTrueFalseTotal
unless $quietly 1 0 1
190 1 ▶ $self->talk_and_respond('quit') unless $quietly;
Branch: 1 branch
BranchTrueFalseTotal
unless $quietly 1 0 1
191  
192 1 ▶ if (my $socket = $self->socket) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $socket = $self->socket) 1 0 1
193 1 close $socket;
194 1 $socket = undef;
195 };
196 1 $self->socket(undef);
197 1 $self->connected(0);
198 1 return $self;
199 };
200  
201 1 1;
202  
203 __END__
« Bulkmail/Object.pm ↑ Summary