39131aaa7dcbda9280af8e55bc1039aa015631da
[spider.git] / cmd / directory.pl
1 #
2 # show the contents of the message directory
3 #
4 # Copyright (c) Dirk Koopman G1TLH
5 #
6 # $Id$
7 #
8
9 my ($self, $line) = @_;
10 my @f = split /\s+/, $line;
11 my @ref;
12 my $ref;
13 my @out;
14 my $f;
15 my $n = 0;
16 my @all = grep {!$_->private || !($self->priv < 5 && $_->to ne $self->call && $_->from ne $self->call)} (DXMsg::get_all());
17 return (1, $self->msg('e3', 'directory', $line)) unless @all;
18 my $sel = 0;
19 my $from = 0;
20 my $to = $all[@all-1]->msgno;
21
22 while (@f) {
23         $f = uc shift @f;
24         if ($f eq 'ALL') {
25                 @ref = @all;
26                 $n = @ref;
27                 $sel++;
28         } elsif ($f =~ /^O/o) {         # dir/own
29                 @ref = grep { $_->to eq $self->call || $_->from eq $self->call } @all;
30                 $sel++;
31         } elsif ($f =~ /^N/o) {         # dir/new
32                 @ref = grep { $_->t > $self->user->lastin } @all;
33                 $sel++;
34         } elsif ($f =~ /^S/o) {     # dir/subject
35                 $f = shift @f;
36                 if ($f) {
37                         $f =~ s{(.)}{"\Q$1"}ge;
38                         @ref = grep { $_->subject =~ m{$f}i } @all;
39                         $sel++;
40                 }
41         } elsif ($f eq '>' || $f =~ /^T/o){  
42                 $f = uc shift @f;
43                 if ($f) {
44                         $f = shellregex($f);
45                         @ref = grep { $_->to =~ m{$f} } @all;
46                         $sel++;
47                 }
48         } elsif ($f eq '<' || $f =~ /^F/o){
49                 $f = uc shift @f;
50                 if ($f) {
51                         $f = shellregex($f);
52                         @ref = grep { $_->from =~ m{$f} } @all;
53                         $sel++;
54                 }
55         } elsif ($f =~ /^(\d+)-(\d+)$/) {               # a range of items
56                 $from = $1;
57                 $to = $2;
58         } elsif ($f =~ /^\d+$/ && $f > 0) {             # a number of items
59                 $n = $f;
60         }
61 }
62
63 $n = 10 unless $n;
64 @ref = @all unless $sel || @ref;
65
66 if (@ref) {
67         if ($from != 0 || $to != $all[@all-1]->msgno) {
68                 @ref = grep {$_->msgno >= $from && $_->msgno <= $to} @ref;
69         }
70         my $i = @ref - $n;
71         $i = 0 unless $i > 0;
72         my $count;
73         while ($i < @ref) {
74                 $ref = $ref[$i++];
75                 push @out, $ref->dir;
76                 last if ++$count >= $n;
77         }
78 } else {
79         push @out, $self->msg('e3', 'directory', $line); 
80 }
81 return (1, @out);