diff --git a/setup.py b/setup.py index 051699f..b4530a0 100755 --- a/setup.py +++ b/setup.py @@ -201,6 +201,7 @@ if sys.version_info[0] < 3: module_pEp = Extension( 'pEp._pEp', sources=[ + 'src/pEp/_pEp/adapter_main.cc', 'src/pEp/_pEp/pEpmodule.cc', 'src/pEp/_pEp/basic_api.cc', 'src/pEp/_pEp/identity.cc', diff --git a/src/pEp/_pEp/adapter_main.cc b/src/pEp/_pEp/adapter_main.cc new file mode 100644 index 0000000..bea2bf3 --- /dev/null +++ b/src/pEp/_pEp/adapter_main.cc @@ -0,0 +1,163 @@ +// This file is under GNU Affero General Public License 3.0 +// see LICENSE.txt + +#include "adapter_main.hh" +#include "message.hh" +#include "message_api.hh" + +namespace pEp { +namespace PythonAdapter { + + +static const char *version_string = "p≡p Python adapter version 0.3"; + +void init_before_main_module() { + pEpLog("called"); +} + +// 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); +} + +void config_unencrypted_subject(bool enable) { + ::config_unencrypted_subject(Adapter::session(), enable); +} + +void key_reset_user(const string &user_id, const string &fpr) { + if (user_id == "") { + throw invalid_argument("user_id required"); + } + + ::PEP_STATUS status = ::key_reset_user(Adapter::session(), user_id.c_str(), fpr != "" ? fpr.c_str() : nullptr); + _throw_status(status); +} + +void key_reset_user2(const string &user_id) { + key_reset_user(user_id, ""); +} + +void key_reset_all_own_keys() { + ::PEP_STATUS status = ::key_reset_all_own_keys(Adapter::session()); + _throw_status(status); +} + +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 (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(status_to_string(status)); + } +} + +::PEP_STATUS _messageToSend(::message *msg) { + pEpLog("called"); + try { + PyGILState_STATE gil = PyGILState_Ensure(); + pEpLog("GIL Aquired"); + bp::object modref = bp::import("pEp"); + bp::object funcref = modref.attr("message_to_send"); + bp::call(funcref.ptr(), Message()); + PyGILState_Release(gil); + pEpLog("GIL released"); + } catch (exception &e) { + } + + return ::PEP_STATUS_OK; +} + +::PEP_STATUS notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal) { + pEpLog("called"); + try { + PyGILState_STATE gil = PyGILState_Ensure(); + pEpLog("GIL Aquired"); + bp::object modref = bp::import("pEp"); + bp::object funcref = modref.attr("notify_handshake"); + bp::call(funcref.ptr(), me, partner, signal); + PyGILState_Release(gil); + pEpLog("GIL released"); + } catch (exception &e) { + } + + return ::PEP_STATUS_OK; +} + +void start_sync() { + CallbackDispatcher::start_sync(); +} + +void shutdown_sync() { + CallbackDispatcher::stop_sync(); +} + +void debug_color(int ansi_color) { + ::set_debug_color(Adapter::session(), ansi_color); +} + +void leave_device_group() { + ::leave_device_group(Adapter::session()); +} + +bool is_sync_active() { + return Adapter::is_sync_running(); +} + +void testfunc() { + _messageToSend(nullptr); +} + +void deliverHandshakeResult(int result, bp::object identities) { + identity_list *shared_identities = nullptr; + if (identities != bp::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 < bp::len(identities); ++i) { + Identity ident = bp::extract(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); +} +} // namespace PythonAdapter +} // namespace pEp diff --git a/src/pEp/_pEp/adapter_main.hh b/src/pEp/_pEp/adapter_main.hh new file mode 100644 index 0000000..d167153 --- /dev/null +++ b/src/pEp/_pEp/adapter_main.hh @@ -0,0 +1,84 @@ +// This file is under GNU Affero General Public License 3.0 +// see LICENSE.txt + +#ifndef ADAPTER_MAIN_HH +#define ADAPTER_MAIN_HH + +// System +#include +#include + +// Boost +#include +#include + +// Engine +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// libpEpAdapter +#include +#include +#include +#include + +namespace pEp { +namespace PythonAdapter { + +using namespace std; +namespace bp = boost::python; +namespace bl = boost::locale; + +void init_before_main_module(); + +void _init_after_main_module(); + +void testfunc(); + + +//extern string device_name; + +string about(); + +void config_passive_mode(bool enable); + +void start_sync(); + +void shutdown_sync(); + +void debug_color(int ansi_color); + +bool is_sync_active(); + +void config_unencrypted_subject(bool enable); + +void key_reset_user(const string &user_id, const string &fpr); + +void key_reset_user2(const string &user_id); + +void key_reset_all_own_keys(); + +void _throw_status(::PEP_STATUS status); + +void leave_device_group(); + +::PEP_STATUS _messageToSend(::message *msg); + +::PEP_STATUS notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal); + +void deliverHandshakeResult(int result, bp::object identities); + +} // namespace PythonAdapter +} // namespace pEp + +#endif // ADAPTER_MAIN_HH diff --git a/src/pEp/_pEp/basic_api.cc b/src/pEp/_pEp/basic_api.cc index af3ba48..86f98fa 100644 --- a/src/pEp/_pEp/basic_api.cc +++ b/src/pEp/_pEp/basic_api.cc @@ -1,20 +1,10 @@ // 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 { -using namespace std; void update_identity(Identity &ident) { if (ident.address() == "") { @@ -48,7 +38,7 @@ string _trustwords(Identity me, Identity partner, string lang, bool full) { if (lang == "" && me.lang() == partner.lang()) { lang = me.lang(); } - char *words = NULL; + char *words = nullptr; size_t size = 0; ::PEP_STATUS status = ::get_trustwords(Adapter::session(), me, partner, lang.c_str(), &words, &size, full); _throw_status(status); @@ -66,7 +56,7 @@ void trust_personal_key(Identity ident) { _throw_status(status); } -void set_identity_flags(Identity ident, ::identity_flags_t flags) { +void set_identity_flags(Identity ident,const ::identity_flags_t &flags) { if (ident.address() == "") { throw invalid_argument("address needed"); } @@ -77,7 +67,7 @@ void set_identity_flags(Identity ident, ::identity_flags_t flags) { _throw_status(status); } -void unset_identity_flags(Identity ident, ::identity_flags_t flags) { +void unset_identity_flags(Identity ident,const ::identity_flags_t &flags) { if (ident.address() == "") { throw invalid_argument("address needed"); } @@ -102,8 +92,8 @@ void key_reset_trust(Identity ident) { _throw_status(status); } -bp::list import_key(string key_data) { - ::identity_list *private_keys = NULL; +bp::list import_key(const string &key_data) { + ::identity_list *private_keys = nullptr; ::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); @@ -123,7 +113,7 @@ bp::list import_key(string key_data) { string export_key(Identity ident) { ::PEP_STATUS status = ::PEP_STATUS_OK; - char *key_data = NULL; + char *key_data = nullptr; size_t size; status = ::export_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size); _throw_status(status); @@ -139,7 +129,7 @@ string export_secret_key(Identity ident) { return key_data; } -void set_own_key(Identity &ident, string fpr) { +void set_own_key(Identity &ident, const string &fpr) { if (ident.address() == "") { throw invalid_argument("address needed"); } diff --git a/src/pEp/_pEp/basic_api.hh b/src/pEp/_pEp/basic_api.hh index 1709aa2..85d0000 100644 --- a/src/pEp/_pEp/basic_api.hh +++ b/src/pEp/_pEp/basic_api.hh @@ -4,12 +4,11 @@ #ifndef BASIC_API_HH #define BASIC_API_HH -#include "pEpmodule.hh" +#include "adapter_main.hh" #include "identity.hh" namespace pEp { namespace PythonAdapter { -namespace bp = boost::python; void update_identity(Identity &ident); @@ -19,19 +18,19 @@ 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 set_identity_flags(Identity ident,const ::identity_flags_t &flags); -void unset_identity_flags(Identity ident, ::identity_flags_t flags); +void unset_identity_flags(Identity ident,const ::identity_flags_t &flags); void key_reset_trust(Identity ident); -bp::list import_key(string key_data); +bp::list import_key(const string &key_data); string export_key(Identity ident); string export_secret_key(Identity ident); -void set_own_key(Identity &ident, string fpr); +void set_own_key(Identity &ident, const string &fpr); } /* namespace PythonAdapter */ } /* namespace pEp */ diff --git a/src/pEp/_pEp/identity.cc b/src/pEp/_pEp/identity.cc index 32c7bec..b72ef1c 100644 --- a/src/pEp/_pEp/identity.cc +++ b/src/pEp/_pEp/identity.cc @@ -1,25 +1,13 @@ // 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 { -using namespace std; -namespace bp = 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) { @@ -147,7 +135,7 @@ void Identity::update() { update_identity(*this); } -void Identity::key_reset(string fpr) { +void Identity::key_reset(const string &fpr) { ::PEP_STATUS status = ::key_reset_identity(Adapter::session(), *this, fpr != "" ? fpr.c_str() : nullptr); _throw_status(status); } @@ -232,7 +220,7 @@ bp::list identitylist_attr(::identity_list *&il) { } void identitylist_attr(::identity_list *&il, bp::list value) { - ::identity_list *_il = ::new_identity_list(NULL); + ::identity_list *_il = ::new_identity_list(nullptr); if (!_il) { throw bad_alloc(); } diff --git a/src/pEp/_pEp/identity.hh b/src/pEp/_pEp/identity.hh index 5654e51..18dc80c 100644 --- a/src/pEp/_pEp/identity.hh +++ b/src/pEp/_pEp/identity.hh @@ -5,29 +5,13 @@ #define IDENTITY_HH // System -#include -#include -#include -#include - -// Engine -#include -#include - -//libpEpAdapter -#include "pEp/Adapter.hh" - -// local +#include "adapter_main.hh" #include "str_attr.hh" namespace pEp { namespace PythonAdapter { -using std::string; -using std::shared_ptr; - // Identity is owning a pEp_identity - class Identity { protected: shared_ptr<::pEp_identity> _ident; @@ -87,7 +71,7 @@ class Identity { virtual void update(); - void key_reset(string fpr = ""); + void key_reset(const string &fpr = ""); void key_mistrusted(); diff --git a/src/pEp/_pEp/message.cc b/src/pEp/_pEp/message.cc index 8cf8da2..bacac18 100644 --- a/src/pEp/_pEp/message.cc +++ b/src/pEp/_pEp/message.cc @@ -1,27 +1,13 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -// 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; -namespace bp = boost::python; + Message::Blob::Blob(::bloblist_t *bl, bool chained) : _bl(bl), @@ -32,7 +18,7 @@ Message::Blob::Blob(::bloblist_t *bl, bool chained) } Message::Blob::Blob(bp::object data, string mime_type, string filename) - : _bl(::new_bloblist(NULL, 0, NULL, NULL)), + : _bl(::new_bloblist(nullptr, 0, nullptr, nullptr)), part_of_chain(false) { if (!_bl) { throw bad_alloc(); @@ -96,20 +82,20 @@ string Message::Blob::_repr() { } int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { - ::bloblist_t *bl = NULL; + ::bloblist_t *bl = nullptr; try { Message::Blob &blob = bp::extract(self); bl = blob._bl; } catch (exception &e) { PyErr_SetString(PyExc_RuntimeError, "extract not possible"); - view->obj = NULL; + view->obj = nullptr; return -1; } if (!(bl && bl->value)) { PyErr_SetString(PyExc_RuntimeError, "no data available"); - view->obj = NULL; + view->obj = nullptr; return -1; } @@ -135,7 +121,7 @@ string Message::Blob::decode(string encoding) { return bp::call(_decode.ptr(), this, encoding); } -PyBufferProcs Message::Blob::bp = {getbuffer, NULL}; +PyBufferProcs Message::Blob::bp = {getbuffer, nullptr}; Message::Message(int dir, Identity *from) : _msg(new_message((::PEP_msg_direction)dir), &::free_message) { @@ -153,9 +139,9 @@ Message::Message(int dir, Identity *from) } Message::Message(string mimetext) - : _msg(NULL, &::free_message) { - message *_cpy; - ::PEP_STATUS status = ::mime_decode_message(mimetext.c_str(), mimetext.size(), &_cpy, NULL); + : _msg(nullptr, &::free_message) { + ::message *_cpy; + ::PEP_STATUS status = ::mime_decode_message(mimetext.c_str(), mimetext.size(), &_cpy, nullptr); switch (status) { case ::PEP_STATUS_OK: if (_cpy) { @@ -257,7 +243,7 @@ bp::tuple Message::attachments() { } void Message::attachments(bp::list value) { - ::bloblist_t *bl = ::new_bloblist(NULL, 0, NULL, NULL); + ::bloblist_t *bl = ::new_bloblist(nullptr, 0, nullptr, nullptr); if (!bl) { throw bad_alloc(); } @@ -280,12 +266,12 @@ void Message::attachments(bp::list value) { for (int i = 0; i < len(value); i++) { Message::Blob &blob = bp::extract(value[i]); - blob._bl->value = NULL; + blob._bl->value = nullptr; blob._bl->size = 0; free(blob._bl->mime_type); - blob._bl->mime_type = NULL; + blob._bl->mime_type = nullptr; free(blob._bl->filename); - blob._bl->filename = NULL; + blob._bl->filename = nullptr; } free_bloblist(_msg->attachments); diff --git a/src/pEp/_pEp/message.hh b/src/pEp/_pEp/message.hh index ef13348..d356011 100644 --- a/src/pEp/_pEp/message.hh +++ b/src/pEp/_pEp/message.hh @@ -4,26 +4,14 @@ #ifndef MESSAGE_HH #define MESSAGE_HH -// System -#include -#include - -// Engine -#include -#include - -// local +#include "adapter_main.hh" #include "str_attr.hh" #include "identity.hh" namespace pEp { namespace PythonAdapter { -using std::string; -using std::runtime_error; -using std::invalid_argument; // Message is owning a message struct - class Message { shared_ptr<::message> _msg; @@ -36,7 +24,7 @@ class Message { bool part_of_chain; public: - Blob(::bloblist_t *bl = ::new_bloblist(NULL, 0, NULL, NULL), bool chained = false); + Blob(::bloblist_t *bl = ::new_bloblist(nullptr, 0, nullptr, nullptr), bool chained = false); Blob(bp::object data, string mime_type = "", string filename = ""); @@ -68,7 +56,7 @@ class Message { static int getbuffer(PyObject *self, Py_buffer *view, int flags); }; - Message(int dir = ::PEP_dir_outgoing, Identity *from = NULL); + Message(int dir = ::PEP_dir_outgoing, Identity *from = nullptr); Message(string mimetext); diff --git a/src/pEp/_pEp/message_api.cc b/src/pEp/_pEp/message_api.cc index 29bc9f9..d0467e6 100644 --- a/src/pEp/_pEp/message_api.cc +++ b/src/pEp/_pEp/message_api.cc @@ -1,20 +1,12 @@ // 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" namespace pEp { namespace PythonAdapter { -using namespace std; -namespace bp = boost::python; + Message encrypt_message(Message src, bp::list extra, int enc_format, int flags) { Identity _from = src.from(); @@ -32,7 +24,7 @@ Message encrypt_message(Message src, bp::list extra, int enc_format, int flags) ::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 *_dst = nullptr; ::message *_src = src; ::PEP_STATUS status = ::encrypt_message(Adapter::session(), _src, _extra, &_dst, _enc_format, _flags); @@ -47,8 +39,8 @@ Message encrypt_message(Message src, bp::list extra, int enc_format, int flags) } bp::tuple decrypt_message(Message src, int flags) { - ::message *_dst = NULL; - ::stringlist_t *_keylist = NULL; + ::message *_dst = nullptr; + ::stringlist_t *_keylist = nullptr; ::PEP_rating _rating = ::PEP_rating_undefined; ::PEP_decrypt_flags_t _flags = (::PEP_decrypt_flags_t)flags; ::message *_src = src; @@ -77,7 +69,7 @@ bp::tuple sync_decode(bp::object buffer) { throw invalid_argument("need a contiguous buffer to read"); } - char *dst = NULL; + char *dst = nullptr; ::PEP_STATUS status = ::PER_to_XER_Sync_msg((char *)src.buf, src.len, &dst); PyBuffer_Release(&src); _throw_status(status); @@ -88,7 +80,7 @@ bp::tuple sync_decode(bp::object buffer) { } static bp::tuple sync_encode(string text) { - char *data = NULL; + char *data = nullptr; size_t size = 0; ::PEP_STATUS status = ::XER_to_PER_Sync_msg(text.c_str(), &data, &size); _throw_status(status); @@ -109,7 +101,7 @@ bp::tuple Distribution_decode(bp::object buffer) { throw invalid_argument("need a contiguous buffer to read"); } - char *dst = NULL; + char *dst = nullptr; ::PEP_STATUS status = ::PER_to_XER_Distribution_msg((char *)src.buf, src.len, &dst); PyBuffer_Release(&src); _throw_status(status); @@ -120,7 +112,7 @@ bp::tuple Distribution_decode(bp::object buffer) { } static bp::tuple Distribution_encode(string text) { - char *data = NULL; + char *data = nullptr; size_t size = 0; ::PEP_STATUS status = ::XER_to_PER_Distribution_msg(text.c_str(), &data, &size); _throw_status(status); @@ -163,4 +155,4 @@ bp::object distribution_search(string name) { } } // namespace PythonAdapter -} // namespace pEp { +} // namespace pEp diff --git a/src/pEp/_pEp/message_api.hh b/src/pEp/_pEp/message_api.hh index 56140a5..3e0421a 100644 --- a/src/pEp/_pEp/message_api.hh +++ b/src/pEp/_pEp/message_api.hh @@ -4,7 +4,8 @@ #ifndef MESSAGE_API_HH #define MESSAGE_API_HH -#include "pEpmodule.hh" +#include "adapter_main.hh" +#include "message.hh" namespace pEp { namespace PythonAdapter { diff --git a/src/pEp/_pEp/pEpmodule.cc b/src/pEp/_pEp/pEpmodule.cc index 4ca9eca..205a609 100644 --- a/src/pEp/_pEp/pEpmodule.cc +++ b/src/pEp/_pEp/pEpmodule.cc @@ -1,186 +1,16 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -// System -#include -#include -#include -#include -#include - -// Engine -#include -#include -#include -#include - -// libpEpAdapter -#include -#include -#include // local -#include "pEpmodule.hh" +#include "adapter_main.hh" #include "basic_api.hh" +#include "message.hh" #include "message_api.hh" -//#include "user_interface.hh" namespace pEp { namespace PythonAdapter { -using namespace std; -namespace bp = boost::python; -namespace bl = boost::locale; -static const char *version_string = "p≡p Python adapter version 0.3"; - -void init_before_main_module() { - pEpLog("called"); -} - -// 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); -} - -void config_unencrypted_subject(bool enable) { - ::config_unencrypted_subject(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(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(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 _messageToSend(::message *msg) { - pEpLog("called"); - try { - PyGILState_STATE gil = PyGILState_Ensure(); - pEpLog("GIL Aquired"); - bp::object modref = bp::import("pEp"); - bp::object funcref = modref.attr("message_to_send"); - bp::call(funcref.ptr(), Message()); - PyGILState_Release(gil); - pEpLog("GIL released"); - } catch (exception &e) { - } - - return ::PEP_STATUS_OK; -} - -::PEP_STATUS notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal) { - pEpLog("called"); - try { - PyGILState_STATE gil = PyGILState_Ensure(); - pEpLog("GIL Aquired"); - bp::object modref = bp::import("pEp"); - bp::object funcref = modref.attr("notify_handshake"); - bp::call(funcref.ptr(), me, partner, signal); - PyGILState_Release(gil); - pEpLog("GIL released"); - } catch (exception &e) { - } - - return ::PEP_STATUS_OK; -} - -void start_sync() { - CallbackDispatcher::start_sync(); -} - -void shutdown_sync() { - CallbackDispatcher::stop_sync(); -} - -void debug_color(int ansi_color) { - ::set_debug_color(Adapter::session(), ansi_color); -} - -void leave_device_group() { - ::leave_device_group(Adapter::session()); -} - -bool is_sync_active() { - return Adapter::is_sync_running(); -} - -void testfunc() { - _messageToSend(NULL); -} - -void deliverHandshakeResult(int result, bp::object identities) { - identity_list *shared_identities = nullptr; - if (identities != bp::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 < bp::len(identities); ++i) { - Identity ident = bp::extract(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(_pEp) { using boost::python::def; @@ -367,7 +197,7 @@ BOOST_PYTHON_MODULE(_pEp) { .add_property("dir", (int(Message::*)()) (PEP_msg_direction(Message::*)()) &Message::dir, (void(Message::*)(int)) - (void(Message::*)(PEP_msg_direction)) &Message::dir, + (void(Message::*)(::PEP_msg_direction)) &Message::dir, "0: incoming, 1: outgoing message") .add_property("id", (string(Message::*)()) &Message::id, (void(Message::*)(string)) &Message::id, diff --git a/src/pEp/_pEp/pEpmodule.hh b/src/pEp/_pEp/pEpmodule.hh index 829eac1..66a8db9 100644 --- a/src/pEp/_pEp/pEpmodule.hh +++ b/src/pEp/_pEp/pEpmodule.hh @@ -4,30 +4,10 @@ #ifndef PEPMODULE_HH #define PEPMODULE_HH -// 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); - -::PEP_STATUS _messageToSend(::message *msg); - -::PEP_STATUS notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal); } /* namespace PythonAdapter */ } /* namespace pEp */ diff --git a/src/pEp/_pEp/str_attr.cc b/src/pEp/_pEp/str_attr.cc index 3e09bb4..53dd906 100644 --- a/src/pEp/_pEp/str_attr.cc +++ b/src/pEp/_pEp/str_attr.cc @@ -1,25 +1,18 @@ // This file is under GNU Affero General Public License 3.0 // see LICENSE.txt -// System -#include -#include -#include - // local #include "str_attr.hh" namespace pEp { namespace PythonAdapter { -using namespace std; -namespace bp = boost::python; -namespace bl = boost::locale; + bp::object repr(bp::object s) { return s.attr("__repr__")(); } -string repr(string s) { +string repr(const string &s) { bp::str _s = s.c_str(); bp::object _r = _s.attr("__repr__")(); string r = bp::extract(_r); @@ -33,7 +26,7 @@ string str_attr(char *&str) { return string(str); } -void str_attr(char *&str, string value) { +void str_attr(char *&str, const string &value) { string normalized = normalize(value, bl::norm_nfc); free(str); str = strdup(normalized.c_str()); @@ -58,7 +51,7 @@ void timestamp_attr(::timestamp *&ts, time_t value) { bp::list strlist_attr(::stringlist_t *&sl) { bp::list result; - for (::stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { + for (const ::stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { string s(_sl->value); result.append(bp::object(s)); } @@ -108,7 +101,7 @@ bp::dict strdict_attr(::stringpair_list_t *&spl) { } void strdict_attr(::stringpair_list_t *&spl, bp::dict value) { - ::stringpair_list_t *_spl = ::new_stringpair_list(NULL); + ::stringpair_list_t *_spl = ::new_stringpair_list(nullptr); if (!_spl) { throw bad_alloc(); } @@ -143,7 +136,7 @@ void strdict_attr(::stringpair_list_t *&spl, bp::dict value) { } ::stringlist_t *to_stringlist(bp::list l) { - ::stringlist_t *result = ::new_stringlist(NULL); + ::stringlist_t *result = ::new_stringlist(nullptr); if (!result) { throw bad_alloc(); } diff --git a/src/pEp/_pEp/str_attr.hh b/src/pEp/_pEp/str_attr.hh index 8a7238c..d57c545 100644 --- a/src/pEp/_pEp/str_attr.hh +++ b/src/pEp/_pEp/str_attr.hh @@ -4,27 +4,18 @@ #ifndef STR_ATTR_HH #define STR_ATTR_HH -// System -#include - -// Engine -#include -#include -#include -#include +#include "adapter_main.hh" namespace pEp { namespace PythonAdapter { -using std::string; -namespace bp = boost::python; bp::object repr(bp::object s); -string repr(string s); +string repr(const string &s); string str_attr(char *&str); -void str_attr(char *&str, string value); +void str_attr(char *&str, const string &value); time_t timestamp_attr(::timestamp *&ts); diff --git a/src/pEp/_pEp/user_interface.cc b/src/pEp/_pEp/user_interface.cc index a8306fa..79202d3 100644 --- a/src/pEp/_pEp/user_interface.cc +++ b/src/pEp/_pEp/user_interface.cc @@ -1,16 +1,11 @@ // 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 { -using namespace std; -//namespace bp = boost::python; UserInterface *UserInterface::_ui = nullptr; @@ -40,9 +35,9 @@ UserInterface_callback::~UserInterface_callback() { // ::unregister_sync_callbacks(Adapter::session()); } -::PEP_STATUS UserInterface::_notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) { +::PEP_STATUS UserInterface::_notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal) { if (!(me && partner)) { - return PEP_ILLEGAL_VALUE; + return ::PEP_ILLEGAL_VALUE; } auto that = dynamic_cast< UserInterface_callback * >(_ui); @@ -104,7 +99,7 @@ void UserInterface::deliverHandshakeResult(int result, bp::object identities) { // } // i = 0; // } -// nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); +// nanosleep((const struct timespec[]){{0, 100000000L}}, nullptr); // } // // if (timeout) @@ -118,5 +113,5 @@ void UserInterface_callback::notifyHandshake(Identity me, Identity partner, sync } } // namespace PythonAdapter -} // namespace pEp { +} // namespace pEp diff --git a/src/pEp/_pEp/user_interface.hh b/src/pEp/_pEp/user_interface.hh index 5cd4269..f650aa4 100644 --- a/src/pEp/_pEp/user_interface.hh +++ b/src/pEp/_pEp/user_interface.hh @@ -4,16 +4,8 @@ #ifndef USER_INTERFACE_HH #define USER_INTERFACE_HH -// System -#include - -// Engine -#include -#include - -// local -#include "pEpmodule.hh" - +#include "adapter_main.hh" +#include "identity.hh" namespace pEp { namespace PythonAdapter { @@ -24,7 +16,7 @@ class UserInterface { virtual ~UserInterface(); - virtual void notifyHandshake(Identity me, Identity partner, sync_handshake_signal signal) { + virtual void notifyHandshake(Identity me, Identity partner, ::sync_handshake_signal signal) { throw runtime_error("override this method"); } @@ -33,7 +25,7 @@ class UserInterface { // 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 ::PEP_STATUS _notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal); }; class UserInterface_callback : public UserInterface {