diff options
Diffstat (limited to 'src/xmpp/biboumi_component.cpp')
-rw-r--r-- | src/xmpp/biboumi_component.cpp | 98 |
1 files changed, 63 insertions, 35 deletions
diff --git a/src/xmpp/biboumi_component.cpp b/src/xmpp/biboumi_component.cpp index 6fe6972..3cfa9d0 100644 --- a/src/xmpp/biboumi_component.cpp +++ b/src/xmpp/biboumi_component.cpp @@ -158,39 +158,50 @@ void BiboumiComponent::handle_presence(const Stanza& stanza) { const std::string own_nick = bridge->get_own_nick(iid); const XmlNode* x = stanza.get_child("x", MUC_NS); - const XmlNode* password = x ? x->get_child("password", MUC_NS): nullptr; - const XmlNode* history = x ? x->get_child("history", MUC_NS): nullptr; - HistoryLimit history_limit; - if (history) + const IrcClient* irc = bridge->find_irc_client(iid.get_server()); + // if there is no <x/>, this is a presence status update, we don’t care about those + if (x) { - const auto seconds = history->get_tag("seconds"); - if (!seconds.empty()) + const XmlNode* password = x->get_child("password", MUC_NS); + const XmlNode* history = x->get_child("history", MUC_NS); + HistoryLimit history_limit; + if (history) { - const auto now = std::chrono::system_clock::now(); - std::time_t timestamp = std::chrono::system_clock::to_time_t(now); - int int_seconds = std::atoi(seconds.data()); - timestamp -= int_seconds; - history_limit.since = utils::to_string(timestamp); + const auto seconds = history->get_tag("seconds"); + if (!seconds.empty()) + { + const auto now = std::chrono::system_clock::now(); + std::time_t timestamp = std::chrono::system_clock::to_time_t(now); + int int_seconds = std::atoi(seconds.data()); + timestamp -= int_seconds; + history_limit.since = utils::to_string(timestamp); + } + const auto since = history->get_tag("since"); + if (!since.empty()) + history_limit.since = since; + const auto maxstanzas = history->get_tag("maxstanzas"); + if (!maxstanzas.empty()) + history_limit.stanzas = std::atoi(maxstanzas.data()); + // Ignore any other value, because this is too complex to implement, + // so I won’t do it. + if (history->get_tag("maxchars") == "0") + history_limit.stanzas = 0; } - const auto since = history->get_tag("since"); - if (!since.empty()) - history_limit.since = since; - const auto maxstanzas = history->get_tag("maxstanzas"); - if (!maxstanzas.empty()) - history_limit.stanzas = std::atoi(maxstanzas.data()); - // Ignore any other value, because this is too complex to implement, - // so I won’t do it. - if (history->get_tag("maxchars") == "0") - history_limit.stanzas = 0; + bridge->join_irc_channel(iid, to.resource, password ? password->get_inner(): "", + from.resource, history_limit); } - bridge->join_irc_channel(iid, to.resource, password ? password->get_inner(): "", - from.resource, history_limit, x != nullptr); - const IrcClient* irc = bridge->find_irc_client(iid.get_server()); - if (irc) + else { - const auto chan = irc->find_channel(iid.get_local()); - if (chan->joined) - bridge->send_irc_nick_change(iid, to.resource, from.resource); + if (irc) + { + const auto chan = irc->find_channel(iid.get_local()); + if (chan && chan->joined) + bridge->send_irc_nick_change(iid, to.resource, from.resource); + else + { // send an error if we are not joined yet, instead of treating it as a join + this->send_stanza_error("presence", from_str, to_str, id, "modify", "not-acceptable", "You are not joined to this MUC."); + } + } } } else if (type == "unavailable") @@ -253,7 +264,7 @@ void BiboumiComponent::handle_presence(const Stanza& stanza) { if (type != "unavailable") this->send_stanza_error("presence", from_str, to_str, id, - "cancel", "remote-server-not-found", + "cancel", "recipient-unavailable", "Not connected to IRC server " + ex.hostname, true); } @@ -291,7 +302,26 @@ void BiboumiComponent::handle_message(const Stanza& stanza) if (body && !body->get_inner().empty()) { if (bridge->is_resource_in_chan(iid.to_tuple(), from.resource)) - bridge->send_channel_message(iid, body->get_inner(), id); + { + // Extract some XML nodes that we must include in the + // reflection (if any), because XMPP says so + std::vector<XmlNode> nodes_to_reflect; + const XmlNode* origin_id = stanza.get_child("origin-id", STABLE_ID_NS); + if (origin_id) + nodes_to_reflect.push_back(*origin_id); + const auto own_address = std::to_string(iid) + '@' + this->served_hostname; + for (const XmlNode* stanza_id: stanza.get_children("stanza-id", STABLE_ID_NS)) + { + // Stanza ID generating entities, which encounter a + // <stanza-id/> element where the 'by' attribute matches + // the 'by' attribute they would otherwise set, MUST + // delete that element even if they are not adding their + // own stanza ID. + if (stanza_id->get_tag("by") != own_address) + nodes_to_reflect.push_back(*stanza_id); + } + bridge->send_channel_message(iid, body->get_inner(), id, std::move(nodes_to_reflect)); + } else { error_type = "modify"; @@ -330,7 +360,6 @@ void BiboumiComponent::handle_message(const Stanza& stanza) if (iid.type == Iid::Type::User && !iid.get_local().empty()) { bridge->send_private_message(iid, body->get_inner()); - bridge->remove_preferred_from_jid(iid.get_local()); } else if (iid.type != Iid::Type::User && !to.resource.empty()) { // a message for chan%server@biboumi/Nick or @@ -338,7 +367,6 @@ void BiboumiComponent::handle_message(const Stanza& stanza) // Convert that into a message to nick!server Iid user_iid(utils::tolower(to.resource), iid.get_server(), Iid::Type::User); bridge->send_private_message(user_iid, body->get_inner()); - bridge->set_preferred_from_jid(user_iid.get_local(), to_str); } else if (iid.type == Iid::Type::Server) bridge->send_raw_message(iid.get_server(), body->get_inner()); @@ -368,7 +396,7 @@ void BiboumiComponent::handle_message(const Stanza& stanza) } catch (const IRCNotConnected& ex) { this->send_stanza_error("message", from_str, to_str, id, - "cancel", "remote-server-not-found", + "cancel", "recipient-unavailable", "Not connected to IRC server " + ex.hostname, true); } @@ -689,7 +717,7 @@ void BiboumiComponent::handle_iq(const Stanza& stanza) catch (const IRCNotConnected& ex) { this->send_stanza_error("iq", from, to_str, id, - "cancel", "remote-server-not-found", + "cancel", "recipient-unavailable", "Not connected to IRC server " + ex.hostname, true); stanza_error.disable(); @@ -997,7 +1025,7 @@ void BiboumiComponent::send_irc_channel_disco_info(const std::string& id, const identity["category"] = "conference"; identity["type"] = "irc"; identity["name"] = ""s + iid.get_local() + " on " + iid.get_server(); - for (const char *ns: {DISCO_INFO_NS, MUC_NS, ADHOC_NS, PING_NS, MAM_NS, VERSION_NS, STABLE_MUC_ID_NS, SELF_PING_FLAG, "muc_nonanonymous"}) + for (const char *ns: {DISCO_INFO_NS, MUC_NS, ADHOC_NS, PING_NS, MAM_NS, VERSION_NS, STABLE_MUC_ID_NS, SELF_PING_FLAG, "muc_nonanonymous", STABLE_ID_NS}) { XmlSubNode feature(query, "feature"); feature["var"] = ns; |