-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- improved deobfuscation: - enhanced parsing of the subject field in NZB files, along with improved validation of downloaded article filenames - added a check for filename obfuscation after unpacking; excessively obfuscated filenames are renamed to the NZB filename if the NZB filename is not also obfuscated - new options: - "RenameAfterUnpack" - enables/disables renaming of extracted and obfuscated files using the NZB filename - "RenameIgnoreExt" - a comma separated list of file extensions to ignore when renaming files after unpacking - new value for field "Status" in API-method "listgroups": "POST_UNPACK_RENAMING"
- Loading branch information
Showing
23 changed files
with
626 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* This file is part of nzbget. See <https://nzbget.com>. | ||
* | ||
* Copyright (C) 2024 Denis <denis@nzbget.com> | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "nzbget.h" | ||
|
||
#include "PostUnpackRenamer.h" | ||
#include "FileSystem.h" | ||
#include "Deobfuscation.h" | ||
|
||
namespace PostUnpackRenamer | ||
{ | ||
void Controller::StartJob(PostInfo* postInfo) | ||
{ | ||
Controller* controller = new (std::nothrow) Controller(); | ||
|
||
if (!controller) | ||
{ | ||
error("Failed to allocate memory for PostUnpackRenamer::Controller"); | ||
return; | ||
} | ||
|
||
controller->m_postInfo = postInfo; | ||
controller->SetAutoDestroy(false); | ||
|
||
postInfo->SetPostThread(controller); | ||
|
||
controller->Start(); | ||
} | ||
|
||
void Controller::Run() | ||
{ | ||
{ | ||
GuardedDownloadQueue guard = DownloadQueue::Guard(); | ||
|
||
m_name = m_postInfo->GetNzbInfo()->GetName(); | ||
m_dstDir = m_postInfo->GetNzbInfo()->GetDestDir(); | ||
} | ||
|
||
std::string infoName = "Post-unpack renaming for " + m_name; | ||
SetInfoName(infoName.c_str()); | ||
|
||
if (Deobfuscation::IsExcessivelyObfuscated(m_name)) | ||
{ | ||
PrintMessage(Message::mkWarning, | ||
"Skipping Post-unpack renaming. NZB filename %s is excessively obfuscated which makes renaming unreliable.", | ||
m_name.c_str() | ||
); | ||
m_postInfo->GetNzbInfo()->SetPostUnpackRenamingStatus( | ||
NzbInfo::PostUnpackRenamingStatus::Skipped | ||
); | ||
m_postInfo->SetWorking(false); | ||
return; | ||
} | ||
|
||
bool ok = RenameFiles(m_dstDir, m_name); | ||
|
||
GuardedDownloadQueue guard = DownloadQueue::Guard(); | ||
if (ok) | ||
{ | ||
PrintMessage(Message::mkInfo, "%s successful", infoName.c_str()); | ||
m_postInfo->GetNzbInfo()->SetPostUnpackRenamingStatus( | ||
NzbInfo::PostUnpackRenamingStatus::Success | ||
); | ||
} | ||
else | ||
{ | ||
PrintMessage(Message::mkError, "%s failed", infoName.c_str()); | ||
m_postInfo->GetNzbInfo()->SetPostUnpackRenamingStatus( | ||
NzbInfo::PostUnpackRenamingStatus::Failure | ||
); | ||
} | ||
|
||
m_postInfo->SetWorking(false); | ||
} | ||
|
||
bool Controller::RenameFiles(const std::string& dir, const std::string& newName) | ||
{ | ||
DirBrowser dirBrowser(dir.c_str()); | ||
while (const char* fileOrDir = dirBrowser.Next()) | ||
{ | ||
std::string srcFileOrDir = dir + PATH_SEPARATOR + fileOrDir; | ||
|
||
if (FileSystem::DirectoryExists(srcFileOrDir.c_str())) | ||
{ | ||
RenameFiles(srcFileOrDir, newName); | ||
continue; | ||
} | ||
|
||
if (!Deobfuscation::IsExcessivelyObfuscated(fileOrDir)) | ||
{ | ||
PrintMessage(Message::mkInfo, | ||
"Filename %s is not excessively obfuscated, no renaming needed.", | ||
fileOrDir | ||
); | ||
continue; | ||
} | ||
|
||
std::string dstFile = dir + PATH_SEPARATOR + newName; | ||
dstFile += FileSystem::GetFileExtension(srcFileOrDir).value_or(""); | ||
|
||
if (Util::MatchFileExt(dstFile.c_str(), g_Options->GetRenameIgnoreExt(), ",")) | ||
{ | ||
continue; | ||
} | ||
|
||
if (FileSystem::MoveFile(srcFileOrDir.c_str(), dstFile.c_str())) | ||
{ | ||
PrintMessage(Message::mkInfo, "%s renamed to %s", srcFileOrDir.c_str(), dstFile.c_str()); | ||
} | ||
else | ||
{ | ||
PrintMessage(Message::mkError, | ||
"Could not rename file %s to %s: %s", | ||
srcFileOrDir.c_str(), | ||
dstFile.c_str(), | ||
*FileSystem::GetLastErrorMessage() | ||
); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void Controller::AddMessage(Message::EKind kind, const char* text) | ||
{ | ||
m_postInfo->GetNzbInfo()->AddMessage(kind, text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* This file is part of nzbget. See <https://nzbget.com>. | ||
* | ||
* Copyright (C) 2024 Denis <denis@nzbget.com> | ||
* | ||
* 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 <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
|
||
#ifndef POST_UNPACK_H | ||
#define POST_UNPACK_H | ||
|
||
#include <string> | ||
#include "Thread.h" | ||
#include "ScriptController.h" | ||
#include "DownloadInfo.h" | ||
|
||
namespace PostUnpackRenamer | ||
{ | ||
class Controller final : public Thread, public ScriptController | ||
{ | ||
public: | ||
void Run() override; | ||
static void StartJob(PostInfo* postInfo); | ||
|
||
protected: | ||
void AddMessage(Message::EKind kind, const char* text) override; | ||
|
||
private: | ||
PostInfo* m_postInfo; | ||
std::string m_name; | ||
std::string m_dstDir; | ||
bool RenameFiles(const std::string& dir, const std::string& nameToRename); | ||
}; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.