10 }; |
10 }; |
11 --]] |
11 --]] |
12 |
12 |
13 local verse = require "verse"; |
13 local verse = require "verse"; |
14 local st = require "util.stanza"; |
14 local st = require "util.stanza"; |
|
15 local json = require "util.json"; |
15 |
16 |
16 -- FIXME Should this really be here? |
17 -- FIXME Should this really be here? |
17 local extractor_mt = { |
18 local extractor_mt = { |
18 __index = function (t, k) |
19 __index = function (t, k) |
19 local n; |
20 local n; |
34 end |
35 end |
35 return n[1]; |
36 return n[1]; |
36 end |
37 end |
37 }; |
38 }; |
38 |
39 |
|
40 local json_extractor_mt = { |
|
41 __index = function (t, k) |
|
42 if not k:find"%." then |
|
43 return nil; |
|
44 end |
|
45 for x in k:gmatch("[^.]+") do |
|
46 t = rawget(t, x) or rawget(t, tonumber(x)); |
|
47 if t == nil then return end |
|
48 end |
|
49 if type(t) == "table" then |
|
50 return json.encode(t); |
|
51 end |
|
52 return tostring(t); |
|
53 end |
|
54 }; |
|
55 |
39 local function new_extractor(stanza, data) |
56 local function new_extractor(stanza, data) |
40 if not stanza then return nil end |
57 if not stanza then return nil end |
|
58 if stanza.attr.xmlns == "urn:xmpp:json:0" then |
|
59 local payload = json.decode(stanza:get_text()); |
|
60 if type(payload) == "table" then |
|
61 return setmetatable(payload, json_extractor_mt); |
|
62 end |
|
63 end |
41 return setmetatable({ stanza = stanza, data = data }, extractor_mt); |
64 return setmetatable({ stanza = stanza, data = data }, extractor_mt); |
42 end |
65 end |
43 |
66 |
44 local last_message_time = 0; |
67 local last_message_time = 0; |
45 |
68 |