Browse Source

Includes/namespace/using cleanup

PYADPT-81
heck 5 years ago
parent
commit
50ebe60221
  1. 2
      src/pEp/__init__.py
  2. 78
      src/pEp/native_pEp/basic_api.cc
  3. 36
      src/pEp/native_pEp/basic_api.hh
  4. 172
      src/pEp/native_pEp/identity.cc
  5. 41
      src/pEp/native_pEp/identity.hh
  6. 185
      src/pEp/native_pEp/message.cc
  7. 31
      src/pEp/native_pEp/message.hh
  8. 67
      src/pEp/native_pEp/message_api.cc
  9. 25
      src/pEp/native_pEp/message_api.hh
  10. 231
      src/pEp/native_pEp/pEpmodule.cc
  11. 29
      src/pEp/native_pEp/pEpmodule.hh
  12. 86
      src/pEp/native_pEp/str_attr.cc
  13. 38
      src/pEp/native_pEp/str_attr.hh
  14. 65
      src/pEp/native_pEp/user_interface.cc
  15. 41
      src/pEp/native_pEp/user_interface.hh
  16. 1
      test/pyadpt-81.py

2
src/pEp/__init__.py

@ -17,7 +17,7 @@ import native_pEp
# Executed on module import # Executed on module import
def init(): def init():
print("init() called") print("init() called")
# native_pEp._init() native_pEp._init()
# Executed when run as script # Executed when run as script
def main(): def main():

78
src/pEp/native_pEp/basic_api.cc

@ -1,18 +1,23 @@
// 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> #include <sstream>
// Engine
#include <pEp/keymanagement.h> #include <pEp/keymanagement.h>
#include <pEp/message_api.h> #include <pEp/message_api.h>
#include <pEp/Adapter.hh> #include <pEp/Adapter.hh>
// local
#include "basic_api.hh" #include "basic_api.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
void update_identity(Identity& ident) using namespace std;
{
void update_identity(Identity& ident)
{
if (ident.address() == "") if (ident.address() == "")
throw invalid_argument("address needed"); throw invalid_argument("address needed");
if (ident.user_id() == PEP_OWN_USERID) if (ident.user_id() == PEP_OWN_USERID)
@ -21,10 +26,10 @@ namespace pEp {
PEP_STATUS status = update_identity(pEp::Adapter::session(), ident); PEP_STATUS status = update_identity(pEp::Adapter::session(), ident);
_throw_status(status); _throw_status(status);
} }
void myself(Identity& ident) void myself(Identity& ident)
{ {
if (ident.address() == "") if (ident.address() == "")
throw invalid_argument("address needed"); throw invalid_argument("address needed");
if (ident.username() == "") if (ident.username() == "")
@ -35,10 +40,10 @@ namespace pEp {
PEP_STATUS status = myself(pEp::Adapter::session(), ident); PEP_STATUS status = myself(pEp::Adapter::session(), ident);
_throw_status(status); _throw_status(status);
} }
string _trustwords(Identity me, Identity partner, string lang, bool full) string _trustwords(Identity me, Identity partner, string lang, bool full)
{ {
if (me.fpr() == "" || partner.fpr() == "") if (me.fpr() == "" || partner.fpr() == "")
throw invalid_argument("fingerprint needed in Identities"); throw invalid_argument("fingerprint needed in Identities");
@ -51,10 +56,10 @@ namespace pEp {
lang.c_str(),&words, &size, full); lang.c_str(),&words, &size, full);
_throw_status(status); _throw_status(status);
return words; return words;
} }
void trust_personal_key(Identity ident) void trust_personal_key(Identity ident)
{ {
if (ident.fpr() == "") if (ident.fpr() == "")
throw invalid_argument("fingerprint needed in Identities"); throw invalid_argument("fingerprint needed in Identities");
if (ident.user_id() == "") if (ident.user_id() == "")
@ -62,10 +67,10 @@ namespace pEp {
PEP_STATUS status = trust_personal_key(pEp::Adapter::session(), ident); PEP_STATUS status = trust_personal_key(pEp::Adapter::session(), ident);
_throw_status(status); _throw_status(status);
} }
void set_identity_flags(Identity ident, identity_flags_t flags) void set_identity_flags(Identity ident, identity_flags_t flags)
{ {
if (ident.address() == "") if (ident.address() == "")
throw invalid_argument("address needed"); throw invalid_argument("address needed");
if (ident.user_id() == "") if (ident.user_id() == "")
@ -73,10 +78,10 @@ namespace pEp {
PEP_STATUS status = set_identity_flags(pEp::Adapter::session(), ident, flags); PEP_STATUS status = set_identity_flags(pEp::Adapter::session(), ident, flags);
_throw_status(status); _throw_status(status);
} }
void unset_identity_flags(Identity ident, identity_flags_t flags) void unset_identity_flags(Identity ident, identity_flags_t flags)
{ {
if (ident.address() == "") if (ident.address() == "")
throw invalid_argument("address needed"); throw invalid_argument("address needed");
if (ident.user_id() == "") if (ident.user_id() == "")
@ -84,10 +89,10 @@ namespace pEp {
PEP_STATUS status = unset_identity_flags(pEp::Adapter::session(), ident, flags); PEP_STATUS status = unset_identity_flags(pEp::Adapter::session(), ident, flags);
_throw_status(status); _throw_status(status);
} }
void key_reset_trust(Identity ident) void key_reset_trust(Identity ident)
{ {
if (ident.fpr() == "") if (ident.fpr() == "")
throw invalid_argument("fpr needed"); throw invalid_argument("fpr needed");
if (ident.address() == "") if (ident.address() == "")
@ -97,12 +102,12 @@ namespace pEp {
PEP_STATUS status = key_reset_trust(pEp::Adapter::session(), ident); PEP_STATUS status = key_reset_trust(pEp::Adapter::session(), ident);
_throw_status(status); _throw_status(status);
} }
boost::python::list import_key(string key_data) boost::python::list import_key(string key_data)
{ {
::identity_list *private_keys = NULL; ::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(pEp::Adapter::session(), key_data.c_str(), key_data.size(), &private_keys);
if (status && status != PEP_KEY_IMPORTED) if (status && status != PEP_KEY_IMPORTED)
@ -120,10 +125,10 @@ namespace pEp {
free_identity_list(private_keys); free_identity_list(private_keys);
return result; return result;
} }
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 = NULL;
size_t size; size_t size;
@ -131,10 +136,10 @@ namespace pEp {
_throw_status(status); _throw_status(status);
return key_data; return key_data;
} }
string export_secret_key(Identity ident) string export_secret_key(Identity ident)
{ {
PEP_STATUS status = PEP_STATUS_OK; PEP_STATUS status = PEP_STATUS_OK;
char* key_data = NULL; char* key_data = NULL;
size_t size; size_t size;
@ -142,10 +147,10 @@ namespace pEp {
_throw_status(status); _throw_status(status);
return key_data; return key_data;
} }
void set_own_key(Identity& ident, string fpr) void set_own_key(Identity& ident, string fpr)
{ {
if (ident.address() == "") if (ident.address() == "")
throw invalid_argument("address needed"); throw invalid_argument("address needed");
if (ident.username() == "") if (ident.username() == "")
@ -159,6 +164,9 @@ namespace pEp {
const char* fpr_c = fpr.c_str(); 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(pEp::Adapter::session(), ident, fpr_c);
_throw_status(status); _throw_status(status);
}
}
} }
} // namespace PythonAdapter
} // namespace pEp {

36
src/pEp/native_pEp/basic_api.hh

@ -6,20 +6,22 @@
#include "pEpmodule.hh" #include "pEpmodule.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
void update_identity(Identity& ident);
void myself(Identity& ident); void update_identity(Identity& ident);
string _trustwords(Identity me, Identity partner, string lang, bool full); void myself(Identity& ident);
void trust_personal_key(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 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);
void key_reset_trust(Identity ident);
boost::python::list import_key(string key_data);
string export_key(Identity ident); boost::python::list import_key(string key_data);
string export_secret_key(Identity ident); string export_key(Identity ident);
void set_own_key(Identity& ident, string fpr); string export_secret_key(Identity ident);
} void set_own_key(Identity& ident, string fpr);
}
} // namespace PythonAdapter
} // namespace pEp {

