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

147 lines
4.8 KiB
C++
Raw Normal View History

2018-03-05 20:22:25 +01:00
/*
Copyright 2018 Daniil Gentili
(https://daniil.it)
This file is part of pif-tdpony.
pif-tdpony is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the free Software Foundation, either version 3 of the License, or (at your option) any later version.
The PWRTelegram API is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License along with pif-tdpony.
If not, see <http://www.gnu.org/licenses/>.
*/
2018-03-03 16:09:03 +01:00
#include <td/telegram/td_json_client.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 <string>
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-03-03 16:09:03 +01:00
void __construct(Php::Parameters &params) {
Php::Value self(this);
2018-03-06 15:29:54 +01:00
self["tdlibParameters"]["@type"] = "setTdlibParameters";
self["tdlibParameters"]["parameters"] = params[0];
2018-02-17 12:02:43 +01:00
initTdlib();
}
void __wakeup() {
initTdlib();
2018-03-01 01:49:03 +01:00
}
void __destruct() {
deinitTdlib();
2018-02-17 12:02:43 +01:00
}
void initTdlib() {
2018-03-03 16:09:03 +01:00
client = td_json_client_create();
Php::Value self(this);
2018-03-06 15:29:54 +01:00
td_json_client_execute(client, json_encode(self["tdlibParameters"]));
2018-01-01 17:01:56 +01:00
}
2018-02-17 12:02:43 +01:00
void deinitTdlib() {
2018-03-03 16:09:03 +01:00
td_json_client_destroy(client);
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-03 19:38:45 +01:00
td_json_client_send(client, json_encode(value));
2018-01-01 17:01:56 +01:00
}
2018-03-01 01:49:03 +01:00
Php::Value receive(Php::Parameters &params) {
2018-03-03 19:38:45 +01:00
return json_decode(td_json_client_receive(client, params[0]));
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-03 19:38:45 +01:00
return json_decode(td_json_client_execute(client, json_encode(value)));
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:
2018-03-03 16:09:03 +01:00
void *client;
2018-03-03 19:38:45 +01:00
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);
}
2018-03-01 01:49:03 +01:00
};
2018-01-01 17:01:56 +01:00
2018-03-03 16:09:03 +01:00
class Logger : public Php::Base
2018-03-01 01:49:03 +01:00
{
public:
2018-03-03 16:09:03 +01:00
Logger() = default;
virtual ~Logger() = default;
2018-03-01 01:49:03 +01:00
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-03 16:09:03 +01:00
api.method<&API::__construct>("__construct", Php::Public | Php::Final, {Php::ByVal("tdlibParameters", Php::Type::Array)});
2018-03-01 01:49:03 +01:00
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);
2018-03-03 16:09:03 +01:00
api.method<&API::receive>("receive", Php::Public | Php::Final, {Php::ByVal("timeout", Php::Type::Float)});
2018-03-01 01:49:03 +01:00
api.method<&API::execute>("execute", Php::Public | Php::Final);
2018-02-15 22:33:15 +01:00
2018-03-03 16:09:03 +01:00
api.property("tdlibParameters", nullptr, Php::Private);
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-03 16:09:03 +01:00
Php::Class<Logger> logger("Logger");
2018-03-01 01:49:03 +01:00
2018-03-03 16:09:03 +01:00
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)});
2018-02-15 22:33:15 +01:00
2018-02-17 12:02:43 +01:00
X.add(std::move(api));
2018-03-03 16:09:03 +01:00
X.add(std::move(logger));
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;
}
}