Browse Source

FROMATTING: reformat cxx codebase

pull/14/head
heck 4 years ago
parent
commit
4f13825991
  1. 56
      src/pEp/_pEp/basic_api.cc
  2. 167
      src/pEp/_pEp/identity.cc
  3. 91
      src/pEp/_pEp/identity.hh
  4. 192
      src/pEp/_pEp/message.cc
  5. 303
      src/pEp/_pEp/message.hh
  6. 84
      src/pEp/_pEp/message_api.cc
  7. 9
      src/pEp/_pEp/message_api.hh
  8. 841
      src/pEp/_pEp/pEpmodule.cc
  9. 74
      src/pEp/_pEp/str_attr.cc
  10. 34
      src/pEp/_pEp/str_attr.hh

56
src/pEp/_pEp/basic_api.cc

@ -16,19 +16,20 @@ namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using namespace std; using namespace std;
void update_identity(Identity &ident) { void update_identity(Identity &ident)
{
if (ident.address() == "") if (ident.address() == "")
throw invalid_argument("address needed"); throw invalid_argument("address needed");
if (ident.user_id() == PEP_OWN_USERID) if (ident.user_id() == PEP_OWN_USERID)
throw runtime_error("update_identity: '" 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() == "")
@ -41,7 +42,8 @@ namespace pEp {
_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");
@ -50,13 +52,20 @@ namespace pEp {
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(
lang.c_str(), &words, &size, full); Adapter::session(),
me,
partner,
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() == "")
@ -66,7 +75,8 @@ namespace pEp {
_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() == "")
@ -76,7 +86,8 @@ namespace pEp {
_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() == "")
@ -86,7 +97,8 @@ namespace pEp {
_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() == "")
@ -99,9 +111,14 @@ namespace pEp {
} }
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);
@ -119,7 +136,8 @@ namespace pEp {
return result; return result;
} }
string export_key(Identity ident) { string export_key(Identity ident)
{
PEP_STATUS status = PEP_STATUS_OK; PEP_STATUS status = PEP_STATUS_OK;
char *key_data = NULL; char *key_data = NULL;
size_t size; size_t size;
@ -129,7 +147,8 @@ namespace pEp {
return key_data; return key_data;
} }
string export_secret_key(Identity ident) { string export_secret_key(Identity ident)
{
PEP_STATUS status = PEP_STATUS_OK; PEP_STATUS status = PEP_STATUS_OK;
char *key_data = NULL; char *key_data = NULL;
size_t size; size_t size;
@ -139,7 +158,8 @@ namespace pEp {
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() == "")
@ -157,5 +177,3 @@ namespace pEp {
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp } // namespace pEp

167
src/pEp/_pEp/identity.cc

@ -21,40 +21,44 @@ 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 fpr, int comm_type, string lang, identity_flags_t flags) string address,
: _ident(new_identity(address.c_str(), fpr.c_str(), user_id.c_str(), string username,
username.c_str()), &::free_identity) { string user_id,
if (!_ident) 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)
{
if (!_ident) {
throw bad_alloc(); throw bad_alloc();
_ident->comm_type = (PEP_comm_type) comm_type; }
_ident->flags = (identity_flags_t) flags; _ident->comm_type = (PEP_comm_type)comm_type;
_ident->flags = (identity_flags_t)flags;
this->lang(lang); this->lang(lang);
} }
Identity::Identity(const Identity &second) Identity::Identity(const Identity &second) : _ident(second._ident) {}
: _ident(second._ident) {
}
Identity::Identity(pEp_identity *ident)
: _ident(ident, &::free_identity) {
}
Identity::~Identity() { Identity::Identity(pEp_identity *ident) : _ident(ident, &::free_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;
@ -73,160 +77,174 @@ namespace pEp {
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(Adapter::session(), _ident.get(), &rating); PEP_STATUS status = ::identity_rating(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(Adapter::session(), *this, {
fpr != "" ? fpr.c_str() : nullptr); PEP_STATUS status = ::key_reset_identity(
Adapter::session(),
*this,
fpr != "" ? fpr.c_str() : nullptr);
_throw_status(status); _throw_status(status);
} }
void Identity::key_mistrusted() { void Identity::key_mistrusted()
{
PEP_STATUS status = ::key_mistrusted(Adapter::session(), *this); PEP_STATUS status = ::key_mistrusted(Adapter::session(), *this);
_throw_status(status); _throw_status(status);
} }
bool Identity::is_pEp_user() { bool Identity::is_pEp_user()
{
bool result; bool result;
PEP_STATUS status = ::is_pEp_user(Adapter::session(), *this, &result); PEP_STATUS status = ::is_pEp_user(Adapter::session(), *this, &result);
_throw_status(status); _throw_status(status);
return result; return result;
} }
void Identity::enable_for_sync() { void Identity::enable_for_sync()
{
PEP_STATUS status = ::enable_identity_for_sync(Adapter::session(), *this); PEP_STATUS status = ::enable_identity_for_sync(Adapter::session(), *this);
_throw_status(status); _throw_status(status);
} }
void Identity::disable_for_sync() { void Identity::disable_for_sync()
{
PEP_STATUS status = ::disable_identity_for_sync(Adapter::session(), *this); PEP_STATUS status = ::disable_identity_for_sync(Adapter::session(), *this);
_throw_status(status); _throw_status(status);
} }
// Myself::Myself(string address, string username, string user_id, string lang) Identity identity_attr(pEp_identity *&ident)
// : Identity(address, username, user_id, "", 0, lang) { {
// if (!(address.length() && username.length())) if (!ident) {
// throw invalid_argument("address and username must be set");
// if (lang.length() && lang.length() != 2)
// throw length_error("lang must be an ISO 639-1 language code or empty");
//
// // FIXME: should set .me
// // _ident->me = true;
// if (user_id.length())
// throw runtime_error("user_id feature not yet implemented for Myself");
// }
// void Myself::update() {
// pEp::PythonAdapter::myself(*this);
// }
Identity identity_attr(pEp_identity *&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(Adapter::session(), _dup); PEP_STATUS status = ::update_identity(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 identitylist_attr(identity_list *&il)
{
boost::python::list result; boost::python::list result;
for (identity_list *_il = il; _il && _il->ident; _il = _il->next) { for (identity_list *_il = il; _il && _il->ident; _il = _il->next) {
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)));
} }
return result; return result;
} }
void identitylist_attr(identity_list *&il, boost::python::list value) { void identitylist_attr(identity_list *&il, boost::python::list value)
{
identity_list *_il = ::new_identity_list(NULL); identity_list *_il = ::new_identity_list(NULL);
if (!_il) if (!_il) {
throw bad_alloc(); throw bad_alloc();
}
identity_list *_i = _il; identity_list *_i = _il;
for (int i = 0; i < len(value); i++) { for (int i = 0; i < len(value); i++) {
extract < Identity & > extract_identity(value[i]); extract<Identity &> extract_identity(value[i]);
if (!extract_identity.check()) { if (!extract_identity.check()) {
free_identity_list(_il); free_identity_list(_il);
} }
@ -254,4 +272,3 @@ namespace pEp {
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp } // namespace pEp

91
src/pEp/_pEp/identity.hh

@ -23,19 +23,24 @@
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using std::string;
using std::shared_ptr; using std::shared_ptr;
using std::string;
// Identity is owning a pEp_identity // Identity is owning a pEp_identity
class Identity { class Identity {
protected: protected:
shared_ptr <pEp_identity> _ident; shared_ptr<pEp_identity> _ident;
public: public:
Identity(string address = "", string username = "", Identity(
string user_id = "", string fpr = "", int comm_type = 0, string address = "",
string lang = "", identity_flags_t flags = 0); string username = "",
string user_id = "",
string fpr = "",
int comm_type = 0,
string lang = "",
identity_flags_t flags = 0);
Identity(const Identity &second); Identity(const Identity &second);
@ -51,33 +56,66 @@ namespace pEp {
string _str(); string _str();
string address() { return str_attr(_ident->address); } string address()
{
void address(string value) { str_attr(_ident->address, value); } return str_attr(_ident->address);
}
string fpr() { return str_attr(_ident->fpr); }
void address(string value)
void fpr(string value) { str_attr(_ident->fpr, value); } {
str_attr(_ident->address, value);
string user_id() { return str_attr(_ident->user_id); } }
void user_id(string value) { str_attr(_ident->user_id, value); } string fpr()
{
string username() { return str_attr(_ident->username); } return str_attr(_ident->fpr);
}
void fpr(string value)
{
str_attr(_ident->fpr, value);
}
string user_id()
{
return str_attr(_ident->user_id);
}
void user_id(string value)
{
str_attr(_ident->user_id, value);
}
string username()
{
return str_attr(_ident->username);
}
void username(string value); void username(string value);
PEP_comm_type comm_type() { return _ident->comm_type; } PEP_comm_type comm_type()
{
return _ident->comm_type;
}
void comm_type(PEP_comm_type value) { _ident->comm_type = value; }; void comm_type(PEP_comm_type value)
{
_ident->comm_type = value;
};
std::string lang(); std::string lang();
void lang(std::string value); void lang(std::string value);
identity_flags_t flags() { return _ident->flags; } identity_flags_t flags()
{
return _ident->flags;
}
void flags(identity_flags_t flags) { _ident->flags = flags; } void flags(identity_flags_t flags)
{
_ident->flags = flags;
}
int rating(); int rating();
@ -100,13 +138,6 @@ namespace pEp {
void disable_for_sync(); void disable_for_sync();
}; };
// class Myself : public Identity {
// public:
// Myself(string address, string username, string user_id = "", string lang = "");
//
// virtual void update();
// };
Identity identity_attr(pEp_identity *&ident); Identity identity_attr(pEp_identity *&ident);
void identity_attr(pEp_identity *&ident, object value); void identity_attr(pEp_identity *&ident, object value);

192
src/pEp/_pEp/message.cc

@ -23,23 +23,28 @@ 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), part_of_chain(chained) { {
if (!_bl) if (!_bl) {
throw bad_alloc(); throw bad_alloc();
}
} }
Message::Blob::Blob(object data, string mime_type, string filename) : Message::Blob::Blob(object data, string mime_type, string filename) :
_bl(new_bloblist(NULL, 0, NULL, NULL)), part_of_chain(false) { _bl(new_bloblist(NULL, 0, NULL, NULL)),
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) {
PyBuffer_Release(&src); PyBuffer_Release(&src);
throw bad_alloc(); throw bad_alloc();
@ -56,19 +61,18 @@ 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()
{
if (!part_of_chain) { if (!part_of_chain) {
free(_bl->value); free(_bl->value);
free(_bl); free(_bl);
} }
} }
string Message::Blob::_repr() { string Message::Blob::_repr()
{
stringstream build; stringstream build;
build << "Blob("; build << "Blob(";
if (!_bl) { if (!_bl) {
@ -88,14 +92,14 @@ namespace pEp {
return build.str(); return build.str();
} }
int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags)
{
bloblist_t *bl = NULL; bloblist_t *bl = NULL;
try { try {
Message::Blob &blob = extract<Message::Blob &>(self); Message::Blob &blob = extract<Message::Blob &>(self);
bl = blob._bl; bl = blob._bl;
} } catch (exception &e) {
catch (exception &e) {
PyErr_SetString(PyExc_RuntimeError, "extract not possible"); PyErr_SetString(PyExc_RuntimeError, "extract not possible");
view->obj = NULL; view->obj = NULL;
return -1; return -1;
@ -110,52 +114,58 @@ namespace pEp {
return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags); return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags);
} }
string Message::Blob::decode(string encoding) { string Message::Blob::decode(string encoding)
{
if (encoding == "") { if (encoding == "") {
string _mime_type = _bl->mime_type ? _bl->mime_type : ""; string _mime_type = _bl->mime_type ? _bl->mime_type : "";
encoding = "ascii"; encoding = "ascii";
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");
object _decode = codecs.attr("decode"); object _decode = codecs.attr("decode");
return call<string>(_decode.ptr(), this, encoding); return call<string>(_decode.ptr(), this, encoding);
} }
PyBufferProcs Message::Blob::bp = {getbuffer, NULL}; PyBufferProcs Message::Blob::bp = { getbuffer, NULL };
Message::Message(int dir, Identity *from) Message::Message(int dir, Identity *from) :
: _msg(new_message((PEP_msg_direction) dir), &free_message) { _msg(new_message((PEP_msg_direction)dir), &free_message)
if (!_msg) {
if (!_msg) {
throw bad_alloc(); throw bad_alloc();
}
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;
} }
} }
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;
@ -171,37 +181,37 @@ namespace pEp {
default: default:
stringstream build; stringstream build;
build << "mime_decode_message: unknown error (" << (int) status << ")"; build << "mime_decode_message: unknown error (" << (int)status << ")";
throw runtime_error(build.str()); throw runtime_error(build.str());
} }
} }
Message::Message(const Message &second) Message::Message(const Message &second) : _msg(second._msg)
: _msg(second._msg) { {
if (!_msg.get()) if (!_msg.get()) {
throw bad_alloc(); throw bad_alloc();
}
} }
Message::Message(message *msg) Message::Message(message *msg) : _msg(::message_dup(msg), &free_message) {}
: _msg(::message_dup(msg), &free_message) {
}
Message::~Message() {
} Message::~Message() {}
Message::operator message *() { Message::operator message *()
{
return _msg.get(); return _msg.get();
} }
Message::operator const message *() const { Message::operator const message *() const
{
return _msg.get(); return _msg.get();
} }
string Message::_str() { string Message::_str()
if (!(_msg->from && _msg->from->address && _msg->from->address[0])) {
if (!(_msg->from && _msg->from->address && _msg->from->address[0])) {
throw out_of_range(".from_.address missing"); throw out_of_range(".from_.address missing");
}
char *mimetext; char *mimetext;
string result; string result;
@ -224,40 +234,47 @@ namespace pEp {
default: default:
stringstream build; stringstream build;
build << "mime_encode_message: unknown error (" << (int) status << ")"; build << "mime_encode_message: unknown error (" << (int)status << ")";
throw runtime_error(build.str()); throw runtime_error(build.str());
} }
return result; return result;
} }
string Message::_repr() { string Message::_repr()
{
stringstream build; stringstream build;
build << "Message(" << repr(_str()) << ")"; build << "Message(" << repr(_str()) << ")";
return build.str(); return build.str();
} }
boost::python::tuple Message::attachments() { boost::python::tuple Message::attachments()
{
boost::python::list l; boost::python::list l;
for (bloblist_t *bl = _msg->attachments; bl && bl->value; bl = for (bloblist_t *bl = _msg->attachments; bl && bl->value; bl = bl->next) {
bl->next) {
l.append(Blob(bl, true)); l.append(Blob(bl, true));
} }
return boost::python::tuple(l); return boost::python::tuple(l);
} }
void Message::attachments(boost::python::list value) { void Message::attachments(boost::python::list value)
{
bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL); bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL);
if (!bl) if (!bl) {
throw bad_alloc(); throw bad_alloc();
}
bloblist_t *_l = bl; bloblist_t *_l = bl;
for (int i = 0; i < len(value); i++) { for (int i = 0; i < len(value); i++) {
Message::Blob &blob = extract<Message::Blob &>(value[i]); Message::Blob &blob = extract<Message::Blob &>(value[i]);
_l = bloblist_add(_l, blob._bl->value, blob._bl->size, _l = bloblist_add(
blob._bl->mime_type, blob._bl->filename); _l,
blob._bl->value,
blob._bl->size,
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);
@ -284,32 +301,41 @@ namespace pEp {
_msg->attachments = bl; _msg->attachments = bl;
} }
Message Message::encrypt() { Message Message::encrypt()
{
boost::python::list extra; boost::python::list extra;
return encrypt_message(*this, extra, PEP_enc_PGP_MIME, 0); return encrypt_message(*this, extra, PEP_enc_PGP_MIME, 0);
} }
Message Message::_encrypt(boost::python::list extra, int enc_format, int flags) { Message Message::_encrypt(boost::python::list extra, int enc_format, int flags)
if (!enc_format) {
if (!enc_format) {
enc_format = PEP_enc_PGP_MIME; enc_format = PEP_enc_PGP_MIME;
}
return encrypt_message(*this, extra, enc_format, flags); return encrypt_message(*this, extra, enc_format, flags);
} }
boost::python::tuple Message::decrypt(int flags) { boost::python::tuple Message::decrypt(int flags)
{
return pEp::PythonAdapter::decrypt_message(*this, flags); return pEp::PythonAdapter::decrypt_message(*this, flags);
} }
PEP_rating Message::outgoing_rating() { PEP_rating Message::outgoing_rating()
if (_msg->dir != PEP_dir_outgoing) {
if (_msg->dir != PEP_dir_outgoing) {
throw invalid_argument("Message.dir must be outgoing"); throw invalid_argument("Message.dir must be outgoing");
}
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);
@ -321,38 +347,47 @@ namespace pEp {
return rating; return rating;
} }
PEP_color Message::outgoing_color() { PEP_color Message::outgoing_color()
{
return _color(outgoing_rating()); return _color(outgoing_rating());
} }
Message Message::copy() { Message Message::copy()
{
message *dup = message_dup(*this); message *dup = message_dup(*this);
if (!dup) if (!dup) {
throw bad_alloc(); throw bad_alloc();
}
return Message(dup); return Message(dup);
} }
Message Message::deepcopy(dict &) { Message Message::deepcopy(dict &)
{
return copy(); return copy();
} }
Message outgoing_message(Identity me) { Message outgoing_message(Identity me)
if (me.address().empty() || me.user_id().empty()) {
if (me.address().empty() || me.user_id().empty()) {
throw runtime_error("at least address and user_id of own user needed"); throw runtime_error("at least address and user_id of own user needed");
}
::myself(Adapter::session(), me); ::myself(Adapter::session(), me);
auto m = Message(PEP_dir_outgoing, &me); auto m = Message(PEP_dir_outgoing, &me);
return m; return m;
} }
static object update(Identity ident) { static object update(Identity ident)
if (ident.address().empty()) {
if (ident.address().empty()) {
throw runtime_error("at least address needed"); throw runtime_error("at least address needed");
}
update_identity(Adapter::session(), ident); update_identity(Adapter::session(), ident);
return object(ident); return object(ident);
} }
static boost::python::list update(boost::python::list il) { static boost::python::list update(boost::python::list il)
{
for (int i = 0; i < len(il); i++) { for (int i = 0; i < len(il); i++) {
update(extract<Identity>(il[i])); update(extract<Identity>(il[i]));
} }
@ -360,19 +395,20 @@ namespace pEp {
return il; return il;
} }
Message incoming_message(string mime_text) { Message incoming_message(string mime_text)
{
auto m = Message(mime_text); auto m = Message(mime_text);
m.dir(PEP_dir_incoming); m.dir(PEP_dir_incoming);
try { try {
m.from(update(m.from())); m.from(update(m.from()));
} catch (out_of_range &) {
} }
catch (out_of_range &) {}
try { try {
m.recv_by(update(m.recv_by())); m.recv_by(update(m.recv_by()));
} catch (out_of_range &) {
} }
catch (out_of_range &) {}
m.to(update(m.to())); m.to(update(m.to()));
m.cc(update(m.cc())); m.cc(update(m.cc()));

303
src/pEp/_pEp/message.hh

@ -19,12 +19,12 @@
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using std::string;
using std::runtime_error;
using std::invalid_argument;
using boost::lexical_cast; using boost::lexical_cast;
using std::invalid_argument;
using std::runtime_error;
using std::string;
// Message is owning a message struct // Message is owning a message struct
class Message { class Message {
shared_ptr<::message> _msg; shared_ptr<::message> _msg;
@ -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 = "");
@ -49,19 +48,37 @@ namespace pEp {
string _repr(); string _repr();
string mime_type() { return _bl ? str_attr(_bl->mime_type) : ""; } string mime_type()
{
return _bl ? str_attr(_bl->mime_type) : "";
}
void mime_type(string value) { str_attr(_bl->mime_type, value); } void mime_type(string value)
{
str_attr(_bl->mime_type, value);
}
string filename() { return str_attr(_bl->filename); } string filename()
{
return str_attr(_bl->filename);
}
void filename(string value) { str_attr(_bl->filename, value); } void filename(string value)
{
str_attr(_bl->filename, value);
}
size_t size() { return _bl->size; } size_t size()
{
return _bl->size;
}
string decode(string encoding); string decode(string encoding);
string decode() { return decode(""); } string decode()
{
return decode("");
}
static PyBufferProcs bp; static PyBufferProcs bp;
@ -89,85 +106,199 @@ namespace pEp {
string _repr(); string _repr();
PEP_msg_direction dir() { return _msg->dir; } PEP_msg_direction dir()
{
void dir(PEP_msg_direction value) { _msg->dir = value; } return _msg->dir;
}
string id() { return str_attr(_msg->id); }
void dir(PEP_msg_direction value)
void id(string value) { str_attr(_msg->id, value); } {
_msg->dir = value;
string shortmsg() { return str_attr(_msg->shortmsg); } }
void shortmsg(string value) { str_attr(_msg->shortmsg, value); } string id()
{
string longmsg() { return str_attr(_msg->longmsg); } return str_attr(_msg->id);
}
void longmsg(string value) { str_attr(_msg->longmsg, value); }
void id(string value)
string longmsg_formatted() { return str_attr(_msg->longmsg_formatted); } {
str_attr(_msg->id, value);
void longmsg_formatted(string value) { str_attr(_msg->longmsg_formatted, value); } }
string shortmsg()
{
return str_attr(_msg->shortmsg);
}
void shortmsg(string value)
{
str_attr(_msg->shortmsg, value);
}
string longmsg()
{
return str_attr(_msg->longmsg);
}
void longmsg(string value)
{
str_attr(_msg->longmsg, value);
}
string longmsg_formatted()
{
return str_attr(_msg->longmsg_formatted);
}
void longmsg_formatted(string value)
{
str_attr(_msg->longmsg_formatted, value);
}
boost::python::tuple attachments(); boost::python::tuple attachments();
void attachments(boost::python::list value); void attachments(boost::python::list value);
time_t sent() { return timestamp_attr(_msg->sent); } time_t sent()
{
void sent(time_t value) { timestamp_attr(_msg->sent, value); } return timestamp_attr(_msg->sent);
}
time_t recv() { return timestamp_attr(_msg->recv); }
void sent(time_t value)
void recv(time_t value) { timestamp_attr(_msg->recv, value); } {
timestamp_attr(_msg->sent, value);
Identity from() { return identity_attr(_msg->from); } }
void from(object value) { identity_attr(_msg->from, value); } time_t recv()
{
boost::python::list to() { return identitylist_attr(_msg->to); } return timestamp_attr(_msg->recv);
}
void to(boost::python::list value) { identitylist_attr(_msg->to, value); }
void recv(time_t value)
Identity recv_by() { return identity_attr(_msg->recv_by); } {
timestamp_attr(_msg->recv, value);
void recv_by(object value) { identity_attr(_msg->recv_by, value); } }
boost::python::list cc() { return identitylist_attr(_msg->cc); } Identity from()
{
void cc(boost::python::list value) { identitylist_attr(_msg->cc, value); } return identity_attr(_msg->from);
}
boost::python::list bcc() { return identitylist_attr(_msg->bcc); }
void from(object value)
void bcc(boost::python::list value) { identitylist_attr(_msg->bcc, value); } {
identity_attr(_msg->from, value);
boost::python::list reply_to() { return identitylist_attr(_msg->reply_to); } }
void reply_to(boost::python::list value) { identitylist_attr(_msg->reply_to, value); } boost::python::list to()
{
boost::python::list in_reply_to() { return strlist_attr(_msg->in_reply_to); } return identitylist_attr(_msg->to);
}
void in_reply_to(boost::python::list value) { strlist_attr(_msg->in_reply_to, value); }
void to(boost::python::list value)
boost::python::list references() { return strlist_attr(_msg->references); } {
identitylist_attr(_msg->to, value);
void references(boost::python::list value) { strlist_attr(_msg->references, value); } }
boost::python::list keywords() { return strlist_attr(_msg->keywords); } Identity recv_by()
{
void keywords(boost::python::list value) { strlist_attr(_msg->keywords, value); } return identity_attr(_msg->recv_by);
}
string comments() { return str_attr(_msg->comments); }
void recv_by(object value)
void comments(string value) { str_attr(_msg->comments, value); } {
identity_attr(_msg->recv_by, value);
dict opt_fields() { return strdict_attr(_msg->opt_fields); } }
void opt_fields(dict value) { return strdict_attr(_msg->opt_fields, value); } boost::python::list cc()
{
PEP_enc_format enc_format() { return _msg->enc_format; } return identitylist_attr(_msg->cc);
}
void enc_format(PEP_enc_format value) { _msg->enc_format = value; }
void cc(boost::python::list value)
{
identitylist_attr(_msg->cc, value);
}
boost::python::list bcc()
{
return identitylist_attr(_msg->bcc);
}
void bcc(boost::python::list value)
{
identitylist_attr(_msg->bcc, value);
}
boost::python::list reply_to()
{
return identitylist_attr(_msg->reply_to);
}
void reply_to(boost::python::list value)
{
identitylist_attr(_msg->reply_to, value);
}
boost::python::list in_reply_to()
{
return strlist_attr(_msg->in_reply_to);
}
void in_reply_to(boost::python::list value)
{
strlist_attr(_msg->in_reply_to, value);
}
boost::python::list references()
{
return strlist_attr(_msg->references);
}
void references(boost::python::list value)
{
strlist_attr(_msg->references, value);
}
boost::python::list keywords()
{
return strlist_attr(_msg->keywords);
}
void keywords(boost::python::list value)
{
strlist_attr(_msg->keywords, value);
}
string comments()
{
return str_attr(_msg->comments);
}
void comments(string value)
{
str_attr(_msg->comments, value);
}
dict opt_fields()
{
return strdict_attr(_msg->opt_fields);
}
void opt_fields(dict value)
{
return strdict_attr(_msg->opt_fields, value);
}
PEP_enc_format enc_format()
{
return _msg->enc_format;
}
void enc_format(PEP_enc_format value)
{
_msg->enc_format = value;
}
Message encrypt(); Message encrypt();

84
src/pEp/_pEp/message_api.cc

@ -17,42 +17,58 @@ namespace pEp {
using namespace std; using namespace std;
using namespace boost::python; using namespace boost::python;
Message encrypt_message(Message src, boost::python::list extra, int enc_format, int flags) { Message encrypt_message(Message src, boost::python::list extra, int enc_format, int flags)
{
Identity _from = src.from(); Identity _from = src.from();
if (_from.address() == "") if (_from.address() == "") {
throw invalid_argument("encrypt_message: src.from_.address empty"); throw invalid_argument("encrypt_message: src.from_.address empty");
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;
PEP_encrypt_flags_t _flags = (PEP_encrypt_flags_t) flags; PEP_encrypt_flags_t _flags = (PEP_encrypt_flags_t)flags;
message *_dst = NULL; message *_dst = NULL;
message *_src = src; message *_src = src;
PEP_STATUS status = encrypt_message(Adapter::session(), _src, _extra, &_dst, PEP_STATUS status = encrypt_message(
_enc_format, _flags); Adapter::session(),
_src,
_extra,
&_dst,
_enc_format,
_flags);
free_stringlist(_extra); free_stringlist(_extra);
_throw_status(status); _throw_status(status);
if (!_dst || _dst == _src) if (!_dst || _dst == _src) {
return Message(_src); return Message(_src);
}
return Message(_dst); return Message(_dst);
} }
boost::python::tuple decrypt_message(Message src, int flags) { boost::python::tuple decrypt_message(Message src, int flags)
{
message *_dst = NULL; message *_dst = NULL;
stringlist_t *_keylist = NULL; stringlist_t *_keylist = NULL;
PEP_rating _rating = PEP_rating_undefined; PEP_rating _rating = PEP_rating_undefined;
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(
&_rating, &_flags); Adapter::session(),
_src,
&_dst,
&_keylist,
&_rating,
&_flags);
_throw_status(status); _throw_status(status);
boost::python::list keylist; boost::python::list keylist;
@ -65,18 +81,21 @@ namespace pEp {
return boost::python::make_tuple(dst, keylist, _rating, _flags); return boost::python::make_tuple(dst, keylist, _rating, _flags);
} }
PEP_color _color(int rating) { PEP_color _color(int rating)
return ::color_from_rating((PEP_rating) rating); {
return ::color_from_rating((PEP_rating)rating);
} }
boost::python::tuple sync_decode(object buffer) { boost::python::tuple sync_decode(object buffer)
{
Py_buffer src; Py_buffer src;
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO);
if (result) if (result) {
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_Sync_msg((char *) src.buf, src.len, &dst); PEP_STATUS status = PER_to_XER_Sync_msg((char *)src.buf, src.len, &dst);
PyBuffer_Release(&src); PyBuffer_Release(&src);
_throw_status(status); _throw_status(status);
@ -85,7 +104,8 @@ namespace pEp {
return boost::python::make_tuple(_dst, 0); return boost::python::make_tuple(_dst, 0);
} }
static boost::python::tuple sync_encode(string text) { static boost::python::tuple sync_encode(string text)
{
char *data = NULL; char *data = NULL;
size_t size = 0; size_t size = 0;
PEP_STATUS status = XER_to_PER_Sync_msg(text.c_str(), &data, &size); PEP_STATUS status = XER_to_PER_Sync_msg(text.c_str(), &data, &size);
@ -93,20 +113,23 @@ 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);
} }
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);
PyBuffer_Release(&src); PyBuffer_Release(&src);
_throw_status(status); _throw_status(status);
@ -115,7 +138,8 @@ namespace pEp {
return boost::python::make_tuple(_dst, 0); return boost::python::make_tuple(_dst, 0);
} }
static boost::python::tuple Distribution_encode(string text) { static boost::python::tuple Distribution_encode(string text)
{
char *data = NULL; char *data = NULL;
size_t size = 0; size_t size = 0;
PEP_STATUS status = XER_to_PER_Distribution_msg(text.c_str(), &data, &size); PEP_STATUS status = XER_to_PER_Distribution_msg(text.c_str(), &data, &size);
@ -123,13 +147,14 @@ 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);
} }
object sync_search(string name) { object sync_search(string name)
{
if (name != "pep.sync") { if (name != "pep.sync") {
return object(); return object();
} else { } else {
@ -143,7 +168,8 @@ namespace pEp {
} }
} }
object distribution_search(string name) { object distribution_search(string name)
{
if (name != "pep.distribution") { if (name != "pep.distribution") {
return object(); return object();
} else { } else {
@ -158,4 +184,4 @@ namespace pEp {
} }
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp { } // namespace pEp

9
src/pEp/_pEp/message_api.hh

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

841
src/pEp/_pEp/pEpmodule.cc

File diff suppressed because it is too large

74
src/pEp/_pEp/str_attr.cc

@ -15,44 +15,54 @@ namespace pEp {
using namespace boost::python; using namespace boost::python;
using namespace boost::locale; using namespace boost::locale;
object repr(object s) { object repr(object s)
{
return s.attr("__repr__")(); return s.attr("__repr__")();
} }
string repr(string s) { string repr(string s)
{
str _s = s.c_str(); str _s = s.c_str();
object _r = _s.attr("__repr__")(); object _r = _s.attr("__repr__")();
string r = extract<string>(_r); string r = extract<string>(_r);
return r; return r;
} }
string str_attr(char *&str) { string str_attr(char *&str)
if (!str) {
if (!str) {
return string(""); return string("");
}
return string(str); return string(str);
} }
void str_attr(char *&str, string value) { void str_attr(char *&str, string value)
{
string normalized = normalize(value, norm_nfc); string normalized = normalize(value, norm_nfc);
free(str); free(str);
str = strdup(normalized.c_str()); str = strdup(normalized.c_str());
if (!str) if (!str) {
throw bad_alloc(); throw bad_alloc();
}
} }
time_t timestamp_attr(timestamp *&ts) { time_t timestamp_attr(timestamp *&ts)
if (!ts) {
if (!ts) {
return 0; return 0;
}
return timegm(ts); return timegm(ts);
} }
void timestamp_attr(timestamp *&ts, time_t value) { void timestamp_attr(timestamp *&ts, time_t value)
{
free_timestamp(ts); free_timestamp(ts);
ts = new_timestamp(value); ts = new_timestamp(value);
} }
boost::python::list strlist_attr(stringlist_t *&sl) { boost::python::list strlist_attr(stringlist_t *&sl)
{
boost::python::list result; boost::python::list result;
for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) {
@ -63,14 +73,16 @@ namespace pEp {
return result; return result;
} }
void strlist_attr(stringlist_t *&sl, boost::python::list value) { void strlist_attr(stringlist_t *&sl, boost::python::list value)
{
stringlist_t *_sl = new_stringlist(NULL); stringlist_t *_sl = new_stringlist(NULL);
if (!_sl) if (!_sl) {
throw bad_alloc(); throw bad_alloc();
}
stringlist_t *_s = _sl; stringlist_t *_s = _sl;
for (int i = 0; i < len(value); i++) { for (int i = 0; i < len(value); i++) {
extract <string> extract_string(value[i]); extract<string> extract_string(value[i]);
if (!extract_string.check()) { if (!extract_string.check()) {
free_stringlist(_sl); free_stringlist(_sl);
} }
@ -87,11 +99,11 @@ namespace pEp {
sl = _sl; sl = _sl;
} }
dict strdict_attr(stringpair_list_t *&spl) { dict strdict_attr(stringpair_list_t *&spl)
{
dict result; dict result;
for (stringpair_list_t *_spl = spl; _spl && _spl->value; _spl = for (stringpair_list_t *_spl = spl; _spl && _spl->value; _spl = _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);
@ -104,18 +116,21 @@ namespace pEp {
return result; return result;
} }
void strdict_attr(stringpair_list_t *&spl, dict value) { void strdict_attr(stringpair_list_t *&spl, dict value)
{
stringpair_list_t *_spl = new_stringpair_list(NULL); stringpair_list_t *_spl = new_stringpair_list(NULL);
if (!_spl) if (!_spl) {
throw bad_alloc(); throw bad_alloc();
}
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);
@ -137,16 +152,19 @@ namespace pEp {
spl = _spl; spl = _spl;
} }
stringlist_t *to_stringlist(boost::python::list l) { stringlist_t *to_stringlist(boost::python::list l)
{
stringlist_t *result = new_stringlist(NULL); stringlist_t *result = new_stringlist(NULL);
if (!result) if (!result) {
throw bad_alloc(); throw bad_alloc();
}
stringlist_t *_s = result; stringlist_t *_s = result;
for (int i = 0; i < len(l); i++) { for (int i = 0; i < len(l); i++) {
extract <string> extract_string(l[i]); extract<string> extract_string(l[i]);
if (!extract_string.check()) if (!extract_string.check()) {
free_stringlist(result); free_stringlist(result);
}
string s = extract_string(); string s = extract_string();
_s = stringlist_add(_s, s.c_str()); _s = stringlist_add(_s, s.c_str());
if (!_s) { if (!_s) {
@ -158,7 +176,8 @@ namespace pEp {
return result; return result;
} }
boost::python::list from_stringlist(const stringlist_t *sl) { boost::python::list from_stringlist(const stringlist_t *sl)
{
boost::python::list result; boost::python::list result;
for (const stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { for (const stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) {
string s = _sl->value; string s = _sl->value;
@ -168,5 +187,4 @@ namespace pEp {
} }
} // namespace PythonAdapter } // namespace PythonAdapter
} // namespace pEp { } // namespace pEp

34
src/pEp/_pEp/str_attr.hh

@ -15,30 +15,30 @@
#include <pEp/platform.h> #include <pEp/platform.h>
namespace pEp { namespace pEp {
namespace PythonAdapter { namespace PythonAdapter {
using std::string; using boost::python::dict;
using boost::python::object; using boost::python::object;
using boost::python::dict; using std::string;
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 */
} /* namespace pEp */ } /* namespace pEp */
#endif /* STR_ATTR_HH */ #endif /* STR_ATTR_HH */
Loading…
Cancel
Save