File Coverage

blib/lib/POE/Filter/Postfix.pm
Criterion Covered Total %
statement 37 51 72.5
branch 3 6 50.0
condition 0 9 0.0
subroutine 14 19 73.6
pod 13 13 100.0
total 67 98 68.3


line stmt bran cond sub pod time code
1 1     1   7 use strict;
  1         2  
  1         34  
2 1     1   6 use warnings;
  1         1  
  1         86  
3              
4             package POE::Filter::Postfix;
5             our $VERSION = '0.003';
6              
7              
8             # ABSTRACT: Postfix (MTA) text attribute communication
9 1     1   6 use base qw(POE::Filter);
  1         2  
  1         1074  
10              
11             sub _abstract {
12 3     3   5 my $name = shift;
13 3   0 0 1 809 eval sprintf <<'', ($name) x 2;
  0   0 0 1 0  
  0   0 0 1 0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
  0         0  
14             sub %s {
15             my $class = ref($_[0]) || $_[0];
16             require Carp;
17             Carp::croak("$class must override %s()");
18             }
19              
20             }
21              
22             BEGIN {
23 1     1   639 _abstract($_) for qw(
24             attribute_separator
25             attribute_terminator
26             request_terminator
27             )
28             }
29              
30              
31             sub new {
32 3     3 1 2645 my $class = shift;
33 3         16 bless {
34             @_,
35             buffer => '',
36             } => $class;
37             }
38              
39              
40             sub clone {
41 0     0 1 0 my $self = shift;
42 0         0 $self->new(%$self);
43             }
44              
45              
46             sub get_one_start {
47 3     3 1 27 my ($self, $buf) = @_;
48 3         36 $self->{buffer} .= $_ for @$buf;
49             }
50              
51              
52             sub get_one {
53 6     6 1 48 my ($self) = @_;
54 6         9 my %attr;
55 6         14 my $buf = $self->{buffer};
56 6         24 my ($a_s, $a_t, $r_t) = (
57             $self->attribute_separator,
58             $self->attribute_terminator,
59             $self->request_terminator,
60             );
61 6         118 while ($buf =~ s/^([^$r_t]+?)\Q$a_s\E//) {
62 12         74 my $key = $self->decode_key("$1");
63 12 50       129 $buf =~ s/^([^$r_t]*)?\Q$a_t\E// or return [];
64 12         51 $attr{$key} = $self->decode_value("$1");
65             }
66 6 100       613 return [] unless $buf =~ s/^\Q$r_t\E//;
67 3         8 $self->{buffer} = $buf;
68 3         14 return [ \%attr ];
69             }
70              
71              
72             sub get_pending {
73 0     0 1 0 my ($self) = @_;
74 0 0       0 return [ $self->{buffer} ] if length $self->{buffer};
75 0         0 return undef;
76             }
77              
78              
79 8     8 1 20 sub decode_key { $_[1] }
80 8     8 1 62 sub decode_value { $_[1] }
81              
82              
83             sub put {
84 3     3 1 42 my ($self, $chunks) = @_;
85              
86 3         6 return [ map { $self->_encode($_) } @$chunks ];
  3         17  
87             }
88              
89             sub _encode {
90 3     3   5 my ($self, $attr) = @_;
91 12         39 return join $self->attribute_terminator,
92             (map {
93 3         12 join $self->attribute_separator,
94             $self->encode_key($_),
95             $self->encode_value($attr->{$_})
96             } keys %$attr),
97             $self->request_terminator;
98             }
99              
100              
101 8     8 1 28 sub encode_key { $_[1] }
102 8     8 1 33 sub encode_value { $_[1] }
103              
104              
105             1;
106              
107              
108             __END__