translate && del Redundant codes && some new var

This commit is contained in:
Denny Dai 2015-05-30 01:30:08 +08:00
parent 75c23b86e9
commit 2347defd47
10 changed files with 18 additions and 100 deletions

View File

@ -1,67 +0,0 @@
<?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;
}
}

View File

@ -38,14 +38,4 @@ class Base
return false;
}
public function escapeStringArgument($argument)
{
return '"' . addslashes($argument) . '"';
}
public function escapePeer($peer)
{
return str_replace(' ', '_', $peer);
}
}

View File

@ -1,2 +1,4 @@
<?php
define('TAG', '#');
define('OWNER_NAME', 'Denny Dai');
define('BOT_NAME', 'Dx. Dennx');

View File

@ -10,11 +10,4 @@ function escapePeer($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');
}

View File

@ -4,9 +4,9 @@ require_once('config.inc.php');
require_once('functions.php');
$plugins = [];
if($_GET['from'] != "Dx._Dennx"){
if($_GET['from'] != BOT_NAME){
//is group?
if ($_GET['to'] == 'Dx._Dennx') {
if ($_GET['to'] == BOT_NAME) {
$isgroup = false;
}else{
$isgroup = true;
@ -17,7 +17,7 @@ if($_GET['from'] != "Dx._Dennx"){
$from = $_GET['to'];
}else{
$from = $_GET['from'];
$BOT->exec("msg Denny_Dai ".json_encode($_GET)); //私聊监测
$BOT->exec("msg ".escapePeer(OWNER_NAME)." ".json_encode($_GET));
}
//load plugins

View File

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

View File

@ -1,7 +1,7 @@
<?php
PluginList("help", "查看帮助");
PluginList("help", "Ask for help");
if (preg_match("/^".TAG."help$/", $_GET['text'])) {
$plugin_list = "欢迎使用来自Dennx.com的TG-BOT";
$plugin_list = "Welcome to use TG-BOT From Dennx.com";
foreach ($plugins as $value) {
$plugin_list .= "\n".$value;
}

View File

@ -1,12 +1,12 @@
<?php
PluginList("ipto", "IP变形");
PluginList("ipto", "Change the form of IP address");
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]);
$msg = 'IP: '.$arr[0].'.'.$arr[1].'.'.$arr[2].'.'.$arr[3];
$msg .= "\nint: ".($arr[0] * pow(256,3) + $arr[1] * pow(256,2) + $arr[2] * 256 + $arr[3]);
$msg .= "\noct: 0".decoct($arr[0]).".0".decoct($arr[1]).".0".decoct($arr[2]).'.0'.decoct($arr[3]);
$msg .= "\nhex: 0x".dechex($arr[0]).".0x".dechex($arr[1]).".0x".dechex($arr[2]).'.0x'.dechex($arr[3]);
$msg .= "\nfucking hex: 0x0000000000".dechex($arr[0]).".0x0000000000".dechex($arr[1]).".0x0000000000".dechex($arr[2]).'.0x0000000000'.dechex($arr[3]);
$msg .= "\nYou can combine them with any combination. Such as: 0".decoct($arr[0]).'.0x0000000000'.dechex($arr[1]).'.'.$arr[2].'.0x'.dechex($arr[3]);
$BOT->exec("msg ".$from." ".escapeString($msg));
}

View File

@ -1,5 +1,5 @@
<?php
//PluginList("trans", "翻译");
//PluginList("trans", "translate");
if (preg_match("/^".TAG."trans (.*)$/", $_GET['text'], $matches)) {
$matches = explode(" ", $matches[1], 2);
if (!isset($matches[1])) {
@ -10,7 +10,7 @@ if (preg_match("/^".TAG."trans (.*)$/", $_GET['text'], $matches)) {
$text = $matches[1];
}
$trans = file_get_contents("http://tool.dennx.com/translate/?text=".$text."&to=".$to);
$msg = "翻译结果: ".$trans;
$msg = "Result: ".$trans;
$BOT->exec("msg ".$from." ".escapeString($msg));
}

View File

@ -1,5 +1,5 @@
<?php
PluginList("tts", "文字转语音");
PluginList("tts", "Text To Speech");
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");