comparison mod_vjud/mod_vjud.lua @ 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
comparison
equal deleted inserted replaced
880:312602605269 881:4b06d6c79b15
28 <nick>{nick}</nick> 28 <nick>{nick}</nick>
29 <email>{email}</email> 29 <email>{email}</email>
30 </item> 30 </item>
31 ]]; 31 ]];
32 32
33 local search_mode = module:get_option_string("vjud_mode", "opt-in");
33 local base_host = module:get_option_string("vjud_search_domain", 34 local base_host = module:get_option_string("vjud_search_domain",
34 module:get_host_type() == "component" 35 module:get_host_type() == "component"
35 and module.host:gsub("^[^.]+%.","") 36 and module.host:gsub("^[^.]+%.","")
36 or module.host); 37 or module.host);
37 38
38 module:depends"disco"; 39 module:depends"disco";
39 module:add_feature("jabber:iq:search"); 40 module:add_feature("jabber:iq:search");
40 41
41 local opted_in;
42 function module.load()
43 opted_in = dm_load(nil, module.host, "user_index") or {};
44 end
45 function module.unload()
46 dm_store(nil, module.host, "user_index", opted_in);
47 end
48
49 local opt_in_layout = dataforms_new{
50 title = "Search settings";
51 instructions = "Do you want to appear in search results?";
52 {
53 name = "searchable",
54 label = "Appear in search results?",
55 type = "boolean",
56 },
57 };
58 local vCard_mt = { 42 local vCard_mt = {
59 __index = function(t, k) 43 __index = function(t, k)
60 if type(k) ~= "string" then return nil end 44 if type(k) ~= "string" then return nil end
61 for i=1,#t do 45 for i=1,#t do
62 local t_i = rawget(t, i); 46 local t_i = rawget(t, i);
76 return setmetatable(vCard, vCard_mt); 60 return setmetatable(vCard, vCard_mt);
77 end 61 end
78 end 62 end
79 63
80 local at_host = "@"..base_host; 64 local at_host = "@"..base_host;
65
66 local users; -- The user iterator
81 67
82 module:hook("iq/host/jabber:iq:search:query", function(event) 68 module:hook("iq/host/jabber:iq:search:query", function(event)
83 local origin, stanza = event.origin, event.stanza; 69 local origin, stanza = event.origin, event.stanza;
84 70
85 if stanza.attr.type == "get" then 71 if stanza.attr.type == "get" then
114 nick = vCard.NICKNAME and vCard.NICKNAME[1] or username; 100 nick = vCard.NICKNAME and vCard.NICKNAME[1] or username;
115 email = vCard.EMAIL and vCard.EMAIL[1] or nil; 101 email = vCard.EMAIL and vCard.EMAIL[1] or nil;
116 }); 102 });
117 end 103 end
118 else 104 else
119 for username in pairs(opted_in) do 105 for username in users() do
120 local vCard = get_user_vcard(username); 106 local vCard = get_user_vcard(username);
121 if vCard 107 if vCard
122 and ((first and vCard.N and s_find(s_lower(vCard.N[2]), first, nil, true)) 108 and ((first and vCard.N and s_find(s_lower(vCard.N[2]), first, nil, true))
123 or (last and vCard.N and s_find(s_lower(vCard.N[1]), last, nil, true)) 109 or (last and vCard.N and s_find(s_lower(vCard.N[1]), last, nil, true))
124 or (nick and vCard.NICKNAME and s_find(s_lower(vCard.NICKNAME[1]), nick, nil, true)) 110 or (nick and vCard.NICKNAME and s_find(s_lower(vCard.NICKNAME[1]), nick, nil, true))
136 origin.send(reply); 122 origin.send(reply);
137 end 123 end
138 return true; 124 return true;
139 end); 125 end);
140 126
141 local function opt_in_handler(self, data, state) 127 if search_mode == "all" then
142 local username, hostname = jid_split(data.from); 128 function users()
143 if state then -- the second return value 129 return usermanager.users(base_host);
144 if data.action == "cancel" then 130 end
145 return { status = "canceled" }; 131 else -- if "opt-in", default
132 local opted_in;
133 function module.load()
134 opted_in = dm_load(nil, module.host, "user_index") or {};
135 end
136 function module.unload()
137 dm_store(nil, module.host, "user_index", opted_in);
138 end
139 function users()
140 return pairs(opted_in);
141 end
142 local opt_in_layout = dataforms_new{
143 title = "Search settings";
144 instructions = "Do you want to appear in search results?";
145 {
146 name = "searchable",
147 label = "Appear in search results?",
148 type = "boolean",
149 },
150 };
151 local function opt_in_handler(self, data, state)
152 local username, hostname = jid_split(data.from);
153 if state then -- the second return value
154 if data.action == "cancel" then
155 return { status = "canceled" };
156 end
157
158 if not username or not hostname or hostname ~= base_host then
159 return { status = "error", error = { type = "cancel",
160 condition = "forbidden", message = "Invalid user or hostname." } };
161 end
162
163 local fields = opt_in_layout:data(data.form);
164 opted_in[username] = fields.searchable or nil
165
166 return { status = "completed" }
167 else -- No state, send the form.
168 return { status = "executing", actions = { "complete" },
169 form = { layout = opt_in_layout, values = { searchable = opted_in[username] } } }, true;
146 end 170 end
171 end
147 172
148 if not username or not hostname or hostname ~= base_host then 173 local adhoc_new = module:require "adhoc".new;
149 return { status = "error", error = { type = "cancel", 174 local adhoc_vjudsetup = adhoc_new("Search settings", "vjudsetup", opt_in_handler);--, "self");-- and nil);
150 condition = "forbidden", message = "Invalid user or hostname." } }; 175 module:depends"adhoc";
151 end 176 module:provides("adhoc", adhoc_vjudsetup);
152 177
153 local fields = opt_in_layout:data(data.form);
154 opted_in[username] = fields.searchable or nil
155
156 return { status = "completed" }
157 else -- No state, send the form.
158 return { status = "executing", actions = { "complete" },
159 form = { layout = opt_in_layout, values = { searchable = opted_in[username] } } }, true;
160 end
161 end 178 end
162
163 local adhoc_new = module:require "adhoc".new;
164 local adhoc_vjudsetup = adhoc_new("Search settings", "vjudsetup", opt_in_handler);--, "self");-- and nil);
165 module:depends"adhoc";
166 module:provides("adhoc", adhoc_vjudsetup);
167