# HG changeset patch # User Kim Alvefur # Date 1605364235 -3600 # Node ID 3addfb97296c738a311186f4ffaf873ab02ea6ee # Parent 58d485001448c8a1a5d309ee279c5b613be7bc12 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" diff -r 58d485001448 -r 3addfb97296c clix/raw.lua --- 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,