Annotate

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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
224
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 local xmlns_carbons = "urn:xmpp:carbons:1";
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 local xmlns_forward = "urn:xmpp:forward:0";
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 local os_date = os.date;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 local datetime = function(t) return os_date("!%Y-%m-%dT%H:%M:%SZ", t); end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 -- TODO Check disco for support
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 function verse.plugins.carbons(stream)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 local carbons = {};
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 carbons.enabled = false;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 stream.carbons = carbons;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 function carbons:enable(callback)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 stream:send_iq(verse.iq{type="set"}
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 :tag("enable", { xmlns = xmlns_carbons })
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 , function(result)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local success = result.attr.type == "result";
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 if success then
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 carbons.enabled = true;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 if callback then
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 callback(success);
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 end or nil);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 function carbons:disable(callback)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 stream:send_iq(verse.iq{type="set"}
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 :tag("disable", { xmlns = xmlns_carbons })
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 , function(result)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local success = result.attr.type == "result";
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 if success then
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 carbons.enabled = false;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 if callback then
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 callback(success);
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 end or nil);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 stream:hook("message", function(stanza)
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 stream:debug(stanza);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 local fwd = stanza:get_child("forwarded", xmlns_forward);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 if fwd then
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 local carbon_dir = fwd:get_child(nil, xmlns_carbons);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 carbon_dir = carbon_dir and carbon_dir.name;
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 if carbon_dir then
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 local fwd_stanza = fwd:get_child("message", "jabber:client");
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 assert(fwd_stanza, "No stanza included.\n"..tostring(stanza).."\n--\n"..tostring(fwd_stanza));
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50 return stream:event("carbon", {
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
51 dir = carbon_dir,
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 stanza = fwd_stanza,
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
53 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
54 });
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 end
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 end, 1);
7fc17e40fbaf plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 end