Software /
code /
verse
Annotate
plugins/carbons.lua @ 317:38562b0b50f2
util.vcard: Don't break on data outside of VCARD sections (thanks moo-min)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 17 Dec 2012 12:47:25 +0100 |
parent | 286:04b6c060ff01 |
child | 318:598552fc085b |
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"; |
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) |
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 |
285
99737531734c
plugins.carbons: Don't check carbons flag twice.
Kim Alvefur <zash@zash.se>
parents:
277
diff
changeset
|
52 carbon_dir = 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"); |
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 |