Changeset

99:c51402a40e41

Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
author Matthew Wild <mwild1@gmail.com>
date Sat, 30 Mar 2013 14:23:47 +0000
parents 98:5ad042476235
children 100:de5abce983d9
files clix/presence.lua
diffstat 1 files changed, 65 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/clix/presence.lua	Sat Mar 30 14:23:47 2013 +0000
@@ -0,0 +1,65 @@
+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];
+			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