Contents of type text are now converted to UTF-8 at reception

This commit is contained in:
Mickaël Turnel 2017-12-15 10:38:28 +01:00
parent 253378d167
commit de04087a34
2 changed files with 30 additions and 1 deletions

View file

@ -21,6 +21,7 @@
#include "linphone/core.h"
#include "linphone/lpconfig.h"
#include "linphone/utils/utils.h"
#include "c-wrapper/c-wrapper.h"
#include "address/address.h"
@ -389,6 +390,32 @@ LinphoneReason ChatMessagePrivate::receive () {
contents.push_back(&internalContent);
}
// Convert PlainText contents if they have a charset different than UTF-8
for (Content *c : contents) {
if (c->getContentType() == ContentType::PlainText && !c->getContentType().getParameter().empty()) {
L_BEGIN_LOG_EXCEPTION
ContentType ct = c->getContentType();
string charset = string(ct.getParameter());
size_t n = charset.find("charset=");
if (n != string::npos) {
size_t begin = n + sizeof("charset");
size_t end = charset.find(";", begin);
charset = charset.substr(begin, end - begin);
if (Utils::stringToLower(charset) != "utf-8") {
string converted = Utils::convertString(c->getBodyAsUtf8String(), charset, "UTF-8");
if (!converted.empty()) {
c->setBodyFromUtf8(converted);
string params = ct.getParameter();
ct.setParameter(params.replace(begin, end - begin, "UTF-8"));
c->setContentType(ct);
}
}
}
L_END_LOG_EXCEPTION
}
}
// ---------------------------------------
// End of message modification
// ---------------------------------------

View file

@ -19,6 +19,8 @@
#include <algorithm>
#include "linphone/utils/utils.h"
#include "c-wrapper/c-wrapper.h"
#include "chat/chat-message/chat-message-p.h"
#include "chat/chat-room/chat-room-p.h"
@ -194,7 +196,7 @@ LinphoneReason ChatRoomPrivate::onSipMessageReceived (SalOp *op, const SalMessag
Content content;
content.setContentType(message->content_type);
content.setBody(message->text ? message->text : "");
content.setBodyFromUtf8(message->text ? message->text : "");
msg->setInternalContent(content);
msg->getPrivate()->setTime(message->time);