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
|
// This file is under GNU Affero General Public License 3.0
|
||||
// see LICENSE.txt
|
// 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" |
#include "basic_api.hh" |
||||
|
|
||||
namespace pEp { |
namespace pEp { |
||||
namespace PythonAdapter { |
namespace PythonAdapter { |
||||
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 "' may only be used for own identities"); |
||||
PEP_OWN_USERID |
} |
||||
"' may only be used for own identities"); |
::PEP_STATUS status = ::update_identity(Adapter::session(), ident); |
||||
|
_throw_status(status); |
||||
PEP_STATUS status = update_identity(Adapter::session(), ident); |
} |
||||
_throw_status(status); |
|
||||
} |
void myself(Identity &ident) { |
||||
|
if (ident.address() == "") { |
||||
void myself(Identity &ident) { |
throw invalid_argument("address needed"); |
||||
if (ident.address() == "") |
} |
||||
throw invalid_argument("address needed"); |
if (ident.username() == "") { |
||||
if (ident.username() == "") |
throw invalid_argument("username needed"); |
||||
throw invalid_argument("username needed"); |
} |
||||
|
if (ident.user_id() == "") { |
||||
if (ident.user_id() == "") |
ident.user_id(ident.address()); |
||||
ident.user_id(ident.address()); |
} |
||||
|
::PEP_STATUS status = ::myself(Adapter::session(), ident); |
||||
PEP_STATUS status = myself(Adapter::session(), ident); |
_throw_status(status); |
||||
_throw_status(status); |
} |
||||
} |
|
||||
|
string _trustwords(Identity me, Identity partner, string lang, bool full) { |
||||
string _trustwords(Identity me, Identity partner, string lang, bool full) { |
if (me.fpr() == "" || partner.fpr() == "") { |
||||
if (me.fpr() == "" || partner.fpr() == "") |
throw invalid_argument("fingerprint needed in Identities"); |
||||
throw invalid_argument("fingerprint needed in Identities"); |
} |
||||
|
if (lang == "" && me.lang() == partner.lang()) { |
||||
if (lang == "" && me.lang() == partner.lang()) |
lang = me.lang(); |
||||
lang = me.lang(); |
} |
||||
|
char *words = nullptr; |
||||
char *words = NULL; |
size_t size = 0; |
||||
size_t size = 0; |
::PEP_STATUS status = ::get_trustwords(Adapter::session(), me, partner, lang.c_str(), &words, &size, full); |
||||
PEP_STATUS status = get_trustwords(Adapter::session(), me, partner, |
_throw_status(status); |
||||
lang.c_str(), &words, &size, full); |
return words; |
||||
_throw_status(status); |
} |
||||
return words; |
|
||||
} |
void trust_personal_key(Identity ident) { |
||||
|
if (ident.fpr() == "") { |
||||
void trust_personal_key(Identity ident) { |
throw invalid_argument("fingerprint needed in Identities"); |
||||
if (ident.fpr() == "") |
} |
||||
throw invalid_argument("fingerprint needed in Identities"); |
if (ident.user_id() == "") { |
||||
if (ident.user_id() == "") |
throw invalid_argument("user_id must be provided"); |
||||
throw invalid_argument("user_id must be provided"); |
} |
||||
|
::PEP_STATUS status = ::trust_personal_key(Adapter::session(), ident); |
||||
PEP_STATUS status = trust_personal_key(Adapter::session(), ident); |
_throw_status(status); |
||||
_throw_status(status); |
} |
||||
} |
|
||||
|
void set_identity_flags(Identity ident,const ::identity_flags_t &flags) { |
||||
void set_identity_flags(Identity ident, identity_flags_t flags) { |
if (ident.address() == "") { |
||||
if (ident.address() == "") |
throw invalid_argument("address needed"); |
||||
throw invalid_argument("address needed"); |
} |
||||
if (ident.user_id() == "") |
if (ident.user_id() == "") { |
||||
throw invalid_argument("user_id needed"); |
throw invalid_argument("user_id needed"); |
||||
|
} |
||||
PEP_STATUS status = set_identity_flags(Adapter::session(), ident, flags); |
::PEP_STATUS status = ::set_identity_flags(Adapter::session(), ident, flags); |
||||
_throw_status(status); |
_throw_status(status); |
||||
} |
} |
||||
|
|
||||
void unset_identity_flags(Identity ident, identity_flags_t flags) { |
void unset_identity_flags(Identity ident,const ::identity_flags_t &flags) { |
||||
if (ident.address() == "") |
if (ident.address() == "") { |
||||
throw invalid_argument("address needed"); |
throw invalid_argument("address needed"); |
||||
if (ident.user_id() == "") |
} |
||||
throw invalid_argument("user_id needed"); |
if (ident.user_id() == "") { |
||||
|
throw invalid_argument("user_id needed"); |
||||
PEP_STATUS status = unset_identity_flags(Adapter::session(), ident, flags); |
} |
||||
_throw_status(status); |
::PEP_STATUS status = ::unset_identity_flags(Adapter::session(), ident, flags); |
||||
} |
_throw_status(status); |
||||
|
} |
||||
void key_reset_trust(Identity ident) { |
|
||||
if (ident.fpr() == "") |
void key_reset_trust(Identity ident) { |
||||
throw invalid_argument("fpr needed"); |
if (ident.fpr() == "") { |
||||
if (ident.address() == "") |
throw invalid_argument("fpr needed"); |
||||
throw invalid_argument("address needed"); |
} |
||||
if (ident.user_id() == "") |
if (ident.address() == "") { |
||||
throw invalid_argument("user_id needed"); |
throw invalid_argument("address needed"); |
||||
|
} |
||||
PEP_STATUS status = key_reset_trust(Adapter::session(), ident); |
if (ident.user_id() == "") { |
||||
_throw_status(status); |
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); |
bp::list import_key(const string &key_data) { |
||||
if (status && status != PEP_KEY_IMPORTED) |
::identity_list *private_keys = nullptr; |
||||
_throw_status(status); |
::PEP_STATUS status = ::import_key(Adapter::session(), key_data.c_str(), key_data.size(), &private_keys); |
||||
|
if (status && status != ::PEP_KEY_IMPORTED) { |
||||
auto result = boost::python::list(); |
_throw_status(status); |
||||
for (::identity_list *il = private_keys; il && il->ident; il = il->next) { |
} |
||||
::pEp_identity *ident = ::identity_dup(il->ident); |
auto result = bp::list(); |
||||
if (!ident) { |
for (::identity_list *il = private_keys; il && il->ident; il = il->next) { |
||||
free_identity_list(private_keys); |
::pEp_identity *ident = ::identity_dup(il->ident); |
||||
throw bad_alloc(); |
if (!ident) { |
||||
} |
::free_identity_list(private_keys); |
||||
result.append(Identity(ident)); |
throw bad_alloc(); |
||||
} |
|
||||
|
|
||||
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; |
|
||||
} |
} |
||||
|
result.append(Identity(ident)); |
||||
void set_own_key(Identity &ident, string fpr) { |
} |
||||
if (ident.address() == "") |
::free_identity_list(private_keys); |
||||
throw invalid_argument("address needed"); |
return result; |
||||
if (ident.username() == "") |
} |
||||
throw invalid_argument("username needed"); |
|
||||
if (ident.user_id() == "") |
string export_key(Identity ident) { |
||||
throw invalid_argument("user_id needed"); |
::PEP_STATUS status = ::PEP_STATUS_OK; |
||||
if (fpr == "") |
char *key_data = nullptr; |
||||
throw invalid_argument("fpr needed"); |
size_t size; |
||||
|
status = ::export_key(Adapter::session(), ident.fpr().c_str(), &key_data, &size); |
||||
|
_throw_status(status); |
||||
const char *fpr_c = fpr.c_str(); |
return key_data; |
||||
PEP_STATUS status = set_own_key(Adapter::session(), ident, fpr_c); |
} |
||||
_throw_status(status); |
|
||||
} |
string export_secret_key(Identity ident) { |
||||
|
::PEP_STATUS status = ::PEP_STATUS_OK; |
||||
} // namespace PythonAdapter
|
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
|
} // namespace pEp
|
||||
|
|
||||
|
|
||||
|
@ -1,385 +1,387 @@ |
|||||
// This file is under GNU Affero General Public License 3.0
|
// This file is under GNU Affero General Public License 3.0
|
||||
// see LICENSE.txt
|
// 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
|
// local
|
||||
#include "message.hh" |
#include "message.hh" |
||||
#include "message_api.hh" |
#include "message_api.hh" |
||||
|
|
||||
namespace pEp { |
namespace pEp { |
||||
namespace PythonAdapter { |
namespace PythonAdapter { |
||||
using namespace std; |
|
||||
using namespace boost::python; |
|
||||
|
Message::Blob::Blob(::bloblist_t *bl, bool chained) |
||||
Message::Blob::Blob(bloblist_t *bl, bool chained) : |
: _bl(bl), |
||||
_bl(bl), part_of_chain(chained) { |
part_of_chain(chained) { |
||||
if (!_bl) |
if (!_bl) { |
||||
throw bad_alloc(); |
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); |
||||
} |
} |
||||
|
string filename; |
||||
Message::Blob::Blob(object data, string mime_type, string filename) : |
if (_bl->filename) { |
||||
_bl(new_bloblist(NULL, 0, NULL, NULL)), part_of_chain(false) { |
filename = string(_bl->filename); |
||||
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); |
|
||||
} |
} |
||||
|
build << repr(mime_type) << ", "; |
||||
Message::Blob::Blob(const Message::Blob &second) : |
build << repr(filename); |
||||
_bl(second._bl), part_of_chain(true) { |
} |
||||
|
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 (_mime_type == "application/pEp.keyreset") { |
||||
if (!part_of_chain) { |
encoding = "pep.distribution"; |
||||
free(_bl->value); |
|
||||
free(_bl); |
|
||||
} |
|
||||
} |
} |
||||
|
|
||||
string Message::Blob::_repr() { |
} |
||||
stringstream build; |
bp::object codecs = bp::import("codecs"); |
||||
build << "Blob("; |
bp::object _decode = codecs.attr("decode"); |
||||
if (!_bl) { |
return bp::call<string>(_decode.ptr(), this, encoding); |
||||
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(); |
|
||||
} |
|
||||
|
|
||||
int Message::Blob::getbuffer(PyObject *self, Py_buffer *view, int flags) { |
PyBufferProcs Message::Blob::bp = {getbuffer, nullptr}; |
||||
bloblist_t *bl = NULL; |
|
||||
|
|
||||
try { |
Message::Message(int dir, Identity *from) |
||||
Message::Blob &blob = extract<Message::Blob &>(self); |
: _msg(new_message((::PEP_msg_direction)dir), &::free_message) { |
||||
bl = blob._bl; |
if (!_msg) { |
||||
} |
throw bad_alloc(); |
||||
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; |
|
||||
} |
|
||||
|
|
||||
return PyBuffer_FillInfo(view, self, bl->value, bl->size, 0, flags); |
if (from) { |
||||
|
_msg->from = ::identity_dup(*from); |
||||
|
if (!_msg->from) { |
||||
|
throw bad_alloc(); |
||||
} |
} |
||||
|
_msg->dir = (::PEP_msg_direction)dir; |
||||
string Message::Blob::decode(string encoding) { |
} |
||||
if (encoding == "") { |
} |
||||
string _mime_type = _bl->mime_type ? _bl->mime_type : ""; |
|
||||
encoding = "ascii"; |
Message::Message(string mimetext) |
||||
|
: _msg(nullptr, &::free_message) { |
||||
if (_mime_type == "application/pEp.sync") |
::message *_cpy; |
||||
encoding = "pep.sync"; |
::PEP_STATUS status = ::mime_decode_message(mimetext.c_str(), mimetext.size(), &_cpy, nullptr); |
||||
|
switch (status) { |
||||
if (_mime_type == "application/pEp.keyreset") |
case ::PEP_STATUS_OK: |
||||
encoding = "pep.distribution"; |
if (_cpy) { |
||||
|
_cpy->dir = ::PEP_dir_outgoing; |
||||
} |
} else { |
||||
object codecs = import("codecs"); |
_cpy = new_message(::PEP_dir_outgoing); |
||||
object _decode = codecs.attr("decode"); |
|
||||
return call<string>(_decode.ptr(), this, encoding); |
|
||||
} |
} |
||||
|
|
||||
PyBufferProcs Message::Blob::bp = {getbuffer, NULL}; |
if (!_cpy) { |
||||
|
throw bad_alloc(); |
||||
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; |
|
||||
} |
|
||||
} |
} |
||||
|
|
||||
Message::Message(string mimetext) |
_msg = shared_ptr<::message>(_cpy); |
||||
: _msg(NULL, &free_message) { |
break; |
||||
message *_cpy; |
|
||||
PEP_STATUS status = mime_decode_message(mimetext.c_str(), |
case ::PEP_BUFFER_TOO_SMALL: |
||||
mimetext.size(), &_cpy, NULL); |
throw runtime_error("mime_decode_message: buffer too small"); |
||||
switch (status) { |
|
||||
case PEP_STATUS_OK: |
case ::PEP_CANNOT_CREATE_TEMP_FILE: |
||||
if (_cpy) |
throw runtime_error("mime_decode_message: cannot create temp file"); |
||||
_cpy->dir = PEP_dir_outgoing; |
|
||||
else |
case ::PEP_OUT_OF_MEMORY: |
||||
_cpy = new_message(PEP_dir_outgoing); |
throw bad_alloc(); |
||||
|
|
||||
if (!_cpy) |
default: |
||||
throw bad_alloc(); |
stringstream build; |
||||
|
build << "mime_decode_message: unknown error (" << (int)status << ")"; |
||||
_msg = shared_ptr<message>(_cpy); |
throw runtime_error(build.str()); |
||||
break; |
} |
||||
|
} |
||||
case PEP_BUFFER_TOO_SMALL: |
|
||||
throw runtime_error("mime_decode_message: buffer too small"); |
Message::Message(const Message &second) |
||||
|
: _msg(second._msg) { |
||||
case PEP_CANNOT_CREATE_TEMP_FILE: |
if (!_msg.get()) { |
||||
throw runtime_error("mime_decode_message: cannot create temp file"); |
throw bad_alloc(); |
||||
|
} |
||||
case PEP_OUT_OF_MEMORY: |
} |
||||
throw bad_alloc(); |
|
||||
|
Message::Message(::message *msg) |
||||
default: |
: _msg(::message_dup(msg), &::free_message) {} |
||||
stringstream build; |
|
||||
build << "mime_decode_message: unknown error (" << (int) status << ")"; |
Message::~Message() {} |
||||
throw runtime_error(build.str()); |
|
||||
|
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) { |
for (int i = 0; i < len(value); i++) { |
||||
if (!_msg.get()) |
Message::Blob &blob = bp::extract<Message::Blob &>(value[i]); |
||||
throw bad_alloc(); |
blob._bl->value = nullptr; |
||||
} |
blob._bl->size = 0; |
||||
|
free(blob._bl->mime_type); |
||||
Message::Message(message *msg) |
blob._bl->mime_type = nullptr; |
||||
: _msg(::message_dup(msg), &free_message) { |
free(blob._bl->filename); |
||||
|
blob._bl->filename = nullptr; |
||||
} |
} |
||||
|
|
||||
Message::~Message() { |
free_bloblist(_msg->attachments); |
||||
|
_msg->attachments = bl; |
||||
} |
} |
||||
|
|
||||
Message::operator message *() { |
Message Message::encrypt() { |
||||
return _msg.get(); |
bp::list extra; |
||||
} |
return encrypt_message(*this, extra, ::PEP_enc_PGP_MIME, 0); |
||||
|
} |
||||
Message::operator const message *() const { |
|
||||
return _msg.get(); |
Message Message::_encrypt(bp::list extra, int enc_format, int flags) { |
||||
} |
if (!enc_format) { |
||||
|
enc_format = ::PEP_enc_PGP_MIME; |
||||
string Message::_str() { |
} |
||||
if (!(_msg->from && _msg->from->address && _msg->from->address[0])) |
return encrypt_message(*this, extra, enc_format, flags); |
||||
throw out_of_range(".from_.address missing"); |
} |
||||
|
|
||||
char *mimetext; |
bp::tuple Message::decrypt(int flags) { |
||||
string result; |
return pEp::PythonAdapter::decrypt_message(*this, flags); |
||||
|
} |
||||
PEP_STATUS status = mime_encode_message(*this, false, &mimetext, false); |
|
||||
switch (status) { |
::PEP_rating Message::outgoing_rating() { |
||||
case PEP_STATUS_OK: |
if (_msg->dir != ::PEP_dir_outgoing) { |
||||
result = mimetext; |
throw invalid_argument("Message.dir must be outgoing"); |
||||
free(mimetext); |
} |
||||
break; |
|
||||
|
if (from().address() == "") { |
||||
case PEP_BUFFER_TOO_SMALL: |
throw invalid_argument("from.address needed"); |
||||
throw runtime_error("mime_encode_message: buffer too small"); |
} |
||||
|
if (from().username() == "") { |
||||
case PEP_CANNOT_CREATE_TEMP_FILE: |
throw invalid_argument("from.username needed"); |
||||
throw runtime_error("mime_encode_message: cannot create temp file"); |
} |
||||
|
|
||||
case PEP_OUT_OF_MEMORY: |
if (len(to()) + len(cc()) == 0) { |
||||
throw bad_alloc(); |
throw invalid_argument("either to or cc needed"); |
||||
|
} |
||||
default: |
|
||||
stringstream build; |
::PEP_STATUS status = myself(Adapter::session(), _msg->from); |
||||
build << "mime_encode_message: unknown error (" << (int) status << ")"; |
_throw_status(status); |
||||
throw runtime_error(build.str()); |
|
||||
} |
::PEP_rating rating = ::PEP_rating_undefined; |
||||
|
status = outgoing_message_rating(Adapter::session(), *this, &rating); |
||||
return result; |
_throw_status(status); |
||||
} |
|
||||
|
return rating; |
||||
string Message::_repr() { |
} |
||||
stringstream build; |
|
||||
build << "Message(" << repr(_str()) << ")"; |
::PEP_color Message::outgoing_color() { |
||||
return build.str(); |
return _color(outgoing_rating()); |
||||
} |
} |
||||
|
|
||||
boost::python::tuple Message::attachments() { |
Message Message::copy() { |
||||
boost::python::list l; |
::message *dup = ::message_dup(*this); |
||||
|
if (!dup) { |
||||
for (bloblist_t *bl = _msg->attachments; bl && bl->value; bl = |
throw bad_alloc(); |
||||
bl->next) { |
} |
||||
l.append(Blob(bl, true)); |
return Message(dup); |
||||
} |
} |
||||
|
|
||||
return boost::python::tuple(l); |
Message Message::deepcopy(bp::dict &) { |
||||
} |
return copy(); |
||||
|
} |
||||
void Message::attachments(boost::python::list value) { |
|
||||
bloblist_t *bl = new_bloblist(NULL, 0, NULL, NULL); |
Message outgoing_message(Identity me) { |
||||
if (!bl) |
if (me.address().empty() || me.user_id().empty()) { |
||||
throw bad_alloc(); |
throw runtime_error("at least address and user_id of own user needed"); |
||||
|
} |
||||
bloblist_t *_l = bl; |
|
||||
for (int i = 0; i < len(value); i++) { |
::myself(Adapter::session(), me); |
||||
Message::Blob &blob = extract<Message::Blob &>(value[i]); |
auto m = Message(::PEP_dir_outgoing, &me); |
||||
_l = bloblist_add(_l, blob._bl->value, blob._bl->size, |
return m; |
||||
blob._bl->mime_type, blob._bl->filename); |
} |
||||
if (!_l) { |
|
||||
for (_l = bl; _l && _l->value;) { |
static bp::object update(Identity ident) { |
||||
free(_l->mime_type); |
if (ident.address().empty()) { |
||||
free(_l->filename); |
throw runtime_error("at least address needed"); |
||||
bloblist_t *_ll = _l; |
} |
||||
_l = _l->next; |
::update_identity(Adapter::session(), ident); |
||||
free(_ll); |
return bp::object(ident); |
||||
} |
} |
||||
throw bad_alloc(); |
|
||||
} |
static bp::list update(bp::list il) { |
||||
} |
for (int i = 0; i < len(il); i++) { |
||||
|
update(bp::extract<Identity>(il[i])); |
||||
for (int i = 0; i < len(value); i++) { |
} |
||||
Message::Blob &blob = extract<Message::Blob &>(value[i]); |
|
||||
blob._bl->value = NULL; |
return il; |
||||
blob._bl->size = 0; |
} |
||||
free(blob._bl->mime_type); |
|
||||
blob._bl->mime_type = NULL; |
Message incoming_message(string mime_text) { |
||||
free(blob._bl->filename); |
auto m = Message(mime_text); |
||||
blob._bl->filename = NULL; |
m.dir(::PEP_dir_incoming); |
||||
} |
|
||||
|
try { |
||||
free_bloblist(_msg->attachments); |
m.from(update(m.from())); |
||||
_msg->attachments = bl; |
} catch (out_of_range &) { |
||||
} |
} |
||||
|
|
||||
Message Message::encrypt() { |
try { |
||||
boost::python::list extra; |
m.recv_by(update(m.recv_by())); |
||||
return encrypt_message(*this, extra, PEP_enc_PGP_MIME, 0); |
} catch (out_of_range &) { |
||||
} |
} |
||||
|
|
||||
Message Message::_encrypt(boost::python::list extra, int enc_format, int flags) { |
m.to(update(m.to())); |
||||
if (!enc_format) |
m.cc(update(m.cc())); |
||||
enc_format = PEP_enc_PGP_MIME; |
m.reply_to(update(m.reply_to())); |
||||
return encrypt_message(*this, extra, enc_format, flags); |
|
||||
} |
return m; |
||||
|
} |
||||
boost::python::tuple Message::decrypt(int flags) { |
|
||||
return pEp::PythonAdapter::decrypt_message(*this, flags); |
} // namespace PythonAdapter
|
||||
} |
|
||||
|
|
||||
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
|
|
||||
} // namespace pEp
|
} // namespace pEp
|
||||
|
@ -1,161 +1,158 @@ |
|||||
// This file is under GNU Affero General Public License 3.0
|
// This file is under GNU Affero General Public License 3.0
|
||||
// see LICENSE.txt
|
// 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
|
// local
|
||||
#include "message_api.hh" |
#include "message_api.hh" |
||||
#include "basic_api.hh" |
|
||||
|
|
||||
namespace pEp { |
namespace pEp { |
||||
namespace PythonAdapter { |
namespace PythonAdapter { |
||||
using namespace std; |
|
||||
using namespace boost::python; |
|
||||
|
Message encrypt_message(Message src, bp::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() == "") |
|
||||
src.from().user_id(_from.address()); |
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; |
::stringlist_t *_extra = to_stringlist(extra); |
||||
message *_dst = NULL; |
::PEP_enc_format _enc_format = (::PEP_enc_format)enc_format; |
||||
|
::PEP_encrypt_flags_t _flags = (::PEP_encrypt_flags_t)flags; |
||||
message *_src = src; |
::message *_dst = nullptr; |
||||
PEP_STATUS status = encrypt_message(Adapter::session(), _src, _extra, &_dst, |
|
||||
_enc_format, _flags); |
::message *_src = src; |
||||
free_stringlist(_extra); |
::PEP_STATUS status = ::encrypt_message(Adapter::session(), _src, _extra, &_dst, _enc_format, _flags); |
||||
_throw_status(status); |
::free_stringlist(_extra); |
||||
|
_throw_status(status); |
||||
if (!_dst || _dst == _src) |
|
||||
return Message(_src); |
if (!_dst || _dst == _src) { |
||||
|
return Message(_src); |
||||
return Message(_dst); |
} |
||||
} |
|
||||
|
return Message(_dst); |
||||
boost::python::tuple decrypt_message(Message src, int flags) { |
} |
||||
message *_dst = NULL; |
|
||||
stringlist_t *_keylist = NULL; |
bp::tuple decrypt_message(Message src, int flags) { |
||||
PEP_rating _rating = PEP_rating_undefined; |
::message *_dst = nullptr; |
||||
PEP_decrypt_flags_t _flags = (PEP_decrypt_flags_t) flags; |
::stringlist_t *_keylist = nullptr; |
||||
message *_src = src; |
::PEP_rating _rating = ::PEP_rating_undefined; |
||||
|
::PEP_decrypt_flags_t _flags = (::PEP_decrypt_flags_t)flags; |
||||
PEP_STATUS status = ::decrypt_message(Adapter::session(), _src, &_dst, &_keylist, |
::message *_src = src; |
||||
&_rating, &_flags); |
|
||||
_throw_status(status); |
::PEP_STATUS status = ::decrypt_message(Adapter::session(), _src, &_dst, &_keylist, &_rating, &_flags); |
||||
|
_throw_status(status); |
||||
boost::python::list keylist; |
|
||||
if (_keylist) { |
bp::list keylist; |
||||
keylist = from_stringlist(_keylist); |
if (_keylist) { |
||||
free_stringlist(_keylist); |
keylist = from_stringlist(_keylist); |
||||
} |
::free_stringlist(_keylist); |
||||
|
} |
||||
Message dst = _dst ? Message(_dst) : Message(src); |
|
||||
return boost::python::make_tuple(dst, keylist, _rating, _flags); |
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); |
::PEP_color _color(int rating) { |
||||
} |
return ::color_from_rating((::PEP_rating)rating); |
||||
|
} |
||||
boost::python::tuple sync_decode(object buffer) { |
|
||||
Py_buffer src; |
bp::tuple sync_decode(bp::object buffer) { |
||||
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
Py_buffer src; |
||||
if (result) |
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
||||
throw invalid_argument("need a contiguous buffer to read"); |
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); |
char *dst = nullptr; |
||||
_throw_status(status); |
::PEP_STATUS status = ::PER_to_XER_Sync_msg((char *)src.buf, src.len, &dst); |
||||
|
PyBuffer_Release(&src); |
||||
string _dst(dst); |
_throw_status(status); |
||||
free(dst); |
|
||||
return boost::python::make_tuple(_dst, 0); |
string _dst(dst); |
||||
} |
free(dst); |
||||
|
return bp::make_tuple(_dst, 0); |
||||
static boost::python::tuple sync_encode(string text) { |
} |
||||
char *data = NULL; |
|
||||
size_t size = 0; |
static bp::tuple sync_encode(string text) { |
||||
PEP_STATUS status = XER_to_PER_Sync_msg(text.c_str(), &data, &size); |
char *data = nullptr; |
||||
_throw_status(status); |
size_t size = 0; |
||||
|
::PEP_STATUS status = ::XER_to_PER_Sync_msg(text.c_str(), &data, &size); |
||||
PyObject *ba = PyBytes_FromStringAndSize(data, size); |
_throw_status(status); |
||||
free(data); |
|
||||
if (!ba) |
PyObject * ba = PyBytes_FromStringAndSize(data, size); |
||||
throw bad_alloc(); |
free(data); |
||||
|
if (!ba) { |
||||
return boost::python::make_tuple(object(handle<>(ba)), 0); |
throw bad_alloc(); |
||||
} |
} |
||||
|
|
||||
boost::python::tuple Distribution_decode(object buffer) { |
return bp::make_tuple(bp::object(bp::handle<>(ba)), 0); |
||||
Py_buffer src; |
} |
||||
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
|
||||
if (result) |
bp::tuple Distribution_decode(bp::object buffer) { |
||||
throw invalid_argument("need a contiguous buffer to read"); |
Py_buffer src; |
||||
|
int result = PyObject_GetBuffer(buffer.ptr(), &src, PyBUF_CONTIG_RO); |
||||
char *dst = NULL; |
if (result) { |
||||
PEP_STATUS status = PER_to_XER_Distribution_msg((char *) src.buf, src.len, &dst); |
throw invalid_argument("need a contiguous buffer to read"); |
||||
PyBuffer_Release(&src); |
} |
||||
_throw_status(status); |
|
||||
|
char *dst = nullptr; |
||||
string _dst(dst); |
::PEP_STATUS status = ::PER_to_XER_Distribution_msg((char *)src.buf, src.len, &dst); |
||||
free(dst); |
PyBuffer_Release(&src); |
||||
return boost::python::make_tuple(_dst, 0); |
_throw_status(status); |
||||
} |
|
||||
|
string _dst(dst); |
||||
static boost::python::tuple Distribution_encode(string text) { |
free(dst); |
||||
char *data = NULL; |
return bp::make_tuple(_dst, 0); |
||||
size_t size = 0; |
} |
||||
PEP_STATUS status = XER_to_PER_Distribution_msg(text.c_str(), &data, &size); |
|
||||
_throw_status(status); |
static bp::tuple Distribution_encode(string text) { |
||||
|
char *data = nullptr; |
||||
PyObject *ba = PyBytes_FromStringAndSize(data, size); |
size_t size = 0; |
||||
free(data); |
::PEP_STATUS status = ::XER_to_PER_Distribution_msg(text.c_str(), &data, &size); |
||||
if (!ba) |
_throw_status(status); |
||||
throw bad_alloc(); |
|
||||
|
PyObject * ba = PyBytes_FromStringAndSize(data, size); |
||||
return boost::python::make_tuple(object(handle<>(ba)), 0); |
free(data); |
||||
} |
if (!ba) { |
||||
|
throw bad_alloc(); |
||||
object sync_search(string name) { |
} |
||||
if (name != "pep.sync") { |
|
||||
return object(); |
return bp::make_tuple(bp::object(bp::handle<>(ba)), 0); |
||||
} else { |
} |
||||
object codecs = import("codecs"); |
|
||||
object CodecInfo = codecs.attr("CodecInfo"); |
bp::object sync_search(string name) { |
||||
|
if (name != "pep.sync") { |
||||
object _sync_decode = make_function(sync_decode); |
return bp::object(); |
||||
object _sync_encode = make_function(sync_encode); |
} else { |
||||
|
bp::object codecs = bp::import("codecs"); |
||||
return call<object>(CodecInfo.ptr(), _sync_encode, _sync_decode); |
bp::object CodecInfo = codecs.attr("CodecInfo"); |
||||
} |
|
||||
} |
bp::object _sync_decode = make_function(sync_decode); |
||||
|
bp::object _sync_encode = make_function(sync_encode); |
||||
object distribution_search(string name) { |
|
||||
if (name != "pep.distribution") { |
return bp::call<bp::object>(CodecInfo.ptr(), _sync_encode, _sync_decode); |
||||
return object(); |
} |
||||
} else { |
} |
||||
object codecs = import("codecs"); |
|
||||
object CodecInfo = codecs.attr("CodecInfo"); |
bp::object distribution_search(string name) { |
||||
|
if (name != "pep.distribution") { |
||||
object _distribution_decode = make_function(Distribution_decode); |
return bp::object(); |
||||
object _distribution_encode = make_function(Distribution_encode); |
} else { |
||||
|
bp::object codecs = bp::import("codecs"); |
||||
return call<object>(CodecInfo.ptr(), _distribution_encode, _distribution_decode); |
bp::object CodecInfo = codecs.attr("CodecInfo"); |
||||
} |
|
||||
} |
bp::object _distribution_decode = make_function(Distribution_decode); |
||||
|
bp::object _distribution_encode = make_function(Distribution_encode); |
||||
} // namespace PythonAdapter
|
|
||||
} // namespace pEp {
|
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
|
// This file is under GNU Affero General Public License 3.0
|
||||
// see LICENSE.txt
|
// see LICENSE.txt
|
||||
|
|
||||
// System
|
|
||||
#include <cstdlib> |
|
||||
#include <boost/python.hpp> |
|
||||
#include <boost/locale.hpp> |
|
||||
|
|
||||
// local
|
// local
|
||||
#include "str_attr.hh" |
#include "str_attr.hh" |
||||
|
|
||||
namespace pEp { |
namespace pEp { |
||||
namespace PythonAdapter { |
namespace PythonAdapter { |
||||
using namespace std; |
|
||||
using namespace boost::python; |
|
||||
using namespace boost::locale; |
bp::object repr(bp::object s) { |
||||
|
return s.attr("__repr__")(); |
||||
object repr(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 s = extract_string(); |
||||
string repr(string s) { |
s = normalize(s, bl::norm_nfc); |
||||
str _s = s.c_str(); |
_s = stringlist_add(_s, s.c_str()); |
||||
object _r = _s.attr("__repr__")(); |
if (!_s) { |
||||
string r = extract<string>(_r); |
free_stringlist(_sl); |
||||
return r; |
throw bad_alloc(); |
||||
} |
} |
||||
|
} |
||||
|
|
||||
string str_attr(char *&str) { |
free_stringlist(sl); |
||||
if (!str) |
sl = _sl; |
||||
return string(""); |
} |
||||
return string(str); |
|
||||
} |
|
||||
|
|
||||
void str_attr(char *&str, string value) { |
bp::dict strdict_attr(::stringpair_list_t *&spl) { |
||||
string normalized = normalize(value, norm_nfc); |
bp::dict result; |
||||
free(str); |
|
||||
str = strdup(normalized.c_str()); |
|
||||
if (!str) |
|
||||
throw bad_alloc(); |
|
||||
} |
|
||||
|
|
||||
time_t timestamp_attr(timestamp *&ts) { |
for (::stringpair_list_t *_spl = spl; _spl && _spl->value; _spl = _spl->next) { |
||||
if (!ts) |
::stringpair_t *p = _spl->value; |
||||
return 0; |
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) { |
return result; |
||||
free_timestamp(ts); |
} |
||||
ts = new_timestamp(value); |
|
||||
} |
|
||||
|
|
||||
boost::python::list strlist_attr(stringlist_t *&sl) { |
void strdict_attr(::stringpair_list_t *&spl, bp::dict value) { |
||||
boost::python::list result; |
::stringpair_list_t *_spl = ::new_stringpair_list(nullptr); |
||||
|
if (!_spl) { |
||||
|
throw bad_alloc(); |
||||
|
} |
||||
|
|
||||
for (stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
::stringpair_list_t *_s = _spl; |
||||
string s(_sl->value); |
for (int i = 0; i < len(value); i++) { |
||||
result.append(object(s)); |
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) { |
string key = extract_key(); |
||||
stringlist_t *_sl = new_stringlist(NULL); |
key = normalize(key, bl::norm_nfc); |
||||
if (!_sl) |
string _value = extract_value(); |
||||
throw bad_alloc(); |
_value = normalize(_value, bl::norm_nfc); |
||||
|
::stringpair_t *pair = ::new_stringpair(key.c_str(), _value.c_str()); |
||||
stringlist_t *_s = _sl; |
if (!pair) { |
||||
for (int i = 0; i < len(value); i++) { |
free_stringpair_list(_spl); |
||||
extract <string> extract_string(value[i]); |
throw bad_alloc(); |
||||
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; |
|
||||
} |
} |
||||
|
_s = stringpair_list_add(_s, pair); |
||||
dict strdict_attr(stringpair_list_t *&spl) { |
if (!_s) { |
||||
dict result; |
free_stringpair_list(_spl); |
||||
|
throw bad_alloc(); |
||||
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; |
|
||||
} |
} |
||||
|
} |
||||
void strdict_attr(stringpair_list_t *&spl, dict value) { |
|
||||
stringpair_list_t *_spl = new_stringpair_list(NULL); |
free_stringpair_list(spl); |
||||
if (!_spl) |
spl = _spl; |
||||
throw bad_alloc(); |
} |
||||
|
|
||||
stringpair_list_t *_s = _spl; |
::stringlist_t *to_stringlist(bp::list l) { |
||||
for (int i = 0; i < len(value); i++) { |
::stringlist_t *result = ::new_stringlist(nullptr); |
||||
extract <string> extract_key(value.keys()[i]); |
if (!result) { |
||||
extract <string> extract_value(value.values()[i]); |
throw bad_alloc(); |
||||
|
} |
||||
if (!(extract_key.check() && extract_value.check())) |
|
||||
free_stringpair_list(_spl); |
::stringlist_t *_s = result; |
||||
|
for (int i = 0; i < len(l); i++) { |
||||
string key = extract_key(); |
bp::extract<string> extract_string(l[i]); |
||||
key = normalize(key, norm_nfc); |
if (!extract_string.check()) { |
||||
string _value = extract_value(); |
free_stringlist(result); |
||||
_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; |
|
||||
} |
} |
||||
|
string s = extract_string(); |
||||
stringlist_t *to_stringlist(boost::python::list l) { |
_s = stringlist_add(_s, s.c_str()); |
||||
stringlist_t *result = new_stringlist(NULL); |
if (!_s) { |
||||
if (!result) |
free_stringlist(result); |
||||
throw bad_alloc(); |
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; |
|
||||
} |
} |
||||
|
} |
||||
|
|
||||
boost::python::list from_stringlist(const stringlist_t *sl) { |
return result; |
||||
boost::python::list result; |
} |
||||
for (const stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
|
||||
string s = _sl->value; |
bp::list from_stringlist(const ::stringlist_t *sl) { |
||||
result.append(s); |
bp::list result; |
||||
} |
for (const ::stringlist_t *_sl = sl; _sl && _sl->value; _sl = _sl->next) { |
||||
return result; |
string s = _sl->value; |
||||
} |
result.append(s); |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
} // namespace PythonAdapter
|
} // namespace PythonAdapter
|
||||
} // namespace pEp {
|
} // namespace pEp {
|
||||
|
|
||||
|
Loading…
Reference in new issue