From f1f9f70e40230731561509dcdf45c503bf56f880 Mon Sep 17 00:00:00 2001 From: Sayan Biswas Date: Tue, 28 Dec 2021 23:59:43 +0530 Subject: [PATCH] make a super basic arg and time parse method in Utilities class --- botsrc/SynicalBot/Utilities.php | 298 +++++++++++++++++--------------- 1 file changed, 158 insertions(+), 140 deletions(-) diff --git a/botsrc/SynicalBot/Utilities.php b/botsrc/SynicalBot/Utilities.php index 7a2e60a..400ebae 100644 --- a/botsrc/SynicalBot/Utilities.php +++ b/botsrc/SynicalBot/Utilities.php @@ -1,146 +1,164 @@ getText(true))) == 0 && - $message->getReplyToMessage() == null - ) - { - return null; - } - - if($message->getText(true) !== null && strlen($message->getText(true)) > 0) - { - $Username = trim(str_ireplace('@', (string)null, $message->getText(true))); - - if(strlen($Username) > 0) - { - try - { - return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient( - TelegramClientSearchMethod::byUsername, $Username - ); - } - catch(Exception $e) - { - unset($e); - } - } - - if(stripos(strtoupper(trim($message->getText(true))), 'TEL-') !== false) - { - try - { - return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient( - TelegramClientSearchMethod::byPublicId, trim($message->getText(true)) - ); - } - catch(Exception $e) - { - unset($e); - } - } - } - - if($clients->ReplyToUserClient !== null) - { - if($clients->ReplyToSenderChatClient !== null) - return $clients->ReplyToSenderChatClient; - - return $clients->ReplyToUserClient; - } - - if($clients->MentionUserClients !== null) - { - if(count($clients->MentionUserClients) > 0) - return $clients->MentionUserClients[array_keys($clients->MentionUserClients)[0]]; - } - - if($reply_only == false) - { - if($clients->SenderChatClient !== null) - return $clients->SenderChatClient; - - if($clients->UserClient !== null) - return $clients->UserClient; - } - - // Final attempt with pop - if($message->getText(true) !== null && strlen($message->getText(true)) > 0) - { - // NOTE: Argument parsing is done with pop now. - $options = pop::parse($message->getText(true)); - - $TargetTelegramParameter = array_values($options)[(count($options)-1)]; - if(is_bool($TargetTelegramParameter)) - { - $TargetTelegramParameter = array_keys($options)[(count($options)-1)]; - } - - $EstimatedPrivateID = Hashing::telegramClientPublicID((int)$TargetTelegramParameter, (int)$TargetTelegramParameter); - - try - { - return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient( - TelegramClientSearchMethod::byPublicId, $EstimatedPrivateID - ); - } - catch(TelegramClientNotFoundException $e) - { - unset($e); - } - - try - { - return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient( - TelegramClientSearchMethod::byPublicId, $TargetTelegramParameter - ); - } - catch(TelegramClientNotFoundException $e) - { - unset($e); - } - - try - { - return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient( - TelegramClientSearchMethod::byUsername, str_ireplace('@', (string)null, $TargetTelegramParameter) - ); - } - catch(TelegramClientNotFoundException $e) - { - unset($e); - } - } - - throw new SynicalBot\Exceptions\CannotFindTargetUserException('Cannot resolve user ' . $message->getText(true)); + if ( + strlen(trim($message->getText(true))) == 0 && + $message->getReplyToMessage() == null + ) { + return null; } - } \ No newline at end of file + + if ($message->getText(true) !== null && strlen($message->getText(true)) > 0) { + $Username = trim(str_ireplace('@', (string)null, $message->getText(true))); + + if (strlen($Username) > 0) { + try { + return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient( + TelegramClientSearchMethod::byUsername, $Username + ); + } catch (Exception $e) { + unset($e); + } + } + + if (stripos(strtoupper(trim($message->getText(true))), 'TEL-') !== false) { + try { + return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient( + TelegramClientSearchMethod::byPublicId, trim($message->getText(true)) + ); + } catch (Exception $e) { + unset($e); + } + } + } + + if ($clients->ReplyToUserClient !== null) { + if ($clients->ReplyToSenderChatClient !== null) + return $clients->ReplyToSenderChatClient; + + return $clients->ReplyToUserClient; + } + + if ($clients->MentionUserClients !== null) { + if (count($clients->MentionUserClients) > 0) + return $clients->MentionUserClients[array_keys($clients->MentionUserClients)[0]]; + } + + if ($reply_only == false) { + if ($clients->SenderChatClient !== null) + return $clients->SenderChatClient; + + if ($clients->UserClient !== null) + return $clients->UserClient; + } + + // Final attempt with pop + if ($message->getText(true) !== null && strlen($message->getText(true)) > 0) { + // NOTE: Argument parsing is done with pop now. + $options = pop::parse($message->getText(true)); + + $TargetTelegramParameter = array_values($options)[(count($options) - 1)]; + if (is_bool($TargetTelegramParameter)) { + $TargetTelegramParameter = array_keys($options)[(count($options) - 1)]; + } + + $EstimatedPrivateID = Hashing::telegramClientPublicID((int)$TargetTelegramParameter, (int)$TargetTelegramParameter); + + try { + return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient( + TelegramClientSearchMethod::byPublicId, $EstimatedPrivateID + ); + } catch (TelegramClientNotFoundException $e) { + unset($e); + } + + try { + return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient( + TelegramClientSearchMethod::byPublicId, $TargetTelegramParameter + ); + } catch (TelegramClientNotFoundException $e) { + unset($e); + } + + try { + return SynicalBot::getTelegramClientManager()->getTelegramClientManager()->getClient( + TelegramClientSearchMethod::byUsername, str_ireplace('@', (string)null, $TargetTelegramParameter) + ); + } catch (TelegramClientNotFoundException $e) { + unset($e); + } + } + + throw new SynicalBot\Exceptions\CannotFindTargetUserException('Cannot resolve user ' . $message->getText(true)); + } + + /** + * Returns the text other than the mention and command entity itself + * @param Message $message + * @return string|null + */ + public static function parseArg(Message $message): ?string + { + $reply = $message->getReplyToMessage(); + $text = $message->getText(); + if ($reply == null) { + return substr(strstr($text, " "), " ", 2); + } + return $text; + } + + /** + * Extract time from the message object (can be used for getting time for tban, tmute, yada yada + * format: "5d" + * @param Message $message + * @return int|null (time in seconds) + */ + public static function parseTime(Message $message): ?int + { + $toParse = self::parseArg($message); + if ($toParse != null) { + $timeStr = $toParse[0]; + $unitStr = $toParse[1]; + switch ($unitStr) { + case "h": + return intval($timeStr, 10) * 3600; + case "m": + return intval($timeStr, 10) * 60; + case "s": + return intval($timeStr, 10); + } + } + return 0; + } +} \ No newline at end of file