clix/presence.lua

Mon, 16 Nov 2020 17:16:01 +0100

author
Kim Alvefur <zash@zash.se>
date
Mon, 16 Nov 2020 17:16:01 +0100
changeset 142
05ec7103c0f7
parent 101
956242b7d5df
child 164
fafdcde2e2eb
permissions
-rw-r--r--

clix.watch_pep: Include short names for some common PEP nodes

local presence_values = { online = "1", chat = "2", away = "3", xa = "4", dnd = "5", offline = "6" };

return function (opts)
	if opts.short_help then
		print("Display Presence Status");
		return;
	end
	local DELIM = opts.delim or "\t";
	local function on_presence(presence)
		local pres_num = presence_values.offline;
		local pres_str;
		if presence.attr.type then
			if presence.attr.type == "unavailable" then
				pres_str = "show=" .. presence.attr.type .. DELIM .. DELIM;
			else
				return;	-- Ignore all other presence type's
			end
		else
			local show = presence:get_child("show");
			local priority = presence:get_child("priority");
			local status = presence:get_child("status");
			local show_str = show and show:get_text():lower() or "online";
			pres_str = "show=" .. show_str;
			pres_num = presence_values[show_str] or pres_num;
			if priority then
				pres_str = pres_str .. DELIM .. "priority=" .. priority:get_text();
			else
				pres_str = pres_str .. DELIM;
			end
			if status then
				local status_str = status:get_text();
				-- sanitize status string
				status_str = status_str:gsub("\n", " "):gsub("[\r"..DELIM.."]", "");
				pres_str = pres_str .. DELIM .. "status=" .. status_str;
			else
				pres_str = pres_str .. DELIM;
			end
		end
		if presence.attr.from then
			if opts.to then
				if opts.to:find("/", 1, true) and opts.to ~= presence.attr.from then
					return;	-- Ignore non-exact match when --to= is defined with a resource
				elseif opts.to ~= presence.attr.from:sub(1, #opts.to) then
					return;	-- Ignore non-matching JID when --to= is defined without a resource
				end
			end
			pres_str = pres_str .. DELIM .. "from=" .. presence.attr.from;
		else
			pres_str = pres_str .. DELIM;
		end
		pres_str = pres_num .. DELIM .. pres_str .. DELIM;
		print(pres_str);
	end
	local function on_connect(conn)
		conn:hook("presence", on_presence);
		if opts.to then
			conn:send(verse.presence{to=opts.to, type="probe"});
		end
		if not tonumber(opts.close_delay) then
			opts.close_delay = 1;
		end
		conn:close();
	end
	clix_connect(opts, on_connect);
end

mercurial