Software /
code /
prosody-modules
Annotate
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 |
parent | 880:312602605269 |
child | 882:4939788a47ea |
rev | line source |
---|---|
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 local dm_load = require "util.datamanager".load; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local dm_store = require "util.datamanager".store; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local usermanager = require "core.usermanager"; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local dataforms_new = require "util.dataforms".new; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local jid_split = require "util.jid".prepped_split; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 local vcard = module:require "vcard"; |
734
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
8 local rawget, rawset = rawget, rawset; |
880
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
9 local s_lower = string.lower; |
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
10 local s_find = string.find; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local st = require "util.stanza"; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local template = require "util.template"; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local get_reply = template[[ |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 <query xmlns="jabber:iq:search"> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 <instructions>Fill in one or more fields to search for any matching Jabber users.</instructions> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 <first/> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 <last/> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 <nick/> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 <email/> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 </query> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 ]].apply({}); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 local item_template = template[[ |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 <item xmlns="jabber:iq:search" jid="{jid}"> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 <first>{first}</first> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 <last>{last}</last> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 <nick>{nick}</nick> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 <email>{email}</email> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 </item> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 ]]; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 |
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
33 local search_mode = module:get_option_string("vjud_mode", "opt-in"); |
789
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
34 local base_host = module:get_option_string("vjud_search_domain", |
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
35 module:get_host_type() == "component" |
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
36 and module.host:gsub("^[^.]+%.","") |
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
37 or module.host); |
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
38 |
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
39 module:depends"disco"; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 module:add_feature("jabber:iq:search"); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 local vCard_mt = { |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 __index = function(t, k) |
734
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
44 if type(k) ~= "string" then return nil end |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 for i=1,#t do |
734
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
46 local t_i = rawget(t, i); |
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
47 if t_i and t_i.name == k then |
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
48 rawset(t, k, t_i); |
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
49 return t_i; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 }; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 |
789
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
55 local function get_user_vcard(user, host) |
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
56 local vCard = dm_load(user, host or base_host, "vcard"); |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 if vCard then |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 vCard = st.deserialize(vCard); |
732
317e142fe6f1
mod_vjud: Update util.vcard from verse
Kim Alvefur <zash@zash.se>
parents:
730
diff
changeset
|
59 vCard = vcard.from_xep54(vCard); |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 return setmetatable(vCard, vCard_mt); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 |
789
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
64 local at_host = "@"..base_host; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 |
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
66 local users; -- The user iterator |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
67 |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 module:hook("iq/host/jabber:iq:search:query", function(event) |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 local origin, stanza = event.origin, event.stanza; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 if stanza.attr.type == "get" then |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 origin.send(st.reply(stanza):add_child(get_reply)); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 else -- type == "set" |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 local query = stanza.tags[1]; |
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
75 local first, last, nick, email = |
879
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
76 s_lower(query:get_child_text"first" or ""), |
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
77 s_lower(query:get_child_text"last" or ""), |
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
78 s_lower(query:get_child_text"nick" or ""), |
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
79 s_lower(query:get_child_text"email" or ""); |
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
80 |
879
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
81 first = #first >= 2 and first; |
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
82 last = #last >= 2 and last; |
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
83 nick = #nick >= 2 and nick; |
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
84 email = #email >= 2 and email; |
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
85 if not ( first or last or nick or email ) then |
879
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
86 origin.send(st.error_reply(stanza, "modify", "not-acceptable", "All fields were empty or too short")); |
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
87 return true; |
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
88 end |
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
89 |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 local reply = st.reply(stanza):query("jabber:iq:search"); |
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
91 |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 local username, hostname = jid_split(email); |
789
7e40d6680093
mod_vjud: Allow working as a component, in which case the parent domain is searched.
Kim Alvefur <zash@zash.se>
parents:
787
diff
changeset
|
93 if hostname == base_host and username and usermanager.user_exists(username, hostname) then |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 local vCard = get_user_vcard(username); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 if vCard then |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 reply:add_child(item_template.apply{ |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 jid = username..at_host; |
733
dd3b30c0dc8a
mod_vjud: Switch first and last name in results to be correct.
Kim Alvefur <zash@zash.se>
parents:
732
diff
changeset
|
98 first = vCard.N and vCard.N[2] or nil; |
dd3b30c0dc8a
mod_vjud: Switch first and last name in results to be correct.
Kim Alvefur <zash@zash.se>
parents:
732
diff
changeset
|
99 last = vCard.N and vCard.N[1] or nil; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 nick = vCard.NICKNAME and vCard.NICKNAME[1] or username; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 email = vCard.EMAIL and vCard.EMAIL[1] or nil; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 }); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 else |
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
105 for username in users() do |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 local vCard = get_user_vcard(username); |
880
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
107 if vCard |
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
108 and ((first and vCard.N and s_find(s_lower(vCard.N[2]), first, nil, true)) |
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
109 or (last and vCard.N and s_find(s_lower(vCard.N[1]), last, nil, true)) |
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
110 or (nick and vCard.NICKNAME and s_find(s_lower(vCard.NICKNAME[1]), nick, nil, true)) |
312602605269
mod_vjud: Do case-insensitive matching
Kim Alvefur <zash@zash.se>
parents:
879
diff
changeset
|
111 or (email and vCard.EMAIL and s_find(s_lower(vCard.EMAIL[1]), email, nil, true))) then |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 reply:add_child(item_template.apply{ |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 jid = username..at_host; |
733
dd3b30c0dc8a
mod_vjud: Switch first and last name in results to be correct.
Kim Alvefur <zash@zash.se>
parents:
732
diff
changeset
|
114 first = vCard.N and vCard.N[2] or nil; |
dd3b30c0dc8a
mod_vjud: Switch first and last name in results to be correct.
Kim Alvefur <zash@zash.se>
parents:
732
diff
changeset
|
115 last = vCard.N and vCard.N[1] or nil; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 nick = vCard.NICKNAME and vCard.NICKNAME[1] or username; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 email = vCard.EMAIL and vCard.EMAIL[1] or nil; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 }); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 origin.send(reply); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 return true; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 end); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 |
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
127 if search_mode == "all" then |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
128 function users() |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
129 return usermanager.users(base_host); |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
130 end |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
131 else -- if "opt-in", default |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
132 local opted_in; |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
133 function module.load() |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
134 opted_in = dm_load(nil, module.host, "user_index") or {}; |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
135 end |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
136 function module.unload() |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
137 dm_store(nil, module.host, "user_index", opted_in); |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
138 end |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
139 function users() |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
140 return pairs(opted_in); |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
141 end |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
142 local opt_in_layout = dataforms_new{ |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
143 title = "Search settings"; |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
144 instructions = "Do you want to appear in search results?"; |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
145 { |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
146 name = "searchable", |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
147 label = "Appear in search results?", |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
148 type = "boolean", |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
149 }, |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
150 }; |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
151 local function opt_in_handler(self, data, state) |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
152 local username, hostname = jid_split(data.from); |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
153 if state then -- the second return value |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
154 if data.action == "cancel" then |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
155 return { status = "canceled" }; |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
156 end |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
157 |
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
158 if not username or not hostname or hostname ~= base_host then |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
159 return { status = "error", error = { type = "cancel", |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
160 condition = "forbidden", message = "Invalid user or hostname." } }; |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
161 end |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 |
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
163 local fields = opt_in_layout:data(data.form); |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
164 opted_in[username] = fields.searchable or nil |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
165 |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
166 return { status = "completed" } |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
167 else -- No state, send the form. |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
168 return { status = "executing", actions = { "complete" }, |
787
cec49ee88c23
mod_vjud: Correctly pass current state to form (Thanks Florob)
Kim Alvefur <zash@zash.se>
parents:
742
diff
changeset
|
169 form = { layout = opt_in_layout, values = { searchable = opted_in[username] } } }, true; |
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
170 end |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
171 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
172 |
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
173 local adhoc_new = module:require "adhoc".new; |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
174 local adhoc_vjudsetup = adhoc_new("Search settings", "vjudsetup", opt_in_handler);--, "self");-- and nil); |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
175 module:depends"adhoc"; |
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
176 module:provides("adhoc", adhoc_vjudsetup); |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
177 |
881
4b06d6c79b15
mod_vjud: Add non-default mode where we search all users
Kim Alvefur <zash@zash.se>
parents:
880
diff
changeset
|
178 end |