Diff

mod_mam/mod_mam.lua @ 2506:5941aac79f06

mod_mam: Add support for XEP-0313 v0.5
author Kim Alvefur <zash@zash.se>
date Mon, 20 Feb 2017 00:41:13 +0100
parent 2505:1398f3f76c0f
child 2510:d81882aa0e1e
line wrap: on
line diff
--- a/mod_mam/mod_mam.lua	Mon Feb 20 00:44:27 2017 +0100
+++ b/mod_mam/mod_mam.lua	Mon Feb 20 00:41:13 2017 +0100
@@ -3,7 +3,8 @@
 --
 -- This file is MIT/X11 licensed.
 
-local xmlns_mam     = "urn:xmpp:mam:0";
+local xmlns_mam0    = "urn:xmpp:mam:0";
+local xmlns_mam1    = "urn:xmpp:mam:1";
 local xmlns_delay   = "urn:xmpp:delay";
 local xmlns_forward = "urn:xmpp:forward:0";
 
@@ -54,11 +55,12 @@
 local cleanup;
 
 -- Handle prefs.
-module:hook("iq/self/"..xmlns_mam..":prefs", function(event)
+local function handle_prefs(event)
 	local origin, stanza = event.origin, event.stanza;
+	local xmlns_mam = stanza.tags[1].attr.xmlns;
 	local user = origin.username;
 	if stanza.attr.type == "get" then
-		local prefs = prefs_to_stanza(get_prefs(user));
+		local prefs = prefs_to_stanza(get_prefs(user), xmlns_mam);
 		local reply = st.reply(stanza):add_child(prefs);
 		origin.send(reply);
 	else -- type == "set"
@@ -72,25 +74,34 @@
 		end
 	end
 	return true;
-end);
+end
+
+module:hook("iq/self/"..xmlns_mam0..":prefs", handle_prefs);
+module:hook("iq/self/"..xmlns_mam1..":prefs", handle_prefs);
 
 local query_form = dataform {
-	{ name = "FORM_TYPE"; type = "hidden"; value = xmlns_mam; };
+	{ name = "FORM_TYPE"; type = "hidden"; value = xmlns_mam0; };
 	{ name = "with"; type = "jid-single"; };
 	{ name = "start"; type = "text-single" };
 	{ name = "end"; type = "text-single"; };
 };
 
 -- Serve form
-module:hook("iq-get/self/"..xmlns_mam..":query", function(event)
+local function handle_get_form(event)
 	local origin, stanza = event.origin, event.stanza;
+	local xmlns_mam = stanza.tags[1].attr.xmlns;
+	query_form[1].value = xmlns_mam;
 	origin.send(st.reply(stanza):query(xmlns_mam):add_child(query_form:form()));
 	return true;
-end);
+end
+
+module:hook("iq-get/self/"..xmlns_mam0..":query", handle_get_form);
+module:hook("iq-get/self/"..xmlns_mam1..":query", handle_get_form);
 
 -- Handle archive queries
-module:hook("iq-set/self/"..xmlns_mam..":query", function(event)
+local function handle_mam_query(event)
 	local origin, stanza = event.origin, event.stanza;
+	local xmlns_mam = stanza.tags[1].attr.xmlns;
 	local query = stanza.tags[1];
 	local qid = query.attr.queryid;
 
@@ -101,6 +112,7 @@
 	local form = query:get_child("x", "jabber:x:data");
 	if form then
 		local err;
+		query_form[1].value = xmlns_mam;
 		form, err = query_form:data(form);
 		if err then
 			origin.send(st.error_reply(stanza, "modify", "bad-request", select(2, next(err))));
@@ -146,7 +158,9 @@
 	end
 	local total = tonumber(err);
 
-	origin.send(st.reply(stanza));
+	if xmlns_mam == xmlns_mam0 then
+		origin.send(st.reply(stanza));
+	end
 	local msg_reply_attr = { to = stanza.attr.from, from = stanza.attr.to };
 
 	local results = {};
@@ -192,12 +206,22 @@
 	-- That's all folks!
 	module:log("debug", "Archive query %s completed", tostring(qid));
 
-	origin.send(st.message(msg_reply_attr)
-		:tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete })
+	local fin;
+	if xmlns_mam == xmlns_mam0 then
+		fin = st.message(msg_reply_attr);
+	else
+		fin = st.reply(stanza);
+	end
+	do
+		fin:tag("fin", { xmlns = xmlns_mam, queryid = qid, complete = complete })
 			:add_child(rsm.generate {
-				first = first, last = last, count = total }));
+				first = first, last = last, count = total })
+	end
+	origin.send(fin);
 	return true;
-end);
+end
+module:hook("iq-set/self/"..xmlns_mam0..":query", handle_mam_query);
+module:hook("iq-set/self/"..xmlns_mam1..":query", handle_mam_query);
 
 local function has_in_roster(user, who)
 	local roster = rm_load_roster(user, host);
@@ -330,9 +354,10 @@
 module:hook("message/bare", message_handler, 2);
 module:hook("message/full", message_handler, 2);
 
-module:add_feature(xmlns_mam); -- COMPAT with XEP-0313 v 0.1
+module:add_feature(xmlns_mam0); -- COMPAT with XEP-0313 v 0.1
 
 module:hook("account-disco-info", function(event)
-	(event.reply or event.stanza):tag("feature", {var=xmlns_mam}):up();
+	(event.reply or event.stanza):tag("feature", {var=xmlns_mam0}):up();
+	(event.reply or event.stanza):tag("feature", {var=xmlns_mam1}):up();
 end);