| 1 | package Net::IMAP::Server::Command::List; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | |||
| 6 | use base qw/Net::IMAP::Server::Command/; | ||
| 7 | |||
| 8 | use Encode; | ||
| 9 | use Encode::IMAPUTF7; | ||
| 10 | |||
| 11 | sub validate { | ||
| 12 | my $self = shift; | ||
| 13 | |||
| 14 | return $self->bad_command("Log in first") if $self->connection->is_unauth; | ||
| 15 | |||
| 16 | my @options = $self->parsed_options; | ||
| 17 | return $self->bad_command("Not enough options") if @options < 2; | ||
| 18 | return $self->bad_command("Too many options") if @options > 2; | ||
| 19 | |||
| 20 | return 1; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub run { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | my ( $root, $search ) = $self->parsed_options; | ||
| 27 | |||
| 28 | # In the special case of a query for the delimiter, give them our delimiter | ||
| 29 | if ( $search eq "" ) { | ||
| 30 | my $sep = (defined $self->connection->model->root->separator) | ||
| 31 | ? q{"}.$self->connection->model->root->separator.q{"} : "NIL"; | ||
| 32 | $self->tagged_response( qq|(\\Noselect) $sep ""| ); | ||
| 33 | } else { | ||
| 34 | my $sep = $self->connection->model->root->separator; | ||
| 35 | $search = quotemeta($search); | ||
| 36 | $search =~ s/\\\*/.*/g; | ||
| 37 | if (defined $sep) { | ||
| 38 | $search =~ s/\\%/[^$sep]*/g; | ||
| 39 | } else { | ||
| 40 | $search =~ s/\\%/.*/g; | ||
| 41 | } | ||
| 42 | my $regex = qr{^\Q$root\E$search$}; | ||
| 43 | $self->connection->model->root->update_tree; | ||
| 44 | $self->traverse( $self->connection->model->root, $regex ); | ||
| 45 | } | ||
| 46 | |||
| 47 | $self->ok_completed; | ||
| 48 | } | ||
| 49 | |||
| 50 | sub list_out { | ||
| 51 | my $self = shift; | ||
| 52 | my $node = shift; | ||
| 53 | my @props = @_; | ||
| 54 | |||
| 55 | my $str = $self->data_out([map {\$_} @props]) . " " | ||
| 56 | . $self->data_out($self->connection->model->root->separator) . " " | ||
| 57 | . $self->data_out(Encode::encode('IMAP-UTF-7',$node->full_path)); | ||
| 58 | $self->tagged_response($str); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub traverse { | ||
| 62 | my $self = shift; | ||
| 63 | my $node = shift; | ||
| 64 | my $regex = shift; | ||
| 65 | |||
| 66 | my @props; | ||
| 67 | push @props, @{$node->children} ? '\HasChildren' : '\HasNoChildren'; | ||
| 68 | push @props, '\Noinferiors' unless defined $self->connection->model->root->separator; | ||
| 69 | push @props, '\Noselect' unless $node->is_selectable; | ||
| 70 | |||
| 71 | $self->list_out($node, @props) if $node->parent and | ||
| 72 | Encode::encode('IMAP-UTF-7',$node->full_path) =~ $regex; | ||
| 73 | $self->traverse( $_, $regex ) for @{ $node->children }; | ||
| 74 | } | ||
| 75 | |||
| 76 | 1; |