Comparison

plugins/mod_pubsub/pubsub.lib.lua @ 9247:26854d7a4947

mod_pubsub: Fix dataforms error handling The :data method returns the table holding parsed values always. The second return value is a table in case some fields had problems.
author Kim Alvefur <zash@zash.se>
date Sat, 01 Sep 2018 21:18:30 +0200
parent 9245:cc32aae5c7da
child 9274:1712e366b081
comparison
equal deleted inserted replaced
9246:397e8e5a2f1f 9247:26854d7a4947
40 end 40 end
41 return reply; 41 return reply;
42 end 42 end
43 _M.pubsub_error_reply = pubsub_error_reply; 43 _M.pubsub_error_reply = pubsub_error_reply;
44 44
45 local function dataform_error_message(err) -- ({ string : string }) -> string?
46 local out = {};
47 for field, errmsg in pairs(err) do
48 table.insert(out, ("%s: %s"):format(field, errmsg))
49 end
50 return table.concat(out, "; ");
51 end
52
45 -- Note: If any config options are added that are of complex types, 53 -- Note: If any config options are added that are of complex types,
46 -- (not simply strings/numbers) then the publish-options code will 54 -- (not simply strings/numbers) then the publish-options code will
47 -- need to be revisited 55 -- need to be revisited
48 local node_config_form = dataform { 56 local node_config_form = dataform {
49 { 57 {
398 if not config_form then 406 if not config_form then
399 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing dataform")); 407 origin.send(st.error_reply(stanza, "modify", "bad-request", "Missing dataform"));
400 return true; 408 return true;
401 end 409 end
402 local form_data, err = node_config_form:data(config_form); 410 local form_data, err = node_config_form:data(config_form);
403 if not form_data then 411 if err then
404 origin.send(st.error_reply(stanza, "modify", "bad-request", err)); 412 origin.send(st.error_reply(stanza, "modify", "bad-request", dataform_error_message(err)));
405 return true; 413 return true;
406 end 414 end
407 config = form_data; 415 config = form_data;
408 end 416 end
409 if node then 417 if node then
455 origin.send(pubsub_error_reply(stanza, jid and "nodeid-required" or "invalid-jid")); 463 origin.send(pubsub_error_reply(stanza, jid and "nodeid-required" or "invalid-jid"));
456 return true; 464 return true;
457 end 465 end
458 local options_tag, options = stanza.tags[1]:get_child("options"), nil; 466 local options_tag, options = stanza.tags[1]:get_child("options"), nil;
459 if options_tag then 467 if options_tag then
460 options = subscribe_options_form:data(options_tag.tags[1]); 468 -- FIXME form parsing errors ignored here, why?
469 local err
470 options, err = subscribe_options_form:data(options_tag.tags[1]);
471 if err then
472 origin.send(st.error_reply(stanza, "modify", "bad-request", dataform_error_message(err)));
473 return true
474 end
461 end 475 end
462 local ok, ret = service:add_subscription(node, stanza.attr.from, jid, options); 476 local ok, ret = service:add_subscription(node, stanza.attr.from, jid, options);
463 local reply; 477 local reply;
464 if ok then 478 if ok then
465 reply = st.reply(stanza) 479 reply = st.reply(stanza)
531 origin.send(pubsub_error_reply(stanza, "not-subscribed")); 545 origin.send(pubsub_error_reply(stanza, "not-subscribed"));
532 return true; 546 return true;
533 end 547 end
534 local old_subopts = ret; 548 local old_subopts = ret;
535 local new_subopts, err = subscribe_options_form:data(options.tags[1], old_subopts); 549 local new_subopts, err = subscribe_options_form:data(options.tags[1], old_subopts);
536 if not new_subopts then 550 if err then
537 origin.send(pubsub_error_reply(stanza, ret)); 551 origin.send(st.error_reply(stanza, "modify", "bad-request", dataform_error_message(err)));
538 return true; 552 return true;
539 end 553 end
540 local ok, err = service:add_subscription(node, stanza.attr.from, jid, new_subopts); 554 local ok, err = service:add_subscription(node, stanza.attr.from, jid, new_subopts);
541 if not ok then 555 if not ok then
542 origin.send(pubsub_error_reply(stanza, err)); 556 origin.send(pubsub_error_reply(stanza, err));
555 local required_config = nil; 569 local required_config = nil;
556 local publish_options = stanza.tags[1]:get_child("publish-options"); 570 local publish_options = stanza.tags[1]:get_child("publish-options");
557 if publish_options then 571 if publish_options then
558 -- Ensure that the node configuration matches the values in publish-options 572 -- Ensure that the node configuration matches the values in publish-options
559 local publish_options_form = publish_options:get_child("x", "jabber:x:data"); 573 local publish_options_form = publish_options:get_child("x", "jabber:x:data");
560 required_config = node_config_form:data(publish_options_form); 574 local err;
575 required_config, err = node_config_form:data(publish_options_form);
576 if err then
577 origin.send(st.error_reply(stanza, "modify", "bad-request", dataform_error_message(err)));
578 return true
579 end
561 end 580 end
562 local item = publish:get_child("item"); 581 local item = publish:get_child("item");
563 local id = (item and item.attr.id); 582 local id = (item and item.attr.id);
564 if not id then 583 if not id then
565 id = uuid_generate(); 584 id = uuid_generate();
665 if not ok then 684 if not ok then
666 origin.send(pubsub_error_reply(stanza, old_config)); 685 origin.send(pubsub_error_reply(stanza, old_config));
667 return true; 686 return true;
668 end 687 end
669 local new_config, err = node_config_form:data(config_form, old_config); 688 local new_config, err = node_config_form:data(config_form, old_config);
670 if not new_config then 689 if err then
671 origin.send(st.error_reply(stanza, "modify", "bad-request", err)); 690 origin.send(st.error_reply(stanza, "modify", "bad-request", dataform_error_message(err)));
672 return true; 691 return true;
673 end 692 end
674 local ok, err = service:set_node_config(node, stanza.attr.from, new_config); 693 local ok, err = service:set_node_config(node, stanza.attr.from, new_config);
675 if not ok then 694 if not ok then
676 origin.send(pubsub_error_reply(stanza, err)); 695 origin.send(pubsub_error_reply(stanza, err));