Karmasphere/Query.pm

filter: Statementstmt 49.7% 93 / 187 Branchbran 24.5% 25 / 102 Conditioncond 25.0% 2 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 55.5% 15 / 27 Podpod 47.3% 9 / 19 totaltotal 41.6% 145 / 348 CC 65 SCAR 35.7
CC
65
Cov
40%
new
120.4
guess_identity_type
42.0
composite
30.0
CRAP 35.6
« SpamAssassin/Plugin/Karmasphere.pm ↑ Summary Karmasphere/Response.pm »
1 package Mail::Karmasphere::Query;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $ID);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 @ISA = qw(Exporter);
11 5 @EXPORT_OK = qw(guess_identity_type);
12 5 %EXPORT_TAGS = (
13 'all' => \@EXPORT_OK,
14 );
15 }
16  
17 5 ▶use Mail::Karmasphere::Client qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 $ID = 0;
20  
21 2 ▶sub new {
Subroutine new: called CC=20 SCAR=47.9
Pod undocumented
22 2 my $class = shift;
23 0 ▶ my $args = ($#_ == 0) ? { %{ (shift) } } : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
$#_ == 0 ? : 0 2 2
24  
25 #use Data::Dumper;
26 #print Dumper($args);
27  
28 2 my $self = bless { }, $class;
29  
30 2 ▶ if (exists $args->{Id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"Id"}) 0 2 2
31 0 $self->id(delete $args->{Id});
32 }
33  
34 2 ▶ if (exists $args->{Identities}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"Identities"}) 0 2 2
35 0 my $identities = delete $args->{Identities};
36 0 ▶ die "Identities must be a listref, not " . ref($identities)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $identities eq "ARRAY" 0 0 0
37 unless ref($identities) eq 'ARRAY';
38 0 for my $identity (@$identities) {
39 0 ▶ if (ref($identity) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $identity eq 'ARRAY') { } 0 0 0
40 0 $self->identity(@$identity);
41 }
42 else {
43 0 $self->identity($identity);
44 }
45 }
46 }
47  
48 2 ▶ if (exists $args->{Composites}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"Composites"}) 0 2 2
49 0 my $composites = delete $args->{Composites};
50 0 ▶ die "Composites must be a listref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $composites eq "ARRAY" 0 0 0
51 unless ref($composites) eq 'ARRAY';
52 0 $self->composite($_) for @$composites;
53 }
54  
55 2 ▶ if (exists $args->{Composite}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"Composite"}) 0 2 2
56 0 $self->composite(delete $args->{Composite});
57 }
58  
59 2 ▶ if (exists $args->{Feeds}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"Feeds"}) 0 2 2
60 0 my $feeds = delete $args->{Feeds};
61 0 ▶ die "Feeds must be a listref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $feeds eq "ARRAY" 0 0 0
62 unless ref($feeds) eq 'ARRAY';
63 0 $self->feed($_) for @$feeds;
64 }
65  
66 2 ▶ if (exists $args->{Combiners}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"Combiners"}) 0 2 2
67 0 my $combiners = delete $args->{Combiners};
68 0 ▶ die "Combiners must be a listref"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $combiners eq "ARRAY" 0 0 0
69 unless ref($combiners) eq 'ARRAY';
70 0 $self->combiner($_) for @$combiners;
71 }
72  
73 2 ▶ if (exists $args->{Combiner}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"Combiner"}) 0 2 2
74 0 $self->combiner(delete $args->{Combiner});
75 }
76  
77 2 ▶ if (exists $args->{Flags}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"Flags"}) 0 2 2
78 0 $self->flags(delete $args->{Flags});
79 }
80  
81 2 my @remain = keys %$args;
82 2 ▶ if (@remain) {
Branch: 1 branch
BranchTrueFalseTotal
if (@remain) 0 2 2
83 0 carp "Unrecognised arguments to constructor: @remain";
84 }
85  
86 2 return $self;
87 }
88  
89 0 ▶sub guess_identity_type {
Subroutine guess_identity_type: not called CC=6 SCAR=37.4
Pod undocumented
90 0 my $identity = shift;
91  
92 0 ▶ if ($identity =~ /^[0-9\.]{7,15}$/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($identity =~ /^[0-9\.]{7,15}$/) { } 0 0 0
elsif ($identity =~ /^[0-9a-f:]{2,64}$/i) { } 0 0 0
elsif ($identity =~ m[^https?://]) { } 0 0 0
elsif ($identity =~ /@/) { } 0 0 0
elsif ($identity =~ /\./) { } 0 0 0
93 0 return IDT_IP4;
94 }
95 elsif ($identity =~ /^[0-9a-f:]{2,64}$/i) {
96 0 return IDT_IP6;
97 }
98 elsif ($identity =~ /^https?:\/\//) {
99 0 return IDT_URL;
100 }
101 elsif ($identity =~ /@/) {
102 0 return IDT_EMAIL;
103 }
104 elsif ($identity =~ /\./) {
105 0 return IDT_DOMAIN;
106 }
107  
108 0 return undef;
109 }
110  
111 1 ▶sub id {
Subroutine id: called CC=3 SCAR=12.0
Pod documented
112 1 my $self = shift;
113 1 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 1 1
elsif (not defined $self->{'Id'}) { } 1 0 1
114 0 $self->{Id} = shift;
115 }
116 elsif (!defined $self->{Id}) {
117 1 $self->{Id} = 'mkc' . $ID++ . "-" . time();
118 }
119 1 return $self->{Id};
120 }
121  
122 8 ▶sub identity {
Subroutine identity: called CC=7 SCAR=20.0
Pod documented
123 8 my ($self, $identity, @tags) = @_;
124 8 ▶ unless (ref($identity) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $identity eq "ARRAY") 8 0 8
125 8 my $type;
126 8 ▶ if (@tags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tags) { } 8 0 8
127 8 $type = shift @tags;
128 }
129 else {
130 0 warn "Guessing identity type for $identity";
131 0 $type = guess_identity_type($identity);
132 }
133 8 $identity = [ $identity, $type ];
134 }
135 8 ▶ push(@$identity, @tags) if @tags;
Branch: 1 branch
BranchTrueFalseTotal
if @tags 8 0 8
136 8 for (@{ $self->{Identities} }) {
137 # If the data and the type match
138 6 ▶ if (($_->[0] eq $identity->[0]) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($_->[0] eq $identity->[0] and $_->[1] eq $identity->[1]) 5 1 6
Condition66%2/3
$_->[0] eq $identity->[0] and $_->[1] eq $identity->[1]
!ll&&!rl&&r
015
MC/DC50%1/2
$_->[0] eq $identity->[0] and $_->[1] eq $identity->[1]
$_->[0] eq $identity->[0] $_->[1] eq $identity->[1]
Truth table66%2/3
A and B
A= $_->[0] eq $identity->[0]B= $_->[1] eq $identity->[1]
ABresult
0X0
100
111
139 ($_->[1] eq $identity->[1])) {
140 # Combine the tags from the new identity;
141 5 shift @$identity; shift @$identity;
142 5 push(@{ $_ }, @$identity);
143 5 return;
144 }
145 }
146 3 push(@{ $self->{Identities} }, $identity);
147 }
148  
149 1 ▶sub identities {
Subroutine identities: called CC=3 SCAR=13.1
Pod documented
150 1 my $self = shift;
151 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 1 1
152 0 $self->{Identities} = [ ];
153 0 $self->identity($_) for @_;
154 }
155 1 return $self->{Identities};
156 }
157  
158 0 ▶sub has_identities {
Subroutine has_identities: not called CC=3 SCAR=24.8
Pod undocumented
159 0 my $self = shift;
160 0 ▶ return undef unless exists $self->{Identities};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Identities"} 0 0 0
161 0 ▶ return undef unless @{ $self->{Identities} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"Identities"};} 0 0 0
162 0 return 1;
163 }
164  
165 0 ▶sub composite {
Subroutine composite: not called CC=5 SCAR=34.0
Pod documented
166 0 my ($self, @composites) = @_;
167 0 for my $composite (@composites) {
168 # Validate
169 0 ▶ if (ref($composite)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $composite) { } 0 0 0
elsif ($composite =~ /^[0-9]+$/) { } 0 0 0
elsif ($composite =~ /\./) { } 0 0 0
170 0 die "Composite may not be a reference";
171 }
172 elsif ($composite =~ /^[0-9]+$/) {
173 0 warn "Using numeric ids for composites should be avoided.";
174 }
175 elsif ($composite =~ /\./) {
176 }
177 else {
178 0 warn "Composite name does not contain a dot. Invalid?";
179 }
180 0 push(@{ $self->{Composites} }, $composite);
181 }
182 }
183  
184 0 ▶sub composites {
Subroutine composites: not called CC=2 SCAR=17.9
Pod documented
185 0 my $self = shift;
186 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
187 0 $self->{Composites} = [ ];
188 0 $self->composite(@_);
189 }
190 0 return $self->{Composites};
191 }
192  
193 0 ▶sub has_composites {
Subroutine has_composites: not called CC=3 SCAR=24.8
Pod undocumented
194 0 my $self = shift;
195 0 ▶ return undef unless exists $self->{Composites};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Composites"} 0 0 0
196 0 ▶ return undef unless @{ $self->{Composites} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"Composites"};} 0 0 0
197 0 return 1;
198 }
199  
200 3 ▶sub feed {
Subroutine feed: called CC=2 SCAR=6.9
Pod undocumented
201 3 my ($self, @feeds) = @_;
202 3 for my $feed (@feeds) {
203 # Validate.
204 # if ($feed =~ /^[0-9]+$/) {
205 # warn "Numeric feed ids are deprecated.";
206 # }
207 3 push(@{ $self->{Feeds} }, $feed);
208 }
209 }
210  
211 0 ▶sub feeds {
Subroutine feeds: not called CC=2 SCAR=17.9
Pod documented
212 0 my $self = shift;
213 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
214 0 $self->{Feeds} = [ ];
215 0 $self->feed(@_);
216 }
217 0 return $self->{Feeds};
218 }
219  
220 0 ▶sub has_feeds {
Subroutine has_feeds: not called CC=3 SCAR=24.8
Pod undocumented
221 0 my $self = shift;
222 0 ▶ return undef unless exists $self->{Feeds};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Feeds"} 0 0 0
223 0 ▶ return undef unless @{ $self->{Feeds} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"Feeds"};} 0 0 0
224 0 return 1;
225 }
226  
227 3 ▶sub combiner {
Subroutine combiner: called CC=2 SCAR=6.9
Pod documented
228 3 my ($self, @combiners) = @_;
229 3 for my $combiner (@combiners) {
230 # Validate.
231 3 push(@{ $self->{Combiners} }, $combiner);
232 }
233 }
234  
235 0 ▶sub combiners {
Subroutine combiners: not called CC=2 SCAR=17.9
Pod documented
236 0 my $self = shift;
237 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
238 0 $self->{Combiners} = [ ];
239 0 $self->combiner(@_);
240 }
241 0 return $self->{Combiners};
242 }
243  
244 0 ▶sub has_combiners {
Subroutine has_combiners: not called CC=3 SCAR=24.8
Pod undocumented
245 0 my $self = shift;
246 0 ▶ return undef unless exists $self->{Combiners};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Combiners"} 0 0 0
247 0 ▶ return undef unless @{ $self->{Combiners} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"Combiners"};} 0 0 0
248 0 return 1;
249 }
250  
251 0 ▶sub flags {
Subroutine flags: not called CC=3 SCAR=24.8
Pod documented
252 0 my $self = shift;
253 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
254 0 my $flags = shift;
255 0 ▶ die "Flags must be an integer" unless $flags =~ /^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $flags =~ /^[0-9]+$/ 0 0 0
256 0 $self->{Flags} = $flags;
257 }
258 0 return $self->{Flags};
259 }
260  
261 0 ▶sub has_flags {
Subroutine has_flags: not called CC=3 SCAR=24.8
Pod undocumented
262 0 my $self = shift;
263 0 ▶ return undef unless exists $self->{Flags};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"Flags"} 0 0 0
264 0 ▶ return undef unless defined $self->{Flags};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"Flags"} 0 0 0
265 0 return 1;
266 }
267  
268 0 ▶sub identities_as_humanreadable_string {
Subroutine identities_as_humanreadable_string: not called CC=3 SCAR=24.8
Pod undocumented
269 0 my $self = shift;
270 0 ▶ my @identities = @{ $self->{Identities} || [] };
Condition0%0/2
$self->{'Identities'} || []
l!l
00
MC/DC0%0/1
$self->{'Identities'} || []
$self->{'Identities'}
Truth table0%0/2
A
A= $self->{'Identities'}
Aresult
11
00
271 0 ▶ return join ",", (map { join "=", ($_->[1], $_->[0], ($_->[2] || ())) } @identities);
Condition0%0/3
$_->[2] || ()
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->[2] || ()
$_->[2] ()
Truth table0%0/3
A || B
A= $_->[2]B= ()
ABresult
1X1
011
000
272 }
273  
274 4 ▶sub _as_string_sizeof {
Subroutine _as_string_sizeof: called CC=2 SCAR=6.9
275 4 my $ref = shift;
276 4 ▶ return "0" unless defined $ref;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 1 3 4
277 3 return scalar(@$ref);
278 }
279  
280 1 ▶sub as_string {
Subroutine as_string: called CC=7 SCAR=20.1
Pod undocumented
281 1 my ($self) = @_;
282 1 my $out = "Query id '" . $self->id . "': ";
283 1 $out .= _as_string_sizeof($self->{Identities}) . ' identities, ';
284 1 $out .= _as_string_sizeof($self->{Feeds}) . ' feeds, ';
285 1 $out .= _as_string_sizeof($self->{Composites}) . " composites, ";
286 1 $out .= _as_string_sizeof($self->{Combiners}) . " combiners\n";
287 1 ▶ if ($self->{Identities}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Identities"}) 1 0 1
288 1 for (@{ $self->{Identities} }) {
289 1 my ($id, $t, @t) = @$_;
290 1 $out .= "Identity: $id ($t)";
291 1 ▶ $out .= " (= @t)" if @t;
Branch: 1 branch
BranchTrueFalseTotal
if @t 1 0 1
292 1 $out .= "\n";
293 }
294 }
295 1 ▶ if ($self->{Composites}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Composites"}) 0 1 1
296 $out .= "Composites: " .
297 0 join(' ', sort @{ $self->{Composites} } ) .
298 "\n";
299 }
300 1 ▶ if ($self->{Feeds}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Feeds"}) 1 0 1
301 $out .= "Feeds: " .
302 1 join(' ', sort @{ $self->{Feeds} } ) .
303 "\n";
304 }
305 1 ▶ if ($self->{Combiners}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Combiners"}) 1 0 1
306 $out .= "Combiners: " .
307 1 join(' ', sort @{ $self->{Combiners} } ) .
308 "\n";
309 }
310 1 return $out;
311 }
312  
313 =head1 NAME
314  
315 Mail::Karmasphere::Query - Karmasphere Query Object
316  
317 =head1 SYNOPSIS
318  
319 my $query = new Mail::Karmasphere::Query(...);
320  
321 =head1 DESCRIPTION
322  
323 The Perl Karma Client API consists of three objects: The Query, the
324 Response and the Client. The user constructs a Query and passes it to
325 a Client, which returns a Response. See L<Mail::Karmasphere::Client>
326 for more information.
327  
328 =head1 CONSTRUCTOR
329  
330 The class method new(...) constructs a new Query object. All arguments
331 are optional. The following parameters are recognised as arguments
332 to new():
333  
334 =over 4
335  
336 =item Identities
337  
338 A listref of identities, each of which is an [ identity, type ] pair.
339  
340 =item Composites
341  
342 A listref of composite keynames.
343  
344 =item Composite
345  
346 A single composite keyname.
347  
348 =item Flags
349  
350 The query flags.
351  
352 =item Id
353  
354 The id of this query, returned in the response. The id is autogenerated
355 in a new query if not provided, and may be retrieved using $query->id.
356  
357 =item Feeds
358  
359 A listref of feed ids.
360  
361 =item Combiners
362  
363 A listref of combiner names.
364  
365 =item Combiner
366  
367 A single combiner name.
368  
369 =back
370  
371 =head1 METHODS
372  
373 =head2 PRIMARY METHODS
374  
375 These methods are the ones you must understand in order to use
376 Mail::Karmashere::Client.
377  
378 =over 4
379  
380 =item $query->identity($data, $type, @tags)
381  
382 Adds an identity to this query.
383  
384 =item $query->composite(@composites)
385  
386 Adds one or more composites to this query.
387  
388 =item $query->flags($flags)
389  
390 Sets or returns the flags of this query.
391  
392 =back
393  
394 =head2 OTHER METHODS
395  
396 These methods permit more flexibility and access to more features.
397  
398 =over 4
399  
400 =item $query->id([$id])
401  
402 Sets or returns the id of this query. If the query has no id, an id
403 will be generated by the client and will appear in the response.
404  
405 =item $query->identities(@identities)
406  
407 Sets or returns the identities of this query.
408  
409 =item $query->composites(@composites)
410  
411 Sets or returns the composites of this query.
412  
413 =item $query->feeds(@feeds)
414  
415 Sets or returns the feeds of this query.
416  
417 =item $query->feeds(@feeds)
418  
419 Adds a feed to this query.
420  
421 =item $query->combiners(@combiners)
422  
423 Sets or returns the combiners of this query.
424  
425 =item $query->combiner(@combiners)
426  
427 Adds combiners to this query.
428  
429 =back
430  
431 =head1 BUGS
432  
433 This document is incomplete.
434  
435 =head1 SEE ALSO
436  
437 L<Mail::Karmasphere::Client>
438 L<Mail::Karmasphere::Response>
439 http://www.karmasphere.com/
440  
441 =head1 COPYRIGHT
442  
443 Copyright (c) 2005 Shevek, Karmasphere. All rights reserved.
444  
445 This program is free software; you can redistribute it and/or modify
446 it under the same terms as Perl itself.
447  
448 =cut
449  
450 5 1;
« SpamAssassin/Plugin/Karmasphere.pm ↑ Summary Karmasphere/Response.pm »