Software /
code /
prosody
Comparison
util/stanza.lua @ 90:da468ed49a7b
Stanza preserialize/deserialize helpers, to strip and restore stanzas respectively. Fixed mod_vcard to use these.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 09 Oct 2008 00:50:45 +0100 |
parent | 70:a6c00467a3f8 |
child | 91:6d66eb6b24cb |
comparison
equal
deleted
inserted
replaced
89:081e920dc74e | 90:da468ed49a7b |
---|---|
4 local tostring = tostring; | 4 local tostring = tostring; |
5 local setmetatable = setmetatable; | 5 local setmetatable = setmetatable; |
6 local pairs = pairs; | 6 local pairs = pairs; |
7 local ipairs = ipairs; | 7 local ipairs = ipairs; |
8 local type = type; | 8 local type = type; |
9 local unpack = unpack; | |
9 local s_gsub = string.gsub; | 10 local s_gsub = string.gsub; |
10 module "stanza" | 11 module "stanza" |
11 | 12 |
12 stanza_mt = {}; | 13 stanza_mt = {}; |
13 stanza_mt.__index = stanza_mt; | 14 stanza_mt.__index = stanza_mt; |
105 id = id + 1; | 106 id = id + 1; |
106 return "lx"..id; | 107 return "lx"..id; |
107 end | 108 end |
108 end | 109 end |
109 | 110 |
111 function preserialize(stanza) | |
112 local s = { name = stanza.name, attr = stanza.attr }; | |
113 for _, child in ipairs(stanza) do | |
114 if type(child) == "table" then | |
115 t_insert(s, preserialize(child)); | |
116 else | |
117 t_insert(s, child); | |
118 end | |
119 end | |
120 return s; | |
121 end | |
122 | |
123 function deserialize(stanza) | |
124 -- Set metatable | |
125 setmetatable(stanza, stanza_mt); | |
126 for _, child in ipairs(stanza) do | |
127 if type(child) == "table" then | |
128 deserialize(child); | |
129 end | |
130 end | |
131 if not stanza.tags then | |
132 -- Rebuild tags | |
133 local tags = {}; | |
134 for _, child in ipairs(stanza) do | |
135 if type(child) == "table" then | |
136 t_insert(tags, child); | |
137 end | |
138 end | |
139 stanza.tags = tags; | |
140 end | |
141 | |
142 return stanza; | |
143 end | |
144 | |
110 function message(attr, body) | 145 function message(attr, body) |
111 if not body then | 146 if not body then |
112 return stanza("message", attr); | 147 return stanza("message", attr); |
113 else | 148 else |
114 return stanza("message", attr):tag("body"):text(body); | 149 return stanza("message", attr):tag("body"):text(body); |