clix.raw: Add a shorthand for top level stanza attributes

Sat, 14 Nov 2020 15:30:35 +0100

author
Kim Alvefur <zash@zash.se>
date
Sat, 14 Nov 2020 15:30:35 +0100
changeset 132
3addfb97296c
parent 131
58d485001448
child 133
be3b857e991f

clix.raw: Add a shorthand for top level stanza attributes

The m, p and iq functions are amended so that indexing them sets the
'type' attribute and a subsequent function call with a string sets the
'to' attribute.

Examples:

m.chat"recipient":body"Hello"

iq.get"example.com":query"jabber:iq:version"

clix/raw.lua file | annotate | diff | comparison | revisions
--- a/clix/raw.lua	Sat Nov 14 15:26:06 2020 +0100
+++ b/clix/raw.lua	Sat Nov 14 15:30:35 2020 +0100
@@ -39,11 +39,19 @@
 					return data;
 				end
 			};
+			local stwrap;
+			do
+				local f_mt, r_mt = {}, {};
+				function f_mt:__call(...) if ... and type(...) == "string" then return self{ to=... } end return self._f(...) end
+				function f_mt:__index(k) return setmetatable({_st = self._f{ type = k }}, r_mt); end
+				function r_mt:__call(to) self._st.attr.to = to; return self._st end
+				function stwrap(f) return setmetatable({_f=f},f_mt) end
+			end
 			local env = setmetatable({}, { __index = {
 				s = verse.stanza,
-				m = verse.message,
-				p = verse.presence,
-				iq = verse.iq,
+				m = stwrap(verse.message),
+				p = stwrap(verse.presence),
+				iq = stwrap(verse.iq),
 				ping = function(host)
 					return verse.iq{ type="get", to=host}:tag("ping", {xmlns="urn:xmpp:ping"});
 				end,

mercurial