Class: MPDTestServer

Inherits:
GServer
  • Object
show all
Defined in:
lib/mpdserver.rb

Instance Method Summary collapse

Constructor Details

#initialize(port, db_file = nil, *args) ⇒ MPDTestServer

Returns a new instance of MPDTestServer.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/mpdserver.rb', line 57

def initialize( port, db_file = nil, *args )
  super port, *args

  if db_file.nil?
    db_file = __FILE__.gsub(/\/[^\/]*$/, '') + '/../data/database.yaml'
  end

  @status = {
    :volume => 0,
    :repeat => 0,
    :random => 0,
    :playlist => 1,
    :state => 'stop',
    :xfade => 0
  }
  @elapsed_time = 0
  @current_song = nil
  @database = YAML::load( File.open( db_file ) )
  @songs = @database[0]
  @playlists = @database[1]
  @artists = []
  @albums = []
  @titles = []
  @the_playlist = []
  @playback_thread = nil
  @filetree = {:name =>'', :dirs =>[], :songs =>[]}
  @songs.each_with_index do |song,i|
    song['id'] = i
    if !song['artist'].nil? and !@artists.include? song['artist']
      @artists << song['artist']
    end
    if !song['album'].nil? and !@albums.include? song['album']
      @albums << song['album']
    end
    if !song['title'].nil?
      @titles << song['title']
    end
    if !song['file'].nil?
      dirs = song['file'].split '/'
      dirs.pop
      the_dir = @filetree
      dirs.each do |d|
        found = nil
        the_dir[:dirs].each do |sub|
          if sub[:name] == d
            found = sub
            break
          end
        end
        if found.nil?
          found = {:name => d, :dirs =>[], :songs =>[]}
          the_dir[:dirs] << found
        end
        the_dir = found
      end # End dirs.each
      the_dir[:songs] << song
    end # End if !song['file'].nil?
  end # End @songs.each

  sort_dir @filetree
  @artists.sort!
  @albums.sort!
  @titles.sort!
end

Instance Method Details

#add_dir_to_pls(dir) ⇒ Object



1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
# File 'lib/mpdserver.rb', line 1182

def add_dir_to_pls( dir )
  dir[:songs].each do |song|
    song['_mod_ver'] = @status[:playlist]
    incr_version
    @the_playlist << song
  end

  dir[:dirs].each do |d|
    add_dir_to_pls d
  end
end

#args_check(sock, cmd, argv, argc) ⇒ Object



1119
1120
1121
1122
1123
1124
1125
1126
# File 'lib/mpdserver.rb', line 1119

def args_check( sock, cmd, argv, argc )
  if (argc.kind_of? Range and argc.include?(argv.length)) or
      (argv.length == argc)
    yield argv
  else
    sock.puts "ACK [2@0] {#{cmd}} wrong number of arguments for \"#{cmd}\""
  end
end

#build_args(line) ⇒ Object



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
# File 'lib/mpdserver.rb', line 1087

def build_args( line )
  ret = []
  word = ''
  escaped = false
  in_quote = false

  line.strip!

  line.each_byte do |c|
    c = c.chr
    if c == ' ' and !in_quote
      ret << word unless word.empty?
      word = ''
    elsif c == '"' and !escaped
      if in_quote
        in_quote = false
      else
        in_quote = true
      end
      ret << word unless word.empty?
      word = ''
    else
      escaped = (c == '\\')
      word += c
    end
  end

  ret << word unless word.empty?

  return ret
end

#cmd_fail(sock, msg) ⇒ Object



1082
1083
1084
1085
# File 'lib/mpdserver.rb', line 1082

def cmd_fail( sock, msg )
  sock.puts msg
  return false
end

#do_cmd(sock, cmd, args) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
# File 'lib/mpdserver.rb', line 224

