Mercurial > prosody-modules
annotate mod_mam_adhoc/mod_mam_adhoc.lua @ 832:9087431d35f6
mod_carbons: Add comments and rename some variables to make it clearer
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 18 Sep 2012 18:58:13 +0200 |
parents | 6c7b6a0dcacf |
children | 91d210b6106a |
rev | line source |
---|---|
714
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 module:depends"adhoc"; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local dataforms_new = require "util.dataforms".new; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local dm_load = require "util.datamanager".load; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local dm_store = require "util.datamanager".store; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local jid_split = require "util.jid".split; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local t_insert = table.insert; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local mam_prefs_form = dataforms_new{ |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 title = "Archive preferences"; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 --instructions = ""; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 { |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 name = "default", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 label = "Default storage policy", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 type = "list-single", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 value = { |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 { value = "always", label = "Always" }, |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 { value = "never", label = "Never", default = true}, |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 { value = "roster", label = "Roster" }, |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 }, |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 { |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 name = "always", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 label = "Always store messages to/from", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 type = "jid-multi" |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 { |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 name = "never", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 label = "Never store messages to/from", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 type = "jid-multi" |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 local host = module.host; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 local default_attrs = { |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 always = true, [true] = "always", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 never = false, [false] = "never", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 roster = "roster", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 } |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 local global_default_policy = module:get_option("default_archive_policy", false); |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 local archive_store = "archive2"; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 local prefs_store = archive_store .. "_prefs"; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 local function get_prefs(user) |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 return dm_load(user, host, prefs_store) or |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 { [false] = global_default_policy }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 local function set_prefs(user, prefs) |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
49 return dm_store(user, host, prefs_store, prefs); |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 local function mam_prefs_handler(self, data, state) |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 local username, hostname = jid_split(data.from); |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 if state then -- the second return value |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 if data.action == "cancel" then |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 return { status = "canceled" }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
57 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 if not username or not hostname or hostname ~= module.host then |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
60 return { status = "error", error = { type = "cancel", |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
61 condition = "forbidden", message = "Invalid user or hostname." } }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 local fields = mam_prefs_form:data(data.form); |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 local default, always, never = fields.default, fields.always, fields.never; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 local prefs = {}; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 if default then |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 prefs[false] = default_attrs[default]; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
71 if always then |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
72 for i=1,#always do |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
73 prefs[always[i]] = true; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 if never then |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 for i=1,#never do |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 prefs[never[i]] = false; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 set_prefs(username, prefs); |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 return { status = "completed" } |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 else -- No state, send the form. |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 local prefs = get_prefs(username); |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 local values = { |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 default = { |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 { value = "always", label = "Always" }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 { value = "never", label = "Never" }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 { value = "roster", label = "Roster" }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
92 }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 always = {}; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 never = {}; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 }; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
96 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 for jid, p in pairs(prefs) do |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 if jid then |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
99 t_insert(values[p and "always" or "never"], jid); |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
100 |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 elseif p == true then -- Yes, this is ugly. FIXME later. |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 values.default[1].default = true; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 elseif p == false then |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 values.default[2].default = true; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 elseif p == "roster" then |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 values.default[3].default = true; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 return { status = "executing", actions = { "complete" }, form = { layout = mam_prefs_form, values = values } }, true; |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 end |
5879368d9ff7
mod_mam_adhoc: New module that allows clients to change MAM preferences through AdHoc.
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 |
739
6c7b6a0dcacf
mod_mam_adhoc: Use ad-hoc command node suggested by XEP-0313
Kim Alvefur <zash@zash.se>
parents:
714
diff
changeset
|
113 module:provides("adhoc", module:require"adhoc".new("Archive settings", "urn:xmpp:mam#configure", mam_prefs_handler)); |