From 9ab379e3c2014e1ccfa9cc6ecef5733554c6e1c9 Mon Sep 17 00:00:00 2001 From: Denny Dai Date: Sat, 30 May 2015 00:20:55 +0800 Subject: [PATCH] Second Pust, Big Change --- BOT.php | 7 +++ class.inc.php | 67 ++++++++++++++++++++ classes/Telegram/Base.php | 51 +++++++++++++++ classes/Telegram/Client.php | 85 +++++++++++++++++++++++++ config.inc.php | 2 + functions.php | 20 ++++++ init.php | 34 ++++++++++ plugins/dzdown.php | 22 +++++++ plugins/echo.php | 5 ++ plugins/exec.php | 6 ++ plugins/help.php | 9 +++ plugins/ipto.php | 12 ++++ plugins/trans.php | 16 +++++ plugins/tts.php | 10 +++ telegram-bot.php | 122 ------------------------------------ 15 files changed, 346 insertions(+), 122 deletions(-) create mode 100644 BOT.php create mode 100644 class.inc.php create mode 100644 classes/Telegram/Base.php create mode 100644 classes/Telegram/Client.php create mode 100644 config.inc.php create mode 100644 functions.php create mode 100644 init.php create mode 100644 plugins/dzdown.php create mode 100644 plugins/echo.php create mode 100644 plugins/exec.php create mode 100644 plugins/help.php create mode 100644 plugins/ipto.php create mode 100644 plugins/trans.php create mode 100644 plugins/tts.php delete mode 100644 telegram-bot.php diff --git a/BOT.php b/BOT.php new file mode 100644 index 0000000..c28b742 --- /dev/null +++ b/BOT.php @@ -0,0 +1,7 @@ + \ No newline at end of file diff --git a/class.inc.php b/class.inc.php new file mode 100644 index 0000000..fe53c73 --- /dev/null +++ b/class.inc.php @@ -0,0 +1,67 @@ +_fp = stream_socket_client($remoteSocket); + if ($this->_fp === false) { + throw new ClientException('Could not connect to socket "' . $remoteSocket . '"'); + } + stream_set_timeout($this->_fp, 1); //This way fgets() returns false if telegram-cli gives us no response. + } + + public function __destruct() + { + fclose($this->_fp); + } + + public function exec($command) + { + fwrite($this->_fp, str_replace("\n", '\n', $command) . PHP_EOL); + + $answer = fgets($this->_fp); //"ANSWER $bytes" if there is a return value or \n if not + if (is_string($answer)) { + if (substr($answer, 0, 7) === 'ANSWER ') { + $bytes = (int) substr($answer, 7); + if ($bytes > 0) { + $string = trim(fread($this->_fp, $bytes + 1)); + + if ($string === 'SUCCESS') { //For "status_online" and "status_offline" + return true; + } + + return $string; + } + } else if ($answer === PHP_EOL) { //For commands like "msg" + return true; + } + } + + return false; + } + + public function encodeUri($var){ + return iconv("gb2312", "UTF-8", $var); + } + + + public function escapeString($var){ + return '"' . addslashes($var) . '"'; + } + + public function escapePeer($peer){ + return str_replace(' ', '_', $peer); + } + + + public function escape($var){ + return escapeString(encodeUri($var)); + } + + public function PluginList($commands, $name){ + global $plugins; + $plugins[] .= TAG.$commands." ".$name; + } +} \ No newline at end of file diff --git a/classes/Telegram/Base.php b/classes/Telegram/Base.php new file mode 100644 index 0000000..b160eb8 --- /dev/null +++ b/classes/Telegram/Base.php @@ -0,0 +1,51 @@ +_fp = stream_socket_client($Socket); + if ($this->_fp === false) { + throw new ClientException('Could not connect to socket "' . $Socket . '"'); + } + stream_set_timeout($this->_fp, 1); + } + + public function __destruct() + { + fclose($this->_fp); + } + + + public function exec($command) + { + fwrite($this->_fp, str_replace("\n", '\n', $command) . PHP_EOL); + + $answer = fgets($this->_fp); + if (is_string($answer)) { + if (substr($answer, 0, 7) === 'ANSWER ') { + $bytes = (int) substr($answer, 7); + if ($bytes > 0) { + $Response = trim(fread($this->_fp, $bytes + 1)); + return $Response; + } + }elseif ($answer === PHP_EOL) { + return true; + } + } + + return false; + } + + public function escapeStringArgument($argument) + { + return '"' . addslashes($argument) . '"'; + } + + public function escapePeer($peer) + { + return str_replace(' ', '_', $peer); + } +} diff --git a/classes/Telegram/Client.php b/classes/Telegram/Client.php new file mode 100644 index 0000000..0dd429e --- /dev/null +++ b/classes/Telegram/Client.php @@ -0,0 +1,85 @@ +exec('status_online'); + } + + public function setStatusOffline() + { + return $this->exec('status_offline'); + } + + + public function msg($peer, $msg) + { + $peer = $this->escapePeer($peer); + $msg = $this->escapeStringArgument($msg); + return $this->exec('msg ' . $peer . ' ' . $msg); + } + + public function addContact($phoneNumber, $firstName, $lastName) + { + $phoneNumber = preg_replace('%[^0-9]%', '', (string) $phoneNumber); + if (empty($phoneNumber)) { + return false; + } + + return $this->exec('add_contact ' . $phoneNumber . ' ' . $this->escapeStringArgument($firstName) + . ' ' . $this->escapeStringArgument($lastName)); + } + + public function renameContact($contact, $firstName, $lastName) + { + return $this->exec('rename_contact ' . $this->escapePeer($contact) + . ' ' . $this->escapeStringArgument($firstName) . ' ' . $this->escapeStringArgument($lastName)); + } + + public function deleteContact($contact) + { + return $this->exec('del_contact ' . $this->escapePeer($contact)); + } + + public function markRead($peer) + { + return $this->exec('mark_read ' . $this->escapePeer($peer)); + } + + public function getContactList() + { + return explode(PHP_EOL, $this->exec('contact_list')); + } + + public function getUserInfo($user) + { + return $this->exec('user_info ' . $this->escapePeer($user)); + } + + public function getDialogList() + { + return explode(PHP_EOL, $this->exec('dialog_list')); + } + + public function getHistory($peer, $limit = null, $offset = null) + { + if ($limit !== null) { + $limit = (int) $limit; + if ($limit < 1) { //if limit is lesser than 1, telegram-cli crashes + $limit = 1; + } + $limit = ' ' . $limit; + } else { + $limit = ''; + } + if ($offset !== null) { + $offset = ' ' . (int) $offset; + } else { + $offset = ''; + } + + return $this->exec('history ' . $this->escapePeer($peer) . $limit . $offset); + } +} diff --git a/config.inc.php b/config.inc.php new file mode 100644 index 0000000..71c5ef7 --- /dev/null +++ b/config.inc.php @@ -0,0 +1,2 @@ +exec("msg Denny_Dai ".json_encode($_GET)); //私聊监测 + } + +//load plugins + $load_plugins = glob(dirname(__FILE__).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'*.php'); + foreach ($load_plugins as $key => $value) { + if ($value == dirname(__FILE__).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'help.php') { + unset($load_plugins[$key]); + } + } + foreach ($load_plugins as $value) { + require_once $value; + } + require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'help.php'; +} \ No newline at end of file diff --git a/plugins/dzdown.php b/plugins/dzdown.php new file mode 100644 index 0000000..96023ae --- /dev/null +++ b/plugins/dzdown.php @@ -0,0 +1,22 @@ +exec("msg ".$from." ".escapeString($msg)); +} \ No newline at end of file diff --git a/plugins/echo.php b/plugins/echo.php new file mode 100644 index 0000000..3f2cdcb --- /dev/null +++ b/plugins/echo.php @@ -0,0 +1,5 @@ +exec("msg ".$from." ".$matches[1]); +} \ No newline at end of file diff --git a/plugins/exec.php b/plugins/exec.php new file mode 100644 index 0000000..c554e91 --- /dev/null +++ b/plugins/exec.php @@ -0,0 +1,6 @@ +msg($from,$msg); +} \ No newline at end of file diff --git a/plugins/help.php b/plugins/help.php new file mode 100644 index 0000000..48eedc5 --- /dev/null +++ b/plugins/help.php @@ -0,0 +1,9 @@ +exec("msg ".$from." ".escapeString($plugin_list)); +} \ No newline at end of file diff --git a/plugins/ipto.php b/plugins/ipto.php new file mode 100644 index 0000000..70e3529 --- /dev/null +++ b/plugins/ipto.php @@ -0,0 +1,12 @@ +exec("msg ".$from." ".escapeString($msg)); +} \ No newline at end of file diff --git a/plugins/trans.php b/plugins/trans.php new file mode 100644 index 0000000..e5a57a1 --- /dev/null +++ b/plugins/trans.php @@ -0,0 +1,16 @@ +exec("msg ".$from." ".escapeString($msg)); + +} \ No newline at end of file diff --git a/plugins/tts.php b/plugins/tts.php new file mode 100644 index 0000000..8bc5825 --- /dev/null +++ b/plugins/tts.php @@ -0,0 +1,10 @@ +exec("send_audio ".$from." /home/wwwroot/1.vps.dennx.com/".$_GET['from'].".mp3"); + //unlink($matches[1].".mp3"); +} \ No newline at end of file diff --git a/telegram-bot.php b/telegram-bot.php deleted file mode 100644 index d77f1da..0000000 --- a/telegram-bot.php +++ /dev/null @@ -1,122 +0,0 @@ -exec("msg Denny_Dai ".json_encode($_GET)); //私聊监测 - } - -//begin! - - #dzdown - $plugins[] .= "#dzdown Discuz! 免积分下载 [#dzdown <附件URL>]"; - if (preg_match("/^#dzdown (.*)$/", $_GET['text'], $matches)) { - $url = str_replace("%3D","=",$matches[0]); - preg_match_all("/(\?|&)aid=([^&?]*)/i",$url,$match0); - $aid=$match0[2][0]; - $aid = base64_decode($aid); - preg_match_all("/\|(.*?)\|/i",$aid,$match1); - if(!empty($_POST['uid'])){ - $uid = '|'.$_POST['uid'].'|'; - }else{ - $uid = '|1|'; - } - $aid = str_replace($match1[0][1], $uid, $aid); - $aid = base64_encode($aid); - $aid = str_replace("=","%3D",$aid); - preg_match_all("#https?://(.*?)($|/)#m",$url,$match2); - $url = $match2[0][0].'forum.php?mod=attachment&aid='.$aid; - $msg = "下载链接: ".$url; - $msg .= "\n若提示[抱歉,只有特定用户可以下载本站附件]则该uid没下载权限,可更改uid试试(uid就是用户id)\n若提示[抱歉,该附件无法读取]则证明该uid未打开过该帖,可更改uid试试;或漏洞已修补,无法免积分下载\n若提示[请不要从外部链接下载本站附件]在新打开页面刷新窗口即可"; - $telegram->exec("msg ".$from." ".escape($msg)); - } - - #echo - $plugins[] .= "#echo 输出 [#echo ]"; - if (preg_match("/^#echo (.*)$/", $_GET['text'], $matches)) { - $telegram->exec("msg ".$from." ".$matches[1]); - } - - #exec - //$plugins[] .= "#exec Execute [#exec ]"; - if (preg_match("/^#exec (.*)$/", $_GET['text'], $matches) and $_GET['from'] == 'Denny_Dai') { - $msg = file_get_contents("http://1.vps.dennx.com/?exec=".urlencode($matches[1])); - $telegram->msg($from,$msg); - } - - #ipto - $plugins[] .= "#ipto IP变形 [#ip ]"; - if (preg_match("/^#ipto (.*)$/", $_GET['text'], $matches)) { - $arr = explode('.',$matches[1]); - $msg = '原IP: '.$arr[0].'.'.$arr[1].'.'.$arr[2].'.'.$arr[3]; - $msg .= "\n整数型: ".($arr[0] * pow(256,3) + $arr[1] * pow(256,2) + $arr[2] * 256 + $arr[3]); - $msg .= "\n八进制: 0".decoct($arr[0]).".0".decoct($arr[1]).".0".decoct($arr[2]).'.0'.decoct($arr[3]); - $msg .= "\n十六进制: 0x".dechex($arr[0]).".0x".dechex($arr[1]).".0x".dechex($arr[2]).'.0x'.dechex($arr[3]); - $msg .= "\n变态十六进制: 0x0000000000".dechex($arr[0]).".0x0000000000".dechex($arr[1]).".0x0000000000".dechex($arr[2]).'.0x0000000000'.dechex($arr[3]); - $msg .= "\n原ip、八进制、十六进制以及变态十六进制ip可以任意组合,如: 0".decoct($arr[0]).'.0x0000000000'.dechex($arr[1]).'.'.$arr[2].'.0x'.dechex($arr[3]); - $telegram->exec("msg ".$from." ".escape($msg)); - } - - #trans - //$plugins[] .= "#trans 翻译 [#trans (两位) ]"; - if (preg_match("/^#trans (.*)$/", $_GET['text'], $matches)) { - $matches = explode(" ", $matches[1], 2); - if (!isset($matches[1])) { - $to = "zh"; - $text = $matches[0]; - }else{ - $to = $matches[0]; - $text = $matches[1]; - } - $trans = file_get_contents("http://tool.dennx.com/translate/?text=".$text."&to=".$to); - $msg = "翻译结果: ".$trans; - $telegram->exec("msg ".$from." ".escape($msg)); - - } - - #tts - $plugins[] .= "#tts 文字转语音 [#tts ]"; - if (preg_match("/^#tts (.*)$/", $_GET['text'], $matches)) { - $mp3 = file_get_contents("http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&text=".$matches[1]); - $file = fopen($_GET['from'].".mp3","w"); - echo fwrite($file,$mp3); - fclose($file); - $telegram->exec("send_audio ".$from." /home/wwwroot/1.vps.dennx.com/".$_GET['from'].".mp3"); - //unlink($matches[1].".mp3"); - } - - #help & plugin List (!at the last of plugins) - $plugins[] .= "#help 查看帮助 [#help]"; - if (preg_match("/^#help$/", $_GET['text'])) { - $plugin_list = "欢迎使用来自Dennx.com的TG-BOT"; - foreach ($plugins as $value) { - $plugin_list .= "\n".$value; - } - $telegram->exec("msg ".$from." ".escape($plugin_list)); - } - -} \ No newline at end of file