diff --git a/setup.py b/setup.py index b35fafa..19dd53f 100644 --- a/setup.py +++ b/setup.py @@ -11,7 +11,7 @@ module_pEp = Extension('pEp', include_dirs = [prefix+'/include', boost+'/include',], library_dirs = [prefix+'/lib', boost+'/lib',], libraries = ['pEpEngine', 'boost_python-mt', 'boost_locale-mt',], - extra_compile_args = ['-O0', '-UNDEBUG',], + extra_compile_args = ['-O0', '-UNDEBUG', '-std=c++14',], ) setup( diff --git a/src/identity.cc b/src/identity.cc index 4bec195..afb18c3 100644 --- a/src/identity.cc +++ b/src/identity.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include namespace pEp { @@ -13,7 +14,7 @@ namespace pEp { Identity::Identity(string address, string fpr, string user_id, string username, int comm_type, string lang) : _ident(new_identity(address.c_str(), fpr.c_str(), - user_id.c_str(), username.c_str())) + user_id.c_str(), username.c_str()), &::free_identity) { if (!_ident) throw bad_alloc(); @@ -22,46 +23,30 @@ namespace pEp { } Identity::Identity(const Identity& second) - : _ident(identity_dup(second._ident)) + : _ident(second._ident) { - if (!_ident) - throw bad_alloc(); + } Identity::Identity(pEp_identity *ident) - : _ident(ident) + : _ident(ident, &::free_identity) { } Identity::~Identity() { - free_identity(_ident); - } - Identity::operator pEp_identity *() - { - if (!_ident) - throw bad_cast(); - return _ident; } - void Identity::attach(pEp_identity *ident) + Identity::operator pEp_identity *() { - free_identity(_ident); - _ident = ident; + return _ident.get(); } - pEp_identity *Identity::detach() + Identity::operator const pEp_identity *() const { - pEp_identity *new_one = new_identity(NULL, NULL, NULL, NULL); - if (!new_one) - throw bad_alloc(); - - pEp_identity *ident = _ident; - _ident = new_one; - - return ident; + return _ident.get(); } string Identity::_repr() @@ -120,7 +105,7 @@ namespace pEp { throw invalid_argument("address must be given"); PEP_rating rating = PEP_rating_undefined; - PEP_STATUS status = identity_rating(session, _ident, &rating); + PEP_STATUS status = ::identity_rating(session, _ident.get(), &rating); _throw_status(status); return (int) rating; @@ -133,8 +118,7 @@ namespace pEp { Identity identity_attr(pEp_identity *&ident) { - pEp_identity *_dup; - + pEp_identity *_dup = NULL; if (!ident) _dup = new_identity(NULL, NULL, NULL, NULL); else @@ -148,20 +132,14 @@ namespace pEp { void identity_attr(pEp_identity *&ident, object value) { - extract< string > extract_string(value); - if (extract_string.check()) { - string str = extract_string(); - pEp_identity *_ident = new_identity(str.c_str(), NULL, NULL, NULL); - if (!_ident) - throw bad_alloc(); - free_identity(ident); - ident = _ident; - return; - } - Identity& _ident = extract< Identity& >(value); free_identity(ident); - ident = _ident.detach(); + pEp_identity *_dup = ::identity_dup(_ident); + if (!_dup) + throw bad_alloc(); + PEP_STATUS status = update_identity(session, _dup); + _throw_status(status); + ident = _dup; } list identitylist_attr(identity_list *&il) @@ -169,7 +147,7 @@ namespace pEp { list result; 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) throw bad_alloc(); result.append(object(Identity(ident))); @@ -190,8 +168,18 @@ namespace pEp { if (!extract_identity.check()) { free_identity_list(_il); } - pEp_identity *_ident = extract_identity().detach(); - _i = identity_list_add(_i, _ident); + pEp_identity *_ident = extract_identity(); + pEp_identity *_dup = ::identity_dup(_ident); + if (!_dup) { + free_identity_list(_il); + throw bad_alloc(); + } + PEP_STATUS status = update_identity(session, _dup); + if (status != PEP_STATUS_OK) { + free_identity_list(_il); + _throw_status(status); + } + _i = identity_list_add(_i, _dup); if (!_i) { free_identity_list(_il); throw bad_alloc(); diff --git a/src/identity.hh b/src/identity.hh index a3237ca..4ba60cd 100644 --- a/src/identity.hh +++ b/src/identity.hh @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include "str_attr.hh" namespace pEp { @@ -12,7 +14,7 @@ namespace pEp { // Identity is owning a pEp_identity class Identity { - pEp_identity *_ident; + shared_ptr< pEp_identity > _ident; public: Identity(string address = "", string fpr = "", string user_id = "", @@ -22,8 +24,7 @@ namespace pEp { Identity(pEp_identity *ident); ~Identity(); operator pEp_identity *(); - void attach(pEp_identity *ident); - pEp_identity *detach(); + operator const pEp_identity *() const; string _repr(); string _str(); diff --git a/src/message.cc b/src/message.cc index 0ba306e..b6b5836 100644 --- a/src/message.cc +++ b/src/message.cc @@ -6,6 +6,7 @@ #include #include #include +#include #include namespace pEp { @@ -107,25 +108,35 @@ namespace pEp { PyBufferProcs Message::Blob::bp = { getbuffer, NULL }; - Message::Message(PEP_msg_direction dir) - : _msg(new_message(dir)) + Message::Message(PEP_msg_direction dir, Identity *from) + : _msg(new_message(dir), &free_message) { if (!_msg) throw bad_alloc(); + if (from) { + _msg->from = ::identity_dup(*from); + if (!_msg->from) + throw bad_alloc(); + } } Message::Message(string mimetext) + : _msg(NULL, &free_message) { - _msg = NULL; + message *_cpy; PEP_STATUS status = mime_decode_message(mimetext.c_str(), - mimetext.size(), &_msg); + mimetext.size(), &_cpy); switch (status) { case PEP_STATUS_OK: - if (_msg) { - _msg->dir = PEP_dir_outgoing; + if (_cpy) { + _cpy->dir = PEP_dir_outgoing; + _msg = shared_ptr< message >(_cpy); return; } - _msg = new_message(PEP_dir_outgoing); + _cpy = new_message(PEP_dir_outgoing); + if (!_cpy) + throw bad_alloc(); + _msg = shared_ptr< message >(_cpy); break; case PEP_BUFFER_TOO_SMALL: @@ -145,9 +156,9 @@ namespace pEp { } Message::Message(const Message& second) - : _msg(message_dup(second._msg)) + : _msg(second._msg) { - if (!_msg) + if (!_msg.get()) throw bad_alloc(); } @@ -159,32 +170,17 @@ namespace pEp { Message::~Message() { - free_message(_msg); - } - void Message::attach(message *msg) - { - free_message(_msg); - _msg = msg; } - message *Message::detach() + Message::operator message *() { - message *new_one = new_message(_msg->dir); - if (!new_one) - throw bad_alloc(); - - message *msg = _msg; - _msg = new_one; - - return msg; + return _msg.get(); } - Message::operator message *() + Message::operator const message *() const { - if (!_msg) - throw bad_cast(); - return _msg; + return _msg.get(); } string Message::_str() @@ -195,7 +191,7 @@ namespace pEp { char *mimetext; string result; - PEP_STATUS status = mime_encode_message(_msg, false, &mimetext); + PEP_STATUS status = mime_encode_message(*this, false, &mimetext); switch (status) { case PEP_STATUS_OK: result = mimetext; @@ -227,7 +223,7 @@ namespace pEp { return build.str(); } - tuple Message::attachments() + boost::python::tuple Message::attachments() { list l; @@ -236,7 +232,7 @@ namespace pEp { l.append(Blob(bl, true)); } - return tuple(l); + return boost::python::tuple(l); } void Message::attachments(list value) @@ -292,7 +288,7 @@ namespace pEp { return encrypt_message(*this); } - tuple Message::decrypt() { + boost::python::tuple Message::decrypt() { return decrypt_message(*this); } @@ -304,7 +300,7 @@ namespace pEp { throw invalid_argument("Message.dir must be outgoing"); PEP_rating rating = PEP_rating_undefined; - PEP_STATUS status = outgoing_message_rating(session, _msg, &rating); + PEP_STATUS status = outgoing_message_rating(session, *this, &rating); _throw_status(status); return (int) rating; @@ -314,6 +310,20 @@ namespace pEp { { return _color(outgoing_rating()); } + + Message outgoing_message(Identity me) + { + ::myself(session, me); + auto m = Message(PEP_dir_outgoing, &me); + return m; + } + + Message incoming_message(string mime_text) + { + auto m = Message(mime_text); + m.dir(PEP_dir_incoming); + return m; + } } } diff --git a/src/message.hh b/src/message.hh index db78ceb..d858fbd 100644 --- a/src/message.hh +++ b/src/message.hh @@ -15,7 +15,7 @@ namespace pEp { // Message is owning a message struct class Message { - message *_msg; + shared_ptr< ::message > _msg; public: // Blob is owning a bloblist_t struct - or not and just managing @@ -50,14 +50,13 @@ namespace pEp { static int getbuffer(PyObject *self, Py_buffer *view, int flags); }; - Message(PEP_msg_direction dir = PEP_dir_outgoing); + Message(PEP_msg_direction dir = PEP_dir_outgoing, Identity *from = NULL); Message(string mimetext); Message(const Message& second); Message(message *msg); ~Message(); operator message *(); - void attach(message *ident); - message *detach(); + operator const message *() const; string _str(); string _repr(); @@ -77,7 +76,7 @@ namespace pEp { string longmsg_formatted() { return str_attr(_msg->longmsg_formatted); } void longmsg_formatted(string value) { str_attr(_msg->longmsg_formatted, value); } - tuple attachments(); + boost::python::tuple attachments(); void attachments(list value); time_t sent() { return timestamp_attr(_msg->sent); } @@ -126,10 +125,14 @@ namespace pEp { Message encrypt(list extra, int enc_format); Message encrypt(list extra); Message encrypt(); - tuple decrypt(); + boost::python::tuple decrypt(); int outgoing_rating(); int outgoing_color(); + }; + + Message outgoing_message(Identity me); + Message incoming_message(string mime_text); } } diff --git a/src/message_api.cc b/src/message_api.cc index 6831191..112641f 100644 --- a/src/message_api.cc +++ b/src/message_api.cc @@ -32,7 +32,7 @@ namespace pEp { return dst; } - tuple decrypt_message(Message src) + boost::python::tuple decrypt_message(Message src) { message *_dst = NULL; stringlist_t *_keylist = NULL; @@ -53,7 +53,7 @@ namespace pEp { int flags = (int) _flags; Message dst(_dst); - return make_tuple(dst, keylist, rating, flags); + return boost::python::make_tuple(dst, keylist, rating, flags); } int _color(int rating) diff --git a/src/message_api.hh b/src/message_api.hh index 39f968e..8f1ec59 100644 --- a/src/message_api.hh +++ b/src/message_api.hh @@ -6,7 +6,7 @@ namespace pEp { namespace PythonAdapter { Message encrypt_message(Message src, list extra = list(), int enc_format = 4, int flags = 0); - tuple decrypt_message(Message src); + boost::python::tuple decrypt_message(Message src); int _color(int rating); } } diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index 6725b94..39c7e2c 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -133,7 +133,7 @@ BOOST_PYTHON_MODULE(pEp) .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", (tuple(Message::*)()) &Message::attachments, + .add_property("attachments", (boost::python::tuple(Message::*)()) &Message::attachments, (void(Message::*)(list)) &Message::attachments, "tuple of Blobs with attachments; setting moves Blobs to attachment tuple") .add_property("sent", (time_t(Message::*)()) &Message::sent, @@ -195,6 +195,8 @@ BOOST_PYTHON_MODULE(pEp) // message API + def("incoming_message", &incoming_message, "create an incoming message from a MIME text"); + def("outgoing_message", &outgoing_message, "create an outgoing message using an own identity"); def("color", &_color, "calculate color value out of rating"); def("trustwords", &_trustwords, "calculate trustwords for two Identities"); diff --git a/src/sync_mixin.cc b/src/sync_mixin.cc index bda136a..602c90b 100644 --- a/src/sync_mixin.cc +++ b/src/sync_mixin.cc @@ -55,10 +55,10 @@ namespace pEp { } #ifndef NDEBUG - void SyncMixIn::_inject(int event, Identity *partner, object extra) + void SyncMixIn::_inject(int event, Identity partner, object extra) { PEP_STATUS status = fsm_DeviceState_inject(session, - (DeviceState_event) event, partner->detach(), NULL); + (DeviceState_event) event, partner, NULL); } #endif diff --git a/src/sync_mixin.hh b/src/sync_mixin.hh index 52c7f1d..8dff9f4 100644 --- a/src/sync_mixin.hh +++ b/src/sync_mixin.hh @@ -19,7 +19,7 @@ namespace pEp { virtual void deliverHandshakeResult(int result); #ifndef NDEBUG - virtual void _inject(int event, Identity *partner, object extra); + virtual void _inject(int event, Identity partner, object extra); #endif protected: