| 1 | package Net::IMAP::Server::Command::Append; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base qw/Net::IMAP::Server::Command/; | ||
| 7 | |||
| 8 | use DateTime::Format::Strptime; | ||
| 9 | |||
| 10 | sub validate { | ||
| 11 | my $self = shift; | ||
| 12 | |||
| 13 | return $self->bad_command("Log in first") if $self->connection->is_unauth; | ||
| 14 | |||
| 15 | my @options = $self->parsed_options; | ||
| 16 | return $self->bad_command("Not enough options") if @options < 2; | ||
| 17 | return $self->bad_command("Too many options") if @options > 4; | ||
| 18 | |||
| 19 | my $mailbox = $self->connection->model->lookup( $options[0] ); | ||
| 20 | return $self->no_command("[TRYCREATE] Mailbox does not exist") unless $mailbox; | ||
| 21 | return $self->bad_command("Mailbox is read-only") if $mailbox->read_only; | ||
| 22 | |||
| 23 | return 1; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub run { | ||
| 27 | my $self = shift; | ||
| 28 | |||
| 29 | my @options = $self->parsed_options; | ||
| 30 | |||
| 31 | my $mailbox = $self->connection->model->lookup( shift @options ); | ||
| 32 | if (my $msg = $mailbox->append(pop @options)) { | ||
| 33 | if (@options and grep {ref $_} @options) { | ||
| 34 | my ($flags) = grep {ref $_} @options; | ||
| 35 | $msg->set_flag($_, 1) for @{$flags}; | ||
| 36 | } | ||
| 37 | if (@options and grep {not ref $_} @options) { | ||
| 38 | my ($time) = grep {not ref $_} @options; | ||
| 39 | $msg->internaldate( $time ) | ||
| 40 | or return $self->bad_command("Invalid date"); | ||
| 41 | } | ||
| 42 | |||
| 43 | $self->connection->previous_exists( $self->connection->previous_exists + 1 ) | ||
| 44 | if $self->connection->is_selected and $mailbox eq $self->connection->selected; | ||
| 45 | $self->ok_command("[APPENDUID @{[$mailbox->uidvalidity]} @{[$msg->uid]}] APPEND COMPLETED"); | ||
| 46 | } else { | ||
| 47 | $self->no_command("Permission denied"); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | 1; |