def do_cmd( sock, cmd, args )
  case cmd
    when 'add'
      if args.length == 0
        # Add the entire database
        @songs.each do |s|
          s['_mod_ver'] = @status[:playlist]
          incr_version
          @the_playlist << s
        end
        return true
      else
        # Add a single entry
        the_song = nil
        @songs.each do |s|
          if s['file'] == args[0]
            the_song = s
            break
          end
        end

        if the_song.nil?
          dir = locate_dir(args[0])
          if not dir.nil?
            # Add the dir
            add_dir_to_pls dir
            return true
          else
            return(cmd_fail(sock,'ACK [50@0] {add} directory or file not found'))
          end
        else
          the_song['_mod_ver'] = @status[:playlist]
          incr_version
          @the_playlist << the_song
          return true
        end
      end
    when 'clear'
      args_check( sock, cmd, args, 0 ) do
        incr_version
        @the_playlist = []
        @current_song = nil
        return true
      end
    when 'clearerror'
      args_check( sock, cmd, args, 0 ) do
        the_error = nil
        return true
      end
    when 'close'
      sock.close
      return true
    when 'crossfade'
      args_check( sock, cmd, args, 1 ) do |args|
        if is_int(args[0]) and args[0].to_i >= 0
          @status[:xfade] = args[0].to_i
          return true
        else
          return(cmd_fail(sock,"ACK [2@0] {crossfade} \"#{args[0]}\" is not a integer >= 0"))
        end
      end
    when 'currentsong'
      args_check( sock, cmd, args, 0 ) do
        if @current_song != nil and @current_song < @the_playlist.length
          send_song sock, @the_playlist[@current_song]
        end
        return true
      end
    when 'delete'
      args_check( sock, cmd, args, 1 ) do |args|
        if is_int args[0]
          if args[0].to_i < 0 or args[0].to_i >= @the_playlist.length
            return(cmd_fail(sock,"ACK [50@0] {delete} song doesn't exist: \"#{args[0]}\""))
          else
            @the_playlist.delete_at args[0].to_i
            args[0].to_i.upto @the_playlist.length - 1 do |i|
              @the_playlist[i]['_mod_ver'] = @status[:playlist]
            end
            incr_version
            return true
          end
        else
          return(cmd_fail('ACK [2@0] {delete} need a positive integer'))
        end
      end
    when 'deleteid'
      args_check( sock, cmd, args, 1 ) do |args|
        if is_int args[0]
          the_song = nil
          @the_playlist.each do |song|
            if song['id'] == args[0].to_i
              the_song = song
              break
            end
          end

          if not the_song.nil?
            index = @the_playlist.index the_song
            @the_playlist.delete the_song
            index.upto @the_playlist.length - 1 do |i|
              @the_playlist[i]['_mod_ver'] = @status[:playlist]
            end
            incr_version
            return true
          else
            return(cmd_fail(sock,"ACK [50@0] {deleteid} song id doesn't exist: \"#{args[0]}\""))
          end
        else
          return(cmd_fail(sock,'ACK [2@0] {deleteid} need a positive integer'))
        end
      end
    when 'find'
      args_check( sock, cmd, args, 2 ) do |args|
        if args[0] != 'album' and args[0] != 'artist' and args[0] != 'title'
          return(cmd_fail(sock,'ACK [2@0] {find} incorrect arguments'))
        else
          if args[0] == 'album'
            @songs.each do |song|
              if song['album'] == args[1]
                send_song sock, song
              end
            end
          elsif args[0] == 'artist'
            @songs.each do |song|
              if song['artist'] == args[1]
                send_song sock, song
              end
            end
          elsif args[0] == 'title'
            @songs.each do |song|
              if song['title'] == args[1]
                send_song sock, song
              end
            end
          end
          return true
        end
      end
    when 'kill'
      args_check( sock, cmd, args, 0 ) do
        sock.close
        return true
      end
    when 'list'
      args_check( sock, cmd, args, 1..2 ) do |args|
        if args[0] != 'album' and args[0] != 'artist' and args[0] != 'title'
          return(cmd_fail(sock,"ACK [2@0] {list} \"#{args[0]}\" is not known"))
        elsif args[0] == 'artist' and args.length > 1
          return(cmd_fail(sock,'ACK [2@0] {list} should be "Album" for 3 arguments'))
        else
          if args[0] == 'artist'
            # List all Artists
            @artists.each do |artist|
              sock.puts "Artist: #{artist}"
            end
            return true
          elsif args[0] == 'title'
            # List all Titles
            @titles.each do |title|
              sock.puts "Title: #{title}"
            end
            return true
          else
            if args.length == 2
              # List all Albums by Artist
              # artist == args[1]
              listed = []
              @songs.each do |song|
                if song['artist'] == args[1]
                  if not song['album'].nil? and !listed.include? song['album']
                    sock.puts "Album: #{song['album']}"
                    listed << song['album']
                  end
                end
              end
              return true
            else
              # List all Albums
              @albums.each do |album|
                sock.puts "Album: #{album}"
              end
              return true
            end
          end
        end
      end
    when 'listall'
      args_check( sock, cmd, args, 0..1 ) do |args|
        if args.length == 0
          @filetree[:dirs].each do |d|
            send_dir sock, d, false
          end
        else
          was_song = false
          @songs.each do |song|
            if song['file'] == args[0]
              sock.puts "file: #{song['file']}"
              was_song = true
              break
            end
          end

          if was_song
            return true
          end

          dir = locate_dir args[0]
          if not dir.nil?
            parents = args[0].split '/'
            parents.pop
            parents = parents.join '/'
            parents += '/' unless parents.length == 0
            send_dir sock, dir, false, parents
          else
            return(cmd_fail(sock,'ACK [50@0] {listall} directory or file not found'))
          end
        end
        return true
      end
    when 'listallinfo'
      args_check( sock, cmd, args, 0..1 ) do |args|
        if args.length == 0
          @filetree[:dirs].each do |d|
            send_dir sock, d, true
          end
        else
          was_song = false
          @songs.each do |song|
            if song['file'] == args[0]
              send_song song
              was_song = true
              break
            end
          end

          if was_song
            return true
          end

          dir = locate_dir args[0]
          if not dir.nil?
            parents = args[0].split '/'
            parents.pop
            parents = parents.join '/'
            parents += '/' unless parents.length == 0
            send_dir sock, dir, true, parents
          else
            return(cmd_fail(sock,'ACK [50@0] {listallinfo} directory or file not found'))
          end
        end
        return true
      end
    when 'load'
      args_check( sock, cmd, args, 1 ) do
        # incr_version for each song loaded
        pls = args[0] + '.m3u'
        the_pls = nil
        @playlists.each do |p|
          if p['file'] == pls
            the_pls = p
            break
          end
        end

        unless the_pls.nil?
          the_pls['songs'].each do |song|
            song['_mod_ver'] = @status[:playlist]
            @the_playlist << song
            incr_version
          end
        else
          return(cmd_fail(sock,"ACK [50@0] {load} playlist \"#{args[0]}\" not found"))
        end
      end
    when 'lsinfo'
      args_check( sock, cmd, args, 0..1 ) do
        if args.length == 0
          @filetree[:dirs].each do |d|
            sock.puts "directory: #{d[:name]}"
            d[:songs].each do |s|
              send_song sock, s
            end
          end
          @playlists.each do |pls|
            sock.puts "playlist: #{pls['file'].gsub( /\.m3u$/, '' )}"
          end
        else
          dir = locate_dir args[0]
          if dir.nil?
            return(cmd_fail(sock,"ACK [50@0] {lsinfo} directory not found"))
          else
            dir[:dirs].each do |d|
              sock.puts "directory: #{args[0] + '/' + d[:name]}"
            end
            dir[:songs].each do |s|
              send_song sock, s
            end
          end
        end
        return true
      end
    when 'move'
      args_check( sock, cmd, args, 2 ) do |args|
        if !is_int args[0]
          return(cmd_fail(sock,"ACK [2@0] {move} \"#{args[0]}\" is not a integer"))
        elsif !is_int args[1]
          return(cmd_fail(sock,"ACK [2@0] {move} \"#{args[1]}\" is not a integer"))
        elsif args[0].to_i < 0 or args[0].to_i >= @the_playlist.length
          return(cmd_fail(sock,"ACK [50@0] {move} song doesn't exist: \"#{args[0]}\""))
        elsif args[1].to_i < 0 or args[1].to_i >= @the_playlist.length
          return(cmd_fail(sock,"ACK [50@0] {move} song doesn't exist: \"#{args[1]}\""))
        else
          tmp = @the_playlist.delete_at args[0].to_i
          @the_playlist.insert args[1].to_i, tmp
          if args[0].to_i < args[1].to_i
            args[0].to_i.upto args[1].to_i do |i|
              @the_playlist[i]['_mod_ver'] = @status[:playlist]
            end
          else
            args[1].to_i.upto args[0].to_i do |i|
              @the_playlist[i]['_mod_ver'] = @status[:playlist]
            end
          end
          incr_version
          return true
        end
      end
    when 'moveid'
      args_check( sock, cmd, args, 2 ) do |args|
        if !is_int args[0]
          return(cmd_fail(sock,"ACK [2@0] {moveid} \"#{args[0]}\" is not a integer"))
        elsif !is_int args[1]
          return(cmd_fail(sock,"ACK [2@0] {moveid} \"#{args[1]}\" is not a integer"))
        elsif args[1].to_i < 0 or args[1].to_i >= @the_playlist.length
          return(cmd_fail(sock,"ACK [50@0] {moveid} song doesn't exist: \"#{args[1]}\""))
        else
          # Note: negative args should be checked
          the_song = nil
          index = -1
          @the_playlist.each_with_index do |song,i|
            if song['id'] == args[0].to_i
              the_song = song
              index = i
            end
          end
          if the_song.nil?
            return(cmd_fail(sock,"ACK [50@0] {moveid} song id doesn't exist: \"#{args[0]}\""))
          end
          tmp = @the_playlist.delete_at index
          @the_playlist.insert args[1].to_i, tmp
          if index < args[1].to_i
            index.upto args[1].to_i do |i|
              @the_playlist[i]['_mod_ver'] = @status[:playlist]
            end
          else
            args[1].to_i.upto index do |i|
              @the_playlist[i]['_mod_ver'] = @status[:playlist]
            end
          end
          incr_version
          return true
        end
      end
    when 'next'
      args_check( sock, cmd, args, 0 ) do
        if @status[:state] != 'stop'
          next_song
          @elapsed_time = 0
          @status[:state] = 'play'
        end
        return true
      end
    when 'pause'
      args_check( sock, cmd, args, 0..1 ) do |args|
        if args.length > 0 and not is_bool args[0]
          return(cmd_fail(sock,"ACK [2@0] {pause} \"#{args[0]}\" is not 0 or 1"))
        end
        
        if @status[:state] != 'stop'
          if args.length == 1
            @status[:state] = ( args[0] == '1' ? 'pause' : 'play' )
          else
            @status[:state] = ( @status[:state] == 'pause' ? 'play' : 'pause' )
          end
        end

        return true
      end
    when 'password'
      args_check( sock, cmd, args, 1 ) do |args|
      return true if args[0] == 'test'
      return(cmd_fail(sock,"ACK [3@0] {password} incorrect password"))
      end
    when 'ping'
      args_check( sock, cmd, args, 0 ) do
        return true
      end
    when 'play'
      args_check( sock, cmd, args, 0..1 ) do |args|
        if args.length > 0 and !is_int(args[0])
          return(cmd_fail(sock,'ACK [2@0] {play} need a positive integer'))
        else
          args.clear if args[0] == '-1'
          if args.length == 0
            if @the_playlist.length > 0 and @status[:state] != 'play'
              @current_song = 0 if @current_song.nil?
              @elapsed_time = 0
              @status[:state] = 'play'
            end
          else
            if args[0].to_i < 0 or args[0].to_i >= @the_playlist.length
              return(cmd_fail(sock,"ACK [50@0] {play} song doesn't exist: \"#{args[0]}\""))
            end

            @current_song = args[0].to_i
            @elapsed_time = 0
            @status[:state] = 'play'
          end
          return true
        end
      end
    when 'playid'
      args_check( sock, cmd, args, 0..1 ) do |args|
        if args.length > 0 and !is_int(args[0])
          return(cmd_fail(sock,'ACK [2@0] {playid} need a positive integer'))
        else
          args.clear if args[0] == '-1'
          if args.length == 0
            if @the_playlist.length > 0 and @status[:state] != 'play'
              @current_song = 0 if @current_song.nil?
              @elapsed_time = 0
              @status[:state] = 'play'
            end
          else
            index = nil
            @the_playlist.each_with_index do |s,i|
              if s['id'] == args[0].to_i
                index = i
                break;
              end
            end

            return(cmd_fail(sock,"ACK [50@0] {playid} song id doesn't exist: \"#{args[0]}\"")) if index.nil?

            @current_song = index
            @elapsed_time = 0
            @status[:state] = 'play'
          end
          return true
        end
      end
    when 'playlist'
      log 'MPD Warning: Call to Deprecated API: "playlist"' if audit
      args_check( sock, cmd, args, 0 ) do
        @the_playlist.each_with_index do |v,i|
          sock.puts "#{i}:#{v['file']}"
        end
        return true
      end
    when 'playlistinfo'
      args_check( sock, cmd, args, 0..1 ) do |args|
        if args.length > 0 and !is_int(args[0])
          return(cmd_fail(sock,'ACK [2@0] {playlistinfo} need a positive integer'))
        else
          args.clear if args.length > 0 and args[0].to_i < 0
          if args.length != 0
            if args[0].to_i >= @the_playlist.length
              return(cmd_fail(sock,"ACK [50@0] {playlistinfo} song doesn't exist: \"#{args[0]}\""))
            else
              song = @the_playlist[args[0].to_i]
              send_song sock, song
              sock.puts "Pos: #{args[0].to_i}"
              return true
            end
          else
            @the_playlist.each_with_index do |song,i|
              send_song sock, song
              sock.puts "Pos: #{i}"
            end
            return true
          end
        end
      end
    when 'playlistid'
      args_check( sock, cmd, args, 0..1 ) do |args|
        if args.length > 0 and !is_int(args[0])
          return(cmd_fail(sock,'ACK [2@0] {playlistid} need a positive integer'))
        else
          song = nil
          pos = nil
          args.clear if args[0].to_i < 0
          if args.length != 0
            @the_playlist.each_with_index do |s,i|
              if s['id'] == args[0].to_i
                song = s
                pos = i
                break;
              end
            end

            return(cmd_fail(sock,"ACK [50@0] {playlistid} song id doesn't exist: \"#{args[0]}\"")) if song.nil?

            send_song sock, song
            sock.puts "Pos: #{pos}"
            return true
          else
            @the_playlist.each_with_index do |song,i|
              send_song sock, song
              sock.puts "Pos: #{i}"
            end
            return true
          end
        end
      end
    when 'plchanges'
      args_check( sock, cmd, args, 1 ) do |args|
        if args.length > 0 and !is_int(args[0])
          return(cmd_fail(sock,'ACK [2@0] {plchanges} need a positive integer'))
        else
          # Note: args[0] < 0 just return OK...
          @the_playlist.each_with_index do |song,i|
            if args[0].to_i > @status[:playlist] or song['_mod_ver'] >= args[0].to_i or song['_mod_ver'] == 0
              send_song sock, song
              sock.puts "Pos: #{i}"
            end
          end
          return true
        end
      end
    when 'plchangesposid'
      args_check( sock, cmd, args, 1 ) do |args|
        if args.length > 0 and !is_int(args[0])
          return(cmd_fail(sock,'ACK [2@0] {plchangesposid} need a positive integer'))
        else
          # Note: args[0] < 0 just return OK...
          @the_playlist.each_with_index do |song,i|
            if args[0].to_i > @status[:playlist] or song['_mod_ver'] >= args[0].to_i or song['_mod_ver'] == 0
              sock.puts "cpos: #{i}"
              sock.puts "Id: #{song['id']}"
            end
          end
          return true
        end
      end
    when 'previous'
      args_check( sock, cmd, args, 0 ) do
        return true if @status[:state] == 'stop'
        prev_song
        @elapsed_time = 0
        @status[:state] = 'play'
        return true
      end
    when 'random'
      args_check( sock, cmd, args, 1 ) do |args|
        if is_bool args[0]
          @status[:random] = args[0].to_i
          return true
        elsif is_int args[0]
          return(cmd_fail(sock,"ACK [2@0] {random} \"#{args[0]}\" is not 0 or 1"))
        else
          return(cmd_fail(sock,'ACK [2@0] {random} need an integer'))
        end
      end
    when 'repeat'
      args_check( sock, cmd, args, 1 ) do |args|
        if is_bool args[0]
          @status[:repeat] = args[0].to_i
          return true
        elsif is_int args[0]
          return(cmd_fail(sock,"ACK [2@0] {repeat} \"#{args[0]}\" is not 0 or 1"))
        else
          return(cmd_fail(sock,'ACK [2@0] {repeat} need an integer'))
        end
      end
    when 'rm'
      args_check( sock, cmd, args, 1 ) do |args|
        rm_pls = args[0] + '.m3u'
        the_pls = -1
        @playlists.each_with_index do |pls,i|
          the_pls = i if pls['file'] == rm_pls
        end

        if the_pls != -1
          @playlists.delete_at the_pls
          return true
        else
          return(cmd_fail(sock,"ACK [50@0] {rm} playlist \"#{args[0]}\" not found"))
        end
      end
    when 'save'
      args_check( sock, cmd, args, 1 ) do |args|
        new_playlist = {'file' => args[0]+'.m3u', 'songs' => @the_playlist}
        @playlists << new_playlist
        return true
      end
    when 'search'
      args_check( sock, cmd, args, 2 ) do |args|
        if args[0] != 'title' and args[0] != 'artist' and args[0] != 'album' and args[0] != 'filename'
          return(cmd_fail(sock,'ACK [2@0] {search} incorrect arguments'))
        end
        args[0] = 'file' if args[0] == 'filename'
        @songs.each do |song|
          data = song[args[0]]
          if not data.nil? and data.downcase.include? args[1]
            send_song sock, song
          end
        end
        return true
      end
    when 'seek'
      args_check( sock, cmd, args, 2 ) do |args|
        if !is_int args[0]
          return(cmd_fail(sock,"ACK [2@0] {seek} \"#{args[0]}\" is not a integer"))
        elsif !is_int args[1]
          return(cmd_fail(sock,"ACK [2@0] {seek} \"#{args[1]}\" is not a integer"))
        else
          if args[0].to_i > @the_playlist.length or args[0].to_i < 0
            return(cmd_fail(sock,"ACK [50@0] {seek} song doesn't exist: \"#{args[0]}\""))
          end
          args[1] = '0' if args[1].to_i < 0
          song = @the_playlist[args[0].to_i]
          if args[1].to_i >= song['time'].to_i
            if args[0].to_i + 1 < @the_playlist.length
              @current_song = args[0].to_i + 1
              @elapsed_time = 0
              @status[:state] = 'play' unless @status[:state] == 'pause'
            else
              @current_song = nil
              @elapsed_time = 0
              @status[:state] = 'stop'
            end
          else
            @current_song = args[0].to_i
            @elapsed_time = args[1].to_i
            @status[:state] = 'play' unless @status[:state] == 'pause'
          end
          return true
        end
      end
    when 'seekid'
      args_check( sock, cmd, args, 2 ) do |args|
        if !is_int args[0]
          return(cmd_fail(sock,"ACK [2@0] {seekid} \"#{args[0]}\" is not a integer"))
        elsif !is_int args[1]
          return(cmd_fail(sock,"ACK [2@0] {seekid} \"#{args[1]}\" is not a integer"))
        else
          pos = nil
          song = nil
          @the_playlist.each_with_index do |s,i|
            if s['id'] == args[0].to_i
              song = s
              pos = i
              break;
            end
          end

          if song.nil?
            return(cmd_fail(sock,"ACK [50@0] {seekid} song id doesn't exist: \"#{args[0]}\""))
          end

          args[1] = '0' if args[1].to_i < 0
          if args[1].to_i >= song['time'].to_i
            if pos + 1 < @the_playlist.length
              @current_song = pos + 1
              @elapsed_time = 0
              @status[:state] = 'play' unless @status[:state] == 'pause'
            else
              @current_song = nil
              @elapsed_time = 0
              @status[:state] = 'stop'
            end
          else
            @current_song = pos
            @elapsed_time = args[1].to_i
            @status[:state] = 'play' unless @status[:state] == 'pause'
          end
          return true
        end
      end
    when 'setvol'
      args_check( sock, cmd, args, 1 ) do |args|
        if !is_int args[0]
          return(cmd_fail(sock,'ACK [2@0] {setvol} need an integer'))
        else
          # Note: args[0] < 0 actually sets the vol val to < 0
          @status[:volume] = args[0].to_i
          return true
        end
      end
    when 'shuffle'
      args_check( sock, cmd, args, 0 ) do
        @the_playlist.each do |s|
          s['_mod_ver'] = @status[:playlist]
        end
        incr_version
        @the_playlist.reverse!
        return true
      end
    when 'stats'
      args_check( sock, cmd, args, 0 ) do
      # artists
      sock.puts "artists: #{@artists.size}"
      # albums
      sock.puts "albums: #{@albums.size}"
      # songs
      sock.puts "songs: #{@songs.size}"
      # uptime
      sock.puts "uptime: 500"
      # db_playtime
      time = 0
      @songs.each do |s|
        time += s['time'].to_i
      end
      sock.puts "db_playtime: #{time}"
      # db_update
      sock.puts "db_update: 1159418502"
      # playtime
      sock.puts "playtime: 10"
      return true
      end
    when 'status'
      args_check( sock, cmd, args, 0 ) do
        @status.each_pair do |key,val|
          sock.puts "#{key}: #{val}" unless val.nil?
        end
        sock.puts "playlistlength: #{@the_playlist.length}"

        if @current_song != nil and @the_playlist.length > @current_song
          sock.puts "song: #{@current_song}"
          sock.puts "songid: #{@the_playlist[@current_song]['id']}"
        end

        @status[:updating_db] = nil
        return true
      end
    when 'stop'
      args_check( sock, cmd, args, 0 ) do
        @status[:state] = 'stop'
        @status[:time] = nil
        @status[:bitrate] = nil
        @status[:audio] = nil
        return true
      end
    when 'swap'
      args_check( sock, cmd, args, 2 ) do |args|
        if !is_int args[0]
          return(cmd_fail(sock,"ACK [2@0] {swap} \"#{args[0]}\" is not a integer"))
        elsif !is_int args[1]
          return(cmd_fail(sock,"ACK [2@0] {swap} \"#{args[1]}\" is not a integer"))
        elsif args[0].to_i >= @the_playlist.length or args[0].to_i < 0
          return(cmd_fail(sock,"ACK [50@0] {swap} song doesn't exist: \"#{args[0]}\""))
        elsif args[1].to_i >= @the_playlist.length or args[1].to_i < 0
          return(cmd_fail(sock,"ACK [50@0] {swap} song doesn't exist: \"#{args[1]}\""))
        else
          tmp = @the_playlist[args[1].to_i]
          @the_playlist[args[1].to_i] = @the_playlist[args[0].to_i]
          @the_playlist[args[0].to_i] = tmp
          @the_playlist[args[0].to_i]['_mod_ver'] = @status[:playlist]
          @the_playlist[args[1].to_i]['_mod_ver'] = @status[:playlist]
          incr_version
          return true
        end
      end
    when 'swapid'
      args_check( sock, cmd, args, 2 ) do |args|
        if !is_int args[0]
          return(cmd_fail(sock,"ACK [2@0] {swapid} \"#{args[0]}\" is not a integer"))
        elsif !is_int args[1]
          return(cmd_fail(sock,"ACK [2@0] {swapid} \"#{args[1]}\" is not a integer"))
        else
          from = nil
          to = nil
          @the_playlist.each_with_index do |song,i|
            if song['id'] == args[0].to_i
              from = i
            elsif song['id'] == args[1].to_i
              to = i
            end
          end
          if from.nil?
            return(cmd_fail(sock,"ACK [50@0] {swapid} song id doesn't exist: \"#{args[0]}\""))
          elsif to.nil?
            return(cmd_fail(sock,"ACK [50@0] {swapid} song id doesn't exist: \"#{args[1]}\""))
          end
          tmp = @the_playlist[to]
          @the_playlist[to] = @the_playlist[from]
          @the_playlist[from] = tmp
          @the_playlist[to]['_mod_ver'] = @status[:playlist]
          @the_playlist[from]['_mod_ver'] = @status[:playlist]

          incr_version
          return true
        end
      end
    when 'update'
      args_check( sock, cmd, args, 0..1 ) do |args|
        incr_version
        sock.puts 'updating_db: 1'
        @status[:updating_db] = '1'
        return true
      end
    when 'volume'
      log 'MPD Warning: Call to Deprecated API: "volume"' if audit
      args_check( sock, cmd, args, 1 ) do |args|
        if !is_int args[0]
          return(cmd_fail(sock,'ACK [2@0] {volume} need an integer'))
        else
          # Note: args[0] < 0 subtract from the volume
          @status[:volume] += args[0].to_i
          return true
        end
      end
    else
      return(cmd_fail(sock,"ACK [5@0] {} unknown command #{cmd}"))
  end # End Case cmd
