Avatar
authorMyhailo Danylenko <isbear@ukrpost.net>
Mon, 23 Mar 2009 02:00:57 +0200
changeset 54 b53355736057
parent 53 2162188b20cf
child 55 9e32b6288c86
Avatar
examples/avatar.lua
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/examples/avatar.lua	Mon Mar 23 02:00:57 2009 +0200
@@ -0,0 +1,126 @@
+
+-- USER AVATAR (XEP-0084)
+
+-- library
+
+require 'lm'
+require 'sha1'
+require 'base64'
+require 'pubsub'
+require 'pep'
+
+-- public
+
+avatar = { }
+
+-- TODO 'temporary disabling'
+--          however I cannot see a method to enable it back without republishing avatar :(
+--          this requires client to know, what is published on the server now
+--          maybe we can do that by requesting item without payload from server
+
+function avatar.publish ( conn, data, success, fail, height, width )
+	local id = sha1.digest ( data )
+	pep.publish ( conn, 'urn:xmpp:avatar:data',
+		{ id = id,
+			data = { xmlns = 'urn:xmpp:avatar:data',
+				base64.encode ( data ),
+			},
+		},
+		function ()
+			pep.publish ( conn, 'urn:xmpp:avatar:metadata',
+				{ id = id,
+					info = { bytes = data:len (), id = id, type = 'image/png', height = height, width = width },
+				}, success, fail )
+		end, fail )
+end
+
+function avatar.publish_url ( conn, url, iid, size, id, mtype, success, fail, height, width )
+	pep.publish ( conn, 'urn:xmpp:avatar:metadata',
+		{ id = iid,
+			info = { bytes = size, id = id, type = mtype, url = url, height = height, width = width },
+		}, success, fail )
+end
+
+function avatar.get ( conn, from, id, success, fail )
+	pubsub.retrieve ( conn, from, 'urn:xmpp:avatar:data',
+		function ( from, node, item )
+			local data = item:child ( 'data' )
+			if data then
+				success ( data:value () )
+			else
+				-- XXX
+			end
+		end, fail, nil, { id } )
+end
+
+-- mcabber
+
+pep.handlers['urn:xmpp:avatar:metadata'] =
+	function ( from, node, data )
+		if not main.yesno ( main.option ( 'lua_pep_notification' ) ) then
+			return true
+		end
+		local item = data:children ()
+		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: --