git-svn-id: svn://db.shs.com.ru/libs@129 a8b55f48-bf90-11e4-a774-851b48703e85

This commit is contained in:
2016-10-19 13:20:53 +00:00
parent 81f9c82679
commit 724d9652bd
14 changed files with 2023 additions and 0 deletions

123
telegram_test/execbot.cpp Normal file
View File

@@ -0,0 +1,123 @@
#include "execbot.h"
#include <QDebug>
#include <QTextCodec>
ExecBot::ExecBot(QObject *parent) : TelegramBotBase(parent) {
}
QString ExecBot::loginMessage(uint id) {
if (sessions.contains(id)) {
if (sessions[id] == Password) return tr("Enter password");
if (sessions[id] == NotLogged) return tr("Please send me /start");
}
return tr("Error");
}
QString ExecBot::help() {
return tr("Input command for exec on server, after exec finished result will be sended to you");
}
bool ExecBot::loginUser(uint id, const QString &msg) {
if (sessions.contains(id)) {
switch (sessions[id]) {
case NotLogged:
if (msg == "/start") sessions[id] = Password;
break;
case Password:
if (msg == "a") {
sessions[id] = Ready;
return true;
}
break;
case Ready:
case CommandExec:
return true;
default:
break;
return false;
}
} else {
if (msg == "/start") sessions[id] = Password;
else sessions[id] = NotLogged;
}
return false;
}
void ExecBot::messageFromUser(uint id, const QString &msg) {
if (sessions.contains(id)) {
if (msg == "/exit") {
sessions[id] = NotLogged;
disconnectUser(id);
getAPI()->sendMessage(id, tr("Logout"));
return;
}
if (sessions[id] == CommandExec) {
if (msg == "/kill") {
int rm = -1;
for (int i=0; i<run_commands.size(); i++) {
if (run_commands[i].user == id) {
run_commands[i].cmd->kill();
getAPI()->sendMessage(id, tr("Process killed"));
sessions[id] = Ready;
rm = i;
}
}
if (rm >= 0) run_commands.remove(rm);
} else
getAPI()->sendMessage(id, tr("Command is running, please wait for finish"));
return;
}
if (sessions[id] == Ready) {
getAPI()->sendMessage(id, "exec: " + msg);
UserCommand uc;
uc.user = id;
uc.cmd = new QProcess();
// uc.cmd->setProcessChannelMode(QProcess::MergedChannels);
connect(uc.cmd, SIGNAL(readyRead()), this, SLOT(cmdRead()));
// connect(uc.cmd, SIGNAL(started()), this, SLOT(cmdStart()));
connect(uc.cmd, SIGNAL(finished(int)), this, SLOT(cmdFinish(int)));
uc.cmd->start(msg);
if (uc.cmd->waitForStarted(3000)) {
sessions[id] = CommandExec;
run_commands.append(uc);
} else getAPI()->sendMessage(id, tr("Can't run command %1").arg(msg));
}
}
}
void ExecBot::cmdRead() {
// qDebug() << "cmdRead()";
QProcess * p = (QProcess *)sender();
for (int i=0; i<run_commands.size(); i++) {
if (run_commands[i].cmd == p) {
int id = run_commands[i].user;
QTextCodec *codec = QTextCodec::codecForName("IBM 866");
QString s = codec->toUnicode(p->readAll());
getAPI()->sendMessage(id, s);
}
}
}
void ExecBot::cmdFinish(int code) {
qDebug() << "cmdFinish()" << code;
QProcess * p = (QProcess *)sender();
int rm = -1;
for (int i=0; i<run_commands.size(); i++) {
if (run_commands[i].cmd == p) {
int id = run_commands[i].user;
QTextCodec *codec = QTextCodec::codecForName("IBM 866");
QString s = codec->toUnicode(p->readAll());
getAPI()->sendMessage(id, s);
rm = i;
sessions[id] = Ready;
}
}
if (rm >= 0) run_commands.remove(rm);
}