Annotate

plugins/archive.lua @ 292:b674f5ca85d4

plugins.archive: Add local verse
author Kim Alvefur <zash@zash.se>
date Fri, 20 Apr 2012 00:33:18 +0200
parent 291:b78137f5118e
child 293:65fb9ae79014
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
291
b78137f5118e plugins.archive: Add a header comment
Kim Alvefur <zash@zash.se>
parents: 275
diff changeset
1 -- This implements XEP-0313: Message Archive Management
b78137f5118e plugins.archive: Add a header comment
Kim Alvefur <zash@zash.se>
parents: 275
diff changeset
2 -- http://xmpp.org/extensions/xep-0313.html
b78137f5118e plugins.archive: Add a header comment
Kim Alvefur <zash@zash.se>
parents: 275
diff changeset
3 -- (ie not XEP-0136)
b78137f5118e plugins.archive: Add a header comment
Kim Alvefur <zash@zash.se>
parents: 275
diff changeset
4
292
b674f5ca85d4 plugins.archive: Add local verse
Kim Alvefur <zash@zash.se>
parents: 291
diff changeset
5 local verse = require "verse";
273
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local xmlns_mam = "urn:xmpp:mam:tmp"
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local uuid = require "util.uuid".generate;
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9 function verse.plugins.archive(stream)
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 function stream:query_archive(where, query_params, callback)
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local queryid = uuid();
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local query_st = verse.iq{ type="get", to=where }
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 :tag("query", { xmlns = xmlns_mam, queryid = queryid });
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local params = { "with", "start", "end" };
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 local query_params = query_params or {};
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 for i=1,#params do
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local k = params[i];
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 if query_params[k] then
275
c077b20ad59f plugins.archive: Add missing :up()
Kim Alvefur <zash@zash.se>
parents: 273
diff changeset
20 query_st:tag(k):text(query_params[k]):up();
273
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 end
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 end
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local results = {};
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local function handle_archived_message(message)
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 local result_tag = message:get_child("result", xmlns_mam);
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 if result_tag and result_tag.attr.queryid == queryid then
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 local forwarded = message:get_child("forwarded", "urn:xmpp:forward:0");
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local delay = forwarded:get_child("delay", "urn:xmpp:delay");
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 local stamp = delay and delay.attr.stamp or nil;
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 local message = forwarded:get_child("message", "jabber:client")
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 results[#results+1] = { stamp = stamp, message = message };
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 return true
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 end
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 self:hook("message", handle_archived_message, 1);
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 self:send_iq(query_st, function(reply)
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 self:unhook("message", handle_archived_message);
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 callback(reply.attr.type == "result" and #results, results);
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 return true
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 end);
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 end
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 --TODO Settings
c5b7a4c717a6 plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end