examples/activity.lua
changeset 66 542f61e113cb
parent 61 5182f466da7d
child 68 742878c74b8e
equal deleted inserted replaced
65:f1be8dbb209c 66:542f61e113cb
     1 
     1 
     2 -- USER ACTIVITY (XEP-0108)
     2 -- USER ACTIVITY (XEP-0108)
     3 
     3 
     4 -- library
     4 -- library
     5 
     5 
     6 require 'lm'
     6 local pep    = require 'pep'
     7 require 'pep'
       
     8 
     7 
     9 -- public
     8 -- public
    10 
     9 
    11 activity = { }
    10 local F = { }
    12 
    11 
    13 function activity.publish ( conn, success, fail, general, specific, message )
    12 function F.publish ( conn, success, fail, general, specific, message )
    14 	local item = { xmlns = 'http://jabber.org/protocol/activity' }
    13 	local item = { xmlns = 'http://jabber.org/protocol/activity' }
    15 	if general then
    14 	if general then
    16 		item[general] = { }
    15 		item[general] = { }
    17 		if specific then
    16 		if specific then
    18 			item[general][specific] = { }
    17 			item[general][specific] = { }
    22 		item.text = { message }
    21 		item.text = { message }
    23 	end
    22 	end
    24 	pep.publish ( conn, 'http://jabber.org/protocol/activity', { activity = item }, success, fail )
    23 	pep.publish ( conn, 'http://jabber.org/protocol/activity', { activity = item }, success, fail )
    25 end
    24 end
    26 
    25 
    27 -- mcabber
    26 return F
    28 
       
    29 pep.handlers['http://jabber.org/protocol/activity'] =
       
    30 	function ( from, node, data )
       
    31 		if not main.yesno ( main.option ( 'lua_pep_notification' ) ) then
       
    32 			return true
       
    33 		end
       
    34 		local item = data:children ()
       
    35 		local activity, desc
       
    36 		while item do
       
    37 			if item:name () == 'text' then
       
    38 				desc = item:value ()
       
    39 			else
       
    40 				activity = item:name ()
       
    41 				local subitem = item:children ()
       
    42 				if subitem then
       
    43 					-- here we can check for non-standard subactivity elements,
       
    44 					-- add subactivity child elements handling
       
    45 					activity = ("%s: %s"):format ( activity, subitem:name () )
       
    46 				end
       
    47 			end
       
    48 			item = item:next ()
       
    49 		end
       
    50 		if activity then
       
    51 			main.print_info ( from, ("Now %s %s"):format ( activity, desc or '' ) )
       
    52 		else
       
    53 			main.print_info ( from, "Buddy hides his activity" )
       
    54 		end
       
    55 		return true
       
    56 	end
       
    57 
       
    58 main.command ( 'activity',
       
    59 	function ( args )
       
    60 		local a, text = args[1], args[2]
       
    61 		local act, subact = a:match ( "(.-)%-(.+)" )
       
    62 		if not act then
       
    63 			act = a
       
    64 		end
       
    65 		activity.publish ( lm.connection.bless ( main.connection () ),
       
    66 			function ()
       
    67 				print ( 'Activity published' )
       
    68 			end,
       
    69 			function ( mesg )
       
    70 				print ( 'Error publishing activity: ' .. mesg )
       
    71 			end, act, subact, text )
       
    72 	end, true )
       
    73 
       
    74 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."
       
    75 
       
    76 main.add_feature ( 'http://jabber.org/protocol/activity+notify' )
       
    77 main.add_feature ( 'http://jabber.org/protocol/activity' )
       
    78 
    27 
    79 -- vim: se ts=4: --
    28 -- vim: se ts=4: --