Commit df8aedec authored by Sylvain Berfini's avatar Sylvain Berfini 🎩

Reworked ChatMessageModifiers to use a shared_ptr<ChatMessage> instead of ChatMessagePrivate*

parent d20d39b2
......@@ -980,11 +980,11 @@ LinphoneReason ChatMessagePrivate::receive() {
if (getContentType() == ContentType::Cpim) {
CpimChatMessageModifier ccmm;
ccmm.decode(this, &errorCode);
ccmm.decode(q->getSharedFromThis(), &errorCode);
}
EncryptionChatMessageModifier ecmm;
ChatMessageModifier::Result result = ecmm.decode(this, &errorCode);
ChatMessageModifier::Result result = ecmm.decode(q->getSharedFromThis(), &errorCode);
if (result == ChatMessageModifier::Result::Error) {
/* Unable to decrypt message */
chatRoom->getPrivate()->notifyUndecryptableMessageReceived(q->getSharedFromThis());
......@@ -994,7 +994,7 @@ LinphoneReason ChatMessagePrivate::receive() {
}
MultipartChatMessageModifier mcmm;
mcmm.decode(this, &errorCode);
mcmm.decode(q->getSharedFromThis(), &errorCode);
// ---------------------------------------
// End of message modification
......@@ -1101,7 +1101,7 @@ void ChatMessagePrivate::send() {
} else {
if (contents.size() > 1) {
MultipartChatMessageModifier mcmm;
mcmm.encode(this, &errorCode);
mcmm.encode(q->getSharedFromThis(), &errorCode);
}
currentSendStep |= ChatMessagePrivate::Step::Multipart;
}
......@@ -1110,7 +1110,7 @@ void ChatMessagePrivate::send() {
lInfo() << "Encryption step already done, skipping";
} else {
EncryptionChatMessageModifier ecmm;
ChatMessageModifier::Result result = ecmm.encode(this, &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);
......@@ -1128,7 +1128,7 @@ void ChatMessagePrivate::send() {
} else {
if (lp_config_get_int(chatRoom->getCore()->config, "sip", "use_cpim", 0) == 1) {
CpimChatMessageModifier ccmm;
ccmm.encode(this, &errorCode);
ccmm.encode(q->getSharedFromThis(), &errorCode);
}
currentSendStep |= ChatMessagePrivate::Step::Cpim;
}
......@@ -1352,6 +1352,11 @@ const Content& ChatMessage::getInternalContent() const {
return d->internalContent;
}
void ChatMessage::setInternalContent(const Content& content) {
L_D();
d->internalContent = content;
}
string ChatMessage::getCustomHeaderValue (const string &headerName) const {
L_D();
try {
......
......@@ -126,7 +126,9 @@ public:
const std::list<Content>& getContents() const;
void addContent(const Content& content);
void removeContent(const Content& content);
const Content& getInternalContent() const;
void setInternalContent(const Content& content);
std::string getCustomHeaderValue(const std::string &headerName) const;
void addCustomHeader(const std::string &headerName, const std::string &headerValue);
......
......@@ -21,13 +21,14 @@
#define _CHAT_MESSAGE_MODIFIER_H_
#include "linphone/utils/general.h"
#include "object/object.h"
#include "private.h"
// =============================================================================
LINPHONE_BEGIN_NAMESPACE
class ChatMessagePrivate;
class ChatMessage;
class ChatMessageModifier {
public:
......@@ -45,13 +46,13 @@ public:
* 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 (ChatMessagePrivate *messagePrivate, int *errorCode) = 0;
virtual Result encode (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 (ChatMessagePrivate *messagePrivate, int *errorCode) = 0;
virtual Result decode (std::shared_ptr<ChatMessage> message, int *errorCode) = 0;
};
LINPHONE_END_NAMESPACE
......
......@@ -32,22 +32,22 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE
ChatMessageModifier::Result CpimChatMessageModifier::encode (ChatMessagePrivate *messagePrivate, int *errorCode) {
Cpim::Message message;
ChatMessageModifier::Result CpimChatMessageModifier::encode (shared_ptr<ChatMessage> message, int *errorCode) {
Cpim::Message cpimMessage;
Cpim::GenericHeader cpimContentTypeHeader;
cpimContentTypeHeader.setName("Content-Type");
cpimContentTypeHeader.setValue("Message/CPIM");
message.addCpimHeader(cpimContentTypeHeader);
cpimMessage.addCpimHeader(cpimContentTypeHeader);
Content content;
if (!messagePrivate->internalContent.isEmpty()) {
if (!message->getInternalContent().isEmpty()) {
// Another ChatMessageModifier was called before this one, we apply our changes on the private content
content = messagePrivate->internalContent;
content = message->getInternalContent();
} else {
// We're the first ChatMessageModifier to be called, we'll create the private content from the public one
// We take the first one because if there is more of them, the multipart modifier should have been called first
// So we should not be in this block
content = messagePrivate->contents.front();
content = message->getContents().front();
}
string contentType = content.getContentType().asString();
......@@ -57,11 +57,11 @@ ChatMessageModifier::Result CpimChatMessageModifier::encode (ChatMessagePrivate
Cpim::GenericHeader contentTypeHeader;
contentTypeHeader.setName("Content-Type");
contentTypeHeader.setValue(contentType);
message.addContentHeader(contentTypeHeader);
cpimMessage.addContentHeader(contentTypeHeader);
message.setContent(contentBody);
cpimMessage.setContent(contentBody);
if (!message.isValid()) {
if (!cpimMessage.isValid()) {
lError() << "[CPIM] Message is invalid: " << contentBody;
*errorCode = 500;
return ChatMessageModifier::Result::Error;
......@@ -69,30 +69,30 @@ ChatMessageModifier::Result CpimChatMessageModifier::encode (ChatMessagePrivate
Content newContent;
ContentType newContentType("Message/CPIM");
newContent.setContentType(newContentType);
newContent.setBody(message.asString());
messagePrivate->internalContent = newContent;
newContent.setBody(cpimMessage.asString());
message->setInternalContent(newContent);
}
return ChatMessageModifier::Result::Done;
}
ChatMessageModifier::Result CpimChatMessageModifier::decode (ChatMessagePrivate *messagePrivate, int *errorCode) {
ChatMessageModifier::Result CpimChatMessageModifier::decode (shared_ptr<ChatMessage> message, int *errorCode) {
Content content;
if (!messagePrivate->internalContent.isEmpty()) {
content = messagePrivate->internalContent;
if (!message->getInternalContent().isEmpty()) {
content = message->getInternalContent();
} else {
content = messagePrivate->contents.front();
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> message = Cpim::Message::createFromString(contentBody);
if (message && message->isValid()) {
shared_ptr<const Cpim::Message> cpimMessage = Cpim::Message::createFromString(contentBody);
if (cpimMessage && cpimMessage->isValid()) {
Content newContent;
ContentType newContentType(message->getContentHeaders()->front()->getValue());
ContentType newContentType(cpimMessage->getContentHeaders()->front()->getValue());
newContent.setContentType(newContentType);
newContent.setBody(message->getContent());
messagePrivate->internalContent = newContent;
newContent.setBody(cpimMessage->getContent());
message->setInternalContent(newContent);
} else {
lError() << "[CPIM] Message is invalid: " << contentBody;
*errorCode = 500;
......
......@@ -30,8 +30,8 @@ class CpimChatMessageModifier : public ChatMessageModifier {
public:
CpimChatMessageModifier () = default;
Result encode (ChatMessagePrivate *messagePrivate, int *errorCode) override;
Result decode (ChatMessagePrivate *messagePrivate, int *errorCode) override;
Result encode (std::shared_ptr<ChatMessage> message, int *errorCode) override;
Result decode (std::shared_ptr<ChatMessage> message, int *errorCode) override;
};
LINPHONE_END_NAMESPACE
......
......@@ -34,16 +34,17 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE
ChatMessageModifier::Result EncryptionChatMessageModifier::encode (ChatMessagePrivate *messagePrivate, int *errorCode) {
ChatMessageModifier::Result EncryptionChatMessageModifier::encode (shared_ptr<ChatMessage> message, int *errorCode) {
int retval = -1;
LinphoneImEncryptionEngine *imee = messagePrivate->chatRoom->getCore()->im_encryption_engine;
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(messagePrivate->chatRoom), L_GET_C_BACK_PTR(messagePrivate->getPublic()->getSharedFromThis()));
retval = cbProcessOutgoingMessage(imee, L_GET_C_BACK_PTR(chatRoom), L_GET_C_BACK_PTR(message->getSharedFromThis()));
if (retval == 0 || retval == 1) {
messagePrivate->isSecured = true;
message->setIsSecured(true);
if (retval == 1) {
return ChatMessageModifier::Result::Suspended;
}
......@@ -58,16 +59,17 @@ ChatMessageModifier::Result EncryptionChatMessageModifier::encode (ChatMessagePr
return ChatMessageModifier::Result::Skipped;
}
ChatMessageModifier::Result EncryptionChatMessageModifier::decode (ChatMessagePrivate *messagePrivate, int *errorCode) {
ChatMessageModifier::Result EncryptionChatMessageModifier::decode (shared_ptr<ChatMessage> message, int *errorCode) {
int retval = -1;
LinphoneImEncryptionEngine *imee = messagePrivate->chatRoom->getCore()->im_encryption_engine;
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(messagePrivate->chatRoom), L_GET_C_BACK_PTR(messagePrivate->getPublic()->getSharedFromThis()));
retval = cbProcessIncomingMessage(imee, L_GET_C_BACK_PTR(chatRoom), L_GET_C_BACK_PTR(message->getSharedFromThis()));
if (retval == 0) {
messagePrivate->isSecured = true;
message->setIsSecured(true);
return ChatMessageModifier::Result::Done;
} else if (retval == -1) {
return ChatMessageModifier::Result::Skipped;
......
......@@ -30,8 +30,8 @@ class EncryptionChatMessageModifier : public ChatMessageModifier {
public:
EncryptionChatMessageModifier () = default;
Result encode (ChatMessagePrivate *messagePrivate, int *errorCode) override;
Result decode (ChatMessagePrivate *messagePrivate, int *errorCode) override;
Result encode (std::shared_ptr<ChatMessage> message, int *errorCode) override;
Result decode (std::shared_ptr<ChatMessage> message, int *errorCode) override;
};
LINPHONE_END_NAMESPACE
......
......@@ -27,22 +27,22 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE
ChatMessageModifier::Result MultipartChatMessageModifier::encode (ChatMessagePrivate *messagePrivate, int *errorCode) {
if (messagePrivate->contents.size() > 1) {
ChatMessageModifier::Result MultipartChatMessageModifier::encode (shared_ptr<ChatMessage> message, int *errorCode) {
if (message->getContents().size() > 1) {
//TODO
return ChatMessageModifier::Result::Done;
}
return ChatMessageModifier::Result::Skipped;
}
ChatMessageModifier::Result MultipartChatMessageModifier::decode (ChatMessagePrivate *messagePrivate, int *errorCode) {
ChatMessageModifier::Result MultipartChatMessageModifier::decode (shared_ptr<ChatMessage> message, int *errorCode) {
//TODO
if (false) { // Multipart required
return ChatMessageModifier::Result::Done;
} else if (messagePrivate->contents.size() == 0) {
} else if (message->getContents().size() == 0) {
// All previous modifiers only altered the internal content, let's fill the content list because we're the last modifier to be called
messagePrivate->contents.push_back(messagePrivate->internalContent);
message->addContent(message->getInternalContent());
}
return ChatMessageModifier::Result::Skipped;
}
......
......@@ -30,8 +30,8 @@ class MultipartChatMessageModifier : public ChatMessageModifier {
public:
MultipartChatMessageModifier () = default;
Result encode (ChatMessagePrivate *message, int *errorCode) override;
Result decode (ChatMessagePrivate *message, int *errorCode) override;
Result encode (std::shared_ptr<ChatMessage> message, int *errorCode) override;
Result decode (std::shared_ptr<ChatMessage> message, int *errorCode) override;
};
LINPHONE_END_NAMESPACE
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment