Software /
code /
verse
Annotate
plugins/archive.lua @ 273:c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 06 Jan 2012 16:54:10 +0100 |
child | 275:c077b20ad59f |
rev | line source |
---|---|
273
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 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
|
2 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
|
3 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 function verse.plugins.archive(stream) |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 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
|
6 local queryid = uuid(); |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 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
|
8 :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
|
9 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local params = { "with", "start", "end" }; |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 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
|
12 for i=1,#params do |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local k = params[i]; |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 if query_params[k] then |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 query_st:tag(k):text(query_params[k]); |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 end |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 end |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local results = {}; |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 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
|
21 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
|
22 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
|
23 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
|
24 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 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
|
26 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
|
27 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 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
|
29 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 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
|
31 return true |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 end |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 end |
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 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
|
36 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
|
37 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
|
38 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
|
39 return true |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 end); |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 end |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 --TODO Settings |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 end |