Software /
code /
prosody-modules
Annotate
mod_mam/mod_mam.lua @ 1135:0d6ab5e4bc30
mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 10 Aug 2013 20:38:25 +0200 |
parent | 1116:2345a30dd8b4 |
child | 1149:d055c44a7f61 |
rev | line source |
---|---|
635 | 1 -- XEP-0313: Message Archive Management for Prosody |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 -- Copyright (C) 2011-2012 Kim Alvefur |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- This file is MIT/X11 licensed. |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
6 local xmlns_mam = "urn:xmpp:mam:tmp"; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
7 local xmlns_delay = "urn:xmpp:delay"; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
8 local xmlns_forward = "urn:xmpp:forward:0"; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
9 |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local st = require "util.stanza"; |
701
cc5805f83583
mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
675
diff
changeset
|
11 local rsm = module:require "rsm"; |
1111
bdbf76730f49
mod_mam: Split out preference functions into a lib for easy reuse
Kim Alvefur <zash@zash.se>
parents:
1094
diff
changeset
|
12 local prefs = module:require"mamprefs"; |
1135
0d6ab5e4bc30
mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
Kim Alvefur <zash@zash.se>
parents:
1116
diff
changeset
|
13 local prefsxml = module:require"mamprefsxml"; |
1111
bdbf76730f49
mod_mam: Split out preference functions into a lib for easy reuse
Kim Alvefur <zash@zash.se>
parents:
1094
diff
changeset
|
14 local set_prefs, get_prefs = prefs.set, prefs.get; |
1135
0d6ab5e4bc30
mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
Kim Alvefur <zash@zash.se>
parents:
1116
diff
changeset
|
15 local prefs_to_stanza, prefs_from_stanza = prefsxml.tostanza, prefsxml.fromstanza; |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 local jid_bare = require "util.jid".bare; |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local jid_split = require "util.jid".split; |
751
3c37445f26ac
mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents:
711
diff
changeset
|
18 local jid_prep = require "util.jid".prep; |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
19 local host = module.host; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
20 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
21 local rm_load_roster = require "core.rostermanager".load_roster; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
22 |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
23 local getmetatable = getmetatable; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
24 local function is_stanza(x) |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
25 return getmetatable(x) == st.stanza_mt; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
26 end |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
27 |
672
8ae5317ba032
mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents:
671
diff
changeset
|
28 local tostring = tostring; |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 local time_now = os.time; |
707 | 30 local m_min = math.min; |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse; |
706
5c2b96c4dde6
mod_mam: Enforce a max number of items returned, with a default.
Kim Alvefur <zash@zash.se>
parents:
705
diff
changeset
|
32 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50); |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
33 local global_default_policy = module:get_option("default_archive_policy", false); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
34 -- TODO Should be possible to enforce it too |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 |
672
8ae5317ba032
mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents:
671
diff
changeset
|
36 -- For translating preference names from string to boolean and back |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
37 local default_attrs = { |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
38 always = true, [true] = "always", |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
39 never = false, [false] = "never", |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
40 roster = "roster", |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
41 } |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
42 |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
43 |
675
da33325453fb
mod_mam: Put name of store in a single variable
Kim Alvefur <zash@zash.se>
parents:
674
diff
changeset
|
44 local archive_store = "archive2"; |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
45 local archive = module:open_store(archive_store, "archive"); |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
46 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
47 -- Handle prefs. |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 module:hook("iq/self/"..xmlns_mam..":prefs", function(event) |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 local origin, stanza = event.origin, event.stanza; |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
50 local user = origin.username; |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 if stanza.attr.type == "get" then |
1135
0d6ab5e4bc30
mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
Kim Alvefur <zash@zash.se>
parents:
1116
diff
changeset
|
52 local prefs = prefs_to_stanza(get_prefs(user)); |
0d6ab5e4bc30
mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
Kim Alvefur <zash@zash.se>
parents:
1116
diff
changeset
|
53 local reply = st.reply(stanza):add_child(prefs); |
0d6ab5e4bc30
mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
Kim Alvefur <zash@zash.se>
parents:
1116
diff
changeset
|
54 return origin.send(reply); |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 else -- type == "set" |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
56 local new_prefs = stanza:get_child("prefs", xmlns_mam); |
1135
0d6ab5e4bc30
mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
Kim Alvefur <zash@zash.se>
parents:
1116
diff
changeset
|
57 local prefs = prefs_from_stanza(new_prefs); |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
58 local ok, err = set_prefs(user, prefs); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
59 if not ok then |
1135
0d6ab5e4bc30
mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
Kim Alvefur <zash@zash.se>
parents:
1116
diff
changeset
|
60 return origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err))); |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
61 end |
1135
0d6ab5e4bc30
mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
Kim Alvefur <zash@zash.se>
parents:
1116
diff
changeset
|
62 return origin.send(st.reply(stanza)); |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 end); |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
66 -- Handle archive queries |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
67 module:hook("iq-get/self/"..xmlns_mam..":query", function(event) |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 local origin, stanza = event.origin, event.stanza; |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 local query = stanza.tags[1]; |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
70 local qid = query.attr.queryid; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
71 |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
72 -- Search query parameters |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
73 local qwith = query:get_child_text("with"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
74 local qstart = query:get_child_text("start"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
75 local qend = query:get_child_text("end"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
76 module:log("debug", "Archive query, id %s with %s from %s until %s)", |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
77 tostring(qid), qwith or "anyone", qstart or "the dawn of time", qend or "now"); |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
79 if qstart or qend then -- Validate timestamps |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
80 local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend)) |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
81 if (qstart and not vstart) or (qend and not vend) then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
82 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp")) |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
83 return true |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
84 end |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
85 qstart, qend = vstart, vend; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
86 end |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
87 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
88 if qwith then -- Validate the 'with' jid |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
89 local pwith = qwith and jid_prep(qwith); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
90 if pwith and not qwith then -- it failed prepping |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
91 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid JID")) |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
92 return true |
751
3c37445f26ac
mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents:
711
diff
changeset
|
93 end |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
94 qwith = jid_bare(pwith); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
95 end |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
97 -- RSM stuff |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
98 local qset = rsm.get(query); |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
99 local qmax = m_min(qset and qset.max or default_max_items, max_max_items); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
100 local reverse = qset and qset.before or false; |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
101 local before, after = qset and qset.before, qset and qset.after; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
102 if type(before) ~= "string" then before = nil; end |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
103 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
104 |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
105 -- Load all the data! |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
106 local data, err = archive:find(origin.username, { |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
107 start = qstart; ["end"] = qend; -- Time range |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
108 with = qwith; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
109 limit = qmax; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
110 before = before; after = after; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
111 reverse = reverse; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
112 total = true; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
113 }); |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
114 |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
115 if not data then |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
116 return origin.send(st.error_reply(stanza, "cancel", "internal-server-error")); |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
117 end |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
118 local count = err; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
119 |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
120 -- Wrap it in stuff and deliver |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
121 local first, last; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
122 for id, item, when in data do |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
123 local fwd_st = st.message{ to = origin.full_jid } |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
124 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }) |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
125 :tag("forwarded", { xmlns = xmlns_forward }) |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
126 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up(); |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
127 |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
128 if not is_stanza(item) then |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
129 item = st.deserialize(item); |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
130 end |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
131 item.attr.xmlns = "jabber:client"; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
132 fwd_st:add_child(item); |
701
cc5805f83583
mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
675
diff
changeset
|
133 |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
134 if not first then first = id; end |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
135 last = id; |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
136 |
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
137 origin.send(fwd_st); |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
138 end |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
139 -- That's all folks! |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
140 module:log("debug", "Archive query %s completed", tostring(qid)); |
705
c9d0ba39a33b
mod_mam: Move RSM pointer to last message into a MAM-namespaced child
Kim Alvefur <zash@zash.se>
parents:
702
diff
changeset
|
141 |
1114
6c0e1f9926f6
mod_mam: Swap first and last first for a simpler argument to RSM in reverse queries
Kim Alvefur <zash@zash.se>
parents:
1113
diff
changeset
|
142 if reverse then first, last = last, first; end |
1113 | 143 return origin.send(st.reply(stanza) |
144 :query(xmlns_mam):add_child(rsm.generate { | |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
145 first = first, last = last, count = count })); |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
146 end); |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
148 local function has_in_roster(user, who) |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
149 local roster = rm_load_roster(user, host); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
150 module:log("debug", "%s has %s in roster? %s", user, who, roster[who] and "yes" or "no"); |
798 | 151 return roster[who]; |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
152 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
153 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
154 local function shall_store(user, who) |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
155 -- TODO Cache this? |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
156 local prefs = get_prefs(user); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
157 local rule = prefs[who]; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
158 module:log("debug", "%s's rule for %s is %s", user, who, tostring(rule)) |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
159 if rule ~= nil then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
160 return rule; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
161 else -- Below could be done by a metatable |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
162 local default = prefs[false]; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
163 module:log("debug", "%s's default rule is %s", user, tostring(default)) |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
164 if default == nil then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
165 default = global_default_policy; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
166 module:log("debug", "Using global default rule, %s", tostring(default)) |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
167 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
168 if default == "roster" then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
169 return has_in_roster(user, who); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
170 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
171 return default; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
172 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
173 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
174 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
175 -- Handle messages |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
176 local function message_handler(event, c2s) |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 local origin, stanza = event.origin, event.stanza; |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
178 local orig_type = stanza.attr.type or "normal"; |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
179 local orig_to = stanza.attr.to; |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
180 local orig_from = stanza.attr.from; |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
181 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
182 if not orig_from and c2s then |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
183 orig_from = origin.full_jid; |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
184 end |
619
cade7dac6159
mod_mam: Try to not bork when sending a message to your bare jid
Kim Alvefur <zash@zash.se>
parents:
559
diff
changeset
|
185 orig_to = orig_to or orig_from; -- Weird corner cases |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
186 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
187 -- Don't store messages of these types |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
188 if orig_type == "error" |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
189 or orig_type == "headline" |
671
74efb2db00a6
mod_mam: From the spec: servers SHOULD NOT archive messages that do not have a <body/> child tag.
Thijs Alkemade <thijsalkemade@gmail.com>
parents:
666
diff
changeset
|
190 or orig_type == "groupchat" |
74efb2db00a6
mod_mam: From the spec: servers SHOULD NOT archive messages that do not have a <body/> child tag.
Thijs Alkemade <thijsalkemade@gmail.com>
parents:
666
diff
changeset
|
191 or not stanza:get_child("body") then |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
192 return; |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
193 end |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
194 |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
195 local store_user = jid_split(c2s and orig_from or orig_to); |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
196 local target_jid = c2s and orig_to or orig_from; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
197 local target_bare = jid_bare(target_jid); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
198 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
199 if shall_store(store_user, target_bare) then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
200 module:log("debug", "Archiving stanza: %s", stanza:top_tag()); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
201 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
202 -- And stash it |
1116
2345a30dd8b4
mod_mam: Update to use 'archive' storage type. Note: this breaks support with 0.9 and older.
Kim Alvefur <zash@zash.se>
parents:
1114
diff
changeset
|
203 local ok, id = archive:append(store_user, time_now(), target_bare, stanza); |
1094
158680bff642
mod_mam: Stamp <archived> on incoming stanzas after archiving them
Kim Alvefur <zash@zash.se>
parents:
1093
diff
changeset
|
204 if ok and not c2s then |
158680bff642
mod_mam: Stamp <archived> on incoming stanzas after archiving them
Kim Alvefur <zash@zash.se>
parents:
1093
diff
changeset
|
205 stanza:tag("archived", { xmlns = xmlns_mam, by = jid_bare(orig_to), id = id }):up(); |
672
8ae5317ba032
mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents:
671
diff
changeset
|
206 end |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
207 else |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
208 module:log("debug", "Not archiving stanza: %s", stanza:top_tag()); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
209 end |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 end |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
211 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
212 local function c2s_message_handler(event) |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
213 return message_handler(event, true); |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
214 end |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
215 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
216 -- Stanzas sent by local clients |
621
7bdd02056e2b
mod_mam: Bumb priority up above carbons, so that archive ids are included
Kim Alvefur <zash@zash.se>
parents:
620
diff
changeset
|
217 module:hook("pre-message/bare", c2s_message_handler, 2); |
7bdd02056e2b
mod_mam: Bumb priority up above carbons, so that archive ids are included
Kim Alvefur <zash@zash.se>
parents:
620
diff
changeset
|
218 module:hook("pre-message/full", c2s_message_handler, 2); |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
219 -- Stanszas to local clients |
621
7bdd02056e2b
mod_mam: Bumb priority up above carbons, so that archive ids are included
Kim Alvefur <zash@zash.se>
parents:
620
diff
changeset
|
220 module:hook("message/bare", message_handler, 2); |
7bdd02056e2b
mod_mam: Bumb priority up above carbons, so that archive ids are included
Kim Alvefur <zash@zash.se>
parents:
620
diff
changeset
|
221 module:hook("message/full", message_handler, 2); |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
222 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 module:add_feature(xmlns_mam); |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
224 |