Annotate

plugins/carbons.lua @ 280:981c8867f1fb

verse: Add :listen(host, port) to Verse objects ("connected" event is fired with new clients)
author Matthew Wild <mwild1@gmail.com>
date Fri, 30 Mar 2012 03:09:59 +0100
parent 277:0d34cd815a93
child 285:99737531734c
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
224
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local xmlns_carbons = "urn:xmpp:carbons:1";
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";
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5 local os_date = os.date;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local datetime = function(t) return os_date("!%Y-%m-%dT%H:%M:%SZ", t); end
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)
277
0d34cd815a93 plugins.carbons: Fix wrong variable name
Kim Alvefur <zash@zash.se>
parents: 274
diff changeset
50 local carbon_dir = stanza:get_child(nil, xmlns_carbons);
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
51 if stanza.attr.from == my_bare and carbon_dir then
224
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 carbon_dir = carbon_dir and carbon_dir.name;
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
53 local fwd = stanza:get_child("forwarded", xmlns_forward);
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");
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
55 if fwd_stanza then
224
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 return stream:event("carbon", {
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 dir = carbon_dir,
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 stanza = fwd_stanza,
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
59 timestamp = nil or datetime(), -- TODO check for delay tag
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
60 });
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
63 end, 1);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
64 end