end

#elapsed_timeObject



1067
1068
1069
# File 'lib/mpdserver.rb', line 1067

def elapsed_time
  @elapsed_time
end

#elapsed_time=(new_time) ⇒ Object



1063
1064
1065
# File 'lib/mpdserver.rb', line 1063

def elapsed_time=( new_time )
  @elapsed_time = new_time
end

#get_current_songObject



1041
1042
1043
1044
1045
1046
1047
# File 'lib/mpdserver.rb', line 1041

def get_current_song
  if @current_song != nil and @current_song < @the_playlist.length
    return @the_playlist[@current_song]
  else
    return nil
  end
end

#incr_versionObject



1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
# File 'lib/mpdserver.rb', line 1071

def incr_version
  if @status[:playlist] == 2147483647
    @status[:playlist] = 1
    @the_playlist.each do |song|
      song['_mod_ver'] = 0
    end
  else
    @status[:playlist] += 1
  end
end

#is_bool(val) ⇒ Object



1132
1133
1134
# File 'lib/mpdserver.rb', line 1132

def is_bool( val )
  val == '0' or val == '1'
end

#is_int(val) ⇒ Object



1128
1129
1130
# File 'lib/mpdserver.rb', line 1128

def is_int( val )
  val =~ /^[-+]?[0-9]*$/
