Browse Source

Formatting only - K&R 1TBS is the standard here

PYADPT-96_init_version
heck 5 years ago
parent
commit
f40ca8d8e6
  1. 15
      setup.py
  2. 2
      src/pEp/__init__.py
  3. 77
      src/pEp/native_pEp/basic_api.cc
  4. 33
      src/pEp/native_pEp/basic_api.hh
  5. 160
      src/pEp/native_pEp/identity.cc
  6. 61
      src/pEp/native_pEp/identity.hh
  7. 200
      src/pEp/native_pEp/message.cc
  8. 74
      src/pEp/native_pEp/message.hh
  9. 63
      src/pEp/native_pEp/message_api.cc
  10. 19
      src/pEp/native_pEp/message_api.hh
  11. 137
      src/pEp/native_pEp/pEpmodule.cc
  12. 31
      src/pEp/native_pEp/pEpmodule.hh
  13. 86
      src/pEp/native_pEp/str_attr.cc
  14. 59
      src/pEp/native_pEp/user_interface.cc
  15. 23
      src/pEp/native_pEp/user_interface.hh

15
setup.py

@ -29,8 +29,8 @@ def pEpLog(*msg):
func = inspect.currentframe().f_back.f_code func = inspect.currentframe().f_back.f_code
print(func.co_filename + " : " + func.co_name + " : " + msgstr) print(func.co_filename + " : " + func.co_name + " : " + msgstr)
class BuildExtCommand(build_ext):
class BuildExtCommand(build_ext):
user_options = build_ext.user_options + [ user_options = build_ext.user_options + [
('local', None, 'Use local pEp install in HOME/USERPROFILE for libs/includes'), ('local', None, 'Use local pEp install in HOME/USERPROFILE for libs/includes'),
('prefix=', None, 'Use local pEp install in prefix for libs/includes'), ('prefix=', None, 'Use local pEp install in prefix for libs/includes'),
@ -155,15 +155,15 @@ class BuildExtCommand(build_ext):
# Append home-dir # Append home-dir
if self.local: if self.local:
pEpLog("local mode") pEpLog("local mode")
home_include=[ join(home, 'include') ] home_include = [join(home, 'include')]
home_libdirs=[ join(home, 'lib') ] home_libdirs = [join(home, 'lib')]
includes += home_include includes += home_include
libdirs += home_libdirs libdirs += home_libdirs
# Append prefix-dir # Append prefix-dir
if self.prefix: if self.prefix:
prefix_include=[ join(self.prefix, 'include') ] prefix_include = [join(self.prefix, 'include')]
prefix_libdirs=[ join(self.prefix, 'lib') ] prefix_libdirs = [join(self.prefix, 'lib')]
includes += prefix_include includes += prefix_include
libdirs += prefix_libdirs libdirs += prefix_libdirs
@ -202,10 +202,9 @@ if sys.platform == 'winnt':
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
FileNotFoundError = EnvironmentError FileNotFoundError = EnvironmentError
module_pEp = Extension( module_pEp = Extension(
'native_pEp', 'native_pEp',
sources = [ sources=[
'src/pEp/native_pEp/pEpmodule.cc', 'src/pEp/native_pEp/pEpmodule.cc',
'src/pEp/native_pEp/basic_api.cc', 'src/pEp/native_pEp/basic_api.cc',
'src/pEp/native_pEp/identity.cc', 'src/pEp/native_pEp/identity.cc',
@ -225,7 +224,7 @@ setup(
author_email="vb@pep-project.org", author_email="vb@pep-project.org",
maintainer="Heck", maintainer="Heck",
maintainer_email="heck@pep.foundation", maintainer_email="heck@pep.foundation",
package_dir={'':'src'}, package_dir={'': 'src'},
packages=['pEp'], packages=['pEp'],
ext_modules=[module_pEp], ext_modules=[module_pEp],
cmdclass={ cmdclass={

2
src/pEp/__init__.py

@ -11,6 +11,7 @@ from native_pEp import *
# import the module # import the module
import native_pEp import native_pEp
# Executed on module import # Executed on module import
def init(): def init():
print(init, "called") print(init, "called")
@ -45,6 +46,7 @@ def notify_handshake(me, partner, signal):
def main(): def main():
print("I am being run as a script") print("I am being run as a script")
# MAIN # MAIN
if __name__ == "__main__": if __name__ == "__main__":
main() main()

77
src/pEp/native_pEp/basic_api.cc

@ -13,23 +13,22 @@
#include "basic_api.hh" #include "basic_api.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std; using namespace std;
void update_identity(Identity& ident) 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)
throw runtime_error("update_identity: '" PEP_OWN_USERID throw runtime_error("update_identity: '"
PEP_OWN_USERID
"' may only be used for own identities"); "' may only be used for own identities");
PEP_STATUS status = update_identity(Adapter::session(), ident); PEP_STATUS status = update_identity(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() == "")
@ -40,10 +39,9 @@ void myself(Identity& ident)
PEP_STATUS status = myself(Adapter::session(), ident); PEP_STATUS status = myself(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");
@ -53,13 +51,12 @@ string _trustwords(Identity me, Identity partner, string lang, bool full)
char *words = NULL; char *words = NULL;
size_t size = 0; size_t size = 0;
PEP_STATUS status = get_trustwords(Adapter::session(), me, partner, PEP_STATUS status = get_trustwords(Adapter::session(), me, partner,
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() == "")
@ -67,10 +64,9 @@ void trust_personal_key(Identity ident)
PEP_STATUS status = trust_personal_key(Adapter::session(), ident); PEP_STATUS status = trust_personal_key(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() == "")
@ -78,10 +74,9 @@ void set_identity_flags(Identity ident, identity_flags_t flags)
PEP_STATUS status = set_identity_flags(Adapter::session(), ident, flags); PEP_STATUS status = set_identity_flags(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() == "")
@ -89,10 +84,9 @@ void unset_identity_flags(Identity ident, identity_flags_t flags)
PEP_STATUS status = unset_identity_flags(Adapter::session(), ident, flags); PEP_STATUS status = unset_identity_flags(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() == "")
@ -102,19 +96,17 @@ void key_reset_trust(Identity ident)
PEP_STATUS status = key_reset_trust(Adapter::session(), ident); PEP_STATUS status = key_reset_trust(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(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);
auto result = boost::python::list(); auto result = boost::python::list();
for (::identity_list *il = private_keys; il && il->ident; il=il->next) { for (::identity_list *il = private_keys; il && il->ident; il = il->next) {
::pEp_identity *ident = ::identity_dup(il->ident); ::pEp_identity *ident = ::identity_dup(il->ident);
if (!ident) { if (!ident) {
free_identity_list(private_keys); free_identity_list(private_keys);
@ -125,32 +117,29 @@ boost::python::list import_key(string key_data)
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;
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);
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;
status = ::export_secret_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size); status = ::export_secret_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size);
_throw_status(status); _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() == "")
@ -161,12 +150,12 @@ void set_own_key(Identity& ident, string fpr)
throw invalid_argument("fpr needed"); throw invalid_argument("fpr needed");
const char* fpr_c = fpr.c_str(); const char *fpr_c = fpr.c_str();
PEP_STATUS status = set_own_key(Adapter::session(), ident, fpr_c); PEP_STATUS status = set_own_key(Adapter::session(), ident, fpr_c);
_throw_status(status); _throw_status(status);
} }
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp { } // namespace pEp

33
src/pEp/native_pEp/basic_api.hh

@ -7,24 +7,31 @@
#include "pEpmodule.hh" #include "pEpmodule.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
void update_identity(Identity& ident); void update_identity(Identity &ident);
void myself(Identity& ident);
string _trustwords(Identity me, Identity partner, string lang, bool full);
void trust_personal_key(Identity ident);
void set_identity_flags(Identity ident, identity_flags_t flags); void myself(Identity &ident);
void unset_identity_flags(Identity ident, identity_flags_t flags);
void key_reset_trust(Identity ident); string _trustwords(Identity me, Identity partner, string lang, bool full);
boost::python::list import_key(string key_data); void trust_personal_key(Identity ident);
string export_key(Identity ident);
string export_secret_key(Identity ident);
void set_own_key(Identity& ident, string fpr);
} /* namespace PythonAdapter */ void set_identity_flags(Identity ident, identity_flags_t flags);
void unset_identity_flags(Identity ident, identity_flags_t flags);
void key_reset_trust(Identity ident);
boost::python::list import_key(string key_data);
string export_key(Identity ident);
string export_secret_key(Identity ident);
void set_own_key(Identity &ident, string fpr);
} /* namespace PythonAdapter */
} /* namespace pEp */ } /* namespace pEp */
#endif /* BASIC_API_HH */ #endif /* BASIC_API_HH */

160
src/pEp/native_pEp/identity.cc

@ -17,51 +17,44 @@
#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; 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;
@ -84,42 +77,37 @@ string Identity::_repr()
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");
@ -128,69 +116,58 @@ int Identity::rating()
_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(Adapter::session(), *this, PEP_STATUS status = ::key_reset_identity(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(Adapter::session(), *this); PEP_STATUS status = ::key_mistrusted(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(Adapter::session(), *this, &result); PEP_STATUS status = ::is_pEp_user(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(Adapter::session(), *this); PEP_STATUS status = ::enable_identity_for_sync(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(Adapter::session(), *this); PEP_STATUS status = ::disable_identity_for_sync(Adapter::session(), *this);
_throw_status(status); _throw_status(status);
} }
Myself::Myself(string address, string username, string user_id, string lang)
: Identity(address, username, user_id, "", 0, lang)
{ Myself::Myself(string address, string username, string user_id, string lang)
: Identity(address, username, user_id, "", 0, lang) {
if (!(address.length() && username.length())) 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)
@ -200,15 +177,13 @@ Myself::Myself(string address, string username, string user_id, string lang)
// _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");
@ -218,11 +193,10 @@ Identity identity_attr(pEp_identity *&ident)
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)
throw bad_alloc(); throw bad_alloc();
@ -230,10 +204,9 @@ void identity_attr(pEp_identity *&ident, object value)
_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) {
@ -244,17 +217,16 @@ boost::python::list identitylist_attr(identity_list *&il)
} }
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();
identity_list *_i = _il; identity_list *_i = _il;
for (int i=0; i<len(value); i++) { for (int i = 0; i < len(value); i++) {
extract< Identity& > extract_identity(value[i]); extract < Identity & > extract_identity(value[i]);
if (!extract_identity.check()) { if (!extract_identity.check()) {
free_identity_list(_il); free_identity_list(_il);
} }
@ -278,8 +250,8 @@ void identitylist_attr(identity_list *&il, boost::python::list value)
free_identity_list(il); free_identity_list(il);
il = _il; il = _il;
} }
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp { } // namespace pEp

61
src/pEp/native_pEp/identity.hh

@ -21,82 +21,101 @@
#include "str_attr.hh" #include "str_attr.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using std::string; using std::string;
using std::shared_ptr; 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);
Identity(const Identity& second); Identity(const Identity &second);
Identity(pEp_identity *ident); Identity(pEp_identity *ident);
virtual ~Identity(); virtual ~Identity();
operator pEp_identity *(); operator pEp_identity *();
operator const pEp_identity *() const; operator const pEp_identity *() const;
string _repr(); string _repr();
string _str(); string _str();
string address() { return str_attr(_ident->address); } string address() { return str_attr(_ident->address); }
void address(string value) { str_attr(_ident->address, value); } void address(string value) { str_attr(_ident->address, value); }
string fpr() { return str_attr(_ident->fpr); } string fpr() { return str_attr(_ident->fpr); }
void fpr(string value) { str_attr(_ident->fpr, value); } void fpr(string value) { str_attr(_ident->fpr, value); }
string user_id() { return str_attr(_ident->user_id); } string user_id() { return str_attr(_ident->user_id); }
void user_id(string value) { str_attr(_ident->user_id, value); } void user_id(string value) { str_attr(_ident->user_id, value); }
string username() { return str_attr(_ident->username); } string username() { return str_attr(_ident->username); }
void username(string value); void username(string value);
PEP_comm_type comm_type() { return _ident->comm_type; } PEP_comm_type comm_type() { return _ident->comm_type; }
void comm_type(PEP_comm_type value) { _ident->comm_type = value; }; void comm_type(PEP_comm_type value) { _ident->comm_type = value; };
std::string lang(); std::string lang();
void lang(std::string value); void lang(std::string value);
identity_flags_t flags() { return _ident->flags; } identity_flags_t flags() { return _ident->flags; }
void flags(identity_flags_t flags) { _ident->flags = flags; } void flags(identity_flags_t flags) { _ident->flags = flags; }
int rating(); int rating();
PEP_color color(); PEP_color color();
Identity copy(); Identity copy();
Identity deepcopy(dict& memo);
Identity deepcopy(dict &memo);
virtual void update(); virtual void update();
void key_reset(string fpr=""); void key_reset(string fpr = "");
void key_mistrusted(); void key_mistrusted();
bool is_pEp_user(); bool is_pEp_user();
void enable_for_sync(); void enable_for_sync();
void disable_for_sync(); void disable_for_sync();
}; };
class Myself : public Identity {
public:
Myself(string address, string username, string user_id = "", string lang = "");
class Myself : public Identity {
public:
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);
boost::python::list identitylist_attr(identity_list *&il); void identitylist_attr(identity_list *&il, boost::python::list value);
void identitylist_attr(identity_list *&il, boost::python::list value);
} /* namespace PythonAdapter */ } // namespace PythonAdapter
} /* namespace pEp */ } // namespace pEp
#endif /* IDENTITY_HH */ #endif /* IDENTITY_HH */

