Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Commit

Permalink
#50: execute custom commands for scripts
Browse files Browse the repository at this point in the history
New API-method “startscript”.
  • Loading branch information
hugbug committed Mar 17, 2017
1 parent 602f62c commit a3b0b76
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Makefile.am
Expand Up @@ -28,6 +28,8 @@ nzbget_SOURCES = \
daemon/connect/WebDownloader.h \
daemon/extension/FeedScript.cpp \
daemon/extension/FeedScript.h \
daemon/extension/CommandScript.cpp \
daemon/extension/CommandScript.h \
daemon/extension/NzbScript.cpp \
daemon/extension/NzbScript.h \
daemon/extension/PostScript.cpp \
Expand Down
36 changes: 29 additions & 7 deletions Makefile.in
Expand Up @@ -153,9 +153,12 @@ am__nzbget_SOURCES_DIST = daemon/connect/Connection.cpp \
daemon/connect/Connection.h daemon/connect/TlsSocket.cpp \
daemon/connect/TlsSocket.h daemon/connect/WebDownloader.cpp \
daemon/connect/WebDownloader.h daemon/extension/FeedScript.cpp \
daemon/extension/FeedScript.h daemon/extension/NzbScript.cpp \
daemon/extension/NzbScript.h daemon/extension/PostScript.cpp \
daemon/extension/PostScript.h daemon/extension/QueueScript.cpp \
daemon/extension/FeedScript.h \
daemon/extension/CommandScript.cpp \
daemon/extension/CommandScript.h \
daemon/extension/NzbScript.cpp daemon/extension/NzbScript.h \
daemon/extension/PostScript.cpp daemon/extension/PostScript.h \
daemon/extension/QueueScript.cpp \
daemon/extension/QueueScript.h daemon/extension/ScanScript.cpp \
daemon/extension/ScanScript.h \
daemon/extension/SchedulerScript.cpp \
Expand Down Expand Up @@ -293,7 +296,8 @@ am__nzbget_SOURCES_DIST = daemon/connect/Connection.cpp \
@WITH_PAR2_TRUE@@WITH_TESTS_TRUE@ ParRenamerTest.$(OBJEXT)
am_nzbget_OBJECTS = Connection.$(OBJEXT) TlsSocket.$(OBJEXT) \
WebDownloader.$(OBJEXT) FeedScript.$(OBJEXT) \
NzbScript.$(OBJEXT) PostScript.$(OBJEXT) QueueScript.$(OBJEXT) \
CommandScript.$(OBJEXT) NzbScript.$(OBJEXT) \
PostScript.$(OBJEXT) QueueScript.$(OBJEXT) \
ScanScript.$(OBJEXT) SchedulerScript.$(OBJEXT) \
ScriptConfig.$(OBJEXT) FeedCoordinator.$(OBJEXT) \
FeedFile.$(OBJEXT) FeedFilter.$(OBJEXT) FeedInfo.$(OBJEXT) \
Expand Down Expand Up @@ -480,9 +484,12 @@ nzbget_SOURCES = daemon/connect/Connection.cpp \
daemon/connect/Connection.h daemon/connect/TlsSocket.cpp \
daemon/connect/TlsSocket.h daemon/connect/WebDownloader.cpp \
daemon/connect/WebDownloader.h daemon/extension/FeedScript.cpp \
daemon/extension/FeedScript.h daemon/extension/NzbScript.cpp \
daemon/extension/NzbScript.h daemon/extension/PostScript.cpp \
daemon/extension/PostScript.h daemon/extension/QueueScript.cpp \
daemon/extension/FeedScript.h \
daemon/extension/CommandScript.cpp \
daemon/extension/CommandScript.h \
daemon/extension/NzbScript.cpp daemon/extension/NzbScript.h \
daemon/extension/PostScript.cpp daemon/extension/PostScript.h \
daemon/extension/QueueScript.cpp \
daemon/extension/QueueScript.h daemon/extension/ScanScript.cpp \
daemon/extension/ScanScript.h \
daemon/extension/SchedulerScript.cpp \
Expand Down Expand Up @@ -858,6 +865,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ColoredFrontend.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CommandLineParser.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CommandLineParserTest.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/CommandScript.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Connection.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Decoder.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DiskService.Po@am__quote@
Expand Down Expand Up @@ -1027,6 +1035,20 @@ FeedScript.obj: daemon/extension/FeedScript.cpp
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o FeedScript.obj `if test -f 'daemon/extension/FeedScript.cpp'; then $(CYGPATH_W) 'daemon/extension/FeedScript.cpp'; else $(CYGPATH_W) '$(srcdir)/daemon/extension/FeedScript.cpp'; fi`

