Skip to content

Commit

Permalink
Recursive session search
Browse files Browse the repository at this point in the history
  • Loading branch information
xtrime-ru committed Mar 28, 2020
1 parent ca740ab commit e296836
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 59 deletions.
5 changes: 3 additions & 2 deletions server.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php

use TelegramApiServer\Files;
use TelegramApiServer\Migrations\SessionsMigration;
use TelegramApiServer\Migrations\SwooleToAmpMigration;

Expand Down Expand Up @@ -68,10 +69,10 @@
throw new InvalidArgumentException('Session name specified as directory');
}

$session = TelegramApiServer\Client::getSessionFile($session);
$session = Files::getSessionFile($session);

if (preg_match('~[' . preg_quote('*?[]!', '~') . ']~', $session)) {
$sessions = glob($session);
$sessions = Files::globRecursive($session);
} else {
$sessions[] = $session;
}
Expand Down
52 changes: 4 additions & 48 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,9 @@

class Client
{
public static string $sessionExtension = '.madeline';
public static string $sessionFolder = 'sessions';
/** @var MadelineProto\API[] */
public array $instances = [];

/**
* @param string|null $session
*
* @return string|null
*/
public static function getSessionFile(?string $session): ?string
{
if (!$session) {
return null;
}
$session = trim(trim($session), '/');
$session = static::$sessionFolder . '/' . $session . static::$sessionExtension;
$session = str_replace('//', '/', $session);
return $session;
}

public static function getSessionName(?string $sessionFile): ?string
{
if (!$sessionFile) {
return null;
}

preg_match(
'~' . static::$sessionFolder . "/(?'sessionName'.*?)" . static::$sessionExtension . '$~',
$sessionFile,
$matches
);

return $matches['sessionName'] ?? null;
}

public static function checkOrCreateSessionFolder(string $session): void
{
$directory = dirname($session);
if ($directory && $directory !== '.' && !is_dir($directory)) {
$parentDirectoryPermissions = fileperms(ROOT_DIR);
if (!mkdir($directory, $parentDirectoryPermissions, true) && !is_dir($directory)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $directory));
}
}
}

