Commit e8f22efe authored by louiz’'s avatar louiz’

XmlNodes are now always closed

Remove the close() method and closed attribute. Remove all the calls to
close().  (Save one bool per XmlNode, yay, and save a few ifs and some
useless function calls. At best it should be unnoticeably faster and lighter
and save a few keystrokes in the future)
parent df006a19
...@@ -25,7 +25,6 @@ void PingStep1(XmppComponent*, AdhocSession&, XmlNode& command_node) ...@@ -25,7 +25,6 @@ void PingStep1(XmppComponent*, AdhocSession&, XmlNode& command_node)
XmlNode note("note"); XmlNode note("note");
note["type"] = "info"; note["type"] = "info";
note.set_inner("Pong"); note.set_inner("Pong");
note.close();
command_node.add_child(std::move(note)); command_node.add_child(std::move(note));
} }
...@@ -35,22 +34,17 @@ void HelloStep1(XmppComponent*, AdhocSession&, XmlNode& command_node) ...@@ -35,22 +34,17 @@ void HelloStep1(XmppComponent*, AdhocSession&, XmlNode& command_node)
x["type"] = "form"; x["type"] = "form";
XmlNode title("title"); XmlNode title("title");
title.set_inner("Configure your name."); title.set_inner("Configure your name.");
title.close();
x.add_child(std::move(title)); x.add_child(std::move(title));
XmlNode instructions("instructions"); XmlNode instructions("instructions");
instructions.set_inner("Please provide your name."); instructions.set_inner("Please provide your name.");
instructions.close();
x.add_child(std::move(instructions)); x.add_child(std::move(instructions));
XmlNode name_field("field"); XmlNode name_field("field");
name_field["var"] = "name"; name_field["var"] = "name";
name_field["type"] = "text-single"; name_field["type"] = "text-single";
name_field["label"] = "Your name"; name_field["label"] = "Your name";
XmlNode required("required"); XmlNode required("required");
required.close();
name_field.add_child(std::move(required)); name_field.add_child(std::move(required));
name_field.close();
x.add_child(std::move(name_field)); x.add_child(std::move(name_field));
x.close();
command_node.add_child(std::move(x)); command_node.add_child(std::move(x));
} }
...@@ -75,7 +69,6 @@ void HelloStep2(XmppComponent*, AdhocSession& session, XmlNode& command_node) ...@@ -75,7 +69,6 @@ void HelloStep2(XmppComponent*, AdhocSession& session, XmlNode& command_node)
XmlNode note("note"); XmlNode note("note");
note["type"] = "info"; note["type"] = "info";
note.set_inner("Hello "s + value->get_inner() + "!"s); note.set_inner("Hello "s + value->get_inner() + "!"s);
note.close();
command_node.delete_all_children(); command_node.delete_all_children();
command_node.add_child(std::move(note)); command_node.add_child(std::move(note));
return; return;
...@@ -86,9 +79,7 @@ void HelloStep2(XmppComponent*, AdhocSession& session, XmlNode& command_node) ...@@ -86,9 +79,7 @@ void HelloStep2(XmppComponent*, AdhocSession& session, XmlNode& command_node)
XmlNode error(ADHOC_NS":error"); XmlNode error(ADHOC_NS":error");
error["type"] = "modify"; error["type"] = "modify";
XmlNode condition(STANZA_NS":bad-request"); XmlNode condition(STANZA_NS":bad-request");
condition.close();
error.add_child(std::move(condition)); error.add_child(std::move(condition));
error.close();
command_node.add_child(std::move(error)); command_node.add_child(std::move(error));
session.terminate(); session.terminate();
} }
...@@ -100,6 +91,5 @@ void Reload(XmppComponent*, AdhocSession&, XmlNode& command_node) ...@@ -100,6 +91,5 @@ void Reload(XmppComponent*, AdhocSession&, XmlNode& command_node)
XmlNode note("note"); XmlNode note("note");
note["type"] = "info"; note["type"] = "info";
note.set_inner("Configuration reloaded."); note.set_inner("Configuration reloaded.");
note.close();
command_node.add_child(std::move(note)); command_node.add_child(std::move(note));
} }
...@@ -36,9 +36,7 @@ XmlNode AdhocCommandsHandler::handle_request(const std::string& executor_jid, Xm ...@@ -36,9 +36,7 @@ XmlNode AdhocCommandsHandler::handle_request(const std::string& executor_jid, Xm
XmlNode error(ADHOC_NS":error"); XmlNode error(ADHOC_NS":error");
error["type"] = "cancel"; error["type"] = "cancel";
XmlNode condition(STANZA_NS":item-not-found"); XmlNode condition(STANZA_NS":item-not-found");
condition.close();
error.add_child(std::move(condition)); error.add_child(std::move(condition));
error.close();
command_node.add_child(std::move(error)); command_node.add_child(std::move(error));
} }
else if (command_it->second.is_admin_only() && else if (command_it->second.is_admin_only() &&
...@@ -47,9 +45,7 @@ XmlNode AdhocCommandsHandler::handle_request(const std::string& executor_jid, Xm ...@@ -47,9 +45,7 @@ XmlNode AdhocCommandsHandler::handle_request(const std::string& executor_jid, Xm
XmlNode error(ADHOC_NS":error"); XmlNode error(ADHOC_NS":error");
error["type"] = "cancel"; error["type"] = "cancel";
XmlNode condition(STANZA_NS":forbidden"); XmlNode condition(STANZA_NS":forbidden");
condition.close();
error.add_child(std::move(condition)); error.add_child(std::move(condition));
error.close();
command_node.add_child(std::move(error)); command_node.add_child(std::move(error));
} }
else else
...@@ -72,9 +68,7 @@ XmlNode AdhocCommandsHandler::handle_request(const std::string& executor_jid, Xm ...@@ -72,9 +68,7 @@ XmlNode AdhocCommandsHandler::handle_request(const std::string& executor_jid, Xm
XmlNode error(ADHOC_NS":error"); XmlNode error(ADHOC_NS":error");
error["type"] = "modify"; error["type"] = "modify";
XmlNode condition(STANZA_NS":bad-request"); XmlNode condition(STANZA_NS":bad-request");
condition.close();
error.add_child(std::move(condition)); error.add_child(std::move(condition));
error.close();
command_node.add_child(std::move(error)); command_node.add_child(std::move(error));
} }
else if (action == "execute" || action == "next" || action == "complete") else if (action == "execute" || action == "next" || action == "complete")
...@@ -95,9 +89,7 @@ XmlNode AdhocCommandsHandler::handle_request(const std::string& executor_jid, Xm ...@@ -95,9 +89,7 @@ XmlNode AdhocCommandsHandler::handle_request(const std::string& executor_jid, Xm
command_node["status"] = "executing"; command_node["status"] = "executing";
XmlNode actions("actions"); XmlNode actions("actions");
XmlNode next("next"); XmlNode next("next");
next.close();
actions.add_child(std::move(next)); actions.add_child(std::move(next));
actions.close();
command_node.add_child(std::move(actions)); command_node.add_child(std::move(actions));
} }
} }
...@@ -112,9 +104,7 @@ XmlNode AdhocCommandsHandler::handle_request(const std::string& executor_jid, Xm ...@@ -112,9 +104,7 @@ XmlNode AdhocCommandsHandler::handle_request(const std::string& executor_jid, Xm
XmlNode error(ADHOC_NS":error"); XmlNode error(ADHOC_NS":error");
error["type"] = "modify"; error["type"] = "modify";
XmlNode condition(STANZA_NS":bad-request"); XmlNode condition(STANZA_NS":bad-request");
condition.close();
error.add_child(std::move(condition)); error.add_child(std::move(condition));
error.close();
command_node.add_child(std::move(error)); command_node.add_child(std::move(error));
} }
} }
......
...@@ -89,12 +89,9 @@ void XmppComponent::on_connected() ...@@ -89,12 +89,9 @@ void XmppComponent::on_connected()
{ {
log_info("connected to XMPP server"); log_info("connected to XMPP server");
this->first_connection_try = true; this->first_connection_try = true;
XmlNode node("", nullptr); this->send_data("<stream:stream to='");
node.set_name("stream:stream"); this->send_data(std::string{this->served_hostname});
node["xmlns"] = COMPONENT_NS; this->send_data("' xmlns:stream='http://etherx.jabber.org/streams' xmlns='" COMPONENT_NS "'>");
node["xmlns:stream"] = STREAM_NS;
node["to"] = this->served_hostname;
this->send_stanza(node);
this->doc_open = true; this->doc_open = true;
// We may have some pending data to send: this happens when we try to send // We may have some pending data to send: this happens when we try to send
// some data before we are actually connected. We send that data right now, if any // some data before we are actually connected. We send that data right now, if any
...@@ -152,10 +149,9 @@ void XmppComponent::on_remote_stream_open(const XmlNode& node) ...@@ -152,10 +149,9 @@ void XmppComponent::on_remote_stream_open(const XmlNode& node)
sprintf(digest + (i*2), "%02x", result[i]); sprintf(digest + (i*2), "%02x", result[i]);
digest[HASH_LENGTH * 2] = '\0'; digest[HASH_LENGTH * 2] = '\0';
Stanza handshake(COMPONENT_NS":handshake"); this->send_data("<handshake xmlns='" COMPONENT_NS "'>");
handshake.set_inner(digest); this->send_data(digest);
handshake.close(); this->send_data("</handshake>");
this->send_stanza(handshake);
} }
void XmppComponent::on_remote_stream_close(const XmlNode& node) void XmppComponent::on_remote_stream_close(const XmlNode& node)
...@@ -192,9 +188,7 @@ void XmppComponent::send_stream_error(const std::string& name, const std::string ...@@ -192,9 +188,7 @@ void XmppComponent::send_stream_error(const std::string& name, const std::string
error["xmlns"] = STREAM_NS; error["xmlns"] = STREAM_NS;
if (!explanation.empty()) if (!explanation.empty())
error.set_inner(explanation); error.set_inner(explanation);
error.close();
node.add_child(std::move(error)); node.add_child(std::move(error));
node.close();
this->send_stanza(node); this->send_stanza(node);
} }
...@@ -220,19 +214,15 @@ void XmppComponent::send_stanza_error(const std::string& kind, const std::string ...@@ -220,19 +214,15 @@ void XmppComponent::send_stanza_error(const std::string& kind, const std::string
error["type"] = error_type; error["type"] = error_type;
XmlNode inner_error(defined_condition); XmlNode inner_error(defined_condition);
inner_error["xmlns"] = STANZA_NS; inner_error["xmlns"] = STANZA_NS;
inner_error.close();
error.add_child(std::move(inner_error)); error.add_child(std::move(inner_error));
if (!text.empty()) if (!text.empty())
{ {
XmlNode text_node("text"); XmlNode text_node("text");
text_node["xmlns"] = STANZA_NS; text_node["xmlns"] = STANZA_NS;
text_node.set_inner(text); text_node.set_inner(text);
text_node.close();
error.add_child(std::move(text_node)); error.add_child(std::move(text_node));
} }
error.close();
node.add_child(std::move(error)); node.add_child(std::move(error));
node.close();
this->send_stanza(node); this->send_stanza(node);
} }
...@@ -295,7 +285,6 @@ void XmppComponent::send_message(const std::string& from, Xmpp::body&& body, con ...@@ -295,7 +285,6 @@ void XmppComponent::send_message(const std::string& from, Xmpp::body&& body, con
node["type"] = type; node["type"] = type;
XmlNode body_node("body"); XmlNode body_node("body");
body_node.set_inner(std::get<0>(body)); body_node.set_inner(std::get<0>(body));
body_node.close();
node.add_child(std::move(body_node)); node.add_child(std::move(body_node));
if (std::get<1>(body)) if (std::get<1>(body))
{ {
...@@ -303,10 +292,8 @@ void XmppComponent::send_message(const std::string& from, Xmpp::body&& body, con ...@@ -303,10 +292,8 @@ void XmppComponent::send_message(const std::string& from, Xmpp::body&& body, con
html["xmlns"] = XHTMLIM_NS; html["xmlns"] = XHTMLIM_NS;
// Pass the ownership of the pointer to this xmlnode // Pass the ownership of the pointer to this xmlnode
html.add_child(std::get<1>(body).release()); html.add_child(std::get<1>(body).release());
html.close();
node.add_child(std::move(html)); node.add_child(std::move(html));
} }
node.close();
this->send_stanza(node); this->send_stanza(node);
} }
...@@ -336,19 +323,15 @@ void XmppComponent::send_user_join(const std::string& from, ...@@ -336,19 +323,15 @@ void XmppComponent::send_user_join(const std::string& from,
if (!preped_jid.empty()) if (!preped_jid.empty())
item["jid"] = preped_jid; item["jid"] = preped_jid;
} }
item.close();
x.add_child(std::move(item)); x.add_child(std::move(item));
if (self) if (self)
{ {
XmlNode status("status"); XmlNode status("status");
status["code"] = "110"; status["code"] = "110";
status.close();
x.add_child(std::move(status)); x.add_child(std::move(status));
} }
x.close();
node.add_child(std::move(x)); node.add_child(std::move(x));
node.close();
this->send_stanza(node); this->send_stanza(node);
} }
...@@ -365,14 +348,12 @@ void XmppComponent::send_invalid_room_error(const std::string& muc_name, ...@@ -365,14 +348,12 @@ void XmppComponent::send_invalid_room_error(const std::string& muc_name,
presence["type"] = "error"; presence["type"] = "error";
XmlNode x("x"); XmlNode x("x");
x["xmlns"] = MUC_NS; x["xmlns"] = MUC_NS;
x.close();
presence.add_child(std::move(x)); presence.add_child(std::move(x));
XmlNode error("error"); XmlNode error("error");
error["by"] = muc_name + "@" + this->served_hostname; error["by"] = muc_name + "@" + this->served_hostname;
error["type"] = "cancel"; error["type"] = "cancel";
XmlNode item_not_found("item-not-found"); XmlNode item_not_found("item-not-found");
item_not_found["xmlns"] = STANZA_NS; item_not_found["xmlns"] = STANZA_NS;
item_not_found.close();
error.add_child(std::move(item_not_found)); error.add_child(std::move(item_not_found));
XmlNode text("text"); XmlNode text("text");
text["xmlns"] = STANZA_NS; text["xmlns"] = STANZA_NS;
...@@ -380,11 +361,8 @@ void XmppComponent::send_invalid_room_error(const std::string& muc_name, ...@@ -380,11 +361,8 @@ void XmppComponent::send_invalid_room_error(const std::string& muc_name,
text.set_inner(muc_name + text.set_inner(muc_name +
" is not a valid IRC channel name. A correct room jid is of the form: #<chan>%<server>@" + " is not a valid IRC channel name. A correct room jid is of the form: #<chan>%<server>@" +
this->served_hostname); this->served_hostname);
text.close();
error.add_child(std::move(text)); error.add_child(std::move(text));
error.close();
presence.add_child(std::move(error)); presence.add_child(std::move(error));
presence.close();
this->send_stanza(presence); this->send_stanza(presence);
} }
...@@ -396,13 +374,11 @@ void XmppComponent::send_invalid_user_error(const std::string& user_name, const ...@@ -396,13 +374,11 @@ void XmppComponent::send_invalid_user_error(const std::string& user_name, const
message["type"] = "error"; message["type"] = "error";
XmlNode x("x"); XmlNode x("x");
x["xmlns"] = MUC_NS; x["xmlns"] = MUC_NS;
x.close();
message.add_child(std::move(x)); message.add_child(std::move(x));
XmlNode error("error"); XmlNode error("error");
error["type"] = "cancel"; error["type"] = "cancel";
XmlNode item_not_found("item-not-found"); XmlNode item_not_found("item-not-found");
item_not_found["xmlns"] = STANZA_NS; item_not_found["xmlns"] = STANZA_NS;
item_not_found.close();
error.add_child(std::move(item_not_found)); error.add_child(std::move(item_not_found));
XmlNode text("text"); XmlNode text("text");
text["xmlns"] = STANZA_NS; text["xmlns"] = STANZA_NS;
...@@ -410,11 +386,8 @@ void XmppComponent::send_invalid_user_error(const std::string& user_name, const ...@@ -410,11 +386,8 @@ void XmppComponent::send_invalid_user_error(const std::string& user_name, const
text.set_inner(user_name + text.set_inner(user_name +
" is not a valid IRC user name. A correct user jid is of the form: <nick>!<server>@" + " is not a valid IRC user name. A correct user jid is of the form: <nick>!<server>@" +
this->served_hostname); this->served_hostname);
text.close();
error.add_child(std::move(text)); error.add_child(std::move(text));
error.close();
message.add_child(std::move(error)); message.add_child(std::move(error));
message.close();
this->send_stanza(message); this->send_stanza(message);
} }
...@@ -426,9 +399,7 @@ void XmppComponent::send_topic(const std::string& from, Xmpp::body&& topic, cons ...@@ -426,9 +399,7 @@ void XmppComponent::send_topic(const std::string& from, Xmpp::body&& topic, cons
message["type"] = "groupchat"; message["type"] = "groupchat";
XmlNode subject("subject"); XmlNode subject("subject");
subject.set_inner(std::get<0>(topic)); subject.set_inner(std::get<0>(topic));
subject.close();
message.add_child(std::move(subject)); message.add_child(std::move(subject));
message.close();
this->send_stanza(message); this->send_stanza(message);
} }
...@@ -443,7 +414,6 @@ void XmppComponent::send_muc_message(const std::string& muc_name, const std::str ...@@ -443,7 +414,6 @@ void XmppComponent::send_muc_message(const std::string& muc_name, const std::str
message["type"] = "groupchat"; message["type"] = "groupchat";
XmlNode body("body"); XmlNode body("body");
body.set_inner(std::get<0>(xmpp_body)); body.set_inner(std::get<0>(xmpp_body));
body.close();
message.add_child(std::move(body)); message.add_child(std::move(body));
if (std::get<1>(xmpp_body)) if (std::get<1>(xmpp_body))
{ {
...@@ -451,10 +421,8 @@ void XmppComponent::send_muc_message(const std::string& muc_name, const std::str ...@@ -451,10 +421,8 @@ void XmppComponent::send_muc_message(const std::string& muc_name, const std::str
html["xmlns"] = XHTMLIM_NS; html["xmlns"] = XHTMLIM_NS;
// Pass the ownership of the pointer to this xmlnode // Pass the ownership of the pointer to this xmlnode
html.add_child(std::get<1>(xmpp_body).release()); html.add_child(std::get<1>(xmpp_body).release());
html.close();
message.add_child(std::move(html)); message.add_child(std::move(html));
} }
message.close();
this->send_stanza(message); this->send_stanza(message);
} }
...@@ -471,19 +439,15 @@ void XmppComponent::send_muc_leave(const std::string& muc_name, std::string&& ni ...@@ -471,19 +439,15 @@ void XmppComponent::send_muc_leave(const std::string& muc_name, std::string&& ni
{ {
XmlNode status("status"); XmlNode status("status");
status["code"] = "110"; status["code"] = "110";
status.close();
x.add_child(std::move(status)); x.add_child(std::move(status));
} }
x.close();
presence.add_child(std::move(x)); presence.add_child(std::move(x));
if (!message_str.empty()) if (!message_str.empty())
{ {
XmlNode status("status"); XmlNode status("status");
status.set_inner(message_str); status.set_inner(message_str);
status.close();
presence.add_child(std::move(status)); presence.add_child(std::move(status));
} }
presence.close();
this->send_stanza(presence); this->send_stanza(presence);
} }
...@@ -503,22 +467,17 @@ void XmppComponent::send_nick_change(const std::string& muc_name, ...@@ -503,22 +467,17 @@ void XmppComponent::send_nick_change(const std::string& muc_name,
x["xmlns"] = MUC_USER_NS; x["xmlns"] = MUC_USER_NS;
XmlNode item("item"); XmlNode item("item");
item["nick"] = new_nick; item["nick"] = new_nick;
item.close();
x.add_child(std::move(item)); x.add_child(std::move(item));
XmlNode status("status"); XmlNode status("status");
status["code"] = "303"; status["code"] = "303";
status.close();
x.add_child(std::move(status)); x.add_child(std::move(status));
if (self) if (self)
{ {
XmlNode status2("status"); XmlNode status2("status");
status2["code"] = "110"; status2["code"] = "110";
status2.close();
x.add_child(std::move(status2)); x.add_child(std::move(status2));
} }
x.close();
presence.add_child(std::move(x)); presence.add_child(std::move(x));
presence.close();
this->send_stanza(presence); this->send_stanza(presence);
this->send_user_join(muc_name, new_nick, "", affiliation, role, jid_to, self); this->send_user_join(muc_name, new_nick, "", affiliation, role, jid_to, self);
...@@ -542,21 +501,15 @@ void XmppComponent::kick_user(const std::string& muc_name, ...@@ -542,21 +501,15 @@ void XmppComponent::kick_user(const std::string& muc_name,
XmlNode actor("actor"); XmlNode actor("actor");
actor["nick"] = author; actor["nick"] = author;
actor["jid"] = author; // backward compatibility with old clients actor["jid"] = author; // backward compatibility with old clients
actor.close();
item.add_child(std::move(actor)); item.add_child(std::move(actor));
XmlNode reason("reason"); XmlNode reason("reason");
reason.set_inner(txt); reason.set_inner(txt);
reason.close();
item.add_child(std::move(reason)); item.add_child(std::move(reason));
item.close();
x.add_child(std::move(item)); x.add_child(std::move(item));
XmlNode status("status"); XmlNode status("status");
status["code"] = "307"; status["code"] = "307";
status.close();
x.add_child(std::move(status)); x.add_child(std::move(status));
x.close();
presence.add_child(std::move(x)); presence.add_child(std::move(x));
presence.close();
this->send_stanza(presence); this->send_stanza(presence);
} }
...@@ -574,7 +527,6 @@ void XmppComponent::send_presence_error(const std::string& muc_name, ...@@ -574,7 +527,6 @@ void XmppComponent::send_presence_error(const std::string& muc_name,
presence["type"] = "error"; presence["type"] = "error";
XmlNode x("x"); XmlNode x("x");
x["xmlns"] = MUC_NS; x["xmlns"] = MUC_NS;
x.close();
presence.add_child(std::move(x)); presence.add_child(std::move(x));
XmlNode error("error"); XmlNode error("error");
error["by"] = muc_name + "@" + this->served_hostname; error["by"] = muc_name + "@" + this->served_hostname;
...@@ -583,11 +535,8 @@ void XmppComponent::send_presence_error(const std::string& muc_name, ...@@ -583,11 +535,8 @@ void XmppComponent::send_presence_error(const std::string& muc_name,
error["code"] = error_code; error["code"] = error_code;
XmlNode subnode(condition); XmlNode subnode(condition);
subnode["xmlns"] = STANZA_NS; subnode["xmlns"] = STANZA_NS;
subnode.close();
error.add_child(std::move(subnode)); error.add_child(std::move(subnode));
error.close();
presence.add_child(std::move(error)); presence.add_child(std::move(error));
presence.close();
this->send_stanza(presence); this->send_stanza(presence);
} }
...@@ -605,11 +554,8 @@ void XmppComponent::send_affiliation_role_change(const std::string& muc_name, ...@@ -605,11 +554,8 @@ void XmppComponent::send_affiliation_role_change(const std::string& muc_name,
XmlNode item("item"); XmlNode item("item");
item["affiliation"] = affiliation; item["affiliation"] = affiliation;
item["role"] = role; item["role"] = role;
item.close();
x.add_child(std::move(item)); x.add_child(std::move(item));
x.close();
presence.add_child(std::move(x)); presence.add_child(std::move(x));
presence.close();
this->send_stanza(presence); this->send_stanza(presence);
} }
...@@ -627,27 +573,21 @@ void XmppComponent::send_version(const std::string& id, const std::string& jid_t ...@@ -627,27 +573,21 @@ void XmppComponent::send_version(const std::string& id, const std::string& jid_t
{ {
XmlNode name("name"); XmlNode name("name");
name.set_inner("biboumi"); name.set_inner("biboumi");
name.close();
query.add_child(std::move(name)); query.add_child(std::move(name));
XmlNode version("version"); XmlNode version("version");
version.set_inner(SOFTWARE_VERSION); version.set_inner(SOFTWARE_VERSION);
version.close();
query.add_child(std::move(version)); query.add_child(std::move(version));
XmlNode os("os"); XmlNode os("os");
os.set_inner(SYSTEM_NAME); os.set_inner(SYSTEM_NAME);
os.close();
query.add_child(std::move(os)); query.add_child(std::move(os));
} }
else else
{ {
XmlNode name("name"); XmlNode name("name");
name.set_inner(version); name.set_inner(version);
name.close();
query.add_child(std::move(name)); query.add_child(std::move(name));