From 63b15006cd31b5edd8d481e37c3bab3112db1a2a Mon Sep 17 00:00:00 2001
From: toadwastoast <toadwastoast@proton.me>
Date: Fri, 20 Dec 2024 20:03:49 -0300
Subject: [PATCH] deluge: patch for python 3.13

---
 srcpkgs/deluge/patches/replace-cgi.patch | 107 +++++++++++++++++++++++
 srcpkgs/deluge/template                  |   2 +-
 2 files changed, 108 insertions(+), 1 deletion(-)
 create mode 100644 srcpkgs/deluge/patches/replace-cgi.patch

diff --git a/srcpkgs/deluge/patches/replace-cgi.patch b/srcpkgs/deluge/patches/replace-cgi.patch
new file mode 100644
index 00000000000..5f1cf3b5832
--- /dev/null
+++ b/srcpkgs/deluge/patches/replace-cgi.patch
@@ -0,0 +1,107 @@
+From 5d96cfc72f0bfa36d90afd2725aa2216b8073d66 Mon Sep 17 00:00:00 2001
+From: Mamoru TASAKA <mtasaka@fedoraproject.org>
+Date: Thu, 29 Aug 2024 15:31:25 +0900
+Subject: [PATCH] [UI] Replace deprecated cgi module with email
+
+As PEP 594 says, cgi module is marked as deprecated
+in python 3.11, and will be removed in 3.13
+(actually removed at least in 3.13 rc1).
+
+As suggested on PEP 594, replace cgi.parse_header
+with email.message.EmailMessage introduced in python 3.6.
+
+Updated test modify test_download_with_rename_sanitised
+- With RFC2045 specification, Content-Disposition filenames
+parameter containing slash (directory separator) must be
+quoted, so changing as such.
+
+Ref: https://peps.python.org/pep-0594/#deprecated-modules
+Ref: https://peps.python.org/pep-0594/#cgi
+
+Closes: https://github.com/deluge-torrent/deluge/pull/462
+---
+ deluge/httpdownloader.py            | 14 +++++++++-----
+ deluge/tests/test_httpdownloader.py |  4 ++--
+ deluge/ui/web/json_api.py           |  6 ++++--
+ 3 files changed, 15 insertions(+), 9 deletions(-)
+
+diff --git a/deluge/httpdownloader.py b/deluge/httpdownloader.py
+index 700ade06bf..c19e3aa7ed 100644
+--- a/deluge/httpdownloader.py
++++ b/deluge/httpdownloader.py
+@@ -6,7 +6,7 @@
+ # See LICENSE for more details.
+ #
+ 
+-import cgi
++import email.message
+ import logging
+ import os.path
+ import zlib
+@@ -133,9 +133,10 @@ def request_callback(self, response):
+                 content_disp = headers.getRawHeaders(b'content-disposition')[0].decode(
+                     'utf-8'
+                 )
+-                content_disp_params = cgi.parse_header(content_disp)[1]
+-                if 'filename' in content_disp_params:
+-                    new_file_name = content_disp_params['filename']
++                message = email.message.EmailMessage()
++                message['content-disposition'] = content_disp
++                new_file_name = message.get_filename()
++                if new_file_name:
+                     new_file_name = sanitise_filename(new_file_name)
+                     new_file_name = os.path.join(
+                         os.path.split(self.filename)[0], new_file_name
+@@ -152,7 +153,10 @@ def request_callback(self, response):
+                     self.filename = new_file_name
+ 
+             cont_type_header = headers.getRawHeaders(b'content-type')[0].decode()
+-            cont_type, params = cgi.parse_header(cont_type_header)
++            message = email.message.EmailMessage()
++            message['content-type'] = cont_type_header
++            cont_type = message.get_content_type()
++            params = message['content-type'].params
+             # Only re-ecode text content types.
+             encoding = None
+             if cont_type.startswith('text/'):
+diff --git a/deluge/tests/test_httpdownloader.py b/deluge/tests/test_httpdownloader.py
+index 1c27045603..0a4695b5ce 100644
+--- a/deluge/tests/test_httpdownloader.py
++++ b/deluge/tests/test_httpdownloader.py
+@@ -206,10 +206,10 @@ async def test_download_with_rename_exists(self):
+         self.assert_contains(filename, 'This file should be called renamed')
+ 
+     async def test_download_with_rename_sanitised(self):
+-        url = self.get_url('rename?filename=/etc/passwd')
++        url = self.get_url('rename?filename="/etc/passwd"')
+         filename = await download_file(url, fname('original'))
+         assert filename == fname('passwd')
+-        self.assert_contains(filename, 'This file should be called /etc/passwd')
++        self.assert_contains(filename, 'This file should be called "/etc/passwd"')
+ 
+     async def test_download_with_attachment_no_filename(self):
+         url = self.get_url('attachment')
+diff --git a/deluge/ui/web/json_api.py b/deluge/ui/web/json_api.py
+index 1a0e66f77d..dd921c801e 100644
+--- a/deluge/ui/web/json_api.py
++++ b/deluge/ui/web/json_api.py
+@@ -6,7 +6,7 @@
+ # See LICENSE for more details.
+ #
+ 
+-import cgi
++import email.message
+ import json
+ import logging
+ import os
+@@ -191,7 +191,9 @@ def _on_json_request(self, request):
+         Handler to take the json data as a string and pass it on to the
+         _handle_request method for further processing.
+         """
+-        content_type, _ = cgi.parse_header(request.getHeader(b'content-type').decode())
++        message = email.message.EmailMessage()
++        message['content-type'] = request.getHeader(b'content-type').decode()
++        content_type = message.get_content_type()
+         if content_type != 'application/json':
+             message = 'Invalid JSON request content-type: %s' % content_type
+             raise JSONException(message)
diff --git a/srcpkgs/deluge/template b/srcpkgs/deluge/template
index 7c464732902..28f0fec8275 100644
--- a/srcpkgs/deluge/template
+++ b/srcpkgs/deluge/template
@@ -1,7 +1,7 @@
 # Template file for 'deluge'
 pkgname=deluge
 version=2.1.1
-revision=3
+revision=4
 build_style=python3-module
 # TODO package python3-slimit to minify javascript
 hostmakedepends="intltool python3-setuptools python3-wheel"