172
src/pEp/native_pEp/identity.cc

@ -1,62 +1,67 @@
// 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 <typeinfo>
#include <sstream> #include <sstream>
// Engine
#include <pEp/identity_list.h> #include <pEp/identity_list.h>
#include <pEp/keymanagement.h> #include <pEp/keymanagement.h>
#include <pEp/key_reset.h> #include <pEp/key_reset.h>
// local
#include "identity.hh" #include "identity.hh"
#include "pEpmodule.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;
using namespace boost::python;
Identity::Identity(string address, string username, string user_id, Identity::Identity(string address, string username, string user_id,
string fpr, int comm_type, string lang, identity_flags_t flags) string fpr, int comm_type, string lang, identity_flags_t flags)
: _ident(new_identity(address.c_str(), fpr.c_str(), user_id.c_str(), : _ident(new_identity(address.c_str(), fpr.c_str(), user_id.c_str(),
username.c_str()), &::free_identity) username.c_str()), &::free_identity)
{ {
if (!_ident) if (!_ident)
throw bad_alloc(); throw bad_alloc();
_ident->comm_type = (PEP_comm_type) comm_type; _ident->comm_type = (PEP_comm_type) comm_type;
_ident->flags = (identity_flags_t) flags; _ident->flags = (identity_flags_t) flags;
this->lang(lang); this->lang(lang);
} }
Identity::Identity(const Identity& second) Identity::Identity(const Identity& second)
: _ident(second._ident) : _ident(second._ident)
{ {
} }
Identity::Identity(pEp_identity *ident) Identity::Identity(pEp_identity *ident)
: _ident(ident, &::free_identity) : _ident(ident, &::free_identity)
{ {
} }
Identity::~Identity() Identity::~Identity()
{ {
} }
Identity::operator pEp_identity *() Identity::operator pEp_identity *()
{ {
return _ident.get(); return _ident.get();
} }
Identity::operator const pEp_identity *() const Identity::operator const pEp_identity *() const
{ {
return _ident.get(); return _ident.get();
} }
string Identity::_repr() string Identity::_repr()
{ {
stringstream build; stringstream build;
build << "Identity("; build << "Identity(";
string address; string address;
@ -79,42 +84,42 @@ namespace pEp {
string lang = _ident->lang; string lang = _ident->lang;
build << repr(lang) << ")"; build << repr(lang) << ")";
return build.str(); return build.str();
} }
string Identity::_str() string Identity::_str()
{ {
if (!(_ident->address && _ident->address[0])) if (!(_ident->address && _ident->address[0]))
return ""; return "";
if (!(_ident->username && _ident->username[0])) if (!(_ident->username && _ident->username[0]))
return _ident->address; return _ident->address;
return string(_ident->username) + " <" + _ident->address + ">"; return string(_ident->username) + " <" + _ident->address + ">";
} }
void Identity::username(string value) void Identity::username(string value)
{ {
if (value.length() && value.length() < 5) if (value.length() && value.length() < 5)
throw length_error("username must be at least 5 characters"); 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) void Identity::lang(string value)
{ {
if (value == "") if (value == "")
memset(_ident->lang, 0, 3); memset(_ident->lang, 0, 3);
else if (value.length() != 2) else if (value.length() != 2)
throw length_error("length of lang must be 2"); throw length_error("length of lang must be 2");
else else
memcpy(_ident->lang, value.c_str(), 3); memcpy(_ident->lang, value.c_str(), 3);
} }
string Identity::lang() string Identity::lang()
{ {
return _ident->lang; return _ident->lang;
} }
int Identity::rating() int Identity::rating()
{ {
if (!(_ident->address)) if (!(_ident->address))
throw invalid_argument("address must be given"); throw invalid_argument("address must be given");
@ -123,69 +128,69 @@ namespace pEp {
_throw_status(status); _throw_status(status);
return (int) rating; return (int) rating;
} }
PEP_color Identity::color() PEP_color Identity::color()
{ {
return _color(rating()); return _color(rating());
} }
Identity Identity::copy() Identity Identity::copy()
{ {
pEp_identity *dup = ::identity_dup(*this); pEp_identity *dup = ::identity_dup(*this);
if (!dup) if (!dup)
throw bad_alloc(); throw bad_alloc();
return Identity(dup); return Identity(dup);
} }
Identity Identity::deepcopy(dict&) Identity Identity::deepcopy(dict&)
{ {
return copy(); return copy();
} }
void Identity::update() void Identity::update()
{ {
update_identity(*this); update_identity(*this);
} }
void Identity::key_reset(string fpr) void Identity::key_reset(string fpr)
{ {
PEP_STATUS status = ::key_reset_identity(pEp::Adapter::session(), *this, PEP_STATUS status = ::key_reset_identity(pEp::Adapter::session(), *this,
fpr != "" ? fpr.c_str() : nullptr); fpr != "" ? fpr.c_str() : nullptr);
_throw_status(status); _throw_status(status);
} }
void Identity::key_mistrusted() void Identity::key_mistrusted()
{ {
PEP_STATUS status = ::key_mistrusted(pEp::Adapter::session(), *this); PEP_STATUS status = ::key_mistrusted(pEp::Adapter::session(), *this);
_throw_status(status); _throw_status(status);
} }
bool Identity::is_pEp_user() bool Identity::is_pEp_user()
{ {
bool result; bool result;
PEP_STATUS status = ::is_pEp_user(pEp::Adapter::session(), *this, &result); PEP_STATUS status = ::is_pEp_user(pEp::Adapter::session(), *this, &result);
_throw_status(status); _throw_status(status);
return result; return result;
} }
void Identity::enable_for_sync() void Identity::enable_for_sync()
{ {
PEP_STATUS status = ::enable_identity_for_sync(pEp::Adapter::session(), *this); PEP_STATUS status = ::enable_identity_for_sync(pEp::Adapter::session(), *this);
_throw_status(status); _throw_status(status);
} }
void Identity::disable_for_sync() void Identity::disable_for_sync()
{ {
PEP_STATUS status = ::disable_identity_for_sync(pEp::Adapter::session(), *this); PEP_STATUS status = ::disable_identity_for_sync(pEp::Adapter::session(), *this);
_throw_status(status); _throw_status(status);
} }
Myself::Myself(string address, string username, string user_id, string lang) Myself::Myself(string address, string username, string user_id, string lang)
: Identity(address, username, user_id, "", 0, lang) : Identity(address, username, user_id, "", 0, lang)
{ {
if (!(address.length() && username.length())) if (!(address.length() && username.length()))
throw invalid_argument("address and username must be set"); throw invalid_argument("address and username must be set");
if (lang.length() && lang.length() != 2) if (lang.length() && lang.length() != 2)
@ -195,15 +200,15 @@ namespace pEp {
// _ident->me = true; // _ident->me = true;
if (user_id.length()) if (user_id.length())
throw runtime_error("user_id feature not yet implemented for Myself"); throw runtime_error("user_id feature not yet implemented for Myself");
} }
void Myself::update() void Myself::update()
{ {
pEp::PythonAdapter::myself(*this); pEp::PythonAdapter::myself(*this);
} }
Identity identity_attr(pEp_identity *&ident) Identity identity_attr(pEp_identity *&ident)
{ {
if (!ident) if (!ident)
throw out_of_range("no identity assigned"); throw out_of_range("no identity assigned");
@ -213,10 +218,10 @@ namespace pEp {
Identity _ident(_dup); Identity _ident(_dup);
return _ident; return _ident;
} }
void identity_attr(pEp_identity *&ident, object value) void identity_attr(pEp_identity *&ident, object value)
{ {
Identity& _ident = extract< Identity& >(value); Identity& _ident = extract< Identity& >(value);
pEp_identity *_dup = ::identity_dup(_ident); pEp_identity *_dup = ::identity_dup(_ident);
if (!_dup) if (!_dup)
@ -225,10 +230,10 @@ namespace pEp {
_throw_status(status); _throw_status(status);
free_identity(ident); free_identity(ident);
ident = _dup; ident = _dup;
} }
boost::python::list identitylist_attr(identity_list *&il) boost::python::list identitylist_attr(identity_list *&il)
{ {
boost::python::list result; boost::python::list result;
for (identity_list *_il = il; _il && _il->ident; _il = _il->next) { for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
@ -239,10 +244,10 @@ namespace pEp {
} }
return result; return result;
} }
void identitylist_attr(identity_list *&il, boost::python::list value) void identitylist_attr(identity_list *&il, boost::python::list value)
{ {
identity_list *_il = new_identity_list(NULL); identity_list *_il = new_identity_list(NULL);
if (!_il) if (!_il)
throw bad_alloc(); throw bad_alloc();
@ -273,7 +278,8 @@ namespace pEp {
free_identity_list(il); free_identity_list(il);
il = _il; il = _il;
}
}
} }
} // namespace PythonAdapter
} // namespace pEp {

41
src/pEp/native_pEp/identity.hh

@ -3,29 +3,35 @@
#pragma once #pragma once
// System
#include <boost/python.hpp> #include <boost/python.hpp>
#include <string> #include <string>
#include <memory> #include <memory>
#include <cstddef> #include <cstddef>
// Engine
#include <pEp/pEpEngine.h> #include <pEp/pEpEngine.h>
#include <pEp/message_api.h> #include <pEp/message_api.h>
//libpEpAdapter
#include "pEp/Adapter.hh" #include "pEp/Adapter.hh"
// local
#include "str_attr.hh" #include "str_attr.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace utility;
using namespace std; 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;
public: public:
Identity(string address = "", string username = "", Identity(string address = "", string username = "",
string user_id = "", string fpr = "", int comm_type = 0, string user_id = "", string fpr = "", int comm_type = 0,
string lang = "", identity_flags_t flags = 0); string lang = "", identity_flags_t flags = 0);
@ -75,19 +81,20 @@ namespace pEp {
void enable_for_sync(); void enable_for_sync();
void disable_for_sync(); void disable_for_sync();
}; };
class Myself : public Identity { class Myself : public Identity {
public: public:
Myself(string address, string username, string user_id="", string lang=""); Myself(string address, string username, string user_id="", string lang="");
virtual void update(); virtual void update();
}; };
Identity identity_attr(pEp_identity *&ident);
void identity_attr(pEp_identity *&ident, object value);
Identity identity_attr(pEp_identity *&ident); boost::python::list identitylist_attr(identity_list *&il);
void identity_attr(pEp_identity *&ident, object value); void identitylist_attr(identity_list *&il, boost::python::list value);
boost::python::list identitylist_attr(identity_list *&il); } // namespace PythonAdapter
void identitylist_attr(identity_list *&il, boost::python::list value); } // namespace pEp {
}
}

