Browse Source

formatting fornatting - breakfast and lunch - dinner and brunch :)

REWORK
heck 5 years ago
parent
commit
0e893624c9
  1. 83
      src/pEp/_pEp/basic_api.cc
  2. 58
      src/pEp/_pEp/identity.cc
  3. 4
      src/pEp/_pEp/identity.hh
  4. 108
      src/pEp/_pEp/message.cc
  5. 3
      src/pEp/_pEp/message.hh
  6. 22
      src/pEp/_pEp/message_api.cc
  7. 7
      src/pEp/_pEp/message_api.hh
  8. 415
      src/pEp/_pEp/pEpmodule.cc
  9. 27
      src/pEp/_pEp/str_attr.cc
  10. 6
      src/pEp/_pEp/str_attr.hh
  11. 29
      src/pEp/_pEp/user_interface.cc
  12. 12
      src/pEp/_pEp/user_interface.hh

83
src/pEp/_pEp/basic_api.cc

@ -17,94 +17,99 @@ namespace pEp {
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: '" throw runtime_error("update_identity: '"
PEP_OWN_USERID 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() == "") {
throw invalid_argument("username needed"); throw invalid_argument("username needed");
}
if (ident.user_id() == "") if (ident.user_id() == "") {
ident.user_id(ident.address()); ident.user_id(ident.address());
}
::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");
}
if (lang == "" && me.lang() == partner.lang()) if (lang == "" && me.lang() == partner.lang()) {
lang = me.lang(); lang = me.lang();
}
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() == "") {
throw invalid_argument("user_id must be provided"); throw invalid_argument("user_id must be provided");
}
::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() == "") {
throw invalid_argument("user_id needed"); throw invalid_argument("user_id needed");
}
::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() == "") {
throw invalid_argument("user_id needed"); throw invalid_argument("user_id needed");
}
::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() == "") {
throw invalid_argument("address needed"); throw invalid_argument("address needed");
if (ident.user_id() == "") }
if (ident.user_id() == "") {
throw invalid_argument("user_id needed"); throw invalid_argument("user_id needed");
}
::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);
@ -114,7 +119,6 @@ namespace pEp {
} }
result.append(Identity(ident)); result.append(Identity(ident));
} }
::free_identity_list(private_keys); ::free_identity_list(private_keys);
return result; return result;
} }
@ -124,7 +128,6 @@ namespace pEp {
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;
} }
@ -134,27 +137,27 @@ namespace pEp {
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() == "") {
throw invalid_argument("username needed"); throw invalid_argument("username needed");
if (ident.user_id() == "") }
if (ident.user_id() == "") {
throw invalid_argument("user_id needed"); throw invalid_argument("user_id needed");
if (fpr == "") }
if (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

58
src/pEp/_pEp/identity.cc

@ -21,12 +21,11 @@ namespace pEp {
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(), username.c_str()), &::free_identity) {
: _ident(::new_identity(address.c_str(), fpr.c_str(), user_id.c_str(), if (!_ident) {
username.c_str()), &::free_identity) {
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);
@ -58,20 +57,24 @@ namespace pEp {
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;
@ -80,36 +83,41 @@ namespace pEp {
} }
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(Adapter::session(), _ident.get(), &rating); ::PEP_STATUS status = ::identity_rating(Adapter::session(), _ident.get(), &rating);
@ -124,8 +132,9 @@ namespace pEp {
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);
} }
@ -183,12 +192,14 @@ namespace pEp {
// } // }
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;
@ -197,8 +208,9 @@ namespace pEp {
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(Adapter::session(), _dup); ::PEP_STATUS status = ::update_identity(Adapter::session(), _dup);
_throw_status(status); _throw_status(status);
::free_identity(ident); ::free_identity(ident);
@ -210,8 +222,9 @@ namespace pEp {
for (::identity_list *_il = il; _il && _il->ident; _il = _il->next) { for (::identity_list *_il = il; _il && _il->ident; _il = _il->next) {
::pEp_identity *ident = ::identity_dup(_il->ident); ::pEp_identity *ident = ::identity_dup(_il->ident);
if (!ident) if (!ident) {
throw bad_alloc(); throw bad_alloc();
}
result.append(object(Identity(ident))); result.append(object(Identity(ident)));
} }
@ -220,8 +233,9 @@ namespace pEp {
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++) {

4
src/pEp/_pEp/identity.hh

@ -33,9 +33,7 @@ namespace pEp {
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 lang = "", ::identity_flags_t flags = 0);
string user_id = "", string fpr = "", int comm_type = 0,
string lang = "", ::identity_flags_t flags = 0);
Identity(const Identity &second); Identity(const Identity &second);

108
src/pEp/_pEp/message.cc

@ -23,21 +23,26 @@ namespace pEp {
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),
if (!_bl) part_of_chain(chained) {
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)),
if (!_bl) part_of_chain(false) {
if (!_bl) {
throw bad_alloc(); throw bad_alloc();
}
Py_buffer src; Py_buffer src;
int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO); int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO);
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) {
@ -56,9 +61,9 @@ namespace pEp {
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() {
@ -76,11 +81,13 @@ namespace pEp {
} else { } else {
build << "bytes(" << _bl->size << "), "; build << "bytes(" << _bl->size << "), ";
string mime_type; string mime_type;
if (_bl->mime_type) if (_bl->mime_type) {
mime_type = string(_bl->mime_type); mime_type = string(_bl->mime_type);
}
string filename; string filename;
if (_bl->filename) if (_bl->filename) {
filename = string(_bl->filename); filename = string(_bl->filename);
}
build << repr(mime_type) << ", "; build << repr(mime_type) << ", ";
build << repr(filename); build << repr(filename);
} }
@ -94,8 +101,7 @@ namespace pEp {
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;
@ -115,11 +121,13 @@ namespace pEp {
string _mime_type = _bl->mime_type ? _bl->mime_type : ""; string _mime_type = _bl->mime_type ? _bl->mime_type : "";
encoding = "ascii"; encoding = "ascii";
if (_mime_type == "application/pEp.sync") if (_mime_type == "application/pEp.sync") {
encoding = "pep.sync"; encoding = "pep.sync";
}
if (_mime_type == "application/pEp.keyreset") if (_mime_type == "application/pEp.keyreset") {
encoding = "pep.distribution"; encoding = "pep.distribution";
}
} }
object codecs = import("codecs"); object codecs = import("codecs");
@ -131,13 +139,15 @@ namespace pEp {
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();
}
if (from) { if (from) {
_msg->from = ::identity_dup(*from); _msg->from = ::identity_dup(*from);
if (!_msg->from) if (!_msg->from) {
throw bad_alloc(); throw bad_alloc();
}
_msg->dir = (::PEP_msg_direction)dir; _msg->dir = (::PEP_msg_direction)dir;
} }
} }
@ -145,17 +155,18 @@ namespace pEp {
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);
switch (status) { switch (status) {
case ::PEP_STATUS_OK: case ::PEP_STATUS_OK:
if (_cpy) if (_cpy) {
_cpy->dir = ::PEP_dir_outgoing; _cpy->dir = ::PEP_dir_outgoing;
else } else {
_cpy = new_message(::PEP_dir_outgoing); _cpy = new_message(::PEP_dir_outgoing);
}
if (!_cpy) if (!_cpy) {
throw bad_alloc(); throw bad_alloc();
}
_msg = shared_ptr<::message>(_cpy); _msg = shared_ptr<::message>(_cpy);
break; break;
@ -178,18 +189,15 @@ namespace pEp {
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)
: _msg(::message_dup(msg), &::free_message) {
} }
Message::~Message() { Message::Message(::message *msg)
: _msg(::message_dup(msg), &::free_message) {}
} Message::~Message() {}
Message::operator ::message *() { Message::operator ::message *() {
return _msg.get(); return _msg.get();
@ -200,8 +208,9 @@ namespace pEp {
} }
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");
}
char *mimetext; char *mimetext;
string result; string result;
@ -240,8 +249,7 @@ namespace pEp {
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 = bl->next) {
bl->next) {
l.append(Blob(bl, true)); l.append(Blob(bl, true));
} }
@ -250,14 +258,14 @@ namespace pEp {
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);
@ -290,8 +298,9 @@ namespace pEp {
} }
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);
} }
@ -300,16 +309,20 @@ namespace pEp {
} }
::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");
}
if (from().address() == "") if (from().address() == "") {
throw invalid_argument("from.address needed"); throw invalid_argument("from.address needed");
if (from().username() == "") }
if (from().username() == "") {
throw invalid_argument("from.username needed"); throw invalid_argument("from.username needed");
}
if (len(to()) + len(cc()) == 0) if (len(to()) + len(cc()) == 0) {
throw invalid_argument("either to or cc needed"); throw invalid_argument("either to or cc needed");
}
::PEP_STATUS status = myself(Adapter::session(), _msg->from); ::PEP_STATUS status = myself(Adapter::session(), _msg->from);
_throw_status(status); _throw_status(status);
@ -327,8 +340,9 @@ namespace pEp {
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);
} }
@ -337,8 +351,9 @@ namespace pEp {
} }
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);
@ -346,8 +361,9 @@ namespace pEp {
} }
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);
} }
@ -366,13 +382,13 @@ namespace pEp {
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()));

