--- a/examples/avatar.lua Sat Mar 28 19:43:12 2009 +0200
+++ b/examples/avatar.lua Tue Mar 31 18:35:34 2009 +0300
@@ -1,58 +1,74 @@
--- USER AVATAR (XEP-0084)
-
--- library
-
-local sha1 = require 'sha1'
-local base64 = require 'base64'
local lm = require 'lm'
-local pep = require 'pep'
-local pubsub = require 'pubsub'
-
---
+local pubsub = require 'lm.pubsub'
+local avatar = require 'lm.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
+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 )
-function F.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
+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' )
-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
+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.'
-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' )
- if data then
- success ( data:value () )
- else
- -- XXX
- end
- end, fail, nil, { id } )
-end
-
-return F
+main.add_feature ( 'urn:xmpp:avatar:metadata+notify' )
-- vim: se ts=4: --