diff --git a/src/message.cc b/src/message.cc index 058f5ed..7c65c23 100644 --- a/src/message.cc +++ b/src/message.cc @@ -110,7 +110,7 @@ namespace pEp { { if (encoding == "") { if (string(_bl->mime_type) == "application/pEp") - encoding = "sync"; + encoding = "pep-sync"; else encoding = "ascii"; } diff --git a/src/message_api.cc b/src/message_api.cc index 293ac0a..6a52b9c 100644 --- a/src/message_api.cc +++ b/src/message_api.cc @@ -66,7 +66,7 @@ namespace pEp { ::config_keep_sync_msg(session, enabled); } - string sync_decode(object buffer) + boost::python::tuple sync_decode(object buffer) { Py_buffer src; int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); @@ -80,10 +80,10 @@ namespace pEp { string _dst(dst); free(dst); - return _dst; + return boost::python::make_tuple(_dst, 0); } - object sync_encode(string text) + static boost::python::tuple sync_encode(string text) { char *data = NULL; size_t size = 0; @@ -95,7 +95,23 @@ namespace pEp { if (!ba) throw bad_alloc(); - return object(handle<>(ba)); + return boost::python::make_tuple(object(handle<>(ba)), 0); + } + + object sync_search(string name) + { + if (name != "pep-sync") { + return object(); + } + else { + object codecs = import("codecs"); + object CodecInfo = codecs.attr("CodecInfo"); + + object _sync_decode = make_function(sync_decode); + object _sync_encode = make_function(sync_encode); + + return call< object >(CodecInfo.ptr(), _sync_encode, _sync_decode); + } } } } diff --git a/src/message_api.hh b/src/message_api.hh index 780c5c9..6b945fc 100644 --- a/src/message_api.hh +++ b/src/message_api.hh @@ -9,8 +9,7 @@ namespace pEp { boost::python::tuple decrypt_message(Message src); int _color(int rating); void _config_keep_sync_msg(bool enabled); - string sync_decode(object buffer); - object sync_encode(string text); + object sync_search(string name); } } diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index 802ca01..84637af 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -207,8 +207,6 @@ BOOST_PYTHON_MODULE(pEp) def("color", &_color, "calculate color value out of rating"); def("trustwords", &_trustwords, "calculate trustwords for two Identities"); def("config_keep_sync_msg", &_config_keep_sync_msg, "configure if sync messages are being kept or not"); - def("sync_decode", &sync_decode, "decode sync message to XER/XML text"); - def("sync_encode", &sync_encode, "encode sync message from XER/XML text"); // key sync API @@ -223,6 +221,10 @@ BOOST_PYTHON_MODULE(pEp) .def("deliverHandshakeResult", &SyncMixIn::deliverHandshakeResult, "call to deliver the handshake result"); + // codecs + + call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(sync_search)); + // init() and release() PyModuleDef * _def = PyModule_GetDef(scope().ptr()); diff --git a/test/sync_test.py b/test/sync_test.py index dea2d53..189a861 100644 --- a/test/sync_test.py +++ b/test/sync_test.py @@ -27,21 +27,5 @@ def process(path): return Message(text) -def sync_search(name): - def _sync_decode(input, errors='strict'): - return sync_decode(input), 0 - - def _sync_encode(input, errors='strict'): - return sync_encode(input), 0 - - if name != "sync": - return None - else: - return codecs.CodecInfo(_sync_encode, _sync_decode) - - -codecs.register(sync_search) - - # this is an interactive test, so start it with python -i