File Coverage

lib/Qmail/Deliverable/Client.pm
Criterion Covered Total %
statement 17 47 36.1
branch 0 22 0.0
condition 0 3 0.0
subroutine 6 9 66.6
pod 2 2 100.0
total 25 83 30.1


line stmt bran cond sub pod time code
1             package Qmail::Deliverable::Client;
2              
3 1     1   1262 use strict;
  1         6  
  1         29  
4 1     1   12 use 5.006;
  1         2  
5 1     1   4 use Carp qw(carp);
  1         1  
  1         42  
6 1     1   3 use base 'Exporter';
  1         8  
  1         87  
7 1     1   402 use LWP::Simple qw($ua);
  1         69690  
  1         9  
8 1     1   332 use URI::Escape qw(uri_escape);
  1         2  
  1         869  
9              
10             our @EXPORT_OK = qw/qmail_local deliverable/;
11             our %EXPORT_TAGS = (all => \@EXPORT_OK);
12              
13             our $SERVER = "127.0.0.1:8998";
14             our $ERROR;
15              
16             # rfc2822's "atext"
17             my $atext = "[A-Za-z0-9!#\$%&\'*+\/=?^_\`{|}~-]";
18             my $valid = qr/^(?!.*\@.*\@)($atext+(?:[\@.]$atext+)*)\.?\z/;
19              
20             sub _remote {
21 0     0     my ($command, $arg) = @_;
22              
23 0 0         my $server = ref($SERVER) eq 'CODE'
24             ? $SERVER->()
25             : $SERVER;
26              
27 0 0         if (not defined $server) {
28 0           $ERROR = "No SERVER defined; connection not attempted";
29 0           return "\0";
30             }
31              
32 0           my $response = $ua->get(
33             "http://$server/qd1/$command?" . uri_escape($arg)
34             );
35              
36 0           my $code = $response->code;
37 0 0         return undef if $code == 204; # rpc undef
38              
39 0           my $sl = $response->status_line;
40 0 0         if ($code == 200) {
41 0           return $response->content;
42             }
43              
44 0           carp $ERROR = "Server $server unreachable or broken! ($sl)";
45 0           return "\0";
46             }
47              
48             sub qmail_local {
49 0     0 1   my ($in) = @_;
50             my ($address) = lc($in) =~ /$valid/ or
51 0 0         do { carp "Invalid address: $in"; return; };
  0            
  0            
52              
53             # This we can do locally. Let's not waste HTTP requests :)
54 0 0         return $address if $address !~ /\@/;
55              
56 0           my $rv = _remote 'qmail_local', $address;
57 0 0 0       return "" if defined $rv and $rv eq "\0";
58 0           return $rv;
59             }
60              
61             sub deliverable {
62 0     0 1   my ($in) = @_;
63             my ($address) = lc($in) =~ /$valid/
64 0 0         or do { carp "Invalid address: $in"; return; };
  0            
  0            
65              
66 0           my $rv = _remote 'deliverable', $address;
67 0 0         return 0x2f if not defined $rv; # shouldn't happen
68 0 0         return 0x2f if not length $rv; # shouldn't happen
69 0 0         return 0x2f if $rv eq "\0";
70              
71 0           return $rv;
72             }
73              
74             1;
75              
76             __END__