Commit fefbec92 authored by Sylvain Berfini's avatar Sylvain Berfini 🎩
Browse files

Added enum as returned value by ChatMessageModifiers + error code as return parameter

parent 25371cbf
......@@ -969,7 +969,7 @@ void ChatMessagePrivate::createFileTransferInformationsFromVndGsmaRcsFtHttpXml()
LinphoneReason ChatMessagePrivate::receive() {
L_Q();
int errorCode = 0;
LinphoneReason reason = LinphoneReasonNone;
bool store = false;
......@@ -979,34 +979,33 @@ LinphoneReason ChatMessagePrivate::receive() {
if (getContentType() == ContentType::Cpim) {
CpimChatMessageModifier ccmm;
ccmm.decode(this);
ccmm.decode(this, &errorCode);
}
EncryptionChatMessageModifier ecmm;
int retval = 0;
retval = ecmm.decode(this);
if (retval > 0) {
ChatMessageModifier::Result result = ecmm.decode(this, &errorCode);
if (result == ChatMessageModifier::Result::Error) {
/* Unable to decrypt message */
chatRoom->getPrivate()->notifyUndecryptableMessageReceived(q->getSharedFromThis());
reason = linphone_error_code_to_reason(retval);
reason = linphone_error_code_to_reason(errorCode);
q->sendDeliveryNotification(reason);
return reason;
}
MultipartChatMessageModifier mcmm;
mcmm.decode(this);
mcmm.decode(this, &errorCode);
// ---------------------------------------
// End of message modification
// ---------------------------------------
if ((retval <= 0) && (linphone_core_is_content_type_supported(chatRoom->getCore(), getContentType().asString().c_str()) == FALSE)) {
retval = 415;
if ((errorCode <= 0) && (linphone_core_is_content_type_supported(chatRoom->getCore(), getContentType().asString().c_str()) == FALSE)) {
errorCode = 415;
lError() << "Unsupported MESSAGE (content-type " << getContentType().asString() << " not recognized)";
}
if (retval > 0) {
reason = linphone_error_code_to_reason(retval);
if (errorCode > 0) {
reason = linphone_error_code_to_reason(errorCode);
q->sendDeliveryNotification(reason);
return reason;
}
......@@ -1029,6 +1028,7 @@ void ChatMessagePrivate::send() {
L_Q();
SalOp *op = salOp;
LinphoneCall *call = NULL;
int errorCode = 0;
if ((currentSendStep & ChatMessagePrivate::Step::FileUpload) == ChatMessagePrivate::Step::FileUpload) {
lInfo() << "File upload step already done, skipping";
......@@ -1100,7 +1100,7 @@ void ChatMessagePrivate::send() {
} else {
if (contents.size() > 1) {
MultipartChatMessageModifier mcmm;
mcmm.encode(this);
mcmm.encode(this, &errorCode);
}
currentSendStep |= ChatMessagePrivate::Step::Multipart;
}
......@@ -1109,13 +1109,13 @@ void ChatMessagePrivate::send() {
lInfo() << "Encryption step already done, skipping";
} else {
EncryptionChatMessageModifier ecmm;
int retval = ecmm.encode(this);
if (retval > 0) {
if (retval > 1) {
sal_error_info_set((SalErrorInfo *)op->get_error_info(), SalReasonNotAcceptable, "SIP", retval, "Unable to encrypt IM", nullptr);
q->updateState(ChatMessage::State::NotDelivered);
q->store();
}
ChatMessageModifier::Result result = ecmm.encode(this, &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);
q->store();
return;
} else if (result == ChatMessageModifier::Result::Suspended) {
return;
}
currentSendStep |= ChatMessagePrivate::Step::Encryption;
......@@ -1126,7 +1126,7 @@ void ChatMessagePrivate::send() {
} else {
if (lp_config_get_int(chatRoom->getCore()->config, "sip", "use_cpim", 0) == 1) {
CpimChatMessageModifier ccmm;
ccmm.encode(this);
ccmm.encode(this, &errorCode);
}
currentSendStep |= ChatMessagePrivate::Step::Cpim;
}
......
......@@ -30,27 +30,27 @@ class ChatMessagePrivate;
class ChatMessageModifier {
public:
enum Result {
Skipped = -1,
Done = 0,
Suspended = 1,
Error = 2
};
virtual ~ChatMessageModifier () = default;
/**
* This method will be called when the message is about to be sent.
* 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.
* If it returns 0, it means everything went well.
* If it returns -1, it means it didn't change anything.
* If it returns 1, it means to abort the sending process and it will be resumed later.
* If it returns any other value > 1, it's an error code.
*/
virtual int encode (ChatMessagePrivate *messagePrivate) = 0;
virtual Result encode (ChatMessagePrivate *messagePrivate, 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.
* If it returns 0, it means everything went well.
* If it returns -1, it means it didn't change anything.
* If it returns any other value > 0, it's an error code.
*/
virtual int decode (ChatMessagePrivate *messagePrivate) = 0;
virtual Result decode (ChatMessagePrivate *messagePrivate, int *errorCode) = 0;
};
LINPHONE_END_NAMESPACE
......
......@@ -31,7 +31,7 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE
int CpimChatMessageModifier::encode (ChatMessagePrivate *messagePrivate) {
ChatMessageModifier::Result CpimChatMessageModifier::encode (ChatMessagePrivate *messagePrivate, int *errorCode) {
Cpim::Message message;
Cpim::GenericHeader cpimContentTypeHeader;
cpimContentTypeHeader.setName("Content-Type");
......@@ -62,7 +62,8 @@ int CpimChatMessageModifier::encode (ChatMessagePrivate *messagePrivate) {
if (!message.isValid()) {
lError() << "[CPIM] Message is invalid: " << contentBody;
return 500;
*errorCode = 500;
return ChatMessageModifier::Result::Error;
} else {
Content newContent;
ContentType newContentType("Message/CPIM");
......@@ -70,10 +71,10 @@ int CpimChatMessageModifier::encode (ChatMessagePrivate *messagePrivate) {
newContent.setBody(message.asString());
messagePrivate->internalContent = newContent;
}
return 0;
return ChatMessageModifier::Result::Done;
}
int CpimChatMessageModifier::decode (ChatMessagePrivate *messagePrivate) {
ChatMessageModifier::Result CpimChatMessageModifier::decode (ChatMessagePrivate *messagePrivate, int *errorCode) {
Content content;
if (!messagePrivate->internalContent.isEmpty()) {
content = messagePrivate->internalContent;
......@@ -93,13 +94,14 @@ int CpimChatMessageModifier::decode (ChatMessagePrivate *messagePrivate) {
messagePrivate->internalContent = newContent;
} else {
lError() << "[CPIM] Message is invalid: " << contentBody;
return 500;
*errorCode = 500;
return ChatMessageModifier::Result::Error;
}
} else {
lError() << "[CPIM] Message is not CPIM but " << content.getContentType().asString();
return -1;
return ChatMessageModifier::Result::Skipped;
}
return 0;
return ChatMessageModifier::Result::Done;
}
LINPHONE_END_NAMESPACE
......@@ -29,8 +29,8 @@ class CpimChatMessageModifier : public ChatMessageModifier {
public:
CpimChatMessageModifier () = default;
int encode (ChatMessagePrivate *messagePrivate) override;
int decode (ChatMessagePrivate *messagePrivate) override;
Result encode (ChatMessagePrivate *messagePrivate, int *errorCode) override;
Result decode (ChatMessagePrivate *messagePrivate, int *errorCode) override;
};
LINPHONE_END_NAMESPACE
......
......@@ -33,7 +33,7 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE
int EncryptionChatMessageModifier::encode (ChatMessagePrivate *messagePrivate) {
ChatMessageModifier::Result EncryptionChatMessageModifier::encode (ChatMessagePrivate *messagePrivate, int *errorCode) {
int retval = -1;
LinphoneImEncryptionEngine *imee = messagePrivate->chatRoom->getCore()->im_encryption_engine;
if (imee) {
......@@ -43,13 +43,19 @@ int EncryptionChatMessageModifier::encode (ChatMessagePrivate *messagePrivate) {
retval = cbProcessOutgoingMessage(imee, L_GET_C_BACK_PTR(messagePrivate->chatRoom), L_GET_C_BACK_PTR(messagePrivate->getPublic()->getSharedFromThis()));
if (retval == 0 || retval == 1) {
messagePrivate->isSecured = true;
if (retval == 1) {
return ChatMessageModifier::Result::Suspended;
}
return ChatMessageModifier::Result::Done;
}
*errorCode = retval;
return ChatMessageModifier::Result::Error;
}
}
return retval;
return ChatMessageModifier::Result::Skipped;
}
int EncryptionChatMessageModifier::decode (ChatMessagePrivate *messagePrivate) {
ChatMessageModifier::Result EncryptionChatMessageModifier::decode (ChatMessagePrivate *messagePrivate, int *errorCode) {
int retval = -1;
LinphoneImEncryptionEngine *imee = messagePrivate->chatRoom->getCore()->im_encryption_engine;
if (imee) {
......@@ -59,10 +65,15 @@ int EncryptionChatMessageModifier::decode (ChatMessagePrivate *messagePrivate) {
retval = cbProcessIncomingMessage(imee, L_GET_C_BACK_PTR(messagePrivate->chatRoom), L_GET_C_BACK_PTR(messagePrivate->getPublic()->getSharedFromThis()));
if (retval == 0) {
messagePrivate->isSecured = true;
return ChatMessageModifier::Result::Done;
} else if (retval == -1) {
return ChatMessageModifier::Result::Skipped;
}
*errorCode = retval;
return ChatMessageModifier::Result::Error;
}
}
return retval;
return ChatMessageModifier::Result::Skipped;
}
LINPHONE_END_NAMESPACE
......@@ -29,8 +29,8 @@ class EncryptionChatMessageModifier : public ChatMessageModifier {
public:
EncryptionChatMessageModifier () = default;
int encode (ChatMessagePrivate *messagePrivate) override;
int decode (ChatMessagePrivate *messagePrivate) override;
Result encode (ChatMessagePrivate *messagePrivate, int *errorCode) override;
Result decode (ChatMessagePrivate *messagePrivate, int *errorCode) override;
};
LINPHONE_END_NAMESPACE
......
......@@ -26,22 +26,24 @@ using namespace std;
LINPHONE_BEGIN_NAMESPACE
int MultipartChatMessageModifier::encode (ChatMessagePrivate *messagePrivate) {
ChatMessageModifier::Result MultipartChatMessageModifier::encode (ChatMessagePrivate *messagePrivate, int *errorCode) {
if (messagePrivate->contents.size() > 1) {
//TODO
return ChatMessageModifier::Result::Done;
}
return 0;
return ChatMessageModifier::Result::Skipped;
}
int MultipartChatMessageModifier::decode (ChatMessagePrivate *messagePrivate) {
ChatMessageModifier::Result MultipartChatMessageModifier::decode (ChatMessagePrivate *messagePrivate, int *errorCode) {
//TODO
if (false) { // Multipart required
return ChatMessageModifier::Result::Done;
} else if (messagePrivate->contents.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);
}
return 0;
return ChatMessageModifier::Result::Skipped;
}
LINPHONE_END_NAMESPACE
......@@ -29,8 +29,8 @@ class MultipartChatMessageModifier : public ChatMessageModifier {
public:
MultipartChatMessageModifier () = default;
int encode (ChatMessagePrivate *message) override;
int decode (ChatMessagePrivate *message) override;
Result encode (ChatMessagePrivate *message, int *errorCode) override;
Result decode (ChatMessagePrivate *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