end

#locate_dir(path) ⇒ Object



1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
# File 'lib/mpdserver.rb', line 1136

def locate_dir( path )
  dirs = path.split '/'

  the_dir = @filetree
  dirs.each do |d|
    found = nil
    the_dir[:dirs].each do |sub|
      if sub[:name] == d
        found = sub
        break
      end
    end
    if found.nil?
      return nil
    else
      the_dir = found
    end
  end

  return the_dir
end

#next_songObject



1058
1059
1060
1061
# File 'lib/mpdserver.rb', line 1058

def next_song
  return if @current_song.nil?
  @current_song = (@current_song +1 < @the_playlist.length ? @current_song +1 : nil)
end

#prev_songObject



1049
1050
1051
1052
1053
1054
1055
1056
# File 'lib/mpdserver.rb', line 1049

def prev_song
  return if @current_song.nil?
  if @current_song == 0
    @elapsed_time = 0
  else
    @current_song -= 1
  end
end

#send_dir(sock, dir, allinfo, path = '') ⇒ Object



1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
# File 'lib/mpdserver.rb', line 1166

def send_dir( sock, dir, allinfo, path = '' )
  sock.puts "directory: #{path}#{dir[:name]}"

  dir[:songs].each do |song|
    if allinfo
      send_song sock, song
    else
      sock.puts "file: #{song['file']}"
    end
  end

  dir[:dirs].each do |d|
    send_dir(sock, d, allinfo, dir[:name] + '/')
  end