3
src/pEp/_pEp/message.hh

@ -38,8 +38,7 @@ namespace pEp {
bool part_of_chain; bool part_of_chain;
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 = "");

22
src/pEp/_pEp/message_api.cc

@ -16,16 +16,20 @@ namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std; using namespace std;
using namespace boost::python; using namespace boost::python;
// namespace bp = boost::python; // namespace bp = 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");
if (_from.username() == "") }
if (_from.username() == "") {
throw invalid_argument("encrypt_message: src.from_.username empty"); throw invalid_argument("encrypt_message: src.from_.username empty");
}
if (_from.user_id() == "") if (_from.user_id() == "") {
src.from().user_id(_from.address()); src.from().user_id(_from.address());
}
::stringlist_t *_extra = to_stringlist(extra); ::stringlist_t *_extra = to_stringlist(extra);
::PEP_enc_format _enc_format = (::PEP_enc_format)enc_format; ::PEP_enc_format _enc_format = (::PEP_enc_format)enc_format;
@ -51,8 +55,7 @@ namespace pEp {
::PEP_decrypt_flags_t _flags = (::PEP_decrypt_flags_t)flags; ::PEP_decrypt_flags_t _flags = (::PEP_decrypt_flags_t)flags;
::message *_src = src; ::message *_src = src;
::PEP_STATUS status = ::decrypt_message(Adapter::session(), _src, &_dst, &_keylist, ::PEP_STATUS status = ::decrypt_message(Adapter::session(), _src, &_dst, &_keylist, &_rating, &_flags);
&_rating, &_flags);
_throw_status(status); _throw_status(status);
boost::python::list keylist; boost::python::list keylist;
@ -94,8 +97,9 @@ namespace pEp {
PyObject * ba = PyBytes_FromStringAndSize(data, size); PyObject * ba = PyBytes_FromStringAndSize(data, size);
free(data); free(data);
if (!ba) if (!ba) {
throw bad_alloc(); throw bad_alloc();
}
return boost::python::make_tuple(object(handle<>(ba)), 0); return boost::python::make_tuple(object(handle<>(ba)), 0);
} }
@ -103,8 +107,9 @@ namespace pEp {
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) {
throw invalid_argument("need a contiguous buffer to read"); throw invalid_argument("need a contiguous buffer to read");
}
char *dst = NULL; char *dst = NULL;
::PEP_STATUS status = ::PER_to_XER_Distribution_msg((char *)src.buf, src.len, &dst); ::PEP_STATUS status = ::PER_to_XER_Distribution_msg((char *)src.buf, src.len, &dst);
@ -124,8 +129,9 @@ namespace pEp {
PyObject * ba = PyBytes_FromStringAndSize(data, size); PyObject * ba = PyBytes_FromStringAndSize(data, size);
free(data); free(data);
if (!ba) if (!ba) {
throw bad_alloc(); throw bad_alloc();
}
return boost::python::make_tuple(object(handle<>(ba)), 0); return boost::python::make_tuple(object(handle<>(ba)), 0);
} }

7
src/pEp/_pEp/message_api.hh

@ -9,12 +9,7 @@
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
Message encrypt_message( Message encrypt_message(Message src, boost::python::list extra = boost::python::list(), int enc_format = 4, int flags = 0);
Message src,
boost::python::list extra = boost::python::list(),
int enc_format = 4,
int flags = 0
);
boost::python::tuple decrypt_message(Message src, int flags = 0); boost::python::tuple decrypt_message(Message src, int flags = 0);

415
src/pEp/_pEp/pEpmodule.cc

