From 69ad1f61caa8a32ce9cd0b5e9db12251f57fa5c4 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 30 Jan 2020 15:28:07 +0100 Subject: [PATCH] Totally untested and "mutmassliche" implementation of distribution codec --- src/message.cc | 17 ++++++++------- src/message_api.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++-- src/message_api.hh | 2 +- src/pEpmodule.cc | 9 ++++---- 4 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/message.cc b/src/message.cc index 8c7ec9d..9f4e5dc 100644 --- a/src/message.cc +++ b/src/message.cc @@ -45,7 +45,7 @@ namespace pEp { free(_bl->value); _bl->size = src.len; _bl->value = mem; - + PyBuffer_Release(&src); this->mime_type(mime_type); @@ -114,10 +114,14 @@ namespace pEp { { if (encoding == "") { string _mime_type = _bl->mime_type ? _bl->mime_type : ""; + encoding = "ascii"; + if (_mime_type == "application/pEp.sync") encoding = "pep.sync"; - else - encoding = "ascii"; + + if (_mime_type == "application/pEp.keyreset") + encoding = "pep.distribution"; + } object codecs = import("codecs"); object _decode = codecs.attr("decode"); @@ -158,7 +162,7 @@ namespace pEp { _msg = shared_ptr< message >(_cpy); break; - + case PEP_BUFFER_TOO_SMALL: throw runtime_error("mime_decode_message: buffer too small"); @@ -318,7 +322,7 @@ namespace pEp { throw invalid_argument("address needed"); if (from().username() == "") throw invalid_argument("username needed"); - + PEP_STATUS status = myself(adapter.session(), _msg->from); _throw_status(status); @@ -328,7 +332,7 @@ namespace pEp { return (int) rating; } - + int Message::outgoing_color() { return _color(outgoing_rating()); @@ -397,4 +401,3 @@ namespace pEp { } } } - diff --git a/src/message_api.cc b/src/message_api.cc index 8fdb07f..e73053b 100644 --- a/src/message_api.cc +++ b/src/message_api.cc @@ -36,7 +36,7 @@ namespace pEp { if (!_dst || _dst == _src) return Message(_src); - + return Message(_dst); } @@ -99,6 +99,38 @@ namespace pEp { return boost::python::make_tuple(object(handle<>(ba)), 0); } + boost::python::tuple Distribution_decode(object buffer) + { + Py_buffer src; + int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); + if (result) + throw invalid_argument("need a contiguous buffer to read"); + + char *dst = NULL; + PEP_STATUS status = PER_to_XER_Distribution_msg((char *) src.buf, src.len, &dst); + PyBuffer_Release(&src); + _throw_status(status); + + string _dst(dst); + free(dst); + return boost::python::make_tuple(_dst, 0); + } + + static boost::python::tuple Distribution_encode(string text) + { + char *data = NULL; + size_t size = 0; + PEP_STATUS status = XER_to_PER_Distribution_msg(text.c_str(), &data, &size); + _throw_status(status); + + PyObject *ba = PyBytes_FromStringAndSize(data, size); + free(data); + if (!ba) + throw bad_alloc(); + + return boost::python::make_tuple(object(handle<>(ba)), 0); + } + object sync_search(string name) { if (name != "pep.sync") { @@ -114,6 +146,22 @@ namespace pEp { return call< object >(CodecInfo.ptr(), _sync_encode, _sync_decode); } } + + object distribution_search(string name) + { + if (name != "pep.distribution") { + return object(); + } + else { + object codecs = import("codecs"); + object CodecInfo = codecs.attr("CodecInfo"); + + object _distribution_decode = make_function(Distribution_decode); + object _distribution_encode = make_function(Distribution_encode); + + return call< object >(CodecInfo.ptr(), _distribution_encode, _distribution_decode); + } + } + } } - diff --git a/src/message_api.hh b/src/message_api.hh index 0ddd864..b6afcba 100644 --- a/src/message_api.hh +++ b/src/message_api.hh @@ -12,6 +12,6 @@ namespace pEp { boost::python::tuple decrypt_message(Message src, int flags=0); int _color(int rating); object sync_search(string name); + object distribution_search(string name); } } - diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index f007ad3..4c29469 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -268,8 +268,9 @@ BOOST_PYTHON_MODULE(pEp) "\n" "decode Blob data into string depending on MIME type if encoding=''\n" "\n" - " mime_type='application/pEp.sync' decode as 'pEp.sync'\n" - " other mime_type decode as 'ascii' by default\n" + " mime_type='application/pEp.sync' decode as 'pEp.sync'\n" + " mime_type='application/pEp.keyreset' decode as 'pEp.keyreset'\n" + " other mime_type decode as 'ascii' by default\n" ) .add_property("mime_type", (string(Message::Blob::*)()) &Message::Blob::mime_type, (void(Message::Blob::*)(string)) &Message::Blob::mime_type, @@ -579,8 +580,8 @@ BOOST_PYTHON_MODULE(pEp) "True if sync is active, False otherwise\n" ); - // codecs + // codecs call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(sync_search)); + call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(distribution_search)); } -