Software /
code /
prosody-modules
Annotate
mod_vjud/mod_vjud.lua @ 879:3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sun, 16 Dec 2012 12:35:43 +0100 |
parent | 789:7e40d6680093 |
child | 880:312602605269 |
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; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local st = require "util.stanza"; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local template = require "util.template"; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 local get_reply = template[[ |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 <query xmlns="jabber:iq:search"> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 <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
|
16 <first/> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 <last/> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 <nick/> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 <email/> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 </query> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 ]].apply({}); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local item_template = template[[ |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 <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
|
24 <first>{first}</first> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 <last>{last}</last> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 <nick>{nick}</nick> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 <email>{email}</email> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 </item> |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 ]]; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 |
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
|
31 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
|
32 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
|
33 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
|
34 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
|
35 |
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 module:depends"disco"; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 module:add_feature("jabber:iq:search"); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 local opted_in; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 function module.load() |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 opted_in = dm_load(nil, module.host, "user_index") or {}; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 function module.unload() |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 dm_store(nil, module.host, "user_index", opted_in); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 local opt_in_layout = dataforms_new{ |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 title = "Search settings"; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 instructions = "Do you want to appear in search results?"; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 { |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 name = "searchable", |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 label = "Appear in search results?", |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 type = "boolean", |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 }, |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 }; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 local vCard_mt = { |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 __index = function(t, k) |
734
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
58 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
|
59 for i=1,#t do |
734
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
60 local t_i = rawget(t, i); |
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
61 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
|
62 rawset(t, k, t_i); |
81de1e446bfe
mod_vjud: Don't break on undefined properties.
Kim Alvefur <zash@zash.se>
parents:
733
diff
changeset
|
63 return t_i; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 }; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 |
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
|
69 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
|
70 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
|
71 if vCard then |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 vCard = st.deserialize(vCard); |
732
317e142fe6f1
mod_vjud: Update util.vcard from verse
Kim Alvefur <zash@zash.se>
parents:
730
diff
changeset
|
73 vCard = vcard.from_xep54(vCard); |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 return setmetatable(vCard, vCard_mt); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 |
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
|
78 local at_host = "@"..base_host; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 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
|
81 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
|
82 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 if stanza.attr.type == "get" then |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 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
|
85 else -- type == "set" |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 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
|
87 local first, last, nick, email = |
879
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
88 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
|
89 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
|
90 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
|
91 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
|
92 |
879
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
93 first = #first >= 2 and first; |
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
94 last = #last >= 2 and last; |
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
95 nick = #nick >= 2 and nick; |
3a17fc0127b1
mod_vjud: Enforce minimal length of search parameters
Kim Alvefur <zash@zash.se>
parents:
789
diff
changeset
|
96 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
|
97 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
|
98 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
|
99 return true; |
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
100 end |
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
101 |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 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
|
103 |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 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
|
105 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
|
106 local vCard = get_user_vcard(username); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 if vCard then |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 reply:add_child(item_template.apply{ |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 }); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 else |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 for username in pairs(opted_in) do |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 local vCard = get_user_vcard(username); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 if vCard and ( |
730
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
120 (vCard.N and vCard.N[2] == first) or |
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
121 (vCard.N and vCard.N[1] == last) or |
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
122 (vCard.NICKNAME and vCard.NICKNAME[1] == nick) or |
274bb1a96122
mod_vjud: Missing fields should not match everyone. (Thanks Pidgin)
Kim Alvefur <zash@zash.se>
parents:
715
diff
changeset
|
123 (vCard.EMAIL and vCard.EMAIL[1] == email)) then |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 reply:add_child(item_template.apply{ |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 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
|
126 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
|
127 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
|
128 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
|
129 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
|
130 }); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 origin.send(reply); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 return true; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 end); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 local function opt_in_handler(self, data, state) |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 local username, hostname = jid_split(data.from); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 if state then -- the second return value |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 if data.action == "cancel" then |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 return { status = "canceled" }; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
145 |
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
|
146 if not username or not hostname or hostname ~= base_host then |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
147 return { status = "error", error = { type = "cancel", |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 condition = "forbidden", message = "Invalid user or hostname." } }; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
149 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 local fields = opt_in_layout:data(data.form); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
152 opted_in[username] = fields.searchable or nil |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
153 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
154 return { status = "completed" } |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
155 else -- No state, send the form. |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
156 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
|
157 form = { layout = opt_in_layout, values = { searchable = opted_in[username] } } }, true; |
715
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
158 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
159 end |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
160 |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
161 local adhoc_new = module:require "adhoc".new; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
162 local adhoc_vjudsetup = adhoc_new("Search settings", "vjudsetup", opt_in_handler);--, "self");-- and nil); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
163 module:depends"adhoc"; |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 module:provides("adhoc", adhoc_vjudsetup); |
b1268d3aa6ce
mod_vjud: Add. Thanks waqas for the base code.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
165 |