examples/geoloc.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Mon, 23 Mar 2009 04:16:22 +0200
changeset 59 4660c4f10ef1
child 66 542f61e113cb
permissions -rw-r--r--
Pep splitting


-- USER LOCATION (XEP-0080)

-- library

require 'lm'
require 'pep'

-- public

geoloc = { }

function geoloc.publish ( conn, success, fail, data )
	local sdata = { xmlns = 'http://jabber.org/protocol/geoloc' }
	if data then
		for key, value in pairs ( data ) do
			sdata[key] = { value }
		end
	end
	pep.publish ( conn, 'http://jabber.org/protocol/geoloc', { geoloc = sdata }, success, fail )
end

-- mcabber

pep.handlers['http://jabber.org/protocol/geoloc'] =
	function ( from, node, data )
		if not main.yesno ( main.option ( 'lua_pep_notification' ) ) then
			return true
		end
		local item = data:children ()
		local text = ''
		while item do
			text = ("%s\n- %s: %s"):format ( text, item:name (), item:value () or '' )
			item = item:next ()
		end
		if text ~= '' then
			text = 'Now at:' .. text
		else
			text = 'Now in unknown location'
		end
		main.print_info ( from, text )
		return true
	end

main.command ( 'location',
	function ( args )
		geoloc.publish ( lm.connection.bless ( main.connection () ),
			function ()
				print ( 'Geolocation published' )
			end,
			function ( mesg )
				print ( 'Error geolocation publishing: ' .. mesg )
			end, args )
	end, true )

commands_help['location'] = "[-key value [-key value ...]]\n\nPublishes your current geolocation.\nValid keys are accuracy, alt, area, bearing, building, country, datum, description, error, floor, lat, locality, lon, postalcode, region, room, speed, street, text, timestamp and uri, according to xep0080."

main.add_feature ( 'http://jabber.org/protocol/geoloc+notify' )
main.add_feature ( 'http://jabber.org/protocol/geoloc' )

-- vim: se ts=4: --