Introduce /exec support for running simple client-side scripts
[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
23 #include "execwrapper.h"
24
25 #include "client.h"
26 #include "messagemodel.h"
27 #include "quassel.h"
28
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)));
34
35   connect(this, SIGNAL(stdout(QString)), SLOT(postStdout(QString)));
36   connect(this, SIGNAL(stderr(QString)), SLOT(postStderr(QString)));
37 }
38
39 void ExecWrapper::start(const BufferInfo &info, const QString &scriptName, const QStringList& params) {
40   _bufferInfo = info;
41   _scriptName = scriptName;
42   foreach(QString scriptDir, Quassel::scriptDirPaths()) {
43     QString fileName = scriptDir + '/' + scriptName;
44     if(!QFile::exists(fileName))
45       continue;
46     _process.start(fileName, params);
47     return;
48   }
49   emit stderr(tr("Could not find script \"%1\"").arg(scriptName));
50   deleteLater();
51 }
52
53 void ExecWrapper::postStdout(const QString &msg) {
54   if(_bufferInfo.isValid())
55     Client::userInput(_bufferInfo, msg);
56 }
57
58 void ExecWrapper::postStderr(const QString &msg) {
59   if(_bufferInfo.isValid())
60     Client::messageModel()->insertErrorMessage(_bufferInfo, msg);
61 }
62
63 void ExecWrapper::processFinished(int exitCode, QProcess::ExitStatus status) {
64   if(status == QProcess::CrashExit) {
65     emit stderr(tr("Script \"%1\" crashed with exit code %2.").arg(_scriptName).arg(exitCode));
66   }
67
68   // TODO empty buffers
69
70   deleteLater();
71 }
72
73 void ExecWrapper::processError(QProcess::ProcessError error) {
74   emit stderr(tr("Script \"%1\" caused error %2.").arg(_scriptName).arg(error));
75 }
76
77 void ExecWrapper::processReadStdout() {
78   _stdoutBuffer.append(_process.readAllStandardOutput());
79   int idx;
80   while((idx = _stdoutBuffer.indexOf('\n')) >= 0) {
81     emit stdout(_stdoutBuffer.left(idx));
82     _stdoutBuffer = _stdoutBuffer.mid(idx + 1);
83   }
84 }
85
86 void ExecWrapper::processReadStderr() {
87   _stderrBuffer.append(_process.readAllStandardError());
88   int idx;
89   while((idx = _stderrBuffer.indexOf('\n')) >= 0) {
90     emit stdout(_stderrBuffer.left(idx));
91     _stderrBuffer = _stderrBuffer.mid(idx + 1);
92   }
93 }