1 /***************************************************************************
2 * Copyright (C) 2005-2020 by the Quassel Project *
3 * devel@quassel-irc.org *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
21 #include "execwrapper.h"
22
23 #include <QFile>
24 #include <QTextCodec>
25 #include <QRegularExpression>
26
27 #include "client.h"
28 #include "messagemodel.h"
29 #include "quassel.h"
30 #include "util.h"
31
ExecWrapper(QObject * parent)32 ExecWrapper::ExecWrapper(QObject* parent)
33 : QObject(parent)
34 {
35 connect(&_process, &QProcess::readyReadStandardOutput, this, &ExecWrapper::processReadStdout);
36 connect(&_process, &QProcess::readyReadStandardError, this, &ExecWrapper::processReadStderr);
37 connect(&_process, selectOverload<int, QProcess::ExitStatus>(&QProcess::finished), this, &ExecWrapper::processFinished);
38 #if QT_VERSION < QT_VERSION_CHECK(5, 6, 0)
39 connect(&_process, selectOverload<QProcess::ProcessError>(&QProcess::error), this, &ExecWrapper::processError);
40 #else
41 connect(&_process, &QProcess::errorOccurred, this, &ExecWrapper::processError);
42 #endif
43
44 connect(this, &ExecWrapper::output, this, &ExecWrapper::postStdout);
45 connect(this, &ExecWrapper::error, this, &ExecWrapper::postStderr);
46 }
47
start(const BufferInfo & info,const QString & command)48 void ExecWrapper::start(const BufferInfo& info, const QString& command)
49 {
50 _bufferInfo = info;
51 _scriptName.clear();
52
53 QStringList params;
54
55 static const QRegularExpression rx{R"(^\s*(\S+)(\s+(.*))?$)"};
56 auto match = rx.match(command);
57 if (!match.hasMatch()) {
58 emit error(tr("Invalid command string for /exec: %1").arg(command));
59 }
60 else {
61 _scriptName = match.captured(1);
62 static const QRegularExpression splitRx{"\\s+"};
63 params = match.captured(3).split(splitRx, QString::SkipEmptyParts);
64 }
65
66 // Make sure we don't execute something outside a script dir
67 if (_scriptName.contains("../") || _scriptName.contains("..\\")) {
68 emit error(tr(R"(Name "%1" is invalid: ../ or ..\ are not allowed!)").arg(_scriptName));
69 }
70 else if (!_scriptName.isEmpty()) {
71 foreach (QString scriptDir, Quassel::scriptDirPaths()) {
72 QString fileName = scriptDir + _scriptName;
73 if (!QFile::exists(fileName))
74 continue;
75 _process.setWorkingDirectory(scriptDir);
76 _process.start(_scriptName, params);
77 return;
78 }
79 emit error(tr("Could not find script \"%1\"").arg(_scriptName));
80 }
81
82 deleteLater(); // self-destruct
83 }
84
postStdout(const QString & msg)85 void ExecWrapper::postStdout(const QString& msg)
86 {
87 if (_bufferInfo.isValid())
88 Client::userInput(_bufferInfo, msg);
89 }
90
postStderr(const QString & msg)91 void ExecWrapper::postStderr(const QString& msg)
92 {
93 if (_bufferInfo.isValid())
94 Client::messageModel()->insertErrorMessage(_bufferInfo, msg);
95 }
96
processFinished(int exitCode,QProcess::ExitStatus status)97 void ExecWrapper::processFinished(int exitCode, QProcess::ExitStatus status)
98 {
99 if (status == QProcess::CrashExit) {
100 emit error(tr("Script \"%1\" crashed with exit code %2.").arg(_scriptName).arg(exitCode));
101 }
102
103 // empty buffers
104 if (!_stdoutBuffer.isEmpty())
105 foreach (QString msg, _stdoutBuffer.split('\n'))
106 emit output(msg);
107 if (!_stderrBuffer.isEmpty())
108 foreach (QString msg, _stderrBuffer.split('\n'))
109 emit error(msg);
110
111 deleteLater();
112 }
113
processError(QProcess::ProcessError err)114 void ExecWrapper::processError(QProcess::ProcessError err)
115 {
116 if (err == QProcess::FailedToStart)
117 emit error(tr("Script \"%1\" could not start.").arg(_scriptName));
118 else
119 emit error(tr("Script \"%1\" caused error %2.").arg(_scriptName).arg(err));
120
121 if (_process.state() != QProcess::Running)
122 deleteLater();
123 }
124
processReadStdout()125 void ExecWrapper::processReadStdout()
126 {
127 QString str = QTextCodec::codecForLocale()->toUnicode(_process.readAllStandardOutput());
128 str.replace(QRegExp("\r\n?"), "\n");
129 _stdoutBuffer.append(str);
130 int idx;
131 while ((idx = _stdoutBuffer.indexOf('\n')) >= 0) {
132 emit output(_stdoutBuffer.left(idx));
133 _stdoutBuffer = _stdoutBuffer.mid(idx + 1);
134 }
135 }
136
processReadStderr()137 void ExecWrapper::processReadStderr()
138 {
139 QString str = QTextCodec::codecForLocale()->toUnicode(_process.readAllStandardError());
140 str.replace(QRegExp("\r\n?"), "\n");
141 _stderrBuffer.append(str);
142 int idx;
143 while ((idx = _stderrBuffer.indexOf('\n')) >= 0) {
144 emit error(_stderrBuffer.left(idx));
145 _stderrBuffer = _stderrBuffer.mid(idx + 1);
146 }
147 }
148