Diff

mod_default_bookmarks/mod_default_bookmarks.lua @ 1324:853a382c9bd6

mod_turncredentials: Advertise the XEP-0215 feature (thanks Gryffus)
author Kim Alvefur <zash@zash.se>
date Fri, 28 Feb 2014 15:36:06 +0100
parent 479:597c872d691e
child 1325:b21236b6b8d8
line wrap: on
line diff
--- a/mod_default_bookmarks/mod_default_bookmarks.lua	Wed Feb 26 13:08:47 2014 -0800
+++ b/mod_default_bookmarks/mod_default_bookmarks.lua	Fri Feb 28 15:36:06 2014 +0100
@@ -13,37 +13,34 @@
 local dm_load = require "util.datamanager".load
 local jid_split = require "util.jid".split
 
-module:hook("iq/self/jabber:iq:private:query", function(event)
+local private_bookmarks_ns = "storage:storage:bookmarks";
+
+local bookmarks = module:get_option("default_bookmarks");
+
+module:hook("iq-get/self/jabber:iq:private:query", function(event)
 	local origin, stanza = event.origin, event.stanza;
-	local typ = stanza.attr.type;
 	local from = stanza.attr.from;
-	local query = stanza.tags[1];
-	if #query.tags == 1 and typ == "get" then
-		local tag = query.tags[1];
-		local key = tag.name..":"..tag.attr.xmlns;
-		if key == "storage:storage:bookmarks" then
-			local data, err = dm_load(origin.username, origin.host, "private");
-			if not(data and data[key]) then
-				local bookmarks = module:get_option("default_bookmarks");
-				if bookmarks and #bookmarks > 0 then
-					local reply = st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"})
-						:tag("storage", { xmlns = "storage:bookmarks" });
-					local nick = jid_split(from);
-					for i=1,#bookmarks do
-						local bookmark = bookmarks[i];
-						if type(bookmark) ~= "table" then -- assume it's only a jid
-							bookmark = { jid = bookmark, name = jid_split(bookmark) };
-						end
-						reply:tag("conference", {
-							jid = bookmark.jid,
-							name = bookmark.name,
-							autojoin = "1",
-						}):tag("nick"):text(nick):up():up();
-					end
-					origin.send(reply);
-					return true;
-				end
-			end
+	if not stanza.tags[1]:get_child("storage", "storage:bookmarks") then return end
+	local data, err = dm_load(origin.username, origin.host, "private");
+	if data and data[private_bookmarks_ns] then return end
+
+	local reply = st.reply(stanza):tag("query", {xmlns = "jabber:iq:private"})
+		:tag("storage", { xmlns = "storage:bookmarks" });
+
+	local nick = jid_split(from);
+
+	local bookmark;
+	for i=1,#bookmarks do
+		bookmark = bookmarks[i];
+		if type(bookmark) ~= "table" then -- assume it's only a jid
+			bookmark = { jid = bookmark, name = jid_split(bookmark) };
 		end
+		reply:tag("conference", {
+			jid = bookmark.jid,
+			name = bookmark.name,
+			autojoin = "1",
+		}):tag("nick"):text(nick):up():up();
 	end
+	origin.send(reply);
+	return true;
 end, 1);