IMAP/Fetch.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« IMAP/Client.pm ↑ Summary IMAP/FetchResponseParser.pm »
1 package Protocol::IMAP::Fetch;
2 {
3 $Protocol::IMAP::Fetch::VERSION = '0.004';
4 }
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use parent qw(Mixin::Event::Dispatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 use Try::Tiny;
10 use Future;
11 use Protocol::IMAP::FetchResponseParser;
12 use Protocol::IMAP::Envelope;
13 use List::Util qw(min);
14  
15 sub new {
16 my $class = shift;
17 my $self = bless {
18 parse_buffer => '',
19 @_
20 }, $class;
21 $self->{parser} = Protocol::IMAP::FetchResponseParser->new;
22 $self->{parser}->subscribe_to_event(
23 literal_data => sub {
24 my ($ev, $count) = @_;
25 # warn "Have pos=". $self->parser->pos . " count $count len " . length($self->{parse_buffer}) . "\n";
26 eval {
27 my $starter = substr $self->{parse_buffer}, $self->parser->pos, min($count, length($self->{parse_buffer}) - $self->parser->pos), '';
28 $self->{literal} = $starter;
29 $self->{remaining} = $count - length($starter);
30 1
31 } or do { $self->{literal} = ''; $self->{remaining} = $count };
32 $self->{reading_literal} = 1;
33 $self->{parse_buffer} =~ s/\Q{$count}\E$/""/;
34 }
35 );
36 $self
37 }
38  
39 sub parser { shift->{parser} }
40 sub parse_buffer { shift->{parse_buffer} }
41 sub completion { shift->{completion} ||= Future->new }
42  
43 sub on_read {
44 my $self = shift;
45 my $buffref = shift;
46 # warn "reading with " . $$buffref . "\n";
47 READ:
48 while(1) {
49 if($self->{reading_literal}) {
50 # warn "We are reading a literal, remaining " . $self->{remaining};
51 my $chunk = substr $$buffref, 0, min($self->{remaining}, length($$buffref)), '';
52 $self->{literal} .= $chunk;
53 $self->{remaining} -= length $chunk;
54 return 1 if $self->{remaining};
55 # warn "Completed read, had " . length($self->{literal}) . " bytes\n";
56 delete $self->{reading_literal};
57 next READ;
58 }
59 if($$buffref =~ s/^([^\r\n]*)[\r\n]*//) {
60 # warn "[$1]\n";
61 $self->{parse_buffer} .= $1;
62 die "bad chars found..." if $self->parse_buffer =~ /[\r\n]/;
63 # warn "Reading data, buffer is now:\n" . $self->parse_buffer;
64 return 1 unless $self->attempt_parse;
65 $$buffref = $self->{parse_buffer} . $$buffref;
66 return 0;
67 }
68  
69 # warn "no handler, buffer is currently " . $$buffref;
70 # $self->{parse_buffer} .= substr $$buffref, 0, length($$buffref);
71 return 1;
72 }
73 }
74  
75 sub on_done { my $self = shift; $self->completion->on_done(@_) }
76  
77 #sub literal_string {
78 # my $self = shift;
79 # my $str = shift;
80 # my $count = length($str);
81 # warn "Had $count in literal string";
82 # $self->{parse_buffer} =~ s/\Q{$count}\E$/""/;
83 # $self->attempt_parse;
84 #}
85  
86 sub attempt_parse {
87 my $self = shift;
88 my $parser = $self->parser;
89 try {
90 # warn "$self Will try to parse: [" . $self->parse_buffer . "]\n";
91 my $rslt = $parser->from_string($self->parse_buffer);
92 # warn "... and we're done\n";
93 $self->{fetched} = $rslt;
94 $self->{data}{size} = Future->new->done($rslt->{'rfc822.size'});
95 $self->{parse_buffer} = '';
96 $self->completion->done($self);
97 1
98 } catch {
99 if(/^Expected end of input/) {
100 # warn "Had end-of-input warning, this is good\n";
101 substr $self->{parse_buffer}, 0, $parser->pos - 1, '';
102 return 1;
103 }
104 # warn "Failure from parser: $_\n";
105 0
106 };
107 }
108  
109 =head2 data
110  
111 Returns a L<Future> which will resolve when the given
112 item is available. Suitable for smaller data strucures
113 such as the envelope. Not recommended for the full
114 body of a message, unless you really want to load the
115 entire message data into memory.
116  
117 =cut
118  
119 sub data {
120 my $self = shift;
121 my $k = shift;
122 return $self->{data}{$k} if exists $self->{data}{$k};
123 $self->{data}{$k} = my $f = Future->new;
124 $self->completion->on_done(sub {
125 $f->done(Protocol::IMAP::Envelope->new(
126 %{$self->{fetched}{$k}}
127 ));
128 });
129 $f
130 }
131  
132 =head2 stream
133  
134 This is what you would normally use for a message, although
135 at the moment you can't, so don't.
136  
137 =cut
138  
139 sub stream { die 'unimplemented' }
140  
141 1;
142  
143 __END__
« IMAP/Client.pm ↑ Summary IMAP/FetchResponseParser.pm »