examples/mc_tune.lua
changeset 66 542f61e113cb
child 67 d33ca5572e91
equal deleted inserted replaced
65:f1be8dbb209c 66:542f61e113cb
       
     1 
       
     2 local mpd    = require 'mpd'
       
     3 local lm     = require 'lm'
       
     4 local pubsub = require 'pubsub'
       
     5 
       
     6 local tune_enabled = false
       
     7 local mpd_pub_song = { }
       
     8 
       
     9 pubsub.handler ( 'http://jabber.org/protocol/tune',
       
    10 	function ( from, node, data )
       
    11 		local self = false
       
    12 		if from == lm.connection.bless ( main.connection () ):jid():gsub ( '/.*', '' ) then -- o_O
       
    13 			self         = true
       
    14 			mpd_pub_song = { }
       
    15 		end
       
    16 		local item = data:child ()
       
    17 		local text = ''
       
    18 		while item do
       
    19 			local name  = item:name ()
       
    20 			local value = item:value ()
       
    21 			if self then
       
    22 				mpd_pub_song[name] = value or ''
       
    23 			end
       
    24 			text = ("%s\n- %s: %s"):format ( text, item:name (), item:value () or '' )
       
    25 			item = item:next ()
       
    26 		end
       
    27 		if main.yesno ( main.option ( 'lua_pep_notification' ) ) then
       
    28 			if text ~= '' then
       
    29 				text = 'Now listening to:' .. text
       
    30 			else
       
    31 				text = 'Now not listening to anything'
       
    32 			end
       
    33 		end
       
    34 		main.print_info ( from, text )
       
    35 		return true
       
    36 	end )
       
    37 
       
    38 local function mpd_getstatus ()
       
    39 	local status = mpd.call_command { 'status' }
       
    40 	if not tune_enabled or ( status.state ~= 'play' and status.state ~= 'pause' ) then
       
    41 		for k, v in pairs ( mpd_pub_song ) do -- if there is anything published, publish nothing
       
    42 			return { }
       
    43 		end
       
    44 		return nil
       
    45 	end
       
    46 	
       
    47 	local song      = mpd.call_command { 'currentsong' }
       
    48 	local dir, file = song.file:match ( '(.+)/(.-)' )
       
    49 	-- populate according to currentsong fields: artist - artist, length - time, source - album, title - title, track - id, rating - ?, uri - ?
       
    50 	local ret = {
       
    51 		artist = song.artist or 'Unknown',
       
    52 		length = song.time,
       
    53 		source = song.album  or dir,
       
    54 		title  = song.title  or file,
       
    55 		track  = song.id,
       
    56 	}
       
    57 
       
    58 	if not song.time or song.time == '0' then -- XXX
       
    59 		ret.length = nil
       
    60 	end
       
    61 
       
    62 	local modified = false
       
    63 	for k, v in pairs ( ret ) do
       
    64 		if mpd_pub_song[k] ~= v then
       
    65 			modified = true
       
    66 			break
       
    67 		end
       
    68 	end
       
    69 	if not modified then
       
    70 		for k, v in pairs ( mpd_pub_song ) do
       
    71 			if ret[k] ~= v then
       
    72 				modified = true
       
    73 				break
       
    74 			end
       
    75 		end
       
    76 	end
       
    77 
       
    78 	if modified then
       
    79 		return ret
       
    80 	else
       
    81 		return nil
       
    82 	end
       
    83 end
       
    84 
       
    85 local function mpd_callback ()
       
    86 	local sdata = mpd_getstatus ()
       
    87 	if sdata then
       
    88 		pep.publish ( lm.connection.bless ( main.connection () ), 'http://jabber.org/protocol/tune',
       
    89 			function ()
       
    90 			end,
       
    91 			function ( mesg )
       
    92 				print ( 'Error publishing tune: ' .. mesg )
       
    93 			end, sdata )
       
    94 	end
       
    95 	if tune_enabled then
       
    96 		return true
       
    97 	else
       
    98 		return false
       
    99 	end
       
   100 end
       
   101 
       
   102 -- do not call it too fast, or you end up with many daemons at once
       
   103 local function enable_tune ( yn )
       
   104 	if yn == nil then
       
   105 		yn = true
       
   106 	end
       
   107 	if yn then
       
   108 		if not tune_enabled then
       
   109 			main.timer ( 15, mpd_callback )
       
   110 			tune_enabled = true
       
   111 			-- update status
       
   112 		end
       
   113 	else
       
   114 		if tune_enabled then
       
   115 			tune_enabled = false
       
   116 			-- update status
       
   117 		end
       
   118 	end
       
   119 end
       
   120 
       
   121 main.command ( 'tune',
       
   122 	function ( args )
       
   123 		local enable = main.yesno ( args )
       
   124 		if enable == nil then
       
   125 			if tune_enabled then
       
   126 				print ( "Tune notifications enabled" )
       
   127 			else
       
   128 				print ( "Tune notifications disabled" )
       
   129 			end
       
   130 		else
       
   131 			enable_tune ( enable )
       
   132 		end
       
   133 	end, false, 'yesno' )
       
   134 
       
   135 commands_help['tune'] = "[enable|disable|on|off|yes|no|true|false]\n\nEnables or disables publishing of notifications about playing music in your player (currently only mpd is supported)."
       
   136 
       
   137 hooks_d['hook-post-connect'].tune =
       
   138 	function ( args )
       
   139 		if tune_enabled then
       
   140 			mpd_callback ()
       
   141 		end
       
   142 	end
       
   143 
       
   144 main.add_feature ( 'http://jabber.org/protocol/tune+notify' )
       
   145 main.add_feature ( 'http://jabber.org/protocol/tune' )
       
   146 
       
   147 -- vim: se ts=4: --