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