Server/Command/Uid.pm untested

filter: Statementstmt 0.0% 0 / 101 Branchbran 0.0% 0 / 48 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 168 CC 25 SCAR 34.8
CC
25
Cov
0%
store
72.0
copy
42.0
fetch
30.0
CRAP 32.6
1 package Net::IMAP::Server::Command::Uid;
2  
3 use warnings;
4 use strict;
5  
6 use base qw/Net::IMAP::Server::Command/;
7 use Net::IMAP::Server::Command::Search;
8  
9 use Coro;
10  
11 sub validate {
12 my $self = shift;
13  
14 return $self->bad_command("Login first") if $self->connection->is_unauth;
15 return $self->bad_command("Select a mailbox first")
16 unless $self->connection->is_selected;
17  
18 my @options = $self->parsed_options;
19 return $self->bad_command("Not enough options") if @options < 1;
20  
21 return 1;
22 }
23  
24 sub run {
25 my $self = shift;
26  
27 my ($subcommand, @rest) = $self->parsed_options;
28 $subcommand = lc $subcommand;
29 if ($subcommand =~ /^(copy|fetch|store|search|expunge)$/i ) {
30 $self->$subcommand(@rest);
31 } else {
32 $self->log(
33 $subcommand . " wasn't understood by the 'UID' command" );
34 $self->bad_command(
35 q{Unrecognized UID command $subcommand} );
36 }
37  
38 }
39  
40 sub fetch {
41 my $self = shift;
42  
43 return $self->bad_command("Not enough options") if @_ < 2;
44 return $self->bad_command("Too many options") if @_ > 2;
45  
46 my ( $messages, $spec ) = @_;
47 $spec = [$spec] unless ref $spec;
48 unshift @{$spec}, "UID" unless grep {uc $_ eq "UID"} @{$spec};
49 my @messages = $self->connection->selected->get_uids($messages);
50 for my $m (@messages) {
51 $self->untagged_response( $self->connection->sequence($m)
52 . " FETCH "
53 . $self->data_out( [ $m->fetch($spec) ] ) );
54 cede;
55 }
56  
57 $self->ok_completed();
58 }
59  
60 sub store {
61 my $self = shift;
62  
63 return $self->bad_command("Mailbox is read-only") if $self->connection->selected->read_only;
64  
65 return $self->bad_command("Not enough options") if @_ < 3;
66 return $self->bad_command("Too many options") if @_ > 3;
67  
68 my ( $messages, $what, $flags ) = @_;
69 $flags = ref $flags ? $flags : [$flags];
70  
71 return $self->bad_command("Invalid flag $_") for grep {not $self->connection->selected->can_set_flag($_)} @{$flags};
72  
73 my @messages = $self->connection->selected->get_uids($messages);
74 $self->connection->ignore_flags(1) if $what =~ /\.SILENT$/i;
75 for my $m (@messages) {
76 $m->store( $what => $flags );
77 $self->connection->_unsent_fetch->{$self->connection->sequence($m)}{UID}++
78 unless $what =~ /\.SILENT$/i;
79 cede;
80 }
81 $self->connection->ignore_flags(0) if $what =~ /\.SILENT$/i;
82  
83 $self->ok_completed;
84 }
85  
86 sub copy {
87 my $self = shift;
88
89 return $self->bad_command("Not enough options") if @_ < 2;
90 return $self->bad_command("Too many options") if @_ > 2;
91  
92 my ( $messages, $name ) = @_;
93 my $mailbox = $self->connection->model->lookup( $name );
94 return $self->no_command("[TRYCREATE] Mailbox does not exist") unless $mailbox;
95 return $self->bad_command("Mailbox is read-only") if $mailbox->read_only;
96  
97 my @messages = $self->connection->selected->get_uids($messages);
98 return $self->no_command("Permission denied") if grep {not $_->copy_allowed($mailbox)} @messages;
99  
100 my @new;
101 for my $m (@messages) {
102 push @new, $m->copy($mailbox);
103 cede;
104 }
105 my $sequence = join(",",map {$_->uid} @messages);
106 my $uids = join(",",map {$_->uid} @new);
107 $self->ok_command("[COPYUID @{[$mailbox->uidvalidity]} $sequence $uids] COPY COMPLETED");
108 }
109  
110 sub expunge {
111 my $self = shift;
112  
113 return $self->bad_command("Not enough options") if @_ < 1;
114 return $self->bad_command("Too many options") if @_ > 2;
115  
116 return $self->bad_command("Mailbox is read-only") if $self->connection->selected->read_only;
117  
118 my ( $messages ) = @_;
119 my @messages = $self->connection->selected->get_uids($messages);
120 $self->connection->selected->expunge([map {$_->sequence} @messages]);
121  
122 $self->ok_completed;
123 }
124  
125 sub search {
126 my $self = shift;
127  
128 my $filter = Net::IMAP::Server::Command::Search::filter($self, @_);
129 return unless $filter;
130  
131 my @results = map {$_->uid} grep {$filter->($_)} $self->connection->get_messages('1:*');
132 $self->untagged_response(join(" ", SEARCH => @results));
133  
134 $self->ok_completed;
135 }
136  
137 1;