@ -44,7 +44,6 @@ namespace pEp {
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);
} }
@ -54,8 +53,9 @@ namespace pEp {
} }
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(), user_id.c_str(), fpr != "" ? fpr.c_str() : nullptr); ::PEP_STATUS status = ::key_reset_user(Adapter::session(), user_id.c_str(), fpr != "" ? fpr.c_str() : nullptr);
_throw_status(status); _throw_status(status);
@ -71,20 +71,23 @@ namespace pEp {
} }
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) {
return; return;
if (status == ::PEP_OUT_OF_MEMORY) }
if (status == ::PEP_OUT_OF_MEMORY) {
throw bad_alloc(); throw bad_alloc();
if (status == ::PEP_ILLEGAL_VALUE) }
if (status == ::PEP_ILLEGAL_VALUE) {
throw invalid_argument("illegal value"); throw invalid_argument("illegal value");
}
if (string(pEp_status_to_string(status)) == "unknown status code") { if (string(pEp_status_to_string(status)) == "unknown status code") {
stringstream build; stringstream build;
@ -105,7 +108,8 @@ namespace pEp {
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;
} }
@ -120,12 +124,12 @@ namespace pEp {
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();
} }
@ -154,19 +158,20 @@ namespace pEp {
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);
if (!shared_identities) if (!shared_identities) {
throw bad_alloc(); throw bad_alloc();
}
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;
} }
@ -188,38 +193,31 @@ namespace pEp {
boost::locale::generator gen; boost::locale::generator gen;
std::locale::global(gen("")); std::locale::global(gen(""));
scope().attr("about") = about(); scope().attr("about") = about();
scope().attr("per_user_directory") = ::per_user_directory(); scope().attr("per_user_directory") = ::per_user_directory();
scope().attr("per_machine_directory") = ::per_machine_directory(); scope().attr("per_machine_directory") = ::per_machine_directory();
scope().attr("engine_version") = ::get_engine_version(); scope().attr("engine_version") = ::get_engine_version();
scope().attr("protocol_version") = ::get_protocol_version(); scope().attr("protocol_version") = ::get_protocol_version();
def("passive_mode", config_passive_mode, def("passive_mode", config_passive_mode, "do not attach pub keys to all messages");
"do not attach pub keys to all messages");
def("unencrypted_subject", config_unencrypted_subject, def("unencrypted_subject", config_unencrypted_subject, "do not encrypt the subject of messages");
"do not encrypt the subject of messages");
def("key_reset", key_reset_user, def("key_reset", key_reset_user, "reset the default database status for the user / keypair provided\n"
"reset the default database status for the user / keypair provided\n"
"This will effectively perform key_reset on each identity\n" "This will effectively perform key_reset on each identity\n"
"associated with the key and user_id, if a key is provided, and for\n" "associated with the key and user_id, if a key is provided, and for\n"
"each key (and all of their identities) if an fpr is not."); "each key (and all of their identities) if an fpr is not.");
def("key_reset", key_reset_user2, def("key_reset", key_reset_user2, "reset the default database status for the user / keypair provided\n"
"reset the default database status for the user / keypair provided\n"
"This will effectively perform key_reset on each identity\n" "This will effectively perform key_reset on each identity\n"
"associated with the key and user_id, if a key is provided, and for\n" "associated with the key and user_id, if a key is provided, and for\n"
"each key (and all of their identities) if an fpr is not."); "each key (and all of their identities) if an fpr is not.");
def("key_reset_all_own_keys", key_reset_all_own_keys, def("key_reset_all_own_keys", key_reset_all_own_keys, "revoke and mistrust all own keys, generate new keys for all\n"
"revoke and mistrust all own keys, generate new keys for all\n"
"own identities, and opportunistically communicate key reset\n" "own identities, and opportunistically communicate key reset\n"
"information to people we have recently contacted."); "information to people we have recently contacted.");
auto identity_class = class_<Identity>("Identity", auto identity_class = class_<Identity>("Identity", "Identity(address, username, user_id='', fpr='', comm_type=0, lang='en')\n"
"Identity(address, username, user_id='', fpr='', comm_type=0, lang='en')\n"
"\n" "\n"
"represents a p≡p identity\n" "represents a p≡p identity\n"
"\n" "\n"
@ -233,30 +231,16 @@ namespace pEp {
" hex encoded\n" " hex encoded\n"
" comm_type first rating level of this communication channel\n" " comm_type first rating level of this communication channel\n"
" lang ISO 639-1 language code for language being preferred\n" " lang ISO 639-1 language code for language being preferred\n"
" on this communication channel\n" " on this communication channel\n").def(boost::python::init<string>()).def(boost::python::init<string, string>()).def(boost::python::init<string, string, string>()).def(boost::python::init<string, string, string, string>())
) .def(boost::python::init<string, string, string, string, int>()).def(boost::python::init<string, string, string, string, int, string>()).def("__repr__", &Identity::_repr).def("__str__", &Identity::_str, "string representation of this identity\n"
.def(boost::python::init<string>()) "following the pattern 'username < address >'\n")
.def(boost::python::init<string, string>()) .def("key_reset", &Identity::key_reset, boost::python::arg("fpr") = object(""), "reset the default database status for the identity / keypair provided. If this\n"
.def(boost::python::init<string, string, string>())
.def(boost::python::init<string, string, string, string>())
.def(boost::python::init<string, string, string, string, int>())
.def(boost::python::init<string, string, string, string, int, string>())
.def("__repr__", &Identity::_repr)
.def("__str__", &Identity::_str,
"string representation of this identity\n"
"following the pattern 'username < address >'\n"
)
.def("key_reset", &Identity::key_reset,
boost::python::arg("fpr")=object(""),
"reset the default database status for the identity / keypair provided. If this\n"
"corresponds to the own user and a private key, also revoke the key, generate a\n" "corresponds to the own user and a private key, also revoke the key, generate a\n"
"new one, and communicate the reset to recently contacted pEp partners for this\n" "new one, and communicate the reset to recently contacted pEp partners for this\n"
"identity. If it does not, remove the key from the keyring; the key's status is\n" "identity. If it does not, remove the key from the keyring; the key's status is\n"
"completely fresh on next contact from the partner.") "completely fresh on next contact from the partner.")
.def("key_mistrusted", &Identity::key_mistrusted, .def("key_mistrusted", &Identity::key_mistrusted, boost::python::arg("fpr") = object(""), "If you want updated trust on the identity, you ll have"
boost::python::arg("fpr")=object(""),
"If you want updated trust on the identity, you ll have"
"to call update_identity or myself respectively after this." "to call update_identity or myself respectively after this."
"N.B. If you are calling this on a key that is the identity or user default," "N.B. If you are calling this on a key that is the identity or user default,"
"it will be removed as the default key for ANY identity and user for which" "it will be removed as the default key for ANY identity and user for which"
@ -264,79 +248,40 @@ namespace pEp {
"will only undo the current identity's / it's user's default, not any" "will only undo the current identity's / it's user's default, not any"
"other identities which may be impacted (this will not affect most use cases)") "other identities which may be impacted (this will not affect most use cases)")
.def("enable_for_sync", &Identity::enable_for_sync, .def("enable_for_sync", &Identity::enable_for_sync, "Enable own identity for p≡p sync.\n\n"
"Enable own identity for p≡p sync.\n\n" "Only use this on own identities, which are used as accounts.\n").def("disable_for_sync", &Identity::disable_for_sync, "Disable own identity for p≡p sync.\n\n"
"Only use this on own identities, which are used as accounts.\n")
.def("disable_for_sync", &Identity::disable_for_sync,
"Disable own identity for p≡p sync.\n\n"
"Only use this on own identities, which are used as accounts.\n") "Only use this on own identities, which are used as accounts.\n")
.add_property("address", (string(Identity::*)()) &Identity::address, .add_property("address", (string(Identity::*)())&Identity::address, (void (Identity::*)(string))&Identity::address, "email address or URI").add_property("fpr", (string(Identity::*)())&Identity::fpr, (void (Identity::*)(string))&Identity::fpr, "key ID (full fingerprint, hex encoded)")
(void(Identity::*)(string)) &Identity::address, .add_property("user_id", (string(Identity::*)())&Identity::user_id, (void (Identity::*)(string))&Identity::user_id, "ID of person associated or 'pEp_own_userId' if own identity")
"email address or URI") .add_property("username", (string(Identity::*)())&Identity::username, (void (Identity::*)(string))&Identity::username, "name in full of person associated")
.add_property("fpr", (string(Identity::*)()) &Identity::fpr, .add_property("comm_type", (int (Identity::*)())(PEP_comm_type(Identity::*)())&Identity::comm_type, (void (Identity::*)(int))(void (Identity::*)(::PEP_comm_type))&Identity::comm_type, "communication type, first rating level (p≡p internal)")
(void(Identity::*)(string)) &Identity::fpr, .add_property("lang", (string(Identity::*)())&Identity::lang, (void (Identity::*)(string))&Identity::lang, "ISO 639-1 language code").add_property("flags", (identity_flags_t(Identity::*)())&Identity::flags, (void (Identity::*)(::identity_flags_t))&Identity::flags, "flags (p≡p internal)")
"key ID (full fingerprint, hex encoded)") .add_property("rating", &Identity::rating, "rating of Identity").add_property("color", &Identity::color, "color of Identity as PEP_color").add_property("is_pEp_user", &Identity::is_pEp_user, "True if this is an identity of a pEp user").def("__deepcopy__", &Identity::deepcopy)
.add_property("user_id", (string(Identity::*)()) &Identity::user_id, .def("update", &Identity::update, "update Identity").def("__copy__", &Identity::copy);
(void(Identity::*)(string)) &Identity::user_id,
"ID of person associated or 'pEp_own_userId' if own identity")
.add_property("username", (string(Identity::*)()) &Identity::username,
(void(Identity::*)(string)) &Identity::username,
"name in full of person associated")
.add_property("comm_type", (int(Identity::*)())
(PEP_comm_type(Identity::*)()) &Identity::comm_type,
(void(Identity::*)(int))
(void(Identity::*)(::PEP_comm_type)) &Identity::comm_type,
"communication type, first rating level (p≡p internal)")
.add_property("lang", (string(Identity::*)()) &Identity::lang,
(void(Identity::*)(string)) &Identity::lang,
"ISO 639-1 language code")
.add_property("flags", (identity_flags_t(Identity::*)()) &Identity::flags,
(void(Identity::*)(::identity_flags_t)) &Identity::flags,
"flags (p≡p internal)")
.add_property("rating", &Identity::rating, "rating of Identity")
.add_property("color", &Identity::color, "color of Identity as PEP_color")
.add_property("is_pEp_user", &Identity::is_pEp_user, "True if this is an identity of a pEp user")
.def("__deepcopy__", &Identity::deepcopy)
.def("update", &Identity::update, "update Identity")
.def("__copy__", &Identity::copy);
identity_class.attr("PEP_OWN_USERID") = "pEp_own_userId"; identity_class.attr("PEP_OWN_USERID") = "pEp_own_userId";
auto blob_class = class_<Message::Blob>("Blob", auto blob_class = class_<Message::Blob>("Blob", "Blob(data, mime_type='', filename='')\n"
"Blob(data, mime_type='', filename='')\n"
"\n" "\n"
"Binary large object\n" "Binary large object\n"
"\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"))).def(boost::python::init<object, string>()).def(boost::python::init<object>())
boost::python::init< object, char const*, char const* >(args("data", "mime_type", "filename"))) .def("__repr__", &Message::Blob::_repr).def("__len__", &Message::Blob::size, "size of Blob data in bytes").def("decode", (string(Message::Blob::*)())&Message::Blob::decode).def("decode", (string(Message::Blob::*)(string))&Message::Blob::decode, "text = blob.decode(encoding='')\n"
.def(boost::python::init<object, string>())
.def(boost::python::init<object>())
.def("__repr__", &Message::Blob::_repr)
.def("__len__", &Message::Blob::size, "size of Blob data in bytes")
.def("decode", (string(Message::Blob::*)()) &Message::Blob::decode)
.def("decode", (string(Message::Blob::*)(string)) &Message::Blob::decode,
"text = blob.decode(encoding='')\n"
"\n" "\n"
"decode Blob data into string depending on MIME type if encoding=''\n" "decode Blob data into string depending on MIME type if encoding=''\n"
"\n" "\n"
" mime_type='application/pEp.sync' decode as 'pEp.sync'\n" " mime_type='application/pEp.sync' decode as 'pEp.sync'\n"
" mime_type='application/pEp.keyreset' decode as 'pEp.keyreset'\n" " mime_type='application/pEp.keyreset' decode as 'pEp.keyreset'\n"
" other mime_type decode as 'ascii' by default\n" " other mime_type decode as 'ascii' by default\n")
) .add_property("mime_type", (string(Message::Blob::*)())&Message::Blob::mime_type, (void (Message::Blob::*)(string))&Message::Blob::mime_type, "MIME type of object in Blob")
.add_property("mime_type", (string(Message::Blob::*)()) &Message::Blob::mime_type, .add_property("filename", (string(Message::Blob::*)())&Message::Blob::filename, (void (Message::Blob::*)(string))&Message::Blob::filename, "filename of object in Blob");
(void(Message::Blob::*)(string)) &Message::Blob::mime_type,
"MIME type of object in Blob")
.add_property("filename", (string(Message::Blob::*)()) &Message::Blob::filename,
(void(Message::Blob::*)(string)) &Message::Blob::filename,
"filename of object in Blob");
((PyTypeObject *)(void *)blob_class.ptr())->tp_as_buffer = &Message::Blob::bp; ((PyTypeObject *)(void *)blob_class.ptr())->tp_as_buffer = &Message::Blob::bp;
auto message_class = class_<Message>("Message", auto message_class = class_<Message>("Message", "Message(dir=1, from=None)\n"
"Message(dir=1, from=None)\n"
"\n" "\n"
"new p≡p message\n" "new p≡p message\n"
"\n" "\n"
@ -347,84 +292,28 @@ namespace pEp {
"\n" "\n"
"new incoming p≡p message\n" "new incoming p≡p message\n"
"\n" "\n"
" mime_text text in Multipurpose Internet Mail Extensions format\n" " mime_text text in Multipurpose Internet Mail Extensions format\n").def(boost::python::init<int>()).def(boost::python::init<int, Identity *>()).def(boost::python::init<string>())
) .def("__str__", &Message::_str, "the string representation of a Message is it's MIME text").def("__repr__", &Message::_repr)
.def(boost::python::init<int>()) .add_property("dir", (int (Message::*)())(PEP_msg_direction(Message::*)())&Message::dir, (void (Message::*)(int))(void (Message::*)(PEP_msg_direction))&Message::dir, "0: incoming, 1: outgoing message")
.def(boost::python::init<int, Identity *>()) .add_property("id", (string(Message::*)())&Message::id, (void (Message::*)(string))&Message::id, "message ID").add_property("shortmsg", (string(Message::*)())&Message::shortmsg, (void (Message::*)(string))&Message::shortmsg, "subject or short message")
.def(boost::python::init<string>()) .add_property("longmsg", (string(Message::*)())&Message::longmsg, (void (Message::*)(string))&Message::longmsg, "body or long version of message")
.def("__str__", &Message::_str, .add_property("longmsg_formatted", (string(Message::*)())&Message::longmsg_formatted, (void (Message::*)(string))&Message::longmsg_formatted, "HTML body or fromatted long version of message")
"the string representation of a Message is it's MIME text" .add_property("attachments", (boost::python::tuple(Message::*)())&Message::attachments, (void (Message::*)(boost::python::list))&Message::attachments, "tuple of Blobs with attachments; setting moves Blobs to attachment tuple")
) .add_property("sent", (time_t(Message::*)())&Message::sent, (void (Message::*)(time_t))&Message::sent, "time when message was sent in UTC seconds since epoch")
.def("__repr__", &Message::_repr) .add_property("recv", (time_t(Message::*)())&Message::recv, (void (Message::*)(time_t))&Message::recv, "time when message was received in UTC seconds since epoch")
.add_property("dir", (int(Message::*)()) .add_property("from_", (Identity(Message::*)())&Message::from, (void (Message::*)(object))&Message::from, "identity where message is from")
(PEP_msg_direction(Message::*)()) &Message::dir, .add_property("to", (boost::python::list(Message::*)())&Message::to, (void (Message::*)(boost::python::list))&Message::to, "list of identities message is going to")
(void(Message::*)(int)) .add_property("recv_by", (Identity(Message::*)())&Message::recv_by, (void (Message::*)(object))&Message::recv_by, "identity where message was received by")
(void(Message::*)(PEP_msg_direction)) &Message::dir, .add_property("cc", (boost::python::list(Message::*)())&Message::cc, (void (Message::*)(boost::python::list))&Message::cc, "list of identities message is going cc")
"0: incoming, 1: outgoing message") .add_property("bcc", (boost::python::list(Message::*)())&Message::bcc, (void (Message::*)(boost::python::list))&Message::bcc, "list of identities message is going bcc")
.add_property("id", (string(Message::*)()) &Message::id, .add_property("reply_to", (boost::python::list(Message::*)())&Message::reply_to, (void (Message::*)(boost::python::list))&Message::reply_to, "list of identities where message will be replied to")
(void(Message::*)(string)) &Message::id, .add_property("in_reply_to", (boost::python::list(Message::*)())&Message::in_reply_to, (void (Message::*)(boost::python::list))&Message::in_reply_to, "in_reply_to list")
"message ID") .add_property("references", (boost::python::list(Message::*)())&Message::references, (void (Message::*)(boost::python::list))&Message::references, "message IDs of messages this one is referring to")
.add_property("shortmsg", (string(Message::*)()) &Message::shortmsg, .add_property("keywords", (boost::python::list(Message::*)())&Message::keywords, (void (Message::*)(boost::python::list))&Message::keywords, "keywords this message should be stored under")
(void(Message::*)(string)) &Message::shortmsg, .add_property("comments", (string(Message::*)())&Message::comments, (void (Message::*)(string))&Message::comments, "comments added to message").add_property("opt_fields", (dict(Message::*)())&Message::opt_fields, (void (Message::*)(dict))&Message::opt_fields, "opt_fields of message")
"subject or short message") .add_property("enc_format", (int (Message::*)())(PEP_enc_format(Message::*)())&Message::enc_format, (void (Message::*)(int))(void (Message::*)(::PEP_enc_format))&Message::enc_format, "0: unencrypted, 1: inline PGP, 2: S/MIME, 3: PGP/MIME, 4: p≡p format")
.add_property("longmsg", (string(Message::*)()) &Message::longmsg, .def("encrypt", (Message(Message::*)())&Message::encrypt).def("encrypt", (Message(Message::*)(boost::python::list))&Message::_encrypt).def("encrypt", (Message(Message::*)(boost::python::list, int))&Message::_encrypt)
(void(Message::*)(string)) &Message::longmsg, .def("encrypt", (Message(Message::*)(boost::python::list, int, int))&Message::_encrypt, "msg2 = msg1.encrypt(extra_keys=[], enc_format='pEp', flags=0)\n"
"body or long version of message")
.add_property("longmsg_formatted", (string(Message::*)()) &Message::longmsg_formatted,
(void(Message::*)(string)) &Message::longmsg_formatted,
"HTML body or fromatted long version of message")
.add_property("attachments", (boost::python::tuple(Message::*)()) &Message::attachments,
(void(Message::*)(boost::python::list)) &Message::attachments,
"tuple of Blobs with attachments; setting moves Blobs to attachment tuple")
.add_property("sent", (time_t(Message::*)()) &Message::sent,
(void(Message::*)(time_t)) &Message::sent,
"time when message was sent in UTC seconds since epoch")
.add_property("recv", (time_t(Message::*)()) &Message::recv,
(void(Message::*)(time_t)) &Message::recv,
"time when message was received in UTC seconds since epoch")
.add_property("from_", (Identity(Message::*)()) &Message::from,
(void(Message::*)(object)) &Message::from,
"identity where message is from")
.add_property("to", (boost::python::list(Message::*)()) &Message::to,
(void(Message::*)(boost::python::list)) &Message::to,
"list of identities message is going to")
.add_property("recv_by", (Identity(Message::*)()) &Message::recv_by,
(void(Message::*)(object)) &Message::recv_by,
"identity where message was received by")
.add_property("cc", (boost::python::list(Message::*)()) &Message::cc,
(void(Message::*)(boost::python::list)) &Message::cc,
"list of identities message is going cc")
.add_property("bcc", (boost::python::list(Message::*)()) &Message::bcc,
(void(Message::*)(boost::python::list)) &Message::bcc,
"list of identities message is going bcc")
.add_property("reply_to", (boost::python::list(Message::*)()) &Message::reply_to,
(void(Message::*)(boost::python::list)) &Message::reply_to,
"list of identities where message will be replied to")
.add_property("in_reply_to", (boost::python::list(Message::*)()) &Message::in_reply_to,
(void(Message::*)(boost::python::list)) &Message::in_reply_to,
"in_reply_to list")
.add_property("references", (boost::python::list(Message::*)()) &Message::references,
(void(Message::*)(boost::python::list)) &Message::references,
"message IDs of messages this one is referring to")
.add_property("keywords", (boost::python::list(Message::*)()) &Message::keywords,
(void(Message::*)(boost::python::list)) &Message::keywords,
"keywords this message should be stored under")
.add_property("comments", (string(Message::*)()) &Message::comments,
(void(Message::*)(string)) &Message::comments,
"comments added to message")
.add_property("opt_fields", (dict(Message::*)()) &Message::opt_fields,
(void(Message::*)(dict)) &Message::opt_fields,
"opt_fields of message")
.add_property("enc_format", (int(Message::*)())
(PEP_enc_format(Message::*)()) &Message::enc_format,
(void(Message::*)(int))
(void(Message::*)(::PEP_enc_format)) &Message::enc_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::*)(boost::python::list))&Message::_encrypt)
.def("encrypt", (Message(Message::*)(boost::python::list, 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"
"\n" "\n"
"encrypts a p≡p message and returns the encrypted message\n" "encrypts a p≡p message and returns the encrypted message\n"
"\n" "\n"
@ -432,87 +321,58 @@ namespace pEp {
" for encryption\n" " for encryption\n"
" enc_format 0 for none, 1 for partitioned, 2 for S/MIME,\n" " enc_format 0 for none, 1 for partitioned, 2 for S/MIME,\n"
" 3 for PGP/MIME, 4 for pEp\n" " 3 for PGP/MIME, 4 for pEp\n"
" flags 1 is force encryption\n" " flags 1 is force encryption\n").def("decrypt", &Message::decrypt, boost::python::arg("flags") = 0, "msg2, keys, rating, flags = msg1.decrypt()\n"
)
.def("decrypt", &Message::decrypt, boost::python::arg("flags")=0,
"msg2, keys, rating, flags = msg1.decrypt()\n"
"\n" "\n"
"decrypts a p≡p message and returns a tuple with data\n" "decrypts a p≡p message and returns a tuple with data\n"
"\n" "\n"
" msg the decrypted p≡p message\n" " msg the decrypted p≡p message\n"
" keys a list of keys being used\n" " keys a list of keys being used\n"
" rating the rating of the message as integer\n" " rating the rating of the message as integer\n"
" flags flags set while decryption\n" " flags flags set while decryption\n")
) .add_property("outgoing_rating", &Message::outgoing_rating, "rating outgoing message will have").add_property("outgoing_color", &Message::outgoing_color, "color outgoing message will have as PEP_color").def("__deepcopy__", &Message::deepcopy).def("__copy__", &Message::copy);
.add_property("outgoing_rating", &Message::outgoing_rating, "rating outgoing message will have")
.add_property("outgoing_color", &Message::outgoing_color, "color outgoing message will have as PEP_color")
.def("__deepcopy__", &Message::deepcopy)
.def("__copy__", &Message::copy);
// basic API and key management API // basic API and key management API
def("update_identity", &update_identity, def("update_identity", &update_identity, "update_identity(ident)\n"
"update_identity(ident)\n"
"\n" "\n"
"update identity information\n" "update identity information\n"
"call this to complete identity information when you at least have an address\n" "call this to complete identity information when you at least have an address\n");
); def("myself", &myself, "myself(ident)\n"
def("myself", &myself,
"myself(ident)\n"
"\n" "\n"
"ensures that the own identity is being complete\n" "ensures that the own identity is being complete\n"
"supply ident.address and ident.username\n" "supply ident.address and ident.username\n");
); def("trust_personal_key", &trust_personal_key, "trust_personal_key(ident)\n"
def("trust_personal_key", &trust_personal_key,
"trust_personal_key(ident)\n"
"\n" "\n"
"mark a key as trusted with a person\n" "mark a key as trusted with a person\n");
);
enum_<::identity_flags>("identity_flags") enum_<::identity_flags>("identity_flags").value("PEP_idf_not_for_sync", ::PEP_idf_not_for_sync).value("PEP_idf_list", ::PEP_idf_list).value("PEP_idf_devicegroup", ::PEP_idf_devicegroup);
.value("PEP_idf_not_for_sync", ::PEP_idf_not_for_sync)
.value("PEP_idf_list", ::PEP_idf_list)
.value("PEP_idf_devicegroup", ::PEP_idf_devicegroup);
def("set_identity_flags", &set_identity_flags, def("set_identity_flags", &set_identity_flags, "set_identity_flags(ident, flags)\n"
"set_identity_flags(ident, flags)\n"
"\n" "\n"
"set identity flags\n" "set identity flags\n");
);
def("unset_identity_flags", &unset_identity_flags, def("unset_identity_flags", &unset_identity_flags, "unset_identity_flags(ident, flags)\n"
"unset_identity_flags(ident, flags)\n"
"\n" "\n"
"unset identity flags\n" "unset identity flags\n");
);
def("key_reset_trust", &key_reset_trust, def("key_reset_trust", &key_reset_trust, "key_reset_trust(ident)\n"
"key_reset_trust(ident)\n"
"\n" "\n"
"reset trust bit or explicitly mistrusted status for an identity and " "reset trust bit or explicitly mistrusted status for an identity and "
"its accompanying key/user_id pair\n" "its accompanying key/user_id pair\n");
);
def("import_key", &import_key, def("import_key", &import_key, "private_key_list = import_key(key_data)\n"
"private_key_list = import_key(key_data)\n"
"\n" "\n"
"import key(s) from key_data\n" "import key(s) from key_data\n");
);
def("export_key", &export_key, def("export_key", &export_key, "key_data = export_key(identity)\n"
"key_data = export_key(identity)\n"
"\n" "\n"
"export key(s) of identity\n" "export key(s) of identity\n");
);
def("export_secret_key", &export_secret_key, def("export_secret_key", &export_secret_key, "key_data = export_seret_key(identity)\n"
"key_data = export_seret_key(identity)\n"
"\n" "\n"
"export secret key(s) of identity\n" "export secret key(s) of identity\n");
);
def("set_own_key", &set_own_key, def("set_own_key", &set_own_key, "set_own_key(me, fpr)\n"
"set_own_key(me, fpr)\n"
"\n" "\n"
"mark a key as an own key, and make it the default key\n" "mark a key as an own key, and make it the default key\n"
"\n" "\n"
@ -523,65 +383,34 @@ namespace pEp {
"myself() is called by set_own_key() without key generation\n" "myself() is called by set_own_key() without key generation\n"
"me->flags are ignored\n" "me->flags are ignored\n"
"me->address must not be an alias\n" "me->address must not be an alias\n"
"me->fpr will be ignored and replaced by fpr\n" "me->fpr will be ignored and replaced by fpr\n");
);
// message API // message API
enum_<::PEP_rating>("rating") enum_<::PEP_rating>("rating").value("_undefined", ::PEP_rating_undefined).value("cannot_decrypt", ::PEP_rating_cannot_decrypt).value("have_no_key", ::PEP_rating_have_no_key).value("unencrypted", ::PEP_rating_unencrypted).value("unreliable", ::PEP_rating_unreliable)
.value("_undefined", ::PEP_rating_undefined) .value("reliable", ::PEP_rating_reliable).value("trusted", ::PEP_rating_trusted).value("trusted_and_anonymized", ::PEP_rating_trusted_and_anonymized).value("fully_anonymous", ::PEP_rating_fully_anonymous).value("mistrust", ::PEP_rating_mistrust).value("b0rken", ::PEP_rating_b0rken)
.value("cannot_decrypt", ::PEP_rating_cannot_decrypt)
.value("have_no_key", ::PEP_rating_have_no_key)
.value("unencrypted", ::PEP_rating_unencrypted)
.value("unreliable", ::PEP_rating_unreliable)
.value("reliable", ::PEP_rating_reliable)
.value("trusted", ::PEP_rating_trusted)
.value("trusted_and_anonymized", ::PEP_rating_trusted_and_anonymized)
.value("fully_anonymous", ::PEP_rating_fully_anonymous)
.value("mistrust", ::PEP_rating_mistrust)
.value("b0rken", ::PEP_rating_b0rken)
.value("under_attack", ::PEP_rating_under_attack); .value("under_attack", ::PEP_rating_under_attack);
enum_<::PEP_color>("colorvalue") enum_<::PEP_color>("colorvalue").value("no_color", ::PEP_color_no_color).value("yellow", ::PEP_color_yellow).value("green", ::PEP_color_green).value("red", ::PEP_color_red);
.value("no_color", ::PEP_color_no_color)
.value("yellow", ::PEP_color_yellow)
.value("green", ::PEP_color_green)
.value("red", ::PEP_color_red);
def("incoming_message", &incoming_message, def("incoming_message", &incoming_message, "msg = incoming_message(mime_text)\n"
"msg = incoming_message(mime_text)\n"
"\n" "\n"
"create an incoming message from a MIME text" "create an incoming message from a MIME text");
); def("outgoing_message", &outgoing_message, "msg = outgoing_message(ident)\n"
def("outgoing_message", &outgoing_message,
"msg = outgoing_message(ident)\n"
"\n" "\n"
"create an outgoing message using an own identity" "create an outgoing message using an own identity");
); def("color", &_color, "c = color(rating)\n"
def("color", &_color,
"c = color(rating)\n"
"\n" "\n"
"calculate color value out of rating. Returns PEP_color" "calculate color value out of rating. Returns PEP_color");
); def("trustwords", &_trustwords, "text = trustwords(ident_own, ident_partner)\n"
def("trustwords", &_trustwords,
"text = trustwords(ident_own, ident_partner)\n"
"\n" "\n"
"calculate trustwords for two Identities"); "calculate trustwords for two Identities");
// 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_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_UNDEFINED", ::SYNC_NOTIFY_UNDEFINED) .value("SYNC_NOTIFY_INIT_FORM_GROUP", ::SYNC_NOTIFY_INIT_FORM_GROUP).value("SYNC_NOTIFY_TIMEOUT", ::SYNC_NOTIFY_TIMEOUT).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_INIT_ADD_OUR_DEVICE", ::SYNC_NOTIFY_INIT_ADD_OUR_DEVICE) .value("SYNC_NOTIFY_ACCEPTED_DEVICE_ACCEPTED", ::SYNC_NOTIFY_ACCEPTED_DEVICE_ACCEPTED).value("SYNC_NOTIFY_SOLE", ::SYNC_NOTIFY_SOLE).value("SYNC_NOTIFY_IN_GROUP", ::SYNC_NOTIFY_IN_GROUP);
.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_TIMEOUT", ::SYNC_NOTIFY_TIMEOUT)
.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_DEVICE_ACCEPTED", ::SYNC_NOTIFY_ACCEPTED_DEVICE_ACCEPTED)
.value("SYNC_NOTIFY_SOLE", ::SYNC_NOTIFY_SOLE)
.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",
@ -609,47 +438,35 @@ namespace pEp {
// "call to deliver the handshake result of the handshake dialog" // "call to deliver the handshake result of the handshake dialog"
// ); // );
def("deliver_handshake_result", &deliverHandshakeResult, boost::python::arg("identities")=object(), def("deliver_handshake_result", &deliverHandshakeResult, boost::python::arg("identities") = object(), "deliverHandshakeResult(self, result, identities=None)\n"
"deliverHandshakeResult(self, result, identities=None)\n"
"\n" "\n"
" result -1: cancel, 0: accepted, 1: rejected\n" " result -1: cancel, 0: accepted, 1: rejected\n"
" identities list of identities to share or None for all\n" " identities list of identities to share or None for all\n"
"\n" "\n"
"call to deliver the handshake result of the handshake dialog" "call to deliver the handshake result of the handshake dialog");
);
def("start_sync", &start_sync, def("start_sync", &start_sync, "start_sync()\n"
"start_sync()\n"
"\n" "\n"
"starts the sync thread" "starts the sync thread");
);
def("shutdown_sync", &shutdown_sync, def("shutdown_sync", &shutdown_sync, "shutdown_sync()\n"
"shutdown_sync()\n"
"\n" "\n"
"call this from another thread to shut down the sync thread\n" "call this from another thread to shut down the sync thread\n");
);
def("debug_color", &debug_color, def("debug_color", &debug_color, "for debug builds set ANSI color value");
"for debug builds set ANSI color value");
def("leave_device_group", &leave_device_group, def("leave_device_group", &leave_device_group, "leave_device_group()\n"
"leave_device_group()\n"
"\n" "\n"
"call this for a grouped device, which should leave\n" "call this for a grouped device, which should leave\n");
);
def("is_sync_active", &is_sync_active, def("is_sync_active", &is_sync_active, "is_sync_active()\n"
"is_sync_active()\n"
"\n" "\n"
"True if sync is active, False otherwise\n" "True if sync is active, False otherwise\n");
);
// 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

27
src/pEp/_pEp/str_attr.cc

@ -27,8 +27,9 @@ namespace pEp {
} }
string str_attr(char *&str) { string str_attr(char *&str) {
if (!str) if (!str) {
return string(""); return string("");
}
return string(str); return string(str);
} }
@ -36,13 +37,15 @@ namespace pEp {
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);
} }
@ -65,8 +68,9 @@ namespace pEp {
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++) {
@ -90,8 +94,7 @@ namespace pEp {
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 = _spl->next) {
_spl->next) {
::stringpair_t *p = _spl->value; ::stringpair_t *p = _spl->value;
if (p->key && p->value) { if (p->key && p->value) {
string key(p->key); string key(p->key);
@ -106,16 +109,18 @@ namespace pEp {
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);
}
string key = extract_key(); string key = extract_key();
key = normalize(key, norm_nfc); key = normalize(key, norm_nfc);
@ -139,14 +144,16 @@ namespace pEp {
::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();
_s = stringlist_add(_s, s.c_str()); _s = stringlist_add(_s, s.c_str());
if (!_s) { if (!_s) {

6
src/pEp/_pEp/str_attr.hh

@ -20,21 +20,27 @@ using boost::python::object;
using boost::python::dict; using boost::python::dict;
object repr(object s); object repr(object s);
string repr(string s); string repr(string s);
string str_attr(char *&str); string str_attr(char *&str);
void str_attr(char *&str, string value); void str_attr(char *&str, string value);
time_t timestamp_attr(::timestamp *&ts); time_t timestamp_attr(::timestamp *&ts);
void timestamp_attr(::timestamp *&ts, time_t value); void timestamp_attr(::timestamp *&ts, time_t value);
boost::python::list strlist_attr(::stringlist_t *&sl); boost::python::list strlist_attr(::stringlist_t *&sl);
void strlist_attr(::stringlist_t *&sl, boost::python::list value); void strlist_attr(::stringlist_t *&sl, boost::python::list value);
dict strdict_attr(::stringpair_list_t *&spl); dict strdict_attr(::stringpair_list_t *&spl);
void strdict_attr(::stringpair_list_t *&spl, dict value); void strdict_attr(::stringpair_list_t *&spl, dict value);
::stringlist_t *to_stringlist(boost::python::list l); ::stringlist_t *to_stringlist(boost::python::list l);
boost::python::list from_stringlist(const ::stringlist_t *sl); boost::python::list from_stringlist(const ::stringlist_t *sl);
} /* namespace PythonAdapter */ } /* namespace PythonAdapter */

29
src/pEp/_pEp/user_interface.cc

@ -15,8 +15,9 @@ namespace pEp {
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;
} }
@ -24,8 +25,9 @@ namespace pEp {
_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);
@ -38,12 +40,10 @@ namespace pEp {
// ::unregister_sync_callbacks(Adapter::session()); // ::unregister_sync_callbacks(Adapter::session());
} }
::PEP_STATUS UserInterface::_notifyHandshake( ::PEP_STATUS UserInterface::_notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) {
pEp_identity *me, pEp_identity *partner, if (!(me && partner)) {
sync_handshake_signal signal
) {
if (!(me && partner))
return PEP_ILLEGAL_VALUE; return PEP_ILLEGAL_VALUE;
}
auto that = dynamic_cast< UserInterface_callback * >(_ui); auto that = dynamic_cast< UserInterface_callback * >(_ui);
that->notifyHandshake(Identity(me), Identity(partner), signal); that->notifyHandshake(Identity(me), Identity(partner), signal);
@ -55,26 +55,26 @@ namespace pEp {
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);
if (!shared_identities) if (!shared_identities) {
throw bad_alloc(); throw bad_alloc();
}
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;
} }
} }
::PEP_STATUS status = ::deliverHandshakeResult(Adapter::session(), ::PEP_STATUS status = ::deliverHandshakeResult(Adapter::session(), (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);
} }
@ -113,8 +113,7 @@ namespace pEp {
// 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);
} }

12
src/pEp/_pEp/user_interface.hh

@ -14,7 +14,6 @@
// local // local
#include "pEpmodule.hh" #include "pEpmodule.hh"
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
@ -25,10 +24,7 @@ namespace pEp {
virtual ~UserInterface(); virtual ~UserInterface();
virtual void notifyHandshake( virtual void notifyHandshake(Identity me, Identity partner, sync_handshake_signal signal) {
Identity me,
Identity partner,
sync_handshake_signal signal) {
throw runtime_error("override this method"); throw runtime_error("override this method");
} }
@ -47,11 +43,7 @@ namespace pEp {
~UserInterface_callback(); ~UserInterface_callback();
void notifyHandshake( void notifyHandshake(Identity me, Identity partner, sync_handshake_signal signal);
Identity me,
Identity partner,
sync_handshake_signal signal
);
}; };
} /* namespace PythonAdapter */ } /* namespace PythonAdapter */

Loading…
Cancel
Save