From a7292fba9602b2cd3c5638529afb94b52abca1b1 Mon Sep 17 00:00:00 2001 From: Luca Saiu Date: Tue, 18 Jul 2023 14:54:02 +0200 Subject: [PATCH] add instance method Message.set_recv_by This is necessary in Python applications to correctly parse incoming messages (currently from MIME) while setting the Recv-By field as required by the Engine. --- src/pEp/_pEp/message.cc | 30 ++++++++++++++++++++++++++++++ src/pEp/_pEp/message.hh | 2 ++ src/pEp/_pEp/pEpmodule.cc | 7 +++++++ 3 files changed, 39 insertions(+) diff --git a/src/pEp/_pEp/message.cc b/src/pEp/_pEp/message.cc index 2eea22d..2a83225 100644 --- a/src/pEp/_pEp/message.cc +++ b/src/pEp/_pEp/message.cc @@ -314,6 +314,36 @@ namespace pEp { _throw_status(status); } + void Message::set_recv_by(Identity &new_recv_by) + { + // Validate self and the new Recv-By identity. + ::message *msg_c = _msg.get(); + if (msg_c->dir != ::PEP_dir_incoming) { + throw invalid_argument("message not incoming"); + } + if (new_recv_by.address().empty() || new_recv_by.user_id().empty()) { + throw invalid_argument("identity incomplete"); + } + if (new_recv_by.user_id() != PEP_OWN_USERID) { + throw invalid_argument("identity non-own"); + } + + // Make a a copy of the given identity. + ::pEp_identity *new_recv_by_c = new_recv_by; + ::pEp_identity *new_recv_by_copy_c = ::identity_dup(new_recv_by_c); + if (new_recv_by_copy_c == NULL) { + throw std::bad_alloc(); + } + // If we have not failed yet this will succeed. + + // Destroy the old Recv-By. + ::free_identity(msg_c->recv_by); + msg_c->recv_by = NULL; + + // Use the copy as the new Recv-By. + msg_c->recv_by = new_recv_by_copy_c; + } + Message Message::encrypt() { boost::python::list extra; diff --git a/src/pEp/_pEp/message.hh b/src/pEp/_pEp/message.hh index 8f359fb..ed82103 100644 --- a/src/pEp/_pEp/message.hh +++ b/src/pEp/_pEp/message.hh @@ -303,6 +303,8 @@ namespace pEp { void remove_opt_field(string name); void set_opt_field(string name, string value); + void set_recv_by(Identity &new_recv_by); + Message encrypt(); Message _encrypt(boost::python::list extra, int enc_format = 4, int flags = 0); diff --git a/src/pEp/_pEp/pEpmodule.cc b/src/pEp/_pEp/pEpmodule.cc index a9e9376..86c1d6e 100644 --- a/src/pEp/_pEp/pEpmodule.cc +++ b/src/pEp/_pEp/pEpmodule.cc @@ -551,6 +551,13 @@ namespace pEp { " msg a message\n" " name the field name as a string\n" " value the new field value as a string\n") + .def("set_recv_by", &Message::set_recv_by, + "msg.set_recv_by(identity)\n" + "\n" + "Alters the given incoming message setting or replacing the Recv-By\n" + "field with the given own identity.\n" + "\n" + " identity an own identity, on which myself has been called already\n") .def("encrypt", (Message(Message::*)()) & Message::encrypt) .def( "encrypt",