Debian Codegraph @

obvious/lib/mpd/init.lua

raw symbols SCIP
1 --
2 -- _ _
3 -- | |_ _ __ _ _ __ ___ _ __ __| |
4 -- | | | | |/ _` |_____| '_ ` _ \| '_ \ / _` |
5 -- | | |_| | (_| |_____| | | | | | |_) | (_| |
6 -- |_|\__,_|\__,_| |_| |_| |_| .__/ \__,_|
7 -- |_|
8 --
9 -- Small interface to MusicPD
10 -- use luasocket, with a persistant connection to the MPD server.
11 --
12 -- based on a netcat version from Steve Jothen <sjothen at gmail dot com>
13 -- (see http://modeemi.fi/~tuomov/repos/ion-scripts-3/scripts/mpd.lua)
14 --
15 --
16 -- Copyright (c) 2008-2009, Alexandre Perrin <[email protected]>
17 -- All rights reserved.
18 --
19 -- Redistribution and use in source and binary forms, with or without
20 -- modification, are permitted provided that the following conditions
21 -- are met:
22 --
23 -- 1. Redistributions of source code must retain the above copyright
24 -- notice, this list of conditions and the following disclaimer.
25 -- 2. Redistributions in binary form must reproduce the above copyright
26 -- notice, this list of conditions and the following disclaimer in the
27 -- documentation and/or other materials provided with the distribution.
28 -- 4. Neither the name of the author nor the names of its contributors
29 -- may be used to endorse or promote products derived from this software
30 -- without specific prior written permission.
31 --
32 -- THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
33 -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 -- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 -- ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 -- FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 -- DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 -- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 -- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 -- LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 -- OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 -- SUCH DAMAGE.
43
44
45 -- Grab env
46 local have_socket, socket = pcall(function() return require("socket") end)
47 local string = string
48 local tonumber = tonumber
49 local setmetatable = setmetatable
50 local os = os
51
52 -- Music Player Daemon Lua library.
53
54 local MPD = {}
55
56 local MPD_mt = { __index = MPD }
57
58 -- create and return a new mpd client.
59 -- the settings argument is a table with theses keys:
60 -- hostname: the MPD's host (default localhost)
61 -- port: MPD's port to connect to (default 6600)
62 -- desc: server's description (default hostname)
63 -- password: the server's password (default nil, no password)
64 -- timeout: time in sec to wait for connect() and receive() (default 1)
65 -- retry: time in sec to wait before reconnect if error (default 60)
66 local function new(settings)
67 local client = {}
68 if settings == nil then settings = {} end
69
70 client.hostname = settings.hostname or "localhost"
71 client.port = settings.port or 6600
72 client.desc = settings.desc or client.hostname
73 client.password = settings.password
74 client.timeout = settings.timeout or 1
75 client.retry = settings.retry or 60
76
77 setmetatable(client, MPD_mt)
78
79 return client
80 end
81
82
83 -- calls the action and returns the server's response.
84 -- Example: if the server's response to "status" action is:
85 -- volume: 20
86 -- repeat: 0
87 -- random: 0
88 -- playlist: 599
89 -- ...
90 -- then the returned table is:
91 -- { volume = 20, repeat = 0, random = 0, playlist = 599, ... }
92 --
93 -- if an error arise (bad password, connection failed etc.), a table with only
94 -- the errormsg field is returned.
95 -- Example: if there is no server running on host/port, then the returned
96 -- table is:
97 -- { errormsg = "could not connect" }
98 --
99 function MPD:send(action)
100 local command = string.format("%s\n", action)
101 local values = {}
102
103 if not have_socket then
104 return { errormsg = "could not require(\"socket\")" }
105 end
106
107 -- connect to MPD server if not already done.
108 if not self.connected then
109 local now = os.time();
110 if not self.last_try or (now - self.last_try) > self.retry then
111 self.socket = socket.tcp()
112 self.socket:settimeout(self.timeout, 't')
113 self.last_try = os.time()
114 self.connected = self.socket:connect(self.hostname, self.port)
115 if not self.connected then
116 return { errormsg = "could not connect" }
117 end
118
119 -- Read the server's hello message
120 local line = self.socket:receive("*l")
121 if not line:match("^OK MPD") then -- Invalid hello message?
122 self.connected = false
123 return { errormsg = string.format("invalid hello message: %s", line) }
124 end
125
126 -- send the password if needed
127 if self.password then
128 local rsp = self:send(string.format("password %s", self.password))
129 if rsp.errormsg then
130 return rsp
131 end
132 end
133 else
134 local retry_sec = self.retry - (now - self.last_try)
135 return { errormsg = string.format("retrying connection in %d sec", retry_sec) }
136 end
137 end
138
139 self.socket:send(command)
140
141 local line = ""
142 while not line:match("^OK$") do
143 line = self.socket:receive("*l")
144 if not line then -- closed,timeout (mpd killed?)
145 self.connected = false
146 return self:send(action)
147 end
148
149 if line:match("^ACK") then
150 return { errormsg = line }
151 end
152
153 local _, _, key, value = string.find(line, "([^:]+):%s(.+)")
154 if key then
155 values[string.lower(key)] = value
156 end
157 end
158
159 return values
160 end
161
162 function MPD:next()
163 return self:send("next")
164 end
165
166 function MPD:previous()
167 return self:send("previous")
168 end
169
170 function MPD:stop()
171 return self:send("stop")
172 end
173
174 -- no need to check the new value, mpd will set the volume in [0,100]
175 function MPD:volume_up(delta)
176 local stats = self:send("status")
177 local new_volume = tonumber(stats.volume) + delta
178
179 return self:send(string.format("setvol %d", new_volume))
180 end
181
182 function MPD:volume_down(delta)
183 return self:volume_up(-delta)
184 end
185
186 function MPD:toggle_random()
187 local stats = self:send("status")
188 if tonumber(stats.random) == 0 then
189 return self:send("random 1")
190 else
191 return self:send("random 0")
192 end
193 end
194
195 function MPD:toggle_repeat()
196 local stats = self:send("status")
197 if tonumber(stats["repeat"]) == 0 then
198 return self:send("repeat 1")
199 else
200 return self:send("repeat 0")
201 end
202 end
203
204 function MPD:toggle_play()
205 if self:send("status").state == "stop" then
206 return self:send("play")
207 else
208 return self:send("pause")
209 end
210 end
211
212 return {
213 new = new,
214 }
215
216 -- vim:ft=lua:ts=2:sw=2:sts=2:tw=80:et