Vcard-temp avatar
authorMyhailo Danylenko <isbear@ukrpost.net>
Mon, 06 Apr 2009 17:21:19 +0300
changeset 70 e43e386c8a33
parent 69 ab6d4ee8974c
child 71 eb6edac301ac
Vcard-temp avatar * Docgen handles preformatted text * Forms can add fields * Ping help * Easy way to publish vcard-temp photo * File mime-type detection
docgen.pl
examples/forms.lua
examples/lm/disco.lua
examples/lm/iq_register.lua
examples/lm/vcard.lua
examples/lm/x_data.lua
examples/ping.lua
examples/vcard.lua
util.h
--- a/docgen.pl	Fri Apr 03 09:26:21 2009 +0300
+++ b/docgen.pl	Mon Apr 06 17:21:19 2009 +0300
@@ -78,6 +78,8 @@
 			s/^A: /<br\/>Arguments: /;
 			s/^R: /<br\/>Return values: /;
 			s/^V: /<br\/>Values: /;
+			s/^\[ /<br\/><pre>/;
+			s/^\]/<\/pre><br\/>/;
 			if ( $list ) {
 				if ( /^\* / ) {
 					s/^\* /<\/li><li>/;
--- a/examples/forms.lua	Fri Apr 03 09:26:21 2009 +0300
+++ b/examples/forms.lua	Mon Apr 06 17:21:19 2009 +0300
@@ -26,6 +26,8 @@
 			elseif action == 'del' then
 				main.del_completion ( form_cid, id )
 				forms[id] = nil
+			elseif action == 'add' then
+				form:add( args[3], { value = args[4] } )
 			elseif action then
 				local fname, value
 				if action == 'set' then
@@ -109,6 +111,6 @@
 		end
 	end, true, form_cid )
 
-commands_help['form'] = "[form_id [send | reject | [set] fieldname [value]]]\n\nWithout arguments prints form list.\nWith bare form id prints info on that form.\nWhen setting multivalue field, new values are added, not replacing previous.\nWithout value unsets field, multivalue fields lose all their values."
+commands_help['form'] = "[form_id [send | reject | [set | add] fieldname [value]]]\n\nWithout arguments prints form list.\nWith bare form id prints info on that form.\nWhen setting multivalue field, new values are added, not replacing previous.\nWithout value unsets field, multivalue fields lose all their values.\nAdd command allws to append field to form end.\nNote, that form-specific default type will be used."
 
 -- vim: se ts=4: --
--- a/examples/lm/disco.lua	Fri Apr 03 09:26:21 2009 +0300
+++ b/examples/lm/disco.lua	Mon Apr 06 17:21:19 2009 +0300
@@ -49,8 +49,7 @@
 				item = item:next ()
 			end
 			success ( identities, features )
-		end,
-		fail )
+		end, fail )
 end
 
 return F
--- a/examples/lm/iq_register.lua	Fri Apr 03 09:26:21 2009 +0300
+++ b/examples/lm/iq_register.lua	Mon Apr 06 17:21:19 2009 +0300
@@ -108,6 +108,9 @@
 	else
 		fld.var   = name
 		fld.index = #form.f + 1
+		if not fld.type then
+			fld.type = 'text-single'
+		end
 		local obj = form_field.new ( fld )
 		table.insert ( form.f, obj )
 		form.f[name] = obj
--- a/examples/lm/vcard.lua	Fri Apr 03 09:26:21 2009 +0300
+++ b/examples/lm/vcard.lua	Mon Apr 06 17:21:19 2009 +0300
@@ -72,6 +72,9 @@
 function M.add ( form, name, fld )
 	fld.var   = name
 	fld.index = #form.f + 1
+	if not fld.type then
+		fld.type = 'text-single'
+	end
 	local obj = form_field.new ( fld )
 	table.insert ( form.f, obj )
 	form.f[name] = obj
@@ -92,7 +95,7 @@
 			if card and card:attribute ( 'xmlns' ) == 'vcard-temp' then
 				success ( F.parse ( mess:child ( 'vCard' ) ),
 					function ( form, success, fail )
-						iq.send ( conn, from, 'set', form:format ( form, { }, 'submit' ), success, fail )
+						iq.send ( conn, from, 'set', form:format ( { }, 'submit' ), success, fail )
 					end,
 					function ( form, success, fail )
 						success ()
--- a/examples/lm/x_data.lua	Fri Apr 03 09:26:21 2009 +0300
+++ b/examples/lm/x_data.lua	Mon Apr 06 17:21:19 2009 +0300
@@ -210,6 +210,9 @@
 function M.add ( form, var, fld )
 	fld.var   = var
 	fld.index = #form.f + 1 -- XXX
+	if not fld.type then
+		fld.type = 'text-single'
+	end
 	local obj = form_field.new ( fld )
 	table.insert ( form.f, obj )
 	form.f[var] = obj
--- a/examples/ping.lua	Fri Apr 03 09:26:21 2009 +0300
+++ b/examples/ping.lua	Mon Apr 06 17:21:19 2009 +0300
@@ -20,6 +20,8 @@
 			end )
 	end, true, 'jid' )
 
+commands_help['ping'] = '[jid]\n\nSends XMPP ping request to specified jid or current buddy'
+
 --[[
 local ping_handler = lm.message_handler.new ( ping.iq_handler )
 local ping_handler_registered = false
--- 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: --
--- a/util.h	Fri Apr 03 09:26:21 2009 +0300
+++ b/util.h	Mon Apr 06 17:21:19 2009 +0300
@@ -9,8 +9,6 @@
 #define enum_value_t int
 #endif
 
-#define lua_pushconststring(L, STRING) { lua_pushlstring (L, STRING, sizeof(STRING)-1); }
-
 // Array of string2eunm_t's must be ended with pair { NULL, fallback_value }
 typedef struct {
 	const char   *string;