200
src/pEp/native_pEp/message.cc

@ -19,20 +19,18 @@
#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; 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();
@ -41,7 +39,7 @@ Message::Blob::Blob(object data, string mime_type, string filename) :
if (result) if (result)
throw invalid_argument("need a contiguous buffer to read"); throw invalid_argument("need a contiguous buffer to read");
char *mem = (char *)malloc(src.len); char *mem = (char *) malloc(src.len);
if (!mem) { if (!mem) {
PyBuffer_Release(&src); PyBuffer_Release(&src);
throw bad_alloc(); throw bad_alloc();
@ -56,30 +54,26 @@ Message::Blob::Blob(object data, string mime_type, string filename) :
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) {
build << "b'', '', ''"; build << "b'', '', ''";
} } else {
else {
build << "bytes(" << _bl->size << "), "; build << "bytes(" << _bl->size << "), ";
string mime_type; string mime_type;
if (_bl->mime_type) if (_bl->mime_type)
@ -92,16 +86,16 @@ string Message::Blob::_repr()
} }
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 {
Message::Blob& blob = extract< Message::Blob& >(self); Message::Blob &blob = 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 = NULL;
return -1; return -1;
@ -114,10 +108,9 @@ int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) {
} }
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";
@ -131,14 +124,13 @@ string Message::Blob::decode(string encoding)
} }
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();
@ -148,11 +140,10 @@ Message::Message(int dir, Identity *from)
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);
@ -166,7 +157,7 @@ Message::Message(string mimetext)
if (!_cpy) if (!_cpy)
throw bad_alloc(); throw bad_alloc();
_msg = shared_ptr< message >(_cpy); _msg = shared_ptr<message>(_cpy);
break; break;
case PEP_BUFFER_TOO_SMALL: case PEP_BUFFER_TOO_SMALL:
@ -183,38 +174,32 @@ Message::Message(string mimetext)
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");
@ -244,17 +229,15 @@ string Message::_str()
} }
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 =
@ -263,21 +246,20 @@ boost::python::tuple Message::attachments()
} }
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();
bloblist_t *_l = bl; bloblist_t *_l = bl;
for (int i=0; i<len(value); i++) { for (int i = 0; i < len(value); i++) {
Message::Blob& blob = extract< Message::Blob& >(value[i]); Message::Blob &blob = extract<Message::Blob &>(value[i]);
_l = bloblist_add(_l, blob._bl->value, blob._bl->size, _l = bloblist_add(_l, blob._bl->value, blob._bl->size,
blob._bl->mime_type, blob._bl->filename); blob._bl->mime_type, blob._bl->filename);
if (!_l) { if (!_l) {
for (_l = bl; _l && _l->value; ) { for (_l = bl; _l && _l->value;) {
free(_l->mime_type); free(_l->mime_type);
free(_l->filename); free(_l->filename);
bloblist_t *_ll = _l; bloblist_t *_ll = _l;
@ -288,8 +270,8 @@ void Message::attachments(boost::python::list value)
} }
} }
for (int i=0; i<len(value); i++) { for (int i = 0; i < len(value); i++) {
Message::Blob& blob = extract< Message::Blob& >(value[i]); Message::Blob &blob = extract<Message::Blob &>(value[i]);
blob._bl->value = NULL; blob._bl->value = NULL;
blob._bl->size = 0; blob._bl->size = 0;
free(blob._bl->mime_type); free(blob._bl->mime_type);
@ -300,27 +282,24 @@ void Message::attachments(boost::python::list value)
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");
@ -340,74 +319,67 @@ PEP_rating Message::outgoing_rating()
_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(Adapter::session(), me); ::myself(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(Adapter::session(), ident); update_identity(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);
try { try {
m.from(update(m.from())); m.from(update(m.from()));
} }
catch (out_of_range&) { } catch (out_of_range &) {}
try { try {
m.recv_by(update(m.recv_by())); m.recv_by(update(m.recv_by()));
} }
catch (out_of_range&) { } catch (out_of_range &) {}
m.to(update(m.to())); m.to(update(m.to()));
m.cc(update(m.cc())); m.cc(update(m.cc()));
m.reply_to(update(m.reply_to())); m.reply_to(update(m.reply_to()));
return m; return m;
} }
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp { } // namespace pEp

74
src/pEp/native_pEp/message.hh

@ -18,18 +18,18 @@
#include "identity.hh" #include "identity.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using std::string; using std::string;
using std::runtime_error; using std::runtime_error;
using std::invalid_argument; using std::invalid_argument;
using boost::lexical_cast; 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
@ -40,20 +40,27 @@ public:
public: public:
Blob(bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL), Blob(bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL),
bool chained = false); bool chained = false);
Blob(object data, string mime_type = "", string filename = ""); Blob(object data, string mime_type = "", string filename = "");
Blob(const Blob& second);
Blob(const Blob &second);
~Blob(); ~Blob();
string _repr(); string _repr();
string mime_type() { return _bl ? str_attr(_bl->mime_type) : ""; } string mime_type() { return _bl ? str_attr(_bl->mime_type) : ""; }
void mime_type(string value) { str_attr(_bl->mime_type, value); } void mime_type(string value) { str_attr(_bl->mime_type, value); }
string filename() { return str_attr(_bl->filename); } string filename() { return str_attr(_bl->filename); }
void filename(string value) { str_attr(_bl->filename, value); } void filename(string value) { str_attr(_bl->filename, value); }
size_t size() { return _bl->size; } size_t size() { return _bl->size; }
string decode(string encoding); string decode(string encoding);
string decode() { return decode(""); } string decode() { return decode(""); }
static PyBufferProcs bp; static PyBufferProcs bp;
@ -65,90 +72,123 @@ public:
}; };
Message(int dir = PEP_dir_outgoing, Identity *from = NULL); Message(int dir = PEP_dir_outgoing, Identity *from = NULL);
Message(string mimetext); Message(string mimetext);
Message(const Message& second);
Message(const Message &second);
Message(message *msg); Message(message *msg);
~Message(); ~Message();
operator message *(); operator message *();
operator const message *() const; operator const message *() const;
string _str(); string _str();
string _repr(); string _repr();
PEP_msg_direction dir() { return _msg->dir; } PEP_msg_direction dir() { return _msg->dir; }
void dir(PEP_msg_direction value) { _msg->dir = value; } void dir(PEP_msg_direction value) { _msg->dir = value; }
string id() { return str_attr(_msg->id); } string id() { return str_attr(_msg->id); }
void id(string value) { str_attr(_msg->id, value); } void id(string value) { str_attr(_msg->id, value); }
string shortmsg() { return str_attr(_msg->shortmsg); } string shortmsg() { return str_attr(_msg->shortmsg); }
void shortmsg(string value) { str_attr(_msg->shortmsg, value); } void shortmsg(string value) { str_attr(_msg->shortmsg, value); }
string longmsg() { return str_attr(_msg->longmsg); } string longmsg() { return str_attr(_msg->longmsg); }
void longmsg(string value) { str_attr(_msg->longmsg, value); } void longmsg(string value) { str_attr(_msg->longmsg, value); }
string longmsg_formatted() { return str_attr(_msg->longmsg_formatted); } string longmsg_formatted() { return str_attr(_msg->longmsg_formatted); }
void longmsg_formatted(string value) { str_attr(_msg->longmsg_formatted, value); } void longmsg_formatted(string value) { str_attr(_msg->longmsg_formatted, value); }
boost::python::tuple attachments(); boost::python::tuple attachments();
void attachments(boost::python::list value); void attachments(boost::python::list value);
time_t sent() { return timestamp_attr(_msg->sent); } time_t sent() { return timestamp_attr(_msg->sent); }
void sent(time_t value) { timestamp_attr(_msg->sent, value); } void sent(time_t value) { timestamp_attr(_msg->sent, value); }
time_t recv() { return timestamp_attr(_msg->recv); } time_t recv() { return timestamp_attr(_msg->recv); }
void recv(time_t value) { timestamp_attr(_msg->recv, value); } void recv(time_t value) { timestamp_attr(_msg->recv, value); }
Identity from() { return identity_attr(_msg->from); } Identity from() { return identity_attr(_msg->from); }
void from(object value) { identity_attr(_msg->from, value); } void from(object value) { identity_attr(_msg->from, value); }
boost::python::list to() { return identitylist_attr(_msg->to); } boost::python::list to() { return identitylist_attr(_msg->to); }
void to(boost::python::list value) { identitylist_attr(_msg->to, value); } void to(boost::python::list value) { identitylist_attr(_msg->to, value); }
Identity recv_by() { return identity_attr(_msg->recv_by); } Identity recv_by() { return identity_attr(_msg->recv_by); }
void recv_by(object value) { identity_attr(_msg->recv_by, value); } void recv_by(object value) { identity_attr(_msg->recv_by, value); }
boost::python::list cc() { return identitylist_attr(_msg->cc); } boost::python::list cc() { return identitylist_attr(_msg->cc); }
void cc(boost::python::list value) { identitylist_attr(_msg->cc, value); } void cc(boost::python::list value) { identitylist_attr(_msg->cc, value); }
boost::python::list bcc() { return identitylist_attr(_msg->bcc); } boost::python::list bcc() { return identitylist_attr(_msg->bcc); }
void bcc(boost::python::list value) { identitylist_attr(_msg->bcc, value); } void bcc(boost::python::list value) { identitylist_attr(_msg->bcc, value); }
boost::python::list reply_to() { return identitylist_attr(_msg->reply_to); } boost::python::list reply_to() { return identitylist_attr(_msg->reply_to); }
void reply_to(boost::python::list value) { identitylist_attr(_msg->reply_to, value); } void reply_to(boost::python::list value) { identitylist_attr(_msg->reply_to, value); }
boost::python::list in_reply_to() { return strlist_attr(_msg->in_reply_to); } boost::python::list in_reply_to() { return strlist_attr(_msg->in_reply_to); }
void in_reply_to(boost::python::list value) { strlist_attr(_msg->in_reply_to, value); } void in_reply_to(boost::python::list value) { strlist_attr(_msg->in_reply_to, value); }
boost::python::list references() { return strlist_attr(_msg->references); } boost::python::list references() { return strlist_attr(_msg->references); }
void references(boost::python::list value) { strlist_attr(_msg->references, value); } void references(boost::python::list value) { strlist_attr(_msg->references, value); }
boost::python::list keywords() { return strlist_attr(_msg->keywords); } boost::python::list keywords() { return strlist_attr(_msg->keywords); }
void keywords(boost::python::list value) { strlist_attr(_msg->keywords, value); } void keywords(boost::python::list value) { strlist_attr(_msg->keywords, value); }
string comments() { return str_attr(_msg->comments); } string comments() { return str_attr(_msg->comments); }
void comments(string value) { str_attr(_msg->comments, value); } void comments(string value) { str_attr(_msg->comments, value); }
dict opt_fields() { return strdict_attr(_msg->opt_fields); } dict opt_fields() { return strdict_attr(_msg->opt_fields); }
void opt_fields(dict value) { return strdict_attr(_msg->opt_fields, value); } void opt_fields(dict value) { return strdict_attr(_msg->opt_fields, value); }
PEP_enc_format enc_format() { return _msg->enc_format; } PEP_enc_format enc_format() { return _msg->enc_format; }
void enc_format(PEP_enc_format value) { _msg->enc_format = value; } void enc_format(PEP_enc_format value) { _msg->enc_format = value; }
Message encrypt(); Message encrypt();
Message _encrypt(boost::python::list extra, int enc_format=4, int flags=0);
boost::python::tuple decrypt(int flags=0); Message _encrypt(boost::python::list extra, int enc_format = 4, int flags = 0);
boost::python::tuple decrypt(int flags = 0);
PEP_rating outgoing_rating(); PEP_rating outgoing_rating();
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 outgoing_message(Identity me); Message incoming_message(string mime_text);
Message incoming_message(string mime_text);
} /* namespace PythonAdapter */ } /* namespace PythonAdapter */
} /* namespace pEp */ } /* namespace pEp */
#endif /* MESSAGE_HH */ #endif /* MESSAGE_HH */

