examples/avatar.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 28 Nov 2012 20:17:53 +0200
changeset 146 04d19c9c1196
parent 68 742878c74b8e
permissions -rw-r--r--
Fix module loading problem


local lm     = require 'lm'
local pubsub = require 'lm.pubsub'
local avatar = require 'lm.avatar'

pubsub.handler ( 'urn:xmpp:avatar:metadata',
	function ( from, node, data, id )
		if not main.yesno ( main.option ( 'lua_pep_notification' ) ) then
			return true
		end
		local item = data:child ()
		while item do
			main.print_info ( from, ('Avatar: %s [%s] %s bytes, %sx%s %s'):format (
					item:attribute ( 'id' ),
					item:attribute ( 'type' ),
					item:attribute ( 'bytes' ),
					item:attribute ( 'width' ) or '?',
					item:attribute ( 'height' ) or '?',
					item:attribute ( 'url' ) or '' ) )
			item = item:next ()
		end
	end )

main.command ( 'avatar',
	function ( args )
		local action = args[1]
		if action == 'get' then
			local who
			if args.t then
				who = args.t
			else
				who = main.current_buddy ()
			end
			avatar.get ( lm.connection.bless ( main.connection () ), who, args[2],
				function ( data )
					local h = io.open ( args[3], 'w' )
					if h then
						h:write ( data )
						h:close ()
						main.print_info ( who, 'Avatar saved to ' .. args[3] )
					else
						print ( 'Cannot open file for writing ' .. args[3] )
					end
				end,
				function ( mesg )
					main.print_info ( who, 'Error obtaining avatar: ' .. mesg )
				end )
		else
			local file = action
			if action == 'set' then
				file = args[2]
			end
			local h = io.open ( file )
			if h then
				data = h:read ( '*a' )
				h:close ()
				avatar.publish ( lm.connection.bless ( main.connection () ), data,
					function ()
						print ( 'Avatar published' )
					end,
					function ( mesg )
						print ( 'Avatar publishing error: ' .. mesg )
					end )
			else
				print ( 'Cannot open file ' .. file )
			end
		end
	end, true, 'file' )

commands_help['avatar'] = '[-t jid] get id filename | [set] filename\n\nGet action tries to get from server avatar with specified id and save it to \'filename\'.\nSet action publishes avatar to server. File must be a PNG image.'

main.add_feature ( 'urn:xmpp:avatar:metadata+notify' )

-- vim: se ts=4: --