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::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(); }