Commit notifications for all Sup repositories
 help / color / mirror / Atom feed
From: Dan Callaghan <dan@bl2.djc.id.au>
To: sup-commits@supmua.dev
Subject: [sup.git] 05/06: move Source.parse_raw_email_header to EditMessageMode
Date: Sun, 18 Oct 2026 12:52:27 +1100	[thread overview]
Message-ID: <20261018015223.5CA1929C476@bl2.djc.id.au> (raw)
In-Reply-To: <179228834279.389402.1183876010618010579@bl2.djc.id.au>

New commit on branch develop:
https://supmua.dev/git/sup/commit/7055653583756e8d35928158f50dd7a0accf61ef.html

commit 7055653583756e8d35928158f50dd7a0accf61ef
Author: Dan Callaghan <djc@djc.id.au>
AuthorDate: Sat Oct 17 13:54:32 2026 +1100

    move Source.parse_raw_email_header to EditMessageMode
    
    This method was originally used to parse all mail headers for indexing
    and display, but nowadays Sup uses RMail for this purpose instead. The
    only remaining caller of this method is EditMessageMode when loading
    messages composed by the user in their editor.
    
    Since this parsing behaviour is looser than Rmail, and maybe more
    friendly for humans editing mail by hand in their editor, preserve it as
    is but move it to EditMessageMode so its actual purpose is clear. And
    remove the inaccurate comment about being "speed-critical".
---
 lib/sup/modes/edit_message_mode.rb | 39 +++++++++++++++++++++++++++++++-
 lib/sup/source.rb                  | 46 --------------------------------------
 test/test_header_parsing.rb        | 18 +++++++--------
 3 files changed, 47 insertions(+), 56 deletions(-)

diff --git a/lib/sup/modes/edit_message_mode.rb b/lib/sup/modes/edit_message_mode.rb
index 8cba15ba..e69a9bcc 100644
--- a/lib/sup/modes/edit_message_mode.rb
+++ b/lib/sup/modes/edit_message_mode.rb
@@ -451,9 +451,46 @@ protected
     end
   end
 
+  ## utility method to read a raw email header from an IO stream and turn it
+  ## into a hash of key-value pairs. minor special semantics for certain headers.
+  def self.parse_raw_email_header f
+    header = {}
+    last = nil
+
+    while(line = f.gets)
+      case line
+      ## these three can occur multiple times, and we want the first one
+      when /^(Delivered-To|X-Original-To|Envelope-To):\s*(.*?)\s*$/i; header[last = $1.downcase] ||= $2
+      ## regular header: overwrite (not that we should see more than one)
+      ## TODO: figure out whether just using the first occurrence changes
+      ## anything (which would simplify the logic slightly)
+      when /^([^:\s]+):\s*(.*?)\s*$/i; header[last = $1.downcase] = $2
+      when /^\r*$/; break # blank line signifies end of header
+      else
+        if last
+          header[last] << " " unless header[last].empty?
+          header[last] << line.strip
+        end
+      end
+    end
+
+    %w(subject from to cc bcc).each do |k|
+      v = header[k] or next
+      next unless Rfc2047.is_encoded? v
+      header[k] = begin
+        Rfc2047.decode_to $encoding, v
+      rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::IllegalSequence
+        v
+      end
+    end
+    header
+  end
+
+  def parse_raw_email_header f; self.class.parse_raw_email_header f end
+
   def parse_file fn
     File.open(fn) do |f|
-      header = Source.parse_raw_email_header(f).inject({}) { |h, (k, v)| h[k.capitalize] = v; h } # lousy HACK
+      header = parse_raw_email_header(f).inject({}) { |h, (k, v)| h[k.capitalize] = v; h } # lousy HACK
       body = f.readlines.map { |l| l.chomp }
 
       header.delete_if { |k, v| NON_EDITABLE_HEADERS.member? k }
diff --git a/lib/sup/source.rb b/lib/sup/source.rb
index 29120c18..56d2e7d7 100644
--- a/lib/sup/source.rb
+++ b/lib/sup/source.rb
@@ -121,52 +121,6 @@ class Source
     debug "lock released for: #{self}"
   end
 
-  ## utility method to read a raw email header from an IO stream and turn it
-  ## into a hash of key-value pairs. minor special semantics for certain headers.
-  ##
-  ## THIS IS A SPEED-CRITICAL SECTION. Everything you do here will have a
-  ## significant effect on Sup's processing speed of email from ALL sources.
-  ## Little things like string interpolation, regexp interpolation, += vs <<,
-  ## all have DRAMATIC effects. BE CAREFUL WHAT YOU DO!
-  def self.parse_raw_email_header f
-    header = {}
-    last = nil
-
-    while(line = f.gets)
-      case line
-      ## these three can occur multiple times, and we want the first one
-      when /^(Delivered-To|X-Original-To|Envelope-To):\s*(.*?)\s*$/i; header[last = $1.downcase] ||= $2
-      ## regular header: overwrite (not that we should see more than one)
-      ## TODO: figure out whether just using the first occurrence changes
-      ## anything (which would simplify the logic slightly)
-      when /^([^:\s]+):\s*(.*?)\s*$/i; header[last = $1.downcase] = $2
-      when /^\r*$/; break # blank line signifies end of header
-      else
-        if last
-          header[last] << " " unless header[last].empty?
-          header[last] << line.strip
-        end
-      end
-    end
-
-    %w(subject from to cc bcc).each do |k|
-      v = header[k] or next
-      next unless Rfc2047.is_encoded? v
-      header[k] = begin
-        Rfc2047.decode_to $encoding, v
-      rescue Errno::EINVAL, Iconv::InvalidEncoding, Iconv::IllegalSequence
-        #debug "warning: error decoding RFC 2047 header (#{e.class.name}): #{e.message}"
-        v
-      end
-    end
-    header
-  end
-
-protected
-
-  ## convenience function
-  def parse_raw_email_header f; self.class.parse_raw_email_header f end
-
   def Source.expand_filesystem_uri uri
     uri.gsub "~", File.expand_path("~")
   end
