File

plugins/presence.lua @ 485:c9a144591649

component: Avoid adding to the global stream metatable This allows component and client connections to be made side-by-side. Previous to this change, loading this connection module would break the ability to make client connections, due to overriding stream methods such as :reopen() and :reset(). A next step would be to share the methods that the two connection modules have in common.
author Matthew Wild <mwild1@gmail.com>
date Thu, 23 Mar 2023 18:56:32 +0000
parent 476:c34b263499be
child 490:6b2f31da9610
line wrap: on
line source

local verse = require "verse";
local st = require "util.stanza";

function verse.plugins.presence(stream)
	stream.last_presence = nil;

	stream:hook("presence-out", function (presence)
		if not presence.attr.to then
			-- Clone so we don't add stuff over and over..?
			stream.last_presence = st.clone(presence); -- Cache non-directed presence
		end
	end, 1);

	function stream:resend_presence()
		if self.last_presence then
			stream:send(self.last_presence);
		end
	end

	function stream:set_status(opts)
		local p = verse.presence();
		if type(opts) == "table" then
			if opts.show then
				p:tag("show"):text(opts.show):up();
			end
			if opts.priority or opts.prio then
				p:tag("priority"):text(tostring(opts.priority or opts.prio)):up();
			end
			if opts.status or opts.msg then
				p:tag("status"):text(opts.status or opts.msg):up();
			end
		elseif type(opts) == "string" then
			p:tag("status"):text(opts):up();
		end

		stream:send(p);
	end
end