185
src/pEp/native_pEp/message.cc

@ -1,34 +1,38 @@
// 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
#include <Python.h> // System
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <stdexcept> #include <stdexcept>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <Python.h>
// Engine
#include <pEp/mime.h> #include <pEp/mime.h>
#include <pEp/keymanagement.h> #include <pEp/keymanagement.h>
#include <pEp/message_api.h> #include <pEp/message_api.h>
// 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; using namespace std;
using namespace boost::python;
Message::Blob::Blob(bloblist_t *bl, bool chained) : Message::Blob::Blob(bloblist_t *bl, bool chained) :
_bl(bl), part_of_chain(chained) _bl(bl), part_of_chain(chained)
{ {
if (!_bl) if (!_bl)
throw bad_alloc(); throw bad_alloc();
} }
Message::Blob::Blob(object data, string mime_type, string filename) : Message::Blob::Blob(object data, string mime_type, string filename) :
_bl(new_bloblist(NULL, 0, NULL, NULL)), part_of_chain(false) _bl(new_bloblist(NULL, 0, NULL, NULL)), part_of_chain(false)
{ {
if (!_bl) if (!_bl)
throw bad_alloc(); throw bad_alloc();
@ -52,24 +56,24 @@ namespace pEp {
this->mime_type(mime_type); this->mime_type(mime_type);
this->filename(filename); this->filename(filename);
} }
Message::Blob::Blob(const Message::Blob& second) : Message::Blob::Blob(const Message::Blob& second) :
_bl(second._bl), part_of_chain(true) _bl(second._bl), part_of_chain(true)
{ {
} }
Message::Blob::~Blob() Message::Blob::~Blob()
{ {
if (!part_of_chain) { if (!part_of_chain) {
free(_bl->value); free(_bl->value);
free(_bl); free(_bl);
} }
} }
string Message::Blob::_repr() string Message::Blob::_repr()
{ {
stringstream build; stringstream build;
build << "Blob("; build << "Blob(";
if (!_bl) { if (!_bl) {
@ -88,9 +92,9 @@ namespace pEp {
} }
build << ")"; build << ")";
return build.str(); return build.str();
} }
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 = NULL;
try { try {
@ -110,10 +114,10 @@ namespace pEp {
} }
return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags); return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags);
} }
string Message::Blob::decode(string encoding) string Message::Blob::decode(string encoding)
{ {
if (encoding == "") { if (encoding == "") {
string _mime_type = _bl->mime_type ? _bl->mime_type : ""; string _mime_type = _bl->mime_type ? _bl->mime_type : "";
encoding = "ascii"; encoding = "ascii";
@ -128,13 +132,13 @@ namespace pEp {
object codecs = import("codecs"); object codecs = import("codecs");
object _decode = codecs.attr("decode"); object _decode = codecs.attr("decode");
return call< string >(_decode.ptr(), this, encoding); return call< string >(_decode.ptr(), this, encoding);
} }
PyBufferProcs Message::Blob::bp = { getbuffer, NULL }; PyBufferProcs Message::Blob::bp = { getbuffer, NULL };
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)
{ {
if (!_msg) if (!_msg)
throw bad_alloc(); throw bad_alloc();
@ -144,11 +148,11 @@ namespace pEp {
throw bad_alloc(); throw bad_alloc();
_msg->dir = (PEP_msg_direction) dir; _msg->dir = (PEP_msg_direction) dir;
} }
} }
Message::Message(string mimetext) Message::Message(string mimetext)
: _msg(NULL, &free_message) : _msg(NULL, &free_message)
{ {
message *_cpy; message *_cpy;
PEP_STATUS status = mime_decode_message(mimetext.c_str(), PEP_STATUS status = mime_decode_message(mimetext.c_str(),
mimetext.size(), &_cpy, NULL); mimetext.size(), &_cpy, NULL);
@ -179,38 +183,38 @@ namespace pEp {
build << "mime_decode_message: unknown error (" << (int) status << ")"; build << "mime_decode_message: unknown error (" << (int) status << ")";
throw runtime_error(build.str()); throw runtime_error(build.str());
} }
} }
Message::Message(const Message& second) Message::Message(const Message& second)
: _msg(second._msg) : _msg(second._msg)
{ {
if (!_msg.get()) if (!_msg.get())
throw bad_alloc(); throw bad_alloc();
} }
Message::Message(message *msg) Message::Message(message *msg)
: _msg(::message_dup(msg), &free_message) : _msg(::message_dup(msg), &free_message)
{ {
} }
Message::~Message() Message::~Message()
{ {
} }
Message::operator message *() Message::operator message *()
{ {
return _msg.get(); return _msg.get();
} }
Message::operator const message *() const Message::operator const message *() const
{ {
return _msg.get(); return _msg.get();
} }
string Message::_str() string Message::_str()
{ {
if (!(_msg->from && _msg->from->address && _msg->from->address[0])) if (!(_msg->from && _msg->from->address && _msg->from->address[0]))
throw out_of_range(".from_.address missing"); throw out_of_range(".from_.address missing");
@ -240,17 +244,17 @@ namespace pEp {
} }
return result; return result;
} }
string Message::_repr() string Message::_repr()
{ {
stringstream build; stringstream build;
build << "Message(" << repr(_str()) << ")"; build << "Message(" << repr(_str()) << ")";
return build.str(); return build.str();
} }
boost::python::tuple Message::attachments() boost::python::tuple Message::attachments()
{ {
boost::python::list l; boost::python::list l;
for (bloblist_t *bl = _msg->attachments; bl && bl->value; bl = for (bloblist_t *bl = _msg->attachments; bl && bl->value; bl =
@ -259,10 +263,10 @@ namespace pEp {
} }
return boost::python::tuple(l); return boost::python::tuple(l);
} }
void Message::attachments(boost::python::list value) void Message::attachments(boost::python::list value)
{ {
bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL); bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
if (!bl) if (!bl)
throw bad_alloc(); throw bad_alloc();
@ -296,27 +300,27 @@ namespace pEp {
free_bloblist(_msg->attachments); free_bloblist(_msg->attachments);
_msg->attachments = bl; _msg->attachments = bl;
} }
Message Message::encrypt() Message Message::encrypt()
{ {
boost::python::list extra; boost::python::list extra;
return encrypt_message(*this, extra, PEP_enc_PGP_MIME, 0); return encrypt_message(*this, extra, PEP_enc_PGP_MIME, 0);
} }
Message Message::_encrypt(boost::python::list extra, int enc_format, int flags) Message Message::_encrypt(boost::python::list extra, int enc_format, int flags)
{ {
if (!enc_format) if (!enc_format)
enc_format = PEP_enc_PGP_MIME; enc_format = PEP_enc_PGP_MIME;
return encrypt_message(*this, extra, enc_format, flags); return encrypt_message(*this, extra, enc_format, flags);
} }
boost::python::tuple Message::decrypt(int flags) { boost::python::tuple Message::decrypt(int flags) {
return pEp::PythonAdapter::decrypt_message(*this, flags); return pEp::PythonAdapter::decrypt_message(*this, flags);
} }
PEP_rating Message::outgoing_rating() PEP_rating Message::outgoing_rating()
{ {
if (_msg->dir != PEP_dir_outgoing) if (_msg->dir != PEP_dir_outgoing)
throw invalid_argument("Message.dir must be outgoing"); throw invalid_argument("Message.dir must be outgoing");
@ -336,55 +340,55 @@ namespace pEp {
_throw_status(status); _throw_status(status);
return rating; return rating;
} }
PEP_color Message::outgoing_color() PEP_color Message::outgoing_color()
{ {
return _color(outgoing_rating()); return _color(outgoing_rating());
} }
Message Message::copy() Message Message::copy()
{ {
message *dup = message_dup(*this); message *dup = message_dup(*this);
if (!dup) if (!dup)
throw bad_alloc(); throw bad_alloc();
return Message(dup); return Message(dup);
} }
Message Message::deepcopy(dict&) Message Message::deepcopy(dict&)
{ {
return copy(); return copy();
} }
Message outgoing_message(Identity me) Message outgoing_message(Identity me)
{ {
if (me.address().empty() || me.user_id().empty()) if (me.address().empty() || me.user_id().empty())
throw runtime_error("at least address and user_id of own user needed"); throw runtime_error("at least address and user_id of own user needed");
::myself(pEp::Adapter::session(), me); ::myself(pEp::Adapter::session(), me);
auto m = Message(PEP_dir_outgoing, &me); auto m = Message(PEP_dir_outgoing, &me);
return m; return m;
} }
static object update(Identity ident) static object update(Identity ident)
{ {
if (ident.address().empty()) if (ident.address().empty())
throw runtime_error("at least address needed"); throw runtime_error("at least address needed");
update_identity(pEp::Adapter::session(), ident); update_identity(pEp::Adapter::session(), ident);
return object(ident); return object(ident);
} }
static boost::python::list update(boost::python::list il) static boost::python::list update(boost::python::list il)
{ {
for (int i=0; i<len(il); i++) { for (int i=0; i<len(il); i++) {
update(extract< Identity >(il[i])); update(extract< Identity >(il[i]));
} }
return il; return il;
} }
Message incoming_message(string mime_text) Message incoming_message(string mime_text)
{ {
auto m = Message(mime_text); auto m = Message(mime_text);
m.dir(PEP_dir_incoming); m.dir(PEP_dir_incoming);
@ -403,6 +407,7 @@ namespace pEp {
m.reply_to(update(m.reply_to())); m.reply_to(update(m.reply_to()));
return m; return m;
}
}
} }
} // namespace PythonAdapter
} // namespace pEp {

