47 end |
45 end |
48 end |
46 end |
49 |
47 |
50 -- use: mpd.call_command { 'status' } |
48 -- use: mpd.call_command { 'status' } |
51 -- mpd.call_command { 'lsinfo misc', list = { file = true, directory = true } } |
49 -- mpd.call_command { 'lsinfo misc', list = { file = true, directory = true } } |
52 -- mpd.call_command { 'next', noret = true } |
50 -- mpd.call_command { 'next', noret = true } -- removed, early termination with password :/ |
53 -- mpd.call_command { 'status', 'currentsong' } |
51 -- mpd.call_command { 'status', 'currentsong' } |
54 -- on one command returns just results of that command, on multi - array of results |
52 -- on one command returns just results of that command, on multi - array of results |
55 -- on errors returns nil |
53 -- on errors returns nil |
56 -- noret can terminate socket too early, thus, do not use it with lists of commands |
54 -- noret can terminate socket too early, thus, do not use it with lists of commands |
57 function F.call_command ( opts ) |
55 function F.call_command ( opts ) |
58 local tcp = socket.tcp () |
56 local tcp = socket.tcp () |
59 if not tcp then |
57 if not tcp then |
60 print ( 'mpd: cannot get master tcp object' ) |
58 print ( 'mpd: cannot get master tcp object' ) |
61 return nil |
59 return nil |
62 elseif not tcp:connect ( O.hostname, O.port ) then |
60 end |
|
61 |
|
62 if not O.hostname then |
|
63 local server = os.getenv ( 'MPD_HOST' ) |
|
64 if server then |
|
65 local password, host = server:match ( '(.+)@(.-)' ) |
|
66 if password then |
|
67 F.server ( host, os.getenv ( 'MPD_PORT' ) or 6600, password ) |
|
68 else |
|
69 F.server ( server, os.getenv ( 'MPD_PORT' ) or 6600, nil ) |
|
70 end |
|
71 else |
|
72 F.server ( "localhost", 6600, nil ) |
|
73 end |
|
74 end |
|
75 |
|
76 if not tcp:connect ( O.hostname, O.port ) then |
63 tcp:close () |
77 tcp:close () |
64 print ( 'mpd: cannot connect to server' ) |
78 print ( 'mpd: cannot connect to server' ) |
65 return nil |
79 return nil |
66 end |
80 end |
67 |
81 |
68 local ret = {} |
82 local ret = {} |
69 if not opts.noret then |
83 ret = F.receive_message ( tcp ) |
70 ret = F.receive_message ( tcp ) |
84 if not ret then |
|
85 tcp:close () |
|
86 print ( 'mpd: error getting greeting from server' ) |
|
87 return nil |
|
88 elseif ret.STATUS ~= 'OK' then |
|
89 print ( 'mpd: server ack\'s in greeting' ) |
|
90 end |
|
91 |
|
92 if O.password then |
|
93 if not tcp:send ( "password " .. O.password .. "\n" ) then |
|
94 tcp:close () |
|
95 print ( 'mpd: error sending password' ) |
|
96 return nil |
|
97 end |
|
98 ret = mpd.receive_message ( tcp ) |
71 if not ret then |
99 if not ret then |
72 tcp:close () |
100 tcp:close () |
73 print ( 'mpd: error getting greeting from server' ) |
101 print ( 'mpd: error getting response for password' ) |
74 return nil |
102 return nil |
75 elseif ret.STATUS ~= 'OK' then |
103 elseif ret.STATUS ~= 'OK' then |
76 print ( 'mpd: server ack\'s in greeting' ) |
104 print ( 'mpd: server refuses password' ) |
77 end |
105 end |
78 end |
106 end |
79 |
107 |
80 for num, command in ipairs ( opts ) do |
108 for num, command in ipairs ( opts ) do |
81 if not tcp:send ( command .. "\n" ) then |
109 if not tcp:send ( command .. "\n" ) then |
82 tcp:close () |
110 tcp:close () |
83 print ( 'mpd: error sending command ' .. command ) |
111 print ( 'mpd: error sending command ' .. command ) |
84 return nil |
112 return nil |
85 end |
113 end |
86 if not opts.noret then |
114 ret[num] = F.receive_message ( tcp, opts.list ) |
87 ret[num] = F.receive_message ( tcp, opts.list ) |
115 if not ret[num] then |
88 if not ret[num] then |
116 print ( 'mpd: error getting result' ) |
89 print ( 'mpd: error getting result' ) |
117 end |
90 end |
118 if ret[num]['STATUS'] ~= 'OK' then |
91 if ret[num]['STATUS'] ~= 'OK' then |
119 print ( 'mpd: server acks our command ' .. command ) |
92 print ( 'mpd: server acks our command ' .. command ) |
|
93 end |
|
94 end |
120 end |
95 end |
121 end |
96 |
122 |
97 tcp:close () |
123 tcp:close () |
98 if #ret > 1 then |
124 if #ret > 1 then |