Annotate

plugins/carbons.lua @ 465:6707e3a47f71

Add 'shutdown' event for a self-initiated disconnect
author Matthew Wild <mwild1@gmail.com>
date Fri, 17 Mar 2023 09:23:15 +0000
parent 318:598552fc085b
child 490:6b2f31da9610
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
250
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 224
diff changeset
1 local verse = require "verse";
a5ac643a7fd6 added local verse var to all plugins
mva <mva@mva.name>
parents: 224
diff changeset
2
318
598552fc085b plugins.carbons: Update to Carbons v0.8
Kim Alvefur <zash@zash.se>
parents: 286
diff changeset
3 local xmlns_carbons = "urn:xmpp:carbons:2";
224
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local xmlns_forward = "urn:xmpp:forward:0";
286
04b6c060ff01 plugins.carbons: Get and parse timestamp from carbon into a number.
Kim Alvefur <zash@zash.se>
parents: 285
diff changeset
5 local os_time = os.time;
04b6c060ff01 plugins.carbons: Get and parse timestamp from carbon into a number.
Kim Alvefur <zash@zash.se>
parents: 285
diff changeset
6 local parse_datetime = require "util.datetime".parse;
274
a228f3c7808a plugins.carbons: Update to XEP v0.6. Check that messages are from your bare jid.
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
7 local bare_jid = require "util.jid".bare;
224
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 -- TODO Check disco for support
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 function verse.plugins.carbons(stream)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local carbons = {};
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 carbons.enabled = false;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 stream.carbons = carbons;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 function carbons:enable(callback)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 stream:send_iq(verse.iq{type="set"}
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 :tag("enable", { xmlns = xmlns_carbons })
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 , function(result)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local success = result.attr.type == "result";
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 if success then
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 carbons.enabled = true;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 if callback then
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 callback(success);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 end or nil);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 function carbons:disable(callback)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 stream:send_iq(verse.iq{type="set"}
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 :tag("disable", { xmlns = xmlns_carbons })
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 , function(result)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local success = result.attr.type == "result";
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 if success then
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 carbons.enabled = false;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 if callback then
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 callback(success);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end or nil);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
274
a228f3c7808a plugins.carbons: Update to XEP v0.6. Check that messages are from your bare jid.
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
44 local my_bare;
a228f3c7808a plugins.carbons: Update to XEP v0.6. Check that messages are from your bare jid.
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
45 stream:hook("bind-success", function()
a228f3c7808a plugins.carbons: Update to XEP v0.6. Check that messages are from your bare jid.
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
46 my_bare = bare_jid(stream.jid);
a228f3c7808a plugins.carbons: Update to XEP v0.6. Check that messages are from your bare jid.
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
47 end);
a228f3c7808a plugins.carbons: Update to XEP v0.6. Check that messages are from your bare jid.
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
48
224
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 stream:hook("message", function(stanza)
318
598552fc085b plugins.carbons: Update to Carbons v0.8
Kim Alvefur <zash@zash.se>
parents: 286
diff changeset
50 local carbon = stanza:get_child(nil, xmlns_carbons);
598552fc085b plugins.carbons: Update to Carbons v0.8
Kim Alvefur <zash@zash.se>
parents: 286
diff changeset
51 if stanza.attr.from == my_bare and carbon then
598552fc085b plugins.carbons: Update to Carbons v0.8
Kim Alvefur <zash@zash.se>
parents: 286
diff changeset
52 local carbon_dir = carbon.name;
598552fc085b plugins.carbons: Update to Carbons v0.8
Kim Alvefur <zash@zash.se>
parents: 286
diff changeset
53 local fwd = carbon:get_child("forwarded", xmlns_forward);
274
a228f3c7808a plugins.carbons: Update to XEP v0.6. Check that messages are from your bare jid.
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
54 local fwd_stanza = fwd and fwd:get_child("message", "jabber:client");
286
04b6c060ff01 plugins.carbons: Get and parse timestamp from carbon into a number.
Kim Alvefur <zash@zash.se>
parents: 285
diff changeset
55 local delay = fwd:get_child("delay", "urn:xmpp:delay");
04b6c060ff01 plugins.carbons: Get and parse timestamp from carbon into a number.
Kim Alvefur <zash@zash.se>
parents: 285
diff changeset
56 local stamp = delay and delay.attr.stamp;
04b6c060ff01 plugins.carbons: Get and parse timestamp from carbon into a number.
Kim Alvefur <zash@zash.se>
parents: 285
diff changeset
57 stamp = stamp and parse_datetime(stamp);
274
a228f3c7808a plugins.carbons: Update to XEP v0.6. Check that messages are from your bare jid.
Kim Alvefur <zash@zash.se>
parents: 250
diff changeset
58 if fwd_stanza then
224
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 return stream:event("carbon", {
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 dir = carbon_dir,
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 stanza = fwd_stanza,
286
04b6c060ff01 plugins.carbons: Get and parse timestamp from carbon into a number.
Kim Alvefur <zash@zash.se>
parents: 285
diff changeset
62 timestamp = stamp or os_time(),
224
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 });
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
65 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
66 end, 1);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67 end