Commit f5468237 authored by Ronan's avatar Ronan
Browse files

fix(CharModifier): clean code (refactor, cppcheck and errorCode is now one reference

parent 9e73b340
......@@ -21,6 +21,7 @@
#define _CHAT_MESSAGE_P_H_
#include "chat-message.h"
#include "content/content-type.h"
#include "db/events-db.h"
#include "object/object-p.h"
......
......@@ -998,11 +998,11 @@ LinphoneReason ChatMessagePrivate::receive() {
if (internalContent.getContentType() == ContentType::Cpim) {
CpimChatMessageModifier ccmm;
ccmm.decode(q->getSharedFromThis(), &errorCode);
ccmm.decode(q->getSharedFromThis(), errorCode);
}
EncryptionChatMessageModifier ecmm;
ChatMessageModifier::Result result = ecmm.decode(q->getSharedFromThis(), &errorCode);
ChatMessageModifier::Result result = ecmm.decode(q->getSharedFromThis(), errorCode);
if (result == ChatMessageModifier::Result::Error) {
/* Unable to decrypt message */
chatRoom->getPrivate()->notifyUndecryptableMessageReceived(q->getSharedFromThis());
......@@ -1012,7 +1012,7 @@ LinphoneReason ChatMessagePrivate::receive() {
}
MultipartChatMessageModifier mcmm;
mcmm.decode(q->getSharedFromThis(), &errorCode);
mcmm.decode(q->getSharedFromThis(), errorCode);
if (contents.size() == 0) {
// All previous modifiers only altered the internal content, let's fill the content list
......@@ -1138,7 +1138,7 @@ void ChatMessagePrivate::send() {
} else {
if (contents.size() > 1) {
MultipartChatMessageModifier mcmm;
mcmm.encode(q->getSharedFromThis(), &errorCode);
mcmm.encode(q->getSharedFromThis(), errorCode);
}
currentSendStep |= ChatMessagePrivate::Step::Multipart;
}
......@@ -1147,7 +1147,7 @@ void ChatMessagePrivate::send() {
lInfo() << "Encryption step already done, skipping";
} else {
EncryptionChatMessageModifier ecmm;
ChatMessageModifier::Result result = ecmm.encode(q->getSharedFromThis(), &errorCode);
ChatMessageModifier::Result result = ecmm.encode(q->getSharedFromThis(), errorCode);
if (result == ChatMessageModifier::Result::Error) {
sal_error_info_set((SalErrorInfo *)op->get_error_info(), SalReasonNotAcceptable, "SIP", errorCode, "Unable to encrypt IM", nullptr);
q->updateState(ChatMessage::State::NotDelivered);
......@@ -1165,7 +1165,7 @@ void ChatMessagePrivate::send() {
} else {
if (lp_config_get_int(chatRoom->getCore()->config, "sip", "use_cpim", 0) == 1) {
CpimChatMessageModifier ccmm;
ccmm.encode(q->getSharedFromThis(), &errorCode);
ccmm.encode(q->getSharedFromThis(), errorCode);
}
currentSendStep |= ChatMessagePrivate::Step::Cpim;
}
......
......@@ -20,9 +20,7 @@
#ifndef _CHAT_MESSAGE_MODIFIER_H_
#define _CHAT_MESSAGE_MODIFIER_H_
#include "linphone/utils/general.h"
#include "object/object.h"
#include "private.h"
// =============================================================================
......@@ -32,7 +30,7 @@ class ChatMessage;
class ChatMessageModifier {
public:
enum Result {
enum class Result {
Skipped = -1,
Done = 0,
Suspended = 1,
......@@ -43,16 +41,16 @@ public:
/**
* This method will be called when the message is about to be sent.
* It should check first if the internalContent is filled.
* It should check first if the internalContent is filled.
* If so, it should apply it's changes to it, otherwise it should use the contentsList.
*/
virtual Result encode (const std::shared_ptr<ChatMessage> &message, int *errorCode) = 0;
virtual Result encode (const std::shared_ptr<ChatMessage> &message, int &errorCode) = 0;
/**
* This method will be called when the message is about to be received.
* It should apply it's changes to the internal content, the last modifier will take care of filling the contentsList.
*/
virtual Result decode (const std::shared_ptr<ChatMessage> &message, int *errorCode) = 0;
virtual Result decode (const std::shared_ptr<ChatMessage> &message, int &errorCode) = 0;
};
LINPHONE_END_NAMESPACE
......
......@@ -17,14 +17,14 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "cpim-chat-message-modifier.h"
#include "address/address.h"
#include "chat/chat-message.h"
#include "chat/cpim/cpim.h"
#include "content/content-type.h"
#include "content/content.h"
#include "address/address.h"
#include "logger/logger.h"
#include "chat/chat-message.h"
#include "cpim-chat-message-modifier.h"
// =============================================================================
......@@ -32,7 +32,7 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE
ChatMessageModifier::Result CpimChatMessageModifier::encode (const shared_ptr<ChatMessage> &message, int *errorCode) {
ChatMessageModifier::Result CpimChatMessageModifier::encode (const shared_ptr<ChatMessage> &message, int &errorCode) {
Cpim::Message cpimMessage;
Cpim::GenericHeader cpimContentTypeHeader;
cpimContentTypeHeader.setName("Content-Type");
......@@ -50,58 +50,53 @@ ChatMessageModifier::Result CpimChatMessageModifier::encode (const shared_ptr<Ch
content = message->getContents().front();
}
string contentType = content.getContentType().asString();
const vector<char> body = content.getBody();
string contentBody(body.begin(), body.end());
Cpim::GenericHeader contentTypeHeader;
contentTypeHeader.setName("Content-Type");
contentTypeHeader.setValue(contentType);
contentTypeHeader.setValue(content.getContentType().asString());
cpimMessage.addContentHeader(contentTypeHeader);
const string contentBody = content.getBodyAsString();
cpimMessage.setContent(contentBody);
if (!cpimMessage.isValid()) {
lError() << "[CPIM] Message is invalid: " << contentBody;
*errorCode = 500;
errorCode = 500;
return ChatMessageModifier::Result::Error;
} else {
Content newContent;
ContentType newContentType("Message/CPIM");
newContent.setContentType(newContentType);
newContent.setBody(cpimMessage.asString());
message->setInternalContent(newContent);
}
Content newContent;
newContent.setContentType(ContentType("Message/CPIM"));
newContent.setBody(cpimMessage.asString());
message->setInternalContent(newContent);
return ChatMessageModifier::Result::Done;
}
ChatMessageModifier::Result CpimChatMessageModifier::decode (const shared_ptr<ChatMessage> &message, int *errorCode) {
ChatMessageModifier::Result CpimChatMessageModifier::decode (const shared_ptr<ChatMessage> &message, int &errorCode) {
Content content;
if (!message->getInternalContent().isEmpty()) {
if (!message->getInternalContent().isEmpty())
content = message->getInternalContent();
} else {
else
content = message->getContents().front();
}
if (content.getContentType() == ContentType::Cpim) {
const vector<char> body = content.getBody();
string contentBody(body.begin(), body.end());
shared_ptr<const Cpim::Message> cpimMessage = Cpim::Message::createFromString(contentBody);
if (cpimMessage && cpimMessage->isValid()) {
Content newContent;
ContentType newContentType(cpimMessage->getContentHeaders()->front()->getValue());
newContent.setContentType(newContentType);
newContent.setBody(cpimMessage->getContent());
message->setInternalContent(newContent);
} else {
lError() << "[CPIM] Message is invalid: " << contentBody;
*errorCode = 500;
return ChatMessageModifier::Result::Error;
}
} else {
if (content.getContentType() != ContentType::Cpim) {
lError() << "[CPIM] Message is not CPIM but " << content.getContentType().asString();
return ChatMessageModifier::Result::Skipped;
}
const string contentBody = content.getBodyAsString();
const shared_ptr<const Cpim::Message> cpimMessage = Cpim::Message::createFromString(contentBody);
if (!cpimMessage || !cpimMessage->isValid()) {
lError() << "[CPIM] Message is invalid: " << contentBody;
errorCode = 500;
return ChatMessageModifier::Result::Error;
}
Content newContent;
newContent.setContentType(ContentType(cpimMessage->getContentHeaders()->front()->getValue()));
newContent.setBody(cpimMessage->getContent());
message->setInternalContent(newContent);
return ChatMessageModifier::Result::Done;
}
......
......@@ -30,8 +30,8 @@ class CpimChatMessageModifier : public ChatMessageModifier {
public:
CpimChatMessageModifier () = default;
Result encode (const std::shared_ptr<ChatMessage> &message, int *errorCode) override;
Result decode (const std::shared_ptr<ChatMessage> &message, int *errorCode) override;
Result encode (const std::shared_ptr<ChatMessage> &message, int &errorCode) override;
Result decode (const std::shared_ptr<ChatMessage> &message, int &errorCode) override;
};
LINPHONE_END_NAMESPACE
......
......@@ -17,16 +17,14 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "encryption-chat-message-modifier.h"
#include "object/object-p.h"
#include "address/address.h"
#include "c-wrapper/c-wrapper.h"
#include "chat/chat-message.h"
#include "chat/chat-room.h"
#include "content/content-type.h"
#include "content/content.h"
#include "address/address.h"
#include "chat/chat-room.h"
#include "chat/chat-message.h"
#include "encryption-chat-message-modifier.h"
// =============================================================================
......@@ -34,51 +32,67 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE
ChatMessageModifier::Result EncryptionChatMessageModifier::encode (const shared_ptr<ChatMessage> &message, int *errorCode) {
int retval = -1;
ChatMessageModifier::Result EncryptionChatMessageModifier::encode (
const shared_ptr<ChatMessage> &message,
int &errorCode
) {
shared_ptr<ChatRoom> chatRoom = message->getChatRoom();
LinphoneImEncryptionEngine *imee = chatRoom->getCore()->im_encryption_engine;
if (imee) {
LinphoneImEncryptionEngineCbs *imeeCbs = linphone_im_encryption_engine_get_callbacks(imee);
LinphoneImEncryptionEngineCbsOutgoingMessageCb cbProcessOutgoingMessage = linphone_im_encryption_engine_cbs_get_process_outgoing_message(imeeCbs);
if (cbProcessOutgoingMessage) {
retval = cbProcessOutgoingMessage(imee, L_GET_C_BACK_PTR(chatRoom), L_GET_C_BACK_PTR(message->getSharedFromThis()));
if (retval == 0 || retval == 1) {
message->setIsSecured(true);
if (retval == 1) {
return ChatMessageModifier::Result::Suspended;
}
return ChatMessageModifier::Result::Done;
} else if (retval == -1) {
return ChatMessageModifier::Result::Skipped;
}
*errorCode = retval;
return ChatMessageModifier::Result::Error;
}
if (!imee)
return ChatMessageModifier::Result::Skipped;
LinphoneImEncryptionEngineCbsOutgoingMessageCb cbProcessOutgoingMessage =
linphone_im_encryption_engine_cbs_get_process_outgoing_message(
linphone_im_encryption_engine_get_callbacks(imee)
);
if (!cbProcessOutgoingMessage)
return ChatMessageModifier::Result::Skipped;
int retval = cbProcessOutgoingMessage(imee, L_GET_C_BACK_PTR(chatRoom), L_GET_C_BACK_PTR(message));
if (retval == -1)
return ChatMessageModifier::Result::Skipped;
if (retval != 0 && retval != 1) {
errorCode = retval;
return ChatMessageModifier::Result::Error;
}
return ChatMessageModifier::Result::Skipped;
message->setIsSecured(true);
if (retval == 1)
return ChatMessageModifier::Result::Suspended;
return ChatMessageModifier::Result::Done;
}
ChatMessageModifier::Result EncryptionChatMessageModifier::decode (const shared_ptr<ChatMessage> &message, int *errorCode) {
int retval = -1;
ChatMessageModifier::Result EncryptionChatMessageModifier::decode (
const shared_ptr<ChatMessage> &message,
int &errorCode
) {
shared_ptr<ChatRoom> chatRoom = message->getChatRoom();
LinphoneImEncryptionEngine *imee = chatRoom->getCore()->im_encryption_engine;
if (imee) {
LinphoneImEncryptionEngineCbs *imeeCbs = linphone_im_encryption_engine_get_callbacks(imee);
LinphoneImEncryptionEngineCbsIncomingMessageCb cbProcessIncomingMessage = linphone_im_encryption_engine_cbs_get_process_incoming_message(imeeCbs);
if (cbProcessIncomingMessage) {
retval = cbProcessIncomingMessage(imee, L_GET_C_BACK_PTR(chatRoom), L_GET_C_BACK_PTR(message->getSharedFromThis()));
if (retval == 0) {
message->setIsSecured(true);
return ChatMessageModifier::Result::Done;
} else if (retval == -1) {
return ChatMessageModifier::Result::Skipped;
}
*errorCode = retval;
return ChatMessageModifier::Result::Error;
}
if (!imee)
return ChatMessageModifier::Result::Skipped;
LinphoneImEncryptionEngineCbsIncomingMessageCb cbProcessIncomingMessage =
linphone_im_encryption_engine_cbs_get_process_incoming_message(
linphone_im_encryption_engine_get_callbacks(imee)
);
if (!cbProcessIncomingMessage)
return ChatMessageModifier::Result::Skipped;
int retval = cbProcessIncomingMessage(imee, L_GET_C_BACK_PTR(chatRoom), L_GET_C_BACK_PTR(message));
if (retval != 0 && retval != -1) {
errorCode = retval;
return ChatMessageModifier::Result::Error;
}
return ChatMessageModifier::Result::Skipped;
if (retval == -1)
return ChatMessageModifier::Result::Skipped;
message->setIsSecured(true);
return ChatMessageModifier::Result::Done;
}
LINPHONE_END_NAMESPACE
......@@ -30,8 +30,8 @@ class EncryptionChatMessageModifier : public ChatMessageModifier {
public:
EncryptionChatMessageModifier () = default;
Result encode (const std::shared_ptr<ChatMessage> &message, int *errorCode) override;
Result decode (const std::shared_ptr<ChatMessage> &message, int *errorCode) override;
Result encode (const std::shared_ptr<ChatMessage> &message, int &errorCode) override;
Result decode (const std::shared_ptr<ChatMessage> &message, int &errorCode) override;
};
LINPHONE_END_NAMESPACE
......
......@@ -17,49 +17,53 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "multipart-chat-message-modifier.h"
#include "address/address.h"
#include "chat/chat-room.h"
#include "chat/chat-message.h"
#include "chat/chat-room.h"
#include "content/content-type.h"
#include "logger/logger.h"
#include "multipart-chat-message-modifier.h"
// =============================================================================
using namespace std;
LINPHONE_BEGIN_NAMESPACE
ChatMessageModifier::Result MultipartChatMessageModifier::encode (const shared_ptr<ChatMessage> &message, int *errorCode) {
if (message->getContents().size() > 1) {
LinphoneCore *lc = message->getChatRoom()->getCore();
char tmp[64];
lc->sal->create_uuid(tmp, sizeof(tmp));
string boundary = tmp;
stringstream multipartMessage;
ChatMessageModifier::Result MultipartChatMessageModifier::encode (
const shared_ptr<ChatMessage> &message,
int &errorCode
) {
if (message->getContents().size() <= 1)
return ChatMessageModifier::Result::Skipped;
LinphoneCore *lc = message->getChatRoom()->getCore();
char tmp[64];
lc->sal->create_uuid(tmp, sizeof(tmp));
string boundary = tmp;
stringstream multipartMessage;
multipartMessage << "--" << boundary;
for (const auto &content : message->getContents()) {
multipartMessage << "\r\n";
multipartMessage << "Content-Type: " << content.getContentType().asString() << "\r\n\r\n";
multipartMessage << content.getBodyAsString() << "\r\n\r\n";
multipartMessage << "--" << boundary;
for (auto it = message->getContents().begin(); it != message->getContents().end(); it++) {
multipartMessage << "\r\n";
multipartMessage << "Content-Type: " << it->getContentType().asString() << "\r\n\r\n";
multipartMessage << it->getBodyAsString() << "\r\n\r\n";
multipartMessage << "--" << boundary;
}
multipartMessage << "--";
}
multipartMessage << "--";
Content newContent;
ContentType newContentType("multipart/mixed");
newContentType.setParameter("boundary=" + boundary);
newContent.setContentType(newContentType);
newContent.setBody(multipartMessage.str());
message->setInternalContent(newContent);
Content newContent;
ContentType newContentType("multipart/mixed");
newContentType.setParameter("boundary=" + boundary);
newContent.setContentType(newContentType);
newContent.setBody(multipartMessage.str());
message->setInternalContent(newContent);
return ChatMessageModifier::Result::Done;
}
return ChatMessageModifier::Result::Skipped;
}
return ChatMessageModifier::Result::Done;
}
ChatMessageModifier::Result MultipartChatMessageModifier::decode (const shared_ptr<ChatMessage> &message, int *errorCode) {
ChatMessageModifier::Result MultipartChatMessageModifier::decode (const shared_ptr<ChatMessage> &message, int &errorCode) {
if (message->getInternalContent().getContentType().getType() == "multipart") {
string boundary = message->getInternalContent().getContentType().getParameter();
if (boundary.empty()) {
......@@ -74,11 +78,11 @@ ChatMessageModifier::Result MultipartChatMessageModifier::decode (const shared_p
}
boundary = "--" + boundary.substr(pos + 1);
lInfo() << "Multipart boundary is " << boundary;
const vector<char> body = message->getInternalContent().getBody();
string contentsString(body.begin(), body.end());
pos = contentsString.find(boundary);
pos = contentsString.find(boundary);
if (pos == string::npos) {
lError() << "Boundary not found in body !";
return ChatMessageModifier::Result::Error;
......@@ -89,9 +93,9 @@ ChatMessageModifier::Result MultipartChatMessageModifier::decode (const shared_p
do {
end = contentsString.find(boundary, start);
if (end != string::npos) {
string contentString = contentsString.substr(start, end-start);
string contentString = contentsString.substr(start, end - start);
size_t contentTypePos = contentString.find(": ") + 2; // 2 is the size of :
size_t contentTypePos = contentString.find(": ") + 2; // 2 is the size of :
size_t endOfLinePos = contentString.find("\r\n");
if (contentTypePos >= endOfLinePos) {
lError() << "Content should start by a 'Content-Type: ' line !";
......
......@@ -30,8 +30,8 @@ class MultipartChatMessageModifier : public ChatMessageModifier {
public:
MultipartChatMessageModifier () = default;
Result encode (const std::shared_ptr<ChatMessage> &message, int *errorCode) override;
Result decode (const std::shared_ptr<ChatMessage> &message, int *errorCode) override;
Result encode (const std::shared_ptr<ChatMessage> &message, int &errorCode) override;
Result decode (const std::shared_ptr<ChatMessage> &message, int &errorCode) override;
};
LINPHONE_END_NAMESPACE
......
......@@ -22,7 +22,6 @@
#include <vector>
#include "content-type.h"
#include "object/app-data-container.h"
#include "object/clonable-object.h"
......@@ -30,6 +29,7 @@
LINPHONE_BEGIN_NAMESPACE
class ContentType;
class ContentPrivate;
class LINPHONE_PUBLIC Content : public ClonableObject, public AppDataContainer {
......
......@@ -29,6 +29,7 @@
#include "abstract/abstract-db-p.h"
#include "chat/chat-message.h"
#include "conference/participant.h"
#include "content/content-type.h"
#include "content/content.h"
#include "db/provider/db-session-provider.h"
#include "event-log/call-event.h"
......
......@@ -24,6 +24,8 @@
#include <bctoolbox/defs.h>
#include <belle-sip/provider.h>
#include "content/content-type.h"
using namespace std;
LINPHONE_BEGIN_NAMESPACE
......@@ -35,15 +37,15 @@ SalCallOp::~SalCallOp() {
int SalCallOp::set_local_media_description(SalMediaDescription *desc) {
if (desc) sal_media_description_ref(desc);
belle_sip_error_code error;
belle_sdp_session_description_t *sdp = media_description_to_sdp(desc);
vector<char> buffer = marshal_media_description(sdp, error);
if (error != BELLE_SIP_OK) return -1;
this->local_body.setContentType(ContentType::Sdp);
this->local_body.setBody(move(buffer));
if (this->local_media) sal_media_description_unref(this->local_media);
this->local_media=desc;
......@@ -66,7 +68,7 @@ int SalCallOp::set_local_body(const Content &body) {
int SalCallOp::set_local_body(const Content &&body) {
if (!body.isValid()) return -1;
if (body.getContentType() == ContentType::Sdp) {
SalMediaDescription *desc = NULL;
if (body.getSize() > 0) {
......@@ -81,7 +83,7 @@ int SalCallOp::set_local_body(const Content &&body) {
if (this->local_media) sal_media_description_unref(this->local_media);
this->local_media = desc;
}
this->local_body = body;
return 0;
}
......@@ -98,12 +100,12 @@ int SalCallOp::set_custom_body(belle_sip_message_t *msg, const Content &body) {
ContentType contentType = body.getContentType();
string contentDisposition = body.getContentDisposition();
size_t bodySize = body.getBody().size();
if (bodySize > SIP_MESSAGE_BODY_LIMIT) {
bctbx_error("trying to add a body greater than %lukB to message [%p]", (unsigned long)SIP_MESSAGE_BODY_LIMIT/1024, msg);
return -1;
}
if (contentType.isValid()) {
belle_sip_header_content_type_t *content_type = belle_sip_header_content_type_create(contentType.getType().c_str(), contentType.getSubType().c_str());
belle_sip_message_add_header(msg, BELLE_SIP_HEADER(content_type));
......@@ -114,13 +116,13 @@ int SalCallOp::set_custom_body(belle_sip_message_t *msg, const Content &body) {
}
belle_sip_header_content_length_t *content_length = belle_sip_header_content_length_create(bodySize);
belle_sip_message_add_header(msg, BELLE_SIP_HEADER(content_length));
if (bodySize > 0) {
char *buffer = bctbx_new(char, bodySize);
memcpy(buffer, body.getBody().data(), bodySize);
belle_sip_message_assign_body(msg, buffer, bodySize);
}
return 0;
}
......@@ -139,25 +141,25 @@ std::vector<char> SalCallOp::marshal_media_description(belle_sdp_session_descrip
buff.resize(bufLen);
}
}
/* give up if hard limit reached */
if (error != BELLE_SIP_OK) {
ms_error("Buffer too small (%d) or not enough memory, giving up SDP", (int)bufLen);
return std::vector<char>(); // return a new vector in order to free the buffer held by 'buff' vector
}
buff.resize(length);
return buff;
}
int SalCallOp::set_sdp(belle_sip_message_t *msg,belle_sdp_session_description_t* session_desc) {