Comparison

plugins/archive.lua @ 294:ac039aa3a4ef

plugins.archive: Implement archiving preferences
author Kim Alvefur <zash@zash.se>
date Fri, 20 Apr 2012 01:36:13 +0200
parent 293:65fb9ae79014
child 295:5f7203bf1dae
comparison
equal deleted inserted replaced
293:65fb9ae79014 294:ac039aa3a4ef
44 callback(reply.attr.type == "result" and #results, results); 44 callback(reply.attr.type == "result" and #results, results);
45 return true 45 return true
46 end); 46 end);
47 end 47 end
48 48
49 --TODO Settings 49 local default_attrs = {
50 always = true, [true] = "always",
51 never = false, [false] = "never",
52 roster = "roster",
53 }
54
55 local function prefs_decode(stanza) -- from XML
56 local prefs = {};
57 local default = stanza.attr.default;
58
59 if default then
60 prefs[false] = default_attrs[default];
61 end
62
63 local always = stanza:get_child("always");
64 if always then
65 for rule in always:childtags("jid") do
66 local jid = rule:get_text();
67 prefs[jid] = true;
68 end
69 end
70
71 local never = stanza:get_child("never");
72 if never then
73 for rule in never:childtags("jid") do
74 local jid = rule:get_text();
75 prefs[jid] = false;
76 end
77 end
78 return prefs;
79 end
80
81 local function prefs_encode(prefs) -- into XML
82 local default
83 default, prefs[false] = prefs[false], nil;
84 if default ~= nil then
85 default = default_attrs[default];
86 end
87 local reply = st.stanza("prefs", { xmlns = xmlns_mam, default = default })
88 local always = st.stanza("always");
89 local never = st.stanza("never");
90 for k,v in pairs(prefs) do
91 (v and always or never):tag("jid"):text(k):up();
92 end
93 return reply:add_child(always):add_child(never);
94 end
95
96 function stream:archive_prefs_get(callback)
97 self:send_iq(st.iq{ type="get" }:tag("prefs", { xmlns = xmlns_mam }),
98 function(result)
99 if result and result.attr.type == "result" and result.tags[1] then
100 local prefs = prefs_decode(result.tags[1]);
101 callback(prefs, result);
102 else
103 callback(nil, result);
104 end
105 end);
106 end
107
108 function stream:archive_prefs_set(prefs, callback)
109 self:send_iq(st.iq{ type="set" }:add_child(prefs_encode(prefs)), callback);
110 end
50 end 111 end