
16 changed files with 1556 additions and 1472 deletions
@ -1,164 +1,172 @@ |
|||||
// 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; |
||||
{ |
|
||||
if (ident.address() == "") |
void update_identity(Identity& ident) |
||||
throw invalid_argument("address needed"); |
{ |
||||
if (ident.user_id() == PEP_OWN_USERID) |
if (ident.address() == "") |
||||
throw runtime_error("update_identity: '" PEP_OWN_USERID |
throw invalid_argument("address needed"); |
||||
"' may only be used for own identities"); |
if (ident.user_id() == PEP_OWN_USERID) |
||||
|
throw runtime_error("update_identity: '" PEP_OWN_USERID |
||||
PEP_STATUS status = update_identity(pEp::Adapter::session(), ident); |
"' may only be used for own identities"); |
||||
_throw_status(status); |
|
||||
} |
PEP_STATUS status = update_identity(pEp::Adapter::session(), ident); |
||||
|
_throw_status(status); |
||||
void myself(Identity& ident) |
} |
||||
{ |
|
||||
if (ident.address() == "") |
|
||||
throw invalid_argument("address needed"); |
|
||||
if (ident.username() == "") |
|
||||
throw invalid_argument("username needed"); |
|
||||
|
|
||||
if (ident.user_id() == "") |
void myself(Identity& ident) |
||||
ident.user_id(ident.address()); |
{ |
||||
|
if (ident.address() == "") |
||||
|
throw invalid_argument("address needed"); |
||||
|
if (ident.username() == "") |
||||
|
throw invalid_argument("username needed"); |
||||
|
|
||||
PEP_STATUS status = myself(pEp::Adapter::session(), ident); |
if (ident.user_id() == "") |
||||
_throw_status(status); |
ident.user_id(ident.address()); |
||||
} |
|
||||
|
|
||||
string _trustwords(Identity me, Identity partner, string lang, bool full) |
PEP_STATUS status = myself(pEp::Adapter::session(), ident); |
||||
{ |
_throw_status(status); |
||||
if (me.fpr() == "" || partner.fpr() == "") |
} |
||||
throw invalid_argument("fingerprint needed in Identities"); |
|
||||
|
|
||||
if (lang == "" && me.lang() == partner.lang()) |
string _trustwords(Identity me, Identity partner, string lang, bool full) |
||||
lang = me.lang(); |
{ |
||||
|
if (me.fpr() == "" || partner.fpr() == "") |
||||
|
throw invalid_argument("fingerprint needed in Identities"); |
||||
|
|
||||
char *words = NULL; |
if (lang == "" && me.lang() == partner.lang()) |
||||
size_t size = 0; |
lang = me.lang(); |
||||
PEP_STATUS status = get_trustwords(pEp::Adapter::session(), me, partner, |
|
||||
lang.c_str(),&words, &size, full); |
|
||||
_throw_status(status); |
|
||||
return words; |
|
||||
} |
|
||||
|
|
||||
void trust_personal_key(Identity ident) |
char *words = NULL; |
||||
{ |
size_t size = 0; |
||||
if (ident.fpr() == "") |
PEP_STATUS status = get_trustwords(pEp::Adapter::session(), me, partner, |
||||
throw invalid_argument("fingerprint needed in Identities"); |
lang.c_str(),&words, &size, full); |
||||
if (ident.user_id() == "") |
_throw_status(status); |
||||
throw invalid_argument("user_id must be provided"); |
return words; |
||||
|
} |
||||
|
|
||||
PEP_STATUS status = trust_personal_key(pEp::Adapter::session(), ident); |
void trust_personal_key(Identity ident) |
||||
_throw_status(status); |
{ |
||||
} |
if (ident.fpr() == "") |
||||
|
throw invalid_argument("fingerprint needed in Identities"); |
||||
|
if (ident.user_id() == "") |
||||
|
throw invalid_argument("user_id must be provided"); |
||||
|
|
||||
void set_identity_flags(Identity ident, identity_flags_t flags) |
PEP_STATUS status = trust_personal_key(pEp::Adapter::session(), ident); |
||||
{ |
_throw_status(status); |
||||
if (ident.address() == "") |
} |
||||
throw invalid_argument("address needed"); |
|
||||
if (ident.user_id() == "") |
|
||||
throw invalid_argument("user_id needed"); |
|
||||
|
|
||||
PEP_STATUS status = set_identity_flags(pEp::Adapter::session(), ident, flags); |
void set_identity_flags(Identity ident, identity_flags_t flags) |
||||
_throw_status(status); |
{ |
||||
} |
if (ident.address() == "") |
||||
|
throw invalid_argument("address needed"); |
||||
|
if (ident.user_id() == "") |
||||
|
throw invalid_argument("user_id needed"); |
||||
|
|
||||
void unset_identity_flags(Identity ident, identity_flags_t flags) |
PEP_STATUS status = set_identity_flags(pEp::Adapter::session(), ident, flags); |
||||
{ |
_throw_status(status); |
||||
if (ident.address() == "") |
} |
||||
throw invalid_argument("address needed"); |
|
||||
if (ident.user_id() == "") |
|
||||
throw invalid_argument("user_id needed"); |
|
||||
|
|
||||
PEP_STATUS status = unset_identity_flags(pEp::Adapter::session(), ident, flags); |
void unset_identity_flags(Identity ident, identity_flags_t flags) |
||||
_throw_status(status); |
{ |
||||
} |
if (ident.address() == "") |
||||
|
throw invalid_argument("address needed"); |
||||
|
if (ident.user_id() == "") |
||||
|
throw invalid_argument("user_id needed"); |
||||
|
|
||||
void key_reset_trust(Identity ident) |
PEP_STATUS status = unset_identity_flags(pEp::Adapter::session(), ident, flags); |
||||
{ |
_throw_status(status); |
||||
if (ident.fpr() == "") |
} |
||||
throw invalid_argument("fpr needed"); |
|
||||
if (ident.address() == "") |
|
||||
throw invalid_argument("address needed"); |
|
||||
if (ident.user_id() == "") |
|
||||
throw invalid_argument("user_id needed"); |
|
||||
|
|
||||
PEP_STATUS status = key_reset_trust(pEp::Adapter::session(), ident); |
|
||||
_throw_status(status); |
|
||||
} |
|
||||
|
|
||||
|
void key_reset_trust(Identity ident) |
||||
|
{ |
||||
|
if (ident.fpr() == "") |
||||
|
throw invalid_argument("fpr needed"); |
||||
|
if (ident.address() == "") |
||||
|
throw invalid_argument("address needed"); |
||||
|
if (ident.user_id() == "") |
||||
|
throw invalid_argument("user_id needed"); |
||||
|
|
||||
|
PEP_STATUS status = key_reset_trust(pEp::Adapter::session(), ident); |
||||
|
_throw_status(status); |
||||
|
} |
||||
|
|
||||
|
|
||||
boost::python::list import_key(string key_data) |
|
||||
{ |
|
||||
::identity_list *private_keys = NULL; |
|
||||
PEP_STATUS status = ::import_key(pEp::Adapter::session(), key_data.c_str(), key_data.size(), &private_keys); |
|
||||
if (status && status != PEP_KEY_IMPORTED) |
|
||||
_throw_status(status); |
|
||||
|
|
||||
auto result = boost::python::list(); |
boost::python::list import_key(string key_data) |
||||
for (::identity_list *il = private_keys; il && il->ident; il=il->next) { |
{ |
||||
::pEp_identity *ident = ::identity_dup(il->ident); |
::identity_list *private_keys = NULL; |
||||
if (!ident) { |
PEP_STATUS status = ::import_key(pEp::Adapter::session(), key_data.c_str(), key_data.size(), &private_keys); |
||||
free_identity_list(private_keys); |
if (status && status != PEP_KEY_IMPORTED) |
||||
throw bad_alloc(); |
_throw_status(status); |
||||
} |
|
||||
result.append(Identity(ident)); |
|
||||
} |
|
||||
|
|
||||
|
auto result = boost::python::list(); |
||||
|
for (::identity_list *il = private_keys; il && il->ident; il=il->next) { |
||||
|
::pEp_identity *ident = ::identity_dup(il->ident); |
||||
|
if (!ident) { |
||||
free_identity_list(private_keys); |
free_identity_list(private_keys); |
||||
return result; |
throw bad_alloc(); |
||||
} |
} |
||||
|
result.append(Identity(ident)); |
||||
|
} |
||||
|
|
||||
string export_key(Identity ident) |
free_identity_list(private_keys); |
||||
{ |
return result; |
||||
PEP_STATUS status = PEP_STATUS_OK; |
} |
||||
char* key_data = NULL; |
|
||||
size_t size; |
|
||||
status = ::export_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); |
|
||||
|
|
||||
_throw_status(status); |
string export_key(Identity ident) |
||||
return key_data; |
{ |
||||
} |
PEP_STATUS status = PEP_STATUS_OK; |
||||
|
char* key_data = NULL; |
||||
|
size_t size; |
||||
|
status = ::export_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); |
||||
|
|
||||
string export_secret_key(Identity ident) |
_throw_status(status); |
||||
{ |
return key_data; |
||||
PEP_STATUS status = PEP_STATUS_OK; |
} |
||||
char* key_data = NULL; |
|
||||
size_t size; |
|
||||
status = ::export_secret_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); |
|
||||
|
|
||||
_throw_status(status); |
string export_secret_key(Identity ident) |
||||
return key_data; |
{ |
||||
} |
PEP_STATUS status = PEP_STATUS_OK; |
||||
|
char* key_data = NULL; |
||||
|
size_t size; |
||||
|
status = ::export_secret_key(pEp::Adapter::session(), ident.fpr().c_str(), &key_data, &size); |
||||
|
|
||||
void set_own_key(Identity& ident, string fpr) |
_throw_status(status); |
||||
{ |
return key_data; |
||||
if (ident.address() == "") |
} |
||||
throw invalid_argument("address needed"); |
|
||||
if (ident.username() == "") |
void set_own_key(Identity& ident, string fpr) |
||||
throw invalid_argument("username needed"); |
{ |
||||
if (ident.user_id() == "") |
if (ident.address() == "") |
||||
throw invalid_argument("user_id needed"); |
throw invalid_argument("address needed"); |
||||
if (fpr == "") |
if (ident.username() == "") |
||||
throw invalid_argument("fpr needed"); |
throw invalid_argument("username needed"); |
||||
|
if (ident.user_id() == "") |
||||
|
throw invalid_argument("user_id needed"); |
||||
const char* fpr_c = fpr.c_str(); |
if (fpr == "") |
||||
PEP_STATUS status = set_own_key(pEp::Adapter::session(), ident, fpr_c); |
throw invalid_argument("fpr needed"); |
||||
_throw_status(status); |
|
||||
} |
|
||||
} |
const char* fpr_c = fpr.c_str(); |
||||
|
PEP_STATUS status = set_own_key(pEp::Adapter::session(), ident, fpr_c); |
||||
|
_throw_status(status); |
||||
} |
} |
||||
|
|
||||
|
} // namespace PythonAdapter
|
||||
|
} // namespace pEp {
|
||||
|
|
||||
|
|
||||
|
@ -1,279 +1,285 @@ |
|||||
// 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; |
||||
Identity::Identity(string address, string username, string user_id, |
using namespace boost::python; |
||||
string fpr, int comm_type, string lang, identity_flags_t flags) |
|
||||
: _ident(new_identity(address.c_str(), fpr.c_str(), user_id.c_str(), |
Identity::Identity(string address, string username, string user_id, |
||||
username.c_str()), &::free_identity) |
string fpr, int comm_type, string lang, identity_flags_t flags) |
||||
{ |
: _ident(new_identity(address.c_str(), fpr.c_str(), user_id.c_str(), |
||||
if (!_ident) |
username.c_str()), &::free_identity) |
||||
throw bad_alloc(); |
{ |
||||
_ident->comm_type = (PEP_comm_type) comm_type; |
if (!_ident) |
||||
_ident->flags = (identity_flags_t) flags; |
throw bad_alloc(); |
||||
this->lang(lang); |
_ident->comm_type = (PEP_comm_type) comm_type; |
||||
} |
_ident->flags = (identity_flags_t) flags; |
||||
|
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; |
||||
if (_ident->address) |
if (_ident->address) |
||||
address = string(_ident->address); |
address = string(_ident->address); |
||||
build << repr(address) << ", "; |
build << repr(address) << ", "; |
||||
string username; |
string username; |
||||
if (_ident->username) |
if (_ident->username) |
||||
username = string(_ident->username); |
username = string(_ident->username); |
||||
build << repr(username) << ", "; |
build << repr(username) << ", "; |
||||
string user_id; |
string user_id; |
||||
if (_ident->user_id) |
if (_ident->user_id) |
||||
user_id = string(_ident->user_id); |
user_id = string(_ident->user_id); |
||||
build << repr(user_id) << ", "; |
build << repr(user_id) << ", "; |
||||
string fpr; |
string fpr; |
||||
if (_ident->fpr) |
if (_ident->fpr) |
||||
fpr = string(_ident->fpr); |
fpr = string(_ident->fpr); |
||||
build << repr(fpr) << ", "; |
build << repr(fpr) << ", "; |
||||
build << (int) _ident->comm_type << ", "; |
build << (int) _ident->comm_type << ", "; |
||||
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"); |
||||
|
|
||||
PEP_rating rating = PEP_rating_undefined; |
PEP_rating rating = PEP_rating_undefined; |
||||
PEP_STATUS status = ::identity_rating(pEp::Adapter::session(), _ident.get(), &rating); |
PEP_STATUS status = ::identity_rating(pEp::Adapter::session(), _ident.get(), &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(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) |
||||
throw length_error("lang must be an ISO 639-1 language code or empty"); |
throw length_error("lang must be an ISO 639-1 language code or empty"); |
||||
|
|
||||
// FIXME: should set .me
|
// FIXME: should set .me
|
||||
// _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"); |
||||
|
|
||||
pEp_identity *_dup = identity_dup(ident); |
pEp_identity *_dup = identity_dup(ident); |
||||
if (!_dup) |
if (!_dup) |
||||
throw bad_alloc(); |
throw bad_alloc(); |
||||
|
|
||||
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(); |
||||
PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); |
PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); |
||||
_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 result; |
||||
|
|
||||
boost::python::list identitylist_attr(identity_list *&il) |
for (identity_list *_il = il; _il && _il->ident; _il = _il->next) { |
||||
{ |
pEp_identity *ident = ::identity_dup(_il->ident); |
||||
boost::python::list result; |
if (!ident) |
||||
|
throw bad_alloc(); |
||||
|
result.append(object(Identity(ident))); |
||||
|
} |
||||
|
|
||||
for (identity_list *_il = il; _il && _il->ident; _il = _il->next) { |
return result; |
||||
pEp_identity *ident = ::identity_dup(_il->ident); |
} |
||||
if (!ident) |
|
||||
throw bad_alloc(); |
|
||||
result.append(object(Identity(ident))); |
|
||||
} |
|
||||
|
|
||||
return result; |
void identitylist_attr(identity_list *&il, boost::python::list value) |
||||
|
{ |
||||
|
identity_list *_il = new_identity_list(NULL); |
||||
|
if (!_il) |
||||
|
throw bad_alloc(); |
||||
|
|
||||
|
identity_list *_i = _il; |
||||
|
for (int i=0; i<len(value); i++) { |
||||
|
extract< Identity& > extract_identity(value[i]); |
||||
|
if (!extract_identity.check()) { |
||||
|
free_identity_list(_il); |
||||
} |
} |
||||
|
pEp_identity *_ident = extract_identity(); |
||||
void identitylist_attr(identity_list *&il, boost::python::list value) |
pEp_identity *_dup = ::identity_dup(_ident); |
||||
{ |
if (!_dup) { |
||||
identity_list *_il = new_identity_list(NULL); |
free_identity_list(_il); |
||||
if (!_il) |
throw bad_alloc(); |
||||
throw bad_alloc(); |
} |
||||
|
PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); |
||||
identity_list *_i = _il; |
if (status != PEP_STATUS_OK) { |
||||
for (int i=0; i<len(value); i++) { |
free_identity_list(_il); |
||||
extract< Identity& > extract_identity(value[i]); |
_throw_status(status); |
||||
if (!extract_identity.check()) { |
} |
||||
free_identity_list(_il); |
_i = identity_list_add(_i, _dup); |
||||
} |
if (!_i) { |
||||
pEp_identity *_ident = extract_identity(); |
free_identity_list(_il); |
||||
pEp_identity *_dup = ::identity_dup(_ident); |
throw bad_alloc(); |
||||
if (!_dup) { |
|
||||
free_identity_list(_il); |
|
||||
throw bad_alloc(); |
|
||||
} |
|
||||
PEP_STATUS status = update_identity(pEp::Adapter::session(), _dup); |
|
||||
if (status != PEP_STATUS_OK) { |
|
||||
free_identity_list(_il); |
|
||||
_throw_status(status); |
|
||||
} |
|
||||
_i = identity_list_add(_i, _dup); |
|
||||
if (!_i) { |
|
||||
free_identity_list(_il); |
|
||||
throw bad_alloc(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
free_identity_list(il); |
|
||||
il = _il; |
|
||||
} |
} |
||||
} |
} |
||||
|
|
||||
|
free_identity_list(il); |
||||
|
il = _il; |
||||
} |
} |
||||
|
|
||||
|
} // namespace PythonAdapter
|
||||
|
} // namespace pEp {
|
||||
|
|
||||
|
@ -1,408 +1,413 @@ |
|||||
// 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) : |
|
||||
_bl(bl), part_of_chain(chained) |
Message::Blob::Blob(bloblist_t *bl, bool chained) : |
||||
{ |
_bl(bl), part_of_chain(chained) |
||||
if (!_bl) |
{ |
||||
throw bad_alloc(); |
if (!_bl) |
||||
} |
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(); |
||||
|
|
||||
|
Py_buffer src; |
||||
|
int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO); |
||||
|
if (result) |
||||
|
throw invalid_argument("need a contiguous buffer to read"); |
||||
|
|
||||
|
char *mem = (char *)malloc(src.len); |
||||
|
if (!mem) { |
||||
|
PyBuffer_Release(&src); |
||||
|
throw bad_alloc(); |
||||
|
} |
||||
|
|
||||
Py_buffer src; |
memcpy(mem, src.buf, src.len); |
||||
int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO); |
free(_bl->value); |
||||
if (result) |
_bl->size = src.len; |
||||
throw invalid_argument("need a contiguous buffer to read"); |
_bl->value = mem; |
||||
|
|
||||
char *mem = (char *)malloc(src.len); |
PyBuffer_Release(&src); |
||||
if (!mem) { |
|
||||
PyBuffer_Release(&src); |
|
||||
throw bad_alloc(); |
|
||||
} |
|
||||
|
|
||||
memcpy(mem, src.buf, src.len); |
this->mime_type(mime_type); |
||||
free(_bl->value); |
this->filename(filename); |
||||
_bl->size = src.len; |
} |
||||
_bl->value = mem; |
|
||||
|
|
||||
PyBuffer_Release(&src); |
Message::Blob::Blob(const Message::Blob& second) : |
||||
|
_bl(second._bl), part_of_chain(true) |
||||
|
{ |
||||
|
|
||||
this->mime_type(mime_type); |
} |
||||
this->filename(filename); |
|
||||
} |
|
||||
|
|
||||
Message::Blob::Blob(const Message::Blob& second) : |
Message::Blob::~Blob() |
||||
_bl(second._bl), part_of_chain(true) |
{ |
||||
{ |
if (!part_of_chain) { |
||||
|
free(_bl->value); |
||||
|
free(_bl); |
||||
|
} |
||||
|
} |
||||
|
|
||||
} |
string Message::Blob::_repr() |
||||
|
{ |
||||
|
stringstream build; |
||||
|
build << "Blob("; |
||||
|
if (!_bl) { |
||||
|
build << "b'', '', ''"; |
||||
|
} |
||||
|
else { |
||||
|
build << "bytes(" << _bl->size << "), "; |
||||
|
string mime_type; |
||||
|
if (_bl->mime_type) |
||||
|
mime_type = string(_bl->mime_type); |
||||
|
string filename; |
||||
|
if (_bl->filename) |
||||
|
filename = string(_bl->filename); |
||||
|
build << repr(mime_type) << ", "; |
||||
|
build << repr(filename); |
||||
|
} |
||||
|
build << ")"; |
||||
|
return build.str(); |
||||
|
} |
||||
|
|
||||
Message::Blob::~Blob() |
int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { |
||||
{ |
bloblist_t *bl = NULL; |
||||
if (!part_of_chain) { |
|
||||
free(_bl->value); |
|
||||
free(_bl); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
string Message::Blob::_repr() |
try { |
||||
{ |
Message::Blob& blob = extract< Message::Blob& >(self); |
||||
stringstream build; |
bl = blob._bl; |
||||
build << "Blob("; |
} |
||||
if (!_bl) { |
catch (exception& e) { |
||||
build << "b'', '', ''"; |
PyErr_SetString(PyExc_RuntimeError, "extract not possible"); |
||||
} |
view->obj = NULL; |
||||
else { |
return -1; |
||||
build << "bytes(" << _bl->size << "), "; |
} |
||||
string mime_type; |
|
||||
if (_bl->mime_type) |
|
||||
mime_type = string(_bl->mime_type); |
|
||||
string filename; |
|
||||
if (_bl->filename) |
|
||||
filename = string(_bl->filename); |
|
||||
build << repr(mime_type) << ", "; |
|
||||
build << repr(filename); |
|
||||
} |
|
||||
build << ")"; |
|
||||
return build.str(); |
|
||||
} |
|
||||
|
|
||||
int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { |
if (!(bl && bl->value)) { |
||||
bloblist_t *bl = NULL; |
PyErr_SetString(PyExc_RuntimeError, "no data available"); |
||||
|
view->obj = NULL; |
||||
|
return -1; |
||||
|
} |
||||
|
|
||||
try { |
return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags); |
||||
Message::Blob& blob = extract< Message::Blob& >(self); |
} |
||||
bl = blob._bl; |
|
||||
} |
|
||||
catch (exception& e) { |
|
||||
PyErr_SetString(PyExc_RuntimeError, "extract not possible"); |
|
||||
view->obj = NULL; |
|
||||
return -1; |
|
||||
} |
|
||||
|
|
||||
if (!(bl && bl->value)) { |
string Message::Blob::decode(string encoding) |
||||
PyErr_SetString(PyExc_RuntimeError, "no data available"); |
{ |
||||
view->obj = NULL; |
if (encoding == "") { |
||||
return -1; |
string _mime_type = _bl->mime_type ? _bl->mime_type : ""; |
||||
} |
encoding = "ascii"; |
||||
|
|
||||
return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags); |
if (_mime_type == "application/pEp.sync") |
||||
} |
encoding = "pep.sync"; |
||||
|
|
||||
string Message::Blob::decode(string encoding) |
if (_mime_type == "application/pEp.keyreset") |
||||
{ |
encoding = "pep.distribution"; |
||||
if (encoding == "") { |
|
||||
string _mime_type = _bl->mime_type ? _bl->mime_type : ""; |
|
||||
encoding = "ascii"; |
|
||||
|
|
||||
if (_mime_type == "application/pEp.sync") |
} |
||||
encoding = "pep.sync"; |
object codecs = import("codecs"); |
||||
|
object _decode = codecs.attr("decode"); |
||||
|
return call< string >(_decode.ptr(), this, encoding); |
||||
|
} |
||||
|
|
||||
if (_mime_type == "application/pEp.keyreset") |
PyBufferProcs Message::Blob::bp = { getbuffer, NULL }; |
||||
encoding = "pep.distribution"; |
|
||||
|
|
||||
} |
Message::Message(int dir, Identity *from) |
||||
object codecs = import("codecs"); |
: _msg(new_message((PEP_msg_direction) dir), &free_message) |
||||
object _decode = codecs.attr("decode"); |
{ |
||||
return call< string >(_decode.ptr(), this, encoding); |
if (!_msg) |
||||
} |
throw bad_alloc(); |
||||
|
|
||||
PyBufferProcs Message::Blob::bp = { getbuffer, NULL }; |
if (from) { |
||||
|
_msg->from = ::identity_dup(*from); |
||||
|
if (!_msg->from) |
||||
|
throw bad_alloc(); |
||||
|
_msg->dir = (PEP_msg_direction) dir; |
||||
|
} |
||||
|
} |
||||
|
|
||||
Message::Message(int dir, Identity *from) |
Message::Message(string mimetext) |
||||
: _msg(new_message((PEP_msg_direction) dir), &free_message) |
: _msg(NULL, &free_message) |
||||
{ |
{ |
||||
if (!_msg) |
message *_cpy; |
||||
|
PEP_STATUS status = mime_decode_message(mimetext.c_str(), |
||||
|
mimetext.size(), &_cpy, NULL); |
||||
|
switch (status) { |
||||
|
case PEP_STATUS_OK: |
||||
|
if (_cpy) |
||||
|
_cpy->dir = PEP_dir_outgoing; |
||||
|
else |
||||
|
_cpy = new_message(PEP_dir_outgoing); |
||||
|
|
||||
|
if (!_cpy) |
||||
throw bad_alloc(); |
throw bad_alloc(); |
||||
|
|
||||
if (from) { |
_msg = shared_ptr< message >(_cpy); |
||||
_msg->from = ::identity_dup(*from); |
break; |
||||
if (!_msg->from) |
|
||||
throw bad_alloc(); |
|
||||
_msg->dir = (PEP_msg_direction) dir; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
Message::Message(string mimetext) |
case PEP_BUFFER_TOO_SMALL: |
||||
: _msg(NULL, &free_message) |
throw runtime_error("mime_decode_message: buffer too small"); |
||||
{ |
|
||||
message *_cpy; |
|
||||
PEP_STATUS status = mime_decode_message(mimetext.c_str(), |
|
||||
mimetext.size(), &_cpy, NULL); |
|
||||
switch (status) { |
|
||||
case PEP_STATUS_OK: |
|
||||
if (_cpy) |
|
||||
_cpy->dir = PEP_dir_outgoing; |
|
||||
else |
|
||||
_cpy = new_message(PEP_dir_outgoing); |
|
||||
|
|
||||
if (!_cpy) |
|
||||
throw bad_alloc(); |
|
||||
|
|
||||
_msg = shared_ptr< message >(_cpy); |
|
||||
break; |
|
||||
|
|
||||
case PEP_BUFFER_TOO_SMALL: |
|
||||
throw runtime_error("mime_decode_message: buffer too small"); |
|
||||
|
|
||||
case PEP_CANNOT_CREATE_TEMP_FILE: |
|
||||
throw runtime_error("mime_decode_message: cannot create temp file"); |
|
||||
|
|
||||
case PEP_OUT_OF_MEMORY: |
|
||||
throw bad_alloc(); |
|
||||
|
|
||||
default: |
|
||||
stringstream build; |
|
||||
build << "mime_decode_message: unknown error (" << (int) status << ")"; |
|
||||
throw runtime_error(build.str()); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
Message::Message(const Message& second) |
case PEP_CANNOT_CREATE_TEMP_FILE: |
||||
: _msg(second._msg) |
throw runtime_error("mime_decode_message: cannot create temp file"); |
||||
{ |
|
||||
if (!_msg.get()) |
|
||||
throw bad_alloc(); |
|
||||
} |
|
||||
|
|
||||
Message::Message(message *msg) |
case PEP_OUT_OF_MEMORY: |
||||
: _msg(::message_dup(msg), &free_message) |
throw bad_alloc(); |
||||
{ |
|
||||
|
|
||||
} |
default: |
||||
|
stringstream build; |
||||
|
build << "mime_decode_message: unknown error (" << (int) status << ")"; |
||||
|
throw runtime_error(build.str()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
Message::~Message() |
Message::Message(const Message& second) |
||||
{ |
: _msg(second._msg) |
||||
|
{ |
||||
|
if (!_msg.get()) |
||||
|
throw bad_alloc(); |
||||
|
} |
||||
|
|
||||
} |
Message::Message(message *msg) |
||||
|
: _msg(::message_dup(msg), &free_message) |
||||
|
{ |
||||
|
|
||||
Message::operator message *() |
} |
||||
{ |
|
||||
return _msg.get(); |
|
||||
} |
|
||||
|
|
||||
Message::operator const message *() const |
Message::~Message() |
||||
{ |
{ |
||||
return _msg.get(); |
|
||||
} |
|
||||
|
|
||||
string Message::_str() |
} |
||||
{ |
|
||||
if (!(_msg->from && _msg->from->address && _msg->from->address[0])) |
|
||||
throw out_of_range(".from_.address missing"); |
|
||||
|
|
||||
char *mimetext; |
Message::operator message *() |
||||
string result; |
{ |
||||
|
return _msg.get(); |
||||
|
} |
||||
|
|
||||
PEP_STATUS status = mime_encode_message(*this, false, &mimetext, false); |
Message::operator const message *() const |
||||
switch (status) { |
{ |
||||
case PEP_STATUS_OK: |
return _msg.get(); |
||||
result = mimetext; |
} |
||||
free(mimetext); |
|
||||
break; |
|
||||
|
|
||||
case PEP_BUFFER_TOO_SMALL: |
string Message::_str() |
||||
throw runtime_error("mime_encode_message: buffer too small"); |
{ |
||||
|
if (!(_msg->from && _msg->from->address && _msg->from->address[0])) |
||||
|
throw out_of_range(".from_.address missing"); |
||||
|
|
||||
case PEP_CANNOT_CREATE_TEMP_FILE: |
char *mimetext; |
||||
throw runtime_error("mime_encode_message: cannot create temp file"); |
string result; |
||||
|
|
||||
case PEP_OUT_OF_MEMORY: |
PEP_STATUS status = mime_encode_message(*this, false, &mimetext, false); |
||||
throw bad_alloc(); |
switch (status) { |
||||
|
case PEP_STATUS_OK: |
||||
|
result = mimetext; |
||||
|
free(mimetext); |
||||
|
break; |
||||
|
|
||||
default: |
case PEP_BUFFER_TOO_SMALL: |
||||
stringstream build; |
throw runtime_error("mime_encode_message: buffer too small"); |
||||
build << "mime_encode_message: unknown error (" << (int) status << ")"; |
|
||||
throw runtime_error(build.str()); |
|
||||
} |
|
||||
|
|
||||
return result; |
case PEP_CANNOT_CREATE_TEMP_FILE: |
||||
} |
throw runtime_error("mime_encode_message: cannot create temp file"); |
||||
|
|
||||
string Message::_repr() |
case PEP_OUT_OF_MEMORY: |
||||
{ |
throw bad_alloc(); |
||||
|
|
||||
|
default: |
||||
stringstream build; |
stringstream build; |
||||
build << "Message(" << repr(_str()) << ")"; |
build << "mime_encode_message: unknown error (" << (int) status << ")"; |
||||
return build.str(); |
throw runtime_error(build.str()); |
||||
} |
} |
||||
|
|
||||
boost::python::tuple Message::attachments() |
return result; |
||||
{ |
} |
||||
boost::python::list l; |
|
||||
|
|
||||
for (bloblist_t *bl = _msg->attachments; bl && bl->value; bl = |
string Message::_repr() |
||||
bl->next) { |
{ |
||||
l.append(Blob(bl, true)); |
stringstream build; |
||||
} |
build << "Message(" << repr(_str()) << ")"; |
||||
|
return build.str(); |
||||
|
} |
||||
|
|
||||
return boost::python::tuple(l); |
boost::python::tuple Message::attachments() |
||||
} |
{ |
||||
|
boost::python::list l; |
||||
|
|
||||
void Message::attachments(boost::python::list value) |
for (bloblist_t *bl = _msg->attachments; bl && bl->value; bl = |
||||
{ |
bl->next) { |
||||
bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL); |
l.append(Blob(bl, true)); |
||||
if (!bl) |
} |
||||
throw bad_alloc(); |
|
||||
|
|
||||
bloblist_t *_l = bl; |
return boost::python::tuple(l); |
||||
for (int i=0; i<len(value); i++) { |
} |
||||
Message::Blob& blob = extract< Message::Blob& >(value[i]); |
|
||||
_l = bloblist_add(_l, blob._bl->value, blob._bl->size, |
|
||||
blob._bl->mime_type, blob._bl->filename); |
|
||||
if (!_l) { |
|
||||
for (_l = bl; _l && _l->value; ) { |
|
||||
free(_l->mime_type); |
|
||||
free(_l->filename); |
|
||||
bloblist_t *_ll = _l; |
|
||||
_l = _l->next; |
|
||||
free(_ll); |
|
||||
} |
|
||||
throw bad_alloc(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
for (int i=0; i<len(value); i++) { |
void Message::attachments(boost::python::list value) |
||||
Message::Blob& blob = extract< Message::Blob& >(value[i]); |
{ |
||||
blob._bl->value = NULL; |
bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL); |
||||
blob._bl->size = 0; |
if (!bl) |
||||
free(blob._bl->mime_type); |
throw bad_alloc(); |
||||
blob._bl->mime_type = NULL; |
|
||||
free(blob._bl->filename); |
bloblist_t *_l = bl; |
||||
blob._bl->filename = NULL; |
for (int i=0; i<len(value); i++) { |
||||
|
Message::Blob& blob = extract< Message::Blob& >(value[i]); |
||||
|
_l = bloblist_add(_l, blob._bl->value, blob._bl->size, |
||||
|
blob._bl->mime_type, blob._bl->filename); |
||||
|
if (!_l) { |
||||
|
for (_l = bl; _l && _l->value; ) { |
||||
|
free(_l->mime_type); |
||||
|
free(_l->filename); |
||||
|
bloblist_t *_ll = _l; |
||||
|
_l = _l->next; |
||||
|
free(_ll); |
||||
} |
} |
||||
|
throw bad_alloc(); |
||||
free_bloblist(_msg->attachments); |
|
||||
_msg->attachments = bl; |
|
||||
} |
} |
||||
|
} |
||||
|
|
||||
Message Message::encrypt() |
for (int i=0; i<len(value); i++) { |
||||
{ |
Message::Blob& blob = extract< Message::Blob& >(value[i]); |
||||
boost::python::list extra; |
blob._bl->value = NULL; |
||||
return encrypt_message(*this, extra, PEP_enc_PGP_MIME, 0); |
blob._bl->size = 0; |
||||
} |
free(blob._bl->mime_type); |
||||
|
blob._bl->mime_type = NULL; |
||||
|
free(blob._bl->filename); |
||||
|
blob._bl->filename = NULL; |
||||
|
} |
||||
|
|
||||
Message Message::_encrypt(boost::python::list extra, int enc_format, int flags) |
free_bloblist(_msg->attachments); |
||||
{ |
_msg->attachments = bl; |
||||
if (!enc_format) |
} |
||||
enc_format = PEP_enc_PGP_MIME; |
|
||||
return encrypt_message(*this, extra, enc_format, flags); |
|
||||
} |
|
||||
|
|
||||
boost::python::tuple Message::decrypt(int flags) { |
Message Message::encrypt() |
||||
return pEp::PythonAdapter::decrypt_message(*this, flags); |
{ |
||||
} |
boost::python::list extra; |
||||
|
return encrypt_message(*this, extra, PEP_enc_PGP_MIME, 0); |
||||
|
} |
||||
|
|
||||
PEP_rating Message::outgoing_rating() |
Message Message::_encrypt(boost::python::list extra, int enc_format, int flags) |
||||
{ |
{ |
||||
if (_msg->dir != PEP_dir_outgoing) |
if (!enc_format) |
||||
throw invalid_argument("Message.dir must be outgoing"); |
enc_format = PEP_enc_PGP_MIME; |
||||
|
return encrypt_message(*this, extra, enc_format, flags); |
||||
|
} |
||||
|
|
||||
if (from().address() == "") |
boost::python::tuple Message::decrypt(int flags) { |
||||
throw invalid_argument("from.address needed"); |
return pEp::PythonAdapter::decrypt_message(*this, flags); |
||||
if (from().username() == "") |
} |
||||
throw invalid_argument("from.username needed"); |
|
||||
|
|
||||
if (len(to()) + len(cc()) == 0) |
PEP_rating Message::outgoing_rating() |
||||
throw invalid_argument("either to or cc needed"); |
{ |
||||
|
if (_msg->dir != PEP_dir_outgoing) |
||||
|
throw invalid_argument("Message.dir must be outgoing"); |
||||
|
|
||||
PEP_STATUS status = myself(pEp::Adapter::session(), _msg->from); |
if (from().address() == "") |
||||
_throw_status(status); |
throw invalid_argument("from.address needed"); |
||||
|
if (from().username() == "") |
||||
|
throw invalid_argument("from.username needed"); |
||||
|
|
||||
PEP_rating rating = PEP_rating_undefined; |
if (len(to()) + len(cc()) == 0) |
||||
status = outgoing_message_rating(pEp::Adapter::session(), *this, &rating); |
throw invalid_argument("either to or cc needed"); |
||||
_throw_status(status); |
|
||||
|
|
||||
return rating; |
PEP_STATUS status = myself(pEp::Adapter::session(), _msg->from); |
||||
} |
_throw_status(status); |
||||
|
|
||||
PEP_color Message::outgoing_color() |
PEP_rating rating = PEP_rating_undefined; |
||||
{ |
status = outgoing_message_rating(pEp::Adapter::session(), *this, &rating); |
||||
return _color(outgoing_rating()); |
_throw_status(status); |
||||
} |
|
||||
|
|
||||
Message Message::copy() |
return rating; |
||||
{ |
} |
||||
message *dup = message_dup(*this); |
|
||||
if (!dup) |
|
||||
throw bad_alloc(); |
|
||||
return Message(dup); |
|
||||
} |
|
||||
|
|
||||
Message Message::deepcopy(dict&) |
PEP_color Message::outgoing_color() |
||||
{ |
{ |
||||
return copy(); |
return _color(outgoing_rating()); |
||||
} |
} |
||||
|
|
||||
Message outgoing_message(Identity me) |
Message Message::copy() |
||||
{ |
{ |
||||
if (me.address().empty() || me.user_id().empty()) |
message *dup = message_dup(*this); |
||||
throw runtime_error("at least address and user_id of own user needed"); |
if (!dup) |
||||
|
throw bad_alloc(); |
||||
|
return Message(dup); |
||||
|
} |
||||
|
|
||||
::myself(pEp::Adapter::session(), me); |
Message Message::deepcopy(dict&) |
||||
auto m = Message(PEP_dir_outgoing, &me); |
{ |
||||
return m; |
return copy(); |
||||
} |
} |
||||
|
|
||||
static object update(Identity ident) |
Message outgoing_message(Identity me) |
||||
{ |
{ |
||||
if (ident.address().empty()) |
if (me.address().empty() || me.user_id().empty()) |
||||
throw runtime_error("at least address needed"); |
throw runtime_error("at least address and user_id of own user needed"); |
||||
update_identity(pEp::Adapter::session(), ident); |
|
||||
return object(ident); |
|
||||
} |
|
||||
|
|
||||
static boost::python::list update(boost::python::list il) |
::myself(pEp::Adapter::session(), me); |
||||
{ |
auto m = Message(PEP_dir_outgoing, &me); |
||||
for (int i=0; i<len(il); i++) { |
return m; |
||||
update(extract< Identity >(il[i])); |
} |
||||
} |
|
||||
|
|
||||
return il; |
static object update(Identity ident) |
||||
} |
{ |
||||
|
if (ident.address().empty()) |
||||
|
throw runtime_error("at least address needed"); |
||||
|
update_identity(pEp::Adapter::session(), ident); |
||||
|
return object(ident); |
||||
|
} |
||||
|
|
||||
Message incoming_message(string mime_text) |
static boost::python::list update(boost::python::list il) |
||||
{ |
{ |
||||
auto m = Message(mime_text); |
for (int i=0; i<len(il); i++) { |
||||
m.dir(PEP_dir_incoming); |
update(extract< Identity >(il[i])); |
||||
|
} |
||||
|
|
||||
try { |
return il; |
||||
m.from(update(m.from())); |
} |
||||
} |
|
||||
catch (out_of_range&) { } |
|
||||
|
|
||||
try { |
Message incoming_message(string mime_text) |
||||
m.recv_by(update(m.recv_by())); |
{ |
||||
} |
auto m = Message(mime_text); |
||||
catch (out_of_range&) { } |
m.dir(PEP_dir_incoming); |
||||
|
|
||||
m.to(update(m.to())); |
try { |
||||
m.cc(update(m.cc())); |
m.from(update(m.from())); |
||||
m.reply_to(update(m.reply_to())); |
} |
||||
|
catch (out_of_range&) { } |
||||
|
|
||||
return m; |
try { |
||||
} |
m.recv_by(update(m.recv_by())); |
||||
} |
} |
||||
|
catch (out_of_range&) { } |
||||
|
|
||||
|
m.to(update(m.to())); |
||||
|
m.cc(update(m.cc())); |
||||
|
m.reply_to(update(m.reply_to())); |
||||
|
|
||||
|
return m; |
||||
} |
} |
||||
|
|
||||
|
} // namespace PythonAdapter
|
||||
|
} // namespace pEp {
|
||||
|
@ -1,168 +1,173 @@ |
|||||
// 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; |
||||
{ |
|
||||
Identity _from = src.from(); |
Message encrypt_message(Message src, boost::python::list extra, int enc_format, int flags) |
||||
if (_from.address() == "") |
{ |
||||
throw invalid_argument("encrypt_message: src.from_.address empty"); |
Identity _from = src.from(); |
||||
if (_from.username() == "") |
if (_from.address() == "") |
||||
throw invalid_argument("encrypt_message: src.from_.username empty"); |
throw invalid_argument("encrypt_message: src.from_.address empty"); |
||||
|
if (_from.username() == "") |
||||
if (_from.user_id() == "") |
throw invalid_argument("encrypt_message: src.from_.username empty"); |
||||
src.from().user_id(_from.address()); |
|
||||
|
if (_from.user_id() == "") |
||||
stringlist_t *_extra = to_stringlist(extra); |
src.from().user_id(_from.address()); |
||||
PEP_enc_format _enc_format = (PEP_enc_format) enc_format; |
|
||||
PEP_encrypt_flags_t _flags = (PEP_encrypt_flags_t) flags; |
stringlist_t *_extra = to_stringlist(extra); |
||||
message *_dst = NULL; |
PEP_enc_format _enc_format = (PEP_enc_format) enc_format; |
||||
|
PEP_encrypt_flags_t _flags = (PEP_encrypt_flags_t) flags; |
||||
message *_src = src; |
message *_dst = NULL; |
||||
PEP_STATUS status = encrypt_message(pEp::Adapter::session(), _src, _extra, &_dst, |
|
||||
_enc_format, _flags); |
message *_src = src; |
||||
free_stringlist(_extra); |
PEP_STATUS status = encrypt_message(pEp::Adapter::session(), _src, _extra, &_dst, |
||||
_throw_status(status); |
_enc_format, _flags); |
||||
|
free_stringlist(_extra); |
||||
if (!_dst || _dst == _src) |
_throw_status(status); |
||||
return Message(_src); |
|
||||
|
if (!_dst || _dst == _src) |
||||
return Message(_dst); |
return Message(_src); |
||||
} |
|
||||
|
return Message(_dst); |
||||
boost::python::tuple decrypt_message(Message src, int flags) |
} |
||||
{ |
|
||||
message *_dst = NULL; |
boost::python::tuple decrypt_message(Message src, int flags) |
||||
stringlist_t *_keylist = NULL; |
{ |
||||
PEP_rating _rating = PEP_rating_undefined; |
message *_dst = NULL; |
||||
PEP_decrypt_flags_t _flags = (PEP_decrypt_flags_t) flags; |
stringlist_t *_keylist = NULL; |
||||
message *_src = src; |
PEP_rating _rating = PEP_rating_undefined; |
||||
|
PEP_decrypt_flags_t _flags = (PEP_decrypt_flags_t) flags; |
||||
PEP_STATUS status = ::decrypt_message(pEp::Adapter::session(), _src, &_dst, &_keylist, |
message *_src = src; |
||||
&_rating, &_flags); |
|
||||
_throw_status(status); |
PEP_STATUS status = ::decrypt_message(pEp::Adapter::session(), _src, &_dst, &_keylist, |
||||
|
&_rating, &_flags); |
||||
boost::python::list keylist; |
_throw_status(status); |
||||
if (_keylist) { |
|
||||
keylist = from_stringlist(_keylist); |
boost::python::list keylist; |
||||
free_stringlist(_keylist); |
if (_keylist) { |
||||
} |
keylist = from_stringlist(_keylist); |
||||
|
free_stringlist(_keylist); |
||||
Message dst = _dst ? Message(_dst) : Message(src); |
} |
||||
return boost::python::make_tuple(dst, keylist, _rating, _flags); |
|
||||
} |
Message dst = _dst ? Message(_dst) : Message(src); |
||||
|
return boost::python::make_tuple(dst, keylist, _rating, _flags); |
||||
PEP_color _color(int rating) |
} |
||||
{ |
|
||||
return ::color_from_rating((PEP_rating) rating); |
PEP_color _color(int rating) |
||||
} |
{ |
||||
|
return ::color_from_rating((PEP_rating) rating); |
||||
boost::python::tuple sync_decode(object buffer) |
} |
||||
{ |
|
||||
Py_buffer src; |
boost::python::tuple sync_decode(object buffer) |
||||
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
{ |
||||
if (result) |
Py_buffer src; |
||||
throw invalid_argument("need a contiguous buffer to read"); |
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
||||
|
if (result) |
||||
char *dst = NULL; |
throw invalid_argument("need a contiguous buffer to read"); |
||||
PEP_STATUS status = PER_to_XER_Sync_msg((char *) src.buf, src.len, &dst); |
|
||||
PyBuffer_Release(&src); |
char *dst = NULL; |
||||
_throw_status(status); |
PEP_STATUS status = PER_to_XER_Sync_msg((char *) src.buf, src.len, &dst); |
||||
|
PyBuffer_Release(&src); |
||||
string _dst(dst); |
_throw_status(status); |
||||
free(dst); |
|
||||
return boost::python::make_tuple(_dst, 0); |
string _dst(dst); |
||||
} |
free(dst); |
||||
|
return boost::python::make_tuple(_dst, 0); |
||||
static boost::python::tuple sync_encode(string text) |
} |
||||
{ |
|
||||
char *data = NULL; |
static boost::python::tuple sync_encode(string text) |
||||
size_t size = 0; |
{ |
||||
PEP_STATUS status = XER_to_PER_Sync_msg(text.c_str(), &data, &size); |
char *data = NULL; |
||||
_throw_status(status); |
size_t size = 0; |
||||
|
PEP_STATUS status = XER_to_PER_Sync_msg(text.c_str(), &data, &size); |
||||
PyObject *ba = PyBytes_FromStringAndSize(data, size); |
_throw_status(status); |
||||
free(data); |
|
||||
if (!ba) |
PyObject *ba = PyBytes_FromStringAndSize(data, size); |
||||
throw bad_alloc(); |
free(data); |
||||
|
if (!ba) |
||||
return boost::python::make_tuple(object(handle<>(ba)), 0); |
throw bad_alloc(); |
||||
} |
|
||||
|
return boost::python::make_tuple(object(handle<>(ba)), 0); |
||||
boost::python::tuple Distribution_decode(object buffer) |
} |
||||
{ |
|
||||
Py_buffer src; |
boost::python::tuple Distribution_decode(object buffer) |
||||
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
{ |
||||
if (result) |
Py_buffer src; |
||||
throw invalid_argument("need a contiguous buffer to read"); |
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
||||
|
if (result) |
||||
char *dst = NULL; |
throw invalid_argument("need a contiguous buffer to read"); |
||||
PEP_STATUS status = PER_to_XER_Distribution_msg((char *) src.buf, src.len, &dst); |
|
||||
PyBuffer_Release(&src); |
char *dst = NULL; |
||||
_throw_status(status); |
PEP_STATUS status = PER_to_XER_Distribution_msg((char *) src.buf, src.len, &dst); |
||||
|
PyBuffer_Release(&src); |
||||
string _dst(dst); |
_throw_status(status); |
||||
free(dst); |
|
||||
return boost::python::make_tuple(_dst, 0); |
string _dst(dst); |
||||
} |
free(dst); |
||||
|
return boost::python::make_tuple(_dst, 0); |
||||
static boost::python::tuple Distribution_encode(string text) |
} |
||||
{ |
|
||||
char *data = NULL; |
static boost::python::tuple Distribution_encode(string text) |
||||
size_t size = 0; |
{ |
||||
PEP_STATUS status = XER_to_PER_Distribution_msg(text.c_str(), &data, &size); |
char *data = NULL; |
||||
_throw_status(status); |
size_t size = 0; |
||||
|
PEP_STATUS status = XER_to_PER_Distribution_msg(text.c_str(), &data, &size); |
||||
PyObject *ba = PyBytes_FromStringAndSize(data, size); |
_throw_status(status); |
||||
free(data); |
|
||||
if (!ba) |
PyObject *ba = PyBytes_FromStringAndSize(data, size); |
||||
throw bad_alloc(); |
free(data); |
||||
|
if (!ba) |
||||
return boost::python::make_tuple(object(handle<>(ba)), 0); |
throw bad_alloc(); |
||||
} |
|
||||
|
return boost::python::make_tuple(object(handle<>(ba)), 0); |
||||
object sync_search(string name) |
} |
||||
{ |
|
||||
if (name != "pep.sync") { |
object sync_search(string name) |
||||
return object(); |
{ |
||||
} |
if (name != "pep.sync") { |
||||
else { |
return object(); |
||||
object codecs = import("codecs"); |
} |
||||
object CodecInfo = codecs.attr("CodecInfo"); |
else { |
||||
|
object codecs = import("codecs"); |
||||
object _sync_decode = make_function(sync_decode); |
object CodecInfo = codecs.attr("CodecInfo"); |
||||
object _sync_encode = make_function(sync_encode); |
|
||||
|
object _sync_decode = make_function(sync_decode); |
||||
return call< object >(CodecInfo.ptr(), _sync_encode, _sync_decode); |
object _sync_encode = make_function(sync_encode); |
||||
} |
|
||||
} |
|
||||
|
|
||||
object distribution_search(string name) |
|
||||
{ |
|
||||
if (name != "pep.distribution") { |
|
||||
return object(); |
|
||||
} |
|
||||
else { |
|
||||
object codecs = import("codecs"); |
|
||||
object CodecInfo = codecs.attr("CodecInfo"); |
|
||||
|
|
||||
object _distribution_decode = make_function(Distribution_decode); |
|
||||
object _distribution_encode = make_function(Distribution_encode); |
|
||||
|
|
||||
return call< object >(CodecInfo.ptr(), _distribution_encode, _distribution_decode); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
|
return call< object >(CodecInfo.ptr(), _sync_encode, _sync_decode); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
|
object distribution_search(string name) |
||||
|
{ |
||||
|
if (name != "pep.distribution") { |
||||
|
return object(); |
||||
|
} |
||||
|
else { |
||||
|
object codecs = import("codecs"); |
||||
|
object CodecInfo = codecs.attr("CodecInfo"); |
||||
|
|
||||
|
object _distribution_decode = make_function(Distribution_decode); |
||||
|
object _distribution_encode = make_function(Distribution_encode); |
||||
|
|
||||
|
return call< object >(CodecInfo.ptr(), _distribution_encode, _distribution_decode); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} // namespace PythonAdapter
|
||||
|
} // namespace pEp {
|
||||
|
@ -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); |
||||
// void do_sync_protocol();
|
PEP_STATUS _ensure_passphrase(PEP_SESSION session, const char *fpr); |
||||
// extern Adapter adapter;
|
// void do_sync_protocol();
|
||||
} |
// extern Adapter adapter;
|
||||
} |
|
||||
|
} // namespace PythonAdapter
|
||||
|
} // namespace pEp {
|
||||
|
|
||||
|
@ -1,180 +1,184 @@ |
|||||
// 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) |
||||
|
{ |
||||
|
return s.attr("__repr__")(); |
||||
|
} |
||||
|
|
||||
object repr(object s) |
string repr(string s) |
||||
{ |
{ |
||||
return s.attr("__repr__")(); |
str _s = s.c_str(); |
||||
} |
object _r = _s.attr("__repr__")(); |
||||
|
string r = extract< string >(_r); |
||||
|
return r; |
||||
|
} |
||||
|
|
||||
string repr(string s) |
string str_attr(char *&str) |
||||
{ |
{ |
||||
str _s = s.c_str(); |
if (!str) |
||||
object _r = _s.attr("__repr__")(); |
return string(""); |
||||
string r = extract< string >(_r); |
return string(str); |
||||
return r; |
} |
||||
} |
|
||||
|
|
||||
string str_attr(char *&str) |
void str_attr(char *&str, string value) |
||||
{ |
{ |
||||
if (!str) |
string normalized = normalize(value, norm_nfc); |
||||
return string(""); |
free(str); |
||||
return string(str); |
str = strdup(normalized.c_str()); |
||||
} |
if (!str) |
||||
|
throw bad_alloc(); |
||||
|
} |
||||
|
|
||||
void str_attr(char *&str, string value) |
time_t timestamp_attr(timestamp *&ts) |
||||
{ |
{ |
||||
string normalized = normalize(value, norm_nfc); |
if (!ts) |
||||
free(str); |
return 0; |
||||
str = strdup(normalized.c_str()); |
|
||||
if (!str) |
|
||||
throw bad_alloc(); |
|
||||
} |
|
||||
|
|
||||
time_t timestamp_attr(timestamp *&ts) |
return timegm(ts); |
||||
{ |
} |
||||
if (!ts) |
|
||||
return 0; |
|
||||
|
|
||||
return timegm(ts); |
void timestamp_attr(timestamp *&ts, time_t value) |
||||
} |
{ |
||||
|
free_timestamp(ts); |
||||
|
ts = new_timestamp(value); |
||||
|
} |
||||
|
|
||||
void timestamp_attr(timestamp *&ts, time_t value) |
boost::python::list strlist_attr(stringlist_t *&sl) |
||||
{ |
{ |
||||
free_timestamp(ts); |
boost::python::list result; |
||||
ts = new_timestamp(value); |
|
||||
} |
|
||||
|
|
||||
boost::python::list strlist_attr(stringlist_t *&sl) |
for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
||||
{ |
string s(_sl->value); |
||||
boost::python::list result; |
result.append(object(s)); |
||||
|
} |
||||
|
|
||||
for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
return result; |
||||
string s(_sl->value); |
} |
||||
result.append(object(s)); |
|
||||
} |
|
||||
|
|
||||
return result; |
void strlist_attr(stringlist_t *&sl, boost::python::list value) |
||||
|
{ |
||||
|
stringlist_t *_sl = new_stringlist(NULL); |
||||
|
if (!_sl) |
||||
|
throw bad_alloc(); |
||||
|
|
||||
|
stringlist_t *_s = _sl; |
||||
|
for (int i=0; i<len(value); i++) { |
||||
|
extract< string > extract_string(value[i]); |
||||
|
if (!extract_string.check()) { |
||||
|
free_stringlist(_sl); |
||||
} |
} |
||||
|
string s = extract_string(); |
||||
void strlist_attr(stringlist_t *&sl, boost::python::list value) |
s = normalize(s, norm_nfc); |
||||
{ |
_s = stringlist_add(_s, s.c_str()); |
||||
stringlist_t *_sl = new_stringlist(NULL); |
if (!_s) { |
||||
if (!_sl) |
free_stringlist(_sl); |
||||
throw bad_alloc(); |
throw bad_alloc(); |
||||
|
|
||||
stringlist_t *_s = _sl; |
|
||||
for (int i=0; i<len(value); i++) { |
|
||||
extract< string > extract_string(value[i]); |
|
||||
if (!extract_string.check()) { |
|
||||
free_stringlist(_sl); |
|
||||
} |
|
||||
string s = extract_string(); |
|
||||
s = normalize(s, norm_nfc); |
|
||||
_s = stringlist_add(_s, s.c_str()); |
|
||||
if (!_s) { |
|
||||
free_stringlist(_sl); |
|
||||
throw bad_alloc(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
free_stringlist(sl); |
|
||||
sl = _sl; |
|
||||
} |
} |
||||
|
} |
||||
|
|
||||
dict strdict_attr(stringpair_list_t *&spl) |
free_stringlist(sl); |
||||
{ |
sl = _sl; |
||||
dict result; |
} |
||||
|
|
||||
for (stringpair_list_t *_spl = spl; _spl && _spl->value; _spl = |
dict strdict_attr(stringpair_list_t *&spl) |
||||
_spl->next) { |
{ |
||||
stringpair_t *p = _spl->value; |
dict result; |
||||
if (p->key && p->value) { |
|
||||
string key(p->key); |
|
||||
string value(p->value); |
|
||||
|
|
||||
result[key] = value; |
for (stringpair_list_t *_spl = spl; _spl && _spl->value; _spl = |
||||
} |
_spl->next) { |
||||
} |
stringpair_t *p = _spl->value; |
||||
|
if (p->key && p->value) { |
||||
|
string key(p->key); |
||||
|
string value(p->value); |
||||
|
|
||||
return result; |
result[key] = value; |
||||
} |
} |
||||
|
} |
||||
|
|
||||
void strdict_attr(stringpair_list_t *&spl, dict value) |
return result; |
||||
{ |
} |
||||
stringpair_list_t *_spl = new_stringpair_list(NULL); |
|
||||
if (!_spl) |
|
||||
throw bad_alloc(); |
|
||||
|
|
||||
stringpair_list_t *_s = _spl; |
|
||||
for (int i=0; i<len(value); i++) { |
|
||||
extract< string > extract_key(value.keys()[i]); |
|
||||
extract< string > extract_value(value.values()[i]); |
|
||||
|
|
||||
if (!(extract_key.check() && extract_value.check())) |
|
||||
free_stringpair_list(_spl); |
|
||||
|
|
||||
string key = extract_key(); |
|
||||
key = normalize(key, norm_nfc); |
|
||||
string _value = extract_value(); |
|
||||
_value = normalize(_value, norm_nfc); |
|
||||
stringpair_t *pair = new_stringpair(key.c_str(), _value.c_str()); |
|
||||
if (!pair) { |
|
||||
free_stringpair_list(_spl); |
|
||||
throw bad_alloc(); |
|
||||
} |
|
||||
_s = stringpair_list_add(_s, pair); |
|
||||
if (!_s) { |
|
||||
free_stringpair_list(_spl); |
|
||||
throw bad_alloc(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
free_stringpair_list(spl); |
|
||||
spl = _spl; |
|
||||
} |
|
||||
|
|
||||
stringlist_t *to_stringlist(boost::python::list l) |
void strdict_attr(stringpair_list_t *&spl, dict value) |
||||
{ |
{ |
||||
stringlist_t *result = new_stringlist(NULL); |
stringpair_list_t *_spl = new_stringpair_list(NULL); |
||||
if (!result) |
if (!_spl) |
||||
throw bad_alloc(); |
throw bad_alloc(); |
||||
|
|
||||
stringlist_t *_s = result; |
stringpair_list_t *_s = _spl; |
||||
for (int i=0; i<len(l); i++) { |
for (int i=0; i<len(value); i++) { |
||||
extract< string > extract_string(l[i]); |
extract< string > extract_key(value.keys()[i]); |
||||
if (!extract_string.check()) |
extract< string > extract_value(value.values()[i]); |
||||
free_stringlist(result); |
|
||||
string s = extract_string(); |
if (!(extract_key.check() && extract_value.check())) |
||||
_s = stringlist_add(_s, s.c_str()); |
free_stringpair_list(_spl); |
||||
if (!_s) { |
|
||||
free_stringlist(result); |
string key = extract_key(); |
||||
throw bad_alloc(); |
key = normalize(key, norm_nfc); |
||||
} |
string _value = extract_value(); |
||||
} |
_value = normalize(_value, norm_nfc); |
||||
|
stringpair_t *pair = new_stringpair(key.c_str(), _value.c_str()); |
||||
return result; |
if (!pair) { |
||||
|
free_stringpair_list(_spl); |
||||
|
throw bad_alloc(); |
||||
} |
} |
||||
|
_s = stringpair_list_add(_s, pair); |
||||
|
if (!_s) { |
||||
|
free_stringpair_list(_spl); |
||||
|
throw bad_alloc(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
boost::python::list from_stringlist(const stringlist_t *sl) |
free_stringpair_list(spl); |
||||
{ |
spl = _spl; |
||||
boost::python::list result; |
} |
||||
for (const stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
|
||||
string s = _sl->value; |
stringlist_t *to_stringlist(boost::python::list l) |
||||
result.append(s); |
{ |
||||
} |
stringlist_t *result = new_stringlist(NULL); |
||||
return result; |
if (!result) |
||||
|
throw bad_alloc(); |
||||
|
|
||||
|
stringlist_t *_s = result; |
||||
|
for (int i=0; i<len(l); i++) { |
||||
|
extract< string > extract_string(l[i]); |
||||
|
if (!extract_string.check()) |
||||
|
free_stringlist(result); |
||||
|
string s = extract_string(); |
||||
|
_s = stringlist_add(_s, s.c_str()); |
||||
|
if (!_s) { |
||||
|
free_stringlist(result); |
||||
|
throw bad_alloc(); |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
|
return result; |
||||
} |
} |
||||
|
|
||||
|
boost::python::list from_stringlist(const stringlist_t *sl) |
||||
|
{ |
||||
|
boost::python::list result; |
||||
|
for (const stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
||||
|
string s = _sl->value; |
||||
|
result.append(s); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
} // namespace PythonAdapter
|
||||
|
} // namespace pEp {
|
||||
|
|
||||
|
@ -1,124 +1,129 @@ |
|||||
// 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() |
|
||||
{ |
|
||||
if (_ui) |
|
||||
throw runtime_error("only one UserInterface thread allowed"); |
|
||||
_ui = this; |
|
||||
} |
|
||||
|
|
||||
UserInterface::~UserInterface() |
UserInterface *UserInterface::_ui = nullptr; |
||||
{ |
|
||||
_ui = nullptr; |
|
||||
} |
|
||||
|
|
||||
UserInterface_callback::UserInterface_callback(PyObject *self) : |
UserInterface::UserInterface() |
||||
UserInterface(), _self(self) |
{ |
||||
{ |
if (_ui) |
||||
// adapter.ui_object(self);
|
throw runtime_error("only one UserInterface thread allowed"); |
||||
PEP_STATUS status = ::register_sync_callbacks(pEp::Adapter::session(), |
_ui = this; |
||||
(void *) this, _notifyHandshake, retrieve_next_sync_event); |
} |
||||
assert(status == PEP_STATUS_OK); |
|
||||
if (status) |
|
||||
_throw_status(status); |
|
||||
} |
|
||||
|
|
||||
UserInterface_callback::~UserInterface_callback() |
UserInterface::~UserInterface() |
||||
{ |
{ |
||||
::unregister_sync_callbacks(pEp::Adapter::session()); |
_ui = nullptr; |
||||
} |
} |
||||
|
|
||||
PEP_STATUS UserInterface::_notifyHandshake( |
UserInterface_callback::UserInterface_callback(PyObject *self) : |
||||
pEp_identity *me, pEp_identity *partner, |
UserInterface(), _self(self) |
||||
sync_handshake_signal signal |
{ |
||||
) |
// adapter.ui_object(self);
|
||||
{ |
PEP_STATUS status = ::register_sync_callbacks(pEp::Adapter::session(), |
||||
if (!(me && partner)) |
(void *) this, _notifyHandshake, retrieve_next_sync_event); |
||||
return PEP_ILLEGAL_VALUE; |
assert(status == PEP_STATUS_OK); |
||||
|
if (status) |
||||
|
_throw_status(status); |
||||
|
} |
||||
|
|
||||
auto that = dynamic_cast< UserInterface_callback * >(_ui); |
UserInterface_callback::~UserInterface_callback() |
||||
that->notifyHandshake(Identity(me), Identity(partner), signal); |
{ |
||||
|
::unregister_sync_callbacks(pEp::Adapter::session()); |
||||
|
} |
||||
|
|
||||
return PEP_STATUS_OK; |
PEP_STATUS UserInterface::_notifyHandshake( |
||||
} |
pEp_identity *me, pEp_identity *partner, |
||||
|
sync_handshake_signal signal |
||||
|
) |
||||
|
{ |
||||
|
if (!(me && partner)) |
||||
|
return PEP_ILLEGAL_VALUE; |
||||
|
|
||||
void UserInterface::deliverHandshakeResult(int result, object identities) |
auto that = dynamic_cast< UserInterface_callback * >(_ui); |
||||
{ |
that->notifyHandshake(Identity(me), Identity(partner), signal); |
||||
identity_list *shared_identities = nullptr; |
|
||||
if (identities != boost::python::api::object() && |
|
||||
boost::python::len(identities)) { |
|
||||
shared_identities = new_identity_list(nullptr); |
|
||||
if (!shared_identities) |
|
||||
throw bad_alloc(); |
|
||||
|
|
||||
try { |
return PEP_STATUS_OK; |
||||
identity_list *si = shared_identities; |
} |
||||
for (int i=0; i < boost::python::len(identities); ++i) { |
|
||||
Identity ident = extract< Identity >(identities[i]); |
void UserInterface::deliverHandshakeResult(int result, object identities) |
||||
si = identity_list_add(si, ident); |
{ |
||||
if (!si) |
identity_list *shared_identities = nullptr; |
||||
throw bad_alloc(); |
if (identities != boost::python::api::object() && |
||||
} |
boost::python::len(identities)) { |
||||
} |
shared_identities = new_identity_list(nullptr); |
||||
catch (exception& ex) { |
if (!shared_identities) |
||||
free_identity_list(shared_identities); |
throw bad_alloc(); |
||||
throw ex; |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
PEP_STATUS status = ::deliverHandshakeResult(pEp::Adapter::session(), |
try { |
||||
(sync_handshake_result) result, shared_identities); |
identity_list *si = shared_identities; |
||||
|
for (int i=0; i < boost::python::len(identities); ++i) { |
||||
|
Identity ident = extract< Identity >(identities[i]); |
||||
|
si = identity_list_add(si, ident); |
||||
|
if (!si) |
||||
|
throw bad_alloc(); |
||||
|
} |
||||
|
} |
||||
|
catch (exception& ex) { |
||||
free_identity_list(shared_identities); |
free_identity_list(shared_identities); |
||||
_throw_status(status); |
throw ex; |
||||
} |
} |
||||
|
} |
||||
|
|
||||
PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) |
PEP_STATUS status = ::deliverHandshakeResult(pEp::Adapter::session(), |
||||
{ |
(sync_handshake_result) result, shared_identities); |
||||
PEP_rating result; |
free_identity_list(shared_identities); |
||||
PEP_STATUS status = |
_throw_status(status); |
||||
::get_key_rating_for_user(pEp::Adapter::session(), |
} |
||||
user_id.c_str(), fpr.c_str(), &result); |
|
||||
_throw_status(status); |
|
||||
return result; |
|
||||
} |
|
||||
|
|
||||
// SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold)
|
PEP_rating UserInterface::get_key_rating_for_user(string user_id, string fpr) |
||||
// {
|
{ |
||||
// time_t started = time(nullptr);
|
PEP_rating result; |
||||
// bool timeout = false;
|
PEP_STATUS status = |
||||
|
::get_key_rating_for_user(pEp::Adapter::session(), |
||||
|
user_id.c_str(), fpr.c_str(), &result); |
||||
|
_throw_status(status); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
//SYNC_EVENT UserInterface::retrieve_next_sync_event(void *management, unsigned threshold)
|
||||
|
//{
|
||||
|
// time_t started = time(nullptr);
|
||||
|
// bool timeout = false;
|
||||
//
|
//
|
||||
// while (adapter.queue().empty()) {
|
// while (adapter.queue().empty()) {
|
||||
// int i = 0;
|
// int i = 0;
|
||||
// ++i;
|
// ++i;
|
||||
// if (i > 10) {
|
// if (i > 10) {
|
||||
// if (time(nullptr) > started + threshold) {
|
// if (time(nullptr) > started + threshold) {
|
||||
// timeout = true;
|
// timeout = true;
|
||||
// break;
|
// break;
|
||||
// }
|
|
||||
// i = 0;
|
|
||||
// }
|
|
||||
// nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
|
|
||||
// }
|
// }
|
||||
|
// i = 0;
|
||||
|
// }
|
||||
|
// nanosleep((const struct timespec[]){{0, 100000000L}}, NULL);
|
||||
|
// }
|
||||
//
|
//
|
||||
// if (timeout)
|
// if (timeout)
|
||||
// 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 {
|
||||
|
|
||||
|
Loading…
Reference in new issue