examples/avatar.lua
changeset 66 542f61e113cb
parent 54 b53355736057
child 68 742878c74b8e
--- a/examples/avatar.lua	Fri Mar 27 09:48:17 2009 +0200
+++ b/examples/avatar.lua	Fri Mar 27 12:06:19 2009 +0200
@@ -3,22 +3,22 @@
 
 -- library
 
-require 'lm'
-require 'sha1'
-require 'base64'
-require 'pubsub'
-require 'pep'
+local sha1   = require 'sha1'
+local base64 = require 'base64'
+local lm     = require 'lm'
+local pep    = require 'pep'
+local pubsub = require 'pubsub'
 
--- public
+--
 
-avatar = { }
+local F = { }
 
 -- 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 )
+function F.publish ( conn, data, success, fail, height, width )
 	local id = sha1.digest ( data )
 	pep.publish ( conn, 'urn:xmpp:avatar:data',
 		{ id = id,
@@ -34,14 +34,14 @@
 		end, fail )
 end
 
-function avatar.publish_url ( conn, url, iid, size, id, mtype, success, fail, height, width )
+function F.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 )
+function F.get ( conn, from, id, success, fail )
 	pubsub.retrieve ( conn, from, 'urn:xmpp:avatar:data',
 		function ( from, node, item )
 			local data = item:child ( 'data' )
@@ -53,74 +53,6 @@
 		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' )
+return F
 
 -- vim: se ts=4: --