Software /
code /
verse
Comparison
plugins/archive.lua @ 353:8cd05c3d0f1f
plugins.archive: Update to MAM version 0.3
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 08 Sep 2014 12:10:21 +0200 |
parent | 345:266a96ae4c0d |
child | 380:0891b4e27766 |
comparison
equal
deleted
inserted
replaced
352:413e3f449865 | 353:8cd05c3d0f1f |
---|---|
2 -- http://xmpp.org/extensions/xep-0313.html | 2 -- http://xmpp.org/extensions/xep-0313.html |
3 -- (ie not XEP-0136) | 3 -- (ie not XEP-0136) |
4 | 4 |
5 local verse = require "verse"; | 5 local verse = require "verse"; |
6 local st = require "util.stanza"; | 6 local st = require "util.stanza"; |
7 local xmlns_mam = "urn:xmpp:mam:tmp" | 7 local xmlns_mam = "urn:xmpp:mam:0" |
8 local xmlns_forward = "urn:xmpp:forward:0"; | 8 local xmlns_forward = "urn:xmpp:forward:0"; |
9 local xmlns_delay = "urn:xmpp:delay"; | 9 local xmlns_delay = "urn:xmpp:delay"; |
10 local uuid = require "util.uuid".generate; | 10 local uuid = require "util.uuid".generate; |
11 local parse_datetime = require "util.datetime".parse; | 11 local parse_datetime = require "util.datetime".parse; |
12 local datetime = require "util.datetime".datetime; | 12 local datetime = require "util.datetime".datetime; |
13 local dataform = require"util.dataforms".new; | |
13 local rsm = require "util.rsm"; | 14 local rsm = require "util.rsm"; |
14 local tonumber = tonumber; | |
15 local NULL = {}; | 15 local NULL = {}; |
16 | |
17 local query_form = dataform { | |
18 { name = "FORM_TYPE"; type = "hidden"; value = xmlns_mam; }; | |
19 { name = "with"; type = "jid-single"; }; | |
20 { name = "start"; type = "text-single" }; | |
21 { name = "end"; type = "text-single"; }; | |
22 }; | |
16 | 23 |
17 function verse.plugins.archive(stream) | 24 function verse.plugins.archive(stream) |
18 function stream:query_archive(where, query_params, callback) | 25 function stream:query_archive(where, query_params, callback) |
19 local queryid = uuid(); | 26 local queryid = uuid(); |
20 local query_st = st.iq{ type="get", to = where } | 27 local query_st = st.iq{ type="set", to = where } |
21 :tag("query", { xmlns = xmlns_mam, queryid = queryid }); | 28 :tag("query", { xmlns = xmlns_mam, queryid = queryid }); |
22 | 29 |
23 local qwith = query_params["with"]; | 30 |
24 if qwith then | 31 local qstart, qend = tonumber(query_params["start"]), tonumber(query_params["end"]); |
25 query_st:tag("with"):text(qwith):up(); | 32 query_params["start"] = qstart and datetime(qstart); |
26 end | 33 query_params["end"] = qend and datetime(qend); |
27 | 34 |
28 local qstart, qend = tonumber(query_params["start"]), tonumber(query_params["end"]); | 35 query_st:add_child(query_form:form(query_params, "submit")); |
29 if qstart then | 36 -- query_st:up(); |
30 query_st:tag("start"):text(datetime(qstart)):up(); | |
31 end | |
32 if qend then | |
33 query_st:tag("end"):text(datetime(qend)):up(); | |
34 end | |
35 | |
36 query_st:add_child(rsm.generate(query_params)); | 37 query_st:add_child(rsm.generate(query_params)); |
37 | 38 |
38 local results = {}; | 39 local results = {}; |
39 local function handle_archived_message(message) | 40 local function handle_archived_message(message) |
41 | |
42 local finnished = message:get_child("fin", xmlns_mam) | |
43 if finnished and finnished.attr.queryid == queryid then | |
44 local rset = rsm.get(finnished); | |
45 for k,v in pairs(rset or NULL) do results[k]=v; end | |
46 self:unhook("message", handle_archived_message); | |
47 callback(results); | |
48 return true | |
49 end | |
40 local result_tag = message:get_child("result", xmlns_mam); | 50 local result_tag = message:get_child("result", xmlns_mam); |
41 if result_tag and result_tag.attr.queryid == queryid then | 51 if result_tag and result_tag.attr.queryid == queryid then |
42 local forwarded = result_tag:get_child("forwarded", xmlns_forward); | 52 local forwarded = result_tag:get_child("forwarded", xmlns_forward); |
43 forwarded = forwarded or message:get_child("forwarded", xmlns_forward); -- COMPAT XEP-0313 pre 2013-05-31 | 53 forwarded = forwarded or message:get_child("forwarded", xmlns_forward); -- COMPAT XEP-0313 pre 2013-05-31 |
44 | 54 |
53 end | 63 end |
54 end | 64 end |
55 | 65 |
56 self:hook("message", handle_archived_message, 1); | 66 self:hook("message", handle_archived_message, 1); |
57 self:send_iq(query_st, function(reply) | 67 self:send_iq(query_st, function(reply) |
58 self:unhook("message", handle_archived_message); | 68 if reply.attr.type == "error" then |
59 local rset = reply.tags[1] and rsm.get(reply.tags[1]); | 69 self:warn(table.concat({reply:get_error()}, " ")) |
60 for k,v in pairs(rset or NULL) do results[k]=v; end | 70 self:unhook("message", handle_archived_message); |
61 callback(reply.attr.type == "result" and #results, results); | 71 callback(false, reply:get_error()) |
72 end | |
62 return true | 73 return true |
63 end); | 74 end); |
64 end | 75 end |
65 | 76 |
66 local default_attrs = { | 77 local default_attrs = { |