Pep improvements
authorMyhailo Danylenko <isbear@ukrpost.net>
Sun, 15 Mar 2009 02:36:14 +0200
changeset 21 2384ce322282
parent 20 fd96da2c3acc
child 22 6460b020825d
Pep improvements * Send empty only if changed * Incoming mood and activity handlers
examples/mcabberrc.lua
examples/mpd.lua
--- a/examples/mcabberrc.lua	Sun Mar 15 02:33:57 2009 +0200
+++ b/examples/mcabberrc.lua	Sun Mar 15 02:36:14 2009 +0200
@@ -334,7 +334,7 @@
 
 dopath 'marking'
 
--- MPD
+-- MPD (in fact, XEP-0118)
 
 dopath 'mpd'
 
@@ -350,6 +350,10 @@
 
 dopath 'xep0047'
 
+-- PUBSUB (XEP-0060)
+
+dopath 'xep0060'
+
 -- IN-BAND REGISTRATION (XEP-0077)
 
 dopath 'xep0077'
--- a/examples/mpd.lua	Sun Mar 15 02:33:57 2009 +0200
+++ b/examples/mpd.lua	Sun Mar 15 02:36:14 2009 +0200
@@ -118,30 +118,26 @@
 mpd_pub_song = { xmlns = 'http://jabber.org/protocol/tune' }
 
 function mpd_getstatus ()
-	if not mpd_enabled then
-		mpd_pub_song.artist = nil
-		mpd_pub_song.length = nil
-		mpd_pub_song.source = nil
-		mpd_pub_song.title  = nil
-		mpd_pub_song.track  = nil
-		return mpd_pub_song
+	local status   = mpd.call_command { 'status' }
+	if not mpd_enabled or ( status.state ~= 'play' and status.state ~= 'pause' ) then
+		if mpd_pub_song.artist or mpd_pub_song.length or mpd_pub_song.source or mpd_pub_song.title or mpd_pub_song.track then
+			mpd_pub_song.artist = nil
+			mpd_pub_song.length = nil
+			mpd_pub_song.source = nil
+			mpd_pub_song.title  = nil
+			mpd_pub_song.track  = nil
+			return mpd_pub_song
+		else
+			return nil
+		end
 	end
 	
-	local stats = mpd.call_command { 'status', 'currentsong' }
-	if stats[1].state ~= 'play' and stats[1].state ~= 'pause' then
-		mpd_pub_song.artist = nil
-		mpd_pub_song.length = nil
-		mpd_pub_song.source = nil
-		mpd_pub_song.title  = nil
-		mpd_pub_song.track  = nil
-		return mpd_pub_song
-	end
+	local modified  = false
+	local song      = mpd.call_command { 'currentsong' }
+	local dir, file = song.file:match ( '(.+)/(.-)' )
+	-- populate according to currentsong fields: artist - artist, length - time, source - album, title - title, track - id, rating - ?, uri - ?
+	local artist, length, source, title, track = song.artist, song.time, song.album, song.title, song.id
 
-	local dir, file = stats[2].file:match ( '(.+)/(.-)' )
-	local modified  = false
-	-- populate according to currentsong fields: artist - artist, length - time, source - album, title - title, track - id, rating - ?, uri - ?
-	local artist, length, source, title, track = stats[2].artist, stats[2].time, stats[2].album, stats[2].title, stats[2].id
-	
 	if not artist or artist == '' then
 		artist = 'Unknown'
 	end
@@ -204,15 +200,25 @@
 	if sdata then
 		local conn = lm.connection.bless ( main.connection () )
 		if conn:status () == 'authenticated' then
