diff --git a/src/basic_api.cc b/src/basic_api.cc index c1f4009..b2be618 100644 --- a/src/basic_api.cc +++ b/src/basic_api.cc @@ -4,21 +4,6 @@ namespace pEp { namespace PythonAdapter { - void _throw_status(PEP_STATUS status) - { - if (status == PEP_STATUS_OK) - return; - if (status >= 0x400 && status <= 0x4ff) - return; - - if (status == PEP_OUT_OF_MEMORY) - throw bad_alloc(); - - stringstream build; - build << "p≡p error: " << status; - throw runtime_error(build.str()); - } - void update_identity(Identity& ident) { if (ident.me()) diff --git a/src/basic_api.hh b/src/basic_api.hh index 5ebae15..b6bd1ae 100644 --- a/src/basic_api.hh +++ b/src/basic_api.hh @@ -4,8 +4,6 @@ namespace pEp { namespace PythonAdapter { - void _throw_status(PEP_STATUS status); - void update_identity(Identity& ident); void myself(Identity& ident); } diff --git a/src/Identity.cc b/src/identity.cc similarity index 97% rename from src/Identity.cc rename to src/identity.cc index 7189665..d12159c 100644 --- a/src/Identity.cc +++ b/src/identity.cc @@ -102,7 +102,7 @@ namespace pEp { return _ident->lang; } - object identity_attr(pEp_identity *&ident) + Identity identity_attr(pEp_identity *&ident) { pEp_identity *_dup; @@ -113,8 +113,8 @@ namespace pEp { if (!_dup) throw bad_alloc(); - Identity *_ident = new Identity(_dup); - return object(_ident); + Identity _ident(_dup); + return _ident; } void identity_attr(pEp_identity *&ident, object value) diff --git a/src/Identity.hh b/src/identity.hh similarity index 97% rename from src/Identity.hh rename to src/identity.hh index 5b080df..9c0c156 100644 --- a/src/Identity.hh +++ b/src/identity.hh @@ -52,7 +52,7 @@ namespace pEp { void flags(identity_flags_t flags) { _ident->flags = flags; } }; - object identity_attr(pEp_identity *&ident); + Identity identity_attr(pEp_identity *&ident); void identity_attr(pEp_identity *&ident, object value); list identitylist_attr(identity_list *&il); diff --git a/src/message.cc b/src/message.cc index ac357b7..091cfcb 100644 --- a/src/message.cc +++ b/src/message.cc @@ -120,9 +120,11 @@ namespace pEp { mimetext.size(), &_msg); switch (status) { case PEP_STATUS_OK: - if (_msg) + if (_msg) { + _msg->dir = PEP_dir_outgoing; return; - _msg = new_message(PEP_dir_incoming); + } + _msg = new_message(PEP_dir_outgoing); break; case PEP_BUFFER_TOO_SMALL: diff --git a/src/message.hh b/src/message.hh index b345262..25e450b 100644 --- a/src/message.hh +++ b/src/message.hh @@ -54,7 +54,7 @@ namespace pEp { Message(PEP_msg_direction dir = PEP_dir_outgoing); Message(string mimetext); Message(const Message& second); - Message(message *ident); + Message(message *msg); ~Message(); operator message *(); void attach(message *ident); @@ -86,13 +86,13 @@ namespace pEp { time_t recv() { return timestamp_attr(_msg->recv); } void recv(time_t value) { timestamp_attr(_msg->recv, value); } - object from() { return identity_attr(_msg->from); } + Identity from() { return identity_attr(_msg->from); } void from(object value) { identity_attr(_msg->from, value); } list to() { return identitylist_attr(_msg->to); } void to(list value) { identitylist_attr(_msg->to, value); } - object recv_by() { return identity_attr(_msg->recv_by); } + Identity recv_by() { return identity_attr(_msg->recv_by); } void recv_by(object value) { identity_attr(_msg->recv_by, value); } list cc() { return identitylist_attr(_msg->cc); } diff --git a/src/message_api.cc b/src/message_api.cc new file mode 100644 index 0000000..d9141ab --- /dev/null +++ b/src/message_api.cc @@ -0,0 +1,35 @@ +#include "message_api.hh" +#include + +namespace pEp { + namespace PythonAdapter { + Message encrypt_message(Message& src, list extra, int enc_format, + int flags) + { + Identity _from = src.from(); + if (_from.address() == "") + throw invalid_argument("encrypt_message: src.from_.address empty"); + if (_from.username() == "") + throw invalid_argument("encrypt_message: src.from_.username empty"); + + stringlist_t *_extra = to_stringlist(extra); + PEP_enc_format _enc_format = (PEP_enc_format) enc_format; + PEP_encrypt_flags_t _flags = (PEP_encrypt_flags_t) flags; + message *_dst = NULL; + + PEP_STATUS status = encrypt_message(session, src, _extra, &_dst, + _enc_format, _flags); + free_stringlist(_extra); + _throw_status(status); + + if (!_dst || _dst == src) { + Message dst(src); + return dst; + } + + Message dst(_dst); + return dst; + } + } +} + diff --git a/src/message_api.hh b/src/message_api.hh new file mode 100644 index 0000000..55be578 --- /dev/null +++ b/src/message_api.hh @@ -0,0 +1,11 @@ +#pragma once + +#include "pEpmodule.hh" + +namespace pEp { + namespace PythonAdapter { + Message encrypt_message(Message& src, list extra, int enc_format, + int flags); + } +} + diff --git a/src/pEpmodule.cc b/src/pEpmodule.cc index 35fd7ed..5f8adb8 100644 --- a/src/pEpmodule.cc +++ b/src/pEpmodule.cc @@ -3,6 +3,7 @@ #include #include #include "basic_api.hh" +#include "message_api.hh" namespace pEp { namespace PythonAdapter { @@ -22,6 +23,22 @@ namespace pEp { { release(session); } + + void _throw_status(PEP_STATUS status) + { + if (status == PEP_STATUS_OK) + return; + if (status >= 0x400 && status <= 0x4ff) + return; + if (status == PEP_OUT_OF_MEMORY) + throw bad_alloc(); + if (status == PEP_ILLEGAL_VALUE) + throw invalid_argument("illegal value"); + + stringstream build; + build << "p≡p error: " << status; + throw runtime_error(build.str()); + } } } @@ -120,13 +137,13 @@ BOOST_PYTHON_MODULE(pEp) .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_", (object(Message::*)()) &Message::from, + .add_property("from_", (Identity(Message::*)()) &Message::from, (void(Message::*)(object)) &Message::from, "identity where message is from") .add_property("to", (list(Message::*)()) &Message::to, (void(Message::*)(list)) &Message::to, "list of identities message is going to") - .add_property("recv_by", (object(Message::*)()) &Message::recv_by, + .add_property("recv_by", (Identity(Message::*)()) &Message::recv_by, (void(Message::*)(object)) &Message::recv_by, "identity where message was received by") .add_property("cc", (list(Message::*)()) &Message::cc, @@ -159,9 +176,17 @@ BOOST_PYTHON_MODULE(pEp) (void(Message::*)(PEP_enc_format)) &Message::enc_format, "0: unencrypted, 1: inline PGP, 2: S/MIME, 3: PGP/MIME, 4: p≡p format"); + // basic API + def("update_identity", &update_identity, "update identity information"); def("myself", &myself, "ensures that the own identity is being complete"); + // message API + + def("encrypt_message", &encrypt_message, "encrypt message in memory"); + + // init() and release() + PyModuleDef * _def = PyModule_GetDef(scope().ptr()); _def->m_free = free_module; diff --git a/src/pEpmodule.hh b/src/pEpmodule.hh index 25db5eb..23083b6 100644 --- a/src/pEpmodule.hh +++ b/src/pEpmodule.hh @@ -8,6 +8,7 @@ namespace pEp { namespace PythonAdapter { extern PEP_SESSION session; + void _throw_status(PEP_STATUS status); } } diff --git a/src/str_attr.cc b/src/str_attr.cc index 3421532..aeb603d 100644 --- a/src/str_attr.cc +++ b/src/str_attr.cc @@ -162,10 +162,10 @@ namespace pEp { return result; } - list from_stringlist(stringlist_t *sl) + list from_stringlist(const stringlist_t *sl) { list result; - for (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; result.append(s); } diff --git a/src/str_attr.hh b/src/str_attr.hh index 0fa01c9..d63f47d 100644 --- a/src/str_attr.hh +++ b/src/str_attr.hh @@ -27,7 +27,7 @@ namespace pEp { void strdict_attr(stringpair_list_t *&spl, dict value); stringlist_t *to_stringlist(list l); - list from_stringlist(stringlist_t *sl); + list from_stringlist(const stringlist_t *sl); } }