moduleapi: in module:provides(), add the name of the module in item._provided_by
[prosody.git] / util / stanza.lua
index 213ed506df9113691521b7e4ac4244b82823c358..7c21421083d57daafbcf2ad807c3d7b7166c36a5 100644 (file)
@@ -18,6 +18,7 @@ local pairs         =         pairs;
 local ipairs        =        ipairs;
 local type          =          type;
 local s_gsub        =   string.gsub;
+local s_sub         =    string.sub;
 local s_find        =   string.find;
 local os            =            os;
 
@@ -165,7 +166,7 @@ function stanza_mt:maptags(callback)
                                curr_tag = curr_tag - 1;
                        else
                                self[i] = ret;
-                               tags[i] = ret;
+                               tags[curr_tag] = ret;
                        end
                        curr_tag = curr_tag + 1;
                end
@@ -174,6 +175,31 @@ function stanza_mt:maptags(callback)
        return self;
 end
 
+function stanza_mt:find(path)
+       local pos = 1;
+       local len = #path + 1;
+
+       repeat
+               local xmlns, name, text;
+               local char = s_sub(path, pos, pos);
+               if char == "@" then
+                       return self.attr[s_sub(path, pos + 1)];
+               elseif char == "{" then
+                       xmlns, pos = s_match(path, "^([^}]+)}()", pos + 1);
+               end
+               name, text, pos = s_match(path, "^([^@/#]*)([/#]?)()", pos);
+               name = name ~= "" and name or nil;
+               if pos == len then
+                       if text == "#" then
+                               return self:get_child_text(name, xmlns);
+                       end
+                       return self:get_child(name, xmlns);
+               end
+               self = self:get_child(name, xmlns);
+       until not self
+end
+
+
 local xml_escape
 do
        local escape_table = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;", [">"] = "&gt;", ["&"] = "&amp;" };