annotate mod_mam_muc/mod_mam_muc.lua @ 1139:b32d65e41755

mod_mam_muc: Get room objects in a less awkward fashion
author Kim Alvefur <zash@zash.se>
date Sat, 10 Aug 2013 21:08:45 +0200
parents 5c97ee75cadb
children 402cb9b604eb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
1 -- XEP-0313: Message Archive Management for Prosody
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
2 -- Copyright (C) 2011-2012 Kim Alvefur
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
3 --
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
4 -- This file is MIT/X11 licensed.
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
5
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local xmlns_mam = "urn:xmpp:mam:tmp";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local xmlns_delay = "urn:xmpp:delay";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local xmlns_forward = "urn:xmpp:forward:0";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 local st = require "util.stanza";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
11 local rsm = module:require "mod_mam/rsm";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
12 local jid_bare = require "util.jid".bare;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local jid_split = require "util.jid".split;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local jid_prep = require "util.jid".prep;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15 local host = module.host;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
17 local dm_list_load = require "util.datamanager".list_load;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
18 local dm_list_append = require "util.datamanager".list_append;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 local tostring = tostring;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
21 local time_now = os.time;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22 local m_min = math.min;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 local timestamp, timestamp_parse = require "util.datetime".datetime, require "util.datetime".parse;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 local uuid = require "util.uuid".generate;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 local default_max_items, max_max_items = 20, module:get_option_number("max_archive_query_results", 50);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
26 --local rooms_to_archive = module:get_option_set("rooms_to_archive",{});
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27 -- TODO Should be possible to enforce it too
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28
1139
b32d65e41755 mod_mam_muc: Get room objects in a less awkward fashion
Kim Alvefur <zash@zash.se>
parents: 1138
diff changeset
29 local rooms = hosts[module.host].modules.muc.rooms;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 local archive_store = "archive2";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 -- Handle archive queries
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
33 module:hook("iq-get/bare/"..xmlns_mam..":query", function(event)
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 local origin, stanza = event.origin, event.stanza;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 local room = jid_split(stanza.attr.to);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 local query = stanza.tags[1];
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37
1139
b32d65e41755 mod_mam_muc: Get room objects in a less awkward fashion
Kim Alvefur <zash@zash.se>
parents: 1138
diff changeset
38 local room_obj = rooms[room];
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 if not room_obj then
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 return -- FIXME not found
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
41 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 local from = jid_bare(stanza.attr.from);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
44 if room_obj._affiliations[from] == "outcast"
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
45 or room_obj._data.members_only and not room_obj._affiliations[from] then
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46 return -- FIXME unauth
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
49 local qid = query.attr.queryid;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
50
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
51 -- Search query parameters
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
52 local qwith = query:get_child_text("with");
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
53 local qstart = query:get_child_text("start");
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
54 local qend = query:get_child_text("end");
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
55 local qset = rsm.get(query);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
56 module:log("debug", "Archive query, id %s with %s from %s until %s)",
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
57 tostring(qid), qwith or "anyone", qstart or "the dawn of time", qend or "now");
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
59 if qstart or qend then -- Validate timestamps
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
60 local vstart, vend = (qstart and timestamp_parse(qstart)), (qend and timestamp_parse(qend))
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
61 if (qstart and not vstart) or (qend and not vend) then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
62 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid timestamp"))
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
63 return true
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
64 end
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
65 qstart, qend = vstart, vend;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
66 end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
67
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
68 local qres;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
69 if qwith then -- Validate the 'with' jid
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
70 local pwith = qwith and jid_prep(qwith);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
71 if pwith and not qwith then -- it failed prepping
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
72 origin.send(st.error_reply(stanza, "modify", "bad-request", "Invalid JID"))
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
73 return true
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 end
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
75 local _, _, resource = jid_split(qwith);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
76 qwith = jid_bare(pwith);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
77 qres = resource;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
78 end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
80 -- Load all the data!
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
81 local data, err = dm_list_load(room, module.host, archive_store);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
82 if not data then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
83 if (not err) then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
84 module:log("debug", "The archive was empty.");
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
85 origin.send(st.reply(stanza));
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
86 else
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
87 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error loading archive: "..tostring(err)));
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 end
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
89 return true
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
90 end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
92 -- RSM stuff
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
93 local qmax = m_min(qset and qset.max or default_max_items, max_max_items);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
94 local qset_matches = not (qset and qset.after);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
95 local first, last, index;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
96 local n = 0;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
97 local start = qset and qset.index or 1;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
98
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
99 module:log("debug", "Loaded %d items, about to filter", #data);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
100 for i=start,#data do
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
101 local item = data[i];
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
102 local when, nick = item.when, item.resource;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
103 local id = item.id;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
104 --module:log("debug", "id is %s", id);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
105
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
106 -- RSM pre-send-checking
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
107 if qset then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
108 if qset.before == id then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
109 module:log("debug", "End of matching range found");
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
110 qset_matches = false;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
111 break;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
115 --module:log("debug", "message with %s at %s", with, when or "???");
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
116 -- Apply query filter
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
117 if (not qres or (qres == nick))
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
118 and (not qstart or when >= qstart)
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
119 and (not qend or when <= qend)
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
120 and (not qset or qset_matches) then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
121 local fwd_st = st.message{ to = stanza.attr.from }
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
122 :tag("result", { xmlns = xmlns_mam, queryid = qid, id = id }):up()
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
123 :tag("forwarded", { xmlns = xmlns_forward })
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
124 :tag("delay", { xmlns = xmlns_delay, stamp = timestamp(when) }):up();
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
125 local orig_stanza = st.deserialize(item.stanza);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
126 orig_stanza.attr.xmlns = "jabber:client";
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
127 fwd_st:add_child(orig_stanza);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
128 origin.send(fwd_st);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
129 if not first then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
130 index = i;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
131 first = id;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
132 end
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
133 last = id;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
134 n = n + 1;
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
135 elseif (qend and when > qend) then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
136 module:log("debug", "We have passed into messages more recent than requested");
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
137 break -- We have passed into messages more recent than requested
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
138 end
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
139
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
140 -- RSM post-send-checking
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
141 if qset then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
142 if qset.after == id then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
143 module:log("debug", "Start of matching range found");
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
144 qset_matches = true;
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
145 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
146 end
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
147 if n >= qmax then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
148 module:log("debug", "Max number of items matched");
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
149 break
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
150 end
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
151 end
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
152 -- That's all folks!
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
153 module:log("debug", "Archive query %s completed", tostring(qid));
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
154
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
155 local reply = st.reply(stanza);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
156 if last then
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
157 -- This is a bit redundant, isn't it?
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
158 reply:query(xmlns_mam):add_child(rsm.generate{first = first, last = last, count = n});
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
159 end
1138
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
160 origin.send(reply);
5c97ee75cadb mod_mam_muc: Switch to iq-get hook and drop some indentation
Kim Alvefur <zash@zash.se>
parents: 820
diff changeset
161 return true
820
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
162 end);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
163
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
164 -- Handle messages
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
165 local function message_handler(event)
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
166 local origin, stanza = event.origin, event.stanza;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
167 local orig_type = stanza.attr.type or "normal";
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
168 local orig_to = stanza.attr.to;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
169 local orig_from = stanza.attr.from;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
170
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
171 -- Still needed?
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
172 if not orig_from then
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
173 orig_from = origin.full_jid;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
174 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
175
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
176 -- Only store groupchat messages
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
177 if not (orig_type == "groupchat" and (stanza:get_child("body") or stanza:get_child("subject"))) then
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
178 return;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
179 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
180
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
181 local room = jid_split(orig_to);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
182 local room_obj = hosts[host].modules.muc.rooms[orig_to]
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
183 if not room_obj then return end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
184
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
185 local id = uuid();
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
186 local when = time_now();
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
187 local stanza = st.clone(stanza); -- Private copy
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
188 --stanza.attr.to = nil;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
189 local nick = room_obj._jid_nick[orig_from];
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
190 if not nick then return end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
191 stanza.attr.from = nick;
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
192 local _, _, nick = jid_split(nick);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
193 -- And stash it
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
194 local ok, err = dm_list_append(room, host, archive_store, {
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
195 -- WARNING This format may change.
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
196 id = id,
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
197 when = when,
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 resource = nick,
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 stanza = st.preserialize(stanza)
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 });
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 --[[ This was dropped from the spec
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 if ok then
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203 stanza:tag("archived", { xmlns = xmlns_mam, by = host, id = id }):up();
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
204 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
205 --]]
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
206 end
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
207
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
208 module:hook("message/bare", message_handler, 2);
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
209
005037032d65 mod_mam_muc: MUC version of mod_mam
Kim Alvefur <zash@zash.se>
parents:
diff changeset
210 module:add_feature(xmlns_mam);