bool QueueScriptCoordinator::UsableScript(ScriptConfig::Script& script, NzbInfo* nzbInfo, EEvent event) { if (!script.GetQueueScript()) { return false; } if (!Util::EmptyStr(script.GetQueueEvents()) && !strstr(script.GetQueueEvents(), QUEUE_EVENT_NAMES[event])) { return false; } // check extension scripts assigned for that nzb for (NzbParameter& parameter : nzbInfo->GetParameters()) { const char* varname = parameter.GetName(); if (strlen(varname) > 0 && varname[0] != '*' && varname[strlen(varname)-1] == ':' && (!strcasecmp(parameter.GetValue(), "yes") || !strcasecmp(parameter.GetValue(), "on") || !strcasecmp(parameter.GetValue(), "1"))) { BString<1024> scriptName = varname; scriptName[strlen(scriptName)-1] = '\0'; // remove trailing ':' if (FileSystem::SameFilename(scriptName, script.GetName())) { return true; } } } // for URL-events the extension scripts are not assigned yet; // instead we take the default extension scripts for the category (or global) if (event == qeUrlCompleted) { const char* postScript = g_Options->GetExtensions(); if (!Util::EmptyStr(nzbInfo->GetCategory())) { Options::Category* categoryObj = g_Options->FindCategory(nzbInfo->GetCategory(), false); if (categoryObj && !Util::EmptyStr(categoryObj->GetExtensions())) { postScript = categoryObj->GetExtensions(); } } if (!Util::EmptyStr(postScript)) { Tokenizer tok(postScript, ",;"); while (const char* scriptName = tok.Next()) { if (FileSystem::SameFilename(scriptName, script.GetName())) { return true; } } } } return false; }
void QueueScriptCoordinator::InitOptions() { m_bHasQueueScripts = false; for (ScriptConfig::Scripts::iterator it = g_pScriptConfig->GetScripts()->begin(); it != g_pScriptConfig->GetScripts()->end(); it++) { ScriptConfig::Script* pScript = *it; if (pScript->GetQueueScript()) { m_bHasQueueScripts = true; break; } } }
void NZBScriptController::ExecuteScriptList(const char* szScriptList) { for (ScriptConfig::Scripts::iterator it = g_pScriptConfig->GetScripts()->begin(); it != g_pScriptConfig->GetScripts()->end(); it++) { ScriptConfig::Script* pScript = *it; if (szScriptList && *szScriptList) { // split szScriptList into tokens Tokenizer tok(szScriptList, ",;"); while (const char* szScriptName = tok.Next()) { if (Util::SameFilename(szScriptName, pScript->GetName())) { ExecuteScript(pScript); break; } } } } }
void QueueScriptController::AddMessage(Message::EKind eKind, const char* szText) { const char* szMsgText = szText + m_iPrefixLen; if (!strncmp(szMsgText, "[NZB] ", 6)) { debug("Command %s detected", szMsgText + 6); if (!strncmp(szMsgText + 6, "NZBPR_", 6)) { char* szParam = strdup(szMsgText + 6 + 6); char* szValue = strchr(szParam, '='); if (szValue) { *szValue = '\0'; DownloadQueue* pDownloadQueue = DownloadQueue::Lock(); NZBInfo* pNZBInfo = pDownloadQueue->GetQueue()->Find(m_iID); if (pNZBInfo) { pNZBInfo->GetParameters()->SetParameter(szParam, szValue + 1); } DownloadQueue::Unlock(); } else { error("Invalid command \"%s\" received from %s", szMsgText, GetInfoName()); } free(szParam); } else if (!strncmp(szMsgText + 6, "MARK=BAD", 8)) { m_bMarkBad = true; DownloadQueue* pDownloadQueue = DownloadQueue::Lock(); NZBInfo* pNZBInfo = pDownloadQueue->GetQueue()->Find(m_iID); if (pNZBInfo) { SetLogPrefix(NULL); PrintMessage(Message::mkWarning, "Marking %s as bad", m_szNZBName); SetLogPrefix(m_pScript->GetDisplayName()); pNZBInfo->SetMarkStatus(NZBInfo::ksBad); } DownloadQueue::Unlock(); } else { error("Invalid command \"%s\" received from %s", szMsgText, GetInfoName()); } } else { ScriptController::AddMessage(eKind, szText); } }
void QueueScriptController::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, "NZBPR_", 6)) { CString param = msgText + 6 + 6; char* value = strchr(param, '='); if (value) { *value = '\0'; GuardedDownloadQueue downloadQueue = DownloadQueue::Guard(); NzbInfo* nzbInfo = QueueScriptCoordinator::FindNzbInfo(downloadQueue, m_id); if (nzbInfo) { nzbInfo->GetParameters()->SetParameter(param, value + 1); } } else { error("Invalid command \"%s\" received from %s", msgText, GetInfoName()); } } else if (!strncmp(msgText + 6, "DIRECTORY=", 10) && m_event == QueueScriptCoordinator::qeNzbDownloaded) { GuardedDownloadQueue downloadQueue = DownloadQueue::Guard(); NzbInfo* nzbInfo = QueueScriptCoordinator::FindNzbInfo(downloadQueue, m_id); if (nzbInfo) { nzbInfo->SetFinalDir(msgText + 6 + 10); } } else if (!strncmp(msgText + 6, "MARK=BAD", 8)) { m_markBad = true; GuardedDownloadQueue downloadQueue = DownloadQueue::Guard(); NzbInfo* nzbInfo = QueueScriptCoordinator::FindNzbInfo(downloadQueue, m_id); if (nzbInfo) { SetLogPrefix(nullptr); PrintMessage(Message::mkWarning, "Marking %s as bad", *m_nzbName); SetLogPrefix(m_script->GetDisplayName()); nzbInfo->SetMarkStatus(NzbInfo::ksBad); } } else { error("Invalid command \"%s\" received from %s", msgText, GetInfoName()); } } else { NzbInfo* nzbInfo = nullptr; { GuardedDownloadQueue downloadQueue = DownloadQueue::Guard(); nzbInfo = QueueScriptCoordinator::FindNzbInfo(downloadQueue, m_id); if (nzbInfo) { nzbInfo->AddMessage(kind, text); } } if (!nzbInfo) { ScriptController::AddMessage(kind, text); } } }
void QueueScriptCoordinator::EnqueueScript(NZBInfo* pNZBInfo, EEvent eEvent) { if (!m_bHasQueueScripts) { return; } m_mutexQueue.Lock(); if (eEvent == qeNzbDownloaded) { // delete all other queued scripts for this nzb for (Queue::iterator it = m_Queue.begin(); it != m_Queue.end(); ) { QueueItem* pQueueItem = *it; if (pQueueItem->GetNZBID() == pNZBInfo->GetID()) { delete pQueueItem; it = m_Queue.erase(it); continue; } it++; } } // respect option "EventInterval" time_t tCurTime = time(NULL); if (eEvent == qeFileDownloaded && (g_pOptions->GetEventInterval() == -1 || (g_pOptions->GetEventInterval() > 0 && tCurTime - pNZBInfo->GetQueueScriptTime() > 0 && (int)(tCurTime - pNZBInfo->GetQueueScriptTime()) < g_pOptions->GetEventInterval()))) { m_mutexQueue.Unlock(); return; } for (ScriptConfig::Scripts::iterator it = g_pScriptConfig->GetScripts()->begin(); it != g_pScriptConfig->GetScripts()->end(); it++) { ScriptConfig::Script* pScript = *it; if (!pScript->GetQueueScript()) { continue; } bool bUseScript = false; // check queue-scripts const char* szQueueScript = g_pOptions->GetQueueScript(); if (!Util::EmptyStr(szQueueScript)) { // split szQueueScript into tokens Tokenizer tok(szQueueScript, ",;"); while (const char* szScriptName = tok.Next()) { if (Util::SameFilename(szScriptName, pScript->GetName())) { bUseScript = true; break; } } } // check post-processing-scripts if (!bUseScript) { for (NZBParameterList::iterator it = pNZBInfo->GetParameters()->begin(); it != pNZBInfo->GetParameters()->end(); it++) { NZBParameter* pParameter = *it; const char* szVarname = pParameter->GetName(); if (strlen(szVarname) > 0 && szVarname[0] != '*' && szVarname[strlen(szVarname)-1] == ':' && (!strcasecmp(pParameter->GetValue(), "yes") || !strcasecmp(pParameter->GetValue(), "on") || !strcasecmp(pParameter->GetValue(), "1"))) { char szScriptName[1024]; strncpy(szScriptName, szVarname, 1024); szScriptName[1024-1] = '\0'; szScriptName[strlen(szScriptName)-1] = '\0'; // remove trailing ':' if (Util::SameFilename(szScriptName, pScript->GetName())) { bUseScript = true; break; } } } } bUseScript &= Util::EmptyStr(pScript->GetQueueEvents()) || strstr(pScript->GetQueueEvents(), QUEUE_EVENT_NAMES[eEvent]); if (bUseScript) { bool bAlreadyQueued = false; if (eEvent == qeFileDownloaded) { // check if this script is already queued for this nzb for (Queue::iterator it2 = m_Queue.begin(); it2 != m_Queue.end(); it2++) { QueueItem* pQueueItem = *it2; if (pQueueItem->GetNZBID() == pNZBInfo->GetID() && pQueueItem->GetScript() == pScript) { bAlreadyQueued = true; break; } } } if (!bAlreadyQueued) { QueueItem* pQueueItem = new QueueItem(pNZBInfo->GetID(), pScript, eEvent); if (m_pCurItem) { m_Queue.push_back(pQueueItem); } else { StartScript(pNZBInfo, pQueueItem); } } pNZBInfo->SetQueueScriptTime(time(NULL)); } } m_mutexQueue.Unlock(); }