63
src/pEp/native_pEp/message_api.cc

@ -13,12 +13,11 @@
#include "basic_api.hh" #include "basic_api.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std; using namespace std;
using namespace boost::python; using namespace boost::python;
Message encrypt_message(Message src, boost::python::list extra, int enc_format, int flags) 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");
@ -43,10 +42,9 @@ Message encrypt_message(Message src, boost::python::list extra, int enc_format,
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;
@ -65,15 +63,13 @@ boost::python::tuple decrypt_message(Message src, int flags)
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)
@ -87,10 +83,9 @@ boost::python::tuple sync_decode(object buffer)
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);
@ -102,10 +97,9 @@ static boost::python::tuple sync_encode(string text)
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)
@ -119,10 +113,9 @@ boost::python::tuple Distribution_decode(object buffer)
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);
@ -134,39 +127,35 @@ static boost::python::tuple Distribution_encode(string text)
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();
} } else {
else {
object codecs = import("codecs"); object codecs = import("codecs");
object CodecInfo = codecs.attr("CodecInfo"); object CodecInfo = codecs.attr("CodecInfo");
object _sync_decode = make_function(sync_decode); object _sync_decode = make_function(sync_decode);
object _sync_encode = make_function(sync_encode); object _sync_encode = make_function(sync_encode);
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();
} } else {
else {
object codecs = import("codecs"); object codecs = import("codecs");
object CodecInfo = codecs.attr("CodecInfo"); object CodecInfo = codecs.attr("CodecInfo");
object _distribution_decode = make_function(Distribution_decode); object _distribution_decode = make_function(Distribution_decode);
object _distribution_encode = make_function(Distribution_encode); object _distribution_encode = make_function(Distribution_encode);
return call< object >(CodecInfo.ptr(), _distribution_encode, _distribution_decode); return call<object>(CodecInfo.ptr(), _distribution_encode, _distribution_decode);
}
} }
}
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp { } // namespace pEp {

