Software /
code /
verse
Annotate
plugins/carbons.lua @ 274:a228f3c7808a
plugins.carbons: Update to XEP v0.6. Check that messages are from your bare jid.
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 10 Jan 2012 21:08:19 +0100 |
parent | 250:a5ac643a7fd6 |
child | 277:0d34cd815a93 |
rev | line source |
---|---|
250 | 1 local verse = require "verse"; |
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) |
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
|
50 local carbon_dir = fwd:get_child(nil, xmlns_carbons); |
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 |