Browse Source

Move common includes, namespace aliases etc... to adapter_main.hh/.cc,

const, nullptr,
REWORK
heck 5 years ago
parent
commit
19b2ebb18d
  1. 1
      setup.py
  2. 163
      src/pEp/_pEp/adapter_main.cc
  3. 84
      src/pEp/_pEp/adapter_main.hh
  4. 24
      src/pEp/_pEp/basic_api.cc
  5. 11
      src/pEp/_pEp/basic_api.hh
  6. 18
      src/pEp/_pEp/identity.cc
  7. 20
      src/pEp/_pEp/identity.hh
  8. 40
      src/pEp/_pEp/message.cc
  9. 18
      src/pEp/_pEp/message.hh
  10. 26
      src/pEp/_pEp/message_api.cc
  11. 3
      src/pEp/_pEp/message_api.hh
  12. 176
      src/pEp/_pEp/pEpmodule.cc
  13. 20
      src/pEp/_pEp/pEpmodule.hh
  14. 19
      src/pEp/_pEp/str_attr.cc
  15. 15
      src/pEp/_pEp/str_attr.hh
  16. 15
      src/pEp/_pEp/user_interface.cc
  17. 16
      src/pEp/_pEp/user_interface.hh

1
setup.py

@ -201,6 +201,7 @@ if sys.version_info[0] < 3:
module_pEp = Extension( module_pEp = Extension(
'pEp._pEp', 'pEp._pEp',
sources=[ sources=[
'src/pEp/_pEp/adapter_main.cc',
'src/pEp/_pEp/pEpmodule.cc', 'src/pEp/_pEp/pEpmodule.cc',
'src/pEp/_pEp/basic_api.cc', 'src/pEp/_pEp/basic_api.cc',
'src/pEp/_pEp/identity.cc', 'src/pEp/_pEp/identity.cc',

163
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<void>(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<void>(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<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);
}
} // namespace PythonAdapter
} // namespace pEp

84
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 <string>
#include <iomanip>
// Boost
#include <boost/python.hpp>
#include <boost/locale.hpp>
// Engine
#include <pEp/pEpEngine.h>
#include <pEp/keymanagement.h>
#include <pEp/identity_list.h>
#include <pEp/key_reset.h>
#include <pEp/sync_api.h>
#include <pEp/mime.h>
#include <pEp/message.h>
#include <pEp/message_api.h>
#include <pEp/sync_codec.h>
#include <pEp/distribution_codec.h>
#include <pEp/timestamp.h>
#include <pEp/stringpair.h>
// libpEpAdapter
#include <pEp/Adapter.hh>
#include <pEp/callback_dispatcher.hh>
#include <pEp/status_to_string.hh>
#include <pEp/pEpLog.hh>
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

24
src/pEp/_pEp/basic_api.cc

@ -1,20 +1,10 @@
// This file is under GNU Affero General Public License 3.0 // This file is under GNU Affero General Public License 3.0
// see LICENSE.txt // see LICENSE.txt
// System
#include <sstream>
// Engine
#include <pEp/keymanagement.h>
#include <pEp/message_api.h>
#include <pEp/Adapter.hh>
// local
#include "basic_api.hh" #include "basic_api.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std;
void update_identity(Identity &ident) { void update_identity(Identity &ident) {
if (ident.address() == "") { if (ident.address() == "") {
@ -48,7 +38,7 @@ string _trustwords(Identity me, Identity partner, string lang, bool full) {
if (lang == "" && me.lang() == partner.lang()) { if (lang == "" && me.lang() == partner.lang()) {
lang = me.lang(); lang = me.lang();
} }
char *words = NULL; char *words = nullptr;
size_t size = 0; size_t size = 0;
::PEP_STATUS status = ::get_trustwords(Adapter::session(), me, partner, lang.c_str(), &words, &size, full); ::PEP_STATUS status = ::get_trustwords(Adapter::session(), me, partner, lang.c_str(), &words, &size, full);
_throw_status(status); _throw_status(status);
@ -66,7 +56,7 @@ void trust_personal_key(Identity ident) {
_throw_status(status); _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() == "") { if (ident.address() == "") {
throw invalid_argument("address needed"); throw invalid_argument("address needed");
} }
@ -77,7 +67,7 @@ void set_identity_flags(Identity ident, ::identity_flags_t flags) {
_throw_status(status); _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() == "") { if (ident.address() == "") {
throw invalid_argument("address needed"); throw invalid_argument("address needed");
} }
@ -102,8 +92,8 @@ void key_reset_trust(Identity ident) {
_throw_status(status); _throw_status(status);
} }
bp::list import_key(string key_data) { bp::list import_key(const string &key_data) {
::identity_list *private_keys = NULL; ::identity_list *private_keys = nullptr;
::PEP_STATUS status = ::import_key(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) { if (status && status != ::PEP_KEY_IMPORTED) {
_throw_status(status); _throw_status(status);
@ -123,7 +113,7 @@ bp::list import_key(string key_data) {
string export_key(Identity ident) { string export_key(Identity ident) {
::PEP_STATUS status = ::PEP_STATUS_OK; ::PEP_STATUS status = ::PEP_STATUS_OK;
char *key_data = NULL; char *key_data = nullptr;
size_t size; size_t size;
status = ::export_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size); status = ::export_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size);
_throw_status(status); _throw_status(status);
@ -139,7 +129,7 @@ string export_secret_key(Identity ident) {
return key_data; return key_data;
} }
void set_own_key(Identity &ident, string fpr) { void set_own_key(Identity &ident, const string &fpr) {
if (ident.address() == "") { if (ident.address() == "") {
throw invalid_argument("address needed"); throw invalid_argument("address needed");
} }

11
src/pEp/_pEp/basic_api.hh

@ -4,12 +4,11 @@
#ifndef BASIC_API_HH #ifndef BASIC_API_HH
#define BASIC_API_HH #define BASIC_API_HH
#include "pEpmodule.hh" #include "adapter_main.hh"
#include "identity.hh" #include "identity.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
namespace bp = boost::python;
void update_identity(Identity &ident); 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 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); 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_key(Identity ident);
string export_secret_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 PythonAdapter */
} /* namespace pEp */ } /* namespace pEp */

18
src/pEp/_pEp/identity.cc

@ -1,25 +1,13 @@
// This file is under GNU Affero General Public License 3.0 // This file is under GNU Affero General Public License 3.0
// see LICENSE.txt // see LICENSE.txt
// System
#include <typeinfo>
#include <sstream>
// Engine
#include <pEp/identity_list.h>
#include <pEp/keymanagement.h>
#include <pEp/key_reset.h>
// local
#include "identity.hh" #include "identity.hh"
#include "pEpmodule.hh"
#include "basic_api.hh" #include "basic_api.hh"
#include "message_api.hh" #include "message_api.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { 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) 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) { : _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); 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); ::PEP_STATUS status = ::key_reset_identity(Adapter::session(), *this, fpr != "" ? fpr.c_str() : nullptr);
_throw_status(status); _throw_status(status);
} }
@ -232,7 +220,7 @@ bp::list identitylist_attr(::identity_list *&il) {
} }
void identitylist_attr(::identity_list *&il, bp::list value) { 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) { if (!_il) {
throw bad_alloc(); throw bad_alloc();
} }

20
src/pEp/_pEp/identity.hh

@ -5,29 +5,13 @@
#define IDENTITY_HH #define IDENTITY_HH
// System // System
#include <boost/python.hpp> #include "adapter_main.hh"
#include <string>
#include <memory>
#include <cstddef>
// Engine
#include <pEp/pEpEngine.h>
#include <pEp/message_api.h>
//libpEpAdapter
#include "pEp/Adapter.hh"
// local
#include "str_attr.hh" #include "str_attr.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using std::string;
using std::shared_ptr;
// Identity is owning a pEp_identity // Identity is owning a pEp_identity
class Identity { class Identity {
protected: protected:
shared_ptr<::pEp_identity> _ident; shared_ptr<::pEp_identity> _ident;
@ -87,7 +71,7 @@ class Identity {
virtual void update(); virtual void update();
void key_reset(string fpr = ""); void key_reset(const string &fpr = "");
void key_mistrusted(); void key_mistrusted();

40
src/pEp/_pEp/message.cc

@ -1,27 +1,13 @@
// This file is under GNU Affero General Public License 3.0 // This file is under GNU Affero General Public License 3.0
// see LICENSE.txt // see LICENSE.txt
// System
#include <cstdlib>
#include <cstring>
#include <stdexcept>
#include <sstream>
#include <vector>
#include <Python.h>
// Engine
#include <pEp/mime.h>
#include <pEp/keymanagement.h>
#include <pEp/message_api.h>
// local // local
#include "message.hh" #include "message.hh"
#include "message_api.hh" #include "message_api.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std;
namespace bp = boost::python;
Message::Blob::Blob(::bloblist_t *bl, bool chained) Message::Blob::Blob(::bloblist_t *bl, bool chained)
: _bl(bl), : _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) 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) { part_of_chain(false) {
if (!_bl) { if (!_bl) {
throw bad_alloc(); throw bad_alloc();
@ -96,20 +82,20 @@ string Message::Blob::_repr() {
} }
int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) {
::bloblist_t *bl = NULL; ::bloblist_t *bl = nullptr;
try { try {
Message::Blob &blob = bp::extract<Message::Blob &>(self); Message::Blob &blob = bp::extract<Message::Blob &>(self);
bl = blob._bl; bl = blob._bl;
} catch (exception &e) { } catch (exception &e) {
PyErr_SetString(PyExc_RuntimeError, "extract not possible"); PyErr_SetString(PyExc_RuntimeError, "extract not possible");
view->obj = NULL; view->obj = nullptr;
return -1; return -1;
} }
if (!(bl && bl->value)) { if (!(bl && bl->value)) {
PyErr_SetString(PyExc_RuntimeError, "no data available"); PyErr_SetString(PyExc_RuntimeError, "no data available");
view->obj = NULL; view->obj = nullptr;
return -1; return -1;
} }
@ -135,7 +121,7 @@ string Message::Blob::decode(string encoding) {
return bp::call<string>(_decode.ptr(), this, encoding); return bp::call<string>(_decode.ptr(), this, encoding);
} }
PyBufferProcs Message::Blob::bp = {getbuffer, NULL}; PyBufferProcs Message::Blob::bp = {getbuffer, nullptr};
Message::Message(int dir, Identity *from) Message::Message(int dir, Identity *from)
: _msg(new_message((::PEP_msg_direction)dir), &::free_message) { : _msg(new_message((::PEP_msg_direction)dir), &::free_message) {
@ -153,9 +139,9 @@ Message::Message(int dir, Identity *from)
} }
Message::Message(string mimetext) Message::Message(string mimetext)
: _msg(NULL, &::free_message) { : _msg(nullptr, &::free_message) {
message *_cpy; ::message *_cpy;
::PEP_STATUS status = ::mime_decode_message(mimetext.c_str(), mimetext.size(), &_cpy, NULL); ::PEP_STATUS status = ::mime_decode_message(mimetext.c_str(), mimetext.size(), &_cpy, nullptr);
switch (status) { switch (status) {
case ::PEP_STATUS_OK: case ::PEP_STATUS_OK:
if (_cpy) { if (_cpy) {
@ -257,7 +243,7 @@ bp::tuple Message::attachments() {
} }
void Message::attachments(bp::list value) { 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) { if (!bl) {
throw bad_alloc(); throw bad_alloc();
} }
@ -280,12 +266,12 @@ void Message::attachments(bp::list value) {
for (int i = 0; i < len(value); i++) { for (int i = 0; i < len(value); i++) {
Message::Blob &blob = bp::extract<Message::Blob &>(value[i]); Message::Blob &blob = bp::extract<Message::Blob &>(value[i]);
blob._bl->value = NULL; blob._bl->value = nullptr;
blob._bl->size = 0; blob._bl->size = 0;
free(blob._bl->mime_type); free(blob._bl->mime_type);
blob._bl->mime_type = NULL; blob._bl->mime_type = nullptr;
free(blob._bl->filename); free(blob._bl->filename);
blob._bl->filename = NULL; blob._bl->filename = nullptr;
} }
free_bloblist(_msg->attachments); free_bloblist(_msg->attachments);

18
src/pEp/_pEp/message.hh

@ -4,26 +4,14 @@
#ifndef MESSAGE_HH #ifndef MESSAGE_HH
#define MESSAGE_HH #define MESSAGE_HH
// System #include "adapter_main.hh"
#include <string>
#include <boost/python.hpp>
// Engine
#include <pEp/message.h>
#include <pEp/message_api.h>
// local
#include "str_attr.hh" #include "str_attr.hh"
#include "identity.hh" #include "identity.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using std::string;
using std::runtime_error;
using std::invalid_argument;
// Message is owning a message struct // Message is owning a message struct
class Message { class Message {
shared_ptr<::message> _msg; shared_ptr<::message> _msg;
@ -36,7 +24,7 @@ class Message {
bool part_of_chain; bool part_of_chain;
public: 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 = ""); 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); 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); Message(string mimetext);

26
src/pEp/_pEp/message_api.cc

@ -1,20 +1,12 @@
// This file is under GNU Affero General Public License 3.0 // This file is under GNU Affero General Public License 3.0
// see LICENSE.txt // see LICENSE.txt
// Engine
#include <pEp/pEpEngine.h>
#include <pEp/message_api.h>
#include <pEp/sync_api.h>
#include <pEp/sync_codec.h>
#include <pEp/distribution_codec.h>
// local // local
#include "message_api.hh" #include "message_api.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std;
namespace bp = boost::python;
Message encrypt_message(Message src, bp::list extra, int enc_format, int flags) { Message encrypt_message(Message src, bp::list extra, int enc_format, int flags) {
Identity _from = src.from(); 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); ::stringlist_t *_extra = to_stringlist(extra);
::PEP_enc_format _enc_format = (::PEP_enc_format)enc_format; ::PEP_enc_format _enc_format = (::PEP_enc_format)enc_format;
::PEP_encrypt_flags_t _flags = (::PEP_encrypt_flags_t)flags; ::PEP_encrypt_flags_t _flags = (::PEP_encrypt_flags_t)flags;
::message *_dst = NULL; ::message *_dst = nullptr;
::message *_src = src; ::message *_src = src;
::PEP_STATUS status = ::encrypt_message(Adapter::session(), _src, _extra, &_dst, _enc_format, _flags); ::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) { bp::tuple decrypt_message(Message src, int flags) {
::message *_dst = NULL; ::message *_dst = nullptr;
::stringlist_t *_keylist = NULL; ::stringlist_t *_keylist = nullptr;
::PEP_rating _rating = ::PEP_rating_undefined; ::PEP_rating _rating = ::PEP_rating_undefined;
::PEP_decrypt_flags_t _flags = (::PEP_decrypt_flags_t)flags; ::PEP_decrypt_flags_t _flags = (::PEP_decrypt_flags_t)flags;
::message *_src = src; ::message *_src = src;
@ -77,7 +69,7 @@ bp::tuple sync_decode(bp::object buffer) {
throw invalid_argument("need a contiguous buffer to read"); 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); ::PEP_STATUS status = ::PER_to_XER_Sync_msg((char *)src.buf, src.len, &dst);
PyBuffer_Release(&src); PyBuffer_Release(&src);
_throw_status(status); _throw_status(status);
@ -88,7 +80,7 @@ bp::tuple sync_decode(bp::object buffer) {
} }
static bp::tuple sync_encode(string text) { static bp::tuple sync_encode(string text) {
char *data = NULL; char *data = nullptr;
size_t size = 0; size_t size = 0;
::PEP_STATUS status = ::XER_to_PER_Sync_msg(text.c_str(), &data, &size); ::PEP_STATUS status = ::XER_to_PER_Sync_msg(text.c_str(), &data, &size);
_throw_status(status); _throw_status(status);
@ -109,7 +101,7 @@ bp::tuple Distribution_decode(bp::object buffer) {
throw invalid_argument("need a contiguous buffer to read"); 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); ::PEP_STATUS status = ::PER_to_XER_Distribution_msg((char *)src.buf, src.len, &dst);
PyBuffer_Release(&src); PyBuffer_Release(&src);
_throw_status(status); _throw_status(status);
@ -120,7 +112,7 @@ bp::tuple Distribution_decode(bp::object buffer) {
} }
static bp::tuple Distribution_encode(string text) { static bp::tuple Distribution_encode(string text) {
char *data = NULL; char *data = nullptr;
size_t size = 0; size_t size = 0;
::PEP_STATUS status = ::XER_to_PER_Distribution_msg(text.c_str(), &data, &size); ::PEP_STATUS status = ::XER_to_PER_Distribution_msg(text.c_str(), &data, &size);
_throw_status(status); _throw_status(status);
@ -163,4 +155,4 @@ bp::object distribution_search(string name) {
} }
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp { } // namespace pEp

3
src/pEp/_pEp/message_api.hh

@ -4,7 +4,8 @@
#ifndef MESSAGE_API_HH #ifndef MESSAGE_API_HH
#define MESSAGE_API_HH #define MESSAGE_API_HH
#include "pEpmodule.hh" #include "adapter_main.hh"
#include "message.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {

176
src/pEp/_pEp/pEpmodule.cc

@ -1,186 +1,16 @@
// This file is under GNU Affero General Public License 3.0 // This file is under GNU Affero General Public License 3.0
// see LICENSE.txt // see LICENSE.txt
// System
#include <boost/python.hpp>
#include <boost/locale.hpp>
#include <string>
#include <sstream>
#include <iomanip>
// Engine
#include <pEp/key_reset.h>
#include <pEp/message_api.h>
#include <pEp/sync_api.h>
#include <pEp/status_to_string.h>
// libpEpAdapter
#include <pEp/Adapter.hh>
#include <pEp/callback_dispatcher.hh>
#include <pEp/pEpLog.hh>
// local // local
#include "pEpmodule.hh" #include "adapter_main.hh"
#include "basic_api.hh" #include "basic_api.hh"
#include "message.hh"
#include "message_api.hh" #include "message_api.hh"
//#include "user_interface.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { 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<void>(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<void>(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<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(_pEp) { BOOST_PYTHON_MODULE(_pEp) {
using boost::python::def; using boost::python::def;
@ -367,7 +197,7 @@ BOOST_PYTHON_MODULE(_pEp) {
.add_property("dir", (int(Message::*)()) .add_property("dir", (int(Message::*)())
(PEP_msg_direction(Message::*)()) &Message::dir, (PEP_msg_direction(Message::*)()) &Message::dir,
(void(Message::*)(int)) (void(Message::*)(int))
(void(Message::*)(PEP_msg_direction)) &Message::dir, (void(Message::*)(::PEP_msg_direction)) &Message::dir,
"0: incoming, 1: outgoing message") "0: incoming, 1: outgoing message")
.add_property("id", (string(Message::*)()) &Message::id, .add_property("id", (string(Message::*)()) &Message::id,
(void(Message::*)(string)) &Message::id, (void(Message::*)(string)) &Message::id,

20
src/pEp/_pEp/pEpmodule.hh

@ -4,30 +4,10 @@
#ifndef PEPMODULE_HH #ifndef PEPMODULE_HH
#define PEPMODULE_HH #define PEPMODULE_HH
// Engine
#include <pEp/pEpEngine.h>
// local
#include "message.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { 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 PythonAdapter */
} /* namespace pEp */ } /* namespace pEp */

19
src/pEp/_pEp/str_attr.cc

@ -1,25 +1,18 @@
// This file is under GNU Affero General Public License 3.0 // This file is under GNU Affero General Public License 3.0
// see LICENSE.txt // see LICENSE.txt
// System
#include <cstdlib>
#include <boost/python.hpp>
#include <boost/locale.hpp>
// local // local
#include "str_attr.hh" #include "str_attr.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std;
namespace bp = boost::python;
namespace bl = boost::locale;
bp::object repr(bp::object s) { bp::object repr(bp::object s) {
return s.attr("__repr__")(); return s.attr("__repr__")();
} }
string repr(string s) { string repr(const string &s) {
bp::str _s = s.c_str(); bp::str _s = s.c_str();
bp::object _r = _s.attr("__repr__")(); bp::object _r = _s.attr("__repr__")();
string r = bp::extract<string>(_r); string r = bp::extract<string>(_r);
@ -33,7 +26,7 @@ string str_attr(char *&str) {
return string(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); string normalized = normalize(value, bl::norm_nfc);
free(str); free(str);
str = strdup(normalized.c_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 strlist_attr(::stringlist_t *&sl) {
bp::list result; 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); string s(_sl->value);
result.append(bp::object(s)); 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) { 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) { if (!_spl) {
throw bad_alloc(); 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 *to_stringlist(bp::list l) {
::stringlist_t *result = ::new_stringlist(NULL); ::stringlist_t *result = ::new_stringlist(nullptr);
if (!result) { if (!result) {
throw bad_alloc(); throw bad_alloc();
} }

15
src/pEp/_pEp/str_attr.hh

@ -4,27 +4,18 @@
#ifndef STR_ATTR_HH #ifndef STR_ATTR_HH
#define STR_ATTR_HH #define STR_ATTR_HH
// System #include "adapter_main.hh"
#include <string>
// Engine
#include <pEp/pEpEngine.h>
#include <pEp/timestamp.h>
#include <pEp/stringlist.h>
#include <pEp/stringpair.h>
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using std::string;
namespace bp = boost::python;
bp::object repr(bp::object s); bp::object repr(bp::object s);
string repr(string s); string repr(const string &s);
string str_attr(char *&str); 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); time_t timestamp_attr(::timestamp *&ts);

15
src/pEp/_pEp/user_interface.cc

@ -1,16 +1,11 @@
// This file is under GNU Affero General Public License 3.0 // This file is under GNU Affero General Public License 3.0
// see LICENSE.txt // see LICENSE.txt
// System
#include <cassert>
// local
#include "user_interface.hh" #include "user_interface.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std;
//namespace bp = boost::python;
UserInterface *UserInterface::_ui = nullptr; UserInterface *UserInterface::_ui = nullptr;
@ -40,9 +35,9 @@ UserInterface_callback::~UserInterface_callback() {
// ::unregister_sync_callbacks(Adapter::session()); // ::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)) { if (!(me && partner)) {
return PEP_ILLEGAL_VALUE; return ::PEP_ILLEGAL_VALUE;
} }
auto that = dynamic_cast< UserInterface_callback * >(_ui); auto that = dynamic_cast< UserInterface_callback * >(_ui);
@ -104,7 +99,7 @@ void UserInterface::deliverHandshakeResult(int result, bp::object identities) {
// } // }
// i = 0; // i = 0;
// } // }
// nanosleep((const struct timespec[]){{0, 100000000L}}, NULL); // nanosleep((const struct timespec[]){{0, 100000000L}}, nullptr);
// } // }
// //
// if (timeout) // if (timeout)
@ -118,5 +113,5 @@ void UserInterface_callback::notifyHandshake(Identity me, Identity partner, sync
} }
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp { } // namespace pEp

16
src/pEp/_pEp/user_interface.hh

@ -4,16 +4,8 @@
#ifndef USER_INTERFACE_HH #ifndef USER_INTERFACE_HH
#define USER_INTERFACE_HH #define USER_INTERFACE_HH
// System #include "adapter_main.hh"
#include <csetjmp> #include "identity.hh"
// Engine
#include <pEp/sync_api.h>
#include <pEp/message_api.h>
// local
#include "pEpmodule.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
@ -24,7 +16,7 @@ class UserInterface {
virtual ~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"); throw runtime_error("override this method");
} }
@ -33,7 +25,7 @@ class UserInterface {
// 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: 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 { class UserInterface_callback : public UserInterface {

Loading…
Cancel
Save