+--			local bjid = conn:jid():gsub ( '/.*', '' )
 			conn:send (
-				lm.message.create { mtype = 'iq-set', from = conn:jid (),
+				lm.message.create { mtype = 'iq-set', --from = conn:jid (), to = bjid,
 					pubsub = { xmlns = 'http://jabber.org/protocol/pubsub',
 						publish = { node = 'http://jabber.org/protocol/tune',
-							item = {
+							item = { --id = "current",
 								tune = sdata,
 							},
 						},
 					},
+--[[					configure = {
+						x = {
+							field = {{ type = "hidden", var = 'FORM_TYPE',
+								value = { 'http://jabber.org/protocol/pubsub#node_config' },
+							},{ var = "pubsub#access_model",
+								value = { 'presence' },
+							}},
+						},
+					},--]]
 				})
 		end
 	end
@@ -267,7 +273,41 @@
 			local item = tune:children ()
 			main.print_info ( from, "Tunes notification:" )
 			while item do
-				main.print_info ( from, ("- %s: %s"):format ( item:name (), item:value () ) )
+				main.print_info ( from, ("- %s: %s"):format ( item:name (), item:value () or '' ) )
+				item = item:next ()
+			end
+			return true
+		end
+		return false
+	end )
+
+mood_incoming_message_handler = lm.message_handler.new (
+	function ( conn, mess )
+		-- we can add that validation stuff later, if it will be necessary
+		local tune = mess:path ( 'event', 'items', 'item', 'mood' )
+		if tune then
+			local from = mess:attribute ( 'from' )
+			local item = tune:children ()
+			main.print_info ( from, "Mood notification:" )
+			while item do
+				main.print_info ( from, ("- %s: %s"):format ( item:name (), item:value () or '' ) )
+				item = item:next ()
+			end
+			return true
+		end
+		return false
+	end )
+
+activity_incoming_message_handler = lm.message_handler.new (
+	function ( conn, mess )
+		-- we can add that validation stuff later, if it will be necessary
+		local tune = mess:path ( 'event', 'items', 'item', 'activity' )
+		if tune then
+			local from = mess:attribute ( 'from' )
+			local item = tune:children ()
+			main.print_info ( from, "Activity notification:" )
+			while item do
+				main.print_info ( from, ("- %s: %s"):format ( item:name (), item:value () or '' ) )
 				item = item:next ()
 			end
 			return true
@@ -280,7 +320,12 @@
 hooks_d['hook-post-connect'].mpd =
 	function ( args )
 		lm.connection.bless( main.connection () ):handler ( mpd_incoming_message_handler, 'message', 'normal' )
+		lm.connection.bless( main.connection () ):handler ( mood_incoming_message_handler, 'message', 'normal' )
+		lm.connection.bless( main.connection () ):handler ( activity_incoming_message_handler, 'message', 'normal' )
 		mpd_handler_registered = true
+		if mpd_enabled then
+			mpd_callback ()
+		end
 		hooks_d['hook-post-connect'].mpd =
 			function ( args )
 				if mpd_enabled then
@@ -291,18 +336,21 @@
 			function ( args )
 				if mpd_handler_registered then
 					lm.connection.bless( main.connection () ):handler ( mpd_incoming_message_handler, 'message' )
+					lm.connection.bless( main.connection () ):handler ( mood_incoming_message_handler, 'message' )
+					lm.connection.bless( main.connection () ):handler ( activity_incoming_message_handler, 'message' )
 					mpd_handler_registered = false
 				end
 			end
-		if mpd_enabled then
-			mpd_callback ()
-		end
 	end
 
 -- XXX: this really should be initialized after connection establishment?
 -- but as this thing is implemented by now, it will be cached by server,
 -- and, thus, we will be unable to get notifications.
+main.add_feature ( 'http://jabber.org/protocol/tune+notify' )
 main.add_feature ( 'http://jabber.org/protocol/tune' )
-main.add_feature ( 'http://jabber.org/protocol/tune+notify' )
+main.add_feature ( 'http://jabber.org/protocol/mood+notify' )
+main.add_feature ( 'http://jabber.org/protocol/mood' )
+main.add_feature ( 'http://jabber.org/protocol/activity+notify' )
+main.add_feature ( 'http://jabber.org/protocol/activity' )
 
 -- vim: se ts=4: --