Software /
code /
prosody-modules
Annotate
mod_mam/mod_mam.lua @ 1112:1dc07833355e
mod_mam: Use more specific hook
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Fri, 12 Jul 2013 22:54:41 +0200 |
parent | 1111:bdbf76730f49 |
child | 1113:f1f0c87cffdd |
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"; |
bdbf76730f49
mod_mam: Split out preference functions into a lib for easy reuse
Kim Alvefur <zash@zash.se>
parents:
1094
diff
changeset
|
13 local set_prefs, get_prefs = prefs.set, prefs.get; |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 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
|
15 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
|
16 local jid_prep = require "util.jid".prep; |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
17 local host = module.host; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
18 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
19 local dm_list_load = require "util.datamanager".list_load; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
20 local dm_list_append = require "util.datamanager".list_append; |
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 |
672
8ae5317ba032
mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents:
671
diff
changeset
|
23 local tostring = tostring; |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 local time_now = os.time; |
707 | 25 local m_min = math.min; |
903
8a1beff848c1
mod_mam: Implemented an empty <before/>, to request the last n items in the archive.
Thijs Alkemade <thijsalkemade@gmail.com>
parents:
808
diff
changeset
|
26 local t_insert = table.insert; |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse; |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 local uuid = require "util.uuid".generate; |
706
5c2b96c4dde6
mod_mam: Enforce a max number of items returned, with a default.
Kim Alvefur <zash@zash.se>
parents:
705
diff
changeset
|
29 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
|
30 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
|
31 -- 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
|
32 |
706
5c2b96c4dde6
mod_mam: Enforce a max number of items returned, with a default.
Kim Alvefur <zash@zash.se>
parents:
705
diff
changeset
|
33 |
672
8ae5317ba032
mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents:
671
diff
changeset
|
34 -- 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
|
35 local default_attrs = { |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
36 always = true, [true] = "always", |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
37 never = false, [false] = "never", |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
38 roster = "roster", |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
39 } |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
40 |
675
da33325453fb
mod_mam: Put name of store in a single variable
Kim Alvefur <zash@zash.se>
parents:
674
diff
changeset
|
41 local archive_store = "archive2"; |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
42 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
43 -- Handle prefs. |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 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
|
45 local origin, stanza = event.origin, event.stanza; |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
46 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
|
47 if stanza.attr.type == "get" then |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
48 local prefs = get_prefs(user); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
49 local default = prefs[false]; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
50 default = default ~= nil and default_attrs[default] or global_default_policy; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
51 local reply = st.reply(stanza):tag("prefs", { xmlns = xmlns_mam, default = default }) |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
52 local always = st.stanza("always"); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
53 local never = st.stanza("never"); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
54 for k,v in pairs(prefs) do |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
55 if k then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
56 (v and always or never):tag("jid"):text(k):up(); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
57 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
58 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
59 reply:add_child(always):add_child(never); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
60 origin.send(reply); |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 return true |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 else -- type == "set" |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
63 local prefs = {}; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
64 local new_prefs = stanza:get_child("prefs", xmlns_mam); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
65 local new_default = new_prefs.attr.default; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
66 if new_default then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
67 prefs[false] = default_attrs[new_default]; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
68 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
69 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
70 local always = new_prefs:get_child("always"); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
71 if always then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
72 for rule in always:childtags("jid") do |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
73 local jid = rule:get_text(); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
74 prefs[jid] = true; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
75 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
76 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
77 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
78 local never = new_prefs:get_child("never"); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
79 if never then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
80 for rule in never:childtags("jid") do |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
81 local jid = rule:get_text(); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
82 prefs[jid] = false; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
83 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
84 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
85 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
86 local ok, err = set_prefs(user, prefs); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
87 if not ok then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
88 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err))); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
89 else |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
90 origin.send(st.reply(stanza)); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
91 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
92 return true |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 end |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 end); |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
96 -- Handle archive queries |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
97 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
|
98 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
|
99 local query = stanza.tags[1]; |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
100 local qid = query.attr.queryid; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
101 |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
102 -- Search query parameters |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
103 local qwith = query:get_child_text("with"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
104 local qstart = query:get_child_text("start"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
105 local qend = query:get_child_text("end"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
106 local qset = rsm.get(query); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
107 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
|
108 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
|
109 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
110 if qstart or qend then -- Validate timestamps |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
111 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
|
112 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
|
113 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
|
114 return true |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
115 end |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
116 qstart, qend = vstart, vend; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
117 end |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
118 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
119 local qres; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
120 if qwith then -- Validate the 'with' jid |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
121 local pwith = qwith and jid_prep(qwith); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
122 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
|
123 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
|
124 return true |
751
3c37445f26ac
mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents:
711
diff
changeset
|
125 end |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
126 local _, _, resource = jid_split(qwith); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
127 qwith = jid_bare(pwith); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
128 qres = resource; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
129 end |
751
3c37445f26ac
mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents:
711
diff
changeset
|
130 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
131 -- Load all the data! |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
132 local data, err = dm_list_load(origin.username, origin.host, archive_store); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
133 if not data then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
134 if (not err) then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
135 module:log("debug", "The archive was empty."); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
136 origin.send(st.reply(stanza)); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
137 else |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
138 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error loading archive: "..tostring(err))); |
751
3c37445f26ac
mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents:
711
diff
changeset
|
139 end |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
140 return true |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
141 end |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
143 -- RSM stuff |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
144 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
|
145 local qset_matches = not (qset and qset.after); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
146 local first, last, index; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
147 local n = 0; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
148 local start = qset and qset.index or 1; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
149 local results = {}; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
150 -- An empty <before/> means: give the last n items. So we loop backwards. |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
151 local reverse = qset and qset.before or false; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
152 |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
153 module:log("debug", "Loaded %d items, about to filter", #data); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
154 for i=(reverse and #data or start),(reverse and start or #data),(reverse and -1 or 1) do |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
155 local item = data[i]; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
156 local when, with, resource = item.when, item.with, item.resource; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
157 local id = item.id; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
158 --module:log("debug", "id is %s", id); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
159 |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
160 -- RSM pre-send-checking |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
161 if qset then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
162 if qset.before == id then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
163 module:log("debug", "End of matching range found"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
164 qset_matches = false; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
165 break; |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
166 end |
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 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
169 --module:log("debug", "message with %s at %s", with, when or "???"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
170 -- Apply query filter |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
171 if (not qwith or ((qwith == with) and (not qres or qres == resource))) |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
172 and (not qstart or when >= qstart) |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
173 and (not qend or when <= qend) |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
174 and (not qset or qset_matches) then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
175 local fwd_st = st.message{ to = origin.full_jid } |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
176 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }) |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
177 :tag("forwarded", { xmlns = xmlns_forward }) |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
178 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up(); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
179 local orig_stanza = st.deserialize(item.stanza); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
180 orig_stanza.attr.xmlns = "jabber:client"; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
181 fwd_st:add_child(orig_stanza); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
182 if reverse then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
183 t_insert(results, 1, fwd_st); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
184 else |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
185 results[#results + 1] = fwd_st; |
701
cc5805f83583
mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
675
diff
changeset
|
186 end |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
187 if not first then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
188 index = i; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
189 first = id; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
190 end |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
191 last = id; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
192 n = n + 1; |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
193 elseif (qend and when > qend) then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
194 module:log("debug", "We have passed into messages more recent than requested"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
195 break -- We have passed into messages more recent than requested |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
196 end |
701
cc5805f83583
mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
675
diff
changeset
|
197 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
198 -- RSM post-send-checking |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
199 if qset then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
200 if qset.after == id then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
201 module:log("debug", "Start of matching range found"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
202 qset_matches = true; |
701
cc5805f83583
mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
675
diff
changeset
|
203 end |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 end |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
205 if n >= qmax then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
206 module:log("debug", "Max number of items matched"); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
207 break |
903
8a1beff848c1
mod_mam: Implemented an empty <before/>, to request the last n items in the archive.
Thijs Alkemade <thijsalkemade@gmail.com>
parents:
808
diff
changeset
|
208 end |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
209 end |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
210 for _,v in pairs(results) do |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
211 origin.send(v); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
212 end |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
213 -- That's all folks! |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
214 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
|
215 |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
216 local reply = st.reply(stanza); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
217 if last then |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
218 -- This is a bit redundant, isn't it? |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
219 reply:query(xmlns_mam):add_child(rsm.generate{first = (reverse and last or first), last = (reverse and first or last), count = #data}); |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
220 end |
1112
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
221 origin.send(reply); |
1dc07833355e
mod_mam: Use more specific hook
Kim Alvefur <zash@zash.se>
parents:
1111
diff
changeset
|
222 return true |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
223 end); |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
224 |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
225 local function has_in_roster(user, who) |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
226 local roster = rm_load_roster(user, host); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
227 module:log("debug", "%s has %s in roster? %s", user, who, roster[who] and "yes" or "no"); |
798 | 228 return roster[who]; |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
229 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
230 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
231 local function shall_store(user, who) |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
232 -- TODO Cache this? |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
233 local prefs = get_prefs(user); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
234 local rule = prefs[who]; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
235 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
|
236 if rule ~= nil then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
237 return rule; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
238 else -- Below could be done by a metatable |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
239 local default = prefs[false]; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
240 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
|
241 if default == nil then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
242 default = global_default_policy; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
243 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
|
244 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
245 if default == "roster" then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
246 return has_in_roster(user, who); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
247 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
248 return default; |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
249 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
250 end |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
251 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
252 -- Handle messages |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 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
|
254 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
|
255 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
|
256 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
|
257 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
|
258 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 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
|
263 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
264 -- 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
|
265 if orig_type == "error" |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 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
|
267 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
|
268 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
|
269 return; |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 -- TODO Maybe headlines should be configurable? |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
271 -- TODO Write a mod_mam_muc for groupchat messages. |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 end |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 local store_user, store_host = 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
|
275 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
|
276 local target_bare = jid_bare(target_jid); |
799
53917d98d411
mod_mam: Change storage format to store bare JID + resource.
Kim Alvefur <zash@zash.se>
parents:
798
diff
changeset
|
277 local _, _, target_resource = jid_split(target_jid); |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
278 |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
279 if shall_store(store_user, target_bare) then |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
280 module:log("debug", "Archiving stanza: %s", stanza:top_tag()); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
281 |
559
6be3a130c810
mod_mam: Store the message id more accessible.
Kim Alvefur <zash@zash.se>
parents:
558
diff
changeset
|
282 local id = uuid(); |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
283 local when = time_now(); |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
284 -- And stash it |
675
da33325453fb
mod_mam: Put name of store in a single variable
Kim Alvefur <zash@zash.se>
parents:
674
diff
changeset
|
285 local ok, err = dm_list_append(store_user, store_host, archive_store, { |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
286 -- WARNING This format may change. |
559
6be3a130c810
mod_mam: Store the message id more accessible.
Kim Alvefur <zash@zash.se>
parents:
558
diff
changeset
|
287 id = id, |
701
cc5805f83583
mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents:
675
diff
changeset
|
288 when = when, |
799
53917d98d411
mod_mam: Change storage format to store bare JID + resource.
Kim Alvefur <zash@zash.se>
parents:
798
diff
changeset
|
289 with = target_bare, |
53917d98d411
mod_mam: Change storage format to store bare JID + resource.
Kim Alvefur <zash@zash.se>
parents:
798
diff
changeset
|
290 resource = target_resource, |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
291 stanza = st.preserialize(stanza) |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
292 }); |
1094
158680bff642
mod_mam: Stamp <archived> on incoming stanzas after archiving them
Kim Alvefur <zash@zash.se>
parents:
1093
diff
changeset
|
293 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
|
294 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
|
295 end |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
296 else |
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
297 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
|
298 end |
523
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
299 end |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
300 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
301 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
|
302 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
|
303 end |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
304 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
305 -- 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
|
306 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
|
307 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
|
308 -- 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
|
309 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
|
310 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
|
311 |
eff140d53b83
mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
312 module:add_feature(xmlns_mam); |
558
66de25ffc8d9
mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents:
523
diff
changeset
|
313 |