plugins/presence.lua

1
local verse = require "verse";
2
local st = require "prosody.util.stanza";
3
 
4
function verse.plugins.presence(stream)
5
	stream.last_presence = nil;
6
 
7
	stream:hook("presence-out", function (presence)
8
		if not presence.attr.to then
9
			-- Clone so we don't add stuff over and over..?
10
			stream.last_presence = st.clone(presence); -- Cache non-directed presence
11
		end
12
	end, 1);
13
 
14
	function stream:resend_presence()
15
		if self.last_presence then
16
			stream:send(self.last_presence);
17
		end
18
	end
19
 
20
	function stream:set_status(opts)
21
		local p = verse.presence();
22
		if type(opts) == "table" then
23
			if opts.show then
24
				p:tag("show"):text(opts.show):up();
25
			end
26
			if opts.priority or opts.prio then
27
				p:tag("priority"):text(tostring(opts.priority or opts.prio)):up();
28
			end
29
			if opts.status or opts.msg then
30
				p:tag("status"):text(opts.status or opts.msg):up();
31
			end
32
		elseif type(opts) == "string" then
33
			p:tag("status"):text(opts):up();
34
		end
35
 
36
		stream:send(p);
37
	end
38
end