2022-03-15 18:05:33 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
2021-03-18 14:56:35 +08:00
|
|
|
|
use Swoole\Atomic;
|
2020-11-03 21:02:24 +08:00
|
|
|
|
use Swoole\Coroutine;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
use Swoole\Coroutine as Co;
|
|
|
|
|
|
use Swoole\Coroutine\System;
|
2021-03-18 14:56:35 +08:00
|
|
|
|
use Swoole\WebSocket\Server;
|
2021-03-24 23:34:46 +08:00
|
|
|
|
use Symfony\Component\VarDumper\VarDumper;
|
2021-06-16 00:17:30 +08:00
|
|
|
|
use ZM\API\OneBotV11;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use ZM\Config\ZMConfig;
|
2020-12-14 01:24:34 +08:00
|
|
|
|
use ZM\ConnectionManager\ManagerGM;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use ZM\Console\Console;
|
2020-09-29 15:07:43 +08:00
|
|
|
|
use ZM\Context\Context;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
use ZM\Context\ContextInterface;
|
2020-09-29 15:07:43 +08:00
|
|
|
|
use ZM\Event\EventManager;
|
2020-12-14 01:24:34 +08:00
|
|
|
|
use ZM\Exception\RobotNotFoundException;
|
|
|
|
|
|
use ZM\Exception\ZMException;
|
2022-03-17 19:42:59 +08:00
|
|
|
|
use ZM\Exception\ZMKnownException;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use ZM\Framework;
|
2020-12-14 01:24:34 +08:00
|
|
|
|
use ZM\Store\LightCacheInside;
|
2021-03-18 14:56:35 +08:00
|
|
|
|
use ZM\Store\ZMAtomic;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
use ZM\Store\ZMBuf;
|
|
|
|
|
|
use ZM\Utils\DataProvider;
|
2020-05-02 23:27:26 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function getClassPath($class_name)
|
|
|
|
|
|
{
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$dir = str_replace('\\', '/', $class_name);
|
|
|
|
|
|
$dir2 = DataProvider::getSourceRootDir() . '/src/' . $dir . '.php';
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// echo "@@@".$dir2.PHP_EOL;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$dir2 = str_replace('\\', '/', $dir2);
|
|
|
|
|
|
if (file_exists($dir2)) {
|
|
|
|
|
|
return $dir2;
|
|
|
|
|
|
}
|
|
|
|
|
|
return null;
|
2020-03-02 16:14:20 +08:00
|
|
|
|
}
|
2022-03-13 22:47:11 +08:00
|
|
|
|
|
2021-11-02 16:01:24 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 检查炸毛框架运行的环境
|
|
|
|
|
|
* @internal
|
|
|
|
|
|
*/
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function _zm_env_check()
|
|
|
|
|
|
{
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (!extension_loaded('swoole')) {
|
|
|
|
|
|
exit(zm_internal_errcode('E00001') . "Can not find swoole extension.\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
if (version_compare(SWOOLE_VERSION, '4.5.0') == -1) {
|
|
|
|
|
|
exit(zm_internal_errcode('E00002') . 'You must install swoole version >= 4.5.0 !');
|
|
|
|
|
|
}
|
|
|
|
|
|
if (version_compare(PHP_VERSION, '7.2') == -1) {
|
|
|
|
|
|
exit(zm_internal_errcode('E00003') . 'PHP >= 7.2 required.');
|
|
|
|
|
|
}
|
|
|
|
|
|
if (version_compare(SWOOLE_VERSION, '4.6.7') < 0 && !extension_loaded('pcntl')) {
|
|
|
|
|
|
Console::error(zm_internal_errcode('E00004') . 'Swoole 版本必须不低于 4.6.7 或 PHP 安装加载了 pcntl 扩展!');
|
|
|
|
|
|
exit();
|
2021-11-02 16:01:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-03-02 16:14:20 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 使用自己定义的万(san)能分割函数
|
|
|
|
|
|
* @param $msg
|
|
|
|
|
|
* @param bool $ban_comma
|
|
|
|
|
|
*/
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function explodeMsg($msg, $ban_comma = false): array
|
|
|
|
|
|
{
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$msg = str_replace(' ', "\n", trim($msg));
|
2020-03-02 16:14:20 +08:00
|
|
|
|
if (!$ban_comma) {
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// $msg = str_replace(",", "\n", $msg);
|
2020-03-02 16:14:20 +08:00
|
|
|
|
$msg = str_replace("\t", "\n", $msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
$msgs = explode("\n", $msg);
|
|
|
|
|
|
$ls = [];
|
2021-06-16 00:17:30 +08:00
|
|
|
|
foreach ($msgs as $v) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (trim($v) == '') {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2020-03-02 16:14:20 +08:00
|
|
|
|
$ls[] = trim($v);
|
|
|
|
|
|
}
|
|
|
|
|
|
return $ls;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-06-16 00:17:30 +08:00
|
|
|
|
/** @noinspection PhpUnused */
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function unicode_decode($str)
|
|
|
|
|
|
{
|
2020-03-02 16:14:20 +08:00
|
|
|
|
return preg_replace_callback('/\\\\u([0-9a-f]{4})/i', function ($matches) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
return mb_convert_encoding(pack('H*', $matches[1]), 'UTF-8', 'UCS-2BE');
|
2020-03-02 16:14:20 +08:00
|
|
|
|
}, $str);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function matchPattern($pattern, $context): bool
|
|
|
|
|
|
{
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (mb_substr($pattern, 0, 1) == '' && mb_substr($context, 0, 1) == '') {
|
2020-03-02 16:14:20 +08:00
|
|
|
|
return true;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (mb_substr($pattern, 0, 1) == '*' && mb_substr($pattern, 1, 1) != '' && mb_substr($context, 0, 1) == '') {
|
2020-03-02 16:14:20 +08:00
|
|
|
|
return false;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (mb_substr($pattern, 0, 1) == mb_substr($context, 0, 1)) {
|
2020-03-02 16:14:20 +08:00
|
|
|
|
return matchPattern(mb_substr($pattern, 1), mb_substr($context, 1));
|
2022-03-15 18:05:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
if (mb_substr($pattern, 0, 1) == '*') {
|
2020-03-02 16:14:20 +08:00
|
|
|
|
return matchPattern(mb_substr($pattern, 1), $context) || matchPattern($pattern, mb_substr($context, 1));
|
2022-03-15 18:05:33 +08:00
|
|
|
|
}
|
2020-03-02 16:14:20 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function split_explode($del, $str, $divide_en = false): array
|
|
|
|
|
|
{
|
2020-03-02 16:14:20 +08:00
|
|
|
|
$str = explode($del, $str);
|
2022-03-15 18:05:33 +08:00
|
|
|
|
for ($i = 0; $i < mb_strlen($str[0]); ++$i) {
|
2020-03-02 16:14:20 +08:00
|
|
|
|
if (
|
2022-03-15 18:05:33 +08:00
|
|
|
|
is_numeric(mb_substr($str[0], $i, 1))
|
|
|
|
|
|
&& (
|
|
|
|
|
|
!is_numeric(mb_substr($str[0], $i - 1, 1))
|
|
|
|
|
|
&& mb_substr($str[0], $i - 1, 1) != ' '
|
|
|
|
|
|
&& ctype_alpha(mb_substr($str[0], $i - 1, 1)) === false
|
2020-03-02 16:14:20 +08:00
|
|
|
|
)
|
|
|
|
|
|
) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$str[0] = mb_substr($str[0], 0, $i) . ' ' . mb_substr($str[0], $i);
|
2020-03-02 16:14:20 +08:00
|
|
|
|
} elseif (
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$divide_en
|
|
|
|
|
|
&& ctype_alnum(mb_substr($str[0], $i, 1))
|
|
|
|
|
|
&& !ctype_alnum(mb_substr($str[0], $i - 1, 1))
|
|
|
|
|
|
&& mb_substr($str[0], $i - 1, 1) != ' '
|
2020-03-02 16:14:20 +08:00
|
|
|
|
) {
|
|
|
|
|
|
$str[0] = mb_substr($str[0], 0, $i) . ' ' . mb_substr($str[0], $i);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
$str = implode($del, $str);
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// echo $str."\n";
|
2020-03-02 16:14:20 +08:00
|
|
|
|
$ls = [];
|
2021-06-16 00:17:30 +08:00
|
|
|
|
foreach (explode($del, $str) as $v) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (trim($v) == '') {
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
2020-03-02 16:14:20 +08:00
|
|
|
|
$ls[] = $v;
|
|
|
|
|
|
}
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// var_dump($ls);
|
2022-03-15 18:05:33 +08:00
|
|
|
|
return $ls == [] ? [''] : $ls;
|
2020-03-02 16:14:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function matchArgs($pattern, $context)
|
|
|
|
|
|
{
|
2020-03-02 16:14:20 +08:00
|
|
|
|
$result = [];
|
|
|
|
|
|
if (matchPattern($pattern, $context)) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (mb_strpos($pattern, '*') === false) {
|
|
|
|
|
|
return [];
|
|
|
|
|
|
}
|
|
|
|
|
|
$exp = explode('*', $pattern);
|
2020-03-02 16:14:20 +08:00
|
|
|
|
$i = 0;
|
|
|
|
|
|
foreach ($exp as $k => $v) {
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// echo "[MATCH$k] " . $v . PHP_EOL;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if ($v == '' && $k == 0) {
|
|
|
|
|
|
continue;
|
2020-03-02 16:14:20 +08:00
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if ($v == '' && $k == count($exp) - 1) {
|
|
|
|
|
|
$context = $context . '^EOL';
|
|
|
|
|
|
$v = '^EOL';
|
|
|
|
|
|
}
|
|
|
|
|
|
$cur_var = '';
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// echo mb_substr($context, $i) . "|" . $v . PHP_EOL;
|
2020-03-02 16:14:20 +08:00
|
|
|
|
$ori = $i;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
while (($a = mb_substr($context, $i, mb_strlen($v))) != $v && $a != '') {
|
2020-03-02 16:14:20 +08:00
|
|
|
|
$cur_var .= mb_substr($context, $i, 1);
|
|
|
|
|
|
++$i;
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($i != $ori || $k == 1 || $k == count($exp) - 1) {
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// echo $cur_var . PHP_EOL;
|
2020-03-02 16:14:20 +08:00
|
|
|
|
$result[] = $cur_var;
|
|
|
|
|
|
}
|
|
|
|
|
|
$i += mb_strlen($v);
|
|
|
|
|
|
}
|
|
|
|
|
|
return $result;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
return false;
|
2020-03-29 16:29:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function connectIsQQ(): bool
|
|
|
|
|
|
{
|
2020-09-29 15:07:43 +08:00
|
|
|
|
return ctx()->getConnection()->getName() == 'qq';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function connectIsDefault(): bool
|
|
|
|
|
|
{
|
2020-09-29 15:07:43 +08:00
|
|
|
|
return ctx()->getConnection()->getName() == 'default';
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function connectIs($type): bool
|
|
|
|
|
|
{
|
2020-09-29 15:07:43 +08:00
|
|
|
|
return ctx()->getConnection()->getName() == $type;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function getAnnotations(): array
|
|
|
|
|
|
{
|
2020-09-29 15:07:43 +08:00
|
|
|
|
$s = debug_backtrace()[1];
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// echo json_encode($s, 128|256);
|
2020-09-29 15:07:43 +08:00
|
|
|
|
$list = [];
|
2021-06-16 00:17:30 +08:00
|
|
|
|
foreach (EventManager::$events as $v) {
|
|
|
|
|
|
foreach ($v as $vs) {
|
2022-03-20 16:23:07 +08:00
|
|
|
|
// echo get_class($vs).": ".$vs->class." => ".$vs->method.PHP_EOL;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if ($vs->class == $s['class'] && $vs->method == $s['function']) {
|
2020-12-14 01:24:34 +08:00
|
|
|
|
$list[get_class($vs)][] = $vs;
|
2020-09-29 15:07:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return $list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function set_coroutine_params($array)
|
|
|
|
|
|
{
|
2020-03-29 16:29:02 +08:00
|
|
|
|
$cid = Co::getCid();
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if ($cid == -1) {
|
|
|
|
|
|
exit(zm_internal_errcode('E00061') . 'Cannot set coroutine params at none coroutine mode.');
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isset(Context::$context[$cid])) {
|
|
|
|
|
|
Context::$context[$cid] = array_merge(Context::$context[$cid], $array);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Context::$context[$cid] = $array;
|
|
|
|
|
|
}
|
2020-09-29 15:07:43 +08:00
|
|
|
|
foreach (Context::$context as $c => $v) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (!Co::exists($c)) {
|
|
|
|
|
|
unset(Context::$context[$c], ZMBuf::$context_class[$c]);
|
|
|
|
|
|
}
|
2020-03-29 16:29:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-17 19:42:59 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @throws ZMKnownException
|
|
|
|
|
|
*/
|
|
|
|
|
|
function context(): ContextInterface
|
2022-03-13 22:47:11 +08:00
|
|
|
|
{
|
2020-05-23 17:23:29 +08:00
|
|
|
|
return ctx();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-17 19:42:59 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @throws ZMKnownException
|
|
|
|
|
|
*/
|
|
|
|
|
|
function ctx(): ContextInterface
|
2022-03-13 22:47:11 +08:00
|
|
|
|
{
|
2020-03-29 16:29:02 +08:00
|
|
|
|
$cid = Co::getCid();
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$c_class = ZMConfig::get('global', 'context_class');
|
2020-09-29 15:07:43 +08:00
|
|
|
|
if (isset(Context::$context[$cid])) {
|
2020-05-23 17:23:29 +08:00
|
|
|
|
return ZMBuf::$context_class[$cid] ?? (ZMBuf::$context_class[$cid] = new $c_class($cid));
|
2020-04-14 23:46:42 +08:00
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
|
Console::debug("未找到当前协程的上下文({$cid}),正在找父进程的上下文");
|
|
|
|
|
|
while (($pcid = Co::getPcid($cid)) !== -1) {
|
|
|
|
|
|
$cid = $pcid;
|
|
|
|
|
|
if (isset(Context::$context[$cid])) {
|
|
|
|
|
|
return ZMBuf::$context_class[$cid] ?? (ZMBuf::$context_class[$cid] = new $c_class($cid));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-03-17 19:42:59 +08:00
|
|
|
|
throw new ZMKnownException(zm_internal_errcode('E00072') . 'Unable to find context environment');
|
2020-04-14 23:46:42 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function onebot_target_id_name($message_type): string
|
|
|
|
|
|
{
|
2022-03-15 18:05:33 +08:00
|
|
|
|
return $message_type == 'group' ? 'group_id' : 'user_id';
|
2020-11-03 21:02:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_sleep($s = 1): bool
|
|
|
|
|
|
{
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (Coroutine::getCid() != -1) {
|
|
|
|
|
|
System::sleep($s);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
usleep($s * 1000 * 1000);
|
|
|
|
|
|
}
|
2020-11-03 21:02:24 +08:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2020-05-23 17:23:29 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_exec($cmd): array
|
|
|
|
|
|
{
|
2021-03-24 23:34:46 +08:00
|
|
|
|
return System::exec($cmd);
|
|
|
|
|
|
}
|
2020-05-23 17:23:29 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_cid()
|
|
|
|
|
|
{
|
2021-03-24 23:34:46 +08:00
|
|
|
|
return Co::getCid();
|
|
|
|
|
|
}
|
2020-05-23 17:23:29 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_yield()
|
|
|
|
|
|
{
|
2021-03-24 23:34:46 +08:00
|
|
|
|
Co::yield();
|
|
|
|
|
|
}
|
2020-05-23 17:23:29 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_resume(int $cid)
|
|
|
|
|
|
{
|
2021-03-24 23:34:46 +08:00
|
|
|
|
Co::resume($cid);
|
|
|
|
|
|
}
|
2020-05-23 17:23:29 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_timer_after($ms, callable $callable)
|
|
|
|
|
|
{
|
2021-03-13 15:16:10 +08:00
|
|
|
|
Swoole\Timer::after($ms, function () use ($callable) {
|
|
|
|
|
|
call_with_catch($callable);
|
2020-05-23 17:23:29 +08:00
|
|
|
|
});
|
2020-03-29 16:29:02 +08:00
|
|
|
|
}
|
2020-05-23 17:23:29 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function call_with_catch($callable)
|
|
|
|
|
|
{
|
2021-03-13 15:16:10 +08:00
|
|
|
|
try {
|
|
|
|
|
|
$callable();
|
|
|
|
|
|
} catch (Exception $e) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$error_msg = $e->getMessage() . ' at ' . $e->getFile() . '(' . $e->getLine() . ')';
|
|
|
|
|
|
Console::error(zm_internal_errcode('E00033') . 'Uncaught exception ' . get_class($e) . ': ' . $error_msg);
|
2021-03-13 15:16:10 +08:00
|
|
|
|
Console::trace();
|
|
|
|
|
|
} catch (Error $e) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$error_msg = $e->getMessage() . ' at ' . $e->getFile() . '(' . $e->getLine() . ')';
|
|
|
|
|
|
Console::error(zm_internal_errcode('E00033') . 'Uncaught ' . get_class($e) . ': ' . $error_msg);
|
2021-03-13 15:16:10 +08:00
|
|
|
|
Console::trace();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_timer_tick($ms, callable $callable)
|
|
|
|
|
|
{
|
2021-02-15 15:15:26 +08:00
|
|
|
|
if (zm_cid() === -1) {
|
|
|
|
|
|
return go(function () use ($ms, $callable) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
Console::debug('Adding extra timer tick of ' . $ms . ' ms');
|
2021-03-24 23:34:46 +08:00
|
|
|
|
Swoole\Timer::tick($ms, function () use ($callable) {
|
|
|
|
|
|
call_with_catch($callable);
|
|
|
|
|
|
});
|
2021-02-15 15:15:26 +08:00
|
|
|
|
});
|
|
|
|
|
|
} else {
|
2021-03-24 23:34:46 +08:00
|
|
|
|
return Swoole\Timer::tick($ms, function () use ($callable) {
|
|
|
|
|
|
call_with_catch($callable);
|
|
|
|
|
|
});
|
2021-02-15 15:15:26 +08:00
|
|
|
|
}
|
2020-05-23 17:23:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_go(callable $callable)
|
|
|
|
|
|
{
|
2021-03-29 15:34:24 +08:00
|
|
|
|
call_with_catch($callable);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_data_hash($v): string
|
|
|
|
|
|
{
|
2022-03-15 18:05:33 +08:00
|
|
|
|
return md5($v['user_id'] . '^' . $v['self_id'] . '^' . $v['message_type'] . '^' . ($v[$v['message_type'] . '_id'] ?? $v['user_id']));
|
2020-09-29 15:07:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function server(): ?Server
|
|
|
|
|
|
{
|
2020-09-29 15:07:43 +08:00
|
|
|
|
return Framework::$server;
|
2020-08-31 10:11:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取缓存当前框架pid的临时目录
|
|
|
|
|
|
*/
|
|
|
|
|
|
function _zm_pid_dir(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
global $_ZM_HASH;
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (!isset($_ZM_HASH)) {
|
|
|
|
|
|
$_ZM_HASH = md5(DataProvider::getWorkingDir());
|
|
|
|
|
|
}
|
2022-03-13 22:47:11 +08:00
|
|
|
|
return '/tmp/.zm_' . $_ZM_HASH;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-14 01:24:34 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @throws RobotNotFoundException
|
|
|
|
|
|
* @throws ZMException
|
2022-03-15 18:05:33 +08:00
|
|
|
|
* @return OneBotV11
|
2020-12-14 01:24:34 +08:00
|
|
|
|
*/
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function bot()
|
|
|
|
|
|
{
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if (($conn = LightCacheInside::get('connect', 'conn_fd')) == -2) {
|
2021-06-16 00:17:30 +08:00
|
|
|
|
return OneBotV11::getRandom();
|
2020-12-14 01:24:34 +08:00
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
|
if ($conn != -1) {
|
|
|
|
|
|
if (($obj = ManagerGM::get($conn)) !== null) {
|
|
|
|
|
|
return new OneBotV11($obj);
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new RobotNotFoundException('单机器人连接模式可能连接了多个机器人!');
|
|
|
|
|
|
}
|
|
|
|
|
|
throw new RobotNotFoundException('没有任何机器人连接到框架!');
|
2020-12-14 01:24:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-25 16:40:02 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取同类型所有连接的文件描述符 ID
|
2020-12-25 16:53:44 +08:00
|
|
|
|
* @author 854854321
|
2020-12-25 16:40:02 +08:00
|
|
|
|
*/
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function getAllFdByConnectType(string $type = 'default'): array
|
|
|
|
|
|
{
|
2020-12-25 16:40:02 +08:00
|
|
|
|
$fds = [];
|
|
|
|
|
|
foreach (ManagerGM::getAllByName($type) as $obj) {
|
2020-12-25 16:41:14 +08:00
|
|
|
|
$fds[] = $obj->getFd();
|
2020-12-25 15:48:41 +08:00
|
|
|
|
}
|
2020-12-25 16:40:02 +08:00
|
|
|
|
return $fds;
|
2020-12-25 15:48:41 +08:00
|
|
|
|
}
|
2021-02-07 11:46:42 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_atomic($name): ?Atomic
|
|
|
|
|
|
{
|
2021-03-18 14:56:35 +08:00
|
|
|
|
return ZMAtomic::get($name);
|
2021-02-21 22:17:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function uuidgen($uppercase = false): string
|
|
|
|
|
|
{
|
2021-02-21 22:17:34 +08:00
|
|
|
|
try {
|
|
|
|
|
|
$data = random_bytes(16);
|
|
|
|
|
|
} catch (Exception $e) {
|
2022-03-15 18:05:33 +08:00
|
|
|
|
return '';
|
2021-02-21 22:17:34 +08:00
|
|
|
|
}
|
2022-03-15 18:05:33 +08:00
|
|
|
|
$data[6] = chr(ord($data[6]) & 0x0F | 0x40);
|
|
|
|
|
|
$data[8] = chr(ord($data[8]) & 0x3F | 0x80);
|
2021-02-21 22:17:34 +08:00
|
|
|
|
return $uppercase ? strtoupper(vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4))) :
|
|
|
|
|
|
vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
|
2021-02-24 23:37:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function working_dir()
|
|
|
|
|
|
{
|
2021-03-27 16:30:15 +08:00
|
|
|
|
return WORKING_DIR;
|
2021-02-24 23:37:00 +08:00
|
|
|
|
}
|
2021-03-24 23:34:46 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_dump($var, ...$moreVars)
|
|
|
|
|
|
{
|
2021-03-24 23:34:46 +08:00
|
|
|
|
VarDumper::dump($var);
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($moreVars as $v) {
|
|
|
|
|
|
VarDumper::dump($v);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (1 < func_num_args()) {
|
|
|
|
|
|
return func_get_args();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $var;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_info($obj)
|
|
|
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
|
Console::info($obj);
|
|
|
|
|
|
}
|
2021-03-24 23:34:46 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_warning($obj)
|
|
|
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
|
Console::warning($obj);
|
|
|
|
|
|
}
|
2021-03-24 23:34:46 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_success($obj)
|
|
|
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
|
Console::success($obj);
|
|
|
|
|
|
}
|
2021-03-24 23:34:46 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_debug($obj)
|
|
|
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
|
Console::debug($obj);
|
|
|
|
|
|
}
|
2021-03-24 23:34:46 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_verbose($obj)
|
|
|
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
|
Console::verbose($obj);
|
|
|
|
|
|
}
|
2021-03-24 23:34:46 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_error($obj)
|
|
|
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
|
Console::error($obj);
|
|
|
|
|
|
}
|
2021-03-24 23:34:46 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_config($name, $key = null)
|
|
|
|
|
|
{
|
2021-11-02 16:01:24 +08:00
|
|
|
|
return ZMConfig::get($name, $key);
|
|
|
|
|
|
}
|
2021-03-25 16:18:09 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function quick_reply_closure($reply)
|
|
|
|
|
|
{
|
2021-06-16 00:17:30 +08:00
|
|
|
|
return function () use ($reply) {
|
2021-03-25 16:18:09 +08:00
|
|
|
|
return $reply;
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2021-06-16 00:17:30 +08:00
|
|
|
|
|
2022-03-13 22:47:11 +08:00
|
|
|
|
function zm_internal_errcode($code): string
|
|
|
|
|
|
{
|
2022-03-15 18:05:33 +08:00
|
|
|
|
return "[ErrCode:{$code}] ";
|
2021-06-16 00:17:30 +08:00
|
|
|
|
}
|