1
0
mirror of https://github.com/danog/pif-tdpony.git synced 2024-12-02 09:17:54 +01:00
pif-tdpony/main.cpp
2018-03-03 18:38:45 +00:00

152 lines
4.5 KiB
C++

//
// 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)
//
#include <td/telegram/td_json_client.h>
#include <td/telegram/Log.h>
#include <phpcpp.h>
#include <cstdint>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <sstream>
#include <string>
#include <vector>
class API : public Php::Base {
public:
API() = default;
virtual ~API() = default;
void __construct(Php::Parameters &params) {
Php::Value self(this);
self["tdlibParameters"] = params[0];
initTdlib();
}
void __wakeup() {
initTdlib();
}
void __destruct() {
deinitTdlib();
}
void initTdlib() {
client = td_json_client_create();
Php::Value self(this);
Php::Array init;
init["@type"] = "setTdlibParameters";
init["tdlibParameters"] = self["tdlibParameters"];
td_json_client_send(client, json_encode(init));
}
void deinitTdlib() {
td_json_client_destroy(client);
}
void send(Php::Parameters &params) {
Php::Value value = params[0];
if (value.get("@type") == "setTdlibParameters") {
Php::Value self(this);
self["tdlibParameters"] = value;
}
td_json_client_send(client, json_encode(value));
}
Php::Value receive(Php::Parameters &params) {
return json_decode(td_json_client_receive(client, params[0]));
}
Php::Value execute(Php::Parameters &params) {
Php::Value value = params[0];
if (value.get("@type") == "setTdlibParameters") {
Php::Value self(this);
self["tdlibParameters"] = value;
}
return json_decode(td_json_client_execute(client, json_encode(value)));
}
private:
void *client;
const char *json_encode(Php::Value value) {
return strdup(Php::call("json_encode", value));
}
Php::Value json_decode(std::string value) {
return Php::call("json_decode", value, true);
}
};
class Logger : public Php::Base
{
public:
Logger() = default;
virtual ~Logger() = default;
static Php::Value set_file_path(Php::Parameters &params) {
return td::Log::set_file_path(params[0]);
}
static void set_max_file_size(Php::Parameters &params) {
td::Log::set_max_file_size(params[0]);
}
static void set_verbosity_level(Php::Parameters &params) {
td::Log::set_verbosity_level(params[0]);
}
};
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
Php::Class<API> api("API");
api.method<&API::__construct>("__construct", Php::Public | Php::Final, {Php::ByVal("tdlibParameters", Php::Type::Array)});
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, {Php::ByVal("timeout", Php::Type::Float)});
api.method<&API::execute>("execute", Php::Public | Php::Final);
api.property("tdlibParameters", nullptr, Php::Private);
api.constant("PIF_TDPONY_VERSION", "1.0");
Php::Namespace danog("danog");
Php::Namespace MadelineProto("MadelineProto");
Php::Namespace X("X");
Php::Class<Logger> logger("Logger");
logger.method<&Logger::set_file_path>("set_file_path", Php::Public, {Php::ByVal("file_path", Php::Type::String)});
logger.method<&Logger::set_max_file_size>("set_max_file_size", Php::Public, {Php::ByVal("set_max_file_size", Php::Type::Numeric)});
logger.method<&Logger::set_verbosity_level>("set_verbosity_level", Php::Public, {Php::ByVal("verbosity_level", Php::Type::Numeric)});
X.add(std::move(api));
X.add(std::move(logger));
MadelineProto.add(std::move(X));
danog.add(std::move(MadelineProto));
extension.add(std::move(danog));
return extension;
}
}