Software / code / prosody-modules
Comparison
mod_seclabels/mod_seclabels.lua @ 450:fb152d4af082
mod_seclabels: Update to latest catalog schema, while keeping compatibility with the old one.
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Wed, 05 Oct 2011 21:03:51 +0200 |
| parent | 449:08ffbbdafeea |
| child | 451:f43d2d26c1c4 |
comparison
equal
deleted
inserted
replaced
| 449:08ffbbdafeea | 450:fb152d4af082 |
|---|---|
| 1 local st = require "util.stanza"; | 1 local st = require "util.stanza"; |
| 2 | 2 |
| 3 local xmlns_label = "urn:xmpp:sec-label:0"; | 3 local xmlns_label = "urn:xmpp:sec-label:0"; |
| 4 local xmlns_label_catalog = "urn:xmpp:sec-label:catalog:0"; | 4 local xmlns_label_catalog = "urn:xmpp:sec-label:catalog:2"; |
| 5 local xmlns_label_catalog_old = "urn:xmpp:sec-label:catalog:0"; -- COMPAT | |
| 5 | 6 |
| 6 module:add_feature(xmlns_label); | 7 module:add_feature(xmlns_label); |
| 8 module:add_feature(xmlns_label_catalog); | |
| 9 module:add_feature(xmlns_label_catalog_old); | |
| 7 | 10 |
| 8 module:hook("account-disco-info", function(event) | 11 module:hook("account-disco-info", function(event) -- COMPAT |
| 9 local stanza = event.stanza; | 12 local stanza = event.stanza; |
| 10 stanza:tag('feature', {var=xmlns_label}):up(); | 13 stanza:tag('feature', {var=xmlns_label}):up(); |
| 11 stanza:tag('feature', {var=xmlns_label_catalog}):up(); | 14 stanza:tag('feature', {var=xmlns_label_catalog}):up(); |
| 12 end); | 15 end); |
| 13 | 16 |
| 24 labels = module:get_option("security_labels", default_labels); | 27 labels = module:get_option("security_labels", default_labels); |
| 25 end | 28 end |
| 26 module:hook("config-reloaded",get_conf); | 29 module:hook("config-reloaded",get_conf); |
| 27 get_conf(); | 30 get_conf(); |
| 28 | 31 |
| 29 module:hook("iq/self/"..xmlns_label_catalog..":catalog", function (request) | 32 function handle_catalog_request(request) |
| 30 local catalog_request = request.stanza.tags[1]; | 33 local catalog_request = request.stanza.tags[1]; |
| 31 local reply = st.reply(request.stanza) | 34 local reply = st.reply(request.stanza) |
| 32 :tag("catalog", { | 35 :tag("catalog", { |
| 33 xmlns = xmlns_label_catalog, | 36 xmlns = catalog_request.attr.xmlns, |
| 34 to = catalog_request.attr.to, | 37 to = catalog_request.attr.to, |
| 35 name = catalog_name, | 38 name = catalog_name, |
| 36 desc = catalog_desc | 39 desc = catalog_desc |
| 37 }); | 40 }); |
| 38 | 41 |
| 39 local function add_labels(catalog, labels, selector) | 42 local function add_labels(catalog, labels, selector) |
| 40 for name, value in pairs(labels) do | 43 for name, value in pairs(labels) do |
| 41 if value.label then | 44 if value.label then |
| 42 catalog:tag("securitylabel", { xmlns = xmlns_label, selector = selector..name }) | 45 if catalog_request.attr.xmlns == xmlns_label_catalog then |
| 43 :tag("displaymarking", { | 46 catalog:tag("item", { |
| 44 fgcolor = value.color or "black", | 47 selector = selector..name, |
| 45 bgcolor = value.bgcolor or "white", | 48 default = value.default and "true" or nil, |
| 46 }):text(value.name or name):up() | 49 }):tag("securitylabel", { xmlns = xmlns_label }) |
| 47 :tag("label"); | 50 else -- COMPAT |
| 51 catalog:tag("securitylabel", { | |
| 52 xmlns = xmlns_label, | |
| 53 selector = selector..name, | |
| 54 default = value.default and "true" or nil, | |
| 55 }) | |
| 56 end | |
| 57 if value.name or value.color or value.bgcolor then | |
| 58 catalog:tag("displaymarking", { | |
| 59 fgcolor = value.color, | |
| 60 bgcolor = value.bgcolor, | |
| 61 }):text(value.name or name):up(); | |
| 62 end | |
| 48 if type(value.label) == "string" then | 63 if type(value.label) == "string" then |
| 49 catalog:text(value.label); | 64 catalog:tag("label"):text(value.label):up(); |
| 50 else | 65 elseif type(value.label) == "table" then |
| 51 catalog:add_child(value.label); | 66 catalog:tag("label"):add_child(value.label):up(); |
| 52 end | 67 end |
| 53 catalog:up():up(); | 68 catalog:up(); |
| 69 if catalog_request.attr.xmlns == xmlns_label_catalog then | |
| 70 catalog:up(); | |
| 71 end | |
| 54 else | 72 else |
| 55 add_labels(catalog, value, (selector or "")..name.."|"); | 73 add_labels(catalog, value, (selector or "")..name.."|"); |
| 56 end | 74 end |
| 57 end | 75 end |
| 58 end | 76 end |
| 59 add_labels(reply, labels); | 77 add_labels(reply, labels, ""); |
| 60 request.origin.send(reply); | 78 request.origin.send(reply); |
| 61 return true; | 79 return true; |
| 62 end); | 80 end |
| 81 module:hook("iq/host/"..xmlns_label_catalog..":catalog", handle_catalog_request); | |
| 82 module:hook("iq/self/"..xmlns_label_catalog..":catalog", handle_catalog_request); -- COMPAT | |
| 83 module:hook("iq/self/"..xmlns_label_catalog_old..":catalog", handle_catalog_request); -- COMPAT |