From 2d6aac1c7e3a0ebc62a73b7b01f44b81abb8ff76 Mon Sep 17 00:00:00 2001 From: Hearot Date: Wed, 11 Oct 2017 16:05:00 +0200 Subject: [PATCH] Initialize the project --- .gitignore | 7 ++ composer.json | 28 +++++++ index.php | 30 ++++++++ src/hearot/Functions/MadelineFunctions.php | 44 ----------- src/hearot/MadelineClient/MadelineClient.php | 79 ++++++++++++++++---- start_madelineclient.php | 0 6 files changed, 130 insertions(+), 58 deletions(-) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 index.php delete mode 100644 src/hearot/Functions/MadelineFunctions.php delete mode 100644 start_madelineclient.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..000d528 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Do not save important files. + +/vendor +vendor +composer.lock +*.madeline +*.madeline.lock diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d4c841f --- /dev/null +++ b/composer.json @@ -0,0 +1,28 @@ +{ + "name": "hearot/madelineclient", + "description": "A client for MadelineProto", + "license": "AGPLv3", + "authors": [ + { + "name": "hearot", + "email": "gabriel@hearot.it" + } + ], + "type": "Client Interface", + "require": { + "danog/madelineproto": "dev-master", + "rych/bytesize": "^1.0" + }, + "autoload": { + "psr-0": { + "hearot\\MadelineClient\\": "src/" + } + }, + "repositories": [ + { + "type": "git", + "url": "https://github.com/danog/phpseclib" + } + ], + "minimum-stability": "dev" +} diff --git a/index.php b/index.php new file mode 100644 index 0000000..b7cfdd2 --- /dev/null +++ b/index.php @@ -0,0 +1,30 @@ +#!/usr/bin/env php +. +*/ +try { + include __DIR__ . '/vendor/autoload.php'; + $parameters = []; + if (isset($argv[1])) { + $parameters['type'] = $argv[1]; + } else { + $parameters['type'] = ''; + } + if (isset($argv[2])) { + $parameters['file'] = $argv[2]; + } else { + $parameters['file'] = ''; + } + new \hearot\MadelineClient\MadelineClient($parameters); +} catch (Exception $e) { + echo 'Error: ' . $e->getMessage() . PHP_EOL; +} +__HALT_COMPILER(); diff --git a/src/hearot/Functions/MadelineFunctions.php b/src/hearot/Functions/MadelineFunctions.php deleted file mode 100644 index 31fb965..0000000 --- a/src/hearot/Functions/MadelineFunctions.php +++ /dev/null @@ -1,44 +0,0 @@ -. +*/ namespace hearot\MadelineClient; -class Client +if (!function_exists('readline')) { + function readline($prompt = null) + { + if ($prompt) { + echo $prompt; + } + $fp = fopen('php://stdin', 'r'); + $line = rtrim(fgets($fp, 1024)); + return $line; + } +} +function latinreadline($message) +{ + do { + if (isset($res)) { + echo 'Only latin letters, numbers, dashes (-) and underscores (_) can be used!' . PHP_EOL; + } + $res = readline($message); + } while (!preg_match('/^[A-Za-z0-9_.-]+$/', $res)); + return $res; +} +function platinreadline($message) +{ + do { + if (isset($res)) { + echo 'A project with this name already exists!' . PHP_EOL; + } + $res = latinreadline($message); + } while (file_exists($res)); + return $res; +} + +function emailreadline($message) +{ + do { + if (isset($res)) { + echo 'This is not a valid email!' . PHP_EOL; + } + $res = readline($message); + } while (!filter_var($res, FILTER_VALIDATE_EMAIL)); + + return $res; +} + +class MadelineClient { const MADELINECLIENT_DEFAULT_SETTINGS = '{"app_info":{"api_id":6,"api_hash":"eb06d4abfb49dc3eeb1aeb98ae0f581e"}}'; const MADELINECLIENT_VERSION = '1.0'; - public function __construct($settings = '{"app_info":{"api_id":6,"api_hash":"eb06d4abfb49dc3eeb1aeb98ae0f581e"}}') + public function __construct($parameters, $settings = '{"app_info":{"api_id":6,"api_hash":"eb06d4abfb49dc3eeb1aeb98ae0f581e"}}') { global $argv; - $this->settings = (is_array($settings)) ? $settings : null; - if (in_array($argv[1], ['-v', '-version', '-ver'])) { - echo 'MadelineProto CLI Version ' . MADELINECLIENT_VERSION . "\nDeveloped by Hearot, MadelineProto by Daniil Gentili.\nCopyright 2017" . PHP_EOL; + $this->settings = (is_string($settings)) ? $settings : null; + if (in_array($parameters['type'], ['-v', '-version', '-ver'])) { + echo 'MadelineProto CLI Version ' . self::MADELINECLIENT_VERSION . "\nDeveloped by Hearot, MadelineProto by Daniil Gentili.\nCopyright 2017" . PHP_EOL; exit; - } elseif (in_array($argv[1], ['-load', '-l'])) { - $this->load($argv[2]); + } elseif (in_array($parameters['type'], ['-load', '-l'])) { + $this->load($parameters['file']); exit; - } elseif (in_array($argv[1], ['-n', '-new', '-novo'])) { + } elseif (in_array($parameters['type'], ['-n', '-new', '-novo'])) { $this->new(); exit; } else { @@ -41,7 +95,7 @@ class Client switch (strtolower($r)) { case 'version': case '-v': - echo 'MadelineProto CLI Version ' . $this->MADELINECLIENT_VERSION . "\nDeveloped by Hearot, MadelineProto by Daniil Gentili.\nCopyright 2017" . PHP_EOL; + echo 'MadelineProto CLI Version ' . self::MADELINECLIENT_VERSION . "\nDeveloped by Hearot, MadelineProto by Daniil Gentili.\nCopyright 2017" . PHP_EOL; continue 2; break; case 'exit': @@ -97,7 +151,6 @@ class Client echo PHP_EOL; echo PHP_EOL; $project_composer = ''; - foreach (str_split($project) as $c) { if (ctype_upper($c)) { $project_composer .= '-' . strtolower($c); @@ -105,11 +158,9 @@ class Client $project_composer .= $c; } } - $res = ['name' => $nick . '/' . $project_composer, 'description' => $description, 'license' => 'AGPLv3', 'authors' => [['name' => $name, 'email' => $email]], 'type' => $project, 'require' => ['danog/madelineproto' => 'dev-master'], 'autoload' => ['psr-0' => [$nick . '\\' . $project . '\\' => 'src/']], 'repositories' => [['type' => 'git', 'url' => 'https://github.com/danog/phpseclib']], 'minimum-stability' => 'dev']; - mkdir($project); mkdir($project . '/src'); mkdir($project . '/src/' . $nick); @@ -129,7 +180,7 @@ class Client private function new_session() { if (readline('Do you want to create a new session? (y/n) ') == 'y') { - $MadelineProto = new \danog\MadelineProto\API(json_decode($this->settings, true)); + $MadelineProto = new \danog\MadelineProto\API(json_decode('{"app_info":{"api_id":6,"api_hash":"eb06d4abfb49dc3eeb1aeb98ae0f581e"}}', true)); if (readline('Do you want to create a bot or a userbot? (bot/userbot) ') == 'bot') { $MadelineProto->bot_login(readline('Enter the token of the bot: ')); } else { @@ -164,7 +215,7 @@ class Client switch (strtolower($r)) { case 'version': case '-v': - echo 'MadelineProto CLI Version ' . $this->MADELINECLIENT_VERSION . "\nDeveloped by Hearot, MadelineProto by Daniil Gentili.\nCopyright 2017" . PHP_EOL; + echo 'MadelineProto CLI Version ' . self::MADELINECLIENT_VERSION . "\nDeveloped by Hearot, MadelineProto by Daniil Gentili.\nCopyright 2017" . PHP_EOL; continue 2; break; case 'exit': diff --git a/start_madelineclient.php b/start_madelineclient.php deleted file mode 100644 index e69de29..0000000