19
src/pEp/native_pEp/message_api.hh

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

137
src/pEp/native_pEp/pEpmodule.cc

@ -27,64 +27,57 @@
//#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; using namespace boost::python;
static const char *version_string = "p≡p Python adapter version 0.3"; static const char *version_string = "p≡p Python adapter version 0.3";
void init_before_main_module() { void init_before_main_module() {
pEpLog("called"); pEpLog("called");
} }
// hidden init function, wrapped by hello_world.init() // hidden init function, wrapped by hello_world.init()
void _init_after_main_module() { void _init_after_main_module() {
pEpLog("called"); pEpLog("called");
callback_dispatcher.add(_messageToSend, notifyHandshake, nullptr, nullptr); callback_dispatcher.add(_messageToSend, notifyHandshake, nullptr, nullptr);
Adapter::_messageToSend = CallbackDispatcher::messageToSend; Adapter::_messageToSend = CallbackDispatcher::messageToSend;
} }
void config_passive_mode(bool enable) void config_passive_mode(bool enable) {
{
::config_passive_mode(Adapter::session(), enable); ::config_passive_mode(Adapter::session(), enable);
} }
void config_unencrypted_subject(bool enable) void config_unencrypted_subject(bool enable) {
{
::config_unencrypted_subject(Adapter::session(), enable); ::config_unencrypted_subject(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(Adapter::session(), PEP_STATUS status = ::key_reset_user(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(Adapter::session()); PEP_STATUS status = ::key_reset_all_own_keys(Adapter::session());
_throw_status(status); _throw_status(status);
} }
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)
@ -98,14 +91,12 @@ void _throw_status(PEP_STATUS status)
stringstream build; stringstream build;
build << setfill('0') << "p≡p 0x" << setw(4) << hex << status; build << setfill('0') << "p≡p 0x" << setw(4) << hex << status;
throw runtime_error(build.str()); throw runtime_error(build.str());
} } else {
else {
throw runtime_error(pEp_status_to_string(status)); throw runtime_error(pEp_status_to_string(status));
} }
} }
PEP_STATUS _messageToSend(::message *msg) PEP_STATUS _messageToSend(::message *msg) {
{
pEpLog("called"); pEpLog("called");
try { try {
PyGILState_STATE gil = PyGILState_Ensure(); PyGILState_STATE gil = PyGILState_Ensure();
@ -115,13 +106,12 @@ PEP_STATUS _messageToSend(::message *msg)
call<void>(funcref.ptr(), Message()); call<void>(funcref.ptr(), Message());
PyGILState_Release(gil); PyGILState_Release(gil);
pEpLog("GIL released"); pEpLog("GIL released");
} catch (exception& e) { } } catch (exception &e) {}
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) {
{
pEpLog("called"); pEpLog("called");
try { try {
PyGILState_STATE gil = PyGILState_Ensure(); PyGILState_STATE gil = PyGILState_Ensure();
@ -131,43 +121,37 @@ PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handsha
call<void>(funcref.ptr(), me, partner, signal); call<void>(funcref.ptr(), me, partner, signal);
PyGILState_Release(gil); PyGILState_Release(gil);
pEpLog("GIL released"); pEpLog("GIL released");
} catch (exception& e) { } } catch (exception &e) {}
return PEP_STATUS_OK; return PEP_STATUS_OK;
} }
void start_sync() void start_sync() {
{
CallbackDispatcher::start_sync(); CallbackDispatcher::start_sync();
} }
void shutdown_sync() void shutdown_sync() {
{
CallbackDispatcher::stop_sync(); CallbackDispatcher::stop_sync();
} }
void debug_color(int ansi_color) void debug_color(int ansi_color) {
{
::set_debug_color(Adapter::session(), ansi_color); ::set_debug_color(Adapter::session(), ansi_color);
} }
void leave_device_group() void leave_device_group() {
{
::leave_device_group(Adapter::session()); ::leave_device_group(Adapter::session());
} }
bool is_sync_active() bool is_sync_active() {
{
return Adapter::is_sync_running(); return Adapter::is_sync_running();
} }
void testfunc() { void testfunc() {
_messageToSend(NULL); _messageToSend(NULL);
} }
void deliverHandshakeResult(int result, object identities) void deliverHandshakeResult(int result, object identities) {
{
identity_list *shared_identities = nullptr; identity_list *shared_identities = nullptr;
if (identities != boost::python::api::object() && boost::python::len(identities)) { if (identities != boost::python::api::object() && boost::python::len(identities)) {
shared_identities = new_identity_list(nullptr); shared_identities = new_identity_list(nullptr);
@ -176,14 +160,14 @@ void deliverHandshakeResult(int result, object identities)
try { try {
identity_list *si = shared_identities; identity_list *si = shared_identities;
for (int i=0; i < boost::python::len(identities); ++i) { for (int i = 0; i < boost::python::len(identities); ++i) {
Identity ident = extract< Identity >(identities[i]); Identity ident = extract<Identity>(identities[i]);
si = identity_list_add(si, ident); si = identity_list_add(si, ident);
if (!si) if (!si)
throw bad_alloc(); throw bad_alloc();
} }
} }
catch (exception& ex) { catch (exception &ex) {
free_identity_list(shared_identities); free_identity_list(shared_identities);
throw ex; throw ex;
} }
@ -192,10 +176,9 @@ void deliverHandshakeResult(int result, object identities)
PEP_STATUS status = ::deliverHandshakeResult(Adapter::session(), (sync_handshake_result) result, shared_identities); PEP_STATUS status = ::deliverHandshakeResult(Adapter::session(), (sync_handshake_result) result, shared_identities);
free_identity_list(shared_identities); free_identity_list(shared_identities);
_throw_status(status); _throw_status(status);
} }
BOOST_PYTHON_MODULE(native_pEp) BOOST_PYTHON_MODULE(native_pEp) {
{
init_before_main_module(); init_before_main_module();
// Module init function called by pEp.init() // Module init function called by pEp.init()
@ -328,7 +311,7 @@ BOOST_PYTHON_MODULE(native_pEp)
"\n" "\n"
" data bytes-like object\n" " data bytes-like object\n"
" mime_type MIME type for the data\n" " mime_type MIME type for the data\n"
" filename filename to store the data\n" , " filename filename to store the data\n",
boost::python::init< object, char const*, char const* >(args("data", "mime_type", "filename"))) boost::python::init< object, char const*, char const* >(args("data", "mime_type", "filename")))
.def(boost::python::init<object, string>()) .def(boost::python::init<object, string>())
.def(boost::python::init<object>()) .def(boost::python::init<object>())
@ -440,8 +423,8 @@ BOOST_PYTHON_MODULE(native_pEp)
"0: unencrypted, 1: inline PGP, 2: S/MIME, 3: PGP/MIME, 4: p≡p format") "0: unencrypted, 1: inline PGP, 2: S/MIME, 3: PGP/MIME, 4: p≡p format")
.def("encrypt", (Message(Message::*)())&Message::encrypt) .def("encrypt", (Message(Message::*)())&Message::encrypt)
.def("encrypt", (Message(Message::*)(boost::python::list))&Message::_encrypt) .def("encrypt", (Message(Message::*)(boost::python::list))&Message::_encrypt)
.def("encrypt", (Message(Message::*)(boost::python::list,int))&Message::_encrypt) .def("encrypt", (Message(Message::*)(boost::python::list, int))&Message::_encrypt)
.def("encrypt", (Message(Message::*)(boost::python::list,int,int))&Message::_encrypt, .def("encrypt", (Message(Message::*)(boost::python::list, int, int))&Message::_encrypt,
"msg2 = msg1.encrypt(extra_keys=[], enc_format='pEp', flags=0)\n" "msg2 = msg1.encrypt(extra_keys=[], enc_format='pEp', flags=0)\n"
"\n" "\n"
"encrypts a p≡p message and returns the encrypted message\n" "encrypts a p≡p message and returns the encrypted message\n"
@ -590,16 +573,16 @@ BOOST_PYTHON_MODULE(native_pEp)
// Sync API // Sync API
enum_<sync_handshake_signal>("sync_handshake_signal") enum_<sync_handshake_signal>("sync_handshake_signal")
.value("SYNC_NOTIFY_UNDEFINED" , SYNC_NOTIFY_UNDEFINED) .value("SYNC_NOTIFY_UNDEFINED", SYNC_NOTIFY_UNDEFINED)
.value("SYNC_NOTIFY_INIT_ADD_OUR_DEVICE" , SYNC_NOTIFY_INIT_ADD_OUR_DEVICE) .value("SYNC_NOTIFY_INIT_ADD_OUR_DEVICE", SYNC_NOTIFY_INIT_ADD_OUR_DEVICE)
.value("SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE" , SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE) .value("SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE", SYNC_NOTIFY_INIT_ADD_OTHER_DEVICE)
.value("SYNC_NOTIFY_INIT_FORM_GROUP" , SYNC_NOTIFY_INIT_FORM_GROUP) .value("SYNC_NOTIFY_INIT_FORM_GROUP", SYNC_NOTIFY_INIT_FORM_GROUP)
.value("SYNC_NOTIFY_TIMEOUT" , SYNC_NOTIFY_TIMEOUT) .value("SYNC_NOTIFY_TIMEOUT", SYNC_NOTIFY_TIMEOUT)
.value("SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED" , SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED) .value("SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED", SYNC_NOTIFY_ACCEPTED_DEVICE_ADDED)
.value("SYNC_NOTIFY_ACCEPTED_GROUP_CREATED", SYNC_NOTIFY_ACCEPTED_GROUP_CREATED) .value("SYNC_NOTIFY_ACCEPTED_GROUP_CREATED", SYNC_NOTIFY_ACCEPTED_GROUP_CREATED)
.value("SYNC_NOTIFY_ACCEPTED_DEVICE_ACCEPTED", SYNC_NOTIFY_ACCEPTED_DEVICE_ACCEPTED) .value("SYNC_NOTIFY_ACCEPTED_DEVICE_ACCEPTED", SYNC_NOTIFY_ACCEPTED_DEVICE_ACCEPTED)
.value("SYNC_NOTIFY_SOLE" , SYNC_NOTIFY_SOLE) .value("SYNC_NOTIFY_SOLE", SYNC_NOTIFY_SOLE)
.value("SYNC_NOTIFY_IN_GROUP" , SYNC_NOTIFY_IN_GROUP); .value("SYNC_NOTIFY_IN_GROUP", SYNC_NOTIFY_IN_GROUP);
// auto user_interface_class = class_<UserInterface, UserInterface_callback, boost::noncopyable>( // auto user_interface_class = class_<UserInterface, UserInterface_callback, boost::noncopyable>(
// "UserInterface", // "UserInterface",
@ -667,7 +650,7 @@ BOOST_PYTHON_MODULE(native_pEp)
// codecs // codecs
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 PythonAdapter
} // namespace pEp } // namespace pEp

31
src/pEp/native_pEp/pEpmodule.hh

@ -11,18 +11,25 @@
#include "message.hh" #include "message.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
extern string device_name; extern string device_name;
void config_passive_mode(bool enable);
void config_unencrypted_subject(bool enable); void config_passive_mode(bool enable);
void key_reset_user(string user_id, string fpr);
void key_reset_all_own_keys(); void config_unencrypted_subject(bool enable);
void _throw_status(PEP_STATUS status);
PEP_STATUS _messageToSend(::message *msg); void key_reset_user(string user_id, string fpr);
PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal);
void key_reset_all_own_keys();
} /* namespace PythonAdapter */
void _throw_status(PEP_STATUS status);
PEP_STATUS _messageToSend(::message *msg);
PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal);
} /* namespace PythonAdapter */
} /* namespace pEp */ } /* namespace pEp */
#endif /* PEPMODULE_HH */ #endif /* PEPMODULE_HH */

