Mon, 16 Nov 2020 17:15:38 +0100
clix.watch_pep: Allow watching more than one PEP node
139
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 | local verse = require "verse"; |
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 | |
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 | return function (opts, arg) |
140
8815232cbbeb
clix.watch_pep: Take PEP node as positional argument
Kim Alvefur <zash@zash.se>
parents:
139
diff
changeset
|
4 | if opts.short_help or not arg[1] then |
139
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 | print("Watches PEP notifications from all contacts") |
141
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
6 | print("clix watch_pep [--noempty] PEP-NODE+") |
139
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 | return; |
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 | end |
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 | |
141
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
10 | local function handle_notification(event) |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
11 | local payload = event.item; |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
12 | if not payload then return end |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
13 | if opts.noempty and not payload[1] then return end |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
14 | if event.from then |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
15 | io.stderr:write("# From "..event.from.."\n"); |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
16 | end |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
17 | if payload.attr.xmlns == "urn:xmpp:json:0" then |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
18 | print(payload:get_tex()) |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
19 | elseif opts.pretty then |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
20 | print(payload:indent(nil, " ")); |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
21 | else |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
22 | print(payload); |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
23 | end |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
24 | end |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
25 | |
139
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 | local function onconnect(conn) |
141
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
27 | for _, node in ipairs(arg) do |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
28 | conn:hook_pep(node, handle_notification); |
93eda3dd85f7
clix.watch_pep: Allow watching more than one PEP node
Kim Alvefur <zash@zash.se>
parents:
140
diff
changeset
|
29 | end |
139
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 | if not opts.presence then |
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 | conn:send(verse.presence()); |
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 | end |
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 | end |
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 | |
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 | clix_connect(opts, onconnect, {"pep"}) |
6909e479c56b
clix.watch_pep: Subscribe to a PEP node
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 | end; |