clix/presence.lua

changeset 99
c51402a40e41
child 101
956242b7d5df
equal deleted inserted replaced
98:5ad042476235 99:c51402a40e41
1 local presence_values = { online = "1", chat = "2", away = "3", xa = "4", dnd = "5", offline = "6" };
2
3 return function (opts)
4 if opts.short_help then
5 print("Display Presence Status");
6 return;
7 end
8 local DELIM = opts.delim or "\t";
9 local function on_presence(presence)
10 local pres_num = presence_values.offline;
11 local pres_str;
12 if presence.attr.type then
13 if presence.attr.type == "unavailable" then
14 pres_str = "show=" .. presence.attr.type .. DELIM .. DELIM;
15 else
16 return; -- Ignore all other presence type's
17 end
18 else
19 local show = presence:get_child("show");
20 local priority = presence:get_child("priority");
21 local status = presence:get_child("status");
22 local show_str = show and show:get_text():lower() or "online";
23 pres_str = "show=" .. show_str;
24 pres_num = presence_values[show_str];
25 if priority then
26 pres_str = pres_str .. DELIM .. "priority=" .. priority:get_text();
27 else
28 pres_str = pres_str .. DELIM;
29 end
30 if status then
31 local status_str = status:get_text();
32 -- sanitize status string
33 status_str = status_str:gsub("\n", " "):gsub("[\r"..DELIM.."]", "");
34 pres_str = pres_str .. DELIM .. "status=" .. status_str;
35 else
36 pres_str = pres_str .. DELIM;
37 end
38 end
39 if presence.attr.from then
40 if opts.to then
41 if opts.to:find("/", 1, true) and opts.to ~= presence.attr.from then
42 return; -- Ignore non-exact match when --to= is defined with a resource
43 elseif opts.to ~= presence.attr.from:sub(1, #opts.to) then
44 return; -- Ignore non-matching JID when --to= is defined without a resource
45 end
46 end
47 pres_str = pres_str .. DELIM .. "from=" .. presence.attr.from;
48 else
49 pres_str = pres_str .. DELIM;
50 end
51 pres_str = pres_num .. DELIM .. pres_str .. DELIM;
52 print(pres_str);
53 end
54 local function on_connect(conn)
55 conn:hook("presence", on_presence);
56 if opts.to then
57 conn:send(verse.presence{to=opts.to, type="probe"});
58 end
59 if not tonumber(opts.close_delay) then
60 opts.close_delay = 1;
61 end
62 conn:close();
63 end
64 clix_connect(opts, on_connect);
65 end

mercurial