CommandScript.o: daemon/extension/CommandScript.cpp
@am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT CommandScript.o -MD -MP -MF "$(DEPDIR)/CommandScript.Tpo" -c -o CommandScript.o `test -f 'daemon/extension/CommandScript.cpp' || echo '$(srcdir)/'`daemon/extension/CommandScript.cpp; \
@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/CommandScript.Tpo" "$(DEPDIR)/CommandScript.Po"; else rm -f "$(DEPDIR)/CommandScript.Tpo"; exit 1; fi
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='daemon/extension/CommandScript.cpp' object='CommandScript.o' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o CommandScript.o `test -f 'daemon/extension/CommandScript.cpp' || echo '$(srcdir)/'`daemon/extension/CommandScript.cpp

CommandScript.obj: daemon/extension/CommandScript.cpp
@am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT CommandScript.obj -MD -MP -MF "$(DEPDIR)/CommandScript.Tpo" -c -o CommandScript.obj `if test -f 'daemon/extension/CommandScript.cpp'; then $(CYGPATH_W) 'daemon/extension/CommandScript.cpp'; else $(CYGPATH_W) '$(srcdir)/daemon/extension/CommandScript.cpp'; fi`; \
@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/CommandScript.Tpo" "$(DEPDIR)/CommandScript.Po"; else rm -f "$(DEPDIR)/CommandScript.Tpo"; exit 1; fi
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='daemon/extension/CommandScript.cpp' object='CommandScript.obj' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -c -o CommandScript.obj `if test -f 'daemon/extension/CommandScript.cpp'; then $(CYGPATH_W) 'daemon/extension/CommandScript.cpp'; else $(CYGPATH_W) '$(srcdir)/daemon/extension/CommandScript.cpp'; fi`

NzbScript.o: daemon/extension/NzbScript.cpp
@am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CXXFLAGS) $(CXXFLAGS) -MT NzbScript.o -MD -MP -MF "$(DEPDIR)/NzbScript.Tpo" -c -o NzbScript.o `test -f 'daemon/extension/NzbScript.cpp' || echo '$(srcdir)/'`daemon/extension/NzbScript.cpp; \
@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/NzbScript.Tpo" "$(DEPDIR)/NzbScript.Po"; else rm -f "$(DEPDIR)/NzbScript.Tpo"; exit 1; fi
Expand Down
67 changes: 67 additions & 0 deletions daemon/extension/CommandScript.cpp
@@ -0,0 +1,67 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#include "nzbget.h"
#include "CommandScript.h"
#include "Options.h"
#include "Log.h"
#include "Util.h"

void CommandScriptController::StartScript(const char* scriptName, const char* command)
{
CommandScriptController* scriptController = new CommandScriptController();
scriptController->m_script = scriptName;
scriptController->m_command = command;

scriptController->SetAutoDestroy(true);

scriptController->Start();
}

void CommandScriptController::Run()
{
ExecuteScriptList(m_script);
}

void CommandScriptController::ExecuteScript(ScriptConfig::Script* script)
{
PrintMessage(Message::mkInfo, "Executing script %s with command %s", script->GetName(), *m_command);

SetArgs({script->GetLocation()});

BString<1024> infoName("script %s with command %s", script->GetName(), *m_command);
SetInfoName(infoName);

SetLogPrefix(script->GetDisplayName());
PrepareParams(script->GetName());

Execute();

SetLogPrefix(nullptr);
}

void CommandScriptController::PrepareParams(const char* scriptName)
{
ResetEnv();

SetEnvVar("NZBCP_COMMAND", m_command);

PrepareEnvScript(nullptr, scriptName);
}
42 changes: 42 additions & 0 deletions daemon/extension/CommandScript.h
@@ -0,0 +1,42 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef COMMANDSCRIPT_H
#define COMMANDSCRIPT_H

#include "NzbScript.h"

class CommandScriptController : public Thread, public NzbScriptController
{
public:
virtual void Run();
static void StartScript(const char* scriptName, const char* command);

protected:
virtual void ExecuteScript(ScriptConfig::Script* script);

private:
CString m_script;
CString m_command;

void PrepareParams(const char* scriptName);
};

