
17 changed files with 313 additions and 356 deletions
@ -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
|
Loading…
Reference in new issue