comparison mod_mam/mod_mam.lua @ 1135:0d6ab5e4bc30

mod_mam: Break out routines for converting prefs between XML and our internal representation into a library
author Kim Alvefur <zash@zash.se>
date Sat, 10 Aug 2013 20:38:25 +0200
parents 2345a30dd8b4
children d055c44a7f61
comparison
equal deleted inserted replaced
1134:0664f8b783fd 1135:0d6ab5e4bc30
8 local xmlns_forward = "urn:xmpp:forward:0"; 8 local xmlns_forward = "urn:xmpp:forward:0";
9 9
10 local st = require "util.stanza"; 10 local st = require "util.stanza";
11 local rsm = module:require "rsm"; 11 local rsm = module:require "rsm";
12 local prefs = module:require"mamprefs"; 12 local prefs = module:require"mamprefs";
13 local prefsxml = module:require"mamprefsxml";
13 local set_prefs, get_prefs = prefs.set, prefs.get; 14 local set_prefs, get_prefs = prefs.set, prefs.get;
15 local prefs_to_stanza, prefs_from_stanza = prefsxml.tostanza, prefsxml.fromstanza;
14 local jid_bare = require "util.jid".bare; 16 local jid_bare = require "util.jid".bare;
15 local jid_split = require "util.jid".split; 17 local jid_split = require "util.jid".split;
16 local jid_prep = require "util.jid".prep; 18 local jid_prep = require "util.jid".prep;
17 local host = module.host; 19 local host = module.host;
18 20
45 -- Handle prefs. 47 -- Handle prefs.
46 module:hook("iq/self/"..xmlns_mam..":prefs", function(event) 48 module:hook("iq/self/"..xmlns_mam..":prefs", function(event)
47 local origin, stanza = event.origin, event.stanza; 49 local origin, stanza = event.origin, event.stanza;
48 local user = origin.username; 50 local user = origin.username;
49 if stanza.attr.type == "get" then 51 if stanza.attr.type == "get" then
50 local prefs = get_prefs(user); 52 local prefs = prefs_to_stanza(get_prefs(user));
51 local default = prefs[false]; 53 local reply = st.reply(stanza):add_child(prefs);
52 default = default ~= nil and default_attrs[default] or global_default_policy; 54 return origin.send(reply);
53 local reply = st.reply(stanza):tag("prefs", { xmlns = xmlns_mam, default = default })
54 local always = st.stanza("always");
55 local never = st.stanza("never");
56 for k,v in pairs(prefs) do
57 if k then
58 (v and always or never):tag("jid"):text(k):up();
59 end
60 end
61 reply:add_child(always):add_child(never);
62 origin.send(reply);
63 return true
64 else -- type == "set" 55 else -- type == "set"
65 local prefs = {};
66 local new_prefs = stanza:get_child("prefs", xmlns_mam); 56 local new_prefs = stanza:get_child("prefs", xmlns_mam);
67 local new_default = new_prefs.attr.default; 57 local prefs = prefs_from_stanza(new_prefs);
68 if new_default then
69 prefs[false] = default_attrs[new_default];
70 end
71
72 local always = new_prefs:get_child("always");
73 if always then
74 for rule in always:childtags("jid") do
75 local jid = rule:get_text();
76 prefs[jid] = true;
77 end
78 end
79
80 local never = new_prefs:get_child("never");
81 if never then
82 for rule in never:childtags("jid") do
83 local jid = rule:get_text();
84 prefs[jid] = false;
85 end
86 end
87
88 local ok, err = set_prefs(user, prefs); 58 local ok, err = set_prefs(user, prefs);
89 if not ok then 59 if not ok then
90 origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err))); 60 return origin.send(st.error_reply(stanza, "cancel", "internal-server-error", "Error storing preferences: "..tostring(err)));
91 else 61 end
92 origin.send(st.reply(stanza)); 62 return origin.send(st.reply(stanza));
93 end
94 return true
95 end 63 end
96 end); 64 end);
97 65
98 -- Handle archive queries 66 -- Handle archive queries
99 module:hook("iq-get/self/"..xmlns_mam..":query", function(event) 67 module:hook("iq-get/self/"..xmlns_mam..":query", function(event)