examples/vcard.lua
changeset 70 e43e386c8a33
parent 68 742878c74b8e
child 99 ed4676536ed9
--- a/examples/vcard.lua	Fri Apr 03 09:26:21 2009 +0300
+++ b/examples/vcard.lua	Mon Apr 06 17:21:19 2009 +0300
@@ -1,10 +1,70 @@
 
-local lm    = require 'lm'
-local vcard = require 'lm.vcard'
+local lm     = require 'lm'
+local vcard  = require 'lm.vcard'
+local base64 = require 'base64'
+local mime   = require 'mime-type'
 
 main.command ( 'vcard-temp',
 	function ( args )
-		vcard.retrieve ( lm.connection.bless ( main.connection () ), args[1],
+		local who
+		local action = args[1]
+		local conn   = lm.connection.bless ( main.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 )
@@ -31,4 +91,6 @@
 			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: --