examples/activity.lua
changeset 59 4660c4f10ef1
child 61 5182f466da7d
equal deleted inserted replaced
58:aa3376776cf2 59:4660c4f10ef1
       
     1 
       
     2 -- USER ACTIVITY (XEP-0108)
       
     3 
       
     4 -- library
       
     5 
       
     6 require 'lm'
       
     7 require 'pep'
       
     8 
       
     9 -- public
       
    10 
       
    11 activity = { }
       
    12 
       
    13 function activity.publish ( conn, success, fail, general, specific, message )
       
    14 	local item = { xmlns = 'http://jabber.org/protocol/activity' }
       
    15 	if general then
       
    16 		item[general] = { }
       
    17 		if specific then
       
    18 			item[general][specific] = { }
       
    19 		end
       
    20 	end
       
    21 	if message then
       
    22 		item.text = { message }
       
    23 	end
       
    24 	pep.publish ( conn, 'http://jabber.org/protocol/activity', { activity = item }, success, fail )
       
    25 end
       
    26 
       
    27 -- mcabber
       
    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 activity, text = args[1], args[2]
       
    61 		local act, subact = activity:match ( "(.-)%-(.+)" )
       
    62 		if not act then
       
    63 			act = activity
       
    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 
       
    79 -- vim: se ts=4: --