author | Myhailo Danylenko <isbear@ukrpost.net> |
Sat, 05 Mar 2016 18:36:28 +0200 | |
changeset 150 | c8514af9b449 |
parent 69 | ab6d4ee8974c |
permissions | -rw-r--r-- |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
1 |
|
58
aa3376776cf2
Cosmetic comment changes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
2 |
-- MPD INTERATION |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
3 |
|
58
aa3376776cf2
Cosmetic comment changes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
4 |
-- library |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
5 |
|
66 | 6 |
local socket = require 'socket' |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
7 |
|
66 | 8 |
-- |
58
aa3376776cf2
Cosmetic comment changes
Myhailo Danylenko <isbear@ukrpost.net>
parents:
23
diff
changeset
|
9 |
|
66 | 10 |
local O = { |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
11 |
hostname = "localhost", |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
12 |
password = nil, |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
13 |
port = 6600, |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
14 |
} |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
15 |
|
66 | 16 |
local F = { } |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
17 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
18 |
-- separator allows split output into records, that are started by any of present in separator keys |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
19 |
-- returns table of field (lowercase) - value records |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
20 |
-- command status is returned in STATUS field |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
21 |
-- on unexpected errors returns just false, dropping any available data |
66 | 22 |
function F.receive_message ( tcp, separator ) |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
23 |
local ret = {} |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
24 |
local line = tcp:receive ( '*l' ) |
23
e441162b1386
One pep handler, pep commands
Myhailo Danylenko <isbear@ukrpost.net>
parents:
21
diff
changeset
|
25 |
while line and not ( line:match ( '^OK' ) or line:match ( '^ACK' ) ) do |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
26 |
local key, val = line:match ( '^(.-):%s*(.*)$' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
27 |
if key then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
28 |
if separator then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
29 |
key = key:lower () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
30 |
if separator[key] then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
31 |
table.insert ( ret, {} ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
32 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
33 |
ret[#ret][key] = val |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
34 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
35 |
ret[key:lower()] = val |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
36 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
37 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
38 |
line = tcp:receive ( '*l' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
39 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
40 |
if not line then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
41 |
return false -- an error occured, ret, most likely, does not contains exact and complete info... |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
42 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
43 |
ret.STATUS = line:match ( '^%S+' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
44 |
return ret |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
45 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
46 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
47 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
48 |
-- use: mpd.call_command { 'status' } |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
49 |
-- mpd.call_command { 'lsinfo misc', list = { file = true, directory = true } } |
69 | 50 |
-- mpd.call_command { 'next', noret = true } -- removed, early termination with password :/ |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
51 |
-- mpd.call_command { 'status', 'currentsong' } |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
52 |
-- on one command returns just results of that command, on multi - array of results |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
53 |
-- on errors returns nil |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
54 |
-- noret can terminate socket too early, thus, do not use it with lists of commands |
66 | 55 |
function F.call_command ( opts ) |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
56 |
local tcp = socket.tcp () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
57 |
if not tcp then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
58 |
print ( 'mpd: cannot get master tcp object' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
59 |
return nil |
69 | 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 |
|
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
77 |
tcp:close () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
78 |
print ( 'mpd: cannot connect to server' ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
79 |
return nil |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
80 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
81 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
82 |
local ret = {} |
69 | 83 |
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 ) |
|
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
99 |
if not ret then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
100 |
tcp:close () |
69 | 101 |
print ( 'mpd: error getting response for password' ) |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
102 |
return nil |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
103 |
elseif ret.STATUS ~= 'OK' then |
69 | 104 |
print ( 'mpd: server refuses password' ) |
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
105 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
106 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
107 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
108 |
for num, command in ipairs ( opts ) do |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
109 |
if not tcp:send ( command .. "\n" ) then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
110 |
tcp:close () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
111 |
print ( 'mpd: error sending command ' .. command ) |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
112 |
return nil |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
113 |
end |
69 | 114 |
ret[num] = F.receive_message ( tcp, opts.list ) |
115 |
if not ret[num] then |
|
116 |
print ( 'mpd: error getting result' ) |
|
117 |
end |
|
118 |
if ret[num]['STATUS'] ~= 'OK' then |
|
119 |
print ( 'mpd: server acks our command ' .. command ) |
|
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
120 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
121 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
122 |
|
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
123 |
tcp:close () |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
124 |
if #ret > 1 then |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
125 |
return ret |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
126 |
else |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
127 |
return ret[1] |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
128 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
129 |
end |
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
130 |
|
66 | 131 |
function F.server ( host, port, password ) |
132 |
O.hostname = host |
|
133 |
O.port = port |
|
134 |
O.password = password |
|
135 |
end |
|
136 |
||
137 |
return F |
|
138 |
||
5
cba039bd6f13
Included sample configuration into package
Myhailo Danylenko <isbear@ukrpost.net>
parents:
diff
changeset
|
139 |
-- vim: se ts=4: -- |