examples/vcard.lua
author Myhailo Danylenko <isbear@ukrpost.net>
Wed, 28 Nov 2012 20:17:53 +0200
changeset 146 04d19c9c1196
parent 121 75a7d595817c
permissions -rw-r--r--
Fix module loading problem


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' )

-- vim: se ts=4 sw=4: --