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