| 1 | package App::Pimpd::Validate; | ||
| 2 | use strict; | ||
| 3 | no warnings 'experimental::smartmatch'; | ||
| 4 | |||
| 5 | BEGIN { | ||
| 6 | use Exporter; | ||
| 7 | use vars qw(@ISA @EXPORT); | ||
| 8 | @ISA = qw(Exporter); | ||
| 9 | @EXPORT = qw( | ||
| 10 | invalid_regex | ||
| 11 | to_terminal | ||
| 12 | empty_playlist | ||
| 13 | invalid_playlist_pos | ||
| 14 | remote_host | ||
| 15 | escape | ||
| 16 | get_valid_lists | ||
| 17 | isa_valid_playlist | ||
| 18 | ); | ||
| 19 | } | ||
| 20 | |||
| 21 | use Carp 'croak'; | ||
| 22 | use App::Pimpd; | ||
| 23 | use Term::ExtendedColor qw(fg); | ||
| 24 | |||
| 25 | |||
| 26 | sub get_valid_lists { | ||
| 27 | my @lists = @_; | ||
| 28 | my @valid_lists = sort($mpd->collection->all_playlists); | ||
| 29 | |||
| 30 | |||
| 31 | for my $list(@lists) { | ||
| 32 | if($list ~~ @valid_lists) { | ||
| 33 | next; | ||
| 34 | } | ||
| 35 | else { | ||
| 36 | my @choices = (); | ||
| 37 | |||
| 38 | for my $valid(@valid_lists) { | ||
| 39 | if(not invalid_regex($list)) { | ||
| 40 | if($valid =~ /$list/im) { | ||
| 41 | push(@choices, $valid); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | if($valid =~ /\Q$list/im) { | ||
| 46 | push(@choices, $valid); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | if(scalar(@choices) == 0) { | ||
| 51 | print STDERR "No such playlist '" . fg($c[5], $list), "'\n"; | ||
| 52 | return; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | print "'all' uses all playlists\n\n"; | ||
| 57 | |||
| 58 | my $i = 0; | ||
| 59 | for my $choice(@choices) { | ||
| 60 | print fg('bold', sprintf("%3d", $i)), " $choice\n"; | ||
| 61 | $i++; | ||
| 62 | } | ||
| 63 | print "choice: "; | ||
| 64 | chomp(my $answer = <STDIN>); | ||
| 65 | |||
| 66 | if( ($answer eq 'all') or ($answer eq '') ) { | ||
| 67 | return @choices; | ||
| 68 | } | ||
| 69 | elsif($answer eq 'current') { | ||
| 70 | return(undef); | ||
| 71 | } | ||
| 72 | |||
| 73 | if($answer ~~ @valid_lists) { | ||
| 74 | $list = $answer; | ||
| 75 | } | ||
| 76 | # Make sure the number selected is in fact valid | ||
| 77 | elsif($answer >= 0 and $answer < scalar(@valid_lists)) { | ||
| 78 | $list = $choices[$answer]; | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | croak("Playlist '$answer' is not valid\n"); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | } | ||
| 85 | return(@lists); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub isa_valid_playlist { | ||
| 89 | my @playlists = @_; | ||
| 90 | my @lists = $mpd->collection->all_playlists; | ||
| 91 | map { s/^\s+//m } @lists; | ||
| 92 | return ($_[0] ~~ @lists) ? 1 : 0; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub escape { | ||
| 96 | my $str = shift; | ||
| 97 | $str =~ s/([;<>*|`&\$!#()[\]{}:'" ])/\\$1/g; | ||
| 98 | |||
| 99 | return $str; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub remote_host { | ||
| 103 | not exists($config{mpd_host}) and $config{mpd_host} = 'localhost'; | ||
| 104 | if(($config{mpd_host} eq 'localhost') or ($config{mpd_host} eq '127.0.0.1')) { | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | sub invalid_regex { | ||
| 111 | my $re = shift; | ||
| 112 | eval { qr/$re/m }; | ||
| 113 | if($@) { | ||
| 114 | return 1; | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | return 0; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | sub to_terminal { | ||
| 123 | return (-t STDOUT) ? 1 : 0; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | sub empty_playlist { | ||
| 128 | if(scalar($mpd->playlist->as_items) == 0) { | ||
| 129 | return 1; | ||
| 130 | } | ||
| 131 | return 0; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub invalid_playlist_pos { | ||
| 135 | my @pos = @_; | ||
| 136 | my @playlist = map { $_ = $_->pos } $mpd->playlist->as_items; | ||
| 137 | my $fail = 0; | ||
| 138 | |||
| 139 | for(@pos) { | ||
| 140 | if($_ ~~ @playlist) { | ||
| 141 | # all good | ||
| 142 | } | ||
| 143 | else { | ||
| 144 | $fail++; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | return($fail); | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | 1; | ||
| 152 | |||
| 153 | __END__ |