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