Software /
code /
verse
Annotate
plugins/carbons.lua @ 250:a5ac643a7fd6
added local verse var to all plugins
author | mva <mva@mva.name> |
---|---|
date | Fri, 02 Dec 2011 11:02:02 +0600 |
parent | 224:7fc17e40fbaf |
child | 274:a228f3c7808a |
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 |
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 -- TODO Check disco for support |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 function verse.plugins.carbons(stream) |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local carbons = {}; |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 carbons.enabled = false; |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 stream.carbons = carbons; |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 function carbons:enable(callback) |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 stream:send_iq(verse.iq{type="set"} |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 :tag("enable", { xmlns = xmlns_carbons }) |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 , function(result) |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local success = result.attr.type == "result"; |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 if success then |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 carbons.enabled = true; |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 end |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 if callback then |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 callback(success); |
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 end or nil); |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 end |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 function carbons:disable(callback) |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 stream:send_iq(verse.iq{type="set"} |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 :tag("disable", { xmlns = xmlns_carbons }) |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 , function(result) |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 local success = result.attr.type == "result"; |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 if success then |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 carbons.enabled = false; |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 end |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 if callback then |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 callback(success); |
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 end or nil); |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 end |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 stream:hook("message", function(stanza) |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 stream:debug(stanza); |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 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
|
46 if fwd then |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 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
|
48 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
|
49 if carbon_dir then |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 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
|
51 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
|
52 return stream:event("carbon", { |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 dir = carbon_dir, |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 stanza = fwd_stanza, |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 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
|
56 }); |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 end |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 end |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 end, 1); |
7fc17e40fbaf
plugins.carbons: Add, implements Message Carbons (XEP 280)
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 end |