1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include "execwrapper.h"
26 #include "messagemodel.h"
29 ExecWrapper::ExecWrapper(QObject* parent) : QObject(parent) {
30 connect(&_process, SIGNAL(readyReadStandardOutput()), SLOT(processReadStdout()));
31 connect(&_process, SIGNAL(readyReadStandardError()), SLOT(processReadStderr()));
32 connect(&_process, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus)));
33 connect(&_process, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
35 connect(this, SIGNAL(stdout(QString)), SLOT(postStdout(QString)));
36 connect(this, SIGNAL(stderr(QString)), SLOT(postStderr(QString)));
39 void ExecWrapper::start(const BufferInfo &info, const QString &command) {
43 QRegExp rx("^\\s*(\\S+)(\\s+(.*))?$");
44 if(!rx.exactMatch(command)) {
45 emit stderr(tr("Invalid command string for /exec: %1").arg(command));
47 _scriptName = rx.cap(1);
51 // Make sure we don't execute something outside a script dir
52 if(_scriptName.startsWith('/') || _scriptName.contains("../"))
53 emit stderr(tr("Name \"%1\" is invalid: / or ../ are not allowed!").arg(_scriptName));
56 foreach(QString scriptDir, Quassel::scriptDirPaths()) {
57 QString fileName = scriptDir + _scriptName;
58 if(!QFile::exists(fileName))
60 _process.start(fileName + ' ' + params);
63 emit stderr(tr("Could not find script \"%1\"").arg(_scriptName));
66 deleteLater(); // self-destruct
69 void ExecWrapper::postStdout(const QString &msg) {
70 if(_bufferInfo.isValid())
71 Client::userInput(_bufferInfo, msg);
74 void ExecWrapper::postStderr(const QString &msg) {
75 if(_bufferInfo.isValid())
76 Client::messageModel()->insertErrorMessage(_bufferInfo, msg);
79 void ExecWrapper::processFinished(int exitCode, QProcess::ExitStatus status) {
80 if(status == QProcess::CrashExit) {
81 emit stderr(tr("Script \"%1\" crashed with exit code %2.").arg(_scriptName).arg(exitCode));
85 if(!_stdoutBuffer.isEmpty())
86 foreach(QString msg, _stdoutBuffer.split('\n'))
88 if(!_stderrBuffer.isEmpty())
89 foreach(QString msg, _stderrBuffer.split('\n'))
95 void ExecWrapper::processError(QProcess::ProcessError error) {
96 emit stderr(tr("Script \"%1\" caused error %2.").arg(_scriptName).arg(error));
97 if(_process.state() != QProcess::Running)
101 void ExecWrapper::processReadStdout() {
102 _stdoutBuffer.append(_process.readAllStandardOutput());
104 while((idx = _stdoutBuffer.indexOf('\n')) >= 0) {
105 emit stdout(_stdoutBuffer.left(idx));
106 _stdoutBuffer = _stdoutBuffer.mid(idx + 1);
110 void ExecWrapper::processReadStderr() {
111 _stderrBuffer.append(_process.readAllStandardError());
113 while((idx = _stderrBuffer.indexOf('\n')) >= 0) {
114 emit stderr(_stderrBuffer.left(idx));
115 _stderrBuffer = _stderrBuffer.mid(idx + 1);