Compare commits
22 Commits
Author | SHA1 | Date |
---|---|---|
![]() |
16e9bda15f | 4 years ago |
![]() |
348fa8f2f9 | 4 years ago |
![]() |
8e1898446e | 4 years ago |
![]() |
da1d3a8347 | 4 years ago |
![]() |
103208ea95 | 4 years ago |
![]() |
7a84853814 | 4 years ago |
![]() |
d87fd19652 | 4 years ago |
![]() |
784653df6f | 4 years ago |
![]() |
6a1d302821 | 5 years ago |
![]() |
dc0b769136 | 5 years ago |
![]() |
119effbfdb | 5 years ago |
![]() |
7b4ba8b4c4 | 5 years ago |
![]() |
b153ce3c7a | 5 years ago |
![]() |
2e138e1190 | 5 years ago |
![]() |
0792869686 | 5 years ago |
![]() |
c56b3b40f3 | 5 years ago |
![]() |
19b2ebb18d | 5 years ago |
![]() |
0e896bf36c | 5 years ago |
![]() |
0e893624c9 | 5 years ago |
![]() |
63e830040c | 5 years ago |
![]() |
688fff5667 | 5 years ago |
![]() |
8aa36d9f95 | 5 years ago |
37 changed files with 2381 additions and 2024 deletions
@ -0,0 +1,8 @@ |
|||
Module pEp._gen |
|||
--------------- |
|||
.. automodule:: pEp._gen |
|||
:members: |
|||
:imported-members: |
|||
:undoc-members: |
|||
:show-inheritance: |
|||
|
@ -0,0 +1,7 @@ |
|||
Module pEp._pEp |
|||
--------------- |
|||
.. automodule:: pEp._pEp |
|||
:members: |
|||
:imported-members: |
|||
:undoc-members: |
|||
:show-inheritance: |
@ -0,0 +1,2 @@ |
|||
pEpACIDgen |
|||
pyBind11 |
@ -0,0 +1,16 @@ |
|||
HERE:=$(dir $(lastword $(MAKEFILE_LIST))) |
|||
-include $(HERE)../../local.conf |
|||
|
|||
TARGET_PEP=_pEp.so |
|||
TARGET_GEN=_gen.so |
|||
|
|||
# General Build settings
|
|||
CXX=g++ |
|||
CXXFLAGS+=-std=c++11 -g |
|||
INCLUDES+=-I$(PREFIX)/include |
|||
INCLUDES+=-I/opt/local/Library/Frameworks/Python.framework/Versions/3.8/include/python3.8 |
|||
LIB_DIRS=-L$(PREFIX)/lib |
|||
LIB_DIRS+=-L/opt/local/lib |
|||
LIBS=-lpEpEngine -lpEpAdapter |
|||
LDFLAGS+=-undefined dynamic_lookup |
|||
|
@ -0,0 +1,46 @@ |
|||
include Makefile.conf |
|||
|
|||
TARGET=_gen.so |
|||
|
|||
# Swap here, for static vs dyn linking
|
|||
TARGET_MODULE_DYN=$(TARGET) |
|||
TARGET_MODULE_STATIC= |
|||
|
|||
CXX=clang |
|||
CXXFLAGS+=-std=c++11 -g |
|||
|
|||
SRCS+=$(wildcard *.cc) |
|||
OBJS+=$(SRCS:.cc=.o) |
|||
|
|||
CXXFLAGS+=$(INCLUDES) |
|||
LDFLAGS_DYN+=-undefined dynamic_lookup $(LIBS_PATH) $(LIBS) |
|||
LDFLAGS_STATIC+=-undefined dynamic_lookup |
|||
|
|||
$(info -----BUILD INFO----) |
|||
$(info SRCS $(SRCS)) |
|||
$(info OBJS $(OBJS)) |
|||
|
|||
.PHONY: all gen gen-pybind module_dyn module_static clean |
|||
|
|||
all: gen compile |
|||
|
|||
gen: |
|||
$(MAKE) -C gen |
|||
|
|||
gen-pybind: |
|||
$(MAKE) -C gen pybind |
|||
|
|||
compile: $(TARGET) |
|||
|
|||
$(TARGET_MODULE_DYN) : $(OBJS) |
|||
$(CXX) $(LDFLAGS_DYN) -o $@ $^ |
|||
|
|||
$(TARGET_MODULE_STATIC) : $(OBJS) $(LIBS_STATIC) |
|||
$(CXX) $(LDFLAGS_STATIC) -o $@ $^ |
|||
|
|||
clean: |
|||
rm -f $(TARGET) |
|||
rm -f $(OBJS) |
|||
|
|||
clean-all: clean |
|||
$(MAKE) -C gen clean |
@ -0,0 +1,12 @@ |
|||
# pyBind11 and python headers
|
|||
INCLUDES+=$(shell pybind11-config --includes) |
|||
|
|||
# example lib
|
|||
INCLUDES+=-I/Users/heck/local-default/include/ |
|||
|
|||
# static lib (.a)
|
|||
#LIBS_STATIC+=/Users/heck/local-default/include/
|
|||
|
|||
# dynamic lib (.so)
|
|||
LIBS+=-lpEpEngine |
|||
LIBS_PATH+=-L/Users/heck/local-default/lib |
@ -0,0 +1,40 @@ |
|||
// This file is under GNU Affero General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#ifndef ADAPTER_MAIN_HH |
|||
#define ADAPTER_MAIN_HH |
|||
|
|||
// Use only if cant be avoided
|
|||
//#include <pEp/stringlist.h>
|
|||
|
|||
/// =======
|
|||
#include <pEp/pEpEngine.h> |
|||
//// Engine
|
|||
////pEp-API
|
|||
#include <pEp/keymanagement.h> |
|||
#include <pEp/message_api.h> |
|||
#include <pEp/sync_api.h> |
|||
#include <pEp/distribution_codec.h> |
|||
#include <pEp/sync_codec.h> |
|||
/// =======
|
|||
|
|||
// #include <pEp/identity_list.h>
|
|||
// #include <pEp/key_reset.h>
|
|||
// #include <pEp/mime.h>
|
|||
// #include <pEp/message.h>
|
|||
// #include <pEp/sync_codec.h>
|
|||
// #include <pEp/distribution_codec.h>
|
|||
// #include <pEp/timestamp.h>
|
|||
// #include <pEp/stringpair.h>
|
|||
|
|||
// libpEpAdapter
|
|||
/// =======
|
|||
//#include <pEp/Adapter.hh>
|
|||
//#include <pEp/callback_dispatcher.hh>
|
|||
/// =======
|
|||
|
|||
|
|||
// #include <pEp/status_to_string.hh>
|
|||
// #include <pEp/pEpLog.hh>
|
|||
|
|||
#endif // ADAPTER_MAIN_HH
|
@ -0,0 +1,35 @@ |
|||
include ../Makefile.conf |
|||
|
|||
YML2_FILE=py_module.yml2 |
|||
YSL2_FILE=$(shell pEp_acid_gen-config) |
|||
PYBIND11_FILE=py_module.pybind11 |
|||
DEBUG_AST_FILE=adapter_main.hh.ast.json |
|||
DEBUG_ACID_FILE=adapter_main.hh.acid.json |
|||
DEBUG_YML_FILE=adapter_main.hh.acid.yml |
|||
|
|||
$(info -----_gen GEN----) |
|||
$(info YML2_FILE $(YML2_FILE)) |
|||
$(info YSL2_FILE $(YSL2_FILE)) |
|||
$(info CC_FILE $(PYBIND11_FILE)) |
|||
|
|||
.PHONY = all yml pybind |
|||
|
|||
all: pybind |
|||
|
|||
yml: $(YML2_FILE) |
|||
|
|||
pybind: $(YML2_FILE) $(PYBIND11_FILE) |
|||
|
|||
$(YML2_FILE): config.json |
|||
pEp_acid_gen $^ |
|||
|
|||
$(PYBIND11_FILE) : $(YML2_FILE) |
|||
yml2proc --encoding=utf8 -y $(YSL2_FILE) $(YML2_FILE) |
|||
|
|||
clean: |
|||
rm -f $(YML2_FILE) |
|||
rm -f $(PYBIND11_FILE) |
|||
rm -f $(DEBUG_AST_FILE) |
|||
rm -f $(DEBUG_ACID_FILE) |
|||
rm -f $(DEBUG_YML_FILE) |
|||
|
@ -0,0 +1,98 @@ |
|||
{ |
|||
"module_name": "_gen", |
|||
"header_filename": "../adapter_main.hh", |
|||
"libclang_path": "/opt/local/libexec/llvm-9.0/lib/libclang.dylib", |
|||
"variables": [ |
|||
], |
|||
"functions": [ |
|||
"KEYMANAGEMENT", |
|||
"update_identity", |
|||
"myself", |
|||
"//register_examine_function", |
|||
"//do_keymanagement", |
|||
"key_mistrusted", |
|||
"trust_personal_key", |
|||
"trust_own_key", |
|||
"key_reset_trust", |
|||
"//own_key_is_listed", |
|||
"_own_identities_retrieve", |
|||
"own_identities_retrieve", |
|||
"_own_keys_retrieve", |
|||
"own_keys_retrieve", |
|||
"set_own_key", |
|||
"//clean_own_key_defaults", |
|||
|
|||
"//PEPENGINE", |
|||
"update_identity", |
|||
"myself", |
|||
"//register_examine_function", |
|||
"//do_keymanagement", |
|||
"key_mistrusted", |
|||
"trust_personal_key", |
|||
"trust_own_key", |
|||
"key_reset_trust", |
|||
"//own_key_is_listed", |
|||
"own_identities_retrieve", |
|||
"//own_keys_retrieve", |
|||
"set_own_key", |
|||
"//clean_own_key_defaults", |
|||
"new_identity", |
|||
|
|||
"//MESSAGE_API", |
|||
"encrypt_message", |
|||
"encrypt_message_and_add_priv_key", |
|||
"encrypt_message_for_self", |
|||
"color_from_rating", |
|||
"decrypt_message", |
|||
"own_message_private_key_details", |
|||
"outgoing_message_rating", |
|||
"outgoing_message_rating_preview", |
|||
"identity_rating", |
|||
"get_binary_path", |
|||
"get_trustwords", |
|||
"get_message_trustwords", |
|||
"get_trustwords_for_fprs", |
|||
"re_evaluate_message_rating", |
|||
"get_key_rating_for_user", |
|||
"rating_from_comm_type", |
|||
"probe_encrypt", |
|||
|
|||
"//MESSAGE", |
|||
"new_message", |
|||
"free_message", |
|||
"message_dup", |
|||
"message_transfer", |
|||
"new_message_ref_list", |
|||
"free_message_ref_list", |
|||
"message_ref_list_dup", |
|||
"message_ref_list_add", |
|||
|
|||
"//SYNC_API", |
|||
"deliverHandshakeResult", |
|||
"//register_sync_callbacks", |
|||
"//unregister_sync_callbacks", |
|||
"//do_sync_protocol", |
|||
"//do_sync_protocol_step", |
|||
"//is_sync_thread", |
|||
"//new_sync_timeout_event", |
|||
"enter_device_group", |
|||
"//leave_device_group", |
|||
"enable_identity_for_sync", |
|||
"disable_identity_for_sync", |
|||
|
|||
"//SYNC_CODEC", |
|||
"//decode_Sync_message", |
|||
"//encode_Sync_message", |
|||
"//PER_to_XER_Sync_msg", |
|||
"//XER_to_PER_Sync_msg", |
|||
|
|||
"//DISTRIBUTION_CODEC", |
|||
"//decode_Distribution_message", |
|||
"//encode_Distribution_message", |
|||
"//PER_to_XER_Distribution_msg", |
|||
"//XER_to_PER_Distribution_msg" |
|||
], |
|||
"debug_ast" : 0, |
|||
"debug_acid" : 0, |
|||
"debug_yml" : 0 |
|||
} |
@ -0,0 +1,22 @@ |
|||
#include <string> |
|||
#include <iostream> |
|||
#include <pybind11/pybind11.h> |
|||
#include <pybind11/detail/common.h> |
|||
#include "adapter_main.hh" |
|||
#include <pEp/Adapter.hh> |
|||
#include <pEp/callback_dispatcher.hh> |
|||
|
|||
using namespace std; |
|||
namespace alib = pEp::Adapter; |
|||
|
|||
PEP_SESSION pep_session() { |
|||
return alib::session(); |
|||
} |
|||
|
|||
PYBIND11_MODULE(_gen, m) { |
|||
|
|||
// PEP_SESSION
|
|||
// m.def("pep_session",(PEP_SESSION(*)()) &pep_session);
|
|||
|
|||
#include "gen/py_module.pybind11" |
|||
} |
@ -0,0 +1,32 @@ |
|||
include ../Makefile.conf |
|||
TARGET=$(TARGET_PEP) |
|||
|
|||
# Specific Build settings
|
|||
CXXFLAGS+=-Wno-unused-result -Wsign-compare -Wunreachable-code -fno-common -dynamic -g -fwrapv -O3 -Wall -pipe -Os -isysroot/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk |
|||
INCLUDES+= |
|||
LIB_DIRS+= |
|||
LIBS+=-lboost_python38-mt -lboost_locale-mt |
|||
LDFLAGS+= |
|||
|
|||
|
|||
|
|||
# Combine Settings
|
|||
CXXFLAGS+=$(INCLUDES) |
|||
LDFLAGS+=$(LIB_DIRS) |
|||
LDFLAGS+=$(LIBS) |
|||
|
|||
SRCS:=$(wildcard *.cc) |
|||
OBJS:=$(SRCS:.cc=.o) |
|||
|
|||
.PHONY: clean |
|||
|
|||
all: $(TARGET) |
|||
|
|||
# Using implicit compile target
|
|||
# And explicit link taget
|
|||
$(TARGET): $(OBJS) |
|||
$(CXX) $(LDFLAGS) -o $@ $^ |
|||
|
|||
clean: |
|||
rm -rf $(TARGET) |
|||
rm -rf $(OBJS) |
@ -0,0 +1,163 @@ |
|||
// This file is under GNU Affero General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#include "adapter_main.hh" |
|||
#include "message.hh" |
|||
#include "message_api.hh" |
|||
|
|||
namespace pEp { |
|||
namespace PythonAdapter { |
|||
|
|||
|
|||
static const char *version_string = "p≡p Python adapter version 0.3"; |
|||
|
|||
void init_before_main_module() { |
|||
pEpLog("called"); |
|||
} |
|||
|
|||
// hidden init function, wrapped by hello_world.init()
|
|||
void _init_after_main_module() { |
|||
pEpLog("called"); |
|||
callback_dispatcher.add(_messageToSend, notifyHandshake, nullptr, nullptr); |
|||
Adapter::_messageToSend = CallbackDispatcher::messageToSend; |
|||
} |
|||
|
|||
void config_passive_mode(bool enable) { |
|||
::config_passive_mode(Adapter::session(), enable); |
|||
} |
|||
|
|||
void config_unencrypted_subject(bool enable) { |
|||
::config_unencrypted_subject(Adapter::session(), enable); |
|||
} |
|||
|
|||
void key_reset_user(const string &user_id, const string &fpr) { |
|||
if (user_id == "") { |
|||
throw invalid_argument("user_id required"); |
|||
} |
|||
|
|||
::PEP_STATUS status = ::key_reset_user(Adapter::session(), user_id.c_str(), fpr != "" ? fpr.c_str() : nullptr); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
void key_reset_user2(const string &user_id) { |
|||
key_reset_user(user_id, ""); |
|||
} |
|||
|
|||
void key_reset_all_own_keys() { |
|||
::PEP_STATUS status = ::key_reset_all_own_keys(Adapter::session()); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
string about() { |
|||
string version = string(version_string) + "\np≡p version " + PEP_VERSION + "\n"; |
|||
return version; |
|||
} |
|||
|
|||
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"); |
|||
} |
|||
|
|||
if (status_to_string(status) == "unknown status code") { |
|||
stringstream build; |
|||
build << setfill('0') << "p≡p 0x" << setw(4) << hex << status; |
|||
throw runtime_error(build.str()); |
|||
} else { |
|||
throw runtime_error(status_to_string(status)); |
|||
} |
|||
} |
|||
|
|||
::PEP_STATUS _messageToSend(::message *msg) { |
|||
pEpLog("called"); |
|||
try { |
|||
PyGILState_STATE gil = PyGILState_Ensure(); |
|||
pEpLog("GIL Aquired"); |
|||
bp::object modref = bp::import("pEp"); |
|||
bp::object funcref = modref.attr("message_to_send"); |
|||
bp::call<void>(funcref.ptr(), Message()); |
|||
PyGILState_Release(gil); |
|||
pEpLog("GIL released"); |
|||
} catch (exception &e) { |
|||
} |
|||
|
|||
return ::PEP_STATUS_OK; |
|||
} |
|||
|
|||
::PEP_STATUS notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal) { |
|||
pEpLog("called"); |
|||
try { |
|||
PyGILState_STATE gil = PyGILState_Ensure(); |
|||
pEpLog("GIL Aquired"); |
|||
bp::object modref = bp::import("pEp"); |
|||
bp::object funcref = modref.attr("notify_handshake"); |
|||
bp::call<void>(funcref.ptr(), me, partner, signal); |
|||
PyGILState_Release(gil); |
|||
pEpLog("GIL released"); |
|||
} catch (exception &e) { |
|||
} |
|||
|
|||
return ::PEP_STATUS_OK; |
|||
} |
|||
|
|||
void start_sync() { |
|||
CallbackDispatcher::start_sync(); |
|||
} |
|||
|
|||
void shutdown_sync() { |
|||
CallbackDispatcher::stop_sync(); |
|||
} |
|||
|
|||
void debug_color(int ansi_color) { |
|||
::set_debug_color(Adapter::session(), ansi_color); |
|||
} |
|||
|
|||
void leave_device_group() { |
|||
::leave_device_group(Adapter::session()); |
|||
} |
|||
|
|||
bool is_sync_active() { |
|||
return Adapter::is_sync_running(); |
|||
} |
|||
|
|||
void testfunc() { |
|||
_messageToSend(nullptr); |
|||
} |
|||
|
|||
void deliverHandshakeResult(int result, bp::object identities) { |
|||
identity_list *shared_identities = nullptr; |
|||
if (identities != bp::api::object() && boost::python::len(identities)) { |
|||
shared_identities = ::new_identity_list(nullptr); |
|||
if (!shared_identities) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
try { |
|||
::identity_list *si = shared_identities; |
|||
for (int i = 0; i < bp::len(identities); ++i) { |
|||
Identity ident = bp::extract<Identity>(identities[i]); |
|||
si = ::identity_list_add(si, ident); |
|||
if (!si) { |
|||
throw bad_alloc(); |
|||
} |
|||
} |
|||
} catch (exception &ex) { |
|||
::free_identity_list(shared_identities); |
|||
throw ex; |
|||
} |
|||
} |
|||
|
|||
::PEP_STATUS status = ::deliverHandshakeResult(Adapter::session(), (::sync_handshake_result)result, shared_identities); |
|||
free_identity_list(shared_identities); |
|||
_throw_status(status); |
|||
} |
|||
} // namespace PythonAdapter
|
|||
} // namespace pEp
|
@ -0,0 +1,84 @@ |
|||
// This file is under GNU Affero General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
#ifndef ADAPTER_MAIN_HH |
|||
#define ADAPTER_MAIN_HH |
|||
|
|||
// System
|
|||
#include <string> |
|||
#include <iomanip> |
|||
|
|||
// Boost
|
|||
#include <boost/python.hpp> |
|||
#include <boost/locale.hpp> |
|||
|
|||
// Engine
|
|||
#include <pEp/pEpEngine.h> |
|||
#include <pEp/keymanagement.h> |
|||
#include <pEp/identity_list.h> |
|||
#include <pEp/key_reset.h> |
|||
#include <pEp/sync_api.h> |
|||
#include <pEp/mime.h> |
|||
#include <pEp/message.h> |
|||
#include <pEp/message_api.h> |
|||
#include <pEp/sync_codec.h> |
|||
#include <pEp/distribution_codec.h> |
|||
#include <pEp/timestamp.h> |
|||
#include <pEp/stringpair.h> |
|||
|
|||
// libpEpAdapter
|
|||
#include <pEp/Adapter.hh> |
|||
#include <pEp/callback_dispatcher.hh> |
|||
#include <pEp/status_to_string.hh> |
|||
#include <pEp/pEpLog.hh> |
|||
|
|||
namespace pEp { |
|||
namespace PythonAdapter { |
|||
|
|||
using namespace std; |
|||
namespace bp = boost::python; |
|||
namespace bl = boost::locale; |
|||
|
|||
void init_before_main_module(); |
|||
|
|||
void _init_after_main_module(); |
|||
|
|||
void testfunc(); |
|||
|
|||
|
|||
//extern string device_name;
|
|||
|
|||
string about(); |
|||
|
|||
void config_passive_mode(bool enable); |
|||
|
|||
void start_sync(); |
|||
|
|||
void shutdown_sync(); |
|||
|
|||
void debug_color(int ansi_color); |
|||
|
|||
bool is_sync_active(); |
|||
|
|||
void config_unencrypted_subject(bool enable); |
|||
|
|||
void key_reset_user(const string &user_id, const string &fpr); |
|||
|
|||
void key_reset_user2(const string &user_id); |
|||
|
|||
void key_reset_all_own_keys(); |
|||
|
|||
void _throw_status(::PEP_STATUS status); |
|||
|
|||
void leave_device_group(); |
|||
|
|||
::PEP_STATUS _messageToSend(::message *msg); |
|||
|
|||
::PEP_STATUS notifyHandshake(::pEp_identity *me, ::pEp_identity *partner, ::sync_handshake_signal signal); |
|||
|
|||
void deliverHandshakeResult(int result, bp::object identities); |
|||
|
|||
} // namespace PythonAdapter
|
|||
} // namespace pEp
|
|||
|
|||
#endif // ADAPTER_MAIN_HH
|
@ -1,161 +1,152 @@ |
|||
// This file is under GNU Affero General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
// System
|
|||
#include <sstream> |
|||
|
|||
// Engine
|
|||
#include <pEp/keymanagement.h> |
|||
#include <pEp/message_api.h> |
|||
#include <pEp/Adapter.hh> |
|||
|
|||
// local
|
|||
#include "basic_api.hh" |
|||
|
|||
namespace pEp { |
|||
namespace PythonAdapter { |
|||
using namespace std; |
|||
|
|||
void update_identity(Identity &ident) { |
|||
if (ident.address() == "") |
|||
throw invalid_argument("address needed"); |
|||
if (ident.user_id() == PEP_OWN_USERID) |
|||
throw runtime_error("update_identity: '" |
|||
PEP_OWN_USERID |
|||
"' may only be used for own identities"); |
|||
|
|||
PEP_STATUS status = update_identity(Adapter::session(), ident); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
void myself(Identity &ident) { |
|||
if (ident.address() == "") |
|||
throw invalid_argument("address needed"); |
|||
if (ident.username() == "") |
|||
throw invalid_argument("username needed"); |
|||
|
|||
if (ident.user_id() == "") |
|||
ident.user_id(ident.address()); |
|||
|
|||
PEP_STATUS status = myself(Adapter::session(), ident); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
string _trustwords(Identity me, Identity partner, string lang, bool full) { |
|||
if (me.fpr() == "" || partner.fpr() == "") |
|||
throw invalid_argument("fingerprint needed in Identities"); |
|||
|
|||
if (lang == "" && me.lang() == partner.lang()) |
|||
lang = me.lang(); |
|||
|
|||
char *words = NULL; |
|||
size_t size = 0; |
|||
PEP_STATUS status = get_trustwords(Adapter::session(), me, partner, |
|||
lang.c_str(), &words, &size, full); |
|||
_throw_status(status); |
|||
return words; |
|||
} |
|||
|
|||
void trust_personal_key(Identity ident) { |
|||
if (ident.fpr() == "") |
|||
throw invalid_argument("fingerprint needed in Identities"); |
|||
if (ident.user_id() == "") |
|||
throw invalid_argument("user_id must be provided"); |
|||
|
|||
PEP_STATUS status = trust_personal_key(Adapter::session(), ident); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
void set_identity_flags(Identity ident, identity_flags_t flags) { |
|||
if (ident.address() == "") |
|||
throw invalid_argument("address needed"); |
|||
if (ident.user_id() == "") |
|||
throw invalid_argument("user_id needed"); |
|||
|
|||
PEP_STATUS status = set_identity_flags(Adapter::session(), ident, flags); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
void unset_identity_flags(Identity ident, identity_flags_t flags) { |
|||
if (ident.address() == "") |
|||
throw invalid_argument("address needed"); |
|||
if (ident.user_id() == "") |
|||
throw invalid_argument("user_id needed"); |
|||
|
|||
PEP_STATUS status = unset_identity_flags(Adapter::session(), ident, flags); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
void key_reset_trust(Identity ident) { |
|||
if (ident.fpr() == "") |
|||
throw invalid_argument("fpr needed"); |
|||
if (ident.address() == "") |
|||
throw invalid_argument("address needed"); |
|||
if (ident.user_id() == "") |
|||
throw invalid_argument("user_id needed"); |
|||
|
|||
PEP_STATUS status = key_reset_trust(Adapter::session(), ident); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
|
|||
boost::python::list import_key(string key_data) { |
|||
::identity_list *private_keys = NULL; |
|||
PEP_STATUS status = ::import_key(Adapter::session(), key_data.c_str(), key_data.size(), &private_keys); |
|||
if (status && status != PEP_KEY_IMPORTED) |
|||
_throw_status(status); |
|||
|
|||
auto result = boost::python::list(); |
|||
for (::identity_list *il = private_keys; il && il->ident; il = il->next) { |
|||
::pEp_identity *ident = ::identity_dup(il->ident); |
|||
if (!ident) { |
|||
free_identity_list(private_keys); |
|||
throw bad_alloc(); |
|||
} |
|||
result.append(Identity(ident)); |
|||
} |
|||
|
|||
free_identity_list(private_keys); |
|||
return result; |
|||
} |
|||
|
|||
string export_key(Identity ident) { |
|||
PEP_STATUS status = PEP_STATUS_OK; |
|||
char *key_data = NULL; |
|||
size_t size; |
|||
status = ::export_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size); |
|||
|
|||
_throw_status(status); |
|||
return key_data; |
|||
} |
|||
|
|||
string export_secret_key(Identity ident) { |
|||
PEP_STATUS status = PEP_STATUS_OK; |
|||
char *key_data = NULL; |
|||
size_t size; |
|||
status = ::export_secret_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size); |
|||
|
|||
_throw_status(status); |
|||
return key_data; |
|||
namespace PythonAdapter { |
|||
|
|||
void update_identity(Identity &ident) { |
|||
if (ident.address() == "") { |
|||
throw invalid_argument("address needed"); |
|||
} |
|||
if (ident.user_id() == PEP_OWN_USERID) { |
|||
throw runtime_error("update_identity: '" PEP_OWN_USERID "' may only be used for own identities"); |
|||
} |
|||
::PEP_STATUS status = ::update_identity(Adapter::session(), ident); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
void myself(Identity &ident) { |
|||
if (ident.address() == "") { |
|||
throw invalid_argument("address needed"); |
|||
} |
|||
if (ident.username() == "") { |
|||
throw invalid_argument("username needed"); |
|||
} |
|||
if (ident.user_id() == "") { |
|||
ident.user_id(ident.address()); |
|||
} |
|||
::PEP_STATUS status = ::myself(Adapter::session(), ident); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
string _trustwords(Identity me, Identity partner, string lang, bool full) { |
|||
if (me.fpr() == "" || partner.fpr() == "") { |
|||
throw invalid_argument("fingerprint needed in Identities"); |
|||
} |
|||
if (lang == "" && me.lang() == partner.lang()) { |
|||
lang = me.lang(); |
|||
} |
|||
char *words = nullptr; |
|||
size_t size = 0; |
|||
::PEP_STATUS status = ::get_trustwords(Adapter::session(), me, partner, lang.c_str(), &words, &size, full); |
|||
_throw_status(status); |
|||
return words; |
|||
} |
|||
|
|||
void trust_personal_key(Identity ident) { |
|||
if (ident.fpr() == "") { |
|||
throw invalid_argument("fingerprint needed in Identities"); |
|||
} |
|||
if (ident.user_id() == "") { |
|||
throw invalid_argument("user_id must be provided"); |
|||
} |
|||
::PEP_STATUS status = ::trust_personal_key(Adapter::session(), ident); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
void set_identity_flags(Identity ident,const ::identity_flags_t &flags) { |
|||
if (ident.address() == "") { |
|||
throw invalid_argument("address needed"); |
|||
} |
|||
if (ident.user_id() == "") { |
|||
throw invalid_argument("user_id needed"); |
|||
} |
|||
::PEP_STATUS status = ::set_identity_flags(Adapter::session(), ident, flags); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
void unset_identity_flags(Identity ident,const ::identity_flags_t &flags) { |
|||
if (ident.address() == "") { |
|||
throw invalid_argument("address needed"); |
|||
} |
|||
if (ident.user_id() == "") { |
|||
throw invalid_argument("user_id needed"); |
|||
} |
|||
::PEP_STATUS status = ::unset_identity_flags(Adapter::session(), ident, flags); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
void key_reset_trust(Identity ident) { |
|||
if (ident.fpr() == "") { |
|||
throw invalid_argument("fpr needed"); |
|||
} |
|||
if (ident.address() == "") { |
|||
throw invalid_argument("address needed"); |
|||
} |
|||
if (ident.user_id() == "") { |
|||
throw invalid_argument("user_id needed"); |
|||
} |
|||
::PEP_STATUS status = ::key_reset_trust(Adapter::session(), ident); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
bp::list import_key(const string &key_data) { |
|||
::identity_list *private_keys = nullptr; |
|||
::PEP_STATUS status = ::import_key(Adapter::session(), key_data.c_str(), key_data.size(), &private_keys); |
|||
if (status && status != ::PEP_KEY_IMPORTED) { |
|||
_throw_status(status); |
|||
} |
|||
auto result = bp::list(); |
|||
for (::identity_list *il = private_keys; il && il->ident; il = il->next) { |
|||
::pEp_identity *ident = ::identity_dup(il->ident); |
|||
if (!ident) { |
|||
::free_identity_list(private_keys); |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
void set_own_key(Identity &ident, string fpr) { |
|||
if (ident.address() == "") |
|||
throw invalid_argument("address needed"); |
|||
if (ident.username() == "") |
|||
throw invalid_argument("username needed"); |
|||
if (ident.user_id() == "") |
|||
throw invalid_argument("user_id needed"); |
|||
if (fpr == "") |
|||
throw invalid_argument("fpr needed"); |
|||
|
|||
|
|||
const char *fpr_c = fpr.c_str(); |
|||
PEP_STATUS status = set_own_key(Adapter::session(), ident, fpr_c); |
|||
_throw_status(status); |
|||
} |
|||
|
|||
} // namespace PythonAdapter
|
|||
result.append(Identity(ident)); |
|||
} |
|||
::free_identity_list(private_keys); |
|||
return result; |
|||
} |
|||
|
|||
string export_key(Identity ident) { |
|||
::PEP_STATUS status = ::PEP_STATUS_OK; |
|||
char *key_data = nullptr; |
|||
size_t size; |
|||
status = ::export_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size); |
|||
_throw_status(status); |
|||
return key_data; |
|||
} |
|||
|
|||
string export_secret_key(Identity ident) { |
|||
::PEP_STATUS status = ::PEP_STATUS_OK; |
|||
char *key_data = NULL; |
|||
size_t size; |
|||
status = ::export_secret_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size); |
|||
_throw_status(status); |
|||
return key_data; |
|||
} |
|||
|
|||
void set_own_key(Identity &ident, const string &fpr) { |
|||
if (ident.address() == "") { |
|||
throw invalid_argument("address needed"); |
|||
} |
|||
if (ident.username() == "") { |
|||
throw invalid_argument("username needed"); |
|||
} |
|||
if (ident.user_id() == "") { |
|||
throw invalid_argument("user_id needed"); |
|||
} |
|||
if (fpr == "") { |
|||
throw invalid_argument("fpr needed"); |
|||
} |
|||
const char *fpr_c = fpr.c_str(); |
|||
::PEP_STATUS status = ::set_own_key(Adapter::session(), ident, fpr_c); |
|||
_throw_status(status); |
|||
} |
|||
} // namespace PythonAdapter
|
|||
} // namespace pEp
|
|||
|
|||
|
|||
|
@ -1,385 +1,387 @@ |
|||
// This file is under GNU Affero General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
// System
|
|||
#include <cstdlib> |
|||
#include <cstring> |
|||
#include <stdexcept> |
|||
#include <sstream> |
|||
#include <vector> |
|||
#include <Python.h> |
|||
|
|||
// Engine
|
|||
#include <pEp/mime.h> |
|||
#include <pEp/keymanagement.h> |
|||
#include <pEp/message_api.h> |
|||
|
|||
// local
|
|||
#include "message.hh" |
|||
#include "message_api.hh" |
|||
|
|||
namespace pEp { |
|||
namespace PythonAdapter { |
|||
using namespace std; |
|||
using namespace boost::python; |
|||
|
|||
Message::Blob::Blob(bloblist_t *bl, bool chained) : |
|||
_bl(bl), part_of_chain(chained) { |
|||
if (!_bl) |
|||
throw bad_alloc(); |
|||
namespace PythonAdapter { |
|||
|
|||
|
|||
Message::Blob::Blob(::bloblist_t *bl, bool chained) |
|||
: _bl(bl), |
|||
part_of_chain(chained) { |
|||
if (!_bl) { |
|||
throw bad_alloc(); |
|||
} |
|||
} |
|||
|
|||
Message::Blob::Blob(bp::object data, string mime_type, string filename) |
|||
: _bl(::new_bloblist(nullptr, 0, nullptr, nullptr)), |
|||
part_of_chain(false) { |
|||
if (!_bl) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
Py_buffer src; |
|||
int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO); |
|||
if (result) { |
|||
throw invalid_argument("need a contiguous buffer to read"); |
|||
} |
|||
|
|||
char *mem = (char *)malloc(src.len); |
|||
if (!mem) { |
|||
PyBuffer_Release(&src); |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
memcpy(mem, src.buf, src.len); |
|||
free(_bl->value); |
|||
_bl->size = src.len; |
|||
_bl->value = mem; |
|||
|
|||
PyBuffer_Release(&src); |
|||
|
|||
this->mime_type(mime_type); |
|||
this->filename(filename); |
|||
} |
|||
|
|||
Message::Blob::Blob(const Message::Blob &second) |
|||
: _bl(second._bl), |
|||
part_of_chain(true) { |
|||
} |
|||
|
|||
Message::Blob::~Blob() { |
|||
if (!part_of_chain) { |
|||
free(_bl->value); |
|||
free(_bl); |
|||
} |
|||
} |
|||
|
|||
string Message::Blob::_repr() { |
|||
stringstream build; |
|||
build << "Blob("; |
|||
if (!_bl) { |
|||
build << "b'', '', ''"; |
|||
} else { |
|||
build << "bytes(" << _bl->size << "), "; |
|||
string mime_type; |
|||
if (_bl->mime_type) { |
|||
mime_type = string(_bl->mime_type); |
|||
} |
|||
|
|||
Message::Blob::Blob(object data, string mime_type, string filename) : |
|||
_bl(new_bloblist(NULL, 0, NULL, NULL)), part_of_chain(false) { |
|||
if (!_bl) |
|||
throw bad_alloc(); |
|||
|
|||
Py_buffer src; |
|||
int result = PyObject_GetBuffer(data.ptr(), &src, PyBUF_CONTIG_RO); |
|||
if (result) |
|||
throw invalid_argument("need a contiguous buffer to read"); |
|||
|
|||
char *mem = (char *) malloc(src.len); |
|||
if (!mem) { |
|||
PyBuffer_Release(&src); |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
memcpy(mem, src.buf, src.len); |
|||
free(_bl->value); |
|||
_bl->size = src.len; |
|||
_bl->value = mem; |
|||
|
|||
PyBuffer_Release(&src); |
|||
|
|||
this->mime_type(mime_type); |
|||
this->filename(filename); |
|||
string filename; |
|||
if (_bl->filename) { |
|||
filename = string(_bl->filename); |
|||
} |
|||
|
|||
Message::Blob::Blob(const Message::Blob &second) : |
|||
_bl(second._bl), part_of_chain(true) { |
|||
|
|||
build << repr(mime_type) << ", "; |
|||
build << repr(filename); |
|||
} |
|||
build << ")"; |
|||
return build.str(); |
|||
} |
|||
|
|||
int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { |
|||
::bloblist_t *bl = nullptr; |
|||
|
|||
try { |
|||
Message::Blob &blob = bp::extract<Message::Blob &>(self); |
|||
bl = blob._bl; |
|||
} catch (exception &e) { |
|||
PyErr_SetString(PyExc_RuntimeError, "extract not possible"); |
|||
view->obj = nullptr; |
|||
return -1; |
|||
} |
|||
|
|||
if (!(bl && bl->value)) { |
|||
PyErr_SetString(PyExc_RuntimeError, "no data available"); |
|||
view->obj = nullptr; |
|||
return -1; |
|||
} |
|||
|
|||
return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags); |
|||
} |
|||
|
|||
string Message::Blob::decode(string encoding) { |
|||
if (encoding == "") { |
|||
string _mime_type = _bl->mime_type ? _bl->mime_type : ""; |
|||
encoding = "ascii"; |
|||
|
|||
if (_mime_type == "application/pEp.sync") { |
|||
encoding = "pep.sync"; |
|||
} |
|||
|
|||
Message::Blob::~Blob() { |
|||
if (!part_of_chain) { |
|||
free(_bl->value); |
|||
free(_bl); |
|||
} |
|||
if (_mime_type == "application/pEp.keyreset") { |
|||
encoding = "pep.distribution"; |
|||
} |
|||
|
|||
string Message::Blob::_repr() { |
|||
stringstream build; |
|||
build << "Blob("; |
|||
if (!_bl) { |
|||
build << "b'', '', ''"; |
|||
} else { |
|||
build << "bytes(" << _bl->size << "), "; |
|||
string mime_type; |
|||
if (_bl->mime_type) |
|||
mime_type = string(_bl->mime_type); |
|||
string filename; |
|||
if (_bl->filename) |
|||
filename = string(_bl->filename); |
|||
build << repr(mime_type) << ", "; |
|||
build << repr(filename); |
|||
} |
|||
build << ")"; |
|||
return build.str(); |
|||
} |
|||
} |
|||
bp::object codecs = bp::import("codecs"); |
|||
bp::object _decode = codecs.attr("decode"); |
|||
return bp::call<string>(_decode.ptr(), this, encoding); |
|||
} |
|||
|
|||
int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { |
|||
bloblist_t *bl = NULL; |
|||
PyBufferProcs Message::Blob::bp = {getbuffer, nullptr}; |
|||
|
|||
try { |
|||
Message::Blob &blob = extract<Message::Blob &>(self); |
|||
bl = blob._bl; |
|||
} |
|||
catch (exception &e) { |
|||
PyErr_SetString(PyExc_RuntimeError, "extract not possible"); |
|||
view->obj = NULL; |
|||
return -1; |
|||
} |
|||
|
|||
if (!(bl && bl->value)) { |
|||
PyErr_SetString(PyExc_RuntimeError, "no data available"); |
|||
view->obj = NULL; |
|||
return -1; |
|||
} |
|||
Message::Message(int dir, Identity *from) |
|||
: _msg(new_message((::PEP_msg_direction)dir), &::free_message) { |
|||
if (!_msg) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags); |
|||
if (from) { |
|||
_msg->from = ::identity_dup(*from); |
|||
if (!_msg->from) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
string Message::Blob::decode(string encoding) { |
|||
if (encoding == "") { |
|||
string _mime_type = _bl->mime_type ? _bl->mime_type : ""; |
|||
encoding = "ascii"; |
|||
|
|||
if (_mime_type == "application/pEp.sync") |
|||
encoding = "pep.sync"; |
|||
|
|||
if (_mime_type == "application/pEp.keyreset") |
|||
encoding = "pep.distribution"; |
|||
|
|||
} |
|||
object codecs = import("codecs"); |
|||
object _decode = codecs.attr("decode"); |
|||
return call<string>(_decode.ptr(), this, encoding); |
|||
_msg->dir = (::PEP_msg_direction)dir; |
|||
} |
|||
} |
|||
|
|||
Message::Message(string mimetext) |
|||
: _msg(nullptr, &::free_message) { |
|||
::message *_cpy; |
|||
::PEP_STATUS status = ::mime_decode_message(mimetext.c_str(), mimetext.size(), &_cpy, nullptr); |
|||
switch (status) { |
|||
case ::PEP_STATUS_OK: |
|||
if (_cpy) { |
|||
_cpy->dir = ::PEP_dir_outgoing; |
|||
} else { |
|||
_cpy = new_message(::PEP_dir_outgoing); |
|||
} |
|||
|
|||
PyBufferProcs Message::Blob::bp = {getbuffer, NULL}; |
|||
|
|||
Message::Message(int dir, Identity *from) |
|||
: _msg(new_message((PEP_msg_direction) dir), &free_message) { |
|||
if (!_msg) |
|||
throw bad_alloc(); |
|||
|
|||
if (from) { |
|||
_msg->from = ::identity_dup(*from); |
|||
if (!_msg->from) |
|||
throw bad_alloc(); |
|||
_msg->dir = (PEP_msg_direction) dir; |
|||
} |
|||
if (!_cpy) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
Message::Message(string mimetext) |
|||
: _msg(NULL, &free_message) { |
|||
message *_cpy; |
|||
PEP_STATUS status = mime_decode_message(mimetext.c_str(), |
|||
mimetext.size(), &_cpy, NULL); |
|||
switch (status) { |
|||
case PEP_STATUS_OK: |
|||
if (_cpy) |
|||
_cpy->dir = PEP_dir_outgoing; |
|||
else |
|||
_cpy = new_message(PEP_dir_outgoing); |
|||
|
|||
if (!_cpy) |
|||
throw bad_alloc(); |
|||
|
|||
_msg = shared_ptr<message>(_cpy); |
|||
break; |
|||
|
|||
case PEP_BUFFER_TOO_SMALL: |
|||
throw runtime_error("mime_decode_message: buffer too small"); |
|||
|
|||
case PEP_CANNOT_CREATE_TEMP_FILE: |
|||
throw runtime_error("mime_decode_message: cannot create temp file"); |
|||
|
|||
case PEP_OUT_OF_MEMORY: |
|||
throw bad_alloc(); |
|||
|
|||
default: |
|||
stringstream build; |
|||
build << "mime_decode_message: unknown error (" << (int) status << ")"; |
|||
throw runtime_error(build.str()); |
|||
_msg = shared_ptr<::message>(_cpy); |
|||
break; |
|||
|
|||
case ::PEP_BUFFER_TOO_SMALL: |
|||
throw runtime_error("mime_decode_message: buffer too small"); |
|||
|
|||
case ::PEP_CANNOT_CREATE_TEMP_FILE: |
|||
throw runtime_error("mime_decode_message: cannot create temp file"); |
|||
|
|||
case ::PEP_OUT_OF_MEMORY: |
|||
throw bad_alloc(); |
|||
|
|||
default: |
|||
stringstream build; |
|||
build << "mime_decode_message: unknown error (" << (int)status << ")"; |
|||
throw runtime_error(build.str()); |
|||
} |
|||
} |
|||
|
|||
Message::Message(const Message &second) |
|||
: _msg(second._msg) { |
|||
if (!_msg.get()) { |
|||
throw bad_alloc(); |
|||
} |
|||
} |
|||
|
|||
Message::Message(::message *msg) |
|||
: _msg(::message_dup(msg), &::free_message) {} |
|||
|
|||
Message::~Message() {} |
|||
|
|||
Message::operator ::message *() { |
|||
return _msg.get(); |
|||
} |
|||
|
|||
Message::operator const ::message *() const { |
|||
return _msg.get(); |
|||
} |
|||
|
|||
string Message::_str() { |
|||
if (!(_msg->from && _msg->from->address && _msg->from->address[0])) { |
|||
throw out_of_range(".from_.address missing"); |
|||
} |
|||
|
|||
char *mimetext; |
|||
string result; |
|||
|
|||
::PEP_STATUS status = ::mime_encode_message(*this, false, &mimetext, false); |
|||
switch (status) { |
|||
case ::PEP_STATUS_OK: |
|||
result = mimetext; |
|||
free(mimetext); |
|||
break; |
|||
|
|||
case ::PEP_BUFFER_TOO_SMALL: |
|||
throw runtime_error("mime_encode_message: buffer too small"); |
|||
|
|||
case ::PEP_CANNOT_CREATE_TEMP_FILE: |
|||
throw runtime_error("mime_encode_message: cannot create temp file"); |
|||
|
|||
case ::PEP_OUT_OF_MEMORY: |
|||
throw bad_alloc(); |
|||
|
|||
default: |
|||
stringstream build; |
|||
build << "mime_encode_message: unknown error (" << (int)status << ")"; |
|||
throw runtime_error(build.str()); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
string Message::_repr() { |
|||
stringstream build; |
|||
build << "Message(" << repr(_str()) << ")"; |
|||
return build.str(); |
|||
} |
|||
|
|||
bp::tuple Message::attachments() { |
|||
bp::list l; |
|||
|
|||
for (::bloblist_t *bl = _msg->attachments; bl && bl->value; bl = bl->next) { |
|||
l.append(Blob(bl, true)); |
|||
} |
|||
|
|||
return bp::tuple(l); |
|||
} |
|||
|
|||
void Message::attachments(bp::list value) { |
|||
::bloblist_t *bl = ::new_bloblist(nullptr, 0, nullptr, nullptr); |
|||
if (!bl) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
::bloblist_t *_l = bl; |
|||
for (int i = 0; i < len(value); i++) { |
|||
Message::Blob &blob = bp::extract<Message::Blob &>(value[i]); |
|||
_l = bloblist_add(_l, blob._bl->value, blob._bl->size, blob._bl->mime_type, blob._bl->filename); |
|||
if (!_l) { |
|||
for (_l = bl; _l && _l->value;) { |
|||
free(_l->mime_type); |
|||
free(_l->filename); |
|||
::bloblist_t *_ll = _l; |
|||
_l = _l->next; |
|||
free(_ll); |
|||
} |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
Message::Message(const Message &second) |
|||
: _msg(second._msg) { |
|||
if (!_msg.get()) |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
Message::Message(message *msg) |
|||
: _msg(::message_dup(msg), &free_message) { |
|||
|
|||
} |
|||
|
|||
Message::~Message() { |
|||
|
|||
} |
|||
|
|||
Message::operator message *() { |
|||
return _msg.get(); |
|||
} |
|||
|
|||
Message::operator const message *() const { |
|||
return _msg.get(); |
|||
} |
|||
|
|||
string Message::_str() { |
|||
if (!(_msg->from && _msg->from->address && _msg->from->address[0])) |
|||
throw out_of_range(".from_.address missing"); |
|||
|
|||
char *mimetext; |
|||
string result; |
|||
|
|||
PEP_STATUS status = mime_encode_message(*this, false, &mimetext, false); |
|||
switch (status) { |
|||
case PEP_STATUS_OK: |
|||
result = mimetext; |
|||
free(mimetext); |
|||
break; |
|||
|
|||
case PEP_BUFFER_TOO_SMALL: |
|||
throw runtime_error("mime_encode_message: buffer too small"); |
|||
|
|||
case PEP_CANNOT_CREATE_TEMP_FILE: |
|||
throw runtime_error("mime_encode_message: cannot create temp file"); |
|||
|
|||
case PEP_OUT_OF_MEMORY: |
|||
throw bad_alloc(); |
|||
|
|||
default: |
|||
stringstream build; |
|||
build << "mime_encode_message: unknown error (" << (int) status << ")"; |
|||
throw runtime_error(build.str()); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
string Message::_repr() { |
|||
stringstream build; |
|||
build << "Message(" << repr(_str()) << ")"; |
|||
return build.str(); |
|||
} |
|||
|
|||
boost::python::tuple Message::attachments() { |
|||
boost::python::list l; |
|||
|
|||
for (bloblist_t *bl = _msg->attachments; bl && bl->value; bl = |
|||
bl->next) { |
|||
l.append(Blob(bl, true)); |
|||
} |
|||
|
|||
return boost::python::tuple(l); |
|||
} |
|||
|
|||
void Message::attachments(boost::python::list value) { |
|||
bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL); |
|||
if (!bl) |
|||
throw bad_alloc(); |
|||
|
|||
bloblist_t *_l = bl; |
|||
for (int i = 0; i < len(value); i++) { |
|||
Message::Blob &blob = extract<Message::Blob &>(value[i]); |
|||
_l = bloblist_add(_l, blob._bl->value, blob._bl->size, |
|||
blob._bl->mime_type, blob._bl->filename); |
|||
if (!_l) { |
|||
for (_l = bl; _l && _l->value;) { |
|||
free(_l->mime_type); |
|||
free(_l->filename); |
|||
bloblist_t *_ll = _l; |
|||
_l = _l->next; |
|||
free(_ll); |
|||
} |
|||
throw bad_alloc(); |
|||
} |
|||
} |
|||
|
|||
for (int i = 0; i < len(value); i++) { |
|||
Message::Blob &blob = extract<Message::Blob &>(value[i]); |
|||
blob._bl->value = NULL; |
|||
blob._bl->size = 0; |
|||
free(blob._bl->mime_type); |
|||
blob._bl->mime_type = NULL; |
|||
free(blob._bl->filename); |
|||
blob._bl->filename = NULL; |
|||
} |
|||
|
|||
free_bloblist(_msg->attachments); |
|||
_msg->attachments = bl; |
|||
} |
|||
|
|||
Message Message::encrypt() { |
|||
boost::python::list extra; |
|||
return encrypt_message(*this, extra, PEP_enc_PGP_MIME, 0); |
|||
} |
|||
|
|||
Message Message::_encrypt(boost::python::list extra, int enc_format, int flags) { |
|||
if (!enc_format) |
|||
enc_format = PEP_enc_PGP_MIME; |
|||
return encrypt_message(*this, extra, enc_format, flags); |
|||
} |
|||
|
|||
boost::python::tuple Message::decrypt(int flags) { |
|||
return pEp::PythonAdapter::decrypt_message(*this, flags); |
|||
} |
|||
|
|||
PEP_rating Message::outgoing_rating() { |
|||
if (_msg->dir != PEP_dir_outgoing) |
|||
throw invalid_argument("Message.dir must be outgoing"); |
|||
|
|||
if (from().address() == "") |
|||
throw invalid_argument("from.address needed"); |
|||
if (from().username() == "") |
|||
throw invalid_argument("from.username needed"); |
|||
|
|||
if (len(to()) + len(cc()) == 0) |
|||
throw invalid_argument("either to or cc needed"); |
|||
|
|||
PEP_STATUS status = myself(Adapter::session(), _msg->from); |
|||
_throw_status(status); |
|||
|
|||
PEP_rating rating = PEP_rating_undefined; |
|||
status = outgoing_message_rating(Adapter::session(), *this, &rating); |
|||
_throw_status(status); |
|||
|
|||
return rating; |
|||
} |
|||
|
|||
PEP_color Message::outgoing_color() { |
|||
return _color(outgoing_rating()); |
|||
} |
|||
|
|||
Message Message::copy() { |
|||
message *dup = message_dup(*this); |
|||
if (!dup) |
|||
throw bad_alloc(); |
|||
return Message(dup); |
|||
} |
|||
|
|||
Message Message::deepcopy(dict &) { |
|||
return copy(); |
|||
} |
|||
|
|||
Message outgoing_message(Identity me) { |
|||
if (me.address().empty() || me.user_id().empty()) |
|||
throw runtime_error("at least address and user_id of own user needed"); |
|||
|
|||
::myself(Adapter::session(), me); |
|||
auto m = Message(PEP_dir_outgoing, &me); |
|||
return m; |
|||
} |
|||
|
|||
static object update(Identity ident) { |
|||
if (ident.address().empty()) |
|||
throw runtime_error("at least address needed"); |
|||
update_identity(Adapter::session(), ident); |
|||
return object(ident); |
|||
} |
|||
|
|||
static boost::python::list update(boost::python::list il) { |
|||
for (int i = 0; i < len(il); i++) { |
|||
update(extract<Identity>(il[i])); |
|||
} |
|||
|
|||
return il; |
|||
} |
|||
|
|||
Message incoming_message(string mime_text) { |
|||
auto m = Message(mime_text); |
|||
m.dir(PEP_dir_incoming); |
|||
|
|||
try { |
|||
m.from(update(m.from())); |
|||
} |
|||
catch (out_of_range &) {} |
|||
|
|||
try { |
|||
m.recv_by(update(m.recv_by())); |
|||
} |
|||
catch (out_of_range &) {} |
|||
|
|||
m.to(update(m.to())); |
|||
m.cc(update(m.cc())); |
|||
m.reply_to(update(m.reply_to())); |
|||
|
|||
return m; |
|||
} |
|||
|
|||
} // namespace PythonAdapter
|
|||
} |
|||
|
|||
for (int i = 0; i < len(value); i++) { |
|||
Message::Blob &blob = bp::extract<Message::Blob &>(value[i]); |
|||
blob._bl->value = nullptr; |
|||
blob._bl->size = 0; |
|||
free(blob._bl->mime_type); |
|||
blob._bl->mime_type = nullptr; |
|||
free(blob._bl->filename); |
|||
blob._bl->filename = nullptr; |
|||
} |
|||
|
|||
free_bloblist(_msg->attachments); |
|||
_msg->attachments = bl; |
|||
} |
|||
|
|||
Message Message::encrypt() { |
|||
bp::list extra; |
|||
return encrypt_message(*this, extra, ::PEP_enc_PGP_MIME, 0); |
|||
} |
|||
|
|||
Message Message::_encrypt(bp::list extra, int enc_format, int flags) { |
|||
if (!enc_format) { |
|||
enc_format = ::PEP_enc_PGP_MIME; |
|||
} |
|||
return encrypt_message(*this, extra, enc_format, flags); |
|||
} |
|||
|
|||
bp::tuple Message::decrypt(int flags) { |
|||
return pEp::PythonAdapter::decrypt_message(*this, flags); |
|||
} |
|||
|
|||
::PEP_rating Message::outgoing_rating() { |
|||
if (_msg->dir != ::PEP_dir_outgoing) { |
|||
throw invalid_argument("Message.dir must be outgoing"); |
|||
} |
|||
|
|||
if (from().address() == "") { |
|||
throw invalid_argument("from.address needed"); |
|||
} |
|||
if (from().username() == "") { |
|||
throw invalid_argument("from.username needed"); |
|||
} |
|||
|
|||
if (len(to()) + len(cc()) == 0) { |
|||
throw invalid_argument("either to or cc needed"); |
|||
} |
|||
|
|||
::PEP_STATUS status = myself(Adapter::session(), _msg->from); |
|||
_throw_status(status); |
|||
|
|||
::PEP_rating rating = ::PEP_rating_undefined; |
|||
status = outgoing_message_rating(Adapter::session(), *this, &rating); |
|||
_throw_status(status); |
|||
|
|||
return rating; |
|||
} |
|||
|
|||
::PEP_color Message::outgoing_color() { |
|||
return _color(outgoing_rating()); |
|||
} |
|||
|
|||
Message Message::copy() { |
|||
::message *dup = ::message_dup(*this); |
|||
if (!dup) { |
|||
throw bad_alloc(); |
|||
} |
|||
return Message(dup); |
|||
} |
|||
|
|||
Message Message::deepcopy(bp::dict &) { |
|||
return copy(); |
|||
} |
|||
|
|||
Message outgoing_message(Identity me) { |
|||
if (me.address().empty() || me.user_id().empty()) { |
|||
throw runtime_error("at least address and user_id of own user needed"); |
|||
} |
|||
|
|||
::myself(Adapter::session(), me); |
|||
auto m = Message(::PEP_dir_outgoing, &me); |
|||
return m; |
|||
} |
|||
|
|||
static bp::object update(Identity ident) { |
|||
if (ident.address().empty()) { |
|||
throw runtime_error("at least address needed"); |
|||
} |
|||
::update_identity(Adapter::session(), ident); |
|||
return bp::object(ident); |
|||
} |
|||
|
|||
static bp::list update(bp::list il) { |
|||
for (int i = 0; i < len(il); i++) { |
|||
update(bp::extract<Identity>(il[i])); |
|||
} |
|||
|
|||
return il; |
|||
} |
|||
|
|||
Message incoming_message(string mime_text) { |
|||
auto m = Message(mime_text); |
|||
m.dir(::PEP_dir_incoming); |
|||
|
|||
try { |
|||
m.from(update(m.from())); |
|||
} catch (out_of_range &) { |
|||
} |
|||
|
|||
try { |
|||
m.recv_by(update(m.recv_by())); |
|||
} catch (out_of_range &) { |
|||
} |
|||
|
|||
m.to(update(m.to())); |
|||
m.cc(update(m.cc())); |
|||
m.reply_to(update(m.reply_to())); |
|||
|
|||
return m; |
|||
} |
|||
|
|||
} // namespace PythonAdapter
|
|||
} // namespace pEp
|
|||
|
@ -1,161 +1,158 @@ |
|||
// This file is under GNU Affero General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
// Engine
|
|||
#include <pEp/pEpEngine.h> |
|||
#include <pEp/message_api.h> |
|||
#include <pEp/sync_api.h> |
|||
#include <pEp/sync_codec.h> |
|||
#include <pEp/distribution_codec.h> |
|||
|
|||
// local
|
|||
#include "message_api.hh" |
|||
#include "basic_api.hh" |
|||
|
|||
namespace pEp { |
|||
namespace PythonAdapter { |
|||
using namespace std; |
|||
using namespace boost::python; |
|||
|
|||
Message encrypt_message(Message src, boost::python::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"); |
|||
|
|||
if (_from.user_id() == "") |
|||
src.from().user_id(_from.address()); |
|||
|
|||
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; |
|||
|
|||
message *_src = src; |
|||
PEP_STATUS status = encrypt_message(Adapter::session(), _src, _extra, &_dst, |
|||
_enc_format, _flags); |
|||
free_stringlist(_extra); |
|||
_throw_status(status); |
|||
|
|||
if (!_dst || _dst == _src) |
|||
return Message(_src); |
|||
|
|||
return Message(_dst); |
|||
} |
|||
|
|||
boost::python::tuple decrypt_message(Message src, int flags) { |
|||
message *_dst = NULL; |
|||
stringlist_t *_keylist = NULL; |
|||
PEP_rating _rating = PEP_rating_undefined; |
|||
PEP_decrypt_flags_t _flags = (PEP_decrypt_flags_t) flags; |
|||
message *_src = src; |
|||
|
|||
PEP_STATUS status = ::decrypt_message(Adapter::session(), _src, &_dst, &_keylist, |
|||
&_rating, &_flags); |
|||
_throw_status(status); |
|||
|
|||
boost::python::list keylist; |
|||
if (_keylist) { |
|||
keylist = from_stringlist(_keylist); |
|||
free_stringlist(_keylist); |
|||
} |
|||
|
|||
Message dst = _dst ? Message(_dst) : Message(src); |
|||
return boost::python::make_tuple(dst, keylist, _rating, _flags); |
|||
} |
|||
|
|||
PEP_color _color(int rating) { |
|||
return ::color_from_rating((PEP_rating) rating); |
|||
} |
|||
|
|||
boost::python::tuple sync_decode(object buffer) { |
|||
Py_buffer src; |
|||
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
|||
if (result) |
|||
throw invalid_argument("need a contiguous buffer to read"); |
|||
|
|||
char *dst = NULL; |
|||
PEP_STATUS status = PER_to_XER_Sync_msg((char *) src.buf, src.len, &dst); |
|||
PyBuffer_Release(&src); |
|||
_throw_status(status); |
|||
|
|||
string _dst(dst); |
|||
free(dst); |
|||
return boost::python::make_tuple(_dst, 0); |
|||
} |
|||
|
|||
static boost::python::tuple sync_encode(string text) { |
|||
char *data = NULL; |
|||
size_t size = 0; |
|||
PEP_STATUS status = XER_to_PER_Sync_msg(text.c_str(), &data, &size); |
|||
_throw_status(status); |
|||
|
|||
PyObject *ba = PyBytes_FromStringAndSize(data, size); |
|||
free(data); |
|||
if (!ba) |
|||
throw bad_alloc(); |
|||
|
|||
return boost::python::make_tuple(object(handle<>(ba)), 0); |
|||
} |
|||
|
|||
boost::python::tuple Distribution_decode(object buffer) { |
|||
Py_buffer src; |
|||
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
|||
if (result) |
|||
throw invalid_argument("need a contiguous buffer to read"); |
|||
|
|||
char *dst = NULL; |
|||
PEP_STATUS status = PER_to_XER_Distribution_msg((char *) src.buf, src.len, &dst); |
|||
PyBuffer_Release(&src); |
|||
_throw_status(status); |
|||
|
|||
string _dst(dst); |
|||
free(dst); |
|||
return boost::python::make_tuple(_dst, 0); |
|||
} |
|||
|
|||
static boost::python::tuple Distribution_encode(string text) { |
|||
char *data = NULL; |
|||
size_t size = 0; |
|||
PEP_STATUS status = XER_to_PER_Distribution_msg(text.c_str(), &data, &size); |
|||
_throw_status(status); |
|||
|
|||
PyObject *ba = PyBytes_FromStringAndSize(data, size); |
|||
free(data); |
|||
if (!ba) |
|||
throw bad_alloc(); |
|||
|
|||
return boost::python::make_tuple(object(handle<>(ba)), 0); |
|||
} |
|||
|
|||
object sync_search(string name) { |
|||
if (name != "pep.sync") { |
|||
return object(); |
|||
} else { |
|||
object codecs = import("codecs"); |
|||
object CodecInfo = codecs.attr("CodecInfo"); |
|||
|
|||
object _sync_decode = make_function(sync_decode); |
|||
object _sync_encode = make_function(sync_encode); |
|||
|
|||
return call<object>(CodecInfo.ptr(), _sync_encode, _sync_decode); |
|||
} |
|||
} |
|||
|
|||
object distribution_search(string name) { |
|||
if (name != "pep.distribution") { |
|||
return object(); |
|||
} else { |
|||
object codecs = import("codecs"); |
|||
object CodecInfo = codecs.attr("CodecInfo"); |
|||
|
|||
object _distribution_decode = make_function(Distribution_decode); |
|||
object _distribution_encode = make_function(Distribution_encode); |
|||
|
|||
return call<object>(CodecInfo.ptr(), _distribution_encode, _distribution_decode); |
|||
} |
|||
} |
|||
|
|||
} // namespace PythonAdapter
|
|||
} // namespace pEp {
|
|||
namespace PythonAdapter { |
|||
|
|||
|
|||
Message encrypt_message(Message src, bp::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"); |
|||
} |
|||
|
|||
if (_from.user_id() == "") { |
|||
src.from().user_id(_from.address()); |
|||
} |
|||
|
|||
::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 = nullptr; |
|||
|
|||
::message *_src = src; |
|||
::PEP_STATUS status = ::encrypt_message(Adapter::session(), _src, _extra, &_dst, _enc_format, _flags); |
|||
::free_stringlist(_extra); |
|||
_throw_status(status); |
|||
|
|||
if (!_dst || _dst == _src) { |
|||
return Message(_src); |
|||
} |
|||
|
|||
return Message(_dst); |
|||
} |
|||
|
|||
bp::tuple decrypt_message(Message src, int flags) { |
|||
::message *_dst = nullptr; |
|||
::stringlist_t *_keylist = nullptr; |
|||
::PEP_rating _rating = ::PEP_rating_undefined; |
|||
::PEP_decrypt_flags_t _flags = (::PEP_decrypt_flags_t)flags; |
|||
::message *_src = src; |
|||
|
|||
::PEP_STATUS status = ::decrypt_message(Adapter::session(), _src, &_dst, &_keylist, &_rating, &_flags); |
|||
_throw_status(status); |
|||
|
|||
bp::list keylist; |
|||
if (_keylist) { |
|||
keylist = from_stringlist(_keylist); |
|||
::free_stringlist(_keylist); |
|||
} |
|||
|
|||
Message dst = _dst ? Message(_dst) : Message(src); |
|||
return bp::make_tuple(dst, keylist, _rating, _flags); |
|||
} |
|||
|
|||
::PEP_color _color(int rating) { |
|||
return ::color_from_rating((::PEP_rating)rating); |
|||
} |
|||
|
|||
bp::tuple sync_decode(bp::object buffer) { |
|||
Py_buffer src; |
|||
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
|||
if (result) { |
|||
throw invalid_argument("need a contiguous buffer to read"); |
|||
} |
|||
|
|||
char *dst = nullptr; |
|||
::PEP_STATUS status = ::PER_to_XER_Sync_msg((char *)src.buf, src.len, &dst); |
|||
PyBuffer_Release(&src); |
|||
_throw_status(status); |
|||
|
|||
string _dst(dst); |
|||
free(dst); |
|||
return bp::make_tuple(_dst, 0); |
|||
} |
|||
|
|||
static bp::tuple sync_encode(string text) { |
|||
char *data = nullptr; |
|||
size_t size = 0; |
|||
::PEP_STATUS status = ::XER_to_PER_Sync_msg(text.c_str(), &data, &size); |
|||
_throw_status(status); |
|||
|
|||
PyObject * ba = PyBytes_FromStringAndSize(data, size); |
|||
free(data); |
|||
if (!ba) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
return bp::make_tuple(bp::object(bp::handle<>(ba)), 0); |
|||
} |
|||
|
|||
bp::tuple Distribution_decode(bp::object buffer) { |
|||
Py_buffer src; |
|||
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
|||
if (result) { |
|||
throw invalid_argument("need a contiguous buffer to read"); |
|||
} |
|||
|
|||
char *dst = nullptr; |
|||
::PEP_STATUS status = ::PER_to_XER_Distribution_msg((char *)src.buf, src.len, &dst); |
|||
PyBuffer_Release(&src); |
|||
_throw_status(status); |
|||
|
|||
string _dst(dst); |
|||
free(dst); |
|||
return bp::make_tuple(_dst, 0); |
|||
} |
|||
|
|||
static bp::tuple Distribution_encode(string text) { |
|||
char *data = nullptr; |
|||
size_t size = 0; |
|||
::PEP_STATUS status = ::XER_to_PER_Distribution_msg(text.c_str(), &data, &size); |
|||
_throw_status(status); |
|||
|
|||
PyObject * ba = PyBytes_FromStringAndSize(data, size); |
|||
free(data); |
|||
if (!ba) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
return bp::make_tuple(bp::object(bp::handle<>(ba)), 0); |
|||
} |
|||
|
|||
bp::object sync_search(string name) { |
|||
if (name != "pep.sync") { |
|||
return bp::object(); |
|||
} else { |
|||
bp::object codecs = bp::import("codecs"); |
|||
bp::object CodecInfo = codecs.attr("CodecInfo"); |
|||
|
|||
bp::object _sync_decode = make_function(sync_decode); |
|||
bp::object _sync_encode = make_function(sync_encode); |
|||
|
|||
return bp::call<bp::object>(CodecInfo.ptr(), _sync_encode, _sync_decode); |
|||
} |
|||
} |
|||
|
|||
bp::object distribution_search(string name) { |
|||
if (name != "pep.distribution") { |
|||
return bp::object(); |
|||
} else { |
|||
bp::object codecs = bp::import("codecs"); |
|||
bp::object CodecInfo = codecs.attr("CodecInfo"); |
|||
|
|||
bp::object _distribution_decode = make_function(Distribution_decode); |
|||
bp::object _distribution_encode = make_function(Distribution_encode); |
|||
|
|||
return bp::call<bp::object>(CodecInfo.ptr(), _distribution_encode, _distribution_decode); |
|||
} |
|||
} |
|||
|
|||
} // namespace PythonAdapter
|
|||
} // namespace pEp
|
|||
|
File diff suppressed because it is too large
@ -1,172 +1,172 @@ |
|||
// This file is under GNU Affero General Public License 3.0
|
|||
// see LICENSE.txt
|
|||
|
|||
// System
|
|||
#include <cstdlib> |
|||
#include <boost/python.hpp> |
|||
#include <boost/locale.hpp> |
|||
|
|||
// local
|
|||
#include "str_attr.hh" |
|||
|
|||
namespace pEp { |
|||
namespace PythonAdapter { |
|||
using namespace std; |
|||
using namespace boost::python; |
|||
using namespace boost::locale; |
|||
|
|||
object repr(object s) { |
|||
return s.attr("__repr__")(); |
|||
namespace PythonAdapter { |
|||
|
|||
|
|||
bp::object repr(bp::object s) { |
|||
return s.attr("__repr__")(); |
|||
} |
|||
|
|||
string repr(const string &s) { |
|||
bp::str _s = s.c_str(); |
|||
bp::object _r = _s.attr("__repr__")(); |
|||
string r = bp::extract<string>(_r); |
|||
return r; |
|||
} |
|||
|
|||
string str_attr(char *&str) { |
|||
if (!str) { |
|||
return string(""); |
|||
} |
|||
return string(str); |
|||
} |
|||
|
|||
void str_attr(char *&str, const string &value) { |
|||
string normalized = normalize(value, bl::norm_nfc); |
|||
free(str); |
|||
str = strdup(normalized.c_str()); |
|||
if (!str) { |
|||
throw bad_alloc(); |
|||
} |
|||
} |
|||
|
|||
time_t timestamp_attr(::timestamp *&ts) { |
|||
if (!ts) { |
|||
return 0; |
|||
} |
|||
|
|||
return timegm(ts); |
|||
} |
|||
|
|||
void timestamp_attr(::timestamp *&ts, time_t value) { |
|||
free_timestamp(ts); |
|||
ts = ::new_timestamp(value); |
|||
} |
|||
|
|||
bp::list strlist_attr(::stringlist_t *&sl) { |
|||
bp::list result; |
|||
|
|||
for (const ::stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
|||
string s(_sl->value); |
|||
result.append(bp::object(s)); |
|||
} |
|||
|
|||
return result; |
|||
} |
|||
|
|||
void strlist_attr(::stringlist_t *&sl, bp::list value) { |
|||
::stringlist_t *_sl = ::new_stringlist(NULL); |
|||
if (!_sl) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
::stringlist_t *_s = _sl; |
|||
for (int i = 0; i < len(value); i++) { |
|||
bp::extract<string> extract_string(value[i]); |
|||
if (!extract_string.check()) { |
|||
free_stringlist(_sl); |
|||
} |
|||
|
|||
string repr(string s) { |
|||
str _s = s.c_str(); |
|||
object _r = _s.attr("__repr__")(); |
|||
string r = extract<string>(_r); |
|||
return r; |
|||
string s = extract_string(); |
|||
s = normalize(s, bl::norm_nfc); |
|||
_s = stringlist_add(_s, s.c_str()); |
|||
if (!_s) { |
|||
free_stringlist(_sl); |
|||
throw bad_alloc(); |
|||
} |
|||
} |
|||
|
|||
string str_attr(char *&str) { |
|||
if (!str) |
|||
return string(""); |
|||
return string(str); |
|||
} |
|||
free_stringlist(sl); |
|||
sl = _sl; |
|||
} |
|||
|
|||
void str_attr(char *&str, string value) { |
|||
string normalized = normalize(value, norm_nfc); |
|||
free(str); |
|||
str = strdup(normalized.c_str()); |
|||
if (!str) |
|||
throw bad_alloc(); |
|||
} |
|||
bp::dict strdict_attr(::stringpair_list_t *&spl) { |
|||
bp::dict result; |
|||
|
|||
time_t timestamp_attr(timestamp *&ts) { |
|||
if (!ts) |
|||
return 0; |
|||
for (::stringpair_list_t *_spl = spl; _spl && _spl->value; _spl = _spl->next) { |
|||
::stringpair_t *p = _spl->value; |
|||
if (p->key && p->value) { |
|||
string key(p->key); |
|||
string value(p->value); |
|||
|
|||
return timegm(ts); |
|||
result[key] = value; |
|||
} |
|||
} |
|||
|
|||
void timestamp_attr(timestamp *&ts, time_t value) { |
|||
free_timestamp(ts); |
|||
ts = new_timestamp(value); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
boost::python::list strlist_attr(stringlist_t *&sl) { |
|||
boost::python::list result; |
|||
void strdict_attr(::stringpair_list_t *&spl, bp::dict value) { |
|||
::stringpair_list_t *_spl = ::new_stringpair_list(nullptr); |
|||
if (!_spl) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
|||
string s(_sl->value); |
|||
result.append(object(s)); |
|||
} |
|||
::stringpair_list_t *_s = _spl; |
|||
for (int i = 0; i < len(value); i++) { |
|||
bp::extract<string> extract_key(value.keys()[i]); |
|||
bp::extract<string> extract_value(value.values()[i]); |
|||
|
|||
return result; |
|||
if (!(extract_key.check() && extract_value.check())) { |
|||
free_stringpair_list(_spl); |
|||
} |
|||
|
|||
void strlist_attr(stringlist_t *&sl, boost::python::list value) { |
|||
stringlist_t *_sl = new_stringlist(NULL); |
|||
if (!_sl) |
|||
throw bad_alloc(); |
|||
|
|||
stringlist_t *_s = _sl; |
|||
for (int i = 0; i < len(value); i++) { |
|||
extract <string> extract_string(value[i]); |
|||
if (!extract_string.check()) { |
|||
free_stringlist(_sl); |
|||
} |
|||
string s = extract_string(); |
|||
s = normalize(s, norm_nfc); |
|||
_s = stringlist_add(_s, s.c_str()); |
|||
if (!_s) { |
|||
free_stringlist(_sl); |
|||
throw bad_alloc(); |
|||
} |
|||
} |
|||
|
|||
free_stringlist(sl); |
|||
sl = _sl; |
|||
string key = extract_key(); |
|||
key = normalize(key, bl::norm_nfc); |
|||
string _value = extract_value(); |
|||
_value = normalize(_value, bl::norm_nfc); |
|||
::stringpair_t *pair = ::new_stringpair(key.c_str(), _value.c_str()); |
|||
if (!pair) { |
|||
free_stringpair_list(_spl); |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
dict strdict_attr(stringpair_list_t *&spl) { |
|||
dict result; |
|||
|
|||
for (stringpair_list_t *_spl = spl; _spl && _spl->value; _spl = |
|||
_spl->next) { |
|||
stringpair_t *p = _spl->value; |
|||
if (p->key && p->value) { |
|||
string key(p->key); |
|||
string value(p->value); |
|||
|
|||
result[key] = value; |
|||
} |
|||
} |
|||
|
|||
return result; |
|||
_s = stringpair_list_add(_s, pair); |
|||
if (!_s) { |
|||
free_stringpair_list(_spl); |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
void strdict_attr(stringpair_list_t *&spl, dict value) { |
|||
stringpair_list_t *_spl = new_stringpair_list(NULL); |
|||
if (!_spl) |
|||
throw bad_alloc(); |
|||
|
|||
stringpair_list_t *_s = _spl; |
|||
for (int i = 0; i < len(value); i++) { |
|||
extract <string> extract_key(value.keys()[i]); |
|||
extract <string> extract_value(value.values()[i]); |
|||
|
|||
if (!(extract_key.check() && extract_value.check())) |
|||
free_stringpair_list(_spl); |
|||
|
|||
string key = extract_key(); |
|||
key = normalize(key, norm_nfc); |
|||
string _value = extract_value(); |
|||
_value = normalize(_value, norm_nfc); |
|||
stringpair_t *pair = new_stringpair(key.c_str(), _value.c_str()); |
|||
if (!pair) { |
|||
free_stringpair_list(_spl); |
|||
throw bad_alloc(); |
|||
} |
|||
_s = stringpair_list_add(_s, pair); |
|||
if (!_s) { |
|||
free_stringpair_list(_spl); |
|||
throw bad_alloc(); |
|||
} |
|||
} |
|||
|
|||
free_stringpair_list(spl); |
|||
spl = _spl; |
|||
} |
|||
|
|||
free_stringpair_list(spl); |
|||
spl = _spl; |
|||
} |
|||
|
|||
::stringlist_t *to_stringlist(bp::list l) { |
|||
::stringlist_t *result = ::new_stringlist(nullptr); |
|||
if (!result) { |
|||
throw bad_alloc(); |
|||
} |
|||
|
|||
::stringlist_t *_s = result; |
|||
for (int i = 0; i < len(l); i++) { |
|||
bp::extract<string> extract_string(l[i]); |
|||
if (!extract_string.check()) { |
|||
free_stringlist(result); |
|||
} |
|||
|
|||
stringlist_t *to_stringlist(boost::python::list l) { |
|||
stringlist_t *result = new_stringlist(NULL); |
|||
if (!result) |
|||
throw bad_alloc(); |
|||
|
|||
stringlist_t *_s = result; |
|||
for (int i = 0; i < len(l); i++) { |
|||
extract <string> extract_string(l[i]); |
|||
if (!extract_string.check()) |
|||
free_stringlist(result); |
|||
string s = extract_string(); |
|||
_s = stringlist_add(_s, s.c_str()); |
|||
if (!_s) { |
|||
free_stringlist(result); |
|||
throw bad_alloc(); |
|||
} |
|||
} |
|||
|
|||
return result; |
|||
string s = extract_string(); |
|||
_s = stringlist_add(_s, s.c_str()); |
|||
if (!_s) { |
|||
free_stringlist(result); |
|||
throw bad_alloc(); |
|||
} |
|||
} |
|||
|
|||
boost::python::list from_stringlist(const stringlist_t *sl) { |
|||
boost::python::list result; |
|||
for (const stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
|||
string s = _sl->value; |
|||
result.append(s); |
|||
} |
|||
return result; |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
bp::list from_stringlist(const ::stringlist_t *sl) { |
|||
bp::list result; |
|||
for (const ::stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
|||
string s = _sl->value; |
|||
result.append(s); |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
} // namespace PythonAdapter
|
|||
} // namespace PythonAdapter
|
|||
} // namespace pEp {
|
|||
|
|||
|
Loading…
Reference in new issue