31
src/pEp/native_pEp/message.hh

@ -3,28 +3,32 @@
#pragma once #pragma once
// System
#include <string>
#include <boost/python.hpp> #include <boost/python.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <string>
// Engine
#include <pEp/message.h> #include <pEp/message.h>
#include <pEp/message_api.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 namespace std; using std::string;
using namespace boost::python; using std::runtime_error;
using boost::lexical_cast; using std::invalid_argument;
using boost::lexical_cast;
// Message is owning a message struct // Message is owning a message struct
class Message { class Message {
shared_ptr< ::message > _msg; shared_ptr< ::message > _msg;
public: public:
// Blob is owning a bloblist_t struct - or not and just managing // Blob is owning a bloblist_t struct - or not and just managing
// one depending on part_of_chain // one depending on part_of_chain
@ -138,10 +142,11 @@ namespace pEp {
PEP_color outgoing_color(); PEP_color outgoing_color();
Message deepcopy(dict& memo); Message deepcopy(dict& memo);
Message copy(); Message copy();
}; };
Message outgoing_message(Identity me);
Message incoming_message(string mime_text);
Message outgoing_message(Identity me); } // namespace PythonAdapter
Message incoming_message(string mime_text); } // namespace pEp {
}
}

67
src/pEp/native_pEp/message_api.cc

@ -1,20 +1,25 @@
// 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/pEpEngine.h>
#include <pEp/message_api.h> #include <pEp/message_api.h>
#include <pEp/sync_api.h> #include <pEp/sync_api.h>
#include <pEp/sync_codec.h> #include <pEp/sync_codec.h>
#include <pEp/distribution_codec.h> #include <pEp/distribution_codec.h>
// local
#include "message_api.hh" #include "message_api.hh"
#include "basic_api.hh" #include "basic_api.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
Message encrypt_message(Message src, boost::python::list extra, int enc_format, using namespace std;
int flags) using namespace boost::python;
{
Message encrypt_message(Message src, boost::python::list extra, int enc_format, int flags)
{
Identity _from = src.from(); Identity _from = src.from();
if (_from.address() == "") if (_from.address() == "")
throw invalid_argument("encrypt_message: src.from_.address empty"); throw invalid_argument("encrypt_message: src.from_.address empty");
@ -39,10 +44,10 @@ namespace pEp {
return Message(_src); return Message(_src);
return Message(_dst); return Message(_dst);
} }
boost::python::tuple decrypt_message(Message src, int flags) boost::python::tuple decrypt_message(Message src, int flags)
{ {
message *_dst = NULL; message *_dst = NULL;
stringlist_t *_keylist = NULL; stringlist_t *_keylist = NULL;
PEP_rating _rating = PEP_rating_undefined; PEP_rating _rating = PEP_rating_undefined;
@ -61,15 +66,15 @@ namespace pEp {
Message dst = _dst ? Message(_dst) : Message(src); Message dst = _dst ? Message(_dst) : Message(src);
return boost::python::make_tuple(dst, keylist, _rating, _flags); return boost::python::make_tuple(dst, keylist, _rating, _flags);
} }
PEP_color _color(int rating) PEP_color _color(int rating)
{ {
return ::color_from_rating((PEP_rating) rating); return ::color_from_rating((PEP_rating) rating);
} }
boost::python::tuple sync_decode(object buffer) boost::python::tuple sync_decode(object buffer)
{ {
Py_buffer src; Py_buffer src;
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO);
if (result) if (result)
@ -83,10 +88,10 @@ namespace pEp {
string _dst(dst); string _dst(dst);
free(dst); free(dst);
return boost::python::make_tuple(_dst, 0); return boost::python::make_tuple(_dst, 0);
} }
static boost::python::tuple sync_encode(string text) static boost::python::tuple sync_encode(string text)
{ {
char *data = NULL; char *data = NULL;
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);
@ -98,10 +103,10 @@ namespace pEp {
throw bad_alloc(); throw bad_alloc();
return boost::python::make_tuple(object(handle<>(ba)), 0); return boost::python::make_tuple(object(handle<>(ba)), 0);
} }
boost::python::tuple Distribution_decode(object buffer) boost::python::tuple Distribution_decode(object buffer)
{ {
Py_buffer src; Py_buffer src;
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO);
if (result) if (result)
@ -115,10 +120,10 @@ namespace pEp {
string _dst(dst); string _dst(dst);
free(dst); free(dst);
return boost::python::make_tuple(_dst, 0); return boost::python::make_tuple(_dst, 0);
} }
static boost::python::tuple Distribution_encode(string text) static boost::python::tuple Distribution_encode(string text)
{ {
char *data = NULL; char *data = NULL;
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);
@ -130,10 +135,10 @@ namespace pEp {
throw bad_alloc(); throw bad_alloc();
return boost::python::make_tuple(object(handle<>(ba)), 0); return boost::python::make_tuple(object(handle<>(ba)), 0);
} }
object sync_search(string name) object sync_search(string name)
{ {
if (name != "pep.sync") { if (name != "pep.sync") {
return object(); return object();
} }
@ -146,10 +151,10 @@ namespace pEp {
return call< object >(CodecInfo.ptr(), _sync_encode, _sync_decode); return call< object >(CodecInfo.ptr(), _sync_encode, _sync_decode);
} }
} }
object distribution_search(string name) object distribution_search(string name)
{ {
if (name != "pep.distribution") { if (name != "pep.distribution") {
return object(); return object();
} }
@ -162,7 +167,7 @@ namespace pEp {
return call< object >(CodecInfo.ptr(), _distribution_encode, _distribution_decode); return call< object >(CodecInfo.ptr(), _distribution_encode, _distribution_decode);
} }
}
}
} }
} // namespace PythonAdapter
} // namespace pEp {

