changeset 881:4b06d6c79b15

mod_vjud: Add non-default mode where we search all users
author Kim Alvefur <zash@zash.se>
date Sun, 16 Dec 2012 12:47:26 +0100
parents 312602605269
children 4939788a47ea
files mod_vjud/mod_vjud.lua
diffstat 1 files changed, 50 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/mod_vjud/mod_vjud.lua	Sun Dec 16 12:29:02 2012 +0100
+++ b/mod_vjud/mod_vjud.lua	Sun Dec 16 12:47:26 2012 +0100
@@ -30,6 +30,7 @@
 </item>
 ]];
 
+local search_mode = module:get_option_string("vjud_mode", "opt-in");
 local base_host = module:get_option_string("vjud_search_domain",
 	module:get_host_type() == "component"
 		and module.host:gsub("^[^.]+%.","")
@@ -38,23 +39,6 @@
 module:depends"disco";
 module:add_feature("jabber:iq:search");
 
-local opted_in;
-function module.load()
-	opted_in = dm_load(nil, module.host, "user_index") or {};
-end
-function module.unload()
-	dm_store(nil, module.host, "user_index", opted_in);
-end
-
-local opt_in_layout = dataforms_new{
-	title = "Search settings";
-	instructions = "Do you want to appear in search results?";
-	{
-		name = "searchable",
-		label = "Appear in search results?",
-		type = "boolean",
-	},
-};
 local vCard_mt = {
 	__index = function(t, k)
 		if type(k) ~= "string" then return nil end
@@ -79,6 +63,8 @@
 
 local at_host = "@"..base_host;
 
+local users; -- The user iterator
+
 module:hook("iq/host/jabber:iq:search:query", function(event)
 	local origin, stanza = event.origin, event.stanza;
 
@@ -116,7 +102,7 @@
 				});
 			end
 		else
-			for username in pairs(opted_in) do
+			for username in users() do
 				local vCard = get_user_vcard(username);
 				if vCard
 				and ((first and vCard.N and s_find(s_lower(vCard.N[2]), first, nil, true))
@@ -138,30 +124,55 @@
 	return true;
 end);
 
-local function opt_in_handler(self, data, state)
-	local username, hostname = jid_split(data.from);
-	if state then -- the second return value
-		if data.action == "cancel" then
-			return { status = "canceled" };
-		end
-
-		if not username or not hostname or hostname ~= base_host then
-			return { status = "error", error = { type = "cancel",
-				condition = "forbidden", message = "Invalid user or hostname." } };
-		end
+if search_mode == "all" then
+	function users()
+		return usermanager.users(base_host);
+	end
+else -- if "opt-in", default
+	local opted_in;
+	function module.load()
+		opted_in = dm_load(nil, module.host, "user_index") or {};
+	end
+	function module.unload()
+		dm_store(nil, module.host, "user_index", opted_in);
+	end
+	function users()
+		return pairs(opted_in);
+	end
+	local opt_in_layout = dataforms_new{
+		title = "Search settings";
+		instructions = "Do you want to appear in search results?";
+		{
+			name = "searchable",
+			label = "Appear in search results?",
+			type = "boolean",
+		},
+	};
+	local function opt_in_handler(self, data, state)
+		local username, hostname = jid_split(data.from);
+		if state then -- the second return value
+			if data.action == "cancel" then
+				return { status = "canceled" };
+			end
 
-		local fields = opt_in_layout:data(data.form);
-		opted_in[username] = fields.searchable or nil
+			if not username or not hostname or hostname ~= base_host then
+				return { status = "error", error = { type = "cancel",
+				condition = "forbidden", message = "Invalid user or hostname." } };
+			end
 
-		return { status = "completed" }
-	else -- No state, send the form.
-		return { status = "executing", actions  = { "complete" },
+			local fields = opt_in_layout:data(data.form);
+			opted_in[username] = fields.searchable or nil
+
+			return { status = "completed" }
+		else -- No state, send the form.
+			return { status = "executing", actions  = { "complete" },
 			form = { layout = opt_in_layout, values = { searchable = opted_in[username] } } }, true;
+		end
 	end
-end
 
-local adhoc_new = module:require "adhoc".new;
-local adhoc_vjudsetup = adhoc_new("Search settings", "vjudsetup", opt_in_handler);--, "self");-- and nil);
-module:depends"adhoc";
-module:provides("adhoc", adhoc_vjudsetup);
+	local adhoc_new = module:require "adhoc".new;
+	local adhoc_vjudsetup = adhoc_new("Search settings", "vjudsetup", opt_in_handler);--, "self");-- and nil);
+	module:depends"adhoc";
+	module:provides("adhoc", adhoc_vjudsetup);
 
+end