clix/presence.lua

Wed, 01 Dec 2021 17:25:25 +0100

author
Kim Alvefur <zash@zash.se>
date
Wed, 01 Dec 2021 17:25:25 +0100
changeset 164
fafdcde2e2eb
parent 101
956242b7d5df
permissions
-rw-r--r--

clix: Import Verse where needed

In the olden days of `module()` this would have been a global, but that
is no longer the convention nor the case.

164
fafdcde2e2eb clix: Import Verse where needed
Kim Alvefur <zash@zash.se>
parents: 101
diff changeset
1 local verse = require "verse";
99
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local presence_values = { online = "1", chat = "2", away = "3", xa = "4", dnd = "5", offline = "6" };
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
4 return function (opts)
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 if opts.short_help then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6 print("Display Presence Status");
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 return;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
9 local DELIM = opts.delim or "\t";
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 local function on_presence(presence)
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 local pres_num = presence_values.offline;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 local pres_str;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 if presence.attr.type then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 if presence.attr.type == "unavailable" then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 pres_str = "show=" .. presence.attr.type .. DELIM .. DELIM;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 else
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 return; -- Ignore all other presence type's
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 else
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 local show = presence:get_child("show");
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 local priority = presence:get_child("priority");
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 local status = presence:get_child("status");
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 local show_str = show and show:get_text():lower() or "online";
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 pres_str = "show=" .. show_str;
101
956242b7d5df clix/presence: Handle invalid <show> values (thanks Lonnie Abelbeck)
Matthew Wild <mwild1@gmail.com>
parents: 99
diff changeset
25 pres_num = presence_values[show_str] or pres_num;
99
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 if priority then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 pres_str = pres_str .. DELIM .. "priority=" .. priority:get_text();
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 else
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29 pres_str = pres_str .. DELIM;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 if status then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local status_str = status:get_text();
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 -- sanitize status string
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 status_str = status_str:gsub("\n", " "):gsub("[\r"..DELIM.."]", "");
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 pres_str = pres_str .. DELIM .. "status=" .. status_str;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 else
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 pres_str = pres_str .. DELIM;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 if presence.attr.from then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 if opts.to then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 if opts.to:find("/", 1, true) and opts.to ~= presence.attr.from then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
43 return; -- Ignore non-exact match when --to= is defined with a resource
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
44 elseif opts.to ~= presence.attr.from:sub(1, #opts.to) then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
45 return; -- Ignore non-matching JID when --to= is defined without a resource
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
46 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
47 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
48 pres_str = pres_str .. DELIM .. "from=" .. presence.attr.from;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
49 else
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
50 pres_str = pres_str .. DELIM;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
51 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
52 pres_str = pres_num .. DELIM .. pres_str .. DELIM;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
53 print(pres_str);
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
54 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
55 local function on_connect(conn)
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 conn:hook("presence", on_presence);
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57 if opts.to then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 conn:send(verse.presence{to=opts.to, type="probe"});
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 if not tonumber(opts.close_delay) then
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 opts.close_delay = 1;
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
63 conn:close();
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
64 end
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
65 clix_connect(opts, on_connect);
c51402a40e41 Add clix/presence.lua: list the presence of your contacts (or probe one with --to). Contributed by Lonnie Abelbeck
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
66 end

mercurial