diff --git a/test/test_header_parsing.rb b/test/test_header_parsing.rb
index 7008834f..5ccc20ea 100644
--- a/test/test_header_parsing.rb
+++ b/test/test_header_parsing.rb
@@ -24,7 +24,7 @@ class TestMBoxParsing < Minitest::Test
   end
 
   def test_normal_headers
-    h = Source.parse_raw_email_header StringIO.new(<<EOS)
+    h = EditMessageMode.parse_raw_email_header StringIO.new(<<EOS)
 From: Bob <bob@bob.com>
 To: Sally <sally@sally.com>
 EOS
@@ -35,7 +35,7 @@ EOS
   end
 
   def test_multiline
-    h = Source.parse_raw_email_header StringIO.new(<<EOS)
+    h = EditMessageMode.parse_raw_email_header StringIO.new(<<EOS)
 From: Bob <bob@bob.com>
 Subject: one two three
   four five six
@@ -57,7 +57,7 @@ EOS
       "Subject:   one two  three   end    \n",
     ]
     variants.each do |s|
-      h = Source.parse_raw_email_header StringIO.new(s)
+      h = EditMessageMode.parse_raw_email_header StringIO.new(s)
       assert_equal "one two  three   end", h["subject"]
     end
   end
@@ -68,13 +68,13 @@ EOS
       "Message-Id:<one@bob.com>       \n",
     ]
     variants.each do |s|
-      h = Source.parse_raw_email_header StringIO.new(s)
+      h = EditMessageMode.parse_raw_email_header StringIO.new(s)
       assert_equal "<one@bob.com>", h["message-id"]
     end
   end
 
   def test_blank_lines
-    h = Source.parse_raw_email_header StringIO.new("")
+    h = EditMessageMode.parse_raw_email_header StringIO.new("")
     assert_nil h["message-id"]
   end
 
@@ -84,13 +84,13 @@ EOS
       "Message-Id:\n",
     ]
     variants.each do |s|
-      h = Source.parse_raw_email_header StringIO.new(s)
+      h = EditMessageMode.parse_raw_email_header StringIO.new(s)
       assert_equal "", h["message-id"]
     end
   end
 
   def test_detect_end_of_headers
-    h = Source.parse_raw_email_header StringIO.new(<<EOS)
+    h = EditMessageMode.parse_raw_email_header StringIO.new(<<EOS)
 From: Bob <bob@bob.com>
 
 To: a dear friend
@@ -98,7 +98,7 @@ EOS
   assert_equal "Bob <bob@bob.com>", h["from"]
   assert_nil h["to"]
 
-  h = Source.parse_raw_email_header StringIO.new(<<EOS)
+  h = EditMessageMode.parse_raw_email_header StringIO.new(<<EOS)
 From: Bob <bob@bob.com>
 \r
 To: a dear friend
@@ -106,7 +106,7 @@ EOS
   assert_equal "Bob <bob@bob.com>", h["from"]
   assert_nil h["to"]
 
-  h = Source.parse_raw_email_header StringIO.new(<<EOS)
+  h = EditMessageMode.parse_raw_email_header StringIO.new(<<EOS)
 From: Bob <bob@bob.com>
 \r\n\r
 To: a dear friend

  parent reply	other threads:[~2026-10-18  1:52 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-18  1:52 [sup.git] branch develop updated (cf3a2e38 -> c9f29b8d) Dan Callaghan
2026-10-18  1:52 ` [sup.git] 01/06: prepend type:mail to every search before parsing Dan Callaghan
2026-10-18  1:52 ` [sup.git] 02/06: always set MIME-Version header on outgoing mails Dan Callaghan
2026-10-18  1:52 ` [sup.git] 03/06: unfold headers before further parsing Dan Callaghan
2026-10-18  1:52 ` [sup.git] 04/06: delete unused load_header method Dan Callaghan
2026-10-18  1:52 ` Dan Callaghan [this message]
2026-10-18  1:52 ` [sup.git] 06/06: ncursesw: 1.4.13 → 1.4.14 Dan Callaghan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261018015223.5CA1929C476@bl2.djc.id.au \
    --to=dan@bl2.djc.id.au \
    --cc=djc@djc.id.au \
    --cc=sup-commits@supmua.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox