/* * This file is part of nzbget * * Copyright (C) 2007-2015 Andrey Prygunkov * * 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * $Revision$ * $Date$ * */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #ifdef WIN32 #include "win32.h" #endif #include #include #include #ifndef WIN32 #include #endif #include #include #include "nzbget.h" #include "ScanScript.h" #include "Scanner.h" #include "Options.h" #include "Log.h" #include "Util.h" void ScanScriptController::ExecuteScripts(const char* nzbFilename, const char* url, const char* directory, char** nzbName, char** category, int* priority, NZBParameterList* parameters, bool* addTop, bool* addPaused, char** dupeKey, int* dupeScore, EDupeMode* dupeMode) { ScanScriptController* scriptController = new ScanScriptController(); scriptController->m_nzbFilename = nzbFilename; scriptController->m_url = url; scriptController->m_directory = directory; scriptController->m_nzbName = nzbName; scriptController->m_category = category; scriptController->m_parameters = parameters; scriptController->m_priority = priority; scriptController->m_addTop = addTop; scriptController->m_addPaused = addPaused; scriptController->m_dupeKey = dupeKey; scriptController->m_dupeScore = dupeScore; scriptController->m_dupeMode = dupeMode; scriptController->m_prefixLen = 0; scriptController->ExecuteScriptList(g_pOptions->GetScanScript()); delete scriptController; } void ScanScriptController::ExecuteScript(ScriptConfig::Script* script) { if (!script->GetScanScript() || !Util::FileExists(m_nzbFilename)) { return; } PrintMessage(Message::mkInfo, "Executing scan-script %s for %s", script->GetName(), Util::BaseFileName(m_nzbFilename)); SetScript(script->GetLocation()); SetArgs(NULL, false); char infoName[1024]; snprintf(infoName, 1024, "scan-script %s for %s", script->GetName(), Util::BaseFileName(m_nzbFilename)); infoName[1024-1] = '\0'; SetInfoName(infoName); SetLogPrefix(script->GetDisplayName()); m_prefixLen = strlen(script->GetDisplayName()) + 2; // 2 = strlen(": "); PrepareParams(script->GetName()); Execute(); SetLogPrefix(NULL); } void ScanScriptController::PrepareParams(const char* scriptName) { ResetEnv(); SetEnvVar("NZBNP_FILENAME", m_nzbFilename); SetEnvVar("NZBNP_URL", m_url); SetEnvVar("NZBNP_NZBNAME", strlen(*m_nzbName) > 0 ? *m_nzbName : Util::BaseFileName(m_nzbFilename)); SetEnvVar("NZBNP_CATEGORY", *m_category); SetIntEnvVar("NZBNP_PRIORITY", *m_priority); SetIntEnvVar("NZBNP_TOP", *m_addTop ? 1 : 0); SetIntEnvVar("NZBNP_PAUSED", *m_addPaused ? 1 : 0); SetEnvVar("NZBNP_DUPEKEY", *m_dupeKey); SetIntEnvVar("NZBNP_DUPESCORE", *m_dupeScore); const char* dupeModeName[] = { "SCORE", "ALL", "FORCE" }; SetEnvVar("NZBNP_DUPEMODE", dupeModeName[*m_dupeMode]); // remove trailing slash char dir[1024]; strncpy(dir, m_directory, 1024); dir[1024-1] = '\0'; int len = strlen(dir); if (dir[len-1] == PATH_SEPARATOR) { dir[len-1] = '\0'; } SetEnvVar("NZBNP_DIRECTORY", dir); PrepareEnvScript(m_parameters, scriptName); } void ScanScriptController::AddMessage(Message::EKind kind, const char* text) { const char* msgText = text + m_prefixLen; if (!strncmp(msgText, "[NZB] ", 6)) { debug("Command %s detected", msgText + 6); if (!strncmp(msgText + 6, "NZBNAME=", 8)) { free(*m_nzbName); *m_nzbName = strdup(msgText + 6 + 8); } else if (!strncmp(msgText + 6, "CATEGORY=", 9)) { free(*m_category); *m_category = strdup(msgText + 6 + 9); g_pScanner->InitPPParameters(*m_category, m_parameters, true); } else if (!strncmp(msgText + 6, "NZBPR_", 6)) { char* param = strdup(msgText + 6 + 6); char* value = strchr(param, '='); if (value) { *value = '\0'; m_parameters->SetParameter(param, value + 1); } else { error("Invalid command \"%s\" received from %s", msgText, GetInfoName()); } free(param); } else if (!strncmp(msgText + 6, "PRIORITY=", 9)) { *m_priority = atoi(msgText + 6 + 9); } else if (!strncmp(msgText + 6, "TOP=", 4)) { *m_addTop = atoi(msgText + 6 + 4) != 0; } else if (!strncmp(msgText + 6, "PAUSED=", 7)) { *m_addPaused = atoi(msgText + 6 + 7) != 0; } else if (!strncmp(msgText + 6, "DUPEKEY=", 8)) { free(*m_dupeKey); *m_dupeKey = strdup(msgText + 6 + 8); } else if (!strncmp(msgText + 6, "DUPESCORE=", 10)) { *m_dupeScore = atoi(msgText + 6 + 10); } else if (!strncmp(msgText + 6, "DUPEMODE=", 9)) { const char* dupeMode = msgText + 6 + 9; if (strcasecmp(dupeMode, "score") && strcasecmp(dupeMode, "all") && strcasecmp(dupeMode, "force")) { error("Invalid value \"%s\" for command \"DUPEMODE\" received from %s", dupeMode, GetInfoName()); return; } *m_dupeMode = !strcasecmp(dupeMode, "all") ? dmAll : !strcasecmp(dupeMode, "force") ? dmForce : dmScore; } else { error("Invalid command \"%s\" received from %s", msgText, GetInfoName()); } } else { ScriptController::AddMessage(kind, text); } }