| 1 | package Net::IMAP::Server::Command::Copy; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base qw/Net::IMAP::Server::Command/; | ||
| 7 | |||
| 8 | use Coro; | ||
| 9 | |||
| 10 | sub validate { | ||
| 11 | my $self = shift; | ||
| 12 | |||
| 13 | return $self->bad_command("Log in first") if $self->connection->is_unauth; | ||
| 14 | return $self->bad_command("Select a mailbox first") | ||
| 15 | unless $self->connection->is_selected; | ||
| 16 | |||
| 17 | my @options = $self->parsed_options; | ||
| 18 | return $self->bad_command("Not enough options") if @options < 2; | ||
| 19 | return $self->bad_command("Too many options") if @options > 2; | ||
| 20 | |||
| 21 | return $self->bad_command("Invalid message set") | ||
| 22 | unless $options[0] =~ $self->connection->SEQUENCE_STRING; | ||
| 23 | |||
| 24 | my $mailbox = $self->connection->model->lookup( $options[1] ); | ||
| 25 | return $self->no_command("[TRYCREATE] Mailbox does not exist") unless $mailbox; | ||
| 26 | return $self->bad_command("Mailbox is read-only") if $mailbox->read_only; | ||
| 27 | |||
| 28 | return 1; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub run { | ||
| 32 | my $self = shift; | ||
| 33 | |||
| 34 | my ( $messages, $name ) = $self->parsed_options; | ||
| 35 | my @messages = $self->connection->get_messages($messages); | ||
| 36 | |||
| 37 | my $mailbox = $self->connection->model->lookup( $name ); | ||
| 38 | |||
| 39 | return $self->no_command("Permission denied") if grep {not $_->copy_allowed($mailbox)} @messages; | ||
| 40 | |||
| 41 | my @new; | ||
| 42 | for my $m (@messages) { | ||
| 43 | push @new, $m->copy($mailbox); | ||
| 44 | cede; | ||
| 45 | } | ||
| 46 | my $sequence = join(",",map {$_->uid} @messages); | ||
| 47 | my $uids = join(",",map {$_->uid} @new); | ||
| 48 | $self->ok_command("[COPYUID @{[$mailbox->uidvalidity]} $sequence $uids] COPY COMPLETED"); | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; |