1
0
mirror of https://github.com/danog/pif-tdpony.git synced 2024-11-26 20:04:48 +01:00
pif-tdpony/main.cpp

155 lines
4.5 KiB
C++
Raw Normal View History

2018-01-01 17:01:56 +01:00
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2017
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
2018-03-01 01:49:03 +01:00
#include <td/telegram/ClientJson.h>
#include <tdutils/td/utils/Slice.h>
2018-01-01 17:01:56 +01:00
#include <td/telegram/Log.h>
2018-02-15 22:33:15 +01:00
#include <phpcpp.h>
2018-01-01 17:01:56 +01:00
#include <cstdint>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <sstream>
#include <string>
#include <vector>
2018-02-17 12:02:43 +01:00
class API : public Php::Base {
2018-01-01 17:01:56 +01:00
public:
2018-03-01 01:49:03 +01:00
API() = default;
virtual ~API() = default;
2018-02-17 12:02:43 +01:00
void __construct() {
initTdlib();
}
void __wakeup() {
initTdlib();
2018-03-01 01:49:03 +01:00
Php::Value self(this);
if (self["tdlibParameters"]) client_->send(toSlice(self["tdlibParameters"]));
}
void __destruct() {
deinitTdlib();
2018-02-17 12:02:43 +01:00
}
void initTdlib() {
2018-03-01 01:49:03 +01:00
client_ = std::make_unique<td::ClientJson>();
2018-01-01 17:01:56 +01:00
}
2018-02-17 12:02:43 +01:00
void deinitTdlib() {
2018-03-03 01:13:07 +01:00
client_.reset();
2018-02-17 12:02:43 +01:00
}
2018-03-01 01:49:03 +01:00
void send(Php::Parameters &params) {
Php::Value value = params[0];
if (value.get("@type") == "setTdlibParameters") {
Php::Value self(this);
self["tdlibParameters"] = value;
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
client_->send(toSlice(value));
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
Php::Value receive(Php::Parameters &params) {
auto slice = client_->receive(params[0]);
if (slice.empty()) {
return nullptr;
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
Php::Value result = Php::call("json_decode", slice.c_str(), true);
Php::Value self(this);
return result;
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
Php::Value execute(Php::Parameters &params) {
Php::Value value = params[0];
if (value.get("@type") == "setTdlibParameters") {
Php::Value self(this);
self["tdlibParameters"] = value;
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
auto slice = client_->execute(toSlice(value));
if (slice.empty()) {
return nullptr;
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
return Php::call("json_decode", slice.c_str(), true);
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
2018-01-01 17:01:56 +01:00
2018-03-01 01:49:03 +01:00
private:
std::unique_ptr<td::ClientJson> client_;
td::Slice toSlice(Php::Value value) {
std::string str = Php::call("json_encode", value);
return td::Slice(str);
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
};
2018-01-01 17:01:56 +01:00
2018-03-01 01:49:03 +01:00
class Logging : public Php::Base
{
public:
Logging() = default;
virtual ~Logging() = default;
static Php::Value set_file_path(Php::Parameters &params) {
return td::Log::set_file_path(params[0]);
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
static void set_max_file_size(Php::Parameters &params) {
td::Log::set_max_file_size(params[0]);
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
static void set_verbosity_level(Php::Parameters &params) {
td::Log::set_verbosity_level(params[0]);
2018-01-01 17:01:56 +01:00
}
};
2018-02-15 22:33:15 +01:00
extern "C" {
/**
* Function that is called by PHP right after the PHP process
* has started, and that returns an address of an internal PHP
* strucure with all the details and features of your extension
*
* @return void* a pointer to an address that is understood by PHP
*/
PHPCPP_EXPORT void *get_module()
{
// static(!) Php::Extension object that should stay in memory
// for the entire duration of the process (that's why it's static)
static Php::Extension extension("pif-tdpony", "1.0");
// description of the class so that PHP knows which methods are accessible
2018-02-17 12:02:43 +01:00
Php::Class<API> api("API");
2018-02-15 22:33:15 +01:00
2018-03-01 01:49:03 +01:00
api.method<&API::__construct>("__construct", Php::Public | Php::Final, {});
api.method<&API::__wakeup>("__wakeup", Php::Public | Php::Final, {});
api.method<&API::__destruct>("__destruct", Php::Public | Php::Final, {});
api.method<&API::send>("send", Php::Public | Php::Final);
api.method<&API::receive>("receive", Php::Public | Php::Final);
api.method<&API::execute>("execute", Php::Public | Php::Final);
2018-02-15 22:33:15 +01:00
2018-02-17 12:02:43 +01:00
api.property("settings", 0, Php::Public);
2018-02-15 22:33:15 +01:00
2018-02-17 12:02:43 +01:00
api.constant("PIF_TDPONY_VERSION", "1.0");
2018-02-15 22:33:15 +01:00
Php::Namespace danog("danog");
Php::Namespace MadelineProto("MadelineProto");
2018-02-17 12:02:43 +01:00
Php::Namespace X("X");
2018-02-15 22:33:15 +01:00
2018-03-01 01:49:03 +01:00
Php::Class<Logging> logging("Logging");
logging.method<&Logging::set_file_path>("set_file_path", Php::Public, {Php::ByVal("file_path", Php::Type::String)});
logging.method<&Logging::set_max_file_size>("set_max_file_size", Php::Public, {Php::ByVal("set_max_file_size", Php::Type::Numeric)});
logging.method<&Logging::set_verbosity_level>("set_verbosity_level", Php::Public, {Php::ByVal("verbosity_level", Php::Type::Numeric)});
2018-02-15 22:33:15 +01:00
2018-02-17 12:02:43 +01:00
X.add(std::move(api));
2018-03-01 01:49:03 +01:00
X.add(std::move(logging));
2018-02-17 12:02:43 +01:00
MadelineProto.add(std::move(X));
2018-02-15 22:33:15 +01:00
danog.add(std::move(MadelineProto));
extension.add(std::move(danog));
return extension;
}
}