end

#send_song(sock, song) ⇒ Object



1158
1159
1160
1161
1162
1163
1164
# File 'lib/mpdserver.rb', line 1158

def send_song( sock, song )
  return if song.nil?
  sock.puts "file: #{song['file']}"
  song.each_pair do |key,val|
    sock.puts "#{key.capitalize}: #{val}" unless key == 'file' or key == '_mod_ver'
  end
end

#serve(sock) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/mpdserver.rb', line 166

def serve( sock )
  command_list = []
  in_cmd_list = false
  in_ok_list = false
  the_error = nil
  sock.puts 'OK MPD 0.11.5'
  begin
    while line = sock.gets

      args = build_args line

      cmd = args.shift

      if cmd == 'command_list_begin' and args.length == 0 and !in_cmd_list
        in_cmd_list = true
        log 'MPD: Starting Command List' if audit
      elsif cmd == 'command_list_ok_begin' and args.length == 0 and !in_cmd_list
        in_cmd_list = true
        in_ok_list = true
        log 'MPD: Starting Command OK List' if audit
      elsif cmd == 'command_list_end' and in_cmd_list
        log 'MPD: Running Command List' if audit

        the_ret = true
        command_list.each_with_index do |set,i|
          the_ret = do_cmd sock, set[0], set[1]

          if audit
            log "MPD Command List: CMD ##{i}: \"#{set[0]}(#{set[1].join(', ')})\": " + (the_ret ? 'successful' : 'failed')
          end
          
          break unless the_ret

          sock.puts 'list_OK' if in_ok_list
          
        end

        sock.puts 'OK' if the_ret

        command_list.clear
        in_cmd_list = false
        in_ok_list = false
      else
        if in_cmd_list
          command_list << [cmd, args]
        else
          ret = do_cmd sock, cmd, args
          sock.puts 'OK' if ret
          if audit
            log "MPD Command \"#{cmd}(#{args.join(', ')})\": " + (ret ? 'successful' : 'failed')
          end # End if audit
        end # End if in_cmd_list
      end # End if cmd == 'comand_list_begin' ...
    end # End while line = sock.gets
  rescue
  end