25
src/pEp/native_pEp/message_api.hh

@ -6,12 +6,19 @@
#include "pEpmodule.hh" #include "pEpmodule.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
Message encrypt_message(Message src, boost::python::list extra = boost::python::list(),
int enc_format = 4, int flags = 0); Message encrypt_message(
boost::python::tuple decrypt_message(Message src, int flags=0); Message src,
PEP_color _color(int rating); boost::python::list extra = boost::python::list(),
object sync_search(string name); int enc_format = 4,
object distribution_search(string name); 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 {

231
src/pEp/native_pEp/pEpmodule.cc

@ -1,6 +1,7 @@
// 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/python.hpp>
#include <boost/locale.hpp> #include <boost/locale.hpp>
#include <string> #include <string>
@ -8,67 +9,76 @@
#include <iomanip> #include <iomanip>
#include <mutex> #include <mutex>
#include <pEp/Adapter.hh> // Engine
#include <pEp/key_reset.h> #include <pEp/key_reset.h>
#include <pEp/message_api.h> #include <pEp/message_api.h>
#include <pEp/sync_api.h> #include <pEp/sync_api.h>
#include <pEp/status_to_string.h> #include <pEp/status_to_string.h>
// libpEpAdapter
#include <pEp/Adapter.hh>
#include <pEp/callback_dispatcher.hh>
#include <pEp/pEpLog.hh>
// local
#include "pEpmodule.hh" #include "pEpmodule.hh"
#include "basic_api.hh" #include "basic_api.hh"
#include "message_api.hh" #include "message_api.hh"
#include "user_interface.hh" #include "user_interface.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std; using namespace std;
using namespace boost::python;
// Adapter adapter(true); scope *_scope = NULL;
static const char *version_string = "p≡p Python adapter version 0.3";
void config_passive_mode(bool enable) void _init() {
{ pEpLog("called");
callback_dispatcher.add(_messageToSend, nullptr, nullptr, nullptr);
}
void config_passive_mode(bool enable)
{
::config_passive_mode(pEp::Adapter::session(), enable); ::config_passive_mode(pEp::Adapter::session(), enable);
} }
void config_unencrypted_subject(bool enable) void config_unencrypted_subject(bool enable)
{ {
::config_unencrypted_subject(pEp::Adapter::session(), enable); ::config_unencrypted_subject(pEp::Adapter::session(), enable);
} }
void key_reset_user(string user_id, string fpr) void key_reset_user(string user_id, string fpr)
{ {
if (user_id == "") if (user_id == "")
throw invalid_argument("user_id required"); throw invalid_argument("user_id required");
PEP_STATUS status = ::key_reset_user(pEp::Adapter::session(), PEP_STATUS status = ::key_reset_user(pEp::Adapter::session(),
user_id.c_str(), fpr != "" ? fpr.c_str() : nullptr); user_id.c_str(), fpr != "" ? fpr.c_str() : nullptr);
_throw_status(status); _throw_status(status);
} }
void key_reset_user2(string user_id) void key_reset_user2(string user_id)
{ {
key_reset_user(user_id, ""); key_reset_user(user_id, "");
} }
void key_reset_all_own_keys() 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(pEp::Adapter::session());
_throw_status(status); _throw_status(status);
} }
scope *_scope = NULL;
static const char *version_string = "p≡p Python adapter version 0.3"; static string about()
static string about() {
{
string version = string(version_string) + "\np≡p version " string version = string(version_string) + "\np≡p version "
+ PEP_VERSION + "\n"; + PEP_VERSION + "\n";
return version; return version;
} }
void _throw_status(PEP_STATUS status) void _throw_status(PEP_STATUS status)
{ {
if (status == PEP_STATUS_OK) if (status == PEP_STATUS_OK)
return; return;
if (status >= 0x400 && status <= 0x4ff) if (status >= 0x400 && status <= 0x4ff)
@ -86,15 +96,15 @@ namespace pEp {
else { else {
throw runtime_error(pEp_status_to_string(status)); throw runtime_error(pEp_status_to_string(status));
} }
} }
PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr) PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr)
{ {
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
PEP_STATUS _messageToSend(::message *msg) PEP_STATUS _messageToSend(::message *msg)
{ {
if (!_scope) if (!_scope)
return PEP_SEND_FUNCTION_NOT_REGISTERED; return PEP_SEND_FUNCTION_NOT_REGISTERED;
@ -105,83 +115,84 @@ namespace pEp {
catch (exception& e) { } catch (exception& e) { }
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
void messageToSend(Message msg) { void messageToSend(Message msg) {
throw runtime_error("implement pEp.messageToSend(msg)"); throw runtime_error("implement pEp.messageToSend(msg)");
} }
// void do_sync_protocol() // void do_sync_protocol()
// { // {
// ::do_sync_protocol(pEp::Adapter::session(), nullptr); // ::do_sync_protocol(pEp::Adapter::session(), nullptr);
// } // }
void shutdown_sync() void shutdown_sync()
{ {
pEp::Adapter::shutdown(); pEp::Adapter::shutdown();
} }
void debug_color(int ansi_color) void debug_color(int ansi_color)
{ {
::set_debug_color(pEp::Adapter::session(), ansi_color); ::set_debug_color(pEp::Adapter::session(), ansi_color);
} }
void leave_device_group() { void leave_device_group()
{
::leave_device_group(pEp::Adapter::session()); ::leave_device_group(pEp::Adapter::session());
} }
// void script_is_implementing_sync() { // void script_is_implementing_sync() {
// adapter.script_is_implementing_sync(); // adapter.script_is_implementing_sync();
// } // }
bool is_sync_active() { bool is_sync_active()
{
return pEp::Adapter::is_sync_running(); return pEp::Adapter::is_sync_running();
}
}
} }
BOOST_PYTHON_MODULE(native_pEp) BOOST_PYTHON_MODULE(native_pEp)
{ {
using namespace boost::python;
using namespace boost::locale;
using namespace pEp::PythonAdapter;
docstring_options doc_options(true, false, false); docstring_options doc_options(true, false, false);
generator gen; boost::locale::generator gen;
std::locale::global(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("about") = about();
scope().attr("per_user_directory") = per_user_directory(); scope().attr("per_user_directory") = per_user_directory();
scope().attr("per_machine_directory") = per_machine_directory(); scope().attr("per_machine_directory") = per_machine_directory();
scope().attr("engine_version") = get_engine_version(); scope().attr("engine_version") = get_engine_version();
scope().attr("protocol_version") = get_protocol_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"); "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"); "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" "reset the default database status for the user / keypair provided\n"
"This will effectively perform key_reset on each identity\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" "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."); "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" "reset the default database status for the user / keypair provided\n"
"This will effectively perform key_reset on each identity\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" "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."); "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" "revoke and mistrust all own keys, generate new keys for all\n"
"own identities, and opportunistically communicate key reset\n" "own identities, and opportunistically communicate key reset\n"
"information to people we have recently contacted."); "information to people we have recently contacted.");
auto identity_class = class_<pEp::PythonAdapter::Identity>("Identity", auto identity_class = class_<Identity>("Identity",
"Identity(address, username, user_id='', fpr='', comm_type=0, lang='en')\n" "Identity(address, username, user_id='', fpr='', comm_type=0, lang='en')\n"
"\n" "\n"
"represents a p≡p identity\n" "represents a p≡p identity\n"
@ -204,12 +215,12 @@ BOOST_PYTHON_MODULE(native_pEp)
.def(boost::python::init<string, string, string, string>()) .def(boost::python::init<string, string, string, string>())
.def(boost::python::init<string, string, string, string, int>()) .def(boost::python::init<string, string, string, string, int>())
.def(boost::python::init<string, string, string, string, int, string>()) .def(boost::python::init<string, string, string, string, int, string>())
.def("__repr__", &pEp::PythonAdapter::Identity::_repr) .def("__repr__", &Identity::_repr)
.def("__str__", &pEp::PythonAdapter::Identity::_str, .def("__str__", &Identity::_str,
"string representation of this identity\n" "string representation of this identity\n"
"following the pattern 'username < address >'\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(""), boost::python::arg("fpr")=object(""),
"reset the default database status for the identity / keypair provided. If this\n" "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" "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" "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.") "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(""), boost::python::arg("fpr")=object(""),
"If you want updated trust on the identity, you ll have" "If you want updated trust on the identity, you ll have"
"to call update_identity or myself respectively after this." "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" "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)") "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" "Enable own identity for p≡p sync.\n\n"
"Only use this on own identities, which are used as accounts.\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" "Disable own identity for p≡p sync.\n\n"
"Only use this on own identities, which are used as accounts.\n") "Only use this on own identities, which are used as accounts.\n")
.add_property("address", (string(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::address, .add_property("address", (string(Identity::*)()) &Identity::address,
(void(pEp::PythonAdapter::Identity::*)(string)) &pEp::PythonAdapter::Identity::address, (void(Identity::*)(string)) &Identity::address,
"email address or URI") "email address or URI")
.add_property("fpr", (string(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::fpr, .add_property("fpr", (string(Identity::*)()) &Identity::fpr,
(void(pEp::PythonAdapter::Identity::*)(string)) &pEp::PythonAdapter::Identity::fpr, (void(Identity::*)(string)) &Identity::fpr,
"key ID (full fingerprint, hex encoded)") "key ID (full fingerprint, hex encoded)")
.add_property("user_id", (string(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::user_id, .add_property("user_id", (string(Identity::*)()) &Identity::user_id,
(void(pEp::PythonAdapter::Identity::*)(string)) &pEp::PythonAdapter::Identity::user_id, (void(Identity::*)(string)) &Identity::user_id,
"ID of person associated or 'pEp_own_userId' if own identity") "ID of person associated or 'pEp_own_userId' if own identity")
.add_property("username", (string(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::username, .add_property("username", (string(Identity::*)()) &Identity::username,
(void(pEp::PythonAdapter::Identity::*)(string)) &pEp::PythonAdapter::Identity::username, (void(Identity::*)(string)) &Identity::username,
"name in full of person associated") "name in full of person associated")
.add_property("comm_type", (int(pEp::PythonAdapter::Identity::*)()) .add_property("comm_type", (int(Identity::*)())
(PEP_comm_type(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::comm_type, (PEP_comm_type(Identity::*)()) &Identity::comm_type,
(void(pEp::PythonAdapter::Identity::*)(int)) (void(Identity::*)(int))
(void(pEp::PythonAdapter::Identity::*)(PEP_comm_type)) &pEp::PythonAdapter::Identity::comm_type, (void(Identity::*)(PEP_comm_type)) &Identity::comm_type,
"communication type, first rating level (p≡p internal)") "communication type, first rating level (p≡p internal)")
.add_property("lang", (string(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::lang, .add_property("lang", (string(Identity::*)()) &Identity::lang,
(void(pEp::PythonAdapter::Identity::*)(string)) &pEp::PythonAdapter::Identity::lang, (void(Identity::*)(string)) &Identity::lang,
"ISO 639-1 language code") "ISO 639-1 language code")
.add_property("flags", (identity_flags_t(pEp::PythonAdapter::Identity::*)()) &pEp::PythonAdapter::Identity::flags, .add_property("flags", (identity_flags_t(Identity::*)()) &Identity::flags,
(void(pEp::PythonAdapter::Identity::*)(identity_flags_t)) &pEp::PythonAdapter::Identity::flags, (void(Identity::*)(identity_flags_t)) &Identity::flags,
"flags (p≡p internal)") "flags (p≡p internal)")
.add_property("rating", &pEp::PythonAdapter::Identity::rating, "rating of Identity") .add_property("rating", &Identity::rating, "rating of Identity")
.add_property("color", &pEp::PythonAdapter::Identity::color, "color of Identity as PEP_color") .add_property("color", &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") .add_property("is_pEp_user", &Identity::is_pEp_user, "True if this is an identity of a pEp user")
.def("__deepcopy__", &pEp::PythonAdapter::Identity::deepcopy) .def("__deepcopy__", &Identity::deepcopy)
.def("update", &pEp::PythonAdapter::Identity::update, "update Identity") .def("update", &Identity::update, "update Identity")
.def("__copy__", &pEp::PythonAdapter::Identity::copy); .def("__copy__", &Identity::copy);
identity_class.attr("PEP_OWN_USERID") = "pEp_own_userId"; 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" " mime_text text in Multipurpose Internet Mail Extensions format\n"
) )
.def(boost::python::init<int>()) .def(boost::python::init<int>())
.def(boost::python::init<int, pEp::PythonAdapter::Identity *>()) .def(boost::python::init<int, Identity *>())
.def(boost::python::init<string>()) .def(boost::python::init<string>())
.def("__str__", &Message::_str, .def("__str__", &Message::_str,
"the string representation of a Message is it's MIME text" "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, .add_property("recv", (time_t(Message::*)()) &Message::recv,
(void(Message::*)(time_t)) &Message::recv, (void(Message::*)(time_t)) &Message::recv,
"time when message was received in UTC seconds since epoch") "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, (void(Message::*)(object)) &Message::from,
"identity where message is from") "identity where message is from")
.add_property("to", (boost::python::list(Message::*)()) &Message::to, .add_property("to", (boost::python::list(Message::*)()) &Message::to,
(void(Message::*)(boost::python::list)) &Message::to, (void(Message::*)(boost::python::list)) &Message::to,
"list of identities message is going 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, (void(Message::*)(object)) &Message::recv_by,
"identity where message was received by") "identity where message was received by")
.add_property("cc", (boost::python::list(Message::*)()) &Message::cc, .add_property("cc", (boost::python::list(Message::*)()) &Message::cc,
@ -414,19 +425,19 @@ BOOST_PYTHON_MODULE(native_pEp)
// basic API and key management API // basic API and key management API
def("update_identity", &pEp::PythonAdapter::update_identity, def("update_identity", &update_identity,
"update_identity(ident)\n" "update_identity(ident)\n"
"\n" "\n"
"update identity information\n" "update identity information\n"
"call this to complete identity information when you at least have an address\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" "myself(ident)\n"
"\n" "\n"
"ensures that the own identity is being complete\n" "ensures that the own identity is being complete\n"
"supply ident.address and ident.username\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" "trust_personal_key(ident)\n"
"\n" "\n"
"mark a key as trusted with a person\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_list", PEP_idf_list)
.value("PEP_idf_devicegroup", PEP_idf_devicegroup); .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" "set_identity_flags(ident, flags)\n"
"\n" "\n"
"set identity flags\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" "unset_identity_flags(ident, flags)\n"
"\n" "\n"
"unset identity flags\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" "key_reset_trust(ident)\n"
"\n" "\n"
"reset trust bit or explicitly mistrusted status for an identity and " "reset trust bit or explicitly mistrusted status for an identity and "
"its accompanying key/user_id pair\n" "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" "private_key_list = import_key(key_data)\n"
"\n" "\n"
"import key(s) from key_data\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" "key_data = export_key(identity)\n"
"\n" "\n"
"export key(s) of identity\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" "key_data = export_seret_key(identity)\n"
"\n" "\n"
"export secret key(s) of identity\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" "set_own_key(me, fpr)\n"
"\n" "\n"
"mark a key as an own key, and make it the default key\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() // messageToSend()
def("messageToSend", &pEp::PythonAdapter::messageToSend, def("messageToSend", &messageToSend,
"messageToSend(msg)\n" "messageToSend(msg)\n"
"\n" "\n"
"override pEp.messageToSend(msg) with your own implementation\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" // " result -1: cancel, 0: accepted, 1: rejected\n"
// " identities list of identities to share or None for all\n" // " identities list of identities to share or None for all\n"
// "\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() // 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" // "do_sync_protocol()\n"
// "\n" // "\n"
// "in case of an explicit sync thread instead of a single threaded\n" // "in case of an explicit sync thread instead of a single threaded\n"
// "implementation call this function in your sync thread\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" "shutdown_sync()\n"
"\n" "\n"
"call this from another thread to shut down the sync thread\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"); "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" "leave_device_group()\n"
"\n" "\n"
"call this for a grouped device, which should leave\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" // "script_is_implementing_sync()\n"
// "\n" // "\n"
// "call this in case the Python script is implementing sync to make\n" // "call this in case the Python script is implementing sync to make\n"
// "is_sync_active() working\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" "is_sync_active()\n"
"\n" "\n"
"True if sync is active, False otherwise\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(sync_search));
call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(distribution_search)); call< object >(((object)(import("codecs").attr("register"))).ptr(), make_function(distribution_search));
} }
} // namespace PythonAdapter
} // namespace pEp

29
src/pEp/native_pEp/pEpmodule.hh

@ -1,21 +1,26 @@
#pragma once #pragma once
// Engine
#include <pEp/pEpEngine.h> #include <pEp/pEpEngine.h>
// local
#include "message.hh" #include "message.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
extern string device_name;
void config_passive_mode(bool enable); extern string device_name;
void config_unencrypted_subject(bool enable); void config_passive_mode(bool enable);
void key_reset_user(string user_id, string fpr); void config_unencrypted_subject(bool enable);
void key_reset_all_own_keys(); void key_reset_user(string user_id, string fpr);
void _throw_status(PEP_STATUS status); void key_reset_all_own_keys();
void messageToSend(Message msg); void _throw_status(PEP_STATUS status);
PEP_STATUS _messageToSend(::message *msg); void messageToSend(Message msg);
PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr); PEP_STATUS _messageToSend(::message *msg);
PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr);
// void do_sync_protocol(); // void do_sync_protocol();
// extern Adapter adapter; // extern Adapter adapter;
}
} } // namespace PythonAdapter
} // namespace pEp {

86
src/pEp/native_pEp/str_attr.cc

@ -1,62 +1,65 @@
// 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/python.hpp>
#include <boost/locale.hpp> #include <boost/locale.hpp>
#include <cstdlib>
// local
#include "str_attr.hh" #include "str_attr.hh"
namespace pEp { namespace pEp {
namespace utility { namespace PythonAdapter {
using namespace std; using namespace std;
using namespace boost::locale; using namespace boost::python;
using namespace boost::locale;
object repr(object s) object repr(object s)
{ {
return s.attr("__repr__")(); return s.attr("__repr__")();
} }
string repr(string s) string repr(string s)
{ {
str _s = s.c_str(); str _s = s.c_str();
object _r = _s.attr("__repr__")(); object _r = _s.attr("__repr__")();
string r = extract< string >(_r); string r = extract< string >(_r);
return r; return r;
} }
string str_attr(char *&str) string str_attr(char *&str)
{ {
if (!str) if (!str)
return string(""); return string("");
return string(str); return string(str);
} }
void str_attr(char *&str, string value) void str_attr(char *&str, string value)
{ {
string normalized = normalize(value, norm_nfc); string normalized = normalize(value, norm_nfc);
free(str); free(str);
str = strdup(normalized.c_str()); str = strdup(normalized.c_str());
if (!str) if (!str)
throw bad_alloc(); throw bad_alloc();
} }
time_t timestamp_attr(timestamp *&ts) time_t timestamp_attr(timestamp *&ts)
{ {
if (!ts) if (!ts)
return 0; return 0;
return timegm(ts); return timegm(ts);
} }
void timestamp_attr(timestamp *&ts, time_t value) void timestamp_attr(timestamp *&ts, time_t value)
{ {
free_timestamp(ts); free_timestamp(ts);
ts = new_timestamp(value); ts = new_timestamp(value);
} }
boost::python::list strlist_attr(stringlist_t *&sl) boost::python::list strlist_attr(stringlist_t *&sl)
{ {
boost::python::list result; boost::python::list result;
for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) {
@ -65,10 +68,10 @@ namespace pEp {
} }
return result; return result;
} }
void strlist_attr(stringlist_t *&sl, boost::python::list value) void strlist_attr(stringlist_t *&sl, boost::python::list value)
{ {
stringlist_t *_sl = new_stringlist(NULL); stringlist_t *_sl = new_stringlist(NULL);
if (!_sl) if (!_sl)
throw bad_alloc(); throw bad_alloc();
@ -90,10 +93,10 @@ namespace pEp {
free_stringlist(sl); free_stringlist(sl);
sl = _sl; sl = _sl;
} }
dict strdict_attr(stringpair_list_t *&spl) dict strdict_attr(stringpair_list_t *&spl)
{ {
dict result; dict result;
for (stringpair_list_t *_spl = spl; _spl && _spl->value; _spl = for (stringpair_list_t *_spl = spl; _spl && _spl->value; _spl =
@ -108,10 +111,10 @@ namespace pEp {
} }
return result; return result;
} }
void strdict_attr(stringpair_list_t *&spl, dict value) void strdict_attr(stringpair_list_t *&spl, dict value)
{ {
stringpair_list_t *_spl = new_stringpair_list(NULL); stringpair_list_t *_spl = new_stringpair_list(NULL);
if (!_spl) if (!_spl)
throw bad_alloc(); throw bad_alloc();
@ -142,10 +145,10 @@ namespace pEp {
free_stringpair_list(spl); free_stringpair_list(spl);
spl = _spl; spl = _spl;
} }
stringlist_t *to_stringlist(boost::python::list l) stringlist_t *to_stringlist(boost::python::list l)
{ {
stringlist_t *result = new_stringlist(NULL); stringlist_t *result = new_stringlist(NULL);
if (!result) if (!result)
throw bad_alloc(); throw bad_alloc();
@ -164,17 +167,18 @@ namespace pEp {
} }
return result; return result;
} }
boost::python::list from_stringlist(const stringlist_t *sl) boost::python::list from_stringlist(const stringlist_t *sl)
{ {
boost::python::list result; boost::python::list result;
for (const 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(s); result.append(s);
} }
return result; return result;
}
}
} }
} // namespace PythonAdapter
} // namespace pEp {

38
src/pEp/native_pEp/str_attr.hh

@ -3,35 +3,39 @@
#pragma once #pragma once
// System
#include <string> #include <string>
// Engine
#include <pEp/pEpEngine.h> #include <pEp/pEpEngine.h>
#include <pEp/timestamp.h> #include <pEp/timestamp.h>
#include <pEp/stringlist.h> #include <pEp/stringlist.h>
#include <pEp/stringpair.h> #include <pEp/stringpair.h>
namespace pEp { namespace pEp {
namespace utility { namespace PythonAdapter {
using namespace std; using std::string;
using namespace boost::python; using boost::python::object;
using boost::python::dict;
object repr(object s); object repr(object s);
string repr(string s); string repr(string s);
string str_attr(char *&str); string str_attr(char *&str);
void str_attr(char *&str, string value); void str_attr(char *&str, string value);
time_t timestamp_attr(timestamp *&ts); time_t timestamp_attr(timestamp *&ts);
void timestamp_attr(timestamp *&ts, time_t value); void timestamp_attr(timestamp *&ts, time_t value);
boost::python::list strlist_attr(stringlist_t *&sl); boost::python::list strlist_attr(stringlist_t *&sl);
void strlist_attr(stringlist_t *&sl, boost::python::list value); void strlist_attr(stringlist_t *&sl, boost::python::list value);
dict strdict_attr(stringpair_list_t *&spl); dict strdict_attr(stringpair_list_t *&spl);
void strdict_attr(stringpair_list_t *&spl, dict value); void strdict_attr(stringpair_list_t *&spl, dict value);
stringlist_t *to_stringlist(boost::python::list l); stringlist_t *to_stringlist(boost::python::list l);
boost::python::list from_stringlist(const stringlist_t *sl); boost::python::list from_stringlist(const stringlist_t *sl);
}
} } // namespace PythonAdapter
} // namespace pEp {

65
src/pEp/native_pEp/user_interface.cc

@ -1,46 +1,50 @@
// 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> #include <cassert>
// local
#include "user_interface.hh" #include "user_interface.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
UserInterface *UserInterface::_ui = nullptr;
UserInterface *UserInterface::_ui = nullptr;
UserInterface::UserInterface() UserInterface::UserInterface()
{ {
if (_ui) if (_ui)
throw runtime_error("only one UserInterface thread allowed"); throw runtime_error("only one UserInterface thread allowed");
_ui = this; _ui = this;
} }
UserInterface::~UserInterface() UserInterface::~UserInterface()
{ {
_ui = nullptr; _ui = nullptr;
} }
UserInterface_callback::UserInterface_callback(PyObject *self) : UserInterface_callback::UserInterface_callback(PyObject *self) :
UserInterface(), _self(self) UserInterface(), _self(self)
{ {
// adapter.ui_object(self); // adapter.ui_object(self);
PEP_STATUS status = ::register_sync_callbacks(pEp::Adapter::session(), PEP_STATUS status = ::register_sync_callbacks(pEp::Adapter::session(),
(void *) this, _notifyHandshake, retrieve_next_sync_event); (void *) this, _notifyHandshake, retrieve_next_sync_event);
assert(status == PEP_STATUS_OK); assert(status == PEP_STATUS_OK);
if (status) if (status)
_throw_status(status); _throw_status(status);
} }
UserInterface_callback::~UserInterface_callback() UserInterface_callback::~UserInterface_callback()
{ {
::unregister_sync_callbacks(pEp::Adapter::session()); ::unregister_sync_callbacks(pEp::Adapter::session());
} }
PEP_STATUS UserInterface::_notifyHandshake( PEP_STATUS UserInterface::_notifyHandshake(
pEp_identity *me, pEp_identity *partner, pEp_identity *me, pEp_identity *partner,
sync_handshake_signal signal sync_handshake_signal signal
) )
{ {
if (!(me && partner)) if (!(me && partner))
return PEP_ILLEGAL_VALUE; return PEP_ILLEGAL_VALUE;
@ -48,10 +52,10 @@ namespace pEp {
that->notifyHandshake(Identity(me), Identity(partner), signal); that->notifyHandshake(Identity(me), Identity(partner), signal);
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
void UserInterface::deliverHandshakeResult(int result, object identities) void UserInterface::deliverHandshakeResult(int result, object identities)
{ {
identity_list *shared_identities = nullptr; identity_list *shared_identities = nullptr;
if (identities != boost::python::api::object() && if (identities != boost::python::api::object() &&
boost::python::len(identities)) { boost::python::len(identities)) {
@ -78,20 +82,20 @@ namespace pEp {
(sync_handshake_result) result, shared_identities); (sync_handshake_result) result, shared_identities);
free_identity_list(shared_identities); free_identity_list(shared_identities);
_throw_status(status); _throw_status(status);
} }
PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr)
{ {
PEP_rating result; PEP_rating result;
PEP_STATUS status = PEP_STATUS status =
::get_key_rating_for_user(pEp::Adapter::session(), ::get_key_rating_for_user(pEp::Adapter::session(),
user_id.c_str(), fpr.c_str(), &result); user_id.c_str(), fpr.c_str(), &result);
_throw_status(status); _throw_status(status);
return result; return result;
} }
// SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold) //SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold)
// { //{
// time_t started = time(nullptr); // time_t started = time(nullptr);
// bool timeout = false; // bool timeout = false;
// //
@ -112,13 +116,14 @@ namespace pEp {
// return new_sync_timeout_event(); // return new_sync_timeout_event();
// //
// return adapter.queue().pop_front(); // return adapter.queue().pop_front();
// } //}
void UserInterface_callback::notifyHandshake( void UserInterface_callback::notifyHandshake(
Identity me, Identity partner, sync_handshake_signal signal) Identity me, Identity partner, sync_handshake_signal signal)
{ {
call_method< void >(_self, "notifyHandshake", me, partner, signal); call_method< void >(_self, "notifyHandshake", me, partner, signal);
}
}
} }
} // namespace PythonAdapter
} // namespace pEp {

41
src/pEp/native_pEp/user_interface.hh

@ -3,25 +3,30 @@
#pragma once #pragma once
// System
#include <csetjmp> #include <csetjmp>
// Engine
#include <pEp/sync_api.h> #include <pEp/sync_api.h>
#include <pEp/message_api.h> #include <pEp/message_api.h>
// local
#include "pEpmodule.hh" #include "pEpmodule.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
class UserInterface {
class UserInterface {
private:
static UserInterface *_ui; static UserInterface *_ui;
public: public:
UserInterface(); UserInterface();
virtual ~UserInterface(); virtual ~UserInterface();
virtual void notifyHandshake( virtual void notifyHandshake(
pEp::PythonAdapter::Identity me, Identity me,
pEp::PythonAdapter::Identity partner, Identity partner,
sync_handshake_signal signal) sync_handshake_signal signal)
{ {
throw runtime_error("override this method"); throw runtime_error("override this method");
@ -31,23 +36,25 @@ namespace pEp {
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, static PEP_STATUS _notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal);
pEp_identity *partner, sync_handshake_signal signal);
static SYNC_EVENT retrieve_next_sync_event(void *management, unsigned threshold); static SYNC_EVENT retrieve_next_sync_event(void *management, unsigned threshold);
}; };
class UserInterface_callback : public UserInterface { class UserInterface_callback : public UserInterface {
private:
PyObject *_self; PyObject *_self;
public: public:
UserInterface_callback(PyObject *self); UserInterface_callback(PyObject *self);
~UserInterface_callback(); ~UserInterface_callback();
void notifyHandshake( void notifyHandshake(
pEp::PythonAdapter::Identity me, Identity me,
pEp::PythonAdapter::Identity partner, Identity partner,
sync_handshake_signal signal); sync_handshake_signal signal
}; );
} };
}
} // namespace PythonAdapter
} // namespace pEp {

1
test/pyadpt-81.py

@ -3,4 +3,5 @@
import pEp import pEp
dir(pEp)
pEp.is_sync_active() pEp.is_sync_active()
Loading…
Cancel
Save