| 1 | # This code is part of Perl distribution Mail-Box version 4.02. | ||||||||||||||||||||||||||||
| 2 | # The POD got stripped from this file by OODoc version 3.06. | ||||||||||||||||||||||||||||
| 3 | # For contributors see file ChangeLog. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # This software is copyright (c) 2001-2026 by Mark Overmeer. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 9 | # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | package Mail::Box::Search::Grep;{ | ||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = '4.02'; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 2 | ▶ | use parent 'Mail::Box::Search'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use Log::Report 'mail-box', import => [ qw/__x error info/ ]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | #-------------------- | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub init($) | ||||||||||||||||||||||||||||
| 26 | 12 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=16 SCAR=36.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 12 | ▶ | $args->{in} ||= ($args->{field} ? 'HEAD' : 'BODY'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args->{"in"} ||= $args->{"field"} ? "HEAD" : "BODY"
MC/DC0%0/2
$args->{"in"} ||= $args->{"field"} ? "HEAD" : "BODY"
$args->{"in"}
$args->{"field"} ? "HEAD" : "BODY"
Truth table33%1/3
A ||= B
A= $args->{"in"}B= $args->{"field"} ? "HEAD" : "BODY"
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 12 | ▶ | my $deliver = $args->{deliver} || $args->{details}; # details is old name | ||||||||||||||||||||||||||
|
Condition66%2/3
$args->{'deliver'} || $args->{'details'}
MC/DC50%1/2
$args->{'deliver'} || $args->{'details'}
$args->{'deliver'}
$args->{'details'}
Truth table66%2/3
A || B
A= $args->{'deliver'}B= $args->{'details'}
| |||||||||||||||||||||||||||||
| 31 | $args->{deliver} | ||||||||||||||||||||||||||||
| 32 | = !defined $deliver ? undef | ||||||||||||||||||||||||||||
| 33 | : ref $deliver eq 'CODE' ? $deliver | ||||||||||||||||||||||||||||
| 34 | 14 | ▶ | : $deliver eq 'PRINT' ? sub { $_[0]->printMatch($_[1]) } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 35 | 504 | ▶ | : ref $deliver eq 'ARRAY' ? sub { push @$deliver, $_[1] } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 12 | ▶ | : $deliver; | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 12 | $self->SUPER::init($args); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 12 | my $take = $args->{field}; | |||||||||||||||||||||||||||
| 41 | $self->{MBSG_field_check} | ||||||||||||||||||||||||||||
| 42 | 3865 | ▶ | = !defined $take ? sub {1} | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 0 | ▶ | : !ref $take ? do {$take = lc $take; sub { $_[1] eq $take }} | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | : ref $take eq 'Regexp' ? sub { $_[1] =~ $take } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 45 | 12 | ▶ | : ref $take eq 'CODE' ? $take | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 46 | : error __x"unsupported field selector {take UNKNOWN}.", take => $take; | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | my $match = $args->{match} | ||||||||||||||||||||||||||||
| 49 | 12 | ▶ | or error __x"grep requires a match pattern."; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | $self->{MBSG_match_check} | ||||||||||||||||||||||||||||
| 52 | 2251 | ▶ | = !ref $match ? sub { index("$_[1]", $match) >= $[ } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 14939 | ▶ | : ref $match eq 'Regexp' ? sub { "$_[1]" =~ $match } | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 54 | 12 | ▶ | : ref $match eq 'CODE' ? $match | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 55 | : error __x"unsupported match pattern {match UNKNOWN}.", match => $match; | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 12 | $self; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub search(@) | ||||||||||||||||||||||||||||
| 61 | 14 | ▶ | { my ($self, $object, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
search: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 14 | delete $self->{MBSG_last_printed}; | |||||||||||||||||||||||||||
| 63 | 14 | $self->SUPER::search($object, %args); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | sub inHead(@) | ||||||||||||||||||||||||||||
| 67 | 231 | ▶ | { my ($self, $part, $head, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
inHead: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 231 | my @details = (message => $part->toplevel, part => $part); | |||||||||||||||||||||||||||
| 70 | 231 | my ($field_check, $match_check, $deliver) = @$self{ qw/MBSG_field_check MBSG_match_check MBS_deliver/ }; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 231 | my $matched = 0; | |||||||||||||||||||||||||||
| 73 | LINES: | ||||||||||||||||||||||||||||
| 74 | 231 | foreach my $field ($head->orderedFields) | |||||||||||||||||||||||||||
| 75 | 3865 | ▶ | { $field_check->($head, $field->name) && $match_check->($head, $field) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
&$field_check($head, $field->name) and &$match_check($head, $field)
MC/DC50%1/2
&$field_check($head, $field->name) and &$match_check($head, $field)
&$field_check($head, $field->name)
&$match_check($head, $field)
Truth table66%2/3
A and B
A= &$field_check($head, $field->name)B= &$match_check($head, $field)
| |||||||||||||||||||||||||||||
| 76 | 122 | $matched++; | |||||||||||||||||||||||||||
| 77 | 122 | ▶ | $deliver or last LINES; # no deliver: only one match needed | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 122 | $deliver->( {@details, field => $field} ); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 231 | $matched; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub inBody(@) | ||||||||||||||||||||||||||||
| 85 | 326 | ▶ | { my ($self, $part, $body, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
inBody: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 326 | my @details = (message => $part->toplevel, part => $part); | |||||||||||||||||||||||||||
| 88 | 326 | my ($field_check, $match_check, $deliver) = @$self{ qw/MBSG_field_check MBSG_match_check MBS_deliver/ }; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 326 | my $matched = 0; | |||||||||||||||||||||||||||
| 91 | 326 | my $linenr = 0; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | LINES: | ||||||||||||||||||||||||||||
| 94 | 326 | foreach my $line ($body->lines) | |||||||||||||||||||||||||||
| 95 | 13325 | { $linenr++; | |||||||||||||||||||||||||||
| 96 | 13325 | ▶ | $match_check->($body, $line) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 397 | $matched++; | |||||||||||||||||||||||||||
| 99 | 397 | ▶ | $deliver or last LINES; # no deliver: only one match needed | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 396 | $deliver->( +{ @details, linenr => $linenr, line => $line } ); | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 326 | $matched; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | #-------------------- | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub printMatch($;$) | ||||||||||||||||||||||||||||
| 109 | 14 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
printMatch: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 14 | ▶ | my ($out, $match) = @_==2 ? @_ : (select, shift); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | $match->{field} | ||||||||||||||||||||||||||||
| 113 | 14 | ▶ | ? $self->printMatchedHead($out, $match) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | : $self->printMatchedBody($out, $match) | ||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | sub printMatchedHead($$) | ||||||||||||||||||||||||||||
| 119 | 5 | ▶ | { my ($self, $out, $match) = @_; | ||||||||||||||||||||||||||
|
Subroutine
printMatchedHead: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 120 | 5 | my $message = $match->{message}; | |||||||||||||||||||||||||||
| 121 | 5 | my $msgnr = $message->seqnr; | |||||||||||||||||||||||||||
| 122 | 5 | my $folder = $message->folder->name; | |||||||||||||||||||||||||||
| 123 | 5 | ▶ | my $lp = $self->{MBSG_last_printed} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'MBSG_last_printed'} || ''
MC/DC100%1/1
$self->{'MBSG_last_printed'} || ''
$self->{'MBSG_last_printed'}
Truth table100%2/2
A
A= $self->{'MBSG_last_printed'}
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 5 | ▶ | unless($lp eq "$folder $msgnr") # match in new message | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 4 | { my $subject = $message->subject; | |||||||||||||||||||||||||||
| 127 | 4 | $out->print("$folder, message $msgnr: $subject\n"); | |||||||||||||||||||||||||||
| 128 | 4 | $self->{MBSG_last_printed} = "$folder $msgnr"; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 5 | my @lines = $match->{field}->string; | |||||||||||||||||||||||||||
| 132 | 5 | ▶ | my $inpart = $match->{part}->isPart ? 'p ' : ' '; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | 5 | $out->print($inpart, join $inpart, @lines); | |||||||||||||||||||||||||||
| 134 | 5 | $self; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub printMatchedBody($$) | ||||||||||||||||||||||||||||
| 139 | 9 | ▶ | { my ($self, $out, $match) = @_; | ||||||||||||||||||||||||||
|
Subroutine
printMatchedBody: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 9 | my $message = $match->{message}; | |||||||||||||||||||||||||||
| 141 | 9 | my $msgnr = $message->seqnr; | |||||||||||||||||||||||||||
| 142 | 9 | my $folder = $message->folder->name; | |||||||||||||||||||||||||||
| 143 | 9 | ▶ | my $lp = $self->{MBSG_last_printed} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$self->{'MBSG_last_printed'} || ''
MC/DC100%1/1
$self->{'MBSG_last_printed'} || ''
$self->{'MBSG_last_printed'}
Truth table100%2/2
A
A= $self->{'MBSG_last_printed'}
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 9 | ▶ | unless($lp eq "$folder $msgnr") # match in new message | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 9 | { my $subject = $message->subject; | |||||||||||||||||||||||||||
| 147 | 9 | $out->print("$folder, message $msgnr: $subject\n"); | |||||||||||||||||||||||||||
| 148 | 9 | $self->{MBSG_last_printed} = "$folder $msgnr"; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 9 | ▶ | my $inpart = $match->{part}->isPart ? 'p ' : ' '; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | 9 | $out->print(sprintf "$inpart %2d: %s", $match->{linenr}, $match->{line}); | |||||||||||||||||||||||||||
| 153 | 9 | $self; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 2 | 1; | |||||||||||||||||||||||||||