1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "execwrapper.h"
27 #include "messagemodel.h"
31 ExecWrapper::ExecWrapper(QObject* parent)
34 connect(&_process, &QProcess::readyReadStandardOutput, this, &ExecWrapper::processReadStdout);
35 connect(&_process, &QProcess::readyReadStandardError, this, &ExecWrapper::processReadStderr);
36 connect(&_process, selectOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &ExecWrapper::processFinished);
37 #if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
38 connect(&_process, selectOverload<QProcess::ProcessError>(&QProcess::error), this, &ExecWrapper::processError);
40 connect(&_process, &QProcess::errorOccurred, this, &ExecWrapper::processError);
43 connect(this, &ExecWrapper::output, this, &ExecWrapper::postStdout);
44 connect(this, &ExecWrapper::error, this, &ExecWrapper::postStderr);
47 void ExecWrapper::start(const BufferInfo& info, const QString& command)
52 QRegExp rx(R"(^\s*(\S+)(\s+(.*))?$)");
53 if (!rx.exactMatch(command)) {
54 emit error(tr("Invalid command string for /exec: %1").arg(command));
57 _scriptName = rx.cap(1);
61 // Make sure we don't execute something outside a script dir
62 if (_scriptName.contains("../") || _scriptName.contains("..\\"))
63 emit error(tr(R"(Name "%1" is invalid: ../ or ..\ are not allowed!)").arg(_scriptName));
66 foreach (QString scriptDir, Quassel::scriptDirPaths()) {
67 QString fileName = scriptDir + _scriptName;
68 if (!QFile::exists(fileName))
70 _process.setWorkingDirectory(scriptDir);
71 _process.start('"' + fileName + "\" " + params);
74 emit error(tr("Could not find script \"%1\"").arg(_scriptName));
77 deleteLater(); // self-destruct
80 void ExecWrapper::postStdout(const QString& msg)
82 if (_bufferInfo.isValid())
83 Client::userInput(_bufferInfo, msg);
86 void ExecWrapper::postStderr(const QString& msg)
88 if (_bufferInfo.isValid())
89 Client::messageModel()->insertErrorMessage(_bufferInfo, msg);
92 void ExecWrapper::processFinished(int exitCode, QProcess::ExitStatus status)
94 if (status == QProcess::CrashExit) {
95 emit error(tr("Script \"%1\" crashed with exit code %2.").arg(_scriptName).arg(exitCode));
99 if (!_stdoutBuffer.isEmpty())
100 foreach (QString msg, _stdoutBuffer.split('\n'))
102 if (!_stderrBuffer.isEmpty())
103 foreach (QString msg, _stderrBuffer.split('\n'))
109 void ExecWrapper::processError(QProcess::ProcessError err)
111 if (err == QProcess::FailedToStart)
112 emit error(tr("Script \"%1\" could not start.").arg(_scriptName));
114 emit error(tr("Script \"%1\" caused error %2.").arg(_scriptName).arg(err));
116 if (_process.state() != QProcess::Running)
120 void ExecWrapper::processReadStdout()
122 QString str = QTextCodec::codecForLocale()->toUnicode(_process.readAllStandardOutput());
123 str.replace(QRegExp("\r\n?"), "\n");
124 _stdoutBuffer.append(str);
126 while ((idx = _stdoutBuffer.indexOf('\n')) >= 0) {
127 emit output(_stdoutBuffer.left(idx));
128 _stdoutBuffer = _stdoutBuffer.mid(idx + 1);
132 void ExecWrapper::processReadStderr()
134 QString str = QTextCodec::codecForLocale()->toUnicode(_process.readAllStandardError());
135 str.replace(QRegExp("\r\n?"), "\n");
136 _stderrBuffer.append(str);
138 while ((idx = _stderrBuffer.indexOf('\n')) >= 0) {
139 emit error(_stderrBuffer.left(idx));
140 _stderrBuffer = _stderrBuffer.mid(idx + 1);