examples/vcard.lua
changeset 70 e43e386c8a33
parent 68 742878c74b8e
child 99 ed4676536ed9
equal deleted inserted replaced
69:ab6d4ee8974c 70:e43e386c8a33
     1 
     1 
     2 local lm    = require 'lm'
     2 local lm     = require 'lm'
     3 local vcard = require 'lm.vcard'
     3 local vcard  = require 'lm.vcard'
       
     4 local base64 = require 'base64'
       
     5 local mime   = require 'mime-type'
     4 
     6 
     5 main.command ( 'vcard-temp',
     7 main.command ( 'vcard-temp',
     6 	function ( args )
     8 	function ( args )
     7 		vcard.retrieve ( lm.connection.bless ( main.connection () ), args[1],
     9 		local who
       
    10 		local action = args[1]
       
    11 		local conn   = lm.connection.bless ( main.connection () )
       
    12 		if not action then
       
    13 			who = conn:jid():gsub ( '/.*', '' )
       
    14 		elseif action == 'photo' then
       
    15 			local file = args[2]
       
    16 			if not file then
       
    17 				print ( 'You must specify filename' )
       
    18 				return
       
    19 			end
       
    20 			who = conn:jid():gsub ( '/.*', '' )
       
    21 			vcard.retrieve ( conn, who,
       
    22 				function ( form, submit, reject )
       
    23 					local h = io.open ( file, 'r' )
       
    24 					if not h then
       
    25 						print ( 'Cannot open file ' .. file )
       
    26 						reject ( form,
       
    27 							function ()
       
    28 								print ( 'Photo publication cancelled' )
       
    29 							end,
       
    30 							function ( mesg )
       
    31 								print ( 'Photo publication cancellation error: ' .. mesg )
       
    32 							end )
       
    33 						return
       
    34 					end
       
    35 					local data = h:read ( '*a' )
       
    36 					h:close ()
       
    37 					local mtype = mime.type ( file )
       
    38 					local ptype = form:field ( 'PHOTO/TYPE' )
       
    39 					if ptype then
       
    40 						ptype:value ( mtype )
       
    41 					else
       
    42 						form:add ( 'PHOTO/TYPE', { value = mtype } )
       
    43 					end
       
    44 					local pdata = form:field ( 'PHOTO/BINVAL' )
       
    45 					if pdata then
       
    46 						pdata:value ( base64.encode ( data ) )
       
    47 					else
       
    48 						form:add ( 'PHOTO/BINVAL', { value = base64.encode ( data ) } )
       
    49 					end
       
    50 					submit ( form,
       
    51 						function ()
       
    52 							print ( 'Photo published' )
       
    53 						end,
       
    54 						function ( mesg )
       
    55 							print ( 'Photo publication error: ' .. mesg )
       
    56 						end )
       
    57 				end,
       
    58 				function ( mesg )
       
    59 					print ( 'Vcard obtaining error: ' .. mesg )
       
    60 				end )
       
    61 			return
       
    62 		elseif action == '.' then
       
    63 			who = main.current_buddy ()
       
    64 		else
       
    65 			who = action
       
    66 		end
       
    67 		vcard.retrieve ( conn, who,
     8 			function ( form, submit, reject )
    68 			function ( form, submit, reject )
     9 				insert_form ( form,
    69 				insert_form ( form,
    10 					function ( form )
    70 					function ( form )
    11 						submit ( form,
    71 						submit ( form,
    12 							function ()
    72 							function ()
    29 			function ( mesg )
    89 			function ( mesg )
    30 				print ( 'Vcard obtaining error: ' .. mesg )
    90 				print ( 'Vcard obtaining error: ' .. mesg )
    31 			end )
    91 			end )
    32 	end, true, 'jid' )
    92 	end, true, 'jid' )
    33 
    93 
       
    94 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.'
       
    95 
    34 -- vim: se ts=4: --
    96 -- vim: se ts=4: --