86
src/pEp/native_pEp/str_attr.cc

@ -10,56 +10,49 @@
#include "str_attr.hh" #include "str_attr.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std; using namespace std;
using namespace boost::python; using namespace boost::python;
using namespace boost::locale; 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) {
@ -68,17 +61,16 @@ boost::python::list strlist_attr(stringlist_t *&sl)
} }
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();
stringlist_t *_s = _sl; stringlist_t *_s = _sl;
for (int i=0; i<len(value); i++) { for (int i = 0; i < len(value); i++) {
extract< string > extract_string(value[i]); extract <string> extract_string(value[i]);
if (!extract_string.check()) { if (!extract_string.check()) {
free_stringlist(_sl); free_stringlist(_sl);
} }
@ -93,10 +85,9 @@ void strlist_attr(stringlist_t *&sl, boost::python::list value)
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 =
@ -111,18 +102,17 @@ dict strdict_attr(stringpair_list_t *&spl)
} }
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();
stringpair_list_t *_s = _spl; stringpair_list_t *_s = _spl;
for (int i=0; i<len(value); i++) { for (int i = 0; i < len(value); i++) {
extract< string > extract_key(value.keys()[i]); extract <string> extract_key(value.keys()[i]);
extract< string > extract_value(value.values()[i]); extract <string> extract_value(value.values()[i]);
if (!(extract_key.check() && extract_value.check())) if (!(extract_key.check() && extract_value.check()))
free_stringpair_list(_spl); free_stringpair_list(_spl);
@ -145,17 +135,16 @@ void strdict_attr(stringpair_list_t *&spl, dict value)
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();
stringlist_t *_s = result; stringlist_t *_s = result;
for (int i=0; i<len(l); i++) { for (int i = 0; i < len(l); i++) {
extract< string > extract_string(l[i]); extract <string> extract_string(l[i]);
if (!extract_string.check()) if (!extract_string.check())
free_stringlist(result); free_stringlist(result);
string s = extract_string(); string s = extract_string();
@ -167,18 +156,17 @@ stringlist_t *to_stringlist(boost::python::list l)
} }
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 PythonAdapter
} // namespace pEp { } // namespace pEp {

59
src/pEp/native_pEp/user_interface.cc

@ -8,45 +8,40 @@
#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; using namespace boost::python;
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(Adapter::session(), // PEP_STATUS status = ::register_sync_callbacks(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(Adapter::session()); // ::unregister_sync_callbacks(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;
@ -54,10 +49,9 @@ PEP_STATUS UserInterface::_notifyHandshake(
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() && boost::python::len(identities)) { if (identities != boost::python::api::object() && boost::python::len(identities)) {
shared_identities = new_identity_list(nullptr); shared_identities = new_identity_list(nullptr);
@ -66,14 +60,14 @@ void UserInterface::deliverHandshakeResult(int result, object identities)
try { try {
identity_list *si = shared_identities; identity_list *si = shared_identities;
for (int i=0; i < boost::python::len(identities); ++i) { for (int i = 0; i < boost::python::len(identities); ++i) {
Identity ident = extract< Identity >(identities[i]); Identity ident = extract<Identity>(identities[i]);
si = identity_list_add(si, ident); si = identity_list_add(si, ident);
if (!si) if (!si)
throw bad_alloc(); throw bad_alloc();
} }
} }
catch (exception& ex) { catch (exception &ex) {
free_identity_list(shared_identities); free_identity_list(shared_identities);
throw ex; throw ex;
} }
@ -83,7 +77,7 @@ void UserInterface::deliverHandshakeResult(int result, object identities)
(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)
//{ //{
@ -119,12 +113,11 @@ void UserInterface::deliverHandshakeResult(int result, object identities)
// 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 PythonAdapter
} // namespace pEp { } // namespace pEp {

23
src/pEp/native_pEp/user_interface.hh

@ -16,19 +16,19 @@
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
class UserInterface { class UserInterface {
static UserInterface *_ui; static UserInterface *_ui;
public: public:
UserInterface(); UserInterface();
virtual ~UserInterface(); virtual ~UserInterface();
virtual void notifyHandshake( virtual void notifyHandshake(
Identity me, Identity me,
Identity partner, Identity partner,
sync_handshake_signal signal) sync_handshake_signal signal) {
{
throw runtime_error("override this method"); throw runtime_error("override this method");
} }
@ -36,14 +36,15 @@ public:
// 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 {
PyObject *_self; PyObject *_self;
public: public:
UserInterface_callback(PyObject *self); UserInterface_callback(PyObject *self);
~UserInterface_callback(); ~UserInterface_callback();
void notifyHandshake( void notifyHandshake(
@ -51,9 +52,9 @@ public:
Identity partner, Identity partner,
sync_handshake_signal signal sync_handshake_signal signal
); );
}; };
} /* namespace PythonAdapter */ } /* namespace PythonAdapter */
} /* namespace pEp */ } /* namespace pEp */
#endif /* USER_INTERFACE_HH */ #endif /* USER_INTERFACE_HH */

Loading…
Cancel
Save