From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by bl2.djc.id.au (Postfix, from userid 1000) id 5CA1929C476; Sun, 18 Oct 2026 12:52:23 +1100 (AEDT) Date: Sun, 18 Oct 2026 12:52:27 +1100 To: sup-commits@supmua.dev Subject: [sup.git] 05/06: move Source.parse_raw_email_header to EditMessageMode MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Reply-To: Dan Callaghan In-Reply-To: <179228834279.389402.1183876010618010579@bl2.djc.id.au> References: <179228834279.389402.1183876010618010579@bl2.djc.id.au> Thread-Index: AQAAEjRWNCbjvxiESNeReEpYhFO1Gg== X-Git-Host: bl2.djc.id.au X-Git-Repo: sup.git X-Git-Refname: refs/heads/develop X-Git-Reftype: branch X-Git-Rev: 7055653583756e8d35928158f50dd7a0accf61ef X-Git-NotificationType: diff X-Git-Multimail-Version: 1.7.dev Auto-Submitted: auto-generated Message-Id: <20261018015223.5CA1929C476@bl2.djc.id.au> From: Dan Callaghan List-Id: New commit on branch develop: https://supmua.dev/git/sup/commit/7055653583756e8d35928158f50dd7a0accf61ef.html commit 7055653583756e8d35928158f50dd7a0accf61ef Author: Dan Callaghan 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(< To: Sally EOS @@ -35,7 +35,7 @@ EOS end def test_multiline - h = Source.parse_raw_email_header StringIO.new(< 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: \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_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(< To: a dear friend @@ -98,7 +98,7 @@ EOS assert_equal "Bob ", h["from"] assert_nil h["to"] - h = Source.parse_raw_email_header StringIO.new(< \r To: a dear friend @@ -106,7 +106,7 @@ EOS assert_equal "Bob ", h["from"] assert_nil h["to"] - h = Source.parse_raw_email_header StringIO.new(< \r\n\r To: a dear friend