Use the configured encoding value when decoding received messages
This commit is contained in:
@@ -84,6 +84,14 @@ std::string xml_unescape(const std::string& data)
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string sanitize(const std::string& data, const std::string& encoding)
|
||||||
|
{
|
||||||
|
if (utils::is_valid_utf8(data.data()))
|
||||||
|
return xml_escape(utils::remove_invalid_xml_chars(data));
|
||||||
|
else
|
||||||
|
return xml_escape(utils::remove_invalid_xml_chars(utils::convert_to_utf8(data, encoding.data())));
|
||||||
|
}
|
||||||
|
|
||||||
XmlNode::XmlNode(const std::string& name, XmlNode* parent):
|
XmlNode::XmlNode(const std::string& name, XmlNode* parent):
|
||||||
parent(parent)
|
parent(parent)
|
||||||
{
|
{
|
||||||
@@ -259,14 +267,6 @@ std::string& XmlNode::operator[](const std::string& name)
|
|||||||
return this->attributes[name];
|
return this->attributes[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string sanitize(const std::string& data)
|
|
||||||
{
|
|
||||||
if (utils::is_valid_utf8(data.data()))
|
|
||||||
return xml_escape(utils::remove_invalid_xml_chars(data));
|
|
||||||
else
|
|
||||||
return xml_escape(utils::remove_invalid_xml_chars(utils::convert_to_utf8(data, "ISO-8859-1")));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& os, const XmlNode& node)
|
std::ostream& operator<<(std::ostream& os, const XmlNode& node)
|
||||||
{
|
{
|
||||||
return os << node.to_string();
|
return os << node.to_string();
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
std::string xml_escape(const std::string& data);
|
std::string xml_escape(const std::string& data);
|
||||||
std::string xml_unescape(const std::string& data);
|
std::string xml_unescape(const std::string& data);
|
||||||
std::string sanitize(const std::string& data);
|
std::string sanitize(const std::string& data, const std::string& encoding = "ISO-8859-1");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represent an XML node. It has
|
* Represent an XML node. It has
|
||||||
|
|||||||
+34
-6
@@ -9,11 +9,35 @@
|
|||||||
#include <utils/revstr.hpp>
|
#include <utils/revstr.hpp>
|
||||||
#include <utils/split.hpp>
|
#include <utils/split.hpp>
|
||||||
#include <xmpp/jid.hpp>
|
#include <xmpp/jid.hpp>
|
||||||
|
#include <database/database.hpp>
|
||||||
|
|
||||||
using namespace std::string_literals;
|
using namespace std::string_literals;
|
||||||
|
|
||||||
static const char* action_prefix = "\01ACTION ";
|
static const char* action_prefix = "\01ACTION ";
|
||||||
|
|
||||||
|
|
||||||
|
static std::string out_encoding_for(const Bridge& bridge, const Iid& iid)
|
||||||
|
{
|
||||||
|
#ifdef USE_DATABASE
|
||||||
|
const auto jid = bridge.get_bare_jid();
|
||||||
|
auto options = Database::get_irc_channel_options_with_server_default(jid, iid.get_server(), iid.get_local());
|
||||||
|
return options.encodingOut.value();
|
||||||
|
#else
|
||||||
|
return {"ISO-8859-1"};
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::string in_encoding_for(const Bridge& bridge, const Iid& iid)
|
||||||
|
{
|
||||||
|
#ifdef USE_DATABASE
|
||||||
|
const auto jid = bridge.get_bare_jid();
|
||||||
|
auto options = Database::get_irc_channel_options_with_server_default(jid, iid.get_server(), iid.get_local());
|
||||||
|
return options.encodingIn.value();
|
||||||
|
#else
|
||||||
|
return {"ISO-8859-1"};
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
Bridge::Bridge(const std::string& user_jid, BiboumiComponent& xmpp, std::shared_ptr<Poller> poller):
|
Bridge::Bridge(const std::string& user_jid, BiboumiComponent& xmpp, std::shared_ptr<Poller> poller):
|
||||||
user_jid(user_jid),
|
user_jid(user_jid),
|
||||||
xmpp(xmpp),
|
xmpp(xmpp),
|
||||||
@@ -75,13 +99,13 @@ std::string Bridge::get_bare_jid() const
|
|||||||
return jid.local + "@" + jid.domain;
|
return jid.local + "@" + jid.domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
Xmpp::body Bridge::make_xmpp_body(const std::string& str)
|
Xmpp::body Bridge::make_xmpp_body(const std::string& str, const std::string& encoding)
|
||||||
{
|
{
|
||||||
std::string res;
|
std::string res;
|
||||||
if (utils::is_valid_utf8(str.c_str()))
|
if (utils::is_valid_utf8(str.c_str()))
|
||||||
res = str;
|
res = str;
|
||||||
else
|
else
|
||||||
res = utils::convert_to_utf8(str, "ISO-8859-1");
|
res = utils::convert_to_utf8(str, encoding.data());
|
||||||
return irc_format_to_xhtmlim(res);
|
return irc_format_to_xhtmlim(res);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -531,9 +555,10 @@ void Bridge::send_irc_version_request(const std::string& irc_hostname, const std
|
|||||||
|
|
||||||
void Bridge::send_message(const Iid& iid, const std::string& nick, const std::string& body, const bool muc)
|
void Bridge::send_message(const Iid& iid, const std::string& nick, const std::string& body, const bool muc)
|
||||||
{
|
{
|
||||||
|
const auto encoding = in_encoding_for(*this, iid);
|
||||||
if (muc)
|
if (muc)
|
||||||
this->xmpp.send_muc_message(std::to_string(iid), nick,
|
this->xmpp.send_muc_message(std::to_string(iid), nick,
|
||||||
this->make_xmpp_body(body), this->user_jid);
|
this->make_xmpp_body(body, encoding), this->user_jid);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
std::string target = std::to_string(iid);
|
std::string target = std::to_string(iid);
|
||||||
@@ -544,7 +569,7 @@ void Bridge::send_message(const Iid& iid, const std::string& nick, const std::st
|
|||||||
target = it->second;
|
target = it->second;
|
||||||
fulljid = true;
|
fulljid = true;
|
||||||
}
|
}
|
||||||
this->xmpp.send_message(target, this->make_xmpp_body(body),
|
this->xmpp.send_message(target, this->make_xmpp_body(body, encoding),
|
||||||
this->user_jid, "chat", fulljid);
|
this->user_jid, "chat", fulljid);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -590,7 +615,9 @@ void Bridge::send_xmpp_message(const std::string& from, const std::string& autho
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
body = msg;
|
body = msg;
|
||||||
this->xmpp.send_message(from, this->make_xmpp_body(body), this->user_jid, "chat");
|
|
||||||
|
const auto encoding = in_encoding_for(*this, {from});
|
||||||
|
this->xmpp.send_message(from, this->make_xmpp_body(body, encoding), this->user_jid, "chat");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bridge::send_user_join(const std::string& hostname,
|
void Bridge::send_user_join(const std::string& hostname,
|
||||||
@@ -609,7 +636,8 @@ void Bridge::send_user_join(const std::string& hostname,
|
|||||||
|
|
||||||
void Bridge::send_topic(const std::string& hostname, const std::string& chan_name, const std::string& topic)
|
void Bridge::send_topic(const std::string& hostname, const std::string& chan_name, const std::string& topic)
|
||||||
{
|
{
|
||||||
this->xmpp.send_topic(chan_name + utils::empty_if_fixed_server("%" + hostname), this->make_xmpp_body(topic), this->user_jid);
|
const auto encoding = in_encoding_for(*this, {chan_name + '%' + hostname});
|
||||||
|
this->xmpp.send_topic(chan_name + utils::empty_if_fixed_server("%" + hostname), this->make_xmpp_body(topic, encoding), this->user_jid);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string Bridge::get_own_nick(const Iid& iid)
|
std::string Bridge::get_own_nick(const Iid& iid)
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ public:
|
|||||||
const std::string& get_jid() const;
|
const std::string& get_jid() const;
|
||||||
std::string get_bare_jid() const;
|
std::string get_bare_jid() const;
|
||||||
|
|
||||||
static Xmpp::body make_xmpp_body(const std::string& str);
|
static Xmpp::body make_xmpp_body(const std::string& str, const std::string& encodin = "ISO-8859-1");
|
||||||
/***
|
/***
|
||||||
**
|
**
|
||||||
** From XMPP to IRC.
|
** From XMPP to IRC.
|
||||||
@@ -108,7 +108,7 @@ public:
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Send a message corresponding to a server NOTICE, the from attribute
|
* Send a message corresponding to a server NOTICE, the from attribute
|
||||||
* should be juste the server hostname@irc.component.
|
* should be juste the server hostname.
|
||||||
*/
|
*/
|
||||||
void send_xmpp_message(const std::string& from, const std::string& author, const std::string& msg);
|
void send_xmpp_message(const std::string& from, const std::string& author, const std::string& msg);
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -83,6 +83,12 @@ TEST_CASE("Database")
|
|||||||
THEN("we get the channel option")
|
THEN("we get the channel option")
|
||||||
CHECK(r.encodingIn == "channelEncoding");
|
CHECK(r.encodingIn == "channelEncoding");
|
||||||
}
|
}
|
||||||
|
WHEN("we fetch that option, with no channel specified")
|
||||||
|
{
|
||||||
|
auto r = Database::get_irc_channel_options_with_server_default(owner, server, "");
|
||||||
|
THEN("we get the server option")
|
||||||
|
CHECK(r.encodingIn == "serverEncoding");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user