local lm = require 'lm'
local vcard = require 'lm.vcard'
local base64 = require 'base64'
local mime = require 'mime-type'
main.command ( 'vcard-temp',
function ( args )
local connection = main.connection ()
if not connection then
print "You are not online!"
return
end
local who
local action = args[1]
local conn = lm.connection.bless ( connection )
if not action then
who = conn:jid():gsub ( '/.*', '' )
elseif action == 'photo' then
local file = args[2]
if not file then
print ( 'You must specify filename' )
return
end
who = conn:jid():gsub ( '/.*', '' )
vcard.retrieve ( conn, who,
function ( form, submit, reject )
local h = io.open ( file, 'r' )
if not h then
print ( 'Cannot open file ' .. file )
reject ( form,
function ()
print ( 'Photo publication cancelled' )
end,
function ( mesg )
print ( 'Photo publication cancellation error: ' .. mesg )
end )
return
end
local data = h:read ( '*a' )
h:close ()
local mtype = mime.type ( file )
local ptype = form:field ( 'PHOTO/TYPE' )
if ptype then
ptype:value ( mtype )
else
form:add ( 'PHOTO/TYPE', { value = mtype } )
end
local pdata = form:field ( 'PHOTO/BINVAL' )
if pdata then
pdata:value ( base64.encode ( data ) )
else
form:add ( 'PHOTO/BINVAL', { value = base64.encode ( data ) } )
end
submit ( form,
function ()
print ( 'Photo published' )
end,
function ( mesg )
print ( 'Photo publication error: ' .. mesg )
end )
end,
function ( mesg )
print ( 'Vcard obtaining error: ' .. mesg )
end )
return
elseif action == '.' then
who = main.current_buddy ()
else
who = action
end
vcard.retrieve ( conn, who,
function ( form, submit, reject )
insert_form ( form,
function ( form )
submit ( form,
function ()
print ( 'Vcard changed' )
end,
function ( mesg )
print ( 'Vcard changing error: ' .. mesg )
end )
end,
function ( form )
reject ( form,
function ()
print ( 'Vcard changing cancelled' )
end,
function ( mesg )
print ( 'Vcard changing cancellation error: ' .. mesg )
end )
end )
end,
function ( mesg )
print ( 'Vcard obtaining error: ' .. mesg )
end )
end, true, 'jid' )
commands_help['vcard-temp'] = '[photo filename | . | jid]\n\nObtains vcard (as a form, but you may not submit it). Without arguments obtains your own vcard. "." means current buddy.\nYou can also publish your photo from file, that will automatically get your vcard, put a data into it and submit to server.'
-- vim: se ts=4 sw=4: --