Mark Sapiro pushed to branch master at GNU Mailman / Mailman Core


Commits:
464fa109 by Mark Sapiro at 2021-06-30T03:49:50+00:00
Don't fold long Message-ID headers when posting to usenet.

- - - - -
dab0af08 by Mark Sapiro at 2021-06-30T03:49:51+00:00
Merge branch 'nntp' into 'master'

Don't fold long Message-ID headers when posting to usenet.

Closes #919

See merge request mailman/mailman!885
- - - - -


3 changed files:

- src/mailman/docs/NEWS.rst
- src/mailman/runners/nntp.py
- src/mailman/runners/tests/test_nntp.py


Changes:

=====================================
src/mailman/docs/NEWS.rst
=====================================
@@ -83,6 +83,8 @@ Bugs
   (Closes #910)
 * Replacements are now expanded in the ``list:user:notice:goodbye`` template.
   (Closes #922)
+* The nntp runner no longer folds long headers in newsgroup posts.  (Closes
+  #919)
 
 Command line
 ------------


=====================================
src/mailman/runners/nntp.py
=====================================
@@ -66,7 +66,9 @@ class NNTPRunner(Runner):
         if not msgdata.get('prepped'):
             prepare_message(mlist, msg, msgdata)
         # Flatten the message object, sticking it in a BytesIO object
-        fp = BytesIO(msg.as_bytes())
+        fp = BytesIO()
+        email.generator.BytesGenerator(fp, maxheaderlen=0).flatten(msg)
+        fp.seek(0)
         conn = None
         try:
             conn = nntplib.NNTP(host, port,
@@ -143,10 +145,12 @@ def prepare_message(mlist, msg, msgdata):
             # Subtitute our new header for the old one.
             del msg['newsgroups']
             msg['Newsgroups'] = COMMASPACE.join(newsgroups)
-    # Ensure we have a Message-ID.
+    # Ensure we have an unfolded Message-ID.
     if not msg.get('message-id'):
         msg['Message-ID'] = email.utils.make_msgid(mlist.list_name,
                                                    mlist.mail_host)
+    mid = re.sub(r'[\s]', '', msg.get('message-id'))
+    msg.replace_header('message-id', mid)
     # Lines: is useful.
     if msg['Lines'] is None:
         # BAW: is there a better way?


=====================================
src/mailman/runners/tests/test_nntp.py
=====================================
@@ -278,6 +278,31 @@ Testing
         msg = message_from_bytes(args[0][0].read())
         self.assertEqual(msg['subject'], 'A newsgroup posting')
 
+    @mock.patch('nntplib.NNTP')
+    def test_post_long_message_id_not_folded(self, class_mock):
+        # Test that the message with a long message-id is posted to the NNTP
+        # server without folding.
+        del self._msg['message-id']
+        mid = utils.make_msgid('a_long_string_to_make_a_very_long_message-id',
+                               self._mlist.mail_host)
+        self._msg['Message-ID'] = mid
+        self._nntpq.enqueue(self._msg, {}, listid='test.example.com')
+        self._runner.run()
+        # Get the mocked instance, which was used in the runner.
+        conn_mock = class_mock()
+        # The connection object's post() method was called once with a
+        # file-like object containing the message's bytes.  Read those bytes
+        # and make some simple checks that the message is what we expected.
+        args = conn_mock.post.call_args
+        # One positional argument.
+        self.assertEqual(len(args[0]), 1)
+        # No keyword arguments.
+        self.assertEqual(len(args[1]), 0)
+        msg_bytes = args[0][0].read()
+        msg = message_from_bytes(msg_bytes)
+        self.assertEqual(msg['subject'], 'A newsgroup posting')
+        self.assertIn(b'Message-ID: ' + mid.encode('us-ascii'), msg_bytes)
+
     @mock.patch('nntplib.NNTP')
     def test_connection_got_quit(self, class_mock):
         # The NNTP connection gets closed after a successful post.



View it on GitLab: 
https://gitlab.com/mailman/mailman/-/compare/700bd86255b9310aba526b322fce3de990882662...dab0af08a2b57f0e5a3f62342acc7e30431442a2

-- 
View it on GitLab: 
https://gitlab.com/mailman/mailman/-/compare/700bd86255b9310aba526b322fce3de990882662...dab0af08a2b57f0e5a3f62342acc7e30431442a2
You're receiving this email because of your account on gitlab.com.


_______________________________________________
Mailman-checkins mailing list -- mailman-checkins@python.org
To unsubscribe send an email to mailman-checkins-le...@python.org
https://mail.python.org/mailman3/lists/mailman-checkins.python.org/
Member address: arch...@jab.org

Reply via email to