end

#sort_dir(dir) ⇒ Object



1194
1195
1196
1197
1198
1199
1200
1201
1202
# File 'lib/mpdserver.rb', line 1194

def sort_dir( dir )
  dir[:dirs].sort! do |x,y|
    x[:name] <=> y[:name]
  end

  dir[:dirs].each do |d|
    sort_dir d
  end
end

#startObject



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/mpdserver.rb', line 122

def start
  super

  @playback_thread = Thread.new(@status, self) do |status, server|
    while not server.stopped?
      if status[:state] == 'play'
        song = server.get_current_song
        if song.nil?
          server.elapsed_time = 0
          status[:state] = 'stop'
          next
        end

        status[:time] = "#{server.elapsed_time}:#{song['time']}"
        status[:bitrate] = 192
        status[:audio] = '44100:16:2'

        if server.elapsed_time >= song['time'].to_i
          server.elapsed_time = 0
          server.next_song
        end

        server.elapsed_time = server.elapsed_time + 1
      elsif status[:state] == 'pause'
        song = server.get_current_song
        if song.nil?
          server.elapsed_time = 0
          status[:state] = 'stop'
          next
        end
        status[:time] = "#{server.elapsed_time}:#{song['time']}"
        status[:bitrate] = 192
        status[:audio] = '44100:16:2'
      else
        status[:time] = nil
        status[:bitrate] = nil
        status[:audio] = nil
        server.elapsed_time = 0
      end
      sleep 1
    end
  end
end