Annotate

mod_mam/mod_mam.lua @ 785:e781e63a49f4

mod_pubsub_hub: Fix calculating next periodic check of subscriptions.
author Kim Alvefur <zash@zash.se>
date Tue, 07 Aug 2012 01:51:42 +0200
parent 751:3c37445f26ac
child 798:2b8ceb4d1a73
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
635
30be50d2537f mod_mam: Update header
Kim Alvefur <zash@zash.se>
parents: 634
diff changeset
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";
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 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
13 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
14 local jid_prep = require "util.jid".prep;
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
15 local host = module.host;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
16
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
17 local dm_load = require "util.datamanager".load;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
18 local dm_store = require "util.datamanager".store;
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
f987c7b79008 mod_mam: Fix typo
Kim Alvefur <zash@zash.se>
parents: 706
diff changeset
25 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
26 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
27 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
28 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
29 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
30 -- 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
31
706
5c2b96c4dde6 mod_mam: Enforce a max number of items returned, with a default.
Kim Alvefur <zash@zash.se>
parents: 705
diff changeset
32
672
8ae5317ba032 mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents: 671
diff changeset
33 -- 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
34 local default_attrs = {
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
35 always = true, [true] = "always",
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
36 never = false, [false] = "never",
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
37 roster = "roster",
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
38 }
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 do
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
41 local prefs_format = {
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
42 [false] = "roster",
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
43 -- default ::= true | false | "roster"
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
44 -- true = always, false = never, nil = global default
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
45 ["romeo@montague.net"] = true, -- always
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
46 ["montague@montague.net"] = false, -- newer
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
47 };
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
48 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
49
675
da33325453fb mod_mam: Put name of store in a single variable
Kim Alvefur <zash@zash.se>
parents: 674
diff changeset
50 local archive_store = "archive2";
da33325453fb mod_mam: Put name of store in a single variable
Kim Alvefur <zash@zash.se>
parents: 674
diff changeset
51 local prefs_store = archive_store .. "_prefs";
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
52 local function get_prefs(user)
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
53 return dm_load(user, host, prefs_store) or
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
54 { [false] = global_default_policy };
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
55 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
56 local function set_prefs(user, prefs)
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
57 return dm_store(user, host, prefs_store, prefs);
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
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
60
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
61 -- Handle prefs.
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
62 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
63 local origin, stanza = event.origin, event.stanza;
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
64 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
65 if stanza.attr.type == "get" then
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
66 local prefs = get_prefs(user);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
67 local default = prefs[false];
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
68 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
69 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
70 local always = st.stanza("always");
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
71 local never = st.stanza("never");
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
72 for k,v in pairs(prefs) do
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
73 if k then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
74 (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
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 reply:add_child(always):add_child(never);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
78 origin.send(reply);
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 return true
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80 else -- type == "set"
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
81 local prefs = {};
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
82 local new_prefs = stanza:get_child("prefs", xmlns_mam);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
83 local new_default = new_prefs.attr.default;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
84 if new_default then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
85 prefs[false] = default_attrs[new_default];
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
86 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
87
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
88 local always = new_prefs:get_child("always");
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
89 if always then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
90 for rule in always:childtags("jid") do
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
91 local jid = rule:get_text();
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
92 prefs[jid] = true;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
93 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
94 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
95
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
96 local never = new_prefs:get_child("never");
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
97 if never then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
98 for rule in never:childtags("jid") do
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
99 local jid = rule:get_text();
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
100 prefs[jid] = false;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
101 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
102 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
103
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
104 local ok, err = set_prefs(user, prefs);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
105 if not ok then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
106 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
107 else
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
108 origin.send(st.reply(stanza));
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
109 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
110 return true
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 end
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end);
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
114 -- Handle archive queries
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 module:hook("iq/self/"..xmlns_mam..":query", function(event)
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 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
117 local query = stanza.tags[1];
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 if stanza.attr.type == "get" then
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 local qid = query.attr.queryid;
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
121 -- Search query parameters
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 local qwith = query:get_child_text("with");
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123 local qstart = query:get_child_text("start");
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
124 local qend = query:get_child_text("end");
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
125 local qset = rsm.get(query);
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
126 module:log("debug", "Archive query, id %s with %s from %s until %s)",
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
127 tostring(qid), qwith or "anyone", qstart or "the dawn of time", qend or "now");
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
128
751
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
129 if qstart or qend then -- Validate timestamps
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
130 local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend))
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
131 if (qstart and not qwith) or (qend and not vend) then
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
132 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp"))
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
133 return true
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
134 end
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
135 qstart, qend = vstart, vend;
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
136 end
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
137
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
138 if qwith then -- Validate the 'with' jid
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
139 local pwith = qwith and jid_prep(qwith);
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
140 if pwith and not qwith then -- it failed prepping
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
141 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid JID"))
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
142 return true
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
143 end
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
144 qwith = pwith;
3c37445f26ac mod_mam: Stricter validation, reject invalid timestamps, jids.
Kim Alvefur <zash@zash.se>
parents: 711
diff changeset
145 end
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
147 -- Load all the data!
675
da33325453fb mod_mam: Put name of store in a single variable
Kim Alvefur <zash@zash.se>
parents: 674
diff changeset
148 local data, err = dm_list_load(origin.username, origin.host, archive_store);
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
149 if not data then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
150 if (not err) then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
151 module:log("debug", "The archive was empty.");
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
152 origin.send(st.reply(stanza));
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
153 else
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
154 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error loading archive: "..tostring(err)));
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
155 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
156 return true
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
157 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
158
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
159 -- RSM stuff
706
5c2b96c4dde6 mod_mam: Enforce a max number of items returned, with a default.
Kim Alvefur <zash@zash.se>
parents: 705
diff changeset
160 local qmax = m_min(qset and qset.max or default_max_items, max_max_items);
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
161 local qset_matches = not (qset and qset.after);
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
162 local first, last, index;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
163 local n = 0;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
164 local start = qset and qset.index or 1;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
165
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
166 module:log("debug", "Loaded %d items, about to filter", #data);
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
167 for i=start,#data do
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 local item = data[i];
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
169 local when, with, with_bare = item.when, item.with, item.with_bare;
666
b42b75f3bda0 mod_mam: Add the UID to the <result> when sending an archived message back.
Thijs Alkemade <thijsalkemade@gmail.com>
parents: 635
diff changeset
170 local id = item.id;
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
171 --module:log("debug", "id is %s", id);
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
172
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
173 -- RSM pre-send-checking
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
174 if qset then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
175 if qset.before == id then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
176 module:log("debug", "End of matching range found");
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
177 qset_matches = false;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
178 break;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
179 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
180 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
181
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
182 --module:log("debug", "message with %s at %s", with, when or "???");
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 -- Apply query filter
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
184 if (not qwith or ((qwith == with) or (qwith == with_bare)))
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 and (not qstart or when >= qstart)
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
186 and (not qend or when <= qend)
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
187 and (not qset or qset_matches) then
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 local fwd_st = st.message{ to = origin.full_jid }
666
b42b75f3bda0 mod_mam: Add the UID to the <result> when sending an archived message back.
Thijs Alkemade <thijsalkemade@gmail.com>
parents: 635
diff changeset
189 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }):up()
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 :tag("forwarded", { xmlns = xmlns_forward })
702
d94ee0848b27 mod_mam: Fix issue with get_text() and clean some unneeded variables.
Kim Alvefur <zash@zash.se>
parents: 701
diff changeset
191 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up();
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 local orig_stanza = st.deserialize(item.stanza);
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 orig_stanza.attr.xmlns = "jabber:client";
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 fwd_st:add_child(orig_stanza);
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 origin.send(fwd_st);
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
196 if not first then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
197 index = i;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
198 first = id;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
199 end
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
200 last = id;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
201 n = n + 1;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
202 elseif (qend and when > qend) then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
203 module:log("debug", "We have passed into messages more recent than requested");
673
9dcf98018644 mod_mam: Break when the message is more recent than the range requested
Kim Alvefur <zash@zash.se>
parents: 672
diff changeset
204 break -- We have passed into messages more recent than requested
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 end
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
206
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
207 -- RSM post-send-checking
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
208 if qset then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
209 if qset.after == id then
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
210 module:log("debug", "Start of matching range found");
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
211 qset_matches = true;
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
212 end
711
53afd87f3612 mod_mam: Enforce max results even if RSM isn't used.
Kim Alvefur <zash@zash.se>
parents: 707
diff changeset
213 end
53afd87f3612 mod_mam: Enforce max results even if RSM isn't used.
Kim Alvefur <zash@zash.se>
parents: 707
diff changeset
214 if n >= qmax then
53afd87f3612 mod_mam: Enforce max results even if RSM isn't used.
Kim Alvefur <zash@zash.se>
parents: 707
diff changeset
215 module:log("debug", "Max number of items matched");
53afd87f3612 mod_mam: Enforce max results even if RSM isn't used.
Kim Alvefur <zash@zash.se>
parents: 707
diff changeset
216 break
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
217 end
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
218 end
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
219 -- That's all folks!
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
220 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
221
c9d0ba39a33b mod_mam: Move RSM pointer to last message into a MAM-namespaced child
Kim Alvefur <zash@zash.se>
parents: 702
diff changeset
222 local reply = st.reply(stanza);
c9d0ba39a33b mod_mam: Move RSM pointer to last message into a MAM-namespaced child
Kim Alvefur <zash@zash.se>
parents: 702
diff changeset
223 if last then
c9d0ba39a33b mod_mam: Move RSM pointer to last message into a MAM-namespaced child
Kim Alvefur <zash@zash.se>
parents: 702
diff changeset
224 -- This is a bit redundant, isn't it?
c9d0ba39a33b mod_mam: Move RSM pointer to last message into a MAM-namespaced child
Kim Alvefur <zash@zash.se>
parents: 702
diff changeset
225 reply:query(xmlns_mam):add_child(rsm.generate{last = last});
c9d0ba39a33b mod_mam: Move RSM pointer to last message into a MAM-namespaced child
Kim Alvefur <zash@zash.se>
parents: 702
diff changeset
226 end
c9d0ba39a33b mod_mam: Move RSM pointer to last message into a MAM-namespaced child
Kim Alvefur <zash@zash.se>
parents: 702
diff changeset
227 origin.send(reply);
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
228 return true
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
229 end
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
230 end);
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
231
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
232 local function has_in_roster(user, who)
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
233 local roster = rm_load_roster(user, host);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
234 module:log("debug", "%s has %s in roster? %s", user, who, roster[who] and "yes" or "no");
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
235 return roster and roster[who];
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
236 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
237
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
238 local function shall_store(user, who)
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
239 -- TODO Cache this?
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
240 local prefs = get_prefs(user);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
241 local rule = prefs[who];
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
242 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
243 if rule ~= nil then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
244 return rule;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
245 else -- Below could be done by a metatable
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
246 local default = prefs[false];
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
247 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
248 if default == nil then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
249 default = global_default_policy;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
250 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
251 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
252 if default == "roster" then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
253 return has_in_roster(user, who);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
254 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
255 return default;
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
256 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
257 end
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
258
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
259 -- Handle messages
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
260 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
261 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
262 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
263 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
264 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
265
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
266 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
267 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
268 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
269 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
270
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
271 -- 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
272 if orig_type == "error"
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
273 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
274 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
275 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
276 return;
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
277 -- 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
278 -- 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
279 end
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
280
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
281 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
282 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
283 local target_bare = jid_bare(target_jid);
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
284
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
285 if shall_store(store_user, target_bare) then
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
286 module:log("debug", "Archiving stanza: %s", stanza:top_tag());
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
287
559
6be3a130c810 mod_mam: Store the message id more accessible.
Kim Alvefur <zash@zash.se>
parents: 558
diff changeset
288 local id = uuid();
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
289 local when = time_now();
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
290 -- And stash it
675
da33325453fb mod_mam: Put name of store in a single variable
Kim Alvefur <zash@zash.se>
parents: 674
diff changeset
291 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
292 -- WARNING This format may change.
559
6be3a130c810 mod_mam: Store the message id more accessible.
Kim Alvefur <zash@zash.se>
parents: 558
diff changeset
293 id = id,
701
cc5805f83583 mod_mam: Implement support for Result Set Management in queries.
Kim Alvefur <zash@zash.se>
parents: 675
diff changeset
294 when = when,
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
295 with = target_jid,
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
296 with_bare = target_bare, -- Optimization, to avoid loads of jid_bare() calls when filtering.
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
297 stanza = st.preserialize(stanza)
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
298 });
672
8ae5317ba032 mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents: 671
diff changeset
299 --[[ This was dropped from the spec
8ae5317ba032 mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents: 671
diff changeset
300 if ok then
8ae5317ba032 mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents: 671
diff changeset
301 stanza:tag("archived", { xmlns = xmlns_mam, by = host, id = id }):up();
8ae5317ba032 mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents: 671
diff changeset
302 end
8ae5317ba032 mod_mam: local tostring and some comments
Kim Alvefur <zash@zash.se>
parents: 671
diff changeset
303 --]]
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
304 else
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
305 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
306 end
523
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
307 end
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
308
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
309 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
310 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
311 end
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
312
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
313 -- 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
314 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
315 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
316 -- 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
317 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
318 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
319
eff140d53b83 mod_mam: Add experimental implementation of the Message Archive Management ProtoXEP
Kim Alvefur <zash@zash.se>
parents:
diff changeset
320 module:add_feature(xmlns_mam);
558
66de25ffc8d9 mod_mam: Implement archiving preferences.
Kim Alvefur <zash@zash.se>
parents: 523
diff changeset
321