blib/lib/Mail/Query.pm

filter: Statementstmt 33.3% 18 / 54 Branchbran 0.0% 0 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 41.6% 5 / 12 Podpod 25.0% 2 / 8 totaltotal 25.0% 25 / 100 CC 14 SCAR 31.0
CC
14
Cov
22%
compare
56.0
between
20.0
like
12.0
CRAP 22.2
↑ Summary
1 package Mail::Query;
2  
3 1 require 5.005_62;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use base 'Mail::Audit';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Parse::RecDescent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 our $VERSION = '0.01';
9  
10 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.2
Pod documented
11 1 my $package = shift;
12
13 1 my $self = $package->SUPER::new(@_);
14 1 $self->{parser} = new Parse::RecDescent($self->grammar);
15 0 return $self;
16 }
17  
18 0 ▶sub query {
Subroutine query: not called CC=1 SCAR=6.9
Pod undocumented
19 0 my ($self, $query) = @_;
20 0 local $self->{parser}{local}{mq} = $self; # Circular ref, but local. No sweat.
21 0 return $self->{parser}->where_clause($query);
22 }
23  
24 0 ▶sub compare {
Subroutine compare: not called CC=7 SCAR=40.3
Pod undocumented
25 0 my ($self, $field, $op, $string) = @_;
26
27 # We handle =, <, and >
28 0 ▶ return !$self->compare($field, '=', $string) if $op eq '!=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "!=" 0 0 0
29 0 ▶ return !$self->compare($field, '<', $string) if $op eq '>=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq ">=" 0 0 0
30 0 ▶ return !$self->compare($field, '>', $string) if $op eq '<=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "<=" 0 0 0
31  
32  
33 # This should be date-aware, at the least. So far we punt.
34 0 my $val = $self->get($field);
35 #warn "comparing: '$val' $op '$string'";
36 0 ▶ return $val eq $string if $op eq '=';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "=" 0 0 0
37 0 ▶ return $val lt $string if $op eq '<';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq "<" 0 0 0
38 0 ▶ return $val gt $string if $op eq '>';
Branch: 1 branch
BranchTrueFalseTotal
if $op eq ">" 0 0 0
39
40 0 die "Unknown operator '$op'";
41 }
42  
43 0 ▶sub between {
Subroutine between: not called CC=4 SCAR=30.0
Pod undocumented
44 0 my ($self, $field, $one, $two) = @_;
45
46 # This should be date-aware, at the least. So far we punt.
47 0 ▶ ($one, $two) = ($two, $one) if $one gt $two;
Branch: 1 branch
BranchTrueFalseTotal
if $one gt $two 0 0 0
48 0 ▶ return 0 unless $one lt $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $one lt $field 0 0 0
49 0 ▶ return 0 unless $field lt $two;
Branch: 1 branch
BranchTrueFalseTotal
unless $field lt $two 0 0 0
50 0 return 1;
51 }
52  
53 0 ▶sub like {
Subroutine like: not called CC=3 SCAR=24.8
Pod undocumented
54 0 my ($self, $field, $pattern) = @_;
55
56 0 ▶ if ($pattern->[1] eq 'regex') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern->[1] eq "regex") 0 0 0
57 0 (my $pat = $pattern->[0]) =~ s/([\@\$])/\\$1/g; # A limited quotemeta (is this a good idea?)
58 #warn "$field =~ $pat";
59 0 my $result = eval "\$self->get(\$field) =~ $pat"; # eval to maintain 5.004 compat
60 0 ▶ warn "Error in pattern $pat" unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 0 0 0
61 0 return $result;
62 }
63
64 # $pattern->[1] eq 'string'
65 # A string like 'boo%hoo' maps to /^boo.*hoo$/
66 0 my $string = quotemeta($pattern->[0]);
67 0 $string =~ s/%/.*/;
68 0 return $self->get($field) =~ /^$string$/;
69 }
70  
71 0 ▶sub exists {
Subroutine exists: not called CC=1 SCAR=6.9
Pod undocumented
72 0 my ($self, $field) = @_;
73 #my $val = $self->get($field); warn "checking for defined($field): ", defined($val);
74  
75 0 return defined $self->get($field);
76 }
77  
78 # We implement a 'Recipient' field, which is any of To, Cc, or Bcc
79 # We also make 'body' a header-like field, for queries like "body LIKE /blah/"
80 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
Pod documented
81 0 my ($self, $field) = @_;
82 0 ▶ return join '', @{$self->body} if lc($field) eq 'body';
Branch: 1 branch
BranchTrueFalseTotal
if lc $field eq "body" 0 0 0
83 0 ▶ return join ', ', map {$self->SUPER::get($_)} qw(To Cc Bcc) if lc($field) eq 'recipient';
Branch: 1 branch
BranchTrueFalseTotal
if lc $field eq "recipient" 0 0 0
84 0 return $self->SUPER::get($field);
85 }
86  
87 0 ▶sub grammar {
Subroutine grammar: not called CC=1 SCAR=6.9
Pod undocumented
88 0 return <<'EOF';
89 # Excised from http://www.contrib.andrew.cmu.edu/~shadow/sql/sql2bnf.aug92.txt
90
91 where_clause: search_condition /^\z/ {$return = $item{search_condition}}
92 | <error>
93
94 search_condition: <leftop: bool_term /OR/i bool_term>
95 {$return = grep {$_} @{$item[1]}}
96
97 bool_term: <leftop: bool_factor /AND/i bool_factor>
98 {$return = !grep {!$_} @{$item[1]}}
99
100 bool_factor: not(?) bool_primary {$return = @{$item[1]} ? !$item[2] : $item[2]}
101 # Don't support IS TRUE and IS NOT UNKNOWN and all that crap
102
103 bool_primary: '(' <commit> search_condition ')' {$return = $item[3]}
104 | predicate
105
106 predicate: comparison_predicate
107 | between_predicate
108 | like_predicate
109 | null_predicate
110 # There's more here, but I'm skipping for now.
111
112 # These only accept header field names as the LHS, and don't allow functions yet.
113 comparison_predicate: header comp_op string {$return = $thisparser->{local}{mq}->compare(@item[1,2,3])}
114
115 between_predicate: header not(?) between string /AND/i string
116 {my $x = $thisparser->{local}{mq}->between(@item[1,4,6]);
117 $return = @{$item[2]} ? !$x : $x}
118
119 like_predicate: header not(?) like rhs {my $x = $thisparser->{local}{mq}->like(@item[1,4]);
120 $return = @{$item[2]} ? !$x : $x}
121
122 null_predicate: header is not(?) null {my $x = $thisparser->{local}{mq}->exists($item[1]);
123 $return = @{$item[3]} ? $x : !$x}
124
125 rhs: string {$return = [$item[1], 'string']}
126 | regex {$return = [$item[1], 'regex' ]}
127
128 # With a true $arg[0], returns a two-element listref.
129 string: {my @x = extract_quotelike($text);
130 if ($x[0] and ($x[3] =~ m/^q+$/ or $x[4] =~ m/^['"]$/) ) { # Strings only, not regexes & so on
131 substr($text,0,pos($text)) = '';
132 $return = $x[5];
133 } else {
134 $return = undef;
135 }
136 }
137
138 regex: {local $_ = extract_quotelike($text);
139 $return = (m/^m/ or m/^\//) ? $_ : undef}
140
141 comp_op: '=' | '!=' | '<=' | '>=' | '<' | '>'
142
143 header: /[\w-]+/ # dashes are allowed, very common in headers.
144
145 not: /NOT/i
146 is: /IS/i
147 like: /LIKE/i
148 null: /NULL/i
149 between: /BETWEEN/i
150  
151 EOF
152 }
153  
154 1 1;
155 __END__
↑ Summary