Software /
code /
verse
Annotate
plugins/archive.lua @ 294:ac039aa3a4ef
plugins.archive: Implement archiving preferences
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 20 Apr 2012 01:36:13 +0200 |
parent | 293:65fb9ae79014 |
child | 295:5f7203bf1dae |
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"; |
293
65fb9ae79014
plugins.archive: Import util.stanza directly and use it
Kim Alvefur <zash@zash.se>
parents:
292
diff
changeset
|
6 local st = require "util.stanza"; |
273
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 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
|
8 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
|
9 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 function verse.plugins.archive(stream) |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 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
|
12 local queryid = uuid(); |
293
65fb9ae79014
plugins.archive: Import util.stanza directly and use it
Kim Alvefur <zash@zash.se>
parents:
292
diff
changeset
|
13 local query_st = st.iq{ type="get", to = where } |
273
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 :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
|
15 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local params = { "with", "start", "end" }; |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 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
|
18 for i=1,#params do |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local k = params[i]; |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 if query_params[k] then |
275
c077b20ad59f
plugins.archive: Add missing :up()
Kim Alvefur <zash@zash.se>
parents:
273
diff
changeset
|
21 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
|
22 end |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 end |
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 results = {}; |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 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
|
27 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
|
28 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
|
29 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
|
30 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 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
|
32 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
|
33 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 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
|
35 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 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
|
37 return true |
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 end |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 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
|
42 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
|
43 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
|
44 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
|
45 return true |
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 end |
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 |
294
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
49 local default_attrs = { |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
50 always = true, [true] = "always", |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
51 never = false, [false] = "never", |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
52 roster = "roster", |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
53 } |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
54 |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
55 local function prefs_decode(stanza) -- from XML |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
56 local prefs = {}; |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
57 local default = stanza.attr.default; |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
58 |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
59 if default then |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
60 prefs[false] = default_attrs[default]; |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
61 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
62 |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
63 local always = stanza:get_child("always"); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
64 if always then |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
65 for rule in always:childtags("jid") do |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
66 local jid = rule:get_text(); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
67 prefs[jid] = true; |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
68 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
69 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
70 |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
71 local never = stanza:get_child("never"); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
72 if never then |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
73 for rule in never:childtags("jid") do |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
74 local jid = rule:get_text(); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
75 prefs[jid] = false; |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
76 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
77 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
78 return prefs; |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
79 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
80 |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
81 local function prefs_encode(prefs) -- into XML |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
82 local default |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
83 default, prefs[false] = prefs[false], nil; |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
84 if default ~= nil then |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
85 default = default_attrs[default]; |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
86 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
87 local reply = st.stanza("prefs", { xmlns = xmlns_mam, default = default }) |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
88 local always = st.stanza("always"); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
89 local never = st.stanza("never"); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
90 for k,v in pairs(prefs) do |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
91 (v and always or never):tag("jid"):text(k):up(); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
92 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
93 return reply:add_child(always):add_child(never); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
94 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
95 |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
96 function stream:archive_prefs_get(callback) |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
97 self:send_iq(st.iq{ type="get" }:tag("prefs", { xmlns = xmlns_mam }), |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
98 function(result) |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
99 if result and result.attr.type == "result" and result.tags[1] then |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
100 local prefs = prefs_decode(result.tags[1]); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
101 callback(prefs, result); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
102 else |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
103 callback(nil, result); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
104 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
105 end); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
106 end |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
107 |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
108 function stream:archive_prefs_set(prefs, callback) |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
109 self:send_iq(st.iq{ type="set" }:add_child(prefs_encode(prefs)), callback); |
ac039aa3a4ef
plugins.archive: Implement archiving preferences
Kim Alvefur <zash@zash.se>
parents:
293
diff
changeset
|
110 end |
273
c5b7a4c717a6
plugins.archive: Experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 end |