blib/lib/Mail/DSPAM/Learning.pm

filter: Statementstmt 90.5% 134 / 148 Branchbran 60.0% 12 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.6% 29 / 32 Podpod 94.4% 17 / 18 totaltotal 86.5% 193 / 223 CC 14 SCAR 8.0
CC
14
Cov
86%
_getDomain
5.5
forwardMessages
4.1
_getIP
3.3
CRAP 2.2
↑ Summary
1 #!/usr/bin/perl
2  
3 package Mail::DSPAM::Learning;
4  
5  
6 7 our $VERSION='0.1';
7  
8 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use Mail::MboxParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Mail::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Email::Send;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Mail::Box::Manager;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Term::ReadKey;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 7 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 7 ▶use Net::DNS;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Sys::HostIP;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 ▶use Module::TestConfig;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
27 7 my $class = shift;
28  
29 7 my $learner = {
30 'delay' => 5,
31 'passwd' => undef,
32 'mailbox' => undef,
33 'mailer' => undef,
34 'folder' => undef,
35 'mailboxmanager' => undef,
36 'config' => undef,
37 };
38  
39 7 bless $learner, $class;
40 7 return $learner;
41 }
42  
43 3 ▶sub defineMyConfig {
Subroutine defineMyConfig: called CC=2 SCAR=6.9
Pod documented
44 3 my ($self, $path) = @_;
45  
46 3 ▶ if ( ! -f $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (not -f $path) { } 1 2 3
47 1 mkpath(dirname($path));
48  
49 1 my $domain = &_getDomain;
50 1 my $username = "nobody";
51  
52 1 Module::TestConfig->new(
53 verbose => 1,
54 defaults => 'defaults.config',
55 file => $path,
56 package => 'MyConfig', # Mail::DSPAM::Learning::
57 order => [ qw/defaults env/ ],
58 questions => [
59 [ 'Domain?' => 'hello', $domain],
60 [ 'User name?' => 'user', $username ],
61 [ 'Email address?' => 'from', "$username\@$domain" ], # , {skip => \&setDomain }
62 [ 'DSPAM email address' => 'to', "spam\@$domain" ],
63 [ 'SMTP server?' => 'smtp', "mail.$domain" ],
64 [ 'SMTP port?' => 'port', '25' ],
65 ]
66 )->ask->save;
67 1 return(2);
68 } else {
69 2 return(1);
70 }
71 }
72  
73 2 ▶sub setMyConfig {
Subroutine setMyConfig: called CC=1 SCAR=0.0
Pod documented
74 2 my ($self) = @_;
75  
76 2 $self->{"config"} = MyConfig->new;
77 }
78  
79 10 ▶sub getMyConfig {
Subroutine getMyConfig: called CC=1 SCAR=0.0
Pod documented
80 10 my ($self) = @_;
81  
82 10 return($self->{"config"});
83 }
84  
85 1 ▶sub printMyConfig {
Subroutine printMyConfig: called CC=2 SCAR=6.9
Pod documented
86 1 my ($self) = @_;
87  
88 1 my $config = $self->getMyConfig;
89  
90 1 my @question_name = ('hello', 'user', 'from', 'to', 'smtp', 'port');
91  
92 1 warn "\n\n";
93 1 warn "Current Configuration: \n";
94 1 foreach my $q (@question_name) {
95 6 warn "\t$q: " . $config->$q . "\n";
96 }
97 1 warn "\n";
98 1 return(1);
99 }
100  
101 # this method set the delay for sending a mail to learn to dspam
102  
103 1 ▶sub setDelay {
Subroutine setDelay: called CC=1 SCAR=0.0
Pod documented
104 1 my ($self, $delay) = @_;
105  
106 1 $self->{'delay'} = $delay;
107 }
108  
109 # this method returns the delay for sending a mail to learn to dspam
110  
111  
112 6 ▶sub getDelay {
Subroutine getDelay: called CC=1 SCAR=0.0
Pod documented
113 6 my ($self) = @_;
114  
115 6 return($self->{'delay'});
116 }
117  
118 # this method set the password of the user for the current session
119  
120 1 ▶sub askPassword {
Subroutine askPassword: called CC=1 SCAR=0.0
Pod undocumented
121 1 my ($self) = @_;
122
123 1 print "Password: ";
124 1 ReadMode('noecho');
125 1 $self->{'password'} = ReadLine(0);
126 1 chomp $self->{'password'};
127 1 ReadMode('restore');
128 1 print "\n";
129 }
130  
131 # this method set the mailbox file to parse. This mailbox contains the SPAN to learn
132  
133 3 ▶sub setMailbox {
Subroutine setMailbox: called CC=1 SCAR=0.0
Pod documented
134 3 my ($self, $mailbox) = @_;
135  
136 3 $self->{'mailbox'} = $mailbox;
137  
138 }
139  
140 # this method returns the mailbox file which contains the SPAN to learn
141  
142 3 ▶sub getMailbox {
Subroutine getMailbox: called CC=1 SCAR=0.0
Pod documented
143  
144 3 my ($self) = @_;
145  
146 3 return($self->{'mailbox'});
147  
148 }
149  
150 # this method set the new mailbox manager
151  
152 2 ▶sub setMailboxManager {
Subroutine setMailboxManager: called CC=1 SCAR=0.0
Pod documented
153  
154 2 my ($self) = @_;
155  
156 2 my $mgr = Mail::Box::Manager->new;
157  
158 2 $self->{'mailboxmanager'} = $mgr;
159 }
160  
161 # this method get the mailbox manager
162  
163 2 ▶sub getMailboxManager {
Subroutine getMailboxManager: called CC=1 SCAR=0.0
Pod documented
164  
165 2 my ($self) = @_;
166  
167  
168 2 return($self->{'mailboxmanager'});
169 }
170  
171 # the method parses and loads the mailbox containing the SPAMs, and sets the folder field
172  
173 2 ▶sub parseMailbox {
Subroutine parseMailbox: called CC=3 SCAR=11.2
Pod documented
174 2 my ($self) = @_;
175  
176 2 my $mb = $self->getMailbox;
177 2 ▶ if (!defined $mb) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mb) 0 2 2
178 0 die "Mailbox is not set. Exit\n";
179 }
180  
181 2 my $mbmgr = $self->getMailboxManager;
182 2 ▶ if (!defined $mbmgr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $mbmgr) 2 0 2
183 2 $mbmgr = $self->setMailboxManager;
184 }
185 2 warn "Openning mailbox $mb\n";
186  
187 2 my $folder = $mbmgr->open(folder => $mb);
188 2 $self->{'folder'} = $folder;
189  
190 2 print STDERR "folder = $folder\n";
191 }
192  
193 # this method returns the folder field
194  
195 1 ▶sub getFolder {
Subroutine getFolder: called CC=1 SCAR=0.0
Pod documented
196  
197 1 my ($self) = @_;
198  
199 1 return($self->{'folder'});
200  
201 }
202  
203 # This method defines the message encapsulating the forwarded message and returns it
204  
205 1 ▶sub forwardMessages {
Subroutine forwardMessages: called CC=4 SCAR=14.0
Pod documented
206  
207 1 my ($self, $sending) = @_;
208  
209 1 my $msg;
210 my $forward_msg;
211 1 my $folder = $self->getFolder;
212 1 my $count = 0;
213 1 my $return_value = undef;
214  
215  
216 1 my $preamble = Mail::Message::Body->new('data' => "This is a multi-part message in MIME format.");
217  
218 1 ▶ while ($msg = $folder->message($count)) { # $msg is a Mail::Message now
Branch: 1 branch
BranchTrueFalseTotal
if ($msg = $folder->message($count)) 3 1 4
219 3 print STDERR "message Id = " . $msg->messageId . "\n";
220 3 $count++;
221  
222 3 $forward_msg = $msg->forwardEncapsulate('To' => $self->getMyConfig->to,
223 'From' => $self->getMyConfig->from,
224 'Cc' => $self->getMyConfig->from,
225 'Subject' => '[Fwd: ' . $msg->subject . ']',
226 'preamble' => $preamble,
227 );
228 3 print STDERR "\tSending the message ($count)\n";
229  
230 3 ▶ if ((!defined $sending) || ($sending > 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $sending or $sending > 0) { } 0 3 3
Condition33%1/3
not defined $sending or $sending > 0
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $sending or $sending > 0
not defined $sending $sending > 0
Truth table33%1/3
not(A) or B
A= not defined $sendingB= $sending > 0
ABresult
1X1
011
000
231 0 $return_value = $self->sendForwardedMessage($forward_msg);
232 } else {
233 3 $return_value = warn($forward_msg->head);
234 }
235  
236 3 print STDERR "done ($return_value)\n";
237 3 print STDERR "Sleeping for " . $self->getDelay . " second\n";
238 3 sleep($self->getDelay);
239 }
240 1 return($count);
241 }
242  
243 # This method sets the mailer
244  
245 0 ▶sub setMailer {
Subroutine setMailer: not called CC=1 SCAR=6.9
Pod documented
246  
247 0 my ($self) = @_;
248  
249 $self->{"mailer"} = Email::Send->new({mailer_args => [
250 $self->getMyConfig->smtp,
251 Port => $self->getMyConfig->port,
252 User => $self->getMyConfig->user,
253 0 Password => $self->{"passwd"},
254 Hello => $self->getMyConfig->hello,
255 ]
256 });
257 }
258  
259 # This method returns the mailer
260  
261 0 ▶sub getMailer {
Subroutine getMailer: not called CC=1 SCAR=6.9
Pod documented
262  
263 0 my ($self) = @_;
264  
265 0 return($self->{"mailer"});
266  
267 }
268  
269  
270 # this method sends the formwarded message
271  
272 0 ▶sub sendForwardedMessage {
Subroutine sendForwardedMessage: not called CC=1 SCAR=6.9
Pod documented
273  
274 0 my ($self, $forward_msg) = @_;
275  
276  
277 0 return($self->{"mailer"}->send($forward_msg->string));
278
279 }
280  
281  
282 1 ▶sub _getIP {
Subroutine _getIP: called CC=3 SCAR=11.8
283  
284 1 my $ip_addresses = Sys::HostIP->ips;
285 1 my $ip="127.0.0.1";
286 1 my $i=0;
287
288 1 ▶ while($ip_addresses->[$i] eq "127.0.0.1") {
Branch: 1 branch
BranchTrueFalseTotal
if ($ip_addresses->[$i] eq "127.0.0.1") 0 1 1
289 0 $i++;
290 }
291
292 1 ▶ if ($i < scalar(@$ip_addresses)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$ip_addresses) 0 1 1
293 0 $ip = $ip_addresses->[$i];
294 }
295
296
297 1 warn "IP: $ip\n";
298 1 return($ip);
299 }
300 #
301  
302 1 ▶sub _getDomain {
Subroutine _getDomain: called CC=5 SCAR=17.0
303 1 my $ip = &_getIP;
304 1 my $res = Net::DNS::Resolver->new;
305
306 1 my $query;
307 1 my $hostname="localhost";
308  
309 1 eval {
310 1 $query = $res->search($ip);
311 };
312
313 1 ▶ if (!$@) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $@) { } 1 0 1
314 1 ▶ if ($query) {
Branch: 1 branch
BranchTrueFalseTotal
if ($query) { } 1 0 1
315 1 foreach my $rr ($query->answer) {
316 1 ▶ next unless $rr->type eq "PTR";
Branch: 1 branch
BranchTrueFalseTotal
unless $rr->type eq "PTR" 0 1 1
317 1 print STDERR $rr->rdatastr, "\n";
318 1 $hostname = $rr->rdatastr;
319 }
320
321  
322 1 warn "Hostname: $hostname\n";
323
324 1 $hostname =~ /^([^\.]+)\.(.*)\.$/;
325
326 1 my $domain = $2;
327 1 return ($domain);
328 } else {
329 0 warn "query failed: ", $res->errorstring, "\n";
330 0 return("localdomain");
331 }
332 } else {
333 0 warn "query failed: ", $res->errorstring, "\n";
334 0 return("localdomain");
335 }
336  
337 }
338  
339  
340 __END__
↑ Summary