Mpd password support
authorMyhailo Danylenko <isbear@ukrpost.net>
Fri, 03 Apr 2009 09:26:21 +0300
changeset 69 ab6d4ee8974c
parent 68 742878c74b8e
child 70 e43e386c8a33
Mpd password support
examples/mpd.lua
examples/tune.lua
--- a/examples/mpd.lua	Tue Mar 31 18:35:34 2009 +0300
+++ b/examples/mpd.lua	Fri Apr 03 09:26:21 2009 +0300
@@ -1,8 +1,6 @@
 
 -- MPD INTERATION
 
--- TODO password support
-
 -- library
 
 local socket = require 'socket'
@@ -49,7 +47,7 @@
 
 -- use: mpd.call_command { 'status' }
 --      mpd.call_command { 'lsinfo misc', list = { file = true, directory = true } }
---      mpd.call_command { 'next', noret = true }
+--      mpd.call_command { 'next', noret = true } -- removed, early termination with password :/
 --      mpd.call_command { 'status', 'currentsong' }
 -- on one command returns just results of that command, on multi - array of results
 -- on errors returns nil
@@ -59,21 +57,51 @@
 	if not tcp then
 		print ( 'mpd: cannot get master tcp object' )
 		return nil
-	elseif not tcp:connect ( O.hostname, O.port ) then
+	end
+
+	if not O.hostname then
+		local server = os.getenv ( 'MPD_HOST' )
+		if server then
+			local password, host = server:match ( '(.+)@(.-)' )
+			if password then
+				F.server ( host, os.getenv ( 'MPD_PORT' ) or 6600, password )
+			else
+				F.server ( server, os.getenv ( 'MPD_PORT' ) or 6600, nil )
+			end
+		else
+			F.server ( "localhost", 6600, nil )
+		end
+	end
+
+	if not tcp:connect ( O.hostname, O.port ) then
 		tcp:close ()
 		print ( 'mpd: cannot connect to server' )
 		return nil
 	end
 
 	local ret = {}
-	if not opts.noret then
-		ret = F.receive_message ( tcp )
+	ret = F.receive_message ( tcp )
+	if not ret then
+		tcp:close ()
+		print ( 'mpd: error getting greeting from server' )
+		return nil
+	elseif ret.STATUS ~= 'OK' then
+		print ( 'mpd: server ack\'s in greeting' )
+	end
+
+	if O.password then
+		if not tcp:send ( "password " .. O.password .. "\n" ) then
+			tcp:close ()
+			print ( 'mpd: error sending password' )
+			return nil
+		end
+		ret = mpd.receive_message ( tcp )
 		if not ret then
 			tcp:close ()
-			print ( 'mpd: error getting greeting from server' )
+			print ( 'mpd: error getting response for password' )
 			return nil
 		elseif ret.STATUS ~= 'OK' then
-			print ( 'mpd: server ack\'s in greeting' )
+			print ( 'mpd: server refuses password' )
 		end
 	end
 
@@ -83,14 +111,12 @@
 			print ( 'mpd: error sending command ' .. command )
 			return nil
 		end
-		if not opts.noret then
-			ret[num] = F.receive_message ( tcp, opts.list )
-			if not ret[num] then
-				print ( 'mpd: error getting result' )
-			end
-			if ret[num]['STATUS'] ~= 'OK' then
-				print ( 'mpd: server acks our command ' .. command )
-			end
+		ret[num] = F.receive_message ( tcp, opts.list )
+		if not ret[num] then
+			print ( 'mpd: error getting result' )
+		end
+		if ret[num]['STATUS'] ~= 'OK' then
+			print ( 'mpd: server acks our command ' .. command )
 		end
 	end
 
--- a/examples/tune.lua	Tue Mar 31 18:35:34 2009 +0300
+++ b/examples/tune.lua	Fri Apr 03 09:26:21 2009 +0300
@@ -144,4 +144,6 @@
 main.add_feature ( 'http://jabber.org/protocol/tune+notify' )
 main.add_feature ( 'http://jabber.org/protocol/tune' )
 
+mpd.server ( "localhost", 6600, nil )
+
 -- vim: se ts=4: --