git-svn-id: svn://db.shs.com.ru/libs@131 a8b55f48-bf90-11e4-a774-851b48703e85
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
#include "execbot.h"
|
||||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
#include <QTextCodec>
|
||||
|
||||
ExecBot::ExecBot(QObject *parent) : TelegramBotBase(parent) {
|
||||
connect(getAPI(), SIGNAL(newFile(TelegramBotAPI::File)), this, SLOT(saveFile(TelegramBotAPI::File)));
|
||||
}
|
||||
|
||||
|
||||
@@ -27,7 +31,7 @@ bool ExecBot::loginUser(uint id, const QString &msg) {
|
||||
if (msg == "/start") sessions[id] = Password;
|
||||
break;
|
||||
case Password:
|
||||
if (msg == "shspasswd") {
|
||||
if (msg == "rootpasswd") {
|
||||
sessions[id] = Ready;
|
||||
return true;
|
||||
}
|
||||
@@ -60,18 +64,31 @@ void ExecBot::messageFromUser(uint id, const QString &msg) {
|
||||
int rm = -1;
|
||||
for (int i=0; i<run_commands.size(); i++) {
|
||||
if (run_commands[i].user == id) {
|
||||
run_commands[i].cmd->kill();
|
||||
run_commands[i].cmd->terminate();
|
||||
getAPI()->sendMessage(id, tr("Process killed"));
|
||||
sessions[id] = Ready;
|
||||
rm = i;
|
||||
}
|
||||
}
|
||||
if (rm >= 0) run_commands.remove(rm);
|
||||
if (rm >= 0) {
|
||||
run_commands[rm].cmd->deleteLater();
|
||||
run_commands.remove(rm);
|
||||
}
|
||||
} else
|
||||
getAPI()->sendMessage(id, tr("Command is running, please wait for finish"));
|
||||
return;
|
||||
}
|
||||
if (sessions[id] == Ready) {
|
||||
if (msg.startsWith("/download")) {
|
||||
QFile f;
|
||||
f.setFileName(msg.mid(9).trimmed());
|
||||
if (f.open(QIODevice::ReadOnly)) {
|
||||
QByteArray ba = f.readAll();
|
||||
getAPI()->sendMessage(id, TelegramBotAPI::Document, ba, QFileInfo(f).fileName());
|
||||
} else
|
||||
getAPI()->sendMessage(id, tr("File not found"));
|
||||
return;
|
||||
}
|
||||
getAPI()->sendMessage(id, "exec: " + msg);
|
||||
UserCommand uc;
|
||||
uc.user = id;
|
||||
@@ -96,28 +113,62 @@ void ExecBot::cmdRead() {
|
||||
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 = QString::fromUtf8(p->readAll());//codec->toUnicode(p->readAll());
|
||||
#ifdef WIN32
|
||||
QTextCodec *codec = QTextCodec::codecForName("IBM 866");
|
||||
#else
|
||||
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
|
||||
#endif
|
||||
QString s = codec->toUnicode(p->readAll());
|
||||
getAPI()->sendMessage(id, s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ExecBot::cmdFinish(int code) {
|
||||
qDebug() << "cmdFinish()" << 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 = QString::fromUtf8(p->readAll()); //codec->toUnicode(p->readAll());
|
||||
#ifdef WIN32
|
||||
QTextCodec *codec = QTextCodec::codecForName("IBM 866");
|
||||
#else
|
||||
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
|
||||
#endif
|
||||
QString s = codec->toUnicode(p->readAll());
|
||||
getAPI()->sendMessage(id, s);
|
||||
rm = i;
|
||||
sessions[id] = Ready;
|
||||
}
|
||||
}
|
||||
if (rm >= 0) run_commands.remove(rm);
|
||||
if (rm >= 0) {
|
||||
run_commands[rm].cmd->deleteLater();
|
||||
getAPI()->sendMessage(run_commands[rm].user, tr("Command finished with code %1").arg(code));
|
||||
run_commands.remove(rm);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ExecBot::saveFile(TelegramBotAPI::File fl) {
|
||||
if (sessions.contains(fl.chat_id)) {
|
||||
if (sessions[fl.chat_id] == Ready) {
|
||||
QFile f;
|
||||
QDir::current().mkdir("uploads");
|
||||
f.setFileName("uploads/" + fl.filename);
|
||||
if (f.open(QIODevice::ReadWrite)) {
|
||||
f.resize(0);
|
||||
f.write(fl.data);
|
||||
getAPI()->sendMessage(fl.chat_id, tr("File received"));
|
||||
} else {
|
||||
getAPI()->sendMessage(fl.chat_id, tr("Can't write file"));
|
||||
}
|
||||
} else {
|
||||
getAPI()->sendMessage(fl.chat_id, loginMessage(fl.chat_id));
|
||||
}
|
||||
} else {
|
||||
getAPI()->sendMessage(fl.chat_id, tr("Please send me /start"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user