private static function isSessionLoggedIn(MadelineProto\API $instance): bool
{
return ($instance->API->authorized ?? MTProto::NOT_LOGGED_IN) === MTProto::LOGGED_IN;
Expand All @@ -71,7 +27,7 @@ public function connect(array $sessionFiles): void
warning(PHP_EOL . 'Starting MadelineProto...' . PHP_EOL);

foreach ($sessionFiles as $file) {
$sessionName = static::getSessionName($file);
$sessionName = Files::getSessionName($file);
$instance = $this->addSession($sessionName);
$this->runSession($instance);
}
Expand All @@ -90,8 +46,8 @@ public function addSession(string $session, array $settings = []): MadelineProto
if (isset($this->instances[$session])) {
throw new InvalidArgumentException('Session already exists');
}
$file = static::getSessionFile($session);
static::checkOrCreateSessionFolder($file);
$file = Files::getSessionFile($session);
Files::checkOrCreateSessionFolder($file);
$settings = array_replace_recursive((array) Config::getInstance()->get('telegram'), $settings);
$instance = new MadelineProto\API($file, $settings);
$instance->async(true);
Expand Down Expand Up @@ -190,7 +146,7 @@ function() use ($instance) {

private function loop(MadelineProto\API $instance, callable $callback = null): void
{
$sessionName = static::getSessionName($instance->session);
$sessionName = Files::getSessionName($instance->session);
try {
$callback ? $instance->loop($callback) : $instance->loop();
} catch (\Throwable $e) {
Expand Down
4 changes: 2 additions & 2 deletions src/EventObservers/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace TelegramApiServer\EventObservers;

use danog\MadelineProto\APIWrapper;
use TelegramApiServer\Client;
use TelegramApiServer\Files;

class EventHandler extends \danog\MadelineProto\EventHandler
{
Expand All @@ -12,7 +12,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler

public function __construct(APIWrapper $MadelineProto)
{
$this->sessionName = Client::getSessionName($MadelineProto->session);
$this->sessionName = Files::getSessionName($MadelineProto->session);
if (empty(static::$instances[$this->sessionName])) {
static::$instances[$this->sessionName] = true;
parent::__construct($MadelineProto);
Expand Down
62 changes: 62 additions & 0 deletions src/Files.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace TelegramApiServer;

class Files
{

public static string $sessionExtension = '.madeline';
public static string $sessionFolder = 'sessions';

public static function checkOrCreateSessionFolder(string $session): void
{
$directory = dirname($session);
if ($directory && $directory !== '.' && !is_dir($directory)) {
$parentDirectoryPermissions = fileperms(ROOT_DIR);
if (!mkdir($directory, $parentDirectoryPermissions, true) && !is_dir($directory)) {
throw new RuntimeException(sprintf('Directory "%s" was not created', $directory));
}
}
}

public static function getSessionName(?string $sessionFile): ?string
{
if (!$sessionFile) {
return null;
}

preg_match(
'~' . Files::$sessionFolder . "/(?'sessionName'.*?)" . Files::$sessionExtension . '$~',
$sessionFile,
$matches
);

return $matches['sessionName'] ?? null;
}

/**
* @param string|null $session
*
* @return string|null
*/
public static function getSessionFile(?string $session): ?string
{
if (!$session) {
return null;
}
$session = trim(trim($session), '/');
$session = Files::$sessionFolder . '/' . $session . Files::$sessionExtension;
$session = str_replace('//', '/', $session);
return $session;
}

public static function globRecursive($pattern, $flags = 0): array
{
$files = glob($pattern, $flags) ?: [];
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$files = [...$files, ...static::globRecursive($dir.'/'.basename($pattern), $flags)];
}
return $files;
}

}
5 changes: 3 additions & 2 deletions src/MadelineProtoExtensions/SystemApiExtensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use danog\MadelineProto;
use danog\MadelineProto\MTProto;
use TelegramApiServer\Client;
use TelegramApiServer\Files;
use function Amp\call;

class SystemApiExtensions
Expand Down Expand Up @@ -60,7 +61,7 @@ public function getSessionList(): array

$sessions[$session] = [
'session' => $session,
'file' => Client::getSessionFile($session),
'file' => Files::getSessionFile($session),
'status' => $status,
];
}
Expand All @@ -74,7 +75,7 @@ public function getSessionList(): array
public function removeSessionFile($session)
{
return call(static function() use($session) {
$file = Client::getSessionFile($session);
$file = Files::getSessionFile($session);
if (is_file($file)) {
yield \Amp\File\unlink($file);
yield \Amp\File\unlink($file . '.lock');
Expand Down
10 changes: 5 additions & 5 deletions src/Migrations/SessionsMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@

namespace TelegramApiServer\Migrations;

use TelegramApiServer\Client;
use TelegramApiServer\Files;

class SessionsMigration
{
public static function move($rootDir = ROOT_DIR)
{
foreach (glob("$rootDir/*" . Client::$sessionExtension) as $oldFile) {
foreach (glob("$rootDir/*" . Files::$sessionExtension) as $oldFile) {
preg_match(
'~^' . "{$rootDir}(?'session'.*)" . preg_quote(Client::$sessionExtension, '\\') . '$~',
'~^' . "{$rootDir}(?'session'.*)" . preg_quote(Files::$sessionExtension, '\\') . '$~',
$oldFile,
$matches
);

if ($session = $matches['session'] ?? null) {
$session = Client::getSessionFile($session);
Client::checkOrCreateSessionFolder($session);
$session = Files::getSessionFile($session);
Files::checkOrCreateSessionFolder($session);

rename($oldFile, "{$rootDir}/{$session}");
rename("{$oldFile}.lock", "{$rootDir}/{$session}.lock");
Expand Down

0 comments on commit e296836

Please sign in to comment.