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