From 7ee3d324ee8bb07e31d630ae130e4a5b0fc86078 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 19 Aug 2020 11:24:46 +0200 Subject: [PATCH 01/13] remove usage of local adapter.hh/locked_queue.hh / use session() from libpEpAdapter --- setup.py | 16 +++++- src/adapter.cc | 40 ++++++------- src/adapter.hh | 2 +- src/basic_api.cc | 27 +++++---- src/identity.cc | 27 ++++----- src/identity.hh | 7 ++- src/message.cc | 18 +++--- src/message.hh | 4 +- src/message_api.cc | 9 +-- src/pEpmodule.cc | 129 ++++++++++++++++++++++-------------------- src/pEpmodule.hh | 10 ++-- src/str_attr.cc | 3 +- src/str_attr.hh | 1 + src/user_interface.cc | 59 ++++++++++--------- src/user_interface.hh | 7 ++- 15 files changed, 198 insertions(+), 161 deletions(-) diff --git a/setup.py b/setup.py index a970502..7f64886 100644 --- a/setup.py +++ b/setup.py @@ -90,6 +90,7 @@ class BuildExtCommand(build_ext): ] libs = [ 'pEpEngine', + 'pEpAdapter', 'boost_python37-mt', 'boost_locale-mt' ] @@ -111,6 +112,7 @@ class BuildExtCommand(build_ext): ] libs = [ 'pEpEngine', + 'pEpAdapter', 'boost_python37-mt', 'boost_locale-mt' ] @@ -129,6 +131,7 @@ class BuildExtCommand(build_ext): ] libs = [ 'pEpEngine', + 'pEpAdapter', 'boost_python3', 'boost_locale' ] @@ -185,7 +188,7 @@ class BuildExtCommand(build_ext): libdirs += sys_libdirs # Compile flags - compile_flags = ['-std=c++14'] + compile_flags = ['-std=c++14', '-fpermissive'] if self.debug: pEpLog("debug mode") compile_flags += ['-O0', '-g', '-UNDEBUG'] @@ -218,7 +221,16 @@ if sys.version_info[0] < 3: module_pEp = Extension( 'pEp', - sources = glob('src/*.cc'), + sources = [ + 'src/pEpmodule.cc', + 'src/basic_api.cc', + 'src/identity.cc', + 'src/message.cc', + 'src/message_api.cc', + 'src/str_attr.cc', + # 'src/user_interface.cc', + # 'src/adapter.cc' + ], ) # "MAIN" Function diff --git a/src/adapter.cc b/src/adapter.cc index 1973dd8..008644b 100644 --- a/src/adapter.cc +++ b/src/adapter.cc @@ -3,6 +3,7 @@ #include "user_interface.hh" #include +#include "adapter.hh" namespace pEp { namespace PythonAdapter { @@ -42,7 +43,8 @@ namespace pEp { case init: ++booked; if (!_session) - status = ::init(&_session, _messageToSend, _inject_sync_event); + status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase); +// status = ::init(&_session, _messageToSend, _inject_sync_event); break; default: @@ -74,24 +76,24 @@ namespace pEp { return obj; } - int Adapter::_inject_sync_event(SYNC_EVENT ev, void *management) - { - if (!flag_sync_enabled) - return 1; - - if (is_sync_thread(adapter.session())) { - PEP_STATUS status = do_sync_protocol_step(adapter.session(), adapter.ui_object(), ev); - return status == PEP_STATUS_OK ? 0 : 1; - } - - try { - queue().push_back(ev); - } - catch (exception&) { - return 1; - } - return 0; - } +// int Adapter::_inject_sync_event(SYNC_EVENT ev, void *management) +// { +// if (!flag_sync_enabled) +// return 1; +// +// if (is_sync_thread(adapter.session())) { +// PEP_STATUS status = do_sync_protocol_step(adapter.session(), adapter.ui_object(), ev); +// return status == PEP_STATUS_OK ? 0 : 1; +// } +// +// try { +// queue().push_back(ev); +// } +// catch (exception&) { +// return 1; +// } +// return 0; +// } } } diff --git a/src/adapter.hh b/src/adapter.hh index a996910..634e65c 100644 --- a/src/adapter.hh +++ b/src/adapter.hh @@ -4,7 +4,7 @@ #pragma once #include "pEpmodule.hh" -#include "locked_queue.hh" +#include "pEp/locked_queue.hh" #include "user_interface.hh" #include diff --git a/src/basic_api.cc b/src/basic_api.cc index 05f3e30..3070446 100644 --- a/src/basic_api.cc +++ b/src/basic_api.cc @@ -1,10 +1,13 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#include "basic_api.hh" #include + #include #include +#include + +#include "basic_api.hh" namespace pEp { namespace PythonAdapter { @@ -16,7 +19,7 @@ namespace pEp { throw runtime_error("update_identity: '" PEP_OWN_USERID "' may only be used for own identities"); - PEP_STATUS status = update_identity(adapter.session(), ident); + PEP_STATUS status = update_identity(pEp::Adapter::session(), ident); _throw_status(status); } @@ -30,7 +33,7 @@ namespace pEp { if (ident.user_id() == "") ident.user_id(ident.address()); - PEP_STATUS status = myself(adapter.session(), ident); + PEP_STATUS status = myself(pEp::Adapter::session(), ident); _throw_status(status); } @@ -44,7 +47,7 @@ namespace pEp { char *words = NULL; size_t size = 0; - PEP_STATUS status = get_trustwords(adapter.session(), me, partner, + PEP_STATUS status = get_trustwords(pEp::Adapter::session(), me, partner, lang.c_str(),&words, &size, full); _throw_status(status); return words; @@ -57,7 +60,7 @@ namespace pEp { if (ident.user_id() == "") throw invalid_argument("user_id must be provided"); - PEP_STATUS status = trust_personal_key(adapter.session(), ident); + PEP_STATUS status = trust_personal_key(pEp::Adapter::session(), ident); _throw_status(status); } @@ -68,7 +71,7 @@ namespace pEp { if (ident.user_id() == "") throw invalid_argument("user_id needed"); - PEP_STATUS status = set_identity_flags(adapter.session(), ident, flags); + PEP_STATUS status = set_identity_flags(pEp::Adapter::session(), ident, flags); _throw_status(status); } @@ -79,7 +82,7 @@ namespace pEp { if (ident.user_id() == "") throw invalid_argument("user_id needed"); - PEP_STATUS status = unset_identity_flags(adapter.session(), ident, flags); + PEP_STATUS status = unset_identity_flags(pEp::Adapter::session(), ident, flags); _throw_status(status); } @@ -92,7 +95,7 @@ namespace pEp { if (ident.user_id() == "") throw invalid_argument("user_id needed"); - PEP_STATUS status = key_reset_trust(adapter.session(), ident); + PEP_STATUS status = key_reset_trust(pEp::Adapter::session(), ident); _throw_status(status); } @@ -101,7 +104,7 @@ namespace pEp { boost::python::list import_key(string key_data) { ::identity_list *private_keys = NULL; - PEP_STATUS status = ::import_key(adapter.session(), key_data.c_str(), key_data.size(), &private_keys); + PEP_STATUS status = ::import_key(pEp::Adapter::session(), key_data.c_str(), key_data.size(), &private_keys); if (status && status != PEP_KEY_IMPORTED) _throw_status(status); @@ -124,7 +127,7 @@ namespace pEp { PEP_STATUS status = PEP_STATUS_OK; char* key_data = NULL; size_t size; - status = ::export_key(adapter.session(), ident.fpr().c_str(), &key_data, &size); + status = ::export_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); _throw_status(status); return key_data; @@ -135,7 +138,7 @@ namespace pEp { PEP_STATUS status = PEP_STATUS_OK; char* key_data = NULL; size_t size; - status = ::export_secret_key(adapter.session(), ident.fpr().c_str(), &key_data, &size); + status = ::export_secret_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); _throw_status(status); return key_data; @@ -154,7 +157,7 @@ namespace pEp { const char* fpr_c = fpr.c_str(); - PEP_STATUS status = set_own_key(adapter.session(), ident, fpr_c); + PEP_STATUS status = set_own_key(pEp::Adapter::session(), ident, fpr_c); _throw_status(status); } } diff --git a/src/identity.cc b/src/identity.cc index 2f23bcb..f626db5 100644 --- a/src/identity.cc +++ b/src/identity.cc @@ -1,16 +1,17 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#include "identity.hh" -#include "pEpmodule.hh" -#include "basic_api.hh" -#include "message_api.hh" #include #include + #include #include #include -#include + +#include "identity.hh" +#include "pEpmodule.hh" +#include "basic_api.hh" +#include "message_api.hh" namespace pEp { namespace PythonAdapter { @@ -118,7 +119,7 @@ namespace pEp { throw invalid_argument("address must be given"); PEP_rating rating = PEP_rating_undefined; - PEP_STATUS status = ::identity_rating(adapter.session(), _ident.get(), &rating); + PEP_STATUS status = ::identity_rating(pEp::Adapter::session(), _ident.get(), &rating); _throw_status(status); return (int) rating; @@ -150,34 +151,34 @@ namespace pEp { void Identity::key_reset(string fpr) { - PEP_STATUS status = ::key_reset_identity(adapter.session(), *this, + PEP_STATUS status = ::key_reset_identity(pEp::Adapter::session(), *this, fpr != "" ? fpr.c_str() : nullptr); _throw_status(status); } void Identity::key_mistrusted() { - PEP_STATUS status = ::key_mistrusted(adapter.session(), *this); + PEP_STATUS status = ::key_mistrusted(pEp::Adapter::session(), *this); _throw_status(status); } bool Identity::is_pEp_user() { bool result; - PEP_STATUS status = ::is_pEp_user(adapter.session(), *this, &result); + PEP_STATUS status = ::is_pEp_user(pEp::Adapter::session(), *this, &result); _throw_status(status); return result; } void Identity::enable_for_sync() { - PEP_STATUS status = ::enable_identity_for_sync(adapter.session(), *this); + PEP_STATUS status = ::enable_identity_for_sync(pEp::Adapter::session(), *this); _throw_status(status); } void Identity::disable_for_sync() { - PEP_STATUS status = ::disable_identity_for_sync(adapter.session(), *this); + PEP_STATUS status = ::disable_identity_for_sync(pEp::Adapter::session(), *this); _throw_status(status); } @@ -220,7 +221,7 @@ namespace pEp { pEp_identity *_dup = ::identity_dup(_ident); if (!_dup) throw bad_alloc(); - PEP_STATUS status = update_identity(adapter.session(), _dup); + PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); _throw_status(status); free_identity(ident); ident = _dup; @@ -258,7 +259,7 @@ namespace pEp { free_identity_list(_il); throw bad_alloc(); } - PEP_STATUS status = update_identity(adapter.session(), _dup); + PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); if (status != PEP_STATUS_OK) { free_identity_list(_il); _throw_status(status); diff --git a/src/identity.hh b/src/identity.hh index 12daf45..9717624 100644 --- a/src/identity.hh +++ b/src/identity.hh @@ -4,11 +4,14 @@ #pragma once #include -#include -#include #include #include #include + +#include +#include +#include "pEp/Adapter.hh" + #include "str_attr.hh" namespace pEp { diff --git a/src/message.cc b/src/message.cc index 9422876..dee7b15 100644 --- a/src/message.cc +++ b/src/message.cc @@ -2,17 +2,19 @@ // see LICENSE.txt #include -#include "message.hh" -#include "message_api.hh" -#include -#include +#include +#include #include #include #include + #include #include #include +#include "message.hh" +#include "message_api.hh" + namespace pEp { namespace PythonAdapter { using namespace std; @@ -326,11 +328,11 @@ namespace pEp { if (len(to()) + len(cc()) == 0) throw invalid_argument("either to or cc needed"); - PEP_STATUS status = myself(adapter.session(), _msg->from); + PEP_STATUS status = myself(pEp::Adapter::session(), _msg->from); _throw_status(status); PEP_rating rating = PEP_rating_undefined; - status = outgoing_message_rating(adapter.session(), *this, &rating); + status = outgoing_message_rating(pEp::Adapter::session(), *this, &rating); _throw_status(status); return rating; @@ -359,7 +361,7 @@ namespace pEp { if (me.address().empty() || me.user_id().empty()) throw runtime_error("at least address and user_id of own user needed"); - ::myself(adapter.session(), me); + ::myself(pEp::Adapter::session(), me); auto m = Message(PEP_dir_outgoing, &me); return m; } @@ -368,7 +370,7 @@ namespace pEp { { if (ident.address().empty()) throw runtime_error("at least address needed"); - update_identity(adapter.session(), ident); + update_identity(pEp::Adapter::session(), ident); return object(ident); } diff --git a/src/message.hh b/src/message.hh index 85f7242..e366945 100644 --- a/src/message.hh +++ b/src/message.hh @@ -5,9 +5,11 @@ #include #include +#include + #include #include -#include + #include "str_attr.hh" #include "identity.hh" diff --git a/src/message_api.cc b/src/message_api.cc index 09a8aec..7b7d0ad 100644 --- a/src/message_api.cc +++ b/src/message_api.cc @@ -1,14 +1,15 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#include "message_api.hh" -#include "basic_api.hh" #include #include #include #include #include +#include "message_api.hh" +#include "basic_api.hh" + namespace pEp { namespace PythonAdapter { Message encrypt_message(Message src, boost::python::list extra, int enc_format, @@ -29,7 +30,7 @@ namespace pEp { message *_dst = NULL; message *_src = src; - PEP_STATUS status = encrypt_message(adapter.session(), _src, _extra, &_dst, + PEP_STATUS status = encrypt_message(pEp::Adapter::session(), _src, _extra, &_dst, _enc_format, _flags); free_stringlist(_extra); _throw_status(status); @@ -48,7 +49,7 @@ namespace pEp { PEP_decrypt_flags_t _flags = (PEP_decrypt_flags_t) flags; message *_src = src; - PEP_STATUS status = ::decrypt_message(adapter.session(), _src, &_dst, &_keylist, + PEP_STATUS status = ::decrypt_message(pEp::Adapter::session(), _src, &_dst, &_keylist, &_rating, &_flags); _throw_status(status); diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index 63fa846..df58271 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -1,37 +1,39 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#include "pEpmodule.hh" +#include #include #include #include #include -#include "basic_api.hh" -#include "message_api.hh" -#include "user_interface.hh" -#include "adapter.hh" - #include +#include #include #include #include #include +#include "pEpmodule.hh" +#include "basic_api.hh" +#include "message_api.hh" +#include "user_interface.hh" + + namespace pEp { namespace PythonAdapter { using namespace std; - Adapter adapter(true); +// Adapter adapter(true); void config_passive_mode(bool enable) { - ::config_passive_mode(adapter.session(), enable); + ::config_passive_mode(pEp::Adapter::session(), enable); } void config_unencrypted_subject(bool enable) { - ::config_unencrypted_subject(adapter.session(), enable); + ::config_unencrypted_subject(pEp::Adapter::session(), enable); } void key_reset_user(string user_id, string fpr) @@ -39,7 +41,7 @@ namespace pEp { if (user_id == "") throw invalid_argument("user_id required"); - PEP_STATUS status = ::key_reset_user(adapter.session(), + PEP_STATUS status = ::key_reset_user(pEp::Adapter::session(), user_id.c_str(), fpr != "" ? fpr.c_str() : nullptr); _throw_status(status); } @@ -51,7 +53,7 @@ namespace pEp { void key_reset_all_own_keys() { - PEP_STATUS status = ::key_reset_all_own_keys(adapter.session()); + PEP_STATUS status = ::key_reset_all_own_keys(pEp::Adapter::session()); _throw_status(status); } @@ -86,6 +88,11 @@ namespace pEp { } } + PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr) + { + return PEP_STATUS_OK; + } + PEP_STATUS _messageToSend(::message *msg) { if (!_scope) @@ -104,31 +111,31 @@ namespace pEp { throw runtime_error("implement pEp.messageToSend(msg)"); } - void do_sync_protocol() - { - ::do_sync_protocol(adapter.session(), nullptr); - } +// void do_sync_protocol() +// { +// ::do_sync_protocol(pEp::Adapter::session(), nullptr); +// } void shutdown_sync() { - adapter.shutdown_sync(); + pEp::Adapter::shutdown(); } void debug_color(int ansi_color) { - ::set_debug_color(adapter.session(), ansi_color); + ::set_debug_color(pEp::Adapter::session(), ansi_color); } void leave_device_group() { - ::leave_device_group(adapter.session()); + ::leave_device_group(pEp::Adapter::session()); } - void script_is_implementing_sync() { - adapter.script_is_implementing_sync(); - } +// void script_is_implementing_sync() { +// adapter.script_is_implementing_sync(); +// } bool is_sync_active() { - return adapter.is_sync_active(); + return pEp::Adapter::is_sync_running(); } } } @@ -547,38 +554,40 @@ BOOST_PYTHON_MODULE(pEp) .value("SYNC_NOTIFY_SOLE" , SYNC_NOTIFY_SOLE) .value("SYNC_NOTIFY_IN_GROUP" , SYNC_NOTIFY_IN_GROUP); - auto user_interface_class = class_( - "UserInterface", - "class MyUserInterface(UserInterface):\n" - " def notifyHandshake(self, me, partner):\n" - " ...\n" - "\n" - "p≡p User Interface class\n" - "To be used as a mixin\n" - ) - .def("notifyHandshake", &UserInterface::notifyHandshake, - "notifyHandshake(self, me, partner)\n" - "\n" - " me own identity\n" - " partner identity of communication partner\n" - "\n" - "overwrite this method with an implementation of a handshake dialog") - .def("deliverHandshakeResult", &UserInterface::deliverHandshakeResult, - boost::python::arg("identities")=object(), - "deliverHandshakeResult(self, result, identities=None)\n" - "\n" - " result -1: cancel, 0: accepted, 1: rejected\n" - " identities list of identities to share or None for all\n" - "\n" - "call to deliver the handshake result of the handshake dialog") - ; - - def("do_sync_protocol", &pEp::PythonAdapter::do_sync_protocol, - "do_sync_protocol()\n" - "\n" - "in case of an explicit sync thread instead of a single threaded\n" - "implementation call this function in your sync thread\n" - ); +// auto user_interface_class = class_( +// "UserInterface", +// "class MyUserInterface(UserInterface):\n" +// " def notifyHandshake(self, me, partner):\n" +// " ...\n" +// "\n" +// "p≡p User Interface class\n" +// "To be used as a mixin\n" +// ) +// .def("notifyHandshake", &UserInterface::notifyHandshake, +// "notifyHandshake(self, me, partner)\n" +// "\n" +// " me own identity\n" +// " partner identity of communication partner\n" +// "\n" +// "overwrite this method with an implementation of a handshake dialog") +// .def("deliverHandshakeResult", &UserInterface::deliverHandshakeResult, +// boost::python::arg("identities")=object(), +// "deliverHandshakeResult(self, result, identities=None)\n" +// "\n" +// " result -1: cancel, 0: accepted, 1: rejected\n" +// " identities list of identities to share or None for all\n" +// "\n" +// "call to deliver the handshake result of the handshake dialog") +// ; + + +// TODO: Replace with start_sync() +// def("do_sync_protocol", &pEp::PythonAdapter::do_sync_protocol, +// "do_sync_protocol()\n" +// "\n" +// "in case of an explicit sync thread instead of a single threaded\n" +// "implementation call this function in your sync thread\n" +// ); def("shutdown_sync", &pEp::PythonAdapter::shutdown_sync, "shutdown_sync()\n" @@ -595,12 +604,12 @@ BOOST_PYTHON_MODULE(pEp) "call this for a grouped device, which should leave\n" ); - def("script_is_implementing_sync", &pEp::PythonAdapter::script_is_implementing_sync, - "script_is_implementing_sync()\n" - "\n" - "call this in case the Python script is implementing sync to make\n" - "is_sync_active() working\n" - ); +// def("script_is_implementing_sync", &pEp::PythonAdapter::script_is_implementing_sync, +// "script_is_implementing_sync()\n" +// "\n" +// "call this in case the Python script is implementing sync to make\n" +// "is_sync_active() working\n" +// ); def("is_sync_active", &pEp::PythonAdapter::is_sync_active, "is_sync_active()\n" diff --git a/src/pEpmodule.hh b/src/pEpmodule.hh index af575d3..f900c38 100644 --- a/src/pEpmodule.hh +++ b/src/pEpmodule.hh @@ -1,10 +1,7 @@ #pragma once -#include -#include "identity.hh" -#include "message.hh" -#include "adapter.hh" #include +#include "message.hh" namespace pEp { namespace PythonAdapter { @@ -16,8 +13,9 @@ namespace pEp { void _throw_status(PEP_STATUS status); void messageToSend(Message msg); PEP_STATUS _messageToSend(::message *msg); - void do_sync_protocol(); - extern Adapter adapter; + PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr); +// void do_sync_protocol(); +// extern Adapter adapter; } } diff --git a/src/str_attr.cc b/src/str_attr.cc index 3069e37..6240a50 100644 --- a/src/str_attr.cc +++ b/src/str_attr.cc @@ -3,8 +3,9 @@ #include #include +#include + #include "str_attr.hh" -#include namespace pEp { namespace utility { diff --git a/src/str_attr.hh b/src/str_attr.hh index 1292b6c..615312f 100644 --- a/src/str_attr.hh +++ b/src/str_attr.hh @@ -4,6 +4,7 @@ #pragma once #include + #include #include #include diff --git a/src/user_interface.cc b/src/user_interface.cc index 4f678e5..cb8a3e0 100644 --- a/src/user_interface.cc +++ b/src/user_interface.cc @@ -1,9 +1,8 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt +#include #include "user_interface.hh" -#include -#include namespace pEp { namespace PythonAdapter { @@ -24,8 +23,8 @@ namespace pEp { UserInterface_callback::UserInterface_callback(PyObject *self) : UserInterface(), _self(self) { - adapter.ui_object(self); - PEP_STATUS status = ::register_sync_callbacks(adapter.session(), +// adapter.ui_object(self); + PEP_STATUS status = ::register_sync_callbacks(pEp::Adapter::session(), (void *) this, _notifyHandshake, retrieve_next_sync_event); assert(status == PEP_STATUS_OK); if (status) @@ -34,7 +33,7 @@ namespace pEp { UserInterface_callback::~UserInterface_callback() { - ::unregister_sync_callbacks(adapter.session()); + ::unregister_sync_callbacks(pEp::Adapter::session()); } PEP_STATUS UserInterface::_notifyHandshake( @@ -75,7 +74,7 @@ namespace pEp { } } - PEP_STATUS status = ::deliverHandshakeResult(adapter.session(), + PEP_STATUS status = ::deliverHandshakeResult(pEp::Adapter::session(), (sync_handshake_result) result, shared_identities); free_identity_list(shared_identities); _throw_status(status); @@ -85,35 +84,35 @@ namespace pEp { { PEP_rating result; PEP_STATUS status = - ::get_key_rating_for_user(adapter.session(), + ::get_key_rating_for_user(pEp::Adapter::session(), user_id.c_str(), fpr.c_str(), &result); _throw_status(status); return result; } - SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold) - { - time_t started = time(nullptr); - bool timeout = false; - - while (adapter.queue().empty()) { - int i = 0; - ++i; - if (i > 10) { - if (time(nullptr) > started + threshold) { - timeout = true; - break; - } - i = 0; - } - nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); - } - - if (timeout) - return new_sync_timeout_event(); - - return adapter.queue().pop_front(); - } +// SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold) +// { +// time_t started = time(nullptr); +// bool timeout = false; +// +// while (adapter.queue().empty()) { +// int i = 0; +// ++i; +// if (i > 10) { +// if (time(nullptr) > started + threshold) { +// timeout = true; +// break; +// } +// i = 0; +// } +// nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); +// } +// +// if (timeout) +// return new_sync_timeout_event(); +// +// return adapter.queue().pop_front(); +// } void UserInterface_callback::notifyHandshake( Identity me, Identity partner, sync_handshake_signal signal) diff --git a/src/user_interface.hh b/src/user_interface.hh index dfacc6f..f9b5ace 100644 --- a/src/user_interface.hh +++ b/src/user_interface.hh @@ -3,11 +3,14 @@ #pragma once -#include "pEpmodule.hh" -#include +#include + #include #include +#include "pEpmodule.hh" + + namespace pEp { namespace PythonAdapter { class UserInterface { From 07d2130bf0483cacaee5439430ed9cbeaee04aff Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 19 Aug 2020 13:16:42 +0200 Subject: [PATCH 02/13] hybrid C++/Python package / Add Makefile --- Makefile | 19 ++++++++++ setup.py | 40 +++++++++------------- src/pEp/__init__.py | 30 ++++++++++++++++ src/{ => pEp/native_pEp}/adapter.cc | 0 src/{ => pEp/native_pEp}/adapter.hh | 0 src/{ => pEp/native_pEp}/basic_api.cc | 0 src/{ => pEp/native_pEp}/basic_api.hh | 0 src/{ => pEp/native_pEp}/identity.cc | 0 src/{ => pEp/native_pEp}/identity.hh | 0 src/{ => pEp/native_pEp}/locked_queue.hh | 0 src/{ => pEp/native_pEp}/message.cc | 0 src/{ => pEp/native_pEp}/message.hh | 0 src/{ => pEp/native_pEp}/message_api.cc | 0 src/{ => pEp/native_pEp}/message_api.hh | 0 src/{ => pEp/native_pEp}/pEpmodule.cc | 2 +- src/{ => pEp/native_pEp}/pEpmodule.hh | 0 src/{ => pEp/native_pEp}/str_attr.cc | 0 src/{ => pEp/native_pEp}/str_attr.hh | 0 src/{ => pEp/native_pEp}/user_interface.cc | 0 src/{ => pEp/native_pEp}/user_interface.hh | 0 test/pyadpt-81.py | 6 ++++ 21 files changed, 72 insertions(+), 25 deletions(-) create mode 100644 Makefile create mode 100644 src/pEp/__init__.py rename src/{ => pEp/native_pEp}/adapter.cc (100%) rename src/{ => pEp/native_pEp}/adapter.hh (100%) rename src/{ => pEp/native_pEp}/basic_api.cc (100%) rename src/{ => pEp/native_pEp}/basic_api.hh (100%) rename src/{ => pEp/native_pEp}/identity.cc (100%) rename src/{ => pEp/native_pEp}/identity.hh (100%) rename src/{ => pEp/native_pEp}/locked_queue.hh (100%) rename src/{ => pEp/native_pEp}/message.cc (100%) rename src/{ => pEp/native_pEp}/message.hh (100%) rename src/{ => pEp/native_pEp}/message_api.cc (100%) rename src/{ => pEp/native_pEp}/message_api.hh (100%) rename src/{ => pEp/native_pEp}/pEpmodule.cc (99%) rename src/{ => pEp/native_pEp}/pEpmodule.hh (100%) rename src/{ => pEp/native_pEp}/str_attr.cc (100%) rename src/{ => pEp/native_pEp}/str_attr.hh (100%) rename src/{ => pEp/native_pEp}/user_interface.cc (100%) rename src/{ => pEp/native_pEp}/user_interface.hh (100%) create mode 100644 test/pyadpt-81.py diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..24c251d --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +PREFIX = /home/heck/local-default/ +DEBUG = 0 +BUILD_DIR = ./build + +BUILD_EXT_OPTS = --prefix=$(PREFIX) + +ifeq ($(DEBUG),1) + BUILD_EXT_OPTS+=--debug +endif + +.PHONY: all build clean + +all: build_ext + +build_ext: + python3 setup.py build_ext $(BUILD_EXT_OPTS) + +clean: + rm -r $(BUILD_DIR) \ No newline at end of file diff --git a/setup.py b/setup.py index 7f64886..7268c4e 100644 --- a/setup.py +++ b/setup.py @@ -18,18 +18,16 @@ from glob import glob from setuptools.command.build_ext import build_ext -verboseLevel = 0 def pEpLog(*msg): - if verboseLevel > 0: - import inspect - msgstr = '' - separator = ' ' - for m in msg: - msgstr += str(m) - msgstr += separator - func = inspect.currentframe().f_back.f_code - print(func.co_filename + " : " + func.co_name + " : " + msgstr) + import inspect + msgstr = '' + separator = ' ' + for m in msg: + msgstr += str(m) + msgstr += separator + func = inspect.currentframe().f_back.f_code + print(func.co_filename + " : " + func.co_name + " : " + msgstr) class BuildExtCommand(build_ext): @@ -39,13 +37,11 @@ class BuildExtCommand(build_ext): ] def initialize_options(self): - pEpLog("called") build_ext.initialize_options(self) self.local = None != environ.get('PER_USER_DIRECTORY') self.prefix = getattr(self, "prefix=", None) def windowsGetInstallLocation(self): - pEpLog("called") # Note: should be installed to 'C:\Program Files (x86)' while a 32-bit distro # TODO: Try desktop adapter location first, then COM server # FIXME: This is wrong, we should chase the COM server, not the Outlook Plugin (even if they're in the same place) @@ -138,17 +134,12 @@ class BuildExtCommand(build_ext): return (home, sys_includes, sys_libdirs, libs) def finalize_options(self): - pEpLog("called") build_ext.finalize_options(self) - pEpLog("verbose: ", self.verbose) pEpLog("local: ", self.local) pEpLog("prefix: ", self.prefix) pEpLog("sys.platform: ", sys.platform) - global verboseLevel - verboseLevel = self.verbose - # get build information for platform if sys.platform == 'winnt': build_info = self.get_build_info_winnt() @@ -220,14 +211,14 @@ if sys.version_info[0] < 3: module_pEp = Extension( - 'pEp', + 'native_pEp', sources = [ - 'src/pEpmodule.cc', - 'src/basic_api.cc', - 'src/identity.cc', - 'src/message.cc', - 'src/message_api.cc', - 'src/str_attr.cc', + 'src/pEp/native_pEp/pEpmodule.cc', + 'src/pEp/native_pEp/basic_api.cc', + 'src/pEp/native_pEp/identity.cc', + 'src/pEp/native_pEp/message.cc', + 'src/pEp/native_pEp/message_api.cc', + 'src/pEp/native_pEp/str_attr.cc', # 'src/user_interface.cc', # 'src/adapter.cc' ], @@ -242,6 +233,7 @@ setup( author_email="vb@pep-project.org", maintainer="Heck", maintainer_email="heck@pep.foundation", + package_dir='src', ext_modules=[module_pEp], cmdclass={ 'build_ext': BuildExtCommand, diff --git a/src/pEp/__init__.py b/src/pEp/__init__.py new file mode 100644 index 0000000..1990cc0 --- /dev/null +++ b/src/pEp/__init__.py @@ -0,0 +1,30 @@ +# pEp package +# This file is being exectued upon 'import pEp' +# +# __all__ could be used to limit the symbols exported when using from import * + + +# Import all symbols EXCEPT the ones beginning with underscore into the current namespace +from native_pEp import * + +# import the module +import native_pEp + + + + + +# Executed on module import +def init(): + print("init() called") + # native_pEp._init() + +# Executed when run as script +def main(): + print("I am being run as a script") + +# MAIN +if __name__ == "__main__": + main() +else: + init() \ No newline at end of file diff --git a/src/adapter.cc b/src/pEp/native_pEp/adapter.cc similarity index 100% rename from src/adapter.cc rename to src/pEp/native_pEp/adapter.cc diff --git a/src/adapter.hh b/src/pEp/native_pEp/adapter.hh similarity index 100% rename from src/adapter.hh rename to src/pEp/native_pEp/adapter.hh diff --git a/src/basic_api.cc b/src/pEp/native_pEp/basic_api.cc similarity index 100% rename from src/basic_api.cc rename to src/pEp/native_pEp/basic_api.cc diff --git a/src/basic_api.hh b/src/pEp/native_pEp/basic_api.hh similarity index 100% rename from src/basic_api.hh rename to src/pEp/native_pEp/basic_api.hh diff --git a/src/identity.cc b/src/pEp/native_pEp/identity.cc similarity index 100% rename from src/identity.cc rename to src/pEp/native_pEp/identity.cc diff --git a/src/identity.hh b/src/pEp/native_pEp/identity.hh similarity index 100% rename from src/identity.hh rename to src/pEp/native_pEp/identity.hh diff --git a/src/locked_queue.hh b/src/pEp/native_pEp/locked_queue.hh similarity index 100% rename from src/locked_queue.hh rename to src/pEp/native_pEp/locked_queue.hh diff --git a/src/message.cc b/src/pEp/native_pEp/message.cc similarity index 100% rename from src/message.cc rename to src/pEp/native_pEp/message.cc diff --git a/src/message.hh b/src/pEp/native_pEp/message.hh similarity index 100% rename from src/message.hh rename to src/pEp/native_pEp/message.hh diff --git a/src/message_api.cc b/src/pEp/native_pEp/message_api.cc similarity index 100% rename from src/message_api.cc rename to src/pEp/native_pEp/message_api.cc diff --git a/src/message_api.hh b/src/pEp/native_pEp/message_api.hh similarity index 100% rename from src/message_api.hh rename to src/pEp/native_pEp/message_api.hh diff --git a/src/pEpmodule.cc b/src/pEp/native_pEp/pEpmodule.cc similarity index 99% rename from src/pEpmodule.cc rename to src/pEp/native_pEp/pEpmodule.cc index df58271..e15d520 100644 --- a/src/pEpmodule.cc +++ b/src/pEp/native_pEp/pEpmodule.cc @@ -140,7 +140,7 @@ namespace pEp { } } -BOOST_PYTHON_MODULE(pEp) +BOOST_PYTHON_MODULE(native_pEp) { using namespace boost::python; using namespace boost::locale; diff --git a/src/pEpmodule.hh b/src/pEp/native_pEp/pEpmodule.hh similarity index 100% rename from src/pEpmodule.hh rename to src/pEp/native_pEp/pEpmodule.hh diff --git a/src/str_attr.cc b/src/pEp/native_pEp/str_attr.cc similarity index 100% rename from src/str_attr.cc rename to src/pEp/native_pEp/str_attr.cc diff --git a/src/str_attr.hh b/src/pEp/native_pEp/str_attr.hh similarity index 100% rename from src/str_attr.hh rename to src/pEp/native_pEp/str_attr.hh diff --git a/src/user_interface.cc b/src/pEp/native_pEp/user_interface.cc similarity index 100% rename from src/user_interface.cc rename to src/pEp/native_pEp/user_interface.cc diff --git a/src/user_interface.hh b/src/pEp/native_pEp/user_interface.hh similarity index 100% rename from src/user_interface.hh rename to src/pEp/native_pEp/user_interface.hh diff --git a/test/pyadpt-81.py b/test/pyadpt-81.py new file mode 100644 index 0000000..c78cc10 --- /dev/null +++ b/test/pyadpt-81.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import pEp + +pEp.is_sync_active() \ No newline at end of file From 50ebe60221ecca39d918c41266cd71e1369cf776 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 19 Aug 2020 15:25:09 +0200 Subject: [PATCH 03/13] Includes/namespace/using cleanup --- src/pEp/__init__.py | 2 +- src/pEp/native_pEp/basic_api.cc | 262 +++++------ src/pEp/native_pEp/basic_api.hh | 36 +- src/pEp/native_pEp/identity.cc | 446 ++++++++++--------- src/pEp/native_pEp/identity.hh | 115 ++--- src/pEp/native_pEp/message.cc | 643 ++++++++++++++------------- src/pEp/native_pEp/message.hh | 193 ++++---- src/pEp/native_pEp/message_api.cc | 309 ++++++------- src/pEp/native_pEp/message_api.hh | 25 +- src/pEp/native_pEp/pEpmodule.cc | 349 ++++++++------- src/pEp/native_pEp/pEpmodule.hh | 33 +- src/pEp/native_pEp/str_attr.cc | 296 ++++++------ src/pEp/native_pEp/str_attr.hh | 38 +- src/pEp/native_pEp/user_interface.cc | 199 +++++---- src/pEp/native_pEp/user_interface.hh | 81 ++-- test/pyadpt-81.py | 1 + 16 files changed, 1556 insertions(+), 1472 deletions(-) diff --git a/src/pEp/__init__.py b/src/pEp/__init__.py index 1990cc0..aa7d9ef 100644 --- a/src/pEp/__init__.py +++ b/src/pEp/__init__.py @@ -17,7 +17,7 @@ import native_pEp # Executed on module import def init(): print("init() called") - # native_pEp._init() + native_pEp._init() # Executed when run as script def main(): diff --git a/src/pEp/native_pEp/basic_api.cc b/src/pEp/native_pEp/basic_api.cc index 3070446..c389905 100644 --- a/src/pEp/native_pEp/basic_api.cc +++ b/src/pEp/native_pEp/basic_api.cc @@ -1,164 +1,172 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt +// System #include +// Engine #include #include #include +// local #include "basic_api.hh" namespace pEp { - namespace PythonAdapter { - void update_identity(Identity& ident) - { - if (ident.address() == "") - throw invalid_argument("address needed"); - if (ident.user_id() == PEP_OWN_USERID) - throw runtime_error("update_identity: '" PEP_OWN_USERID - "' may only be used for own identities"); - - PEP_STATUS status = update_identity(pEp::Adapter::session(), ident); - _throw_status(status); - } - - void myself(Identity& ident) - { - if (ident.address() == "") - throw invalid_argument("address needed"); - if (ident.username() == "") - throw invalid_argument("username needed"); +namespace PythonAdapter { +using namespace std; + +void update_identity(Identity& ident) +{ + if (ident.address() == "") + throw invalid_argument("address needed"); + if (ident.user_id() == PEP_OWN_USERID) + throw runtime_error("update_identity: '" PEP_OWN_USERID + "' may only be used for own identities"); + + PEP_STATUS status = update_identity(pEp::Adapter::session(), ident); + _throw_status(status); +} - if (ident.user_id() == "") - ident.user_id(ident.address()); +void myself(Identity& ident) +{ + if (ident.address() == "") + throw invalid_argument("address needed"); + if (ident.username() == "") + throw invalid_argument("username needed"); - PEP_STATUS status = myself(pEp::Adapter::session(), ident); - _throw_status(status); - } + if (ident.user_id() == "") + ident.user_id(ident.address()); - string _trustwords(Identity me, Identity partner, string lang, bool full) - { - if (me.fpr() == "" || partner.fpr() == "") - throw invalid_argument("fingerprint needed in Identities"); + PEP_STATUS status = myself(pEp::Adapter::session(), ident); + _throw_status(status); +} - if (lang == "" && me.lang() == partner.lang()) - lang = me.lang(); +string _trustwords(Identity me, Identity partner, string lang, bool full) +{ + if (me.fpr() == "" || partner.fpr() == "") + throw invalid_argument("fingerprint needed in Identities"); - char *words = NULL; - size_t size = 0; - PEP_STATUS status = get_trustwords(pEp::Adapter::session(), me, partner, - lang.c_str(),&words, &size, full); - _throw_status(status); - return words; - } + if (lang == "" && me.lang() == partner.lang()) + lang = me.lang(); - void trust_personal_key(Identity ident) - { - if (ident.fpr() == "") - throw invalid_argument("fingerprint needed in Identities"); - if (ident.user_id() == "") - throw invalid_argument("user_id must be provided"); + char *words = NULL; + size_t size = 0; + PEP_STATUS status = get_trustwords(pEp::Adapter::session(), me, partner, + lang.c_str(),&words, &size, full); + _throw_status(status); + return words; +} - PEP_STATUS status = trust_personal_key(pEp::Adapter::session(), ident); - _throw_status(status); - } +void trust_personal_key(Identity ident) +{ + if (ident.fpr() == "") + throw invalid_argument("fingerprint needed in Identities"); + if (ident.user_id() == "") + throw invalid_argument("user_id must be provided"); - void set_identity_flags(Identity ident, identity_flags_t flags) - { - if (ident.address() == "") - throw invalid_argument("address needed"); - if (ident.user_id() == "") - throw invalid_argument("user_id needed"); + PEP_STATUS status = trust_personal_key(pEp::Adapter::session(), ident); + _throw_status(status); +} - PEP_STATUS status = set_identity_flags(pEp::Adapter::session(), ident, flags); - _throw_status(status); - } +void set_identity_flags(Identity ident, identity_flags_t flags) +{ + if (ident.address() == "") + throw invalid_argument("address needed"); + if (ident.user_id() == "") + throw invalid_argument("user_id needed"); - void unset_identity_flags(Identity ident, identity_flags_t flags) - { - if (ident.address() == "") - throw invalid_argument("address needed"); - if (ident.user_id() == "") - throw invalid_argument("user_id needed"); + PEP_STATUS status = set_identity_flags(pEp::Adapter::session(), ident, flags); + _throw_status(status); +} - PEP_STATUS status = unset_identity_flags(pEp::Adapter::session(), ident, flags); - _throw_status(status); - } +void unset_identity_flags(Identity ident, identity_flags_t flags) +{ + if (ident.address() == "") + throw invalid_argument("address needed"); + if (ident.user_id() == "") + throw invalid_argument("user_id needed"); - void key_reset_trust(Identity ident) - { - if (ident.fpr() == "") - throw invalid_argument("fpr needed"); - if (ident.address() == "") - throw invalid_argument("address needed"); - if (ident.user_id() == "") - throw invalid_argument("user_id needed"); - - PEP_STATUS status = key_reset_trust(pEp::Adapter::session(), ident); - _throw_status(status); - } + PEP_STATUS status = unset_identity_flags(pEp::Adapter::session(), ident, flags); + _throw_status(status); +} +void key_reset_trust(Identity ident) +{ + if (ident.fpr() == "") + throw invalid_argument("fpr needed"); + if (ident.address() == "") + throw invalid_argument("address needed"); + if (ident.user_id() == "") + throw invalid_argument("user_id needed"); + + PEP_STATUS status = key_reset_trust(pEp::Adapter::session(), ident); + _throw_status(status); +} - boost::python::list import_key(string key_data) - { - ::identity_list *private_keys = NULL; - PEP_STATUS status = ::import_key(pEp::Adapter::session(), key_data.c_str(), key_data.size(), &private_keys); - if (status && status != PEP_KEY_IMPORTED) - _throw_status(status); - auto result = boost::python::list(); - for (::identity_list *il = private_keys; il && il->ident; il=il->next) { - ::pEp_identity *ident = ::identity_dup(il->ident); - if (!ident) { - free_identity_list(private_keys); - throw bad_alloc(); - } - result.append(Identity(ident)); - } +boost::python::list import_key(string key_data) +{ + ::identity_list *private_keys = NULL; + PEP_STATUS status = ::import_key(pEp::Adapter::session(), key_data.c_str(), key_data.size(), &private_keys); + if (status && status != PEP_KEY_IMPORTED) + _throw_status(status); + auto result = boost::python::list(); + for (::identity_list *il = private_keys; il && il->ident; il=il->next) { + ::pEp_identity *ident = ::identity_dup(il->ident); + if (!ident) { free_identity_list(private_keys); - return result; + throw bad_alloc(); } + result.append(Identity(ident)); + } - string export_key(Identity ident) - { - PEP_STATUS status = PEP_STATUS_OK; - char* key_data = NULL; - size_t size; - status = ::export_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); + free_identity_list(private_keys); + return result; +} - _throw_status(status); - return key_data; - } +string export_key(Identity ident) +{ + PEP_STATUS status = PEP_STATUS_OK; + char* key_data = NULL; + size_t size; + status = ::export_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); - string export_secret_key(Identity ident) - { - PEP_STATUS status = PEP_STATUS_OK; - char* key_data = NULL; - size_t size; - status = ::export_secret_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); + _throw_status(status); + return key_data; +} - _throw_status(status); - return key_data; - } +string export_secret_key(Identity ident) +{ + PEP_STATUS status = PEP_STATUS_OK; + char* key_data = NULL; + size_t size; + status = ::export_secret_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); - void set_own_key(Identity& ident, string fpr) - { - if (ident.address() == "") - throw invalid_argument("address needed"); - if (ident.username() == "") - throw invalid_argument("username needed"); - if (ident.user_id() == "") - throw invalid_argument("user_id needed"); - if (fpr == "") - throw invalid_argument("fpr needed"); - - - const char* fpr_c = fpr.c_str(); - PEP_STATUS status = set_own_key(pEp::Adapter::session(), ident, fpr_c); - _throw_status(status); - } - } + _throw_status(status); + return key_data; +} + +void set_own_key(Identity& ident, string fpr) +{ + if (ident.address() == "") + throw invalid_argument("address needed"); + if (ident.username() == "") + throw invalid_argument("username needed"); + if (ident.user_id() == "") + throw invalid_argument("user_id needed"); + if (fpr == "") + throw invalid_argument("fpr needed"); + + + const char* fpr_c = fpr.c_str(); + PEP_STATUS status = set_own_key(pEp::Adapter::session(), ident, fpr_c); + _throw_status(status); } + +} // namespace PythonAdapter +} // namespace pEp { + + diff --git a/src/pEp/native_pEp/basic_api.hh b/src/pEp/native_pEp/basic_api.hh index e1ace2d..a9c951a 100644 --- a/src/pEp/native_pEp/basic_api.hh +++ b/src/pEp/native_pEp/basic_api.hh @@ -6,20 +6,22 @@ #include "pEpmodule.hh" namespace pEp { - namespace PythonAdapter { - void update_identity(Identity& ident); - void myself(Identity& ident); - string _trustwords(Identity me, Identity partner, string lang, bool full); - void trust_personal_key(Identity ident); - - void set_identity_flags(Identity ident, identity_flags_t flags); - void unset_identity_flags(Identity ident, identity_flags_t flags); - - void key_reset_trust(Identity ident); - - boost::python::list import_key(string key_data); - string export_key(Identity ident); - string export_secret_key(Identity ident); - void set_own_key(Identity& ident, string fpr); - } -} +namespace PythonAdapter { + +void update_identity(Identity& ident); +void myself(Identity& ident); +string _trustwords(Identity me, Identity partner, string lang, bool full); +void trust_personal_key(Identity ident); + +void set_identity_flags(Identity ident, identity_flags_t flags); +void unset_identity_flags(Identity ident, identity_flags_t flags); + +void key_reset_trust(Identity ident); + +boost::python::list import_key(string key_data); +string export_key(Identity ident); +string export_secret_key(Identity ident); +void set_own_key(Identity& ident, string fpr); + +} // namespace PythonAdapter +} // namespace pEp { diff --git a/src/pEp/native_pEp/identity.cc b/src/pEp/native_pEp/identity.cc index f626db5..9b414f8 100644 --- a/src/pEp/native_pEp/identity.cc +++ b/src/pEp/native_pEp/identity.cc @@ -1,279 +1,285 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt +// System #include #include +// Engine #include #include #include +// local #include "identity.hh" #include "pEpmodule.hh" #include "basic_api.hh" #include "message_api.hh" namespace pEp { - namespace PythonAdapter { - - Identity::Identity(string address, string username, string user_id, - string fpr, int comm_type, string lang, identity_flags_t flags) - : _ident(new_identity(address.c_str(), fpr.c_str(), user_id.c_str(), - username.c_str()), &::free_identity) - { - if (!_ident) - throw bad_alloc(); - _ident->comm_type = (PEP_comm_type) comm_type; - _ident->flags = (identity_flags_t) flags; - this->lang(lang); - } +namespace PythonAdapter { +using namespace std; +using namespace boost::python; + +Identity::Identity(string address, string username, string user_id, + string fpr, int comm_type, string lang, identity_flags_t flags) + : _ident(new_identity(address.c_str(), fpr.c_str(), user_id.c_str(), + username.c_str()), &::free_identity) +{ + if (!_ident) + throw bad_alloc(); + _ident->comm_type = (PEP_comm_type) comm_type; + _ident->flags = (identity_flags_t) flags; + this->lang(lang); +} - Identity::Identity(const Identity& second) - : _ident(second._ident) - { +Identity::Identity(const Identity& second) + : _ident(second._ident) +{ - } +} - Identity::Identity(pEp_identity *ident) - : _ident(ident, &::free_identity) - { +Identity::Identity(pEp_identity *ident) + : _ident(ident, &::free_identity) +{ - } +} - Identity::~Identity() - { +Identity::~Identity() +{ - } +} - Identity::operator pEp_identity *() - { - return _ident.get(); - } +Identity::operator pEp_identity *() +{ + return _ident.get(); +} - Identity::operator const pEp_identity *() const - { - return _ident.get(); - } +Identity::operator const pEp_identity *() const +{ + return _ident.get(); +} - string Identity::_repr() - { - stringstream build; - build << "Identity("; - string address; - if (_ident->address) - address = string(_ident->address); - build << repr(address) << ", "; - string username; - if (_ident->username) - username = string(_ident->username); - build << repr(username) << ", "; - string user_id; - if (_ident->user_id) - user_id = string(_ident->user_id); - build << repr(user_id) << ", "; - string fpr; - if (_ident->fpr) - fpr = string(_ident->fpr); - build << repr(fpr) << ", "; - build << (int) _ident->comm_type << ", "; - string lang = _ident->lang; - build << repr(lang) << ")"; - return build.str(); - } +string Identity::_repr() +{ + stringstream build; + build << "Identity("; + string address; + if (_ident->address) + address = string(_ident->address); + build << repr(address) << ", "; + string username; + if (_ident->username) + username = string(_ident->username); + build << repr(username) << ", "; + string user_id; + if (_ident->user_id) + user_id = string(_ident->user_id); + build << repr(user_id) << ", "; + string fpr; + if (_ident->fpr) + fpr = string(_ident->fpr); + build << repr(fpr) << ", "; + build << (int) _ident->comm_type << ", "; + string lang = _ident->lang; + build << repr(lang) << ")"; + return build.str(); +} - string Identity::_str() - { - if (!(_ident->address && _ident->address[0])) - return ""; - if (!(_ident->username && _ident->username[0])) - return _ident->address; - return string(_ident->username) + " <" + _ident->address + ">"; - } +string Identity::_str() +{ + if (!(_ident->address && _ident->address[0])) + return ""; + if (!(_ident->username && _ident->username[0])) + return _ident->address; + return string(_ident->username) + " <" + _ident->address + ">"; +} - void Identity::username(string value) - { - if (value.length() && value.length() < 5) - throw length_error("username must be at least 5 characters"); +void Identity::username(string value) +{ + if (value.length() && value.length() < 5) + throw length_error("username must be at least 5 characters"); - str_attr(_ident->username, value); - } + str_attr(_ident->username, value); +} - void Identity::lang(string value) - { - if (value == "") - memset(_ident->lang, 0, 3); - else if (value.length() != 2) - throw length_error("length of lang must be 2"); - else - memcpy(_ident->lang, value.c_str(), 3); - } +void Identity::lang(string value) +{ + if (value == "") + memset(_ident->lang, 0, 3); + else if (value.length() != 2) + throw length_error("length of lang must be 2"); + else + memcpy(_ident->lang, value.c_str(), 3); +} - string Identity::lang() - { - return _ident->lang; - } +string Identity::lang() +{ + return _ident->lang; +} - int Identity::rating() - { - if (!(_ident->address)) - throw invalid_argument("address must be given"); +int Identity::rating() +{ + if (!(_ident->address)) + throw invalid_argument("address must be given"); - PEP_rating rating = PEP_rating_undefined; - PEP_STATUS status = ::identity_rating(pEp::Adapter::session(), _ident.get(), &rating); - _throw_status(status); + PEP_rating rating = PEP_rating_undefined; + PEP_STATUS status = ::identity_rating(pEp::Adapter::session(), _ident.get(), &rating); + _throw_status(status); - return (int) rating; - } + return (int) rating; +} - PEP_color Identity::color() - { - return _color(rating()); - } +PEP_color Identity::color() +{ + return _color(rating()); +} - Identity Identity::copy() - { - pEp_identity *dup = ::identity_dup(*this); - if (!dup) - throw bad_alloc(); +Identity Identity::copy() +{ + pEp_identity *dup = ::identity_dup(*this); + if (!dup) + throw bad_alloc(); - return Identity(dup); - } + return Identity(dup); +} - Identity Identity::deepcopy(dict&) - { - return copy(); - } +Identity Identity::deepcopy(dict&) +{ + return copy(); +} - void Identity::update() - { - update_identity(*this); - } +void Identity::update() +{ + update_identity(*this); +} - void Identity::key_reset(string fpr) - { - PEP_STATUS status = ::key_reset_identity(pEp::Adapter::session(), *this, - fpr != "" ? fpr.c_str() : nullptr); - _throw_status(status); - } +void Identity::key_reset(string fpr) +{ + PEP_STATUS status = ::key_reset_identity(pEp::Adapter::session(), *this, + fpr != "" ? fpr.c_str() : nullptr); + _throw_status(status); +} - void Identity::key_mistrusted() - { - PEP_STATUS status = ::key_mistrusted(pEp::Adapter::session(), *this); - _throw_status(status); - } +void Identity::key_mistrusted() +{ + PEP_STATUS status = ::key_mistrusted(pEp::Adapter::session(), *this); + _throw_status(status); +} - bool Identity::is_pEp_user() - { - bool result; - PEP_STATUS status = ::is_pEp_user(pEp::Adapter::session(), *this, &result); - _throw_status(status); - return result; - } +bool Identity::is_pEp_user() +{ + bool result; + PEP_STATUS status = ::is_pEp_user(pEp::Adapter::session(), *this, &result); + _throw_status(status); + return result; +} - void Identity::enable_for_sync() - { - PEP_STATUS status = ::enable_identity_for_sync(pEp::Adapter::session(), *this); - _throw_status(status); - } +void Identity::enable_for_sync() +{ + PEP_STATUS status = ::enable_identity_for_sync(pEp::Adapter::session(), *this); + _throw_status(status); +} - void Identity::disable_for_sync() - { - PEP_STATUS status = ::disable_identity_for_sync(pEp::Adapter::session(), *this); - _throw_status(status); - } +void Identity::disable_for_sync() +{ + PEP_STATUS status = ::disable_identity_for_sync(pEp::Adapter::session(), *this); + _throw_status(status); +} - Myself::Myself(string address, string username, string user_id, string lang) - : Identity(address, username, user_id, "", 0, lang) +Myself::Myself(string address, string username, string user_id, string lang) + : Identity(address, username, user_id, "", 0, lang) - { - if (!(address.length() && username.length())) - throw invalid_argument("address and username must be set"); - if (lang.length() && lang.length() != 2) - throw length_error("lang must be an ISO 639-1 language code or empty"); +{ + if (!(address.length() && username.length())) + throw invalid_argument("address and username must be set"); + if (lang.length() && lang.length() != 2) + throw length_error("lang must be an ISO 639-1 language code or empty"); - // FIXME: should set .me - // _ident->me = true; - if (user_id.length()) - throw runtime_error("user_id feature not yet implemented for Myself"); - } + // FIXME: should set .me + // _ident->me = true; + if (user_id.length()) + throw runtime_error("user_id feature not yet implemented for Myself"); +} - void Myself::update() - { - pEp::PythonAdapter::myself(*this); - } +void Myself::update() +{ + pEp::PythonAdapter::myself(*this); +} - Identity identity_attr(pEp_identity *&ident) - { - if (!ident) - throw out_of_range("no identity assigned"); +Identity identity_attr(pEp_identity *&ident) +{ + if (!ident) + throw out_of_range("no identity assigned"); - pEp_identity *_dup = identity_dup(ident); - if (!_dup) - throw bad_alloc(); + pEp_identity *_dup = identity_dup(ident); + if (!_dup) + throw bad_alloc(); - Identity _ident(_dup); - return _ident; - } + Identity _ident(_dup); + return _ident; +} - void identity_attr(pEp_identity *&ident, object value) - { - Identity& _ident = extract< Identity& >(value); - pEp_identity *_dup = ::identity_dup(_ident); - if (!_dup) - throw bad_alloc(); - PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); - _throw_status(status); - free_identity(ident); - ident = _dup; - } +void identity_attr(pEp_identity *&ident, object value) +{ + Identity& _ident = extract< Identity& >(value); + pEp_identity *_dup = ::identity_dup(_ident); + if (!_dup) + throw bad_alloc(); + PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); + _throw_status(status); + free_identity(ident); + ident = _dup; +} + +boost::python::list identitylist_attr(identity_list *&il) +{ + boost::python::list result; - boost::python::list identitylist_attr(identity_list *&il) - { - boost::python::list result; + for (identity_list *_il = il; _il && _il->ident; _il = _il->next) { + pEp_identity *ident = ::identity_dup(_il->ident); + if (!ident) + throw bad_alloc(); + result.append(object(Identity(ident))); + } - for (identity_list *_il = il; _il && _il->ident; _il = _il->next) { - pEp_identity *ident = ::identity_dup(_il->ident); - if (!ident) - throw bad_alloc(); - result.append(object(Identity(ident))); - } + return result; +} - return result; +void identitylist_attr(identity_list *&il, boost::python::list value) +{ + identity_list *_il = new_identity_list(NULL); + if (!_il) + throw bad_alloc(); + + identity_list *_i = _il; + for (int i=0; i extract_identity(value[i]); + if (!extract_identity.check()) { + free_identity_list(_il); } - - void identitylist_attr(identity_list *&il, boost::python::list value) - { - identity_list *_il = new_identity_list(NULL); - if (!_il) - throw bad_alloc(); - - identity_list *_i = _il; - for (int i=0; i extract_identity(value[i]); - if (!extract_identity.check()) { - free_identity_list(_il); - } - pEp_identity *_ident = extract_identity(); - pEp_identity *_dup = ::identity_dup(_ident); - if (!_dup) { - free_identity_list(_il); - throw bad_alloc(); - } - PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); - if (status != PEP_STATUS_OK) { - free_identity_list(_il); - _throw_status(status); - } - _i = identity_list_add(_i, _dup); - if (!_i) { - free_identity_list(_il); - throw bad_alloc(); - } - } - - free_identity_list(il); - il = _il; + pEp_identity *_ident = extract_identity(); + pEp_identity *_dup = ::identity_dup(_ident); + if (!_dup) { + free_identity_list(_il); + throw bad_alloc(); + } + PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); + if (status != PEP_STATUS_OK) { + free_identity_list(_il); + _throw_status(status); + } + _i = identity_list_add(_i, _dup); + if (!_i) { + free_identity_list(_il); + throw bad_alloc(); } } + + free_identity_list(il); + il = _il; } +} // namespace PythonAdapter +} // namespace pEp { + diff --git a/src/pEp/native_pEp/identity.hh b/src/pEp/native_pEp/identity.hh index 9717624..d01a00c 100644 --- a/src/pEp/native_pEp/identity.hh +++ b/src/pEp/native_pEp/identity.hh @@ -3,91 +3,98 @@ #pragma once +// System #include #include #include #include +// Engine #include #include + +//libpEpAdapter #include "pEp/Adapter.hh" +// local #include "str_attr.hh" namespace pEp { - namespace PythonAdapter { - using namespace utility; - using namespace std; +namespace PythonAdapter { + +using std::string; +using std::shared_ptr; + +// Identity is owning a pEp_identity - // Identity is owning a pEp_identity +class Identity { +protected: + shared_ptr< pEp_identity > _ident; - class Identity { - protected: - shared_ptr< pEp_identity > _ident; +public: + Identity(string address = "", string username = "", + string user_id = "", string fpr = "", int comm_type = 0, + string lang = "", identity_flags_t flags = 0); - public: - Identity(string address = "", string username = "", - string user_id = "", string fpr = "", int comm_type = 0, - string lang = "", identity_flags_t flags = 0); + Identity(const Identity& second); + Identity(pEp_identity *ident); + virtual ~Identity(); + operator pEp_identity *(); + operator const pEp_identity *() const; - Identity(const Identity& second); - Identity(pEp_identity *ident); - virtual ~Identity(); - operator pEp_identity *(); - operator const pEp_identity *() const; + string _repr(); + string _str(); - string _repr(); - string _str(); + string address() { return str_attr(_ident->address); } + void address(string value) { str_attr(_ident->address, value); } - string address() { return str_attr(_ident->address); } - void address(string value) { str_attr(_ident->address, value); } + string fpr() { return str_attr(_ident->fpr); } + void fpr(string value) { str_attr(_ident->fpr, value); } - string fpr() { return str_attr(_ident->fpr); } - void fpr(string value) { str_attr(_ident->fpr, value); } + string user_id() { return str_attr(_ident->user_id); } + void user_id(string value) { str_attr(_ident->user_id, value); } - string user_id() { return str_attr(_ident->user_id); } - void user_id(string value) { str_attr(_ident->user_id, value); } + string username() { return str_attr(_ident->username); } + void username(string value); - string username() { return str_attr(_ident->username); } - void username(string value); + PEP_comm_type comm_type() { return _ident->comm_type; } + void comm_type(PEP_comm_type value) { _ident->comm_type = value; }; - PEP_comm_type comm_type() { return _ident->comm_type; } - void comm_type(PEP_comm_type value) { _ident->comm_type = value; }; + std::string lang(); + void lang(std::string value); - std::string lang(); - void lang(std::string value); + identity_flags_t flags() { return _ident->flags; } + void flags(identity_flags_t flags) { _ident->flags = flags; } - identity_flags_t flags() { return _ident->flags; } - void flags(identity_flags_t flags) { _ident->flags = flags; } + int rating(); + PEP_color color(); - int rating(); - PEP_color color(); + Identity copy(); + Identity deepcopy(dict& memo); - Identity copy(); - Identity deepcopy(dict& memo); + virtual void update(); - virtual void update(); + void key_reset(string fpr=""); + void key_mistrusted(); - void key_reset(string fpr=""); - void key_mistrusted(); + bool is_pEp_user(); - bool is_pEp_user(); + void enable_for_sync(); + void disable_for_sync(); +}; - void enable_for_sync(); - void disable_for_sync(); - }; +class Myself : public Identity { +public: + Myself(string address, string username, string user_id="", string lang=""); + virtual void update(); +}; - class Myself : public Identity { - public: - Myself(string address, string username, string user_id="", string lang=""); - virtual void update(); - }; +Identity identity_attr(pEp_identity *&ident); +void identity_attr(pEp_identity *&ident, object value); - Identity identity_attr(pEp_identity *&ident); - void identity_attr(pEp_identity *&ident, object value); +boost::python::list identitylist_attr(identity_list *&il); +void identitylist_attr(identity_list *&il, boost::python::list value); - boost::python::list identitylist_attr(identity_list *&il); - void identitylist_attr(identity_list *&il, boost::python::list value); - } -} +} // namespace PythonAdapter +} // namespace pEp { diff --git a/src/pEp/native_pEp/message.cc b/src/pEp/native_pEp/message.cc index dee7b15..08ba766 100644 --- a/src/pEp/native_pEp/message.cc +++ b/src/pEp/native_pEp/message.cc @@ -1,408 +1,413 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#include +// System #include #include #include #include #include +#include +// Engine #include #include #include +// local #include "message.hh" #include "message_api.hh" namespace pEp { - namespace PythonAdapter { - using namespace std; - - Message::Blob::Blob(bloblist_t *bl, bool chained) : - _bl(bl), part_of_chain(chained) - { - if (!_bl) - throw bad_alloc(); - } +namespace PythonAdapter { +using namespace std; +using namespace boost::python; + +Message::Blob::Blob(bloblist_t *bl, bool chained) : + _bl(bl), part_of_chain(chained) +{ + if (!_bl) + throw bad_alloc(); +} - Message::Blob::Blob(object data, string mime_type, string filename) : - _bl(new_bloblist(NULL, 0, NULL, NULL)), part_of_chain(false) - { - if (!_bl) - throw bad_alloc(); +Message::Blob::Blob(object data, string mime_type, string filename) : + _bl(new_bloblist(NULL, 0, NULL, NULL)), part_of_chain(false) +{ + if (!_bl) + throw bad_alloc(); + + Py_buffer src; + int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO); + if (result) + throw invalid_argument("need a contiguous buffer to read"); + + char *mem = (char *)malloc(src.len); + if (!mem) { + PyBuffer_Release(&src); + throw bad_alloc(); + } - Py_buffer src; - int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO); - if (result) - throw invalid_argument("need a contiguous buffer to read"); + memcpy(mem, src.buf, src.len); + free(_bl->value); + _bl->size = src.len; + _bl->value = mem; - char *mem = (char *)malloc(src.len); - if (!mem) { - PyBuffer_Release(&src); - throw bad_alloc(); - } + PyBuffer_Release(&src); - memcpy(mem, src.buf, src.len); - free(_bl->value); - _bl->size = src.len; - _bl->value = mem; + this->mime_type(mime_type); + this->filename(filename); +} - PyBuffer_Release(&src); +Message::Blob::Blob(const Message::Blob& second) : + _bl(second._bl), part_of_chain(true) +{ - this->mime_type(mime_type); - this->filename(filename); - } +} - Message::Blob::Blob(const Message::Blob& second) : - _bl(second._bl), part_of_chain(true) - { +Message::Blob::~Blob() +{ + if (!part_of_chain) { + free(_bl->value); + free(_bl); + } +} - } +string Message::Blob::_repr() +{ + stringstream build; + build << "Blob("; + if (!_bl) { + build << "b'', '', ''"; + } + else { + build << "bytes(" << _bl->size << "), "; + string mime_type; + if (_bl->mime_type) + mime_type = string(_bl->mime_type); + string filename; + if (_bl->filename) + filename = string(_bl->filename); + build << repr(mime_type) << ", "; + build << repr(filename); + } + build << ")"; + return build.str(); +} - Message::Blob::~Blob() - { - if (!part_of_chain) { - free(_bl->value); - free(_bl); - } - } +int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { + bloblist_t *bl = NULL; - string Message::Blob::_repr() - { - stringstream build; - build << "Blob("; - if (!_bl) { - build << "b'', '', ''"; - } - else { - build << "bytes(" << _bl->size << "), "; - string mime_type; - if (_bl->mime_type) - mime_type = string(_bl->mime_type); - string filename; - if (_bl->filename) - filename = string(_bl->filename); - build << repr(mime_type) << ", "; - build << repr(filename); - } - build << ")"; - return build.str(); - } + try { + Message::Blob& blob = extract< Message::Blob& >(self); + bl = blob._bl; + } + catch (exception& e) { + PyErr_SetString(PyExc_RuntimeError, "extract not possible"); + view->obj = NULL; + return -1; + } - int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { - bloblist_t *bl = NULL; + if (!(bl && bl->value)) { + PyErr_SetString(PyExc_RuntimeError, "no data available"); + view->obj = NULL; + return -1; + } - try { - Message::Blob& blob = extract< Message::Blob& >(self); - bl = blob._bl; - } - catch (exception& e) { - PyErr_SetString(PyExc_RuntimeError, "extract not possible"); - view->obj = NULL; - return -1; - } + return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags); +} - if (!(bl && bl->value)) { - PyErr_SetString(PyExc_RuntimeError, "no data available"); - view->obj = NULL; - return -1; - } +string Message::Blob::decode(string encoding) +{ + if (encoding == "") { + string _mime_type = _bl->mime_type ? _bl->mime_type : ""; + encoding = "ascii"; - return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags); - } + if (_mime_type == "application/pEp.sync") + encoding = "pep.sync"; - string Message::Blob::decode(string encoding) - { - if (encoding == "") { - string _mime_type = _bl->mime_type ? _bl->mime_type : ""; - encoding = "ascii"; + if (_mime_type == "application/pEp.keyreset") + encoding = "pep.distribution"; - if (_mime_type == "application/pEp.sync") - encoding = "pep.sync"; + } + object codecs = import("codecs"); + object _decode = codecs.attr("decode"); + return call< string >(_decode.ptr(), this, encoding); +} - if (_mime_type == "application/pEp.keyreset") - encoding = "pep.distribution"; +PyBufferProcs Message::Blob::bp = { getbuffer, NULL }; - } - object codecs = import("codecs"); - object _decode = codecs.attr("decode"); - return call< string >(_decode.ptr(), this, encoding); - } +Message::Message(int dir, Identity *from) + : _msg(new_message((PEP_msg_direction) dir), &free_message) +{ + if (!_msg) + throw bad_alloc(); - PyBufferProcs Message::Blob::bp = { getbuffer, NULL }; + if (from) { + _msg->from = ::identity_dup(*from); + if (!_msg->from) + throw bad_alloc(); + _msg->dir = (PEP_msg_direction) dir; + } +} - Message::Message(int dir, Identity *from) - : _msg(new_message((PEP_msg_direction) dir), &free_message) - { - if (!_msg) +Message::Message(string mimetext) + : _msg(NULL, &free_message) +{ + message *_cpy; + PEP_STATUS status = mime_decode_message(mimetext.c_str(), + mimetext.size(), &_cpy, NULL); + switch (status) { + case PEP_STATUS_OK: + if (_cpy) + _cpy->dir = PEP_dir_outgoing; + else + _cpy = new_message(PEP_dir_outgoing); + + if (!_cpy) throw bad_alloc(); - if (from) { - _msg->from = ::identity_dup(*from); - if (!_msg->from) - throw bad_alloc(); - _msg->dir = (PEP_msg_direction) dir; - } - } + _msg = shared_ptr< message >(_cpy); + break; - Message::Message(string mimetext) - : _msg(NULL, &free_message) - { - message *_cpy; - PEP_STATUS status = mime_decode_message(mimetext.c_str(), - mimetext.size(), &_cpy, NULL); - switch (status) { - case PEP_STATUS_OK: - if (_cpy) - _cpy->dir = PEP_dir_outgoing; - else - _cpy = new_message(PEP_dir_outgoing); - - if (!_cpy) - throw bad_alloc(); - - _msg = shared_ptr< message >(_cpy); - break; - - case PEP_BUFFER_TOO_SMALL: - throw runtime_error("mime_decode_message: buffer too small"); - - case PEP_CANNOT_CREATE_TEMP_FILE: - throw runtime_error("mime_decode_message: cannot create temp file"); - - case PEP_OUT_OF_MEMORY: - throw bad_alloc(); - - default: - stringstream build; - build << "mime_decode_message: unknown error (" << (int) status << ")"; - throw runtime_error(build.str()); - } - } + case PEP_BUFFER_TOO_SMALL: + throw runtime_error("mime_decode_message: buffer too small"); - Message::Message(const Message& second) - : _msg(second._msg) - { - if (!_msg.get()) - throw bad_alloc(); - } + case PEP_CANNOT_CREATE_TEMP_FILE: + throw runtime_error("mime_decode_message: cannot create temp file"); - Message::Message(message *msg) - : _msg(::message_dup(msg), &free_message) - { + case PEP_OUT_OF_MEMORY: + throw bad_alloc(); - } + default: + stringstream build; + build << "mime_decode_message: unknown error (" << (int) status << ")"; + throw runtime_error(build.str()); + } +} - Message::~Message() - { +Message::Message(const Message& second) + : _msg(second._msg) +{ + if (!_msg.get()) + throw bad_alloc(); +} - } +Message::Message(message *msg) + : _msg(::message_dup(msg), &free_message) +{ - Message::operator message *() - { - return _msg.get(); - } +} - Message::operator const message *() const - { - return _msg.get(); - } +Message::~Message() +{ - string Message::_str() - { - if (!(_msg->from && _msg->from->address && _msg->from->address[0])) - throw out_of_range(".from_.address missing"); +} - char *mimetext; - string result; +Message::operator message *() +{ + return _msg.get(); +} - PEP_STATUS status = mime_encode_message(*this, false, &mimetext, false); - switch (status) { - case PEP_STATUS_OK: - result = mimetext; - free(mimetext); - break; +Message::operator const message *() const +{ + return _msg.get(); +} - case PEP_BUFFER_TOO_SMALL: - throw runtime_error("mime_encode_message: buffer too small"); +string Message::_str() +{ + if (!(_msg->from && _msg->from->address && _msg->from->address[0])) + throw out_of_range(".from_.address missing"); - case PEP_CANNOT_CREATE_TEMP_FILE: - throw runtime_error("mime_encode_message: cannot create temp file"); + char *mimetext; + string result; - case PEP_OUT_OF_MEMORY: - throw bad_alloc(); + PEP_STATUS status = mime_encode_message(*this, false, &mimetext, false); + switch (status) { + case PEP_STATUS_OK: + result = mimetext; + free(mimetext); + break; - default: - stringstream build; - build << "mime_encode_message: unknown error (" << (int) status << ")"; - throw runtime_error(build.str()); - } + case PEP_BUFFER_TOO_SMALL: + throw runtime_error("mime_encode_message: buffer too small"); - return result; - } + case PEP_CANNOT_CREATE_TEMP_FILE: + throw runtime_error("mime_encode_message: cannot create temp file"); - string Message::_repr() - { + case PEP_OUT_OF_MEMORY: + throw bad_alloc(); + + default: stringstream build; - build << "Message(" << repr(_str()) << ")"; - return build.str(); - } + build << "mime_encode_message: unknown error (" << (int) status << ")"; + throw runtime_error(build.str()); + } - boost::python::tuple Message::attachments() - { - boost::python::list l; + return result; +} - for (bloblist_t *bl = _msg->attachments; bl && bl->value; bl = - bl->next) { - l.append(Blob(bl, true)); - } +string Message::_repr() +{ + stringstream build; + build << "Message(" << repr(_str()) << ")"; + return build.str(); +} - return boost::python::tuple(l); - } +boost::python::tuple Message::attachments() +{ + boost::python::list l; - void Message::attachments(boost::python::list value) - { - bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL); - if (!bl) - throw bad_alloc(); + for (bloblist_t *bl = _msg->attachments; bl && bl->value; bl = + bl->next) { + l.append(Blob(bl, true)); + } - bloblist_t *_l = bl; - for (int i=0; i(value[i]); - _l = bloblist_add(_l, blob._bl->value, blob._bl->size, - blob._bl->mime_type, blob._bl->filename); - if (!_l) { - for (_l = bl; _l && _l->value; ) { - free(_l->mime_type); - free(_l->filename); - bloblist_t *_ll = _l; - _l = _l->next; - free(_ll); - } - throw bad_alloc(); - } - } + return boost::python::tuple(l); +} - for (int i=0; i(value[i]); - blob._bl->value = NULL; - blob._bl->size = 0; - free(blob._bl->mime_type); - blob._bl->mime_type = NULL; - free(blob._bl->filename); - blob._bl->filename = NULL; +void Message::attachments(boost::python::list value) +{ + bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL); + if (!bl) + throw bad_alloc(); + + bloblist_t *_l = bl; + for (int i=0; i(value[i]); + _l = bloblist_add(_l, blob._bl->value, blob._bl->size, + blob._bl->mime_type, blob._bl->filename); + if (!_l) { + for (_l = bl; _l && _l->value; ) { + free(_l->mime_type); + free(_l->filename); + bloblist_t *_ll = _l; + _l = _l->next; + free(_ll); } - - free_bloblist(_msg->attachments); - _msg->attachments = bl; + throw bad_alloc(); } + } - Message Message::encrypt() - { - boost::python::list extra; - return encrypt_message(*this, extra, PEP_enc_PGP_MIME, 0); - } + for (int i=0; i(value[i]); + blob._bl->value = NULL; + blob._bl->size = 0; + free(blob._bl->mime_type); + blob._bl->mime_type = NULL; + free(blob._bl->filename); + blob._bl->filename = NULL; + } - Message Message::_encrypt(boost::python::list extra, int enc_format, int flags) - { - if (!enc_format) - enc_format = PEP_enc_PGP_MIME; - return encrypt_message(*this, extra, enc_format, flags); - } + free_bloblist(_msg->attachments); + _msg->attachments = bl; +} - boost::python::tuple Message::decrypt(int flags) { - return pEp::PythonAdapter::decrypt_message(*this, flags); - } +Message Message::encrypt() +{ + boost::python::list extra; + return encrypt_message(*this, extra, PEP_enc_PGP_MIME, 0); +} - PEP_rating Message::outgoing_rating() - { - if (_msg->dir != PEP_dir_outgoing) - throw invalid_argument("Message.dir must be outgoing"); +Message Message::_encrypt(boost::python::list extra, int enc_format, int flags) +{ + if (!enc_format) + enc_format = PEP_enc_PGP_MIME; + return encrypt_message(*this, extra, enc_format, flags); +} - if (from().address() == "") - throw invalid_argument("from.address needed"); - if (from().username() == "") - throw invalid_argument("from.username needed"); +boost::python::tuple Message::decrypt(int flags) { + return pEp::PythonAdapter::decrypt_message(*this, flags); +} - if (len(to()) + len(cc()) == 0) - throw invalid_argument("either to or cc needed"); +PEP_rating Message::outgoing_rating() +{ + if (_msg->dir != PEP_dir_outgoing) + throw invalid_argument("Message.dir must be outgoing"); - PEP_STATUS status = myself(pEp::Adapter::session(), _msg->from); - _throw_status(status); + if (from().address() == "") + throw invalid_argument("from.address needed"); + if (from().username() == "") + throw invalid_argument("from.username needed"); - PEP_rating rating = PEP_rating_undefined; - status = outgoing_message_rating(pEp::Adapter::session(), *this, &rating); - _throw_status(status); + if (len(to()) + len(cc()) == 0) + throw invalid_argument("either to or cc needed"); - return rating; - } + PEP_STATUS status = myself(pEp::Adapter::session(), _msg->from); + _throw_status(status); - PEP_color Message::outgoing_color() - { - return _color(outgoing_rating()); - } + PEP_rating rating = PEP_rating_undefined; + status = outgoing_message_rating(pEp::Adapter::session(), *this, &rating); + _throw_status(status); - Message Message::copy() - { - message *dup = message_dup(*this); - if (!dup) - throw bad_alloc(); - return Message(dup); - } + return rating; +} - Message Message::deepcopy(dict&) - { - return copy(); - } +PEP_color Message::outgoing_color() +{ + return _color(outgoing_rating()); +} - Message outgoing_message(Identity me) - { - if (me.address().empty() || me.user_id().empty()) - throw runtime_error("at least address and user_id of own user needed"); +Message Message::copy() +{ + message *dup = message_dup(*this); + if (!dup) + throw bad_alloc(); + return Message(dup); +} - ::myself(pEp::Adapter::session(), me); - auto m = Message(PEP_dir_outgoing, &me); - return m; - } +Message Message::deepcopy(dict&) +{ + return copy(); +} - static object update(Identity ident) - { - if (ident.address().empty()) - throw runtime_error("at least address needed"); - update_identity(pEp::Adapter::session(), ident); - return object(ident); - } +Message outgoing_message(Identity me) +{ + if (me.address().empty() || me.user_id().empty()) + throw runtime_error("at least address and user_id of own user needed"); - static boost::python::list update(boost::python::list il) - { - for (int i=0; i(il[i])); - } + ::myself(pEp::Adapter::session(), me); + auto m = Message(PEP_dir_outgoing, &me); + return m; +} - return il; - } +static object update(Identity ident) +{ + if (ident.address().empty()) + throw runtime_error("at least address needed"); + update_identity(pEp::Adapter::session(), ident); + return object(ident); +} - Message incoming_message(string mime_text) - { - auto m = Message(mime_text); - m.dir(PEP_dir_incoming); +static boost::python::list update(boost::python::list il) +{ + for (int i=0; i(il[i])); + } - try { - m.from(update(m.from())); - } - catch (out_of_range&) { } + return il; +} - try { - m.recv_by(update(m.recv_by())); - } - catch (out_of_range&) { } +Message incoming_message(string mime_text) +{ + auto m = Message(mime_text); + m.dir(PEP_dir_incoming); - m.to(update(m.to())); - m.cc(update(m.cc())); - m.reply_to(update(m.reply_to())); + try { + m.from(update(m.from())); + } + catch (out_of_range&) { } - return m; - } + try { + m.recv_by(update(m.recv_by())); } + catch (out_of_range&) { } + + m.to(update(m.to())); + m.cc(update(m.cc())); + m.reply_to(update(m.reply_to())); + + return m; } + +} // namespace PythonAdapter +} // namespace pEp { diff --git a/src/pEp/native_pEp/message.hh b/src/pEp/native_pEp/message.hh index e366945..6ccc031 100644 --- a/src/pEp/native_pEp/message.hh +++ b/src/pEp/native_pEp/message.hh @@ -3,145 +3,150 @@ #pragma once +// System +#include #include #include -#include +// Engine #include #include +// local #include "str_attr.hh" #include "identity.hh" namespace pEp { - namespace PythonAdapter { - using namespace std; - using namespace boost::python; - using boost::lexical_cast; +namespace PythonAdapter { +using std::string; +using std::runtime_error; +using std::invalid_argument; +using boost::lexical_cast; + +// Message is owning a message struct - // Message is owning a message struct +class Message { + shared_ptr< ::message > _msg; - class Message { - shared_ptr< ::message > _msg; +public: + // Blob is owning a bloblist_t struct - or not and just managing + // one depending on part_of_chain - public: - // Blob is owning a bloblist_t struct - or not and just managing - // one depending on part_of_chain + class Blob { + bloblist_t *_bl; + bool part_of_chain; - class Blob { - bloblist_t *_bl; - bool part_of_chain; + public: + Blob(bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL), + bool chained = false); + Blob(object data, string mime_type = "", string filename = ""); + Blob(const Blob& second); + ~Blob(); - public: - Blob(bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL), - bool chained = false); - Blob(object data, string mime_type = "", string filename = ""); - Blob(const Blob& second); - ~Blob(); + string _repr(); - string _repr(); + string mime_type() { return _bl ? str_attr(_bl->mime_type) : ""; } + void mime_type(string value) { str_attr(_bl->mime_type, value); } - string mime_type() { return _bl ? str_attr(_bl->mime_type) : ""; } - void mime_type(string value) { str_attr(_bl->mime_type, value); } + string filename() { return str_attr(_bl->filename); } + void filename(string value) { str_attr(_bl->filename, value); } - string filename() { return str_attr(_bl->filename); } - void filename(string value) { str_attr(_bl->filename, value); } + size_t size() { return _bl->size; } + string decode(string encoding); + string decode() { return decode(""); } - size_t size() { return _bl->size; } - string decode(string encoding); - string decode() { return decode(""); } + static PyBufferProcs bp; - static PyBufferProcs bp; + friend class Message; - friend class Message; + protected: + static int getbuffer(PyObject *self, Py_buffer *view, int flags); + }; - protected: - static int getbuffer(PyObject *self, Py_buffer *view, int flags); - }; + Message(int dir = PEP_dir_outgoing, Identity *from = NULL); + Message(string mimetext); + Message(const Message& second); + Message(message *msg); + ~Message(); + operator message *(); + operator const message *() const; - Message(int dir = PEP_dir_outgoing, Identity *from = NULL); - Message(string mimetext); - Message(const Message& second); - Message(message *msg); - ~Message(); - operator message *(); - operator const message *() const; + string _str(); + string _repr(); - string _str(); - string _repr(); + PEP_msg_direction dir() { return _msg->dir; } + void dir(PEP_msg_direction value) { _msg->dir = value; } - PEP_msg_direction dir() { return _msg->dir; } - void dir(PEP_msg_direction value) { _msg->dir = value; } + string id() { return str_attr(_msg->id); } + void id(string value) { str_attr(_msg->id, value); } - string id() { return str_attr(_msg->id); } - void id(string value) { str_attr(_msg->id, value); } + string shortmsg() { return str_attr(_msg->shortmsg); } + void shortmsg(string value) { str_attr(_msg->shortmsg, value); } - string shortmsg() { return str_attr(_msg->shortmsg); } - void shortmsg(string value) { str_attr(_msg->shortmsg, value); } + string longmsg() { return str_attr(_msg->longmsg); } + void longmsg(string value) { str_attr(_msg->longmsg, value); } - string longmsg() { return str_attr(_msg->longmsg); } - void longmsg(string value) { str_attr(_msg->longmsg, value); } + string longmsg_formatted() { return str_attr(_msg->longmsg_formatted); } + void longmsg_formatted(string value) { str_attr(_msg->longmsg_formatted, value); } - string longmsg_formatted() { return str_attr(_msg->longmsg_formatted); } - void longmsg_formatted(string value) { str_attr(_msg->longmsg_formatted, value); } + boost::python::tuple attachments(); + void attachments(boost::python::list value); - boost::python::tuple attachments(); - void attachments(boost::python::list value); + time_t sent() { return timestamp_attr(_msg->sent); } + void sent(time_t value) { timestamp_attr(_msg->sent, value); } - time_t sent() { return timestamp_attr(_msg->sent); } - void sent(time_t value) { timestamp_attr(_msg->sent, value); } + time_t recv() { return timestamp_attr(_msg->recv); } + void recv(time_t value) { timestamp_attr(_msg->recv, value); } - time_t recv() { return timestamp_attr(_msg->recv); } - void recv(time_t value) { timestamp_attr(_msg->recv, value); } + Identity from() { return identity_attr(_msg->from); } + void from(object value) { identity_attr(_msg->from, value); } - Identity from() { return identity_attr(_msg->from); } - void from(object value) { identity_attr(_msg->from, value); } + boost::python::list to() { return identitylist_attr(_msg->to); } + void to(boost::python::list value) { identitylist_attr(_msg->to, value); } - boost::python::list to() { return identitylist_attr(_msg->to); } - void to(boost::python::list value) { identitylist_attr(_msg->to, value); } + Identity recv_by() { return identity_attr(_msg->recv_by); } + void recv_by(object value) { identity_attr(_msg->recv_by, value); } - Identity recv_by() { return identity_attr(_msg->recv_by); } - void recv_by(object value) { identity_attr(_msg->recv_by, value); } + boost::python::list cc() { return identitylist_attr(_msg->cc); } + void cc(boost::python::list value) { identitylist_attr(_msg->cc, value); } - boost::python::list cc() { return identitylist_attr(_msg->cc); } - void cc(boost::python::list value) { identitylist_attr(_msg->cc, value); } + boost::python::list bcc() { return identitylist_attr(_msg->bcc); } + void bcc(boost::python::list value) { identitylist_attr(_msg->bcc, value); } - boost::python::list bcc() { return identitylist_attr(_msg->bcc); } - void bcc(boost::python::list value) { identitylist_attr(_msg->bcc, value); } + boost::python::list reply_to() { return identitylist_attr(_msg->reply_to); } + void reply_to(boost::python::list value) { identitylist_attr(_msg->reply_to, value); } - boost::python::list reply_to() { return identitylist_attr(_msg->reply_to); } - void reply_to(boost::python::list value) { identitylist_attr(_msg->reply_to, value); } + boost::python::list in_reply_to() { return strlist_attr(_msg->in_reply_to); } + void in_reply_to(boost::python::list value) { strlist_attr(_msg->in_reply_to, value); } - boost::python::list in_reply_to() { return strlist_attr(_msg->in_reply_to); } - void in_reply_to(boost::python::list value) { strlist_attr(_msg->in_reply_to, value); } + boost::python::list references() { return strlist_attr(_msg->references); } + void references(boost::python::list value) { strlist_attr(_msg->references, value); } - boost::python::list references() { return strlist_attr(_msg->references); } - void references(boost::python::list value) { strlist_attr(_msg->references, value); } + boost::python::list keywords() { return strlist_attr(_msg->keywords); } + void keywords(boost::python::list value) { strlist_attr(_msg->keywords, value); } - boost::python::list keywords() { return strlist_attr(_msg->keywords); } - void keywords(boost::python::list value) { strlist_attr(_msg->keywords, value); } + string comments() { return str_attr(_msg->comments); } + void comments(string value) { str_attr(_msg->comments, value); } - string comments() { return str_attr(_msg->comments); } - void comments(string value) { str_attr(_msg->comments, value); } + dict opt_fields() { return strdict_attr(_msg->opt_fields); } + void opt_fields(dict value) { return strdict_attr(_msg->opt_fields, value); } - dict opt_fields() { return strdict_attr(_msg->opt_fields); } - void opt_fields(dict value) { return strdict_attr(_msg->opt_fields, value); } + PEP_enc_format enc_format() { return _msg->enc_format; } + void enc_format(PEP_enc_format value) { _msg->enc_format = value; } - PEP_enc_format enc_format() { return _msg->enc_format; } - void enc_format(PEP_enc_format value) { _msg->enc_format = value; } + Message encrypt(); + Message _encrypt(boost::python::list extra, int enc_format=4, int flags=0); - Message encrypt(); - Message _encrypt(boost::python::list extra, int enc_format=4, int flags=0); + boost::python::tuple decrypt(int flags=0); + PEP_rating outgoing_rating(); + PEP_color outgoing_color(); + Message deepcopy(dict& memo); + Message copy(); +}; - boost::python::tuple decrypt(int flags=0); - PEP_rating outgoing_rating(); - PEP_color outgoing_color(); - Message deepcopy(dict& memo); - Message copy(); - }; +Message outgoing_message(Identity me); +Message incoming_message(string mime_text); - Message outgoing_message(Identity me); - Message incoming_message(string mime_text); - } -} +} // namespace PythonAdapter +} // namespace pEp { diff --git a/src/pEp/native_pEp/message_api.cc b/src/pEp/native_pEp/message_api.cc index 7b7d0ad..da8a199 100644 --- a/src/pEp/native_pEp/message_api.cc +++ b/src/pEp/native_pEp/message_api.cc @@ -1,168 +1,173 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt + +// Engine #include #include #include #include #include +// local #include "message_api.hh" #include "basic_api.hh" namespace pEp { - namespace PythonAdapter { - Message encrypt_message(Message src, boost::python::list extra, int enc_format, - int flags) - { - Identity _from = src.from(); - if (_from.address() == "") - throw invalid_argument("encrypt_message: src.from_.address empty"); - if (_from.username() == "") - throw invalid_argument("encrypt_message: src.from_.username empty"); - - if (_from.user_id() == "") - src.from().user_id(_from.address()); - - stringlist_t *_extra = to_stringlist(extra); - PEP_enc_format _enc_format = (PEP_enc_format) enc_format; - PEP_encrypt_flags_t _flags = (PEP_encrypt_flags_t) flags; - message *_dst = NULL; - - message *_src = src; - PEP_STATUS status = encrypt_message(pEp::Adapter::session(), _src, _extra, &_dst, - _enc_format, _flags); - free_stringlist(_extra); - _throw_status(status); - - if (!_dst || _dst == _src) - return Message(_src); - - return Message(_dst); - } - - boost::python::tuple decrypt_message(Message src, int flags) - { - message *_dst = NULL; - stringlist_t *_keylist = NULL; - PEP_rating _rating = PEP_rating_undefined; - PEP_decrypt_flags_t _flags = (PEP_decrypt_flags_t) flags; - message *_src = src; - - PEP_STATUS status = ::decrypt_message(pEp::Adapter::session(), _src, &_dst, &_keylist, - &_rating, &_flags); - _throw_status(status); - - boost::python::list keylist; - if (_keylist) { - keylist = from_stringlist(_keylist); - free_stringlist(_keylist); - } - - Message dst = _dst ? Message(_dst) : Message(src); - return boost::python::make_tuple(dst, keylist, _rating, _flags); - } - - PEP_color _color(int rating) - { - return ::color_from_rating((PEP_rating) rating); - } - - boost::python::tuple sync_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_Sync_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 sync_encode(string text) - { - char *data = NULL; - size_t size = 0; - PEP_STATUS status = XER_to_PER_Sync_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); - } - - 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") { - 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); - } - } - - 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); - } - } +namespace PythonAdapter { +using namespace std; +using namespace boost::python; + +Message encrypt_message(Message src, boost::python::list extra, int enc_format, int flags) +{ + Identity _from = src.from(); + if (_from.address() == "") + throw invalid_argument("encrypt_message: src.from_.address empty"); + if (_from.username() == "") + throw invalid_argument("encrypt_message: src.from_.username empty"); + + if (_from.user_id() == "") + src.from().user_id(_from.address()); + + stringlist_t *_extra = to_stringlist(extra); + PEP_enc_format _enc_format = (PEP_enc_format) enc_format; + PEP_encrypt_flags_t _flags = (PEP_encrypt_flags_t) flags; + message *_dst = NULL; + + message *_src = src; + PEP_STATUS status = encrypt_message(pEp::Adapter::session(), _src, _extra, &_dst, + _enc_format, _flags); + free_stringlist(_extra); + _throw_status(status); + + if (!_dst || _dst == _src) + return Message(_src); + + return Message(_dst); +} + +boost::python::tuple decrypt_message(Message src, int flags) +{ + message *_dst = NULL; + stringlist_t *_keylist = NULL; + PEP_rating _rating = PEP_rating_undefined; + PEP_decrypt_flags_t _flags = (PEP_decrypt_flags_t) flags; + message *_src = src; + + PEP_STATUS status = ::decrypt_message(pEp::Adapter::session(), _src, &_dst, &_keylist, + &_rating, &_flags); + _throw_status(status); + + boost::python::list keylist; + if (_keylist) { + keylist = from_stringlist(_keylist); + free_stringlist(_keylist); + } + + Message dst = _dst ? Message(_dst) : Message(src); + return boost::python::make_tuple(dst, keylist, _rating, _flags); +} + +PEP_color _color(int rating) +{ + return ::color_from_rating((PEP_rating) rating); +} + +boost::python::tuple sync_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_Sync_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 sync_encode(string text) +{ + char *data = NULL; + size_t size = 0; + PEP_STATUS status = XER_to_PER_Sync_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); +} + +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") { + 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); } } + +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); + } +} + +} // namespace PythonAdapter +} // namespace pEp { diff --git a/src/pEp/native_pEp/message_api.hh b/src/pEp/native_pEp/message_api.hh index ecc3a02..5147fe3 100644 --- a/src/pEp/native_pEp/message_api.hh +++ b/src/pEp/native_pEp/message_api.hh @@ -6,12 +6,19 @@ #include "pEpmodule.hh" namespace pEp { - namespace PythonAdapter { - Message encrypt_message(Message src, boost::python::list extra = boost::python::list(), - int enc_format = 4, int flags = 0); - boost::python::tuple decrypt_message(Message src, int flags=0); - PEP_color _color(int rating); - object sync_search(string name); - object distribution_search(string name); - } -} +namespace PythonAdapter { + +Message encrypt_message( + Message src, + boost::python::list extra = boost::python::list(), + int enc_format = 4, + int flags = 0 +); + +boost::python::tuple decrypt_message(Message src, int flags=0); +PEP_color _color(int rating); +object sync_search(string name); +object distribution_search(string name); + +} // namespace PythonAdapter +} // namespace pEp { diff --git a/src/pEp/native_pEp/pEpmodule.cc b/src/pEp/native_pEp/pEpmodule.cc index e15d520..1dfbb87 100644 --- a/src/pEp/native_pEp/pEpmodule.cc +++ b/src/pEp/native_pEp/pEpmodule.cc @@ -1,6 +1,7 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt +// System #include #include #include @@ -8,180 +9,190 @@ #include #include -#include +// Engine #include #include #include #include +// libpEpAdapter +#include +#include +#include + +// local #include "pEpmodule.hh" #include "basic_api.hh" #include "message_api.hh" #include "user_interface.hh" - namespace pEp { - namespace PythonAdapter { - using namespace std; - -// Adapter adapter(true); - - void config_passive_mode(bool enable) - { - ::config_passive_mode(pEp::Adapter::session(), enable); - } - - void config_unencrypted_subject(bool enable) - { - ::config_unencrypted_subject(pEp::Adapter::session(), enable); - } - - void key_reset_user(string user_id, string fpr) - { - if (user_id == "") - throw invalid_argument("user_id required"); - - PEP_STATUS status = ::key_reset_user(pEp::Adapter::session(), - user_id.c_str(), fpr != "" ? fpr.c_str() : nullptr); - _throw_status(status); - } - - void key_reset_user2(string user_id) - { - key_reset_user(user_id, ""); - } - - void key_reset_all_own_keys() - { - PEP_STATUS status = ::key_reset_all_own_keys(pEp::Adapter::session()); - _throw_status(status); - } - - scope *_scope = NULL; - - static const char *version_string = "p≡p Python adapter version 0.3"; - static string about() - { - string version = string(version_string) + "\np≡p version " - + PEP_VERSION + "\n"; - return version; - } - - void _throw_status(PEP_STATUS status) - { - if (status == PEP_STATUS_OK) - return; - if (status >= 0x400 && status <= 0x4ff) - return; - if (status == PEP_OUT_OF_MEMORY) - throw bad_alloc(); - if (status == PEP_ILLEGAL_VALUE) - throw invalid_argument("illegal value"); - - if (string(pEp_status_to_string(status)) == "unknown status code") { - stringstream build; - build << setfill('0') << "p≡p 0x" << setw(4) << hex << status; - throw runtime_error(build.str()); - } - else { - throw runtime_error(pEp_status_to_string(status)); - } - } - - PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr) - { - return PEP_STATUS_OK; - } - - PEP_STATUS _messageToSend(::message *msg) - { - if (!_scope) - return PEP_SEND_FUNCTION_NOT_REGISTERED; - - try { - object m = _scope->attr("messageToSend"); - call< void >(m.ptr(), Message(msg)); - } - catch (exception& e) { } - - return PEP_STATUS_OK; - } - - void messageToSend(Message msg) { - throw runtime_error("implement pEp.messageToSend(msg)"); - } +namespace PythonAdapter { +using namespace std; +using namespace boost::python; + +scope *_scope = NULL; +static const char *version_string = "p≡p Python adapter version 0.3"; + +void _init() { + pEpLog("called"); + callback_dispatcher.add(_messageToSend, nullptr, nullptr, nullptr); +} + +void config_passive_mode(bool enable) +{ + ::config_passive_mode(pEp::Adapter::session(), enable); +} + +void config_unencrypted_subject(bool enable) +{ + ::config_unencrypted_subject(pEp::Adapter::session(), enable); +} + +void key_reset_user(string user_id, string fpr) +{ + if (user_id == "") + throw invalid_argument("user_id required"); + + PEP_STATUS status = ::key_reset_user(pEp::Adapter::session(), + user_id.c_str(), fpr != "" ? fpr.c_str() : nullptr); + _throw_status(status); +} + +void key_reset_user2(string user_id) +{ + key_reset_user(user_id, ""); +} + +void key_reset_all_own_keys() +{ + PEP_STATUS status = ::key_reset_all_own_keys(pEp::Adapter::session()); + _throw_status(status); +} + +static string about() +{ + string version = string(version_string) + "\np≡p version " + + PEP_VERSION + "\n"; + return version; +} + +void _throw_status(PEP_STATUS status) +{ + if (status == PEP_STATUS_OK) + return; + if (status >= 0x400 && status <= 0x4ff) + return; + if (status == PEP_OUT_OF_MEMORY) + throw bad_alloc(); + if (status == PEP_ILLEGAL_VALUE) + throw invalid_argument("illegal value"); + + if (string(pEp_status_to_string(status)) == "unknown status code") { + stringstream build; + build << setfill('0') << "p≡p 0x" << setw(4) << hex << status; + throw runtime_error(build.str()); + } + else { + throw runtime_error(pEp_status_to_string(status)); + } +} + +PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr) +{ + return PEP_STATUS_OK; +} + +PEP_STATUS _messageToSend(::message *msg) +{ + if (!_scope) + return PEP_SEND_FUNCTION_NOT_REGISTERED; + + try { + object m = _scope->attr("messageToSend"); + call< void >(m.ptr(), Message(msg)); + } + catch (exception& e) { } + + return PEP_STATUS_OK; +} + +void messageToSend(Message msg) { + throw runtime_error("implement pEp.messageToSend(msg)"); +} // void do_sync_protocol() // { // ::do_sync_protocol(pEp::Adapter::session(), nullptr); // } - void shutdown_sync() - { - pEp::Adapter::shutdown(); - } +void shutdown_sync() +{ + pEp::Adapter::shutdown(); +} - void debug_color(int ansi_color) - { - ::set_debug_color(pEp::Adapter::session(), ansi_color); - } +void debug_color(int ansi_color) +{ + ::set_debug_color(pEp::Adapter::session(), ansi_color); +} - void leave_device_group() { - ::leave_device_group(pEp::Adapter::session()); - } +void leave_device_group() +{ + ::leave_device_group(pEp::Adapter::session()); +} // void script_is_implementing_sync() { // adapter.script_is_implementing_sync(); // } - bool is_sync_active() { - return pEp::Adapter::is_sync_running(); - } - } +bool is_sync_active() +{ + return pEp::Adapter::is_sync_running(); } BOOST_PYTHON_MODULE(native_pEp) { - using namespace boost::python; - using namespace boost::locale; - using namespace pEp::PythonAdapter; docstring_options doc_options(true, false, false); - generator gen; + boost::locale::generator gen; std::locale::global(gen("")); - _scope = new scope(); + // Module init function called by pEp.init() + def("_init", _init); + + + _scope = new scope(); scope().attr("about") = about(); scope().attr("per_user_directory") = per_user_directory(); scope().attr("per_machine_directory") = per_machine_directory(); scope().attr("engine_version") = get_engine_version(); scope().attr("protocol_version") = get_protocol_version(); - def("passive_mode", pEp::PythonAdapter::config_passive_mode, + def("passive_mode", config_passive_mode, "do not attach pub keys to all messages"); - def("unencrypted_subject", pEp::PythonAdapter::config_unencrypted_subject, + def("unencrypted_subject", config_unencrypted_subject, "do not encrypt the subject of messages"); - def("key_reset", pEp::PythonAdapter::key_reset_user, + def("key_reset", key_reset_user, "reset the default database status for the user / keypair provided\n" "This will effectively perform key_reset on each identity\n" "associated with the key and user_id, if a key is provided, and for\n" "each key (and all of their identities) if an fpr is not."); - def("key_reset", pEp::PythonAdapter::key_reset_user2, + def("key_reset", key_reset_user2, "reset the default database status for the user / keypair provided\n" "This will effectively perform key_reset on each identity\n" "associated with the key and user_id, if a key is provided, and for\n" "each key (and all of their identities) if an fpr is not."); - def("key_reset_all_own_keys", pEp::PythonAdapter::key_reset_all_own_keys, + def("key_reset_all_own_keys", key_reset_all_own_keys, "revoke and mistrust all own keys, generate new keys for all\n" "own identities, and opportunistically communicate key reset\n" "information to people we have recently contacted."); - auto identity_class = class_("Identity", + auto identity_class = class_("Identity", "Identity(address, username, user_id='', fpr='', comm_type=0, lang='en')\n" "\n" "represents a p≡p identity\n" @@ -204,12 +215,12 @@ BOOST_PYTHON_MODULE(native_pEp) .def(boost::python::init()) .def(boost::python::init()) .def(boost::python::init()) - .def("__repr__", &pEp::PythonAdapter::Identity::_repr) - .def("__str__", &pEp::PythonAdapter::Identity::_str, + .def("__repr__", &Identity::_repr) + .def("__str__", &Identity::_str, "string representation of this identity\n" "following the pattern 'username < address >'\n" ) - .def("key_reset", &pEp::PythonAdapter::Identity::key_reset, + .def("key_reset", &Identity::key_reset, boost::python::arg("fpr")=object(""), "reset the default database status for the identity / keypair provided. If this\n" "corresponds to the own user and a private key, also revoke the key, generate a\n" @@ -217,7 +228,7 @@ BOOST_PYTHON_MODULE(native_pEp) "identity. If it does not, remove the key from the keyring; the key's status is\n" "completely fresh on next contact from the partner.") - .def("key_mistrusted", &pEp::PythonAdapter::Identity::key_mistrusted, + .def("key_mistrusted", &Identity::key_mistrusted, boost::python::arg("fpr")=object(""), "If you want updated trust on the identity, you ll have" "to call update_identity or myself respectively after this." @@ -227,42 +238,42 @@ BOOST_PYTHON_MODULE(native_pEp) "will only undo the current identity's / it's user's default, not any" "other identities which may be impacted (this will not affect most use cases)") - .def("enable_for_sync", &pEp::PythonAdapter::Identity::enable_for_sync, + .def("enable_for_sync", &Identity::enable_for_sync, "Enable own identity for p≡p sync.\n\n" "Only use this on own identities, which are used as accounts.\n") - .def("disable_for_sync", &pEp::PythonAdapter::Identity::disable_for_sync, + .def("disable_for_sync", &Identity::disable_for_sync, "Disable own identity for p≡p sync.\n\n" "Only use this on own identities, which are used as accounts.\n") - .add_property("address", (string(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::address, - (void(pEp::PythonAdapter::Identity::*)(string)) &pEp::PythonAdapter::Identity::address, + .add_property("address", (string(Identity::*)()) &Identity::address, + (void(Identity::*)(string)) &Identity::address, "email address or URI") - .add_property("fpr", (string(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::fpr, - (void(pEp::PythonAdapter::Identity::*)(string)) &pEp::PythonAdapter::Identity::fpr, + .add_property("fpr", (string(Identity::*)()) &Identity::fpr, + (void(Identity::*)(string)) &Identity::fpr, "key ID (full fingerprint, hex encoded)") - .add_property("user_id", (string(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::user_id, - (void(pEp::PythonAdapter::Identity::*)(string)) &pEp::PythonAdapter::Identity::user_id, + .add_property("user_id", (string(Identity::*)()) &Identity::user_id, + (void(Identity::*)(string)) &Identity::user_id, "ID of person associated or 'pEp_own_userId' if own identity") - .add_property("username", (string(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::username, - (void(pEp::PythonAdapter::Identity::*)(string)) &pEp::PythonAdapter::Identity::username, + .add_property("username", (string(Identity::*)()) &Identity::username, + (void(Identity::*)(string)) &Identity::username, "name in full of person associated") - .add_property("comm_type", (int(pEp::PythonAdapter::Identity::*)()) - (PEP_comm_type(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::comm_type, - (void(pEp::PythonAdapter::Identity::*)(int)) - (void(pEp::PythonAdapter::Identity::*)(PEP_comm_type)) &pEp::PythonAdapter::Identity::comm_type, + .add_property("comm_type", (int(Identity::*)()) + (PEP_comm_type(Identity::*)()) &Identity::comm_type, + (void(Identity::*)(int)) + (void(Identity::*)(PEP_comm_type)) &Identity::comm_type, "communication type, first rating level (p≡p internal)") - .add_property("lang", (string(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::lang, - (void(pEp::PythonAdapter::Identity::*)(string)) &pEp::PythonAdapter::Identity::lang, + .add_property("lang", (string(Identity::*)()) &Identity::lang, + (void(Identity::*)(string)) &Identity::lang, "ISO 639-1 language code") - .add_property("flags", (identity_flags_t(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::flags, - (void(pEp::PythonAdapter::Identity::*)(identity_flags_t)) &pEp::PythonAdapter::Identity::flags, + .add_property("flags", (identity_flags_t(Identity::*)()) &Identity::flags, + (void(Identity::*)(identity_flags_t)) &Identity::flags, "flags (p≡p internal)") - .add_property("rating", &pEp::PythonAdapter::Identity::rating, "rating of Identity") - .add_property("color", &pEp::PythonAdapter::Identity::color, "color of Identity as PEP_color") - .add_property("is_pEp_user", &pEp::PythonAdapter::Identity::is_pEp_user, "True if this is an identity of a pEp user") - .def("__deepcopy__", &pEp::PythonAdapter::Identity::deepcopy) - .def("update", &pEp::PythonAdapter::Identity::update, "update Identity") - .def("__copy__", &pEp::PythonAdapter::Identity::copy); + .add_property("rating", &Identity::rating, "rating of Identity") + .add_property("color", &Identity::color, "color of Identity as PEP_color") + .add_property("is_pEp_user", &Identity::is_pEp_user, "True if this is an identity of a pEp user") + .def("__deepcopy__", &Identity::deepcopy) + .def("update", &Identity::update, "update Identity") + .def("__copy__", &Identity::copy); identity_class.attr("PEP_OWN_USERID") = "pEp_own_userId"; @@ -313,7 +324,7 @@ BOOST_PYTHON_MODULE(native_pEp) " mime_text text in Multipurpose Internet Mail Extensions format\n" ) .def(boost::python::init()) - .def(boost::python::init()) + .def(boost::python::init()) .def(boost::python::init()) .def("__str__", &Message::_str, "the string representation of a Message is it's MIME text" @@ -345,13 +356,13 @@ BOOST_PYTHON_MODULE(native_pEp) .add_property("recv", (time_t(Message::*)()) &Message::recv, (void(Message::*)(time_t)) &Message::recv, "time when message was received in UTC seconds since epoch") - .add_property("from_", (pEp::PythonAdapter::Identity(Message::*)()) &Message::from, + .add_property("from_", (Identity(Message::*)()) &Message::from, (void(Message::*)(object)) &Message::from, "identity where message is from") .add_property("to", (boost::python::list(Message::*)()) &Message::to, (void(Message::*)(boost::python::list)) &Message::to, "list of identities message is going to") - .add_property("recv_by", (pEp::PythonAdapter::Identity(Message::*)()) &Message::recv_by, + .add_property("recv_by", (Identity(Message::*)()) &Message::recv_by, (void(Message::*)(object)) &Message::recv_by, "identity where message was received by") .add_property("cc", (boost::python::list(Message::*)()) &Message::cc, @@ -414,19 +425,19 @@ BOOST_PYTHON_MODULE(native_pEp) // basic API and key management API - def("update_identity", &pEp::PythonAdapter::update_identity, + def("update_identity", &update_identity, "update_identity(ident)\n" "\n" "update identity information\n" "call this to complete identity information when you at least have an address\n" ); - def("myself", &pEp::PythonAdapter::myself, + def("myself", &myself, "myself(ident)\n" "\n" "ensures that the own identity is being complete\n" "supply ident.address and ident.username\n" ); - def("trust_personal_key", &pEp::PythonAdapter::trust_personal_key, + def("trust_personal_key", &trust_personal_key, "trust_personal_key(ident)\n" "\n" "mark a key as trusted with a person\n" @@ -437,44 +448,44 @@ BOOST_PYTHON_MODULE(native_pEp) .value("PEP_idf_list", PEP_idf_list) .value("PEP_idf_devicegroup", PEP_idf_devicegroup); - def("set_identity_flags", &pEp::PythonAdapter::set_identity_flags, + def("set_identity_flags", &set_identity_flags, "set_identity_flags(ident, flags)\n" "\n" "set identity flags\n" ); - def("unset_identity_flags", &pEp::PythonAdapter::unset_identity_flags, + def("unset_identity_flags", &unset_identity_flags, "unset_identity_flags(ident, flags)\n" "\n" "unset identity flags\n" ); - def("key_reset_trust", &pEp::PythonAdapter::key_reset_trust, + def("key_reset_trust", &key_reset_trust, "key_reset_trust(ident)\n" "\n" "reset trust bit or explicitly mistrusted status for an identity and " "its accompanying key/user_id pair\n" ); - def("import_key", &pEp::PythonAdapter::import_key, + def("import_key", &import_key, "private_key_list = import_key(key_data)\n" "\n" "import key(s) from key_data\n" ); - def("export_key", &pEp::PythonAdapter::export_key, + def("export_key", &export_key, "key_data = export_key(identity)\n" "\n" "export key(s) of identity\n" ); - def("export_secret_key", &pEp::PythonAdapter::export_secret_key, + def("export_secret_key", &export_secret_key, "key_data = export_seret_key(identity)\n" "\n" "export secret key(s) of identity\n" ); - def("set_own_key", &pEp::PythonAdapter::set_own_key, + def("set_own_key", &set_own_key, "set_own_key(me, fpr)\n" "\n" "mark a key as an own key, and make it the default key\n" @@ -534,7 +545,7 @@ BOOST_PYTHON_MODULE(native_pEp) // messageToSend() - def("messageToSend", &pEp::PythonAdapter::messageToSend, + def("messageToSend", &messageToSend, "messageToSend(msg)\n" "\n" "override pEp.messageToSend(msg) with your own implementation\n" @@ -577,41 +588,40 @@ BOOST_PYTHON_MODULE(native_pEp) // " result -1: cancel, 0: accepted, 1: rejected\n" // " identities list of identities to share or None for all\n" // "\n" -// "call to deliver the handshake result of the handshake dialog") -// ; +// "call to deliver the handshake result of the handshake dialog"); // TODO: Replace with start_sync() -// def("do_sync_protocol", &pEp::PythonAdapter::do_sync_protocol, +// def("do_sync_protocol", &do_sync_protocol, // "do_sync_protocol()\n" // "\n" // "in case of an explicit sync thread instead of a single threaded\n" // "implementation call this function in your sync thread\n" // ); - def("shutdown_sync", &pEp::PythonAdapter::shutdown_sync, + def("shutdown_sync", &shutdown_sync, "shutdown_sync()\n" "\n" "call this from another thread to shut down the sync thread\n" ); - def("debug_color", &pEp::PythonAdapter::debug_color, + def("debug_color", &debug_color, "for debug builds set ANSI color value"); - def("leave_device_group", &pEp::PythonAdapter::leave_device_group, + def("leave_device_group", &leave_device_group, "leave_device_group()\n" "\n" "call this for a grouped device, which should leave\n" ); - -// def("script_is_implementing_sync", &pEp::PythonAdapter::script_is_implementing_sync, + +// def("script_is_implementing_sync", &script_is_implementing_sync, // "script_is_implementing_sync()\n" // "\n" // "call this in case the Python script is implementing sync to make\n" // "is_sync_active() working\n" // ); - def("is_sync_active", &pEp::PythonAdapter::is_sync_active, + def("is_sync_active", &is_sync_active, "is_sync_active()\n" "\n" "True if sync is active, False otherwise\n" @@ -622,3 +632,6 @@ BOOST_PYTHON_MODULE(native_pEp) call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(sync_search)); call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(distribution_search)); } + +} // namespace PythonAdapter +} // namespace pEp diff --git a/src/pEp/native_pEp/pEpmodule.hh b/src/pEp/native_pEp/pEpmodule.hh index f900c38..71ff9c5 100644 --- a/src/pEp/native_pEp/pEpmodule.hh +++ b/src/pEp/native_pEp/pEpmodule.hh @@ -1,21 +1,26 @@ #pragma once +// Engine #include + +// local #include "message.hh" namespace pEp { - namespace PythonAdapter { - extern string device_name; - void config_passive_mode(bool enable); - void config_unencrypted_subject(bool enable); - void key_reset_user(string user_id, string fpr); - void key_reset_all_own_keys(); - void _throw_status(PEP_STATUS status); - void messageToSend(Message msg); - PEP_STATUS _messageToSend(::message *msg); - PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr); -// void do_sync_protocol(); -// extern Adapter adapter; - } -} +namespace PythonAdapter { + +extern string device_name; +void config_passive_mode(bool enable); +void config_unencrypted_subject(bool enable); +void key_reset_user(string user_id, string fpr); +void key_reset_all_own_keys(); +void _throw_status(PEP_STATUS status); +void messageToSend(Message msg); +PEP_STATUS _messageToSend(::message *msg); +PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr); +// void do_sync_protocol(); +// extern Adapter adapter; + +} // namespace PythonAdapter +} // namespace pEp { diff --git a/src/pEp/native_pEp/str_attr.cc b/src/pEp/native_pEp/str_attr.cc index 6240a50..96810ad 100644 --- a/src/pEp/native_pEp/str_attr.cc +++ b/src/pEp/native_pEp/str_attr.cc @@ -1,180 +1,184 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt +// System +#include #include #include -#include +// local #include "str_attr.hh" namespace pEp { - namespace utility { - using namespace std; - using namespace boost::locale; +namespace PythonAdapter { +using namespace std; +using namespace boost::python; +using namespace boost::locale; + +object repr(object s) +{ + return s.attr("__repr__")(); +} - object repr(object s) - { - return s.attr("__repr__")(); - } +string repr(string s) +{ + str _s = s.c_str(); + object _r = _s.attr("__repr__")(); + string r = extract< string >(_r); + return r; +} - string repr(string s) - { - str _s = s.c_str(); - object _r = _s.attr("__repr__")(); - string r = extract< string >(_r); - return r; - } +string str_attr(char *&str) +{ + if (!str) + return string(""); + return string(str); +} - string str_attr(char *&str) - { - if (!str) - return string(""); - return string(str); - } +void str_attr(char *&str, string value) +{ + string normalized = normalize(value, norm_nfc); + free(str); + str = strdup(normalized.c_str()); + if (!str) + throw bad_alloc(); +} - void str_attr(char *&str, string value) - { - string normalized = normalize(value, norm_nfc); - free(str); - str = strdup(normalized.c_str()); - if (!str) - throw bad_alloc(); - } +time_t timestamp_attr(timestamp *&ts) +{ + if (!ts) + return 0; - time_t timestamp_attr(timestamp *&ts) - { - if (!ts) - return 0; + return timegm(ts); +} - return timegm(ts); - } +void timestamp_attr(timestamp *&ts, time_t value) +{ + free_timestamp(ts); + ts = new_timestamp(value); +} - void timestamp_attr(timestamp *&ts, time_t value) - { - free_timestamp(ts); - ts = new_timestamp(value); - } +boost::python::list strlist_attr(stringlist_t *&sl) +{ + boost::python::list result; - boost::python::list strlist_attr(stringlist_t *&sl) - { - boost::python::list result; + for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { + string s(_sl->value); + result.append(object(s)); + } - for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { - string s(_sl->value); - result.append(object(s)); - } + return result; +} - return result; +void strlist_attr(stringlist_t *&sl, boost::python::list value) +{ + stringlist_t *_sl = new_stringlist(NULL); + if (!_sl) + throw bad_alloc(); + + stringlist_t *_s = _sl; + for (int i=0; i extract_string(value[i]); + if (!extract_string.check()) { + free_stringlist(_sl); } - - void strlist_attr(stringlist_t *&sl, boost::python::list value) - { - stringlist_t *_sl = new_stringlist(NULL); - if (!_sl) - throw bad_alloc(); - - stringlist_t *_s = _sl; - for (int i=0; i extract_string(value[i]); - if (!extract_string.check()) { - free_stringlist(_sl); - } - string s = extract_string(); - s = normalize(s, norm_nfc); - _s = stringlist_add(_s, s.c_str()); - if (!_s) { - free_stringlist(_sl); - throw bad_alloc(); - } - } - - free_stringlist(sl); - sl = _sl; + string s = extract_string(); + s = normalize(s, norm_nfc); + _s = stringlist_add(_s, s.c_str()); + if (!_s) { + free_stringlist(_sl); + throw bad_alloc(); } + } - dict strdict_attr(stringpair_list_t *&spl) - { - dict result; + free_stringlist(sl); + sl = _sl; +} - for (stringpair_list_t *_spl = spl; _spl && _spl->value; _spl = - _spl->next) { - stringpair_t *p = _spl->value; - if (p->key && p->value) { - string key(p->key); - string value(p->value); +dict strdict_attr(stringpair_list_t *&spl) +{ + dict result; - result[key] = value; - } - } + for (stringpair_list_t *_spl = spl; _spl && _spl->value; _spl = + _spl->next) { + stringpair_t *p = _spl->value; + if (p->key && p->value) { + string key(p->key); + string value(p->value); - return result; + result[key] = value; } + } - void strdict_attr(stringpair_list_t *&spl, dict value) - { - stringpair_list_t *_spl = new_stringpair_list(NULL); - if (!_spl) - throw bad_alloc(); - - stringpair_list_t *_s = _spl; - for (int i=0; i extract_key(value.keys()[i]); - extract< string > extract_value(value.values()[i]); - - if (!(extract_key.check() && extract_value.check())) - free_stringpair_list(_spl); - - string key = extract_key(); - key = normalize(key, norm_nfc); - string _value = extract_value(); - _value = normalize(_value, norm_nfc); - stringpair_t *pair = new_stringpair(key.c_str(), _value.c_str()); - if (!pair) { - free_stringpair_list(_spl); - throw bad_alloc(); - } - _s = stringpair_list_add(_s, pair); - if (!_s) { - free_stringpair_list(_spl); - throw bad_alloc(); - } - } - - free_stringpair_list(spl); - spl = _spl; - } + return result; +} - stringlist_t *to_stringlist(boost::python::list l) - { - stringlist_t *result = new_stringlist(NULL); - if (!result) - throw bad_alloc(); - - stringlist_t *_s = result; - for (int i=0; i extract_string(l[i]); - if (!extract_string.check()) - free_stringlist(result); - string s = extract_string(); - _s = stringlist_add(_s, s.c_str()); - if (!_s) { - free_stringlist(result); - throw bad_alloc(); - } - } - - return result; +void strdict_attr(stringpair_list_t *&spl, dict value) +{ + stringpair_list_t *_spl = new_stringpair_list(NULL); + if (!_spl) + throw bad_alloc(); + + stringpair_list_t *_s = _spl; + for (int i=0; i extract_key(value.keys()[i]); + extract< string > extract_value(value.values()[i]); + + if (!(extract_key.check() && extract_value.check())) + free_stringpair_list(_spl); + + string key = extract_key(); + key = normalize(key, norm_nfc); + string _value = extract_value(); + _value = normalize(_value, norm_nfc); + stringpair_t *pair = new_stringpair(key.c_str(), _value.c_str()); + if (!pair) { + free_stringpair_list(_spl); + throw bad_alloc(); } + _s = stringpair_list_add(_s, pair); + if (!_s) { + free_stringpair_list(_spl); + throw bad_alloc(); + } + } - boost::python::list from_stringlist(const stringlist_t *sl) - { - boost::python::list result; - for (const stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { - string s = _sl->value; - result.append(s); - } - return result; + free_stringpair_list(spl); + spl = _spl; +} + +stringlist_t *to_stringlist(boost::python::list l) +{ + stringlist_t *result = new_stringlist(NULL); + if (!result) + throw bad_alloc(); + + stringlist_t *_s = result; + for (int i=0; i extract_string(l[i]); + if (!extract_string.check()) + free_stringlist(result); + string s = extract_string(); + _s = stringlist_add(_s, s.c_str()); + if (!_s) { + free_stringlist(result); + throw bad_alloc(); } } + + return result; } +boost::python::list from_stringlist(const stringlist_t *sl) +{ + boost::python::list result; + for (const stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { + string s = _sl->value; + result.append(s); + } + return result; +} + +} // namespace PythonAdapter +} // namespace pEp { + diff --git a/src/pEp/native_pEp/str_attr.hh b/src/pEp/native_pEp/str_attr.hh index 615312f..2a6dbf9 100644 --- a/src/pEp/native_pEp/str_attr.hh +++ b/src/pEp/native_pEp/str_attr.hh @@ -3,35 +3,39 @@ #pragma once +// System #include +// Engine #include #include #include #include namespace pEp { - namespace utility { - using namespace std; - using namespace boost::python; +namespace PythonAdapter { +using std::string; +using boost::python::object; +using boost::python::dict; - object repr(object s); - string repr(string s); +object repr(object s); +string repr(string s); - string str_attr(char *&str); - void str_attr(char *&str, string value); +string str_attr(char *&str); +void str_attr(char *&str, string value); - time_t timestamp_attr(timestamp *&ts); - void timestamp_attr(timestamp *&ts, time_t value); +time_t timestamp_attr(timestamp *&ts); +void timestamp_attr(timestamp *&ts, time_t value); - boost::python::list strlist_attr(stringlist_t *&sl); - void strlist_attr(stringlist_t *&sl, boost::python::list value); +boost::python::list strlist_attr(stringlist_t *&sl); +void strlist_attr(stringlist_t *&sl, boost::python::list value); - dict strdict_attr(stringpair_list_t *&spl); - void strdict_attr(stringpair_list_t *&spl, dict value); +dict strdict_attr(stringpair_list_t *&spl); +void strdict_attr(stringpair_list_t *&spl, dict value); - stringlist_t *to_stringlist(boost::python::list l); - boost::python::list from_stringlist(const stringlist_t *sl); - } -} +stringlist_t *to_stringlist(boost::python::list l); +boost::python::list from_stringlist(const stringlist_t *sl); + +} // namespace PythonAdapter +} // namespace pEp { diff --git a/src/pEp/native_pEp/user_interface.cc b/src/pEp/native_pEp/user_interface.cc index cb8a3e0..61b20af 100644 --- a/src/pEp/native_pEp/user_interface.cc +++ b/src/pEp/native_pEp/user_interface.cc @@ -1,124 +1,129 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt +// System #include + +// local #include "user_interface.hh" namespace pEp { - namespace PythonAdapter { - UserInterface *UserInterface::_ui = nullptr; - - UserInterface::UserInterface() - { - if (_ui) - throw runtime_error("only one UserInterface thread allowed"); - _ui = this; - } +namespace PythonAdapter { - UserInterface::~UserInterface() - { - _ui = nullptr; - } +UserInterface *UserInterface::_ui = nullptr; - UserInterface_callback::UserInterface_callback(PyObject *self) : - UserInterface(), _self(self) - { -// adapter.ui_object(self); - PEP_STATUS status = ::register_sync_callbacks(pEp::Adapter::session(), - (void *) this, _notifyHandshake, retrieve_next_sync_event); - assert(status == PEP_STATUS_OK); - if (status) - _throw_status(status); - } +UserInterface::UserInterface() +{ + if (_ui) + throw runtime_error("only one UserInterface thread allowed"); + _ui = this; +} - UserInterface_callback::~UserInterface_callback() - { - ::unregister_sync_callbacks(pEp::Adapter::session()); - } +UserInterface::~UserInterface() +{ + _ui = nullptr; +} - PEP_STATUS UserInterface::_notifyHandshake( - pEp_identity *me, pEp_identity *partner, - sync_handshake_signal signal - ) - { - if (!(me && partner)) - return PEP_ILLEGAL_VALUE; +UserInterface_callback::UserInterface_callback(PyObject *self) : + UserInterface(), _self(self) +{ +// adapter.ui_object(self); + PEP_STATUS status = ::register_sync_callbacks(pEp::Adapter::session(), + (void *) this, _notifyHandshake, retrieve_next_sync_event); + assert(status == PEP_STATUS_OK); + if (status) + _throw_status(status); +} - auto that = dynamic_cast< UserInterface_callback * >(_ui); - that->notifyHandshake(Identity(me), Identity(partner), signal); +UserInterface_callback::~UserInterface_callback() +{ + ::unregister_sync_callbacks(pEp::Adapter::session()); +} - return PEP_STATUS_OK; - } +PEP_STATUS UserInterface::_notifyHandshake( + pEp_identity *me, pEp_identity *partner, + sync_handshake_signal signal + ) +{ + if (!(me && partner)) + return PEP_ILLEGAL_VALUE; - void UserInterface::deliverHandshakeResult(int result, object identities) - { - identity_list *shared_identities = nullptr; - if (identities != boost::python::api::object() && - boost::python::len(identities)) { - shared_identities = new_identity_list(nullptr); - if (!shared_identities) - throw bad_alloc(); + auto that = dynamic_cast< UserInterface_callback * >(_ui); + that->notifyHandshake(Identity(me), Identity(partner), signal); - try { - identity_list *si = shared_identities; - for (int i=0; i < boost::python::len(identities); ++i) { - Identity ident = extract< Identity >(identities[i]); - si = identity_list_add(si, ident); - if (!si) - throw bad_alloc(); - } - } - catch (exception& ex) { - free_identity_list(shared_identities); - throw ex; - } - } + return PEP_STATUS_OK; +} + +void UserInterface::deliverHandshakeResult(int result, object identities) +{ + identity_list *shared_identities = nullptr; + if (identities != boost::python::api::object() && + boost::python::len(identities)) { + shared_identities = new_identity_list(nullptr); + if (!shared_identities) + throw bad_alloc(); - PEP_STATUS status = ::deliverHandshakeResult(pEp::Adapter::session(), - (sync_handshake_result) result, shared_identities); + try { + identity_list *si = shared_identities; + for (int i=0; i < boost::python::len(identities); ++i) { + Identity ident = extract< Identity >(identities[i]); + si = identity_list_add(si, ident); + if (!si) + throw bad_alloc(); + } + } + catch (exception& ex) { free_identity_list(shared_identities); - _throw_status(status); + throw ex; } + } - PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) - { - PEP_rating result; - PEP_STATUS status = - ::get_key_rating_for_user(pEp::Adapter::session(), - user_id.c_str(), fpr.c_str(), &result); - _throw_status(status); - return result; - } + PEP_STATUS status = ::deliverHandshakeResult(pEp::Adapter::session(), + (sync_handshake_result) result, shared_identities); + free_identity_list(shared_identities); + _throw_status(status); +} -// SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold) -// { -// time_t started = time(nullptr); -// bool timeout = false; +PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) +{ + PEP_rating result; + PEP_STATUS status = + ::get_key_rating_for_user(pEp::Adapter::session(), + user_id.c_str(), fpr.c_str(), &result); + _throw_status(status); + return result; +} + +//SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold) +//{ +// time_t started = time(nullptr); +// bool timeout = false; // -// while (adapter.queue().empty()) { -// int i = 0; -// ++i; -// if (i > 10) { -// if (time(nullptr) > started + threshold) { -// timeout = true; -// break; -// } -// i = 0; -// } -// nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); +// while (adapter.queue().empty()) { +// int i = 0; +// ++i; +// if (i > 10) { +// if (time(nullptr) > started + threshold) { +// timeout = true; +// break; // } +// i = 0; +// } +// nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); +// } // -// if (timeout) -// return new_sync_timeout_event(); +// if (timeout) +// return new_sync_timeout_event(); // -// return adapter.queue().pop_front(); -// } +// return adapter.queue().pop_front(); +//} - void UserInterface_callback::notifyHandshake( - Identity me, Identity partner, sync_handshake_signal signal) - { - call_method< void >(_self, "notifyHandshake", me, partner, signal); - } - } +void UserInterface_callback::notifyHandshake( + Identity me, Identity partner, sync_handshake_signal signal) +{ + call_method< void >(_self, "notifyHandshake", me, partner, signal); } +} // namespace PythonAdapter +} // namespace pEp { + diff --git a/src/pEp/native_pEp/user_interface.hh b/src/pEp/native_pEp/user_interface.hh index f9b5ace..c89c2a2 100644 --- a/src/pEp/native_pEp/user_interface.hh +++ b/src/pEp/native_pEp/user_interface.hh @@ -3,51 +3,58 @@ #pragma once +// System #include +// Engine #include #include +// local #include "pEpmodule.hh" namespace pEp { - namespace PythonAdapter { - class UserInterface { - static UserInterface *_ui; - public: - UserInterface(); - virtual ~UserInterface(); - - virtual void notifyHandshake( - pEp::PythonAdapter::Identity me, - pEp::PythonAdapter::Identity partner, - sync_handshake_signal signal) - { - throw runtime_error("override this method"); - } - - virtual void deliverHandshakeResult(int result, object identities); - - PEP_rating get_key_rating_for_user(string user_id, string fpr); - - protected: - static PEP_STATUS _notifyHandshake(pEp_identity *me, - pEp_identity *partner, sync_handshake_signal signal); - static SYNC_EVENT retrieve_next_sync_event(void *management, unsigned threshold); - }; - - class UserInterface_callback : public UserInterface { - PyObject *_self; - public: - UserInterface_callback(PyObject *self); - ~UserInterface_callback(); - - void notifyHandshake( - pEp::PythonAdapter::Identity me, - pEp::PythonAdapter::Identity partner, - sync_handshake_signal signal); - }; +namespace PythonAdapter { + +class UserInterface { +private: + static UserInterface *_ui; +public: + UserInterface(); + virtual ~UserInterface(); + + virtual void notifyHandshake( + Identity me, + Identity partner, + sync_handshake_signal signal) + { + throw runtime_error("override this method"); } -} + + virtual void deliverHandshakeResult(int result, object identities); + + PEP_rating get_key_rating_for_user(string user_id, string fpr); + +protected: + static PEP_STATUS _notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal); + static SYNC_EVENT retrieve_next_sync_event(void *management, unsigned threshold); +}; + +class UserInterface_callback : public UserInterface { +private: + PyObject *_self; +public: + UserInterface_callback(PyObject *self); + ~UserInterface_callback(); + + void notifyHandshake( + Identity me, + Identity partner, + sync_handshake_signal signal + ); +}; + +} // namespace PythonAdapter +} // namespace pEp { diff --git a/test/pyadpt-81.py b/test/pyadpt-81.py index c78cc10..6ff7021 100644 --- a/test/pyadpt-81.py +++ b/test/pyadpt-81.py @@ -3,4 +3,5 @@ import pEp +dir(pEp) pEp.is_sync_active() \ No newline at end of file From 3d42d9a7d5ffa29e24c7db16b172eeccc6168b03 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 19 Aug 2020 16:24:24 +0200 Subject: [PATCH 04/13] remove unnecessary namespace identifier pEp:: --- setup.py | 3 +- src/pEp/__init__.py | 3 +- src/pEp/native_pEp/basic_api.cc | 22 ++++----- src/pEp/native_pEp/identity.cc | 16 +++---- src/pEp/native_pEp/message.cc | 8 ++-- src/pEp/native_pEp/message_api.cc | 4 +- src/pEp/native_pEp/pEpmodule.cc | 67 ++++++++++++++-------------- src/pEp/native_pEp/user_interface.cc | 18 ++++---- src/pEp/native_pEp/user_interface.hh | 2 - 9 files changed, 72 insertions(+), 71 deletions(-) diff --git a/setup.py b/setup.py index 7268c4e..e62010f 100644 --- a/setup.py +++ b/setup.py @@ -168,7 +168,6 @@ class BuildExtCommand(build_ext): # Append prefix-dir if self.prefix: - pEpLog("using prefix=",self.prefix) prefix_include=[ join(self.prefix, 'include') ] prefix_libdirs=[ join(self.prefix, 'lib') ] includes += prefix_include @@ -219,7 +218,7 @@ module_pEp = Extension( 'src/pEp/native_pEp/message.cc', 'src/pEp/native_pEp/message_api.cc', 'src/pEp/native_pEp/str_attr.cc', - # 'src/user_interface.cc', + 'src/pEp/native_pEp/user_interface.cc', # 'src/adapter.cc' ], ) diff --git a/src/pEp/__init__.py b/src/pEp/__init__.py index aa7d9ef..82b5f39 100644 --- a/src/pEp/__init__.py +++ b/src/pEp/__init__.py @@ -6,7 +6,8 @@ # Import all symbols EXCEPT the ones beginning with underscore into the current namespace from native_pEp import * - +# TODO: inter-pkg ref to make sure which native_pEp in sys.path gets loaded +# like: pEp.native_pEp # import the module import native_pEp diff --git a/src/pEp/native_pEp/basic_api.cc b/src/pEp/native_pEp/basic_api.cc index c389905..c3e1097 100644 --- a/src/pEp/native_pEp/basic_api.cc +++ b/src/pEp/native_pEp/basic_api.cc @@ -24,7 +24,7 @@ void update_identity(Identity& ident) throw runtime_error("update_identity: '" PEP_OWN_USERID "' may only be used for own identities"); - PEP_STATUS status = update_identity(pEp::Adapter::session(), ident); + PEP_STATUS status = update_identity(Adapter::session(), ident); _throw_status(status); } @@ -38,7 +38,7 @@ void myself(Identity& ident) if (ident.user_id() == "") ident.user_id(ident.address()); - PEP_STATUS status = myself(pEp::Adapter::session(), ident); + PEP_STATUS status = myself(Adapter::session(), ident); _throw_status(status); } @@ -52,7 +52,7 @@ string _trustwords(Identity me, Identity partner, string lang, bool full) char *words = NULL; size_t size = 0; - PEP_STATUS status = get_trustwords(pEp::Adapter::session(), me, partner, + PEP_STATUS status = get_trustwords(Adapter::session(), me, partner, lang.c_str(),&words, &size, full); _throw_status(status); return words; @@ -65,7 +65,7 @@ void trust_personal_key(Identity ident) if (ident.user_id() == "") throw invalid_argument("user_id must be provided"); - PEP_STATUS status = trust_personal_key(pEp::Adapter::session(), ident); + PEP_STATUS status = trust_personal_key(Adapter::session(), ident); _throw_status(status); } @@ -76,7 +76,7 @@ void set_identity_flags(Identity ident, identity_flags_t flags) if (ident.user_id() == "") throw invalid_argument("user_id needed"); - PEP_STATUS status = set_identity_flags(pEp::Adapter::session(), ident, flags); + PEP_STATUS status = set_identity_flags(Adapter::session(), ident, flags); _throw_status(status); } @@ -87,7 +87,7 @@ void unset_identity_flags(Identity ident, identity_flags_t flags) if (ident.user_id() == "") throw invalid_argument("user_id needed"); - PEP_STATUS status = unset_identity_flags(pEp::Adapter::session(), ident, flags); + PEP_STATUS status = unset_identity_flags(Adapter::session(), ident, flags); _throw_status(status); } @@ -100,7 +100,7 @@ void key_reset_trust(Identity ident) if (ident.user_id() == "") throw invalid_argument("user_id needed"); - PEP_STATUS status = key_reset_trust(pEp::Adapter::session(), ident); + PEP_STATUS status = key_reset_trust(Adapter::session(), ident); _throw_status(status); } @@ -109,7 +109,7 @@ void key_reset_trust(Identity ident) boost::python::list import_key(string key_data) { ::identity_list *private_keys = NULL; - PEP_STATUS status = ::import_key(pEp::Adapter::session(), key_data.c_str(), key_data.size(), &private_keys); + PEP_STATUS status = ::import_key(Adapter::session(), key_data.c_str(), key_data.size(), &private_keys); if (status && status != PEP_KEY_IMPORTED) _throw_status(status); @@ -132,7 +132,7 @@ string export_key(Identity ident) PEP_STATUS status = PEP_STATUS_OK; char* key_data = NULL; size_t size; - status = ::export_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); + status = ::export_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size); _throw_status(status); return key_data; @@ -143,7 +143,7 @@ string export_secret_key(Identity ident) PEP_STATUS status = PEP_STATUS_OK; char* key_data = NULL; size_t size; - status = ::export_secret_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); + status = ::export_secret_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size); _throw_status(status); return key_data; @@ -162,7 +162,7 @@ void set_own_key(Identity& ident, string fpr) const char* fpr_c = fpr.c_str(); - PEP_STATUS status = set_own_key(pEp::Adapter::session(), ident, fpr_c); + PEP_STATUS status = set_own_key(Adapter::session(), ident, fpr_c); _throw_status(status); } diff --git a/src/pEp/native_pEp/identity.cc b/src/pEp/native_pEp/identity.cc index 9b414f8..6849741 100644 --- a/src/pEp/native_pEp/identity.cc +++ b/src/pEp/native_pEp/identity.cc @@ -124,7 +124,7 @@ int Identity::rating() throw invalid_argument("address must be given"); PEP_rating rating = PEP_rating_undefined; - PEP_STATUS status = ::identity_rating(pEp::Adapter::session(), _ident.get(), &rating); + PEP_STATUS status = ::identity_rating(Adapter::session(), _ident.get(), &rating); _throw_status(status); return (int) rating; @@ -156,34 +156,34 @@ void Identity::update() void Identity::key_reset(string fpr) { - PEP_STATUS status = ::key_reset_identity(pEp::Adapter::session(), *this, + PEP_STATUS status = ::key_reset_identity(Adapter::session(), *this, fpr != "" ? fpr.c_str() : nullptr); _throw_status(status); } void Identity::key_mistrusted() { - PEP_STATUS status = ::key_mistrusted(pEp::Adapter::session(), *this); + PEP_STATUS status = ::key_mistrusted(Adapter::session(), *this); _throw_status(status); } bool Identity::is_pEp_user() { bool result; - PEP_STATUS status = ::is_pEp_user(pEp::Adapter::session(), *this, &result); + PEP_STATUS status = ::is_pEp_user(Adapter::session(), *this, &result); _throw_status(status); return result; } void Identity::enable_for_sync() { - PEP_STATUS status = ::enable_identity_for_sync(pEp::Adapter::session(), *this); + PEP_STATUS status = ::enable_identity_for_sync(Adapter::session(), *this); _throw_status(status); } void Identity::disable_for_sync() { - PEP_STATUS status = ::disable_identity_for_sync(pEp::Adapter::session(), *this); + PEP_STATUS status = ::disable_identity_for_sync(Adapter::session(), *this); _throw_status(status); } @@ -226,7 +226,7 @@ void identity_attr(pEp_identity *&ident, object value) pEp_identity *_dup = ::identity_dup(_ident); if (!_dup) throw bad_alloc(); - PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); + PEP_STATUS status = update_identity(Adapter::session(), _dup); _throw_status(status); free_identity(ident); ident = _dup; @@ -264,7 +264,7 @@ void identitylist_attr(identity_list *&il, boost::python::list value) free_identity_list(_il); throw bad_alloc(); } - PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); + PEP_STATUS status = update_identity(Adapter::session(), _dup); if (status != PEP_STATUS_OK) { free_identity_list(_il); _throw_status(status); diff --git a/src/pEp/native_pEp/message.cc b/src/pEp/native_pEp/message.cc index 08ba766..3d0a279 100644 --- a/src/pEp/native_pEp/message.cc +++ b/src/pEp/native_pEp/message.cc @@ -332,11 +332,11 @@ PEP_rating Message::outgoing_rating() if (len(to()) + len(cc()) == 0) throw invalid_argument("either to or cc needed"); - PEP_STATUS status = myself(pEp::Adapter::session(), _msg->from); + PEP_STATUS status = myself(Adapter::session(), _msg->from); _throw_status(status); PEP_rating rating = PEP_rating_undefined; - status = outgoing_message_rating(pEp::Adapter::session(), *this, &rating); + status = outgoing_message_rating(Adapter::session(), *this, &rating); _throw_status(status); return rating; @@ -365,7 +365,7 @@ Message outgoing_message(Identity me) if (me.address().empty() || me.user_id().empty()) throw runtime_error("at least address and user_id of own user needed"); - ::myself(pEp::Adapter::session(), me); + ::myself(Adapter::session(), me); auto m = Message(PEP_dir_outgoing, &me); return m; } @@ -374,7 +374,7 @@ static object update(Identity ident) { if (ident.address().empty()) throw runtime_error("at least address needed"); - update_identity(pEp::Adapter::session(), ident); + update_identity(Adapter::session(), ident); return object(ident); } diff --git a/src/pEp/native_pEp/message_api.cc b/src/pEp/native_pEp/message_api.cc index da8a199..41a49e0 100644 --- a/src/pEp/native_pEp/message_api.cc +++ b/src/pEp/native_pEp/message_api.cc @@ -35,7 +35,7 @@ Message encrypt_message(Message src, boost::python::list extra, int enc_format, message *_dst = NULL; message *_src = src; - PEP_STATUS status = encrypt_message(pEp::Adapter::session(), _src, _extra, &_dst, + PEP_STATUS status = encrypt_message(Adapter::session(), _src, _extra, &_dst, _enc_format, _flags); free_stringlist(_extra); _throw_status(status); @@ -54,7 +54,7 @@ boost::python::tuple decrypt_message(Message src, int flags) PEP_decrypt_flags_t _flags = (PEP_decrypt_flags_t) flags; message *_src = src; - PEP_STATUS status = ::decrypt_message(pEp::Adapter::session(), _src, &_dst, &_keylist, + PEP_STATUS status = ::decrypt_message(Adapter::session(), _src, &_dst, &_keylist, &_rating, &_flags); _throw_status(status); diff --git a/src/pEp/native_pEp/pEpmodule.cc b/src/pEp/native_pEp/pEpmodule.cc index 1dfbb87..fbd514f 100644 --- a/src/pEp/native_pEp/pEpmodule.cc +++ b/src/pEp/native_pEp/pEpmodule.cc @@ -37,16 +37,17 @@ static const char *version_string = "p≡p Python adapter version 0.3"; void _init() { pEpLog("called"); callback_dispatcher.add(_messageToSend, nullptr, nullptr, nullptr); + Adapter::_messageToSend = CallbackDispatcher::messageToSend; } void config_passive_mode(bool enable) { - ::config_passive_mode(pEp::Adapter::session(), enable); + ::config_passive_mode(Adapter::session(), enable); } void config_unencrypted_subject(bool enable) { - ::config_unencrypted_subject(pEp::Adapter::session(), enable); + ::config_unencrypted_subject(Adapter::session(), enable); } void key_reset_user(string user_id, string fpr) @@ -54,7 +55,7 @@ void key_reset_user(string user_id, string fpr) if (user_id == "") throw invalid_argument("user_id required"); - PEP_STATUS status = ::key_reset_user(pEp::Adapter::session(), + PEP_STATUS status = ::key_reset_user(Adapter::session(), user_id.c_str(), fpr != "" ? fpr.c_str() : nullptr); _throw_status(status); } @@ -66,7 +67,7 @@ void key_reset_user2(string user_id) void key_reset_all_own_keys() { - PEP_STATUS status = ::key_reset_all_own_keys(pEp::Adapter::session()); + PEP_STATUS status = ::key_reset_all_own_keys(Adapter::session()); _throw_status(status); } @@ -123,22 +124,22 @@ void messageToSend(Message msg) { // void do_sync_protocol() // { -// ::do_sync_protocol(pEp::Adapter::session(), nullptr); +// ::do_sync_protocol(Adapter::session(), nullptr); // } void shutdown_sync() { - pEp::Adapter::shutdown(); + Adapter::shutdown(); } void debug_color(int ansi_color) { - ::set_debug_color(pEp::Adapter::session(), ansi_color); + ::set_debug_color(Adapter::session(), ansi_color); } void leave_device_group() { - ::leave_device_group(pEp::Adapter::session()); + ::leave_device_group(Adapter::session()); } // void script_is_implementing_sync() { @@ -147,7 +148,7 @@ void leave_device_group() bool is_sync_active() { - return pEp::Adapter::is_sync_running(); + return Adapter::is_sync_running(); } BOOST_PYTHON_MODULE(native_pEp) @@ -565,30 +566,30 @@ BOOST_PYTHON_MODULE(native_pEp) .value("SYNC_NOTIFY_SOLE" , SYNC_NOTIFY_SOLE) .value("SYNC_NOTIFY_IN_GROUP" , SYNC_NOTIFY_IN_GROUP); -// auto user_interface_class = class_( -// "UserInterface", -// "class MyUserInterface(UserInterface):\n" -// " def notifyHandshake(self, me, partner):\n" -// " ...\n" -// "\n" -// "p≡p User Interface class\n" -// "To be used as a mixin\n" -// ) -// .def("notifyHandshake", &UserInterface::notifyHandshake, -// "notifyHandshake(self, me, partner)\n" -// "\n" -// " me own identity\n" -// " partner identity of communication partner\n" -// "\n" -// "overwrite this method with an implementation of a handshake dialog") -// .def("deliverHandshakeResult", &UserInterface::deliverHandshakeResult, -// boost::python::arg("identities")=object(), -// "deliverHandshakeResult(self, result, identities=None)\n" -// "\n" -// " result -1: cancel, 0: accepted, 1: rejected\n" -// " identities list of identities to share or None for all\n" -// "\n" -// "call to deliver the handshake result of the handshake dialog"); + auto user_interface_class = class_( + "UserInterface", + "class MyUserInterface(UserInterface):\n" + " def notifyHandshake(self, me, partner):\n" + " ...\n" + "\n" + "p≡p User Interface class\n" + "To be used as a mixin\n" + ) + .def("notifyHandshake", &UserInterface::notifyHandshake, + "notifyHandshake(self, me, partner)\n" + "\n" + " me own identity\n" + " partner identity of communication partner\n" + "\n" + "overwrite this method with an implementation of a handshake dialog") + .def("deliverHandshakeResult", &UserInterface::deliverHandshakeResult, + boost::python::arg("identities")=object(), + "deliverHandshakeResult(self, result, identities=None)\n" + "\n" + " result -1: cancel, 0: accepted, 1: rejected\n" + " identities list of identities to share or None for all\n" + "\n" + "call to deliver the handshake result of the handshake dialog"); // TODO: Replace with start_sync() diff --git a/src/pEp/native_pEp/user_interface.cc b/src/pEp/native_pEp/user_interface.cc index 61b20af..0cf5c39 100644 --- a/src/pEp/native_pEp/user_interface.cc +++ b/src/pEp/native_pEp/user_interface.cc @@ -9,6 +9,8 @@ namespace pEp { namespace PythonAdapter { +using namespace std; +using namespace boost::python; UserInterface *UserInterface::_ui = nullptr; @@ -28,16 +30,16 @@ UserInterface_callback::UserInterface_callback(PyObject *self) : UserInterface(), _self(self) { // adapter.ui_object(self); - PEP_STATUS status = ::register_sync_callbacks(pEp::Adapter::session(), - (void *) this, _notifyHandshake, retrieve_next_sync_event); - assert(status == PEP_STATUS_OK); - if (status) - _throw_status(status); +// PEP_STATUS status = ::register_sync_callbacks(Adapter::session(), +// (void *) this, _notifyHandshake, retrieve_next_sync_event); +// assert(status == PEP_STATUS_OK); +// if (status) +// _throw_status(status); } UserInterface_callback::~UserInterface_callback() { - ::unregister_sync_callbacks(pEp::Adapter::session()); +// ::unregister_sync_callbacks(Adapter::session()); } PEP_STATUS UserInterface::_notifyHandshake( @@ -78,7 +80,7 @@ void UserInterface::deliverHandshakeResult(int result, object identities) } } - PEP_STATUS status = ::deliverHandshakeResult(pEp::Adapter::session(), + PEP_STATUS status = ::deliverHandshakeResult(Adapter::session(), (sync_handshake_result) result, shared_identities); free_identity_list(shared_identities); _throw_status(status); @@ -88,7 +90,7 @@ PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) { PEP_rating result; PEP_STATUS status = - ::get_key_rating_for_user(pEp::Adapter::session(), + ::get_key_rating_for_user(Adapter::session(), user_id.c_str(), fpr.c_str(), &result); _throw_status(status); return result; diff --git a/src/pEp/native_pEp/user_interface.hh b/src/pEp/native_pEp/user_interface.hh index c89c2a2..7fa358d 100644 --- a/src/pEp/native_pEp/user_interface.hh +++ b/src/pEp/native_pEp/user_interface.hh @@ -18,7 +18,6 @@ namespace pEp { namespace PythonAdapter { class UserInterface { -private: static UserInterface *_ui; public: UserInterface(); @@ -42,7 +41,6 @@ protected: }; class UserInterface_callback : public UserInterface { -private: PyObject *_self; public: UserInterface_callback(PyObject *self); From 24e13adfe1b94b4e786d52adc793906bb8fb1649 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 20 Aug 2020 14:32:58 +0200 Subject: [PATCH 05/13] sync/message_to_send() callback impl in python and using libpEpAdapter / add pEp.start_sync() --- src/pEp/__init__.py | 17 +++- src/pEp/native_pEp/adapter.cc | 2 +- src/pEp/native_pEp/pEpmodule.cc | 141 ++++++++++++--------------- src/pEp/native_pEp/pEpmodule.hh | 5 +- src/pEp/native_pEp/user_interface.cc | 18 ++-- src/pEp/native_pEp/user_interface.hh | 3 +- 6 files changed, 87 insertions(+), 99 deletions(-) diff --git a/src/pEp/__init__.py b/src/pEp/__init__.py index 82b5f39..2cfd9cc 100644 --- a/src/pEp/__init__.py +++ b/src/pEp/__init__.py @@ -11,15 +11,22 @@ from native_pEp import * # import the module import native_pEp +# Executed on module import +def init(): + print(init, "called") + native_pEp._init_after_main_module() +def message_to_send(msg): + """ + message_to_send(msg) + override pEp.message_to_send(msg) with your own implementation + this callback is being called when a p≡p management message needs to be sent + """ + print("message_to_send() - default callback\n") + print("overwrite this method") -# Executed on module import -def init(): - print("init() called") - native_pEp._init() - # Executed when run as script def main(): print("I am being run as a script") diff --git a/src/pEp/native_pEp/adapter.cc b/src/pEp/native_pEp/adapter.cc index 008644b..6bc1c4f 100644 --- a/src/pEp/native_pEp/adapter.cc +++ b/src/pEp/native_pEp/adapter.cc @@ -43,7 +43,7 @@ namespace pEp { case init: ++booked; if (!_session) - status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase); +// status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase); // status = ::init(&_session, _messageToSend, _inject_sync_event); break; diff --git a/src/pEp/native_pEp/pEpmodule.cc b/src/pEp/native_pEp/pEpmodule.cc index fbd514f..d77dc34 100644 --- a/src/pEp/native_pEp/pEpmodule.cc +++ b/src/pEp/native_pEp/pEpmodule.cc @@ -31,15 +31,20 @@ namespace PythonAdapter { using namespace std; using namespace boost::python; -scope *_scope = NULL; static const char *version_string = "p≡p Python adapter version 0.3"; -void _init() { +void init_before_main_module() { pEpLog("called"); - callback_dispatcher.add(_messageToSend, nullptr, nullptr, nullptr); +} + +// hidden init function, wrapped by hello_world.init() +void _init_after_main_module() { + pEpLog("called"); + callback_dispatcher.add(_messageToSend, notifyHandshake, nullptr, nullptr); Adapter::_messageToSend = CallbackDispatcher::messageToSend; } + void config_passive_mode(bool enable) { ::config_passive_mode(Adapter::session(), enable); @@ -99,37 +104,33 @@ void _throw_status(PEP_STATUS status) } } -PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr) -{ - return PEP_STATUS_OK; -} - PEP_STATUS _messageToSend(::message *msg) { - if (!_scope) - return PEP_SEND_FUNCTION_NOT_REGISTERED; - + pEpLog("called"); try { - object m = _scope->attr("messageToSend"); - call< void >(m.ptr(), Message(msg)); - } - catch (exception& e) { } + object modref = import("pEp"); + object funcref = modref.attr("message_to_send"); + call(funcref.ptr(), Message(msg)); + } catch (exception& e) { } return PEP_STATUS_OK; } -void messageToSend(Message msg) { - throw runtime_error("implement pEp.messageToSend(msg)"); +PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) +{ + pEpLog("called"); + return PEP_STATUS_OK; } -// void do_sync_protocol() -// { -// ::do_sync_protocol(Adapter::session(), nullptr); -// } + +void start_sync() +{ + CallbackDispatcher::start_sync(); +} void shutdown_sync() { - Adapter::shutdown(); + CallbackDispatcher::stop_sync(); } void debug_color(int ansi_color) @@ -142,28 +143,28 @@ void leave_device_group() ::leave_device_group(Adapter::session()); } -// void script_is_implementing_sync() { -// adapter.script_is_implementing_sync(); -// } - bool is_sync_active() { return Adapter::is_sync_running(); } +void testfunc() { + _messageToSend(NULL); +} + BOOST_PYTHON_MODULE(native_pEp) { + init_before_main_module(); - docstring_options doc_options(true, false, false); + // Module init function called by pEp.init() + def("_init_after_main_module", _init_after_main_module); + def("testfunc", &testfunc); + docstring_options doc_options(true, false, false); boost::locale::generator gen; std::locale::global(gen("")); - // Module init function called by pEp.init() - def("_init", _init); - - - _scope = new scope(); +// _scope = new scope(); scope().attr("about") = about(); scope().attr("per_user_directory") = per_user_directory(); scope().attr("per_machine_directory") = per_machine_directory(); @@ -544,14 +545,6 @@ BOOST_PYTHON_MODULE(native_pEp) "\n" "calculate trustwords for two Identities"); - // messageToSend() - - def("messageToSend", &messageToSend, - "messageToSend(msg)\n" - "\n" - "override pEp.messageToSend(msg) with your own implementation\n" - "this callback is being called when a p≡p management message needs to be sent"); - // Sync API enum_("sync_handshake_signal") @@ -566,39 +559,38 @@ BOOST_PYTHON_MODULE(native_pEp) .value("SYNC_NOTIFY_SOLE" , SYNC_NOTIFY_SOLE) .value("SYNC_NOTIFY_IN_GROUP" , SYNC_NOTIFY_IN_GROUP); - auto user_interface_class = class_( - "UserInterface", - "class MyUserInterface(UserInterface):\n" - " def notifyHandshake(self, me, partner):\n" - " ...\n" - "\n" - "p≡p User Interface class\n" - "To be used as a mixin\n" - ) - .def("notifyHandshake", &UserInterface::notifyHandshake, - "notifyHandshake(self, me, partner)\n" - "\n" - " me own identity\n" - " partner identity of communication partner\n" - "\n" - "overwrite this method with an implementation of a handshake dialog") - .def("deliverHandshakeResult", &UserInterface::deliverHandshakeResult, - boost::python::arg("identities")=object(), - "deliverHandshakeResult(self, result, identities=None)\n" - "\n" - " result -1: cancel, 0: accepted, 1: rejected\n" - " identities list of identities to share or None for all\n" - "\n" - "call to deliver the handshake result of the handshake dialog"); +// auto user_interface_class = class_( +// "UserInterface", +// "class MyUserInterface(UserInterface):\n" +// " def notifyHandshake(self, me, partner):\n" +// " ...\n" +// "\n" +// "p≡p User Interface class\n" +// "To be used as a mixin\n" +// ) +// .def("notifyHandshake", &UserInterface::notifyHandshake, +// "notifyHandshake(self, me, partner)\n" +// "\n" +// " me own identity\n" +// " partner identity of communication partner\n" +// "\n" +// "overwrite this method with an implementation of a handshake dialog") +// .def("deliverHandshakeResult", &UserInterface::deliverHandshakeResult, +// boost::python::arg("identities")=object(), +// "deliverHandshakeResult(self, result, identities=None)\n" +// "\n" +// " result -1: cancel, 0: accepted, 1: rejected\n" +// " identities list of identities to share or None for all\n" +// "\n" +// "call to deliver the handshake result of the handshake dialog" +// ); -// TODO: Replace with start_sync() -// def("do_sync_protocol", &do_sync_protocol, -// "do_sync_protocol()\n" -// "\n" -// "in case of an explicit sync thread instead of a single threaded\n" -// "implementation call this function in your sync thread\n" -// ); + def("start_sync", &start_sync, + "start_sync()\n" + "\n" + "starts the sync thread" + ); def("shutdown_sync", &shutdown_sync, "shutdown_sync()\n" @@ -615,13 +607,6 @@ BOOST_PYTHON_MODULE(native_pEp) "call this for a grouped device, which should leave\n" ); -// def("script_is_implementing_sync", &script_is_implementing_sync, -// "script_is_implementing_sync()\n" -// "\n" -// "call this in case the Python script is implementing sync to make\n" -// "is_sync_active() working\n" -// ); - def("is_sync_active", &is_sync_active, "is_sync_active()\n" "\n" diff --git a/src/pEp/native_pEp/pEpmodule.hh b/src/pEp/native_pEp/pEpmodule.hh index 71ff9c5..4bfc6fa 100644 --- a/src/pEp/native_pEp/pEpmodule.hh +++ b/src/pEp/native_pEp/pEpmodule.hh @@ -15,11 +15,8 @@ void config_unencrypted_subject(bool enable); void key_reset_user(string user_id, string fpr); void key_reset_all_own_keys(); void _throw_status(PEP_STATUS status); -void messageToSend(Message msg); PEP_STATUS _messageToSend(::message *msg); -PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr); -// void do_sync_protocol(); -// extern Adapter adapter; +PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal); } // namespace PythonAdapter } // namespace pEp { diff --git a/src/pEp/native_pEp/user_interface.cc b/src/pEp/native_pEp/user_interface.cc index 0cf5c39..a21234d 100644 --- a/src/pEp/native_pEp/user_interface.cc +++ b/src/pEp/native_pEp/user_interface.cc @@ -86,15 +86,15 @@ void UserInterface::deliverHandshakeResult(int result, object identities) _throw_status(status); } -PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) -{ - PEP_rating result; - PEP_STATUS status = - ::get_key_rating_for_user(Adapter::session(), - user_id.c_str(), fpr.c_str(), &result); - _throw_status(status); - return result; -} +//PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) +//{ +// PEP_rating result; +// PEP_STATUS status = +// ::get_key_rating_for_user(Adapter::session(), +// user_id.c_str(), fpr.c_str(), &result); +// _throw_status(status); +// return result; +//} //SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold) //{ diff --git a/src/pEp/native_pEp/user_interface.hh b/src/pEp/native_pEp/user_interface.hh index 7fa358d..e222b3c 100644 --- a/src/pEp/native_pEp/user_interface.hh +++ b/src/pEp/native_pEp/user_interface.hh @@ -33,11 +33,10 @@ public: virtual void deliverHandshakeResult(int result, object identities); - PEP_rating get_key_rating_for_user(string user_id, string fpr); +// PEP_rating get_key_rating_for_user(string user_id, string fpr); protected: static PEP_STATUS _notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal); - static SYNC_EVENT retrieve_next_sync_event(void *management, unsigned threshold); }; class UserInterface_callback : public UserInterface { From c18746e4ecb97a49b495f73083ca10fb06b4c4d7 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 20 Aug 2020 14:34:52 +0200 Subject: [PATCH 06/13] remove local adapter.cc/hh locked_queue.hh --- setup.py | 1 - src/pEp/native_pEp/adapter.cc | 99 ------------------------------ src/pEp/native_pEp/adapter.hh | 59 ------------------ src/pEp/native_pEp/locked_queue.hh | 61 ------------------ 4 files changed, 220 deletions(-) delete mode 100644 src/pEp/native_pEp/adapter.cc delete mode 100644 src/pEp/native_pEp/adapter.hh delete mode 100644 src/pEp/native_pEp/locked_queue.hh diff --git a/setup.py b/setup.py index e62010f..445936c 100644 --- a/setup.py +++ b/setup.py @@ -219,7 +219,6 @@ module_pEp = Extension( 'src/pEp/native_pEp/message_api.cc', 'src/pEp/native_pEp/str_attr.cc', 'src/pEp/native_pEp/user_interface.cc', - # 'src/adapter.cc' ], ) diff --git a/src/pEp/native_pEp/adapter.cc b/src/pEp/native_pEp/adapter.cc deleted file mode 100644 index 6bc1c4f..0000000 --- a/src/pEp/native_pEp/adapter.cc +++ /dev/null @@ -1,99 +0,0 @@ -// This file is under GNU Affero General Public License 3.0 -// see LICENSE.txt - -#include "user_interface.hh" -#include -#include "adapter.hh" - -namespace pEp { - namespace PythonAdapter { - Adapter::Adapter(bool unregister_this) - : flag_unregister(unregister_this) - { - session(init); - } - - Adapter::~Adapter() - { - session(release); - } - - PEP_SESSION Adapter::session(session_action action) - { - lock_guard lock(mtx()); - - thread_local static PEP_SESSION _session = nullptr; - thread_local int booked = 0; - PEP_STATUS status = PEP_STATUS_OK; - - switch (action) { - case release: - if (booked) - --booked; - if (!booked && _session) { - ::release(_session); - _session = nullptr; - } - break; - - case none: - if (_session) - break; - - case init: - ++booked; - if (!_session) -// status = ::init(&_session, _messageToSend, _inject_sync_event, _ensure_passphrase); -// status = ::init(&_session, _messageToSend, _inject_sync_event); - break; - - default: - status = PEP_ILLEGAL_VALUE; - } - - if (status) - _throw_status(status); - - return _session; - } - - ::utility::locked_queue< SYNC_EVENT > * Adapter::q = nullptr; - bool Adapter::flag_sync_enabled = false; - - void Adapter::shutdown_sync() - { - if (queue_active()) - queue().push_front(nullptr); - flag_sync_enabled = false; - } - - PyObject *Adapter::ui_object(PyObject *value) - { - lock_guard lock(mtx()); - static PyObject *obj = nullptr; - if (value) - obj = value; - return obj; - } - -// int Adapter::_inject_sync_event(SYNC_EVENT ev, void *management) -// { -// if (!flag_sync_enabled) -// return 1; -// -// if (is_sync_thread(adapter.session())) { -// PEP_STATUS status = do_sync_protocol_step(adapter.session(), adapter.ui_object(), ev); -// return status == PEP_STATUS_OK ? 0 : 1; -// } -// -// try { -// queue().push_back(ev); -// } -// catch (exception&) { -// return 1; -// } -// return 0; -// } - } -} - diff --git a/src/pEp/native_pEp/adapter.hh b/src/pEp/native_pEp/adapter.hh deleted file mode 100644 index 634e65c..0000000 --- a/src/pEp/native_pEp/adapter.hh +++ /dev/null @@ -1,59 +0,0 @@ -// This file is under GNU Affero General Public License 3.0 -// see LICENSE.txt - -#pragma once - -#include "pEpmodule.hh" -#include "pEp/locked_queue.hh" -#include "user_interface.hh" -#include - -namespace pEp { - namespace PythonAdapter { - using Message = pEp::PythonAdapter::Message; - - class Adapter { - bool flag_unregister; - - public: - Adapter(bool unregister_this = false); - virtual ~Adapter(); - - enum session_action { - none = 0, - init, - release - }; - - PEP_SESSION session(session_action action = none); - static ::utility::locked_queue< SYNC_EVENT >& queue() - { - if (!q) - q = new ::utility::locked_queue< SYNC_EVENT >(); - return *q; - } - void script_is_implementing_sync() { flag_sync_enabled = true; } - void shutdown_sync(); - bool is_sync_active() { return flag_sync_enabled; } - - protected: - static PyObject *ui_object(PyObject *value = nullptr); - static int _inject_sync_event(SYNC_EVENT ev, void *management); - - static ::utility::locked_queue< SYNC_EVENT > *q; - static bool flag_sync_enabled; - - bool queue_active() { return !!q; } - - private: - static mutex& mtx() - { - static mutex m; - return m; - } - - friend class UserInterface_callback; - }; - } -} - diff --git a/src/pEp/native_pEp/locked_queue.hh b/src/pEp/native_pEp/locked_queue.hh deleted file mode 100644 index 88c7430..0000000 --- a/src/pEp/native_pEp/locked_queue.hh +++ /dev/null @@ -1,61 +0,0 @@ -#pragma once - -#include -#include - -namespace utility -{ - using namespace std; - - template class locked_queue - { - mutex _mtx; - list _q; - - public: - T& back() - { - lock_guard lg(_mtx); - return _q.back(); - } - T& front() - { - lock_guard lg(_mtx); - return _q.front(); - } - T pop_back() - { - lock_guard lg(_mtx); - T r = _q.back(); - _q.pop_back(); - return r; - } - T pop_front() - { - lock_guard lg(_mtx); - T r = _q.front(); - _q.pop_front(); - return r; - } - void push_back(const T& data) - { - lock_guard lg(_mtx); - _q.push_back(data); - } - void push_front(const T& data) - { - lock_guard lg(_mtx); - _q.push_front(data); - } - size_t size() - { - lock_guard lg(_mtx); - return _q.size(); - } - bool empty() - { - lock_guard lg(_mtx); - return _q.empty(); - } - }; -}; From 74dcc6f7a7e9682ecb0ee14c5ef3146829e08373 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 20 Aug 2020 14:36:37 +0200 Subject: [PATCH 07/13] test pyadpt-81.py: test start/stop sync, default/user callback impl. of pEp.message_to_send() --- test/pyadpt-81.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/test/pyadpt-81.py b/test/pyadpt-81.py index 6ff7021..7fa0d45 100644 --- a/test/pyadpt-81.py +++ b/test/pyadpt-81.py @@ -2,6 +2,24 @@ # -*- coding: utf-8 -*- import pEp +import time + +def user_message_to_send(msg): + print("User defined callback implementation") + +def start_stop_sync(duration): + pEp.start_sync() + time.sleep(duration) + pEp.shutdown_sync() dir(pEp) -pEp.is_sync_active() \ No newline at end of file +# test default callback +start_stop_sync(1) +# test user defined callback +pEp.message_to_send = user_message_to_send +start_stop_sync(1) + +pEp.start_sync() +while(True): + print("is_sync_active: {}".format(pEp.is_sync_active())) + time.sleep(1) \ No newline at end of file From 1cf31e4687edcb95eb8eebf911160b59f558d78d Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 20 Aug 2020 14:37:45 +0200 Subject: [PATCH 08/13] Add Makefile targets: devenv, envtest --- Makefile | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 24c251d..88dab8d 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ PREFIX = /home/heck/local-default/ -DEBUG = 0 +DEBUG = 1 BUILD_DIR = ./build BUILD_EXT_OPTS = --prefix=$(PREFIX) @@ -8,7 +8,7 @@ ifeq ($(DEBUG),1) BUILD_EXT_OPTS+=--debug endif -.PHONY: all build clean +.PHONY: all build clean devenv envtest all: build_ext @@ -16,4 +16,13 @@ build_ext: python3 setup.py build_ext $(BUILD_EXT_OPTS) clean: - rm -r $(BUILD_DIR) \ No newline at end of file + rm -r $(BUILD_DIR) + +devenv: + LD_LIBRARY_PATH=$(PREFIX)/lib \ + PYTHONPATH=`pwd`/build/lib.linux-x86_64-3.7:\ + `pwd`/src \ + bash -l + +envtest: + python3 -c 'import pEp' \ No newline at end of file From 47a0fb5cc114edbf56d0e64009f6a4fc8b37e5da Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 20 Aug 2020 15:50:39 +0200 Subject: [PATCH 09/13] include guards, replace #pragma once, make sure license headers, minor formatting --- src/pEp/native_pEp/basic_api.hh | 9 ++++++--- src/pEp/native_pEp/identity.hh | 8 +++++--- src/pEp/native_pEp/message.hh | 8 +++++--- src/pEp/native_pEp/message_api.cc | 1 - src/pEp/native_pEp/message_api.hh | 9 ++++++--- src/pEp/native_pEp/pEpmodule.hh | 11 ++++++++--- src/pEp/native_pEp/str_attr.hh | 8 +++++--- src/pEp/native_pEp/user_interface.cc | 3 +-- src/pEp/native_pEp/user_interface.hh | 9 ++++++--- 9 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/pEp/native_pEp/basic_api.hh b/src/pEp/native_pEp/basic_api.hh index a9c951a..f4714a8 100644 --- a/src/pEp/native_pEp/basic_api.hh +++ b/src/pEp/native_pEp/basic_api.hh @@ -1,7 +1,8 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#pragma once +#ifndef BASIC_API_HH +#define BASIC_API_HH #include "pEpmodule.hh" @@ -23,5 +24,7 @@ string export_key(Identity ident); string export_secret_key(Identity ident); void set_own_key(Identity& ident, string fpr); -} // namespace PythonAdapter -} // namespace pEp { +} /* namespace PythonAdapter */ +} /* namespace pEp */ + +#endif /* BASIC_API_HH */ \ No newline at end of file diff --git a/src/pEp/native_pEp/identity.hh b/src/pEp/native_pEp/identity.hh index d01a00c..d70133f 100644 --- a/src/pEp/native_pEp/identity.hh +++ b/src/pEp/native_pEp/identity.hh @@ -1,7 +1,8 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#pragma once +#ifndef IDENTITY_HH +#define IDENTITY_HH // System #include @@ -95,6 +96,7 @@ void identity_attr(pEp_identity *&ident, object value); boost::python::list identitylist_attr(identity_list *&il); void identitylist_attr(identity_list *&il, boost::python::list value); -} // namespace PythonAdapter -} // namespace pEp { +} /* namespace PythonAdapter */ +} /* namespace pEp */ +#endif /* IDENTITY_HH */ \ No newline at end of file diff --git a/src/pEp/native_pEp/message.hh b/src/pEp/native_pEp/message.hh index 6ccc031..dac9a73 100644 --- a/src/pEp/native_pEp/message.hh +++ b/src/pEp/native_pEp/message.hh @@ -1,7 +1,8 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#pragma once +#ifndef MESSAGE_HH +#define MESSAGE_HH // System #include @@ -147,6 +148,7 @@ public: Message outgoing_message(Identity me); Message incoming_message(string mime_text); -} // namespace PythonAdapter -} // namespace pEp { +} /* namespace PythonAdapter */ +} /* namespace pEp */ +#endif /* MESSAGE_HH */ \ No newline at end of file diff --git a/src/pEp/native_pEp/message_api.cc b/src/pEp/native_pEp/message_api.cc index 41a49e0..6a4b76d 100644 --- a/src/pEp/native_pEp/message_api.cc +++ b/src/pEp/native_pEp/message_api.cc @@ -1,7 +1,6 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt - // Engine #include #include diff --git a/src/pEp/native_pEp/message_api.hh b/src/pEp/native_pEp/message_api.hh index 5147fe3..a793d32 100644 --- a/src/pEp/native_pEp/message_api.hh +++ b/src/pEp/native_pEp/message_api.hh @@ -1,7 +1,8 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#pragma once +#ifndef MESSAGE_API_HH +#define MESSAGE_API_HH #include "pEpmodule.hh" @@ -20,5 +21,7 @@ PEP_color _color(int rating); object sync_search(string name); object distribution_search(string name); -} // namespace PythonAdapter -} // namespace pEp { +} /* namespace PythonAdapter */ +} /* namespace pEp */ + +#endif /* MESSAGE_API_HH */ \ No newline at end of file diff --git a/src/pEp/native_pEp/pEpmodule.hh b/src/pEp/native_pEp/pEpmodule.hh index 4bfc6fa..0b72c96 100644 --- a/src/pEp/native_pEp/pEpmodule.hh +++ b/src/pEp/native_pEp/pEpmodule.hh @@ -1,4 +1,8 @@ -#pragma once +// This file is under GNU Affero General Public License 3.0 +// see LICENSE.txt + +#ifndef PEPMODULE_HH +#define PEPMODULE_HH // Engine #include @@ -18,6 +22,7 @@ void _throw_status(PEP_STATUS status); PEP_STATUS _messageToSend(::message *msg); PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal); -} // namespace PythonAdapter -} // namespace pEp { +} /* namespace PythonAdapter */ +} /* namespace pEp */ +#endif /* PEPMODULE_HH */ \ No newline at end of file diff --git a/src/pEp/native_pEp/str_attr.hh b/src/pEp/native_pEp/str_attr.hh index 2a6dbf9..978d22b 100644 --- a/src/pEp/native_pEp/str_attr.hh +++ b/src/pEp/native_pEp/str_attr.hh @@ -1,7 +1,8 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#pragma once +#ifndef STR_ATTR_HH +#define STR_ATTR_HH // System #include @@ -36,6 +37,7 @@ void strdict_attr(stringpair_list_t *&spl, dict value); stringlist_t *to_stringlist(boost::python::list l); boost::python::list from_stringlist(const stringlist_t *sl); -} // namespace PythonAdapter -} // namespace pEp { +} /* namespace PythonAdapter */ +} /* namespace pEp */ +#endif /* STR_ATTR_HH */ \ No newline at end of file diff --git a/src/pEp/native_pEp/user_interface.cc b/src/pEp/native_pEp/user_interface.cc index a21234d..22be5db 100644 --- a/src/pEp/native_pEp/user_interface.cc +++ b/src/pEp/native_pEp/user_interface.cc @@ -59,8 +59,7 @@ PEP_STATUS UserInterface::_notifyHandshake( void UserInterface::deliverHandshakeResult(int result, object identities) { identity_list *shared_identities = nullptr; - if (identities != boost::python::api::object() && - boost::python::len(identities)) { + if (identities != boost::python::api::object() && boost::python::len(identities)) { shared_identities = new_identity_list(nullptr); if (!shared_identities) throw bad_alloc(); diff --git a/src/pEp/native_pEp/user_interface.hh b/src/pEp/native_pEp/user_interface.hh index e222b3c..f609a41 100644 --- a/src/pEp/native_pEp/user_interface.hh +++ b/src/pEp/native_pEp/user_interface.hh @@ -1,7 +1,8 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -#pragma once +#ifndef USER_INTERFACE_HH +#define USER_INTERFACE_HH // System #include @@ -52,6 +53,8 @@ public: ); }; -} // namespace PythonAdapter -} // namespace pEp { +} /* namespace PythonAdapter */ +} /* namespace pEp */ + +#endif /* USER_INTERFACE_HH */ From 57cec46a756078e8daadeca21a8167cce5fa9b96 Mon Sep 17 00:00:00 2001 From: heck Date: Mon, 24 Aug 2020 20:25:38 +0200 Subject: [PATCH 10/13] Add Makefile.conf / local.conf.example (like we have it in pEpEngine, libpEpAdapter, pEpJNIAdapter) --- Makefile | 10 +--------- Makefile.conf | 15 +++++++++++++++ local.conf.example | 11 +++++++++++ 3 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 Makefile.conf create mode 100644 local.conf.example diff --git a/Makefile b/Makefile index 88dab8d..78e08b9 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,4 @@ -PREFIX = /home/heck/local-default/ -DEBUG = 1 -BUILD_DIR = ./build - -BUILD_EXT_OPTS = --prefix=$(PREFIX) - -ifeq ($(DEBUG),1) - BUILD_EXT_OPTS+=--debug -endif +include Makefile.conf .PHONY: all build clean devenv envtest diff --git a/Makefile.conf b/Makefile.conf new file mode 100644 index 0000000..9bacf8a --- /dev/null +++ b/Makefile.conf @@ -0,0 +1,15 @@ +HERE:=$(dir $(lastword $(MAKEFILE_LIST))) + +# Defaults +DEBUG=1 +PREFIX?=$(HOME) +BUILD_DIR = ./build + +######### Overrides ######### +-include $(HERE)local.conf + +ifeq ($(DEBUG),1) + BUILD_EXT_OPTS+=--debug +endif + +BUILD_EXT_OPTS += --prefix=$(PREFIX) diff --git a/local.conf.example b/local.conf.example new file mode 100644 index 0000000..9438ccf --- /dev/null +++ b/local.conf.example @@ -0,0 +1,11 @@ +# This is an Example build config file (local.conf) +# you might not need this file, but if the defaults dont work for you +# You can override them here. +# Tweak the values to your needs and rename it to local.conf + +######### C++ Compiler ######### +# DEBUG=1 +# DEBUG=0 + +############# DIRS ############# +# PREFIX=$(HOME)/local From ae364de4cf1c936e2d8316f96aa5654b330f14cc Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 26 Aug 2020 21:29:06 +0200 Subject: [PATCH 11/13] Adjust build etc. for MacOS --- Makefile | 4 +++- Makefile.conf | 2 +- setup.py | 8 +------- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 78e08b9..466b150 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,9 @@ clean: devenv: LD_LIBRARY_PATH=$(PREFIX)/lib \ - PYTHONPATH=`pwd`/build/lib.linux-x86_64-3.7:\ + DYLD_LIBRARY_PATH=$(PREFIX)/lib \ + PYTHONPATH=$PYTHONPATH:`pwd`/build/lib.linux-x86_64-3.7:\ + PYTHONPATH=$PYTHONPATH:`pwd`/build/lib.macosx-10.9-x86_64-3.8:\ `pwd`/src \ bash -l diff --git a/Makefile.conf b/Makefile.conf index 9bacf8a..3ca9e1d 100644 --- a/Makefile.conf +++ b/Makefile.conf @@ -1,7 +1,7 @@ HERE:=$(dir $(lastword $(MAKEFILE_LIST))) # Defaults -DEBUG=1 +DEBUG=0 PREFIX?=$(HOME) BUILD_DIR = ./build diff --git a/setup.py b/setup.py index 445936c..d71d00e 100644 --- a/setup.py +++ b/setup.py @@ -96,20 +96,14 @@ class BuildExtCommand(build_ext): home = environ.get('PER_USER_DIRECTORY') or environ.get('HOME') sys_includes = [ '/opt/local/include', - '/usr/local/include', - '/Library/Frameworks/PrettyEasyPrivacy.framework/Versions/A/include', - '/usr/include', ] sys_libdirs = [ '/opt/local/lib', - '/usr/local/lib', - '/Library/Frameworks/PrettyEasyPrivacy.framework/Versions/A/lib', - '/usr/lib', ] libs = [ 'pEpEngine', 'pEpAdapter', - 'boost_python37-mt', + 'boost_python38-mt', 'boost_locale-mt' ] return (home, sys_includes, sys_libdirs, libs) From 7eef5f96052a2f5729ef0a91a66fcdf5450b6c77 Mon Sep 17 00:00:00 2001 From: heck Date: Wed, 26 Aug 2020 22:37:09 +0200 Subject: [PATCH 12/13] .hgignore local.conf --- .hgignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.hgignore b/.hgignore index cc3f876..8850bfe 100644 --- a/.hgignore +++ b/.hgignore @@ -1,5 +1,6 @@ syntax: glob +local.conf *.swp ws tags From 73666d8437837022c220f787948dd551f6759e41 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 27 Aug 2020 00:07:21 +0200 Subject: [PATCH 13/13] Mutli-threading support for native libs calling python code added mixing UserInterface replaced with pEp.notify_handshake() to be overwritten (just like message_to_send()) --- setup.py | 2 +- src/pEp/__init__.py | 14 +++++++++ src/pEp/native_pEp/pEpmodule.cc | 54 +++++++++++++++++++++++++++++++-- test/codec_doctest.py | 5 +-- test/pyadpt-81.py | 36 +++++++++++++++++----- 5 files changed, 99 insertions(+), 12 deletions(-) diff --git a/setup.py b/setup.py index d71d00e..b8fb4fd 100644 --- a/setup.py +++ b/setup.py @@ -212,7 +212,7 @@ module_pEp = Extension( 'src/pEp/native_pEp/message.cc', 'src/pEp/native_pEp/message_api.cc', 'src/pEp/native_pEp/str_attr.cc', - 'src/pEp/native_pEp/user_interface.cc', + # 'src/pEp/native_pEp/user_interface.cc', ], ) diff --git a/src/pEp/__init__.py b/src/pEp/__init__.py index 2cfd9cc..8a32e83 100644 --- a/src/pEp/__init__.py +++ b/src/pEp/__init__.py @@ -22,6 +22,20 @@ def message_to_send(msg): message_to_send(msg) override pEp.message_to_send(msg) with your own implementation this callback is being called when a p≡p management message needs to be sent + GIL CAVEAT + """ + print("message_to_send() - default callback\n") + print("overwrite this method") + + +def notify_handshake(me, partner, signal): + """ + notifyHandshake(self, me, partner) + me own identity + partner identity of communication partner + signal the handshake signal + overwrite this method with an implementation of a handshake dialog + GIL CAVEAT """ print("message_to_send() - default callback\n") print("overwrite this method") diff --git a/src/pEp/native_pEp/pEpmodule.cc b/src/pEp/native_pEp/pEpmodule.cc index d77dc34..6148c95 100644 --- a/src/pEp/native_pEp/pEpmodule.cc +++ b/src/pEp/native_pEp/pEpmodule.cc @@ -24,7 +24,7 @@ #include "pEpmodule.hh" #include "basic_api.hh" #include "message_api.hh" -#include "user_interface.hh" +//#include "user_interface.hh" namespace pEp { namespace PythonAdapter { @@ -108,9 +108,13 @@ PEP_STATUS _messageToSend(::message *msg) { pEpLog("called"); try { + PyGILState_STATE gil = PyGILState_Ensure(); + pEpLog("GIL Aquired"); object modref = import("pEp"); object funcref = modref.attr("message_to_send"); - call(funcref.ptr(), Message(msg)); + call(funcref.ptr(), Message()); + PyGILState_Release(gil); + pEpLog("GIL released"); } catch (exception& e) { } return PEP_STATUS_OK; @@ -119,6 +123,16 @@ PEP_STATUS _messageToSend(::message *msg) PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) { pEpLog("called"); + try { + PyGILState_STATE gil = PyGILState_Ensure(); + pEpLog("GIL Aquired"); + object modref = import("pEp"); + object funcref = modref.attr("notify_handshake"); + call(funcref.ptr(), me, partner, signal); + PyGILState_Release(gil); + pEpLog("GIL released"); + } catch (exception& e) { } + return PEP_STATUS_OK; } @@ -152,6 +166,34 @@ void testfunc() { _messageToSend(NULL); } +void deliverHandshakeResult(int result, object identities) +{ + identity_list *shared_identities = nullptr; + if (identities != boost::python::api::object() && boost::python::len(identities)) { + shared_identities = new_identity_list(nullptr); + if (!shared_identities) + throw bad_alloc(); + + try { + identity_list *si = shared_identities; + for (int i=0; i < boost::python::len(identities); ++i) { + Identity ident = extract< Identity >(identities[i]); + si = identity_list_add(si, ident); + if (!si) + throw bad_alloc(); + } + } + catch (exception& ex) { + free_identity_list(shared_identities); + throw ex; + } + } + + PEP_STATUS status = ::deliverHandshakeResult(Adapter::session(), (sync_handshake_result) result, shared_identities); + free_identity_list(shared_identities); + _throw_status(status); +} + BOOST_PYTHON_MODULE(native_pEp) { init_before_main_module(); @@ -585,6 +627,14 @@ BOOST_PYTHON_MODULE(native_pEp) // "call to deliver the handshake result of the handshake dialog" // ); + def("deliver_handshake_result", &deliverHandshakeResult, boost::python::arg("identities")=object(), + "deliverHandshakeResult(self, result, identities=None)\n" + "\n" + " result -1: cancel, 0: accepted, 1: rejected\n" + " identities list of identities to share or None for all\n" + "\n" + "call to deliver the handshake result of the handshake dialog" + ); def("start_sync", &start_sync, "start_sync()\n" diff --git a/test/codec_doctest.py b/test/codec_doctest.py index 95a1a0a..e876826 100755 --- a/test/codec_doctest.py +++ b/test/codec_doctest.py @@ -3,14 +3,15 @@ """ >>> import pEp ->>> def messageToSend(msg): +>>> def message_to_send(msg): ... m, keys, rating, flags = msg.decrypt() ... try: ... m.attachments[0].decode() ... print("decode successfull") ... except UnicodeDecodeError as e: ... print("decode failed") ->>> pEp.messageToSend = messageToSend +>>> pEp.message_to_send = message_to_send +>>> pEp.myself(pEp.Identity("")) >>> pEp.key_reset_all_own_keys() decode successfull decode successfull diff --git a/test/pyadpt-81.py b/test/pyadpt-81.py index 7fa0d45..973125b 100644 --- a/test/pyadpt-81.py +++ b/test/pyadpt-81.py @@ -4,22 +4,44 @@ import pEp import time -def user_message_to_send(msg): - print("User defined callback implementation") +def message_to_send(msg): + print("User defined message_to_send() called") + m, keys, rating, flags = msg.decrypt() + try: + print(m.attachments[0].decode()) + except UnicodeDecodeError as e: + print("decode failed") + +def notify_handshake(me, partner, signal): + print("User defined notify_handshake() called") + print(me) + print(partner) + print(signal) def start_stop_sync(duration): pEp.start_sync() time.sleep(duration) pEp.shutdown_sync() + +alice = pEp.Identity("test@alice.com", "alice", "23") +pEp.myself(alice) +print(alice.fpr) + dir(pEp) + # test default callback start_stop_sync(1) + # test user defined callback -pEp.message_to_send = user_message_to_send +pEp.message_to_send = message_to_send +# pEp.notify_handshake = notify_handshake + start_stop_sync(1) -pEp.start_sync() -while(True): - print("is_sync_active: {}".format(pEp.is_sync_active())) - time.sleep(1) \ No newline at end of file +# pEp.start_sync() +# while(True): +# print("is_sync_active: {}".format(pEp.is_sync_active())) +# time.sleep(3) +# pEp.key_reset_all_own_keys() +# time.sleep(3) \ No newline at end of file