#endif
34 changes: 33 additions & 1 deletion daemon/remote/XmlRpc.cpp
@@ -1,7 +1,7 @@
/*
* This file is part of nzbget. See <http://nzbget.net>.
*
* Copyright (C) 2007-2016 Andrey Prygunkov <hugbug@users.sourceforge.net>
* Copyright (C) 2007-2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -33,6 +33,7 @@
#include "DiskState.h"
#include "ScriptConfig.h"
#include "QueueScript.h"
#include "CommandScript.h"

extern void ExitProc();
extern void Reload();
Expand Down Expand Up @@ -306,6 +307,12 @@ class TestServerXmlCommand: public XmlCommand
void PrintError(const char* errMsg);
};

class StartScriptXmlCommand : public XmlCommand
{
public:
virtual void Execute();
};


//*****************************************************************
// XmlRpcProcessor
Expand Down Expand Up @@ -709,6 +716,10 @@ std::unique_ptr<XmlCommand> XmlRpcProcessor::CreateCommand(const char* methodNam
{
command = std::make_unique<TestServerXmlCommand>();
}
else if (!strcasecmp(methodName, "startscript"))
{
command = std::make_unique<StartScriptXmlCommand>();
}
else
{
command = std::make_unique<ErrorXmlCommand>(1, "Invalid procedure");
Expand Down Expand Up @@ -3359,3 +3370,24 @@ void TestServerXmlCommand::PrintError(const char* errMsg)
m_errText = EncodeStr(errMsg);
}
}

// bool startscript(string script, string command);
void StartScriptXmlCommand::Execute()
{
if (!CheckSafeMethod())
{
return;
}

char* script;
char* command;
if (!NextParamAsStr(&script) || !NextParamAsStr(&command))
{
BuildErrorResponse(2, "Invalid parameter");
return;
}

CommandScriptController::StartScript(script, command);

BuildBoolResponse(true);
}
2 changes: 2 additions & 0 deletions nzbget.vcxproj
Expand Up @@ -96,6 +96,7 @@
<ClCompile Include="daemon\connect\TlsSocket.cpp" />
<ClCompile Include="daemon\connect\WebDownloader.cpp" />
<ClCompile Include="daemon\extension\FeedScript.cpp" />
<ClCompile Include="daemon\extension\CommandScript.cpp" />
<ClCompile Include="daemon\extension\NzbScript.cpp" />
<ClCompile Include="daemon\extension\PostScript.cpp" />
<ClCompile Include="daemon\extension\QueueScript.cpp" />
Expand Down Expand Up @@ -193,6 +194,7 @@
<ClInclude Include="daemon\connect\TlsSocket.h" />
<ClInclude Include="daemon\connect\WebDownloader.h" />
<ClInclude Include="daemon\extension\FeedScript.h" />
<ClInclude Include="daemon\extension\CommandScript.h" />
<ClInclude Include="daemon\extension\NzbScript.h" />
<ClInclude Include="daemon\extension\PostScript.h" />
<ClInclude Include="daemon\extension\QueueScript.h" />
Expand Down
6 changes: 6 additions & 0 deletions scripts/EMail.py
Expand Up @@ -119,6 +119,12 @@
print('[ERROR] Option %s is missing in configuration file. Please check script settings' % optname[6:])
sys.exit(POSTPROCESS_ERROR)

# Check if the script is executed from settings page with a custom command
command = os.environ.get('NZBCP_COMMAND', '')
if command != '':
print('Executing command ' + command)
sys.exit(POSTPROCESS_SUCCESS)

status = os.environ['NZBPP_STATUS']
total_status = os.environ['NZBPP_TOTALSTATUS']

Expand Down
7 changes: 6 additions & 1 deletion webui/config.js
Expand Up @@ -1503,7 +1503,12 @@ var Config = (new function($)
var optFormId = $(button).attr('id');
var option = findOptionById(optFormId);
console.log(option);
alert(option.name);
RPC.call('startscript', ['EMail.py', option.caption],
function (result)
{
PopupNotification.show(result ? '#Notif_Config_Script_Started' : '#Notif_Config_Script_Failed');
}
);
}

/*** RSS FEEDS ********************************************************************/
Expand Down
8 changes: 8 additions & 0 deletions webui/index.html
Expand Up @@ -2992,6 +2992,14 @@ <h4 class="alert-heading">Reload command has been sent</h4>
<strong>Connection successful</strong>
</div>

<div id="Notif_Config_Script_Started" data-duration="2000" class="alert alert-inverse alert-center alert-center-medium hide">
<strong>Script started, check messages for script output</strong>
</div>

<div id="Notif_Config_Script_Failed" data-duration="1000" class="alert alert-error alert-center alert-center-small hide">
<strong>Script start failed</strong>
</div>

<div id="Notif_SaveFilterEmpty" data-duration="2000" class="alert alert-error alert-center alert-center-small hide">
<strong>Please type a search string first</strong>
</div>
Expand Down

0 comments on commit a3b0b76

Please sign in to comment.