Software /
code /
verse
Comparison
plugins/carbons.lua @ 224:7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Wed, 02 Nov 2011 01:17:54 +0100 |
child | 250:a5ac643a7fd6 |
comparison
equal
deleted
inserted
replaced
223:30e66eabffa9 | 224:7fc17e40fbaf |
---|---|
1 local xmlns_carbons = "urn:xmpp:carbons:1"; | |
2 local xmlns_forward = "urn:xmpp:forward:0"; | |
3 local os_date = os.date; | |
4 local datetime = function(t) return os_date("!%Y-%m-%dT%H:%M:%SZ", t); end | |
5 | |
6 -- TODO Check disco for support | |
7 | |
8 function verse.plugins.carbons(stream) | |
9 local carbons = {}; | |
10 carbons.enabled = false; | |
11 stream.carbons = carbons; | |
12 | |
13 function carbons:enable(callback) | |
14 stream:send_iq(verse.iq{type="set"} | |
15 :tag("enable", { xmlns = xmlns_carbons }) | |
16 , function(result) | |
17 local success = result.attr.type == "result"; | |
18 if success then | |
19 carbons.enabled = true; | |
20 end | |
21 if callback then | |
22 callback(success); | |
23 end | |
24 end or nil); | |
25 end | |
26 | |
27 function carbons:disable(callback) | |
28 stream:send_iq(verse.iq{type="set"} | |
29 :tag("disable", { xmlns = xmlns_carbons }) | |
30 , function(result) | |
31 local success = result.attr.type == "result"; | |
32 if success then | |
33 carbons.enabled = false; | |
34 end | |
35 if callback then | |
36 callback(success); | |
37 end | |
38 end or nil); | |
39 end | |
40 | |
41 stream:hook("message", function(stanza) | |
42 stream:debug(stanza); | |
43 local fwd = stanza:get_child("forwarded", xmlns_forward); | |
44 if fwd then | |
45 local carbon_dir = fwd:get_child(nil, xmlns_carbons); | |
46 carbon_dir = carbon_dir and carbon_dir.name; | |
47 if carbon_dir then | |
48 local fwd_stanza = fwd:get_child("message", "jabber:client"); | |
49 assert(fwd_stanza, "No stanza included.\n"..tostring(stanza).."\n--\n"..tostring(fwd_stanza)); | |
50 return stream:event("carbon", { | |
51 dir = carbon_dir, | |
52 stanza = fwd_stanza, | |
53 timestamp = nil or datetime(), -- TODO check for delay tag | |
54 }); | |
55 end | |
56 end | |
57 end, 1); | |
58 end |