examples/activity.lua
changeset 68 742878c74b8e
parent 66 542f61e113cb
--- a/examples/activity.lua	Sat Mar 28 19:43:12 2009 +0200
+++ b/examples/activity.lua	Tue Mar 31 18:35:34 2009 +0300
@@ -1,28 +1,56 @@
 
--- USER ACTIVITY (XEP-0108)
-
--- library
-
-local pep    = require 'pep'
-
--- public
-
-local F = { }
+local lm       = require 'lm'
+local activity = require 'lm.activity'
+local pubsub   = require 'lm.pubsub'
 
-function F.publish ( conn, success, fail, general, specific, message )
-	local item = { xmlns = 'http://jabber.org/protocol/activity' }
-	if general then
-		item[general] = { }
-		if specific then
-			item[general][specific] = { }
+pubsub.handler ( 'http://jabber.org/protocol/activity',
+	function ( from, node, data )
+		if not main.yesno ( main.option ( 'lua_pep_notification' ) ) then
+			return true
+		end
+		local item = data:child ()
+		local activity, desc
+		while item do
+			if item:name () == 'text' then
+				desc = item:value ()
+			else
+				activity = item:name ()
+				local subitem = item:child ()
+				if subitem then
+					-- here we can check for non-standard subactivity elements,
+					-- add subactivity child elements handling
+					activity = ("%s: %s"):format ( activity, subitem:name () )
+				end
+			end
+			item = item:next ()
 		end
-	end
-	if message then
-		item.text = { message }
-	end
-	pep.publish ( conn, 'http://jabber.org/protocol/activity', { activity = item }, success, fail )
-end
+		if activity then
+			main.print_info ( from, ("Now %s %s"):format ( activity, desc or '' ) )
+		else
+			main.print_info ( from, "Buddy hides his activity" )
+		end
+		return true
+	end )
 
-return F
+main.command ( 'activity',
+	function ( args )
+		local a, text = args[1], args[2]
+		local act, subact = a:match ( "(.-)%-(.+)" )
+		if not act then
+			act = a
+		end
+		activity.publish ( lm.connection.bless ( main.connection () ),
+			function ()
+				print ( 'Activity published' )
+			end,
+			function ( mesg )
+				print ( 'Error publishing activity: ' .. mesg )
+			end, act, subact, text )
+	end, true )
+
+commands_help['activity'] = "[activity[-specific_activity] [text]]\n\nPublishes your activity.\nNote, that for now it does not checks for activity validity, so, see xep0108 for valid activity values."
+
+main.add_feature ( 'http://jabber.org/protocol/activity+notify' )
+main.add_feature ( 'http://jabber.org/protocol/activity' )
 
 -- vim: se ts=4: --