Comparison

util/stanza.lua @ 9307:feaef6215bb8

util.stanza: Don't automatically generate ids for iq stanzas Users of this API should provide their own id attribute that meets their uniqueness requirements. The current implementation leaks information (i.e. how many iq stanzas have been sent by the server to other JIDs). Providing any strong guarantees of randomness here would need to pull in additional dependencies that we don't want in this simple library.
author Matthew Wild <mwild1@gmail.com>
date Thu, 13 Sep 2018 16:35:48 +0100
parent 9217:7df29c5fbb9b
child 9489:09b873ac7eb8
comparison
equal deleted inserted replaced
9306:35c128b42509 9307:feaef6215bb8
345 end 345 end
346 end 346 end
347 return error_type, condition or "undefined-condition", text; 347 return error_type, condition or "undefined-condition", text;
348 end 348 end
349 349
350 local id = 0;
351 local function new_id()
352 id = id + 1;
353 return "lx"..id;
354 end
355
356 local function preserialize(stanza) 350 local function preserialize(stanza)
357 local s = { name = stanza.name, attr = stanza.attr }; 351 local s = { name = stanza.name, attr = stanza.attr };
358 for _, child in ipairs(stanza) do 352 for _, child in ipairs(stanza) do
359 if type(child) == "table" then 353 if type(child) == "table" then
360 t_insert(s, preserialize(child)); 354 t_insert(s, preserialize(child));
428 else 422 else
429 return new_stanza("message", attr):tag("body"):text(body):up(); 423 return new_stanza("message", attr):tag("body"):text(body):up();
430 end 424 end
431 end 425 end
432 local function iq(attr) 426 local function iq(attr)
433 if attr and not attr.id then attr.id = new_id(); end 427 if not (attr and attr.id) then
434 return new_stanza("iq", attr or { id = new_id() }); 428 error("iq stanzas require an id attribute");
429 end
430 return new_stanza("iq", attr);
435 end 431 end
436 432
437 local function reply(orig) 433 local function reply(orig)
438 return new_stanza(orig.name, 434 return new_stanza(orig.name,
439 orig.attr and { 435 orig.attr and {
500 496
501 return { 497 return {
502 stanza_mt = stanza_mt; 498 stanza_mt = stanza_mt;
503 stanza = new_stanza; 499 stanza = new_stanza;
504 is_stanza = is_stanza; 500 is_stanza = is_stanza;
505 new_id = new_id;
506 preserialize = preserialize; 501 preserialize = preserialize;
507 deserialize = deserialize; 502 deserialize = deserialize;
508 clone = clone; 503 clone = clone;
509 message = message; 504 message = message;
510 iq = iq; 505 iq = iq;