From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by bl2.djc.id.au (Postfix, from userid 1000) id BD8172D45D3; Sat, 28 Nov 2026 17:13:35 +1100 (AEDT) Date: Sat, 28 Nov 2026 17:13:41 +1100 To: sup-commits@supmua.dev Subject: [sup.git] 06/08: use libmagic to detect attachment MIME types MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Reply-To: Dan Callaghan In-Reply-To: <179584641514.302139.17817016268117078700@bl2.djc.id.au> References: <179584641514.302139.17817016268117078700@bl2.djc.id.au> Thread-Index: AQAAEjRW8AyDbWtbRRaCMIXB6V3YXw== 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: 07f797437938d23a35e7541eccf99c93c899d2f6 X-Git-NotificationType: diff X-Git-Multimail-Version: 1.7.dev Auto-Submitted: auto-generated Message-Id: <20261128061335.BD8172D45D3@bl2.djc.id.au> From: Dan Callaghan List-Id: New commit on branch develop: https://supmua.dev/git/sup/commit/07f797437938d23a35e7541eccf99c93c899d2f6.html commit 07f797437938d23a35e7541eccf99c93c899d2f6 Author: Dan Callaghan AuthorDate: Fri Nov 20 09:43:34 2026 +1100 use libmagic to detect attachment MIME types Fixes #282. --- Manifest.txt | 21 +++++++++++ contrib/nix/Gemfile | 2 +- contrib/nix/Gemfile.lock | 9 ++--- contrib/nix/gemset.nix | 42 ++++++---------------- contrib/nix/ruby2.4-Gemfile.lock | 7 ++-- contrib/nix/ruby2.4-gemset.nix | 32 ++++++----------- contrib/nix/ruby2.4-shell.nix | 4 +++ contrib/nix/ruby2.5-Gemfile.lock | 7 ++-- contrib/nix/ruby2.5-gemset.nix | 32 ++++++----------- contrib/nix/ruby2.5-shell.nix | 4 +++ contrib/nix/ruby2.6-Gemfile.lock | 7 ++-- contrib/nix/ruby2.6-gemset.nix | 32 ++++++----------- contrib/nix/ruby2.6-shell.nix | 4 +++ contrib/nix/ruby2.7-Gemfile.lock | 7 ++-- contrib/nix/ruby2.7-gemset.nix | 32 ++++++----------- contrib/nix/ruby2.7-shell.nix | 6 ++++ contrib/nix/ruby3.0-Gemfile.lock | 7 ++-- contrib/nix/ruby3.0-gemset.nix | 32 ++++++----------- contrib/nix/ruby3.0-shell.nix | 6 ++++ contrib/nix/ruby3.1-Gemfile.lock | 9 ++--- contrib/nix/ruby3.1-gemset.nix | 42 ++++++---------------- contrib/nix/ruby3.1-shell.nix | 6 ++++ contrib/nix/ruby3.2-Gemfile.lock | 9 ++--- contrib/nix/ruby3.2-gemset.nix | 42 ++++++---------------- contrib/nix/ruby3.2-shell.nix | 6 ++++ contrib/nix/ruby3.3-shell.nix | 6 ++++ contrib/nix/ruby3.4-shell.nix | 4 +++ contrib/nix/ruby4.0-shell.nix | 4 +++ lib/sup/util.rb | 21 ++++++----- sup.gemspec | 2 +- test/fixtures/attachments/Cargo.toml | 4 +++ test/fixtures/attachments/Dockerfile | 3 ++ test/fixtures/attachments/Hello.pm | 7 ++++ test/fixtures/attachments/Makefile | 2 ++ test/fixtures/attachments/board.dtb | Bin 0 -> 145 bytes test/fixtures/attachments/board.dts | 6 ++++ test/fixtures/attachments/component.wasm | Bin 0 -> 8 bytes test/fixtures/attachments/configure | 2 ++ test/fixtures/attachments/cp1251.txt | 1 + test/fixtures/attachments/firmware.hex | 2 ++ test/fixtures/attachments/git-format.patch | 23 ++++++++++++ test/fixtures/attachments/go.mod | 3 ++ test/fixtures/attachments/hello | Bin 0 -> 132 bytes test/fixtures/attachments/hello.c | 8 +++++ test/fixtures/attachments/hello.py | 2 ++ test/fixtures/attachments/koi8.txt | 1 + test/fixtures/attachments/main.rs | 3 ++ test/fixtures/attachments/main.ts | 2 ++ test/fixtures/attachments/minimal.pdf | 14 ++++++++ test/fixtures/attachments/module.wasm | Bin 0 -> 13 bytes test/fixtures/attachments/traditional.patch | 9 +++++ test/unit/test_edit_message_mode.rb | 12 +++---- test/unit/test_rmail_message.rb | 54 ++++++++++++++++++++++++++-- 53 files changed, 346 insertions(+), 256 deletions(-) diff --git a/Manifest.txt b/Manifest.txt index 03b5678e..472b0372 100644 --- a/Manifest.txt +++ b/Manifest.txt @@ -139,6 +139,27 @@ lib/sup/version.rb sup.gemspec test/dummy_buffer.rb test/dummy_source.rb +test/fixtures/attachments/Cargo.toml +test/fixtures/attachments/Dockerfile +test/fixtures/attachments/Hello.pm +test/fixtures/attachments/Makefile +test/fixtures/attachments/board.dtb +test/fixtures/attachments/board.dts +test/fixtures/attachments/component.wasm +test/fixtures/attachments/configure +test/fixtures/attachments/cp1251.txt +test/fixtures/attachments/firmware.hex +test/fixtures/attachments/git-format.patch +test/fixtures/attachments/go.mod +test/fixtures/attachments/hello +test/fixtures/attachments/hello.c +test/fixtures/attachments/hello.py +test/fixtures/attachments/koi8.txt +test/fixtures/attachments/main.rs +test/fixtures/attachments/main.ts +test/fixtures/attachments/minimal.pdf +test/fixtures/attachments/module.wasm +test/fixtures/attachments/traditional.patch test/fixtures/bad-content-transfer-encoding-1.eml test/fixtures/binary-content-transfer-encoding-2.eml test/fixtures/blank-header-fields.eml diff --git a/contrib/nix/Gemfile b/contrib/nix/Gemfile index 6afd5a2f..0ac67a73 100644 --- a/contrib/nix/Gemfile +++ b/contrib/nix/Gemfile @@ -6,7 +6,7 @@ gem "rmail", ">= 1.1.2", "< 2" gem "highline" gem "optimist" gem "lockfile" -gem "mime-types", "> 2.0" +gem "ruby-magic" gem "locale", "~> 2.0" gem "chronic" gem "unicode", "~> 0.4.4" diff --git a/contrib/nix/Gemfile.lock b/contrib/nix/Gemfile.lock index 10ad08a0..16ca0dbd 100644 --- a/contrib/nix/Gemfile.lock +++ b/contrib/nix/Gemfile.lock @@ -17,12 +17,7 @@ GEM locale (2.1.5) fiddle lockfile (2.1.3) - logger (1.7.0) method_source (1.1.0) - mime-types (3.7.0) - logger - mime-types-data (~> 3.2025, >= 3.2025.0507) - mime-types-data (3.2026.0922) mini_portile2 (2.8.9) minitest (5.27.0) ncursesw (1.4.15) @@ -61,6 +56,8 @@ GEM rubocop-packaging (0.6.0) lint_roller (~> 1.1.0) rubocop (>= 1.72.1, < 2.0) + ruby-magic (0.6.0) + mini_portile2 (~> 2.8) ruby-progressbar (1.13.0) unicode (0.4.4.5) unicode-display_width (3.3.0) @@ -90,7 +87,6 @@ DEPENDENCIES highline locale (~> 2.0) lockfile - mime-types (> 2.0) minitest (~> 5.5) ncursesw (~> 1.4.0) optimist @@ -99,6 +95,7 @@ DEPENDENCIES rmail (>= 1.1.2, < 2) rr (~> 1.1) rubocop-packaging + ruby-magic unicode (~> 0.4.4) unicode-display_width xapian-ruby (~> 1.2) diff --git a/contrib/nix/gemset.nix b/contrib/nix/gemset.nix index 1472295e..28f31b42 100644 --- a/contrib/nix/gemset.nix +++ b/contrib/nix/gemset.nix @@ -132,16 +132,6 @@ }; version = "2.1.3"; }; - logger = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "00q2zznygpbls8asz5knjvvj2brr3ghmqxgr83xnrdj4rk3xwvhr"; - type = "gem"; - }; - version = "1.7.0"; - }; method_source = { groups = ["default"]; platforms = []; @@ -152,27 +142,6 @@ }; version = "1.1.0"; }; - mime-types = { - dependencies = ["logger" "mime-types-data"]; - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "0mjyxl7c0xzyqdqa8r45hqg7jcw2prp3hkp39mdf223g4hfgdsyw"; - type = "gem"; - }; - version = "3.7.0"; - }; - mime-types-data = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "1bjq9hppcmpawin2a6px1msc3mdxjvndj73rfklg771v1zr8r6zl"; - type = "gem"; - }; - version = "3.2026.0922"; - }; mini_portile2 = { groups = ["default"]; platforms = []; @@ -359,6 +328,17 @@ }; version = "0.6.0"; }; + ruby-magic = { + dependencies = ["mini_portile2"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "00b15fd74bkrxfqbx1gg2nw78fs7lvmn8mf92bway8vxgf3kh8bv"; + type = "gem"; + }; + version = "0.6.0"; + }; ruby-progressbar = { groups = ["default"]; platforms = []; diff --git a/contrib/nix/ruby2.4-Gemfile.lock b/contrib/nix/ruby2.4-Gemfile.lock index 99130e4e..ff8bc2e4 100644 --- a/contrib/nix/ruby2.4-Gemfile.lock +++ b/contrib/nix/ruby2.4-Gemfile.lock @@ -11,9 +11,6 @@ GEM locale (2.1.3) lockfile (2.1.3) method_source (1.0.0) - mime-types (3.4.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2022.0105) mini_portile2 (2.8.0) minitest (5.15.0) ncursesw (1.4.15) @@ -43,6 +40,8 @@ GEM parser (>= 2.7.1.5) rubocop-packaging (0.5.1) rubocop (>= 0.89, < 2.0) + ruby-magic (0.6.0) + mini_portile2 (~> 2.8) ruby-progressbar (1.11.0) unicode (0.4.4.5) unicode-display_width (2.2.0) @@ -66,7 +65,6 @@ DEPENDENCIES highline locale (~> 2.0) lockfile - mime-types (> 2.0) minitest (~> 5.5) ncursesw (~> 1.4.0) optimist @@ -75,6 +73,7 @@ DEPENDENCIES rmail (>= 1.1.2, < 2) rr (~> 1.1) rubocop-packaging + ruby-magic unicode (~> 0.4.4) unicode-display_width xapian-ruby (~> 1.2) diff --git a/contrib/nix/ruby2.4-gemset.nix b/contrib/nix/ruby2.4-gemset.nix index 0e550914..681136b8 100644 --- a/contrib/nix/ruby2.4-gemset.nix +++ b/contrib/nix/ruby2.4-gemset.nix @@ -90,27 +90,6 @@ }; version = "1.0.0"; }; - mime-types = { - dependencies = ["mime-types-data"]; - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "0ipw892jbksbxxcrlx9g5ljq60qx47pm24ywgfbyjskbcl78pkvb"; - type = "gem"; - }; - version = "3.4.1"; - }; - mime-types-data = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "003gd7mcay800k2q4pb2zn8lwwgci4bhi42v2jvlidm8ksx03i6q"; - type = "gem"; - }; - version = "3.2022.0105"; - }; mini_portile2 = { groups = ["default"]; platforms = []; @@ -276,6 +255,17 @@ }; version = "0.5.1"; }; + ruby-magic = { + dependencies = ["mini_portile2"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "00b15fd74bkrxfqbx1gg2nw78fs7lvmn8mf92bway8vxgf3kh8bv"; + type = "gem"; + }; + version = "0.6.0"; + }; ruby-progressbar = { groups = ["default"]; platforms = []; diff --git a/contrib/nix/ruby2.4-shell.nix b/contrib/nix/ruby2.4-shell.nix index 3663b55a..526179ab 100644 --- a/contrib/nix/ruby2.4-shell.nix +++ b/contrib/nix/ruby2.4-shell.nix @@ -11,6 +11,10 @@ let lockfile = ./ruby2.4-Gemfile.lock; gemset = ./ruby2.4-gemset.nix; gemConfig = pkgs.defaultGemConfig // { + ruby-magic = attrs: { + buildInputs = [ pkgs.file ]; + buildFlags = [ "--enable-system-libraries" ]; + }; # Workaround: remove rake from nativeBuildInputs, otherwise it causes # xapian-bindings to build against the default Ruby version # instead of our chosen version. diff --git a/contrib/nix/ruby2.5-Gemfile.lock b/contrib/nix/ruby2.5-Gemfile.lock index 974b47d2..3cd32ffe 100644 --- a/contrib/nix/ruby2.5-Gemfile.lock +++ b/contrib/nix/ruby2.5-Gemfile.lock @@ -11,9 +11,6 @@ GEM locale (2.1.3) lockfile (2.1.3) method_source (1.0.0) - mime-types (3.4.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2022.0105) mini_portile2 (2.8.0) minitest (5.16.2) ncursesw (1.4.15) @@ -43,6 +40,8 @@ GEM parser (>= 3.1.1.0) rubocop-packaging (0.5.1) rubocop (>= 0.89, < 2.0) + ruby-magic (0.6.0) + mini_portile2 (~> 2.8) ruby-progressbar (1.11.0) unicode (0.4.4.5) unicode-display_width (2.2.0) @@ -66,7 +65,6 @@ DEPENDENCIES highline locale (~> 2.0) lockfile - mime-types (> 2.0) minitest (~> 5.5) ncursesw (~> 1.4.0) optimist @@ -75,6 +73,7 @@ DEPENDENCIES rmail (>= 1.1.2, < 2) rr (~> 1.1) rubocop-packaging + ruby-magic unicode (~> 0.4.4) unicode-display_width xapian-ruby (~> 1.2) diff --git a/contrib/nix/ruby2.5-gemset.nix b/contrib/nix/ruby2.5-gemset.nix index 277912a3..eb56d5cf 100644 --- a/contrib/nix/ruby2.5-gemset.nix +++ b/contrib/nix/ruby2.5-gemset.nix @@ -90,27 +90,6 @@ }; version = "1.0.0"; }; - mime-types = { - dependencies = ["mime-types-data"]; - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "0ipw892jbksbxxcrlx9g5ljq60qx47pm24ywgfbyjskbcl78pkvb"; - type = "gem"; - }; - version = "3.4.1"; - }; - mime-types-data = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "003gd7mcay800k2q4pb2zn8lwwgci4bhi42v2jvlidm8ksx03i6q"; - type = "gem"; - }; - version = "3.2022.0105"; - }; mini_portile2 = { groups = ["default"]; platforms = []; @@ -276,6 +255,17 @@ }; version = "0.5.1"; }; + ruby-magic = { + dependencies = ["mini_portile2"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "00b15fd74bkrxfqbx1gg2nw78fs7lvmn8mf92bway8vxgf3kh8bv"; + type = "gem"; + }; + version = "0.6.0"; + }; ruby-progressbar = { groups = ["default"]; platforms = []; diff --git a/contrib/nix/ruby2.5-shell.nix b/contrib/nix/ruby2.5-shell.nix index d80847b0..a45b6fa5 100644 --- a/contrib/nix/ruby2.5-shell.nix +++ b/contrib/nix/ruby2.5-shell.nix @@ -11,6 +11,10 @@ let lockfile = ./ruby2.5-Gemfile.lock; gemset = ./ruby2.5-gemset.nix; gemConfig = pkgs.defaultGemConfig // { + ruby-magic = attrs: { + buildInputs = [ pkgs.file ]; + buildFlags = [ "--enable-system-libraries" ]; + }; # Workaround: remove rake from nativeBuildInputs, otherwise it causes # xapian-bindings to build against the default Ruby version # instead of our chosen version. diff --git a/contrib/nix/ruby2.6-Gemfile.lock b/contrib/nix/ruby2.6-Gemfile.lock index c98c8f9d..51f5d3ee 100644 --- a/contrib/nix/ruby2.6-Gemfile.lock +++ b/contrib/nix/ruby2.6-Gemfile.lock @@ -12,9 +12,6 @@ GEM locale (2.1.3) lockfile (2.1.3) method_source (1.0.0) - mime-types (3.4.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2022.0105) mini_portile2 (2.8.5) minitest (5.16.2) ncursesw (1.4.15) @@ -45,6 +42,8 @@ GEM parser (>= 3.1.1.0) rubocop-packaging (0.5.1) rubocop (>= 0.89, < 2.0) + ruby-magic (0.6.0) + mini_portile2 (~> 2.8) ruby-progressbar (1.11.0) unicode (0.4.4.5) unicode-display_width (2.2.0) @@ -68,7 +67,6 @@ DEPENDENCIES highline locale (~> 2.0) lockfile - mime-types (> 2.0) minitest (~> 5.5) ncursesw (~> 1.4.0) optimist @@ -77,6 +75,7 @@ DEPENDENCIES rmail (>= 1.1.2, < 2) rr (~> 1.1) rubocop-packaging + ruby-magic unicode (~> 0.4.4) unicode-display_width xapian-ruby (~> 1.2) diff --git a/contrib/nix/ruby2.6-gemset.nix b/contrib/nix/ruby2.6-gemset.nix index a6adc858..e9cbe7ac 100644 --- a/contrib/nix/ruby2.6-gemset.nix +++ b/contrib/nix/ruby2.6-gemset.nix @@ -100,27 +100,6 @@ }; version = "1.0.0"; }; - mime-types = { - dependencies = ["mime-types-data"]; - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "0ipw892jbksbxxcrlx9g5ljq60qx47pm24ywgfbyjskbcl78pkvb"; - type = "gem"; - }; - version = "3.4.1"; - }; - mime-types-data = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "003gd7mcay800k2q4pb2zn8lwwgci4bhi42v2jvlidm8ksx03i6q"; - type = "gem"; - }; - version = "3.2022.0105"; - }; mini_portile2 = { groups = ["default"]; platforms = []; @@ -286,6 +265,17 @@ }; version = "0.5.1"; }; + ruby-magic = { + dependencies = ["mini_portile2"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "00b15fd74bkrxfqbx1gg2nw78fs7lvmn8mf92bway8vxgf3kh8bv"; + type = "gem"; + }; + version = "0.6.0"; + }; ruby-progressbar = { groups = ["default"]; platforms = []; diff --git a/contrib/nix/ruby2.6-shell.nix b/contrib/nix/ruby2.6-shell.nix index f656d3bc..e79b7f35 100644 --- a/contrib/nix/ruby2.6-shell.nix +++ b/contrib/nix/ruby2.6-shell.nix @@ -11,6 +11,10 @@ let lockfile = ./ruby2.6-Gemfile.lock; gemset = ./ruby2.6-gemset.nix; gemConfig = pkgs.defaultGemConfig // { + ruby-magic = attrs: { + buildInputs = [ pkgs.file ]; + buildFlags = [ "--enable-system-libraries" ]; + }; # Workaround: remove rake from nativeBuildInputs, otherwise it causes # xapian-bindings to build against the default Ruby version # instead of our chosen version. diff --git a/contrib/nix/ruby2.7-Gemfile.lock b/contrib/nix/ruby2.7-Gemfile.lock index f8e98b8b..26d77781 100644 --- a/contrib/nix/ruby2.7-Gemfile.lock +++ b/contrib/nix/ruby2.7-Gemfile.lock @@ -13,9 +13,6 @@ GEM locale (2.1.4) lockfile (2.1.3) method_source (1.1.0) - mime-types (3.5.2) - mime-types-data (~> 3.2015) - mime-types-data (3.2024.0305) mini_portile2 (2.8.6) minitest (5.22.3) ncursesw (1.4.15) @@ -49,6 +46,8 @@ GEM parser (>= 3.3.0.4) rubocop-packaging (0.5.2) rubocop (>= 1.33, < 2.0) + ruby-magic (0.6.0) + mini_portile2 (~> 2.8) ruby-progressbar (1.13.0) unicode (0.4.4.5) unicode-display_width (2.5.0) @@ -72,7 +71,6 @@ DEPENDENCIES highline locale (~> 2.0) lockfile - mime-types (> 2.0) minitest (~> 5.5) ncursesw (~> 1.4.0) optimist @@ -81,6 +79,7 @@ DEPENDENCIES rmail (>= 1.1.2, < 2) rr (~> 1.1) rubocop-packaging + ruby-magic unicode (~> 0.4.4) unicode-display_width xapian-ruby (~> 1.2) diff --git a/contrib/nix/ruby2.7-gemset.nix b/contrib/nix/ruby2.7-gemset.nix index 3f72cb2f..98b5cc28 100644 --- a/contrib/nix/ruby2.7-gemset.nix +++ b/contrib/nix/ruby2.7-gemset.nix @@ -110,27 +110,6 @@ }; version = "1.1.0"; }; - mime-types = { - dependencies = ["mime-types-data"]; - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "1r64z0m5zrn4k37wabfnv43wa6yivgdfk6cf2rpmmirlz889yaf1"; - type = "gem"; - }; - version = "3.5.2"; - }; - mime-types-data = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "00x7w5xqsj9m33v3vkmy23wipkkysafksib53ypzn27p5g81w455"; - type = "gem"; - }; - version = "3.2024.0305"; - }; mini_portile2 = { groups = ["default"]; platforms = []; @@ -306,6 +285,17 @@ }; version = "0.5.2"; }; + ruby-magic = { + dependencies = ["mini_portile2"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "00b15fd74bkrxfqbx1gg2nw78fs7lvmn8mf92bway8vxgf3kh8bv"; + type = "gem"; + }; + version = "0.6.0"; + }; ruby-progressbar = { groups = ["default"]; platforms = []; diff --git a/contrib/nix/ruby2.7-shell.nix b/contrib/nix/ruby2.7-shell.nix index 17e59e60..e6edc744 100644 --- a/contrib/nix/ruby2.7-shell.nix +++ b/contrib/nix/ruby2.7-shell.nix @@ -10,5 +10,11 @@ let gemfile = ./Gemfile; lockfile = ./ruby2.7-Gemfile.lock; gemset = ./ruby2.7-gemset.nix; + gemConfig = pkgs.defaultGemConfig // { + ruby-magic = attrs: { + buildInputs = [ pkgs.file ]; + buildFlags = [ "--enable-system-libraries" ]; + }; + }; }; in pkgs.mkShell { packages = [ gems gems.wrappedRuby pkgs.pandoc ]; } diff --git a/contrib/nix/ruby3.0-Gemfile.lock b/contrib/nix/ruby3.0-Gemfile.lock index f8e98b8b..26d77781 100644 --- a/contrib/nix/ruby3.0-Gemfile.lock +++ b/contrib/nix/ruby3.0-Gemfile.lock @@ -13,9 +13,6 @@ GEM locale (2.1.4) lockfile (2.1.3) method_source (1.1.0) - mime-types (3.5.2) - mime-types-data (~> 3.2015) - mime-types-data (3.2024.0305) mini_portile2 (2.8.6) minitest (5.22.3) ncursesw (1.4.15) @@ -49,6 +46,8 @@ GEM parser (>= 3.3.0.4) rubocop-packaging (0.5.2) rubocop (>= 1.33, < 2.0) + ruby-magic (0.6.0) + mini_portile2 (~> 2.8) ruby-progressbar (1.13.0) unicode (0.4.4.5) unicode-display_width (2.5.0) @@ -72,7 +71,6 @@ DEPENDENCIES highline locale (~> 2.0) lockfile - mime-types (> 2.0) minitest (~> 5.5) ncursesw (~> 1.4.0) optimist @@ -81,6 +79,7 @@ DEPENDENCIES rmail (>= 1.1.2, < 2) rr (~> 1.1) rubocop-packaging + ruby-magic unicode (~> 0.4.4) unicode-display_width xapian-ruby (~> 1.2) diff --git a/contrib/nix/ruby3.0-gemset.nix b/contrib/nix/ruby3.0-gemset.nix index 3f72cb2f..98b5cc28 100644 --- a/contrib/nix/ruby3.0-gemset.nix +++ b/contrib/nix/ruby3.0-gemset.nix @@ -110,27 +110,6 @@ }; version = "1.1.0"; }; - mime-types = { - dependencies = ["mime-types-data"]; - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "1r64z0m5zrn4k37wabfnv43wa6yivgdfk6cf2rpmmirlz889yaf1"; - type = "gem"; - }; - version = "3.5.2"; - }; - mime-types-data = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "00x7w5xqsj9m33v3vkmy23wipkkysafksib53ypzn27p5g81w455"; - type = "gem"; - }; - version = "3.2024.0305"; - }; mini_portile2 = { groups = ["default"]; platforms = []; @@ -306,6 +285,17 @@ }; version = "0.5.2"; }; + ruby-magic = { + dependencies = ["mini_portile2"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "00b15fd74bkrxfqbx1gg2nw78fs7lvmn8mf92bway8vxgf3kh8bv"; + type = "gem"; + }; + version = "0.6.0"; + }; ruby-progressbar = { groups = ["default"]; platforms = []; diff --git a/contrib/nix/ruby3.0-shell.nix b/contrib/nix/ruby3.0-shell.nix index 9c373877..e34518be 100644 --- a/contrib/nix/ruby3.0-shell.nix +++ b/contrib/nix/ruby3.0-shell.nix @@ -10,5 +10,11 @@ let gemfile = ./Gemfile; lockfile = ./ruby3.0-Gemfile.lock; gemset = ./ruby3.0-gemset.nix; + gemConfig = pkgs.defaultGemConfig // { + ruby-magic = attrs: { + buildInputs = [ pkgs.file ]; + buildFlags = [ "--enable-system-libraries" ]; + }; + }; }; in pkgs.mkShell { packages = [ gems gems.wrappedRuby pkgs.pandoc ]; } diff --git a/contrib/nix/ruby3.1-Gemfile.lock b/contrib/nix/ruby3.1-Gemfile.lock index 7a0bea61..4c09cad4 100644 --- a/contrib/nix/ruby3.1-Gemfile.lock +++ b/contrib/nix/ruby3.1-Gemfile.lock @@ -14,12 +14,7 @@ GEM language_server-protocol (3.17.0.4) locale (2.1.4) lockfile (2.1.3) - logger (1.7.0) method_source (1.1.0) - mime-types (3.6.2) - logger - mime-types-data (~> 3.2015) - mime-types-data (3.2025.0402) mini_portile2 (2.8.8) minitest (5.25.5) ncursesw (1.4.15) @@ -55,6 +50,8 @@ GEM parser (>= 3.3.0.4) rubocop-packaging (0.5.2) rubocop (>= 1.33, < 2.0) + ruby-magic (0.6.0) + mini_portile2 (~> 2.8) ruby-progressbar (1.13.0) unicode (0.4.4.5) unicode-display_width (2.5.0) @@ -82,7 +79,6 @@ DEPENDENCIES highline locale (~> 2.0) lockfile - mime-types (> 2.0) minitest (~> 5.5) ncursesw (~> 1.4.0) optimist @@ -91,6 +87,7 @@ DEPENDENCIES rmail (>= 1.1.2, < 2) rr (~> 1.1) rubocop-packaging + ruby-magic unicode (~> 0.4.4) unicode-display_width xapian-ruby (~> 1.2) diff --git a/contrib/nix/ruby3.1-gemset.nix b/contrib/nix/ruby3.1-gemset.nix index abb19bc7..7b20f777 100644 --- a/contrib/nix/ruby3.1-gemset.nix +++ b/contrib/nix/ruby3.1-gemset.nix @@ -111,16 +111,6 @@ }; version = "2.1.3"; }; - logger = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "00q2zznygpbls8asz5knjvvj2brr3ghmqxgr83xnrdj4rk3xwvhr"; - type = "gem"; - }; - version = "1.7.0"; - }; method_source = { groups = ["default"]; platforms = []; @@ -131,27 +121,6 @@ }; version = "1.1.0"; }; - mime-types = { - dependencies = ["logger" "mime-types-data"]; - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "1bv08jvx1g9ifjdyrp5hgalxkv7qxwfmfx0ba43ncrbfda7182b1"; - type = "gem"; - }; - version = "3.6.2"; - }; - mime-types-data = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "1h0rhy0zf4ssrdr6cyliqijxga94cd64ij2gkw6jdx3bww1cap25"; - type = "gem"; - }; - version = "3.2025.0402"; - }; mini_portile2 = { groups = ["default"]; platforms = []; @@ -338,6 +307,17 @@ }; version = "0.5.2"; }; + ruby-magic = { + dependencies = ["mini_portile2"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "00b15fd74bkrxfqbx1gg2nw78fs7lvmn8mf92bway8vxgf3kh8bv"; + type = "gem"; + }; + version = "0.6.0"; + }; ruby-progressbar = { groups = ["default"]; platforms = []; diff --git a/contrib/nix/ruby3.1-shell.nix b/contrib/nix/ruby3.1-shell.nix index ef8487d4..a86282bc 100644 --- a/contrib/nix/ruby3.1-shell.nix +++ b/contrib/nix/ruby3.1-shell.nix @@ -10,5 +10,11 @@ let gemfile = ./Gemfile; lockfile = ./ruby3.1-Gemfile.lock; gemset = ./ruby3.1-gemset.nix; + gemConfig = pkgs.defaultGemConfig // { + ruby-magic = attrs: { + buildInputs = [ pkgs.file ]; + buildFlags = [ "--enable-system-libraries" ]; + }; + }; }; in pkgs.mkShell { packages = [ gems gems.wrappedRuby pkgs.pandoc ]; } diff --git a/contrib/nix/ruby3.2-Gemfile.lock b/contrib/nix/ruby3.2-Gemfile.lock index 7a0bea61..4c09cad4 100644 --- a/contrib/nix/ruby3.2-Gemfile.lock +++ b/contrib/nix/ruby3.2-Gemfile.lock @@ -14,12 +14,7 @@ GEM language_server-protocol (3.17.0.4) locale (2.1.4) lockfile (2.1.3) - logger (1.7.0) method_source (1.1.0) - mime-types (3.6.2) - logger - mime-types-data (~> 3.2015) - mime-types-data (3.2025.0402) mini_portile2 (2.8.8) minitest (5.25.5) ncursesw (1.4.15) @@ -55,6 +50,8 @@ GEM parser (>= 3.3.0.4) rubocop-packaging (0.5.2) rubocop (>= 1.33, < 2.0) + ruby-magic (0.6.0) + mini_portile2 (~> 2.8) ruby-progressbar (1.13.0) unicode (0.4.4.5) unicode-display_width (2.5.0) @@ -82,7 +79,6 @@ DEPENDENCIES highline locale (~> 2.0) lockfile - mime-types (> 2.0) minitest (~> 5.5) ncursesw (~> 1.4.0) optimist @@ -91,6 +87,7 @@ DEPENDENCIES rmail (>= 1.1.2, < 2) rr (~> 1.1) rubocop-packaging + ruby-magic unicode (~> 0.4.4) unicode-display_width xapian-ruby (~> 1.2) diff --git a/contrib/nix/ruby3.2-gemset.nix b/contrib/nix/ruby3.2-gemset.nix index abb19bc7..7b20f777 100644 --- a/contrib/nix/ruby3.2-gemset.nix +++ b/contrib/nix/ruby3.2-gemset.nix @@ -111,16 +111,6 @@ }; version = "2.1.3"; }; - logger = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "00q2zznygpbls8asz5knjvvj2brr3ghmqxgr83xnrdj4rk3xwvhr"; - type = "gem"; - }; - version = "1.7.0"; - }; method_source = { groups = ["default"]; platforms = []; @@ -131,27 +121,6 @@ }; version = "1.1.0"; }; - mime-types = { - dependencies = ["logger" "mime-types-data"]; - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "1bv08jvx1g9ifjdyrp5hgalxkv7qxwfmfx0ba43ncrbfda7182b1"; - type = "gem"; - }; - version = "3.6.2"; - }; - mime-types-data = { - groups = ["default"]; - platforms = []; - source = { - remotes = ["https://rubygems.org"]; - sha256 = "1h0rhy0zf4ssrdr6cyliqijxga94cd64ij2gkw6jdx3bww1cap25"; - type = "gem"; - }; - version = "3.2025.0402"; - }; mini_portile2 = { groups = ["default"]; platforms = []; @@ -338,6 +307,17 @@ }; version = "0.5.2"; }; + ruby-magic = { + dependencies = ["mini_portile2"]; + groups = ["default"]; + platforms = []; + source = { + remotes = ["https://rubygems.org"]; + sha256 = "00b15fd74bkrxfqbx1gg2nw78fs7lvmn8mf92bway8vxgf3kh8bv"; + type = "gem"; + }; + version = "0.6.0"; + }; ruby-progressbar = { groups = ["default"]; platforms = []; diff --git a/contrib/nix/ruby3.2-shell.nix b/contrib/nix/ruby3.2-shell.nix index f15c31ef..7f0215be 100644 --- a/contrib/nix/ruby3.2-shell.nix +++ b/contrib/nix/ruby3.2-shell.nix @@ -10,5 +10,11 @@ let gemfile = ./Gemfile; lockfile = ./ruby3.2-Gemfile.lock; gemset = ./ruby3.2-gemset.nix; + gemConfig = pkgs.defaultGemConfig // { + ruby-magic = attrs: { + buildInputs = [ pkgs.file ]; + buildFlags = [ "--enable-system-libraries" ]; + }; + }; }; in pkgs.mkShell { packages = [ gems gems.wrappedRuby pkgs.pandoc ]; } diff --git a/contrib/nix/ruby3.3-shell.nix b/contrib/nix/ruby3.3-shell.nix index 4064000e..d225948e 100644 --- a/contrib/nix/ruby3.3-shell.nix +++ b/contrib/nix/ruby3.3-shell.nix @@ -10,5 +10,11 @@ let gemfile = ./Gemfile; lockfile = ./Gemfile.lock; gemset = ./gemset.nix; + gemConfig = pkgs.defaultGemConfig // { + ruby-magic = attrs: { + buildInputs = [ pkgs.file ]; + buildFlags = [ "--enable-system-libraries" ]; + }; + }; }; in pkgs.mkShell { packages = [ gems gems.wrappedRuby pkgs.pandoc ]; } diff --git a/contrib/nix/ruby3.4-shell.nix b/contrib/nix/ruby3.4-shell.nix index 39a842b1..8fc458b8 100644 --- a/contrib/nix/ruby3.4-shell.nix +++ b/contrib/nix/ruby3.4-shell.nix @@ -22,6 +22,10 @@ let }) ]; }; + ruby-magic = attrs: { + buildInputs = [ pkgs.file ]; + buildFlags = [ "--enable-system-libraries" ]; + }; }; }; in pkgs.mkShell { packages = [ gems gems.wrappedRuby pkgs.pandoc ]; } diff --git a/contrib/nix/ruby4.0-shell.nix b/contrib/nix/ruby4.0-shell.nix index 37716a16..70dd1a9f 100644 --- a/contrib/nix/ruby4.0-shell.nix +++ b/contrib/nix/ruby4.0-shell.nix @@ -22,6 +22,10 @@ let }) ]; }; + ruby-magic = attrs: { + buildInputs = [ pkgs.file ]; + buildFlags = [ "--enable-system-libraries" ]; + }; xapian-ruby = attrs: pkgs.defaultGemConfig.xapian-ruby attrs diff --git a/lib/sup/util.rb b/lib/sup/util.rb index b4d58f9a..5096bd12 100644 --- a/lib/sup/util.rb +++ b/lib/sup/util.rb @@ -2,7 +2,7 @@ require 'thread' require 'lockfile' -require 'mime/types' +require 'magic' require 'pathname' require 'rmail' require 'set' @@ -79,15 +79,21 @@ module RMail class Message def self.make_file_attachment fn bfn = File.basename fn - t = MIME::Types.type_for(bfn).first || MIME::Types.type_for("exe").first + content_type = File.mime(fn) || "application/octet-stream" + content_type = content_type.sub(/; charset=binary/, "") if content_type =~ /^application\// payload = IO.binread fn - ## Need to encode as base64 or quoted-printable if any lines are longer than 998 chars. - encoding = if t.encoding != t.default_encoding and payload.each_line.any? { |l| l.length > 998 } - t.default_encoding + encoding = if content_type =~ /^text\/|^message\/|^application\/mbox\b/i + if payload.include?("\0") || payload.each_line.any? { |l| l.bytesize > 998 } || payload =~ /\r(?!\n)/ + "quoted-printable" + elsif !payload.ascii_only? + "8bit" + else + "7bit" + end else - t.encoding + "base64" end - make_attachment payload, t.content_type, encoding, bfn.to_s + make_attachment payload, content_type, encoding.dup, bfn.to_s end def charset @@ -650,4 +656,3 @@ class FinishLine @m.synchronize { !@over && @over = true } end end - diff --git a/sup.gemspec b/sup.gemspec index c3d8b3d0..8d30490e 100644 --- a/sup.gemspec +++ b/sup.gemspec @@ -55,7 +55,7 @@ SUP: please note that our old mailing lists have been shut down, s.add_runtime_dependency "highline" s.add_runtime_dependency "optimist" s.add_runtime_dependency "lockfile" - s.add_runtime_dependency "mime-types", "> 2.0" + s.add_runtime_dependency "ruby-magic" s.add_runtime_dependency "locale", "~> 2.0" s.add_runtime_dependency "chronic" s.add_runtime_dependency "unicode", "~> 0.4.4" diff --git a/test/fixtures/attachments/Cargo.toml b/test/fixtures/attachments/Cargo.toml new file mode 100644 index 00000000..60a39166 --- /dev/null +++ b/test/fixtures/attachments/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" diff --git a/test/fixtures/attachments/Dockerfile b/test/fixtures/attachments/Dockerfile new file mode 100644 index 00000000..d4ff707c --- /dev/null +++ b/test/fixtures/attachments/Dockerfile @@ -0,0 +1,3 @@ +FROM scratch +COPY hello / +CMD ["/hello"] diff --git a/test/fixtures/attachments/Hello.pm b/test/fixtures/attachments/Hello.pm new file mode 100644 index 00000000..ace1e670 --- /dev/null +++ b/test/fixtures/attachments/Hello.pm @@ -0,0 +1,7 @@ +package Hello; +use strict; +use warnings; + +sub greet { return "hello" } + +1; diff --git a/test/fixtures/attachments/Makefile b/test/fixtures/attachments/Makefile new file mode 100644 index 00000000..f554f032 --- /dev/null +++ b/test/fixtures/attachments/Makefile @@ -0,0 +1,2 @@ +hello: hello.c + $(CC) -o $@ $< diff --git a/test/fixtures/attachments/board.dtb b/test/fixtures/attachments/board.dtb new file mode 100644 index 00000000..a6fff46e Binary files /dev/null and b/test/fixtures/attachments/board.dtb differ diff --git a/test/fixtures/attachments/board.dts b/test/fixtures/attachments/board.dts new file mode 100644 index 00000000..c8904203 --- /dev/null +++ b/test/fixtures/attachments/board.dts @@ -0,0 +1,6 @@ +/dts-v1/; + +/ { + model = "Example Board"; + compatible = "example,board"; +}; diff --git a/test/fixtures/attachments/component.wasm b/test/fixtures/attachments/component.wasm new file mode 100644 index 00000000..13d20e9f Binary files /dev/null and b/test/fixtures/attachments/component.wasm differ diff --git a/test/fixtures/attachments/configure b/test/fixtures/attachments/configure new file mode 100755 index 00000000..9539c581 --- /dev/null +++ b/test/fixtures/attachments/configure @@ -0,0 +1,2 @@ +#!/bin/sh +echo "checking for a C compiler... cc" diff --git a/test/fixtures/attachments/cp1251.txt b/test/fixtures/attachments/cp1251.txt new file mode 100644 index 00000000..2fbf7fb1 --- /dev/null +++ b/test/fixtures/attachments/cp1251.txt @@ -0,0 +1 @@ +� ���� ��������! diff --git a/test/fixtures/attachments/firmware.hex b/test/fixtures/attachments/firmware.hex new file mode 100644 index 00000000..ccea65f0 --- /dev/null +++ b/test/fixtures/attachments/firmware.hex @@ -0,0 +1,2 @@ +:04000000DEADBEEFC4 +:00000001FF diff --git a/test/fixtures/attachments/git-format.patch b/test/fixtures/attachments/git-format.patch new file mode 100644 index 00000000..42532efc --- /dev/null +++ b/test/fixtures/attachments/git-format.patch @@ -0,0 +1,23 @@ +From 192aa8d87e24c94592647cbd804a64c3d4d01c07 Mon Sep 17 00:00:00 2001 +From: Dan Callaghan +Date: Sun, 1 Nov 2026 20:26:40 +1100 +Subject: [PATCH] exit 42 + +--- + hello.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git a/hello.c b/hello.c +index c8629c1..fd76ffb 100644 +--- a/hello.c ++++ b/hello.c +@@ -1,5 +1,5 @@ + int main(void) + { + printf("hello\n"); +- return 0; ++ return 42; + } +-- +2.54.0 + diff --git a/test/fixtures/attachments/go.mod b/test/fixtures/attachments/go.mod new file mode 100644 index 00000000..476c039e --- /dev/null +++ b/test/fixtures/attachments/go.mod @@ -0,0 +1,3 @@ +module example.com/hello + +go 1.21 diff --git a/test/fixtures/attachments/hello b/test/fixtures/attachments/hello new file mode 100755 index 00000000..019a592d Binary files /dev/null and b/test/fixtures/attachments/hello differ diff --git a/test/fixtures/attachments/hello.c b/test/fixtures/attachments/hello.c new file mode 100644 index 00000000..27441bf4 --- /dev/null +++ b/test/fixtures/attachments/hello.c @@ -0,0 +1,8 @@ +/* Copyright © 2026 Zoë Example */ +#include + +int main(void) +{ + printf("hello\n"); + return 0; +} diff --git a/test/fixtures/attachments/hello.py b/test/fixtures/attachments/hello.py new file mode 100644 index 00000000..8df28191 --- /dev/null +++ b/test/fixtures/attachments/hello.py @@ -0,0 +1,2 @@ +#!/usr/bin/env python3 +print("hello") diff --git a/test/fixtures/attachments/koi8.txt b/test/fixtures/attachments/koi8.txt new file mode 100644 index 00000000..ed8a41f0 --- /dev/null +++ b/test/fixtures/attachments/koi8.txt @@ -0,0 +1 @@ +������������! diff --git a/test/fixtures/attachments/main.rs b/test/fixtures/attachments/main.rs new file mode 100644 index 00000000..75275760 --- /dev/null +++ b/test/fixtures/attachments/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("hello"); +} diff --git a/test/fixtures/attachments/main.ts b/test/fixtures/attachments/main.ts new file mode 100644 index 00000000..a2b6b306 --- /dev/null +++ b/test/fixtures/attachments/main.ts @@ -0,0 +1,2 @@ +const greeting: string = "hello"; +console.log(greeting); diff --git a/test/fixtures/attachments/minimal.pdf b/test/fixtures/attachments/minimal.pdf new file mode 100644 index 00000000..857888f0 --- /dev/null +++ b/test/fixtures/attachments/minimal.pdf @@ -0,0 +1,14 @@ +%PDF-1.0 +1 0 obj<>endobj +2 0 obj<>endobj +3 0 obj<>>>endobj +xref +0 4 +0000000000 65535 f +0000000009 00000 n +0000000052 00000 n +0000000101 00000 n +trailer<> +startxref +178 +%%EOF diff --git a/test/fixtures/attachments/module.wasm b/test/fixtures/attachments/module.wasm new file mode 100644 index 00000000..00066296 Binary files /dev/null and b/test/fixtures/attachments/module.wasm differ diff --git a/test/fixtures/attachments/traditional.patch b/test/fixtures/attachments/traditional.patch new file mode 100644 index 00000000..d06790aa --- /dev/null +++ b/test/fixtures/attachments/traditional.patch @@ -0,0 +1,9 @@ +diff -ur hello.orig/hello.c hello/hello.c +--- hello.orig/hello.c 2026-11-01 21:02:17.097858746 +1100 ++++ hello/hello.c 2026-11-01 21:02:20.600966920 +1100 +@@ -1,4 +1,4 @@ + int main(void) + { +- return 0; ++ return 42; + } diff --git a/test/unit/test_edit_message_mode.rb b/test/unit/test_edit_message_mode.rb index 5259fbe1..914252c8 100644 --- a/test/unit/test_edit_message_mode.rb +++ b/test/unit/test_edit_message_mode.rb @@ -122,10 +122,8 @@ class TestEditMessageMode < Minitest::Test def test_attachment_8bit_non_utf8 body_content = "löl noñ-ÀSCII bodý" ## CP1251 is not UTF-8 compatible. - ## This is a CSV only due to a quirk of mime-types, which wants to use 8bit for text/csv - ## (whereas text/plain is quoted-printable). attachment_content = "С днем рождения!\n".encode("cp1251") - attachment_filename = File.join @path, "cp1251.csv" + attachment_filename = File.join @path, "cp1251.txt" File.write attachment_filename, attachment_content opts = { @@ -134,7 +132,7 @@ class TestEditMessageMode < Minitest::Test "To" => +"recip@example.invalid", }, :attachments => { - "cp1251.csv" => RMail::Message.make_file_attachment(attachment_filename), + "cp1251.txt" => RMail::Message.make_file_attachment(attachment_filename), }, :body => [body_content], } @@ -148,13 +146,13 @@ class TestEditMessageMode < Minitest::Test assert_equal "8bit", inline.header["Content-Transfer-Encoding"] attachment = msg.part(1) assert_equal attachment_content.b, attachment.body - assert_equal 'text/csv; name="cp1251.csv"', attachment.header["Content-Type"] - assert_equal 'attachment; filename="cp1251.csv"', attachment.header["Content-Disposition"] + assert_equal 'text/plain; charset=iso-8859-1; name="cp1251.txt"', attachment.header["Content-Type"] + assert_equal 'attachment; filename="cp1251.txt"', attachment.header["Content-Disposition"] assert_equal "8bit", attachment.header["Content-Transfer-Encoding"] ## Also check the serialized message, which is what the send_message method uses. assert msg.to_s.lines.include? body_content.b + "\n" - assert msg.to_s.lines.include? "Content-Type: text/csv; name=\"cp1251.csv\"\n" + assert msg.to_s.lines.include? "Content-Type: text/plain; charset=iso-8859-1; name=\"cp1251.txt\"\n" assert msg.to_s.lines.include? attachment_content.b end end diff --git a/test/unit/test_rmail_message.rb b/test/unit/test_rmail_message.rb index d0ef48f1..25a8d1d1 100644 --- a/test/unit/test_rmail_message.rb +++ b/test/unit/test_rmail_message.rb @@ -16,21 +16,69 @@ class TestRMailMessage < Minitest::Test File.write filename, "" a = RMail::Message.make_file_attachment(filename) - assert_equal "text/html; name=\"test.html\"", a.header["Content-Type"] + assert_equal "text/html; charset=us-ascii; name=\"test.html\"", a.header["Content-Type"] assert_equal "attachment; filename=\"test.html\"", a.header["Content-Disposition"] - assert_equal "8bit", a.header["Content-Transfer-Encoding"] + assert_equal "7bit", a.header["Content-Transfer-Encoding"] end def test_make_file_attachment_text_with_long_lines + skip "libmagic 5.40 misdetects this attachment as application/octet-stream" if Magic.version == 540 filename = File.join @path, "test.html" File.write filename, "a" * 1023 a = RMail::Message.make_file_attachment(filename) - assert_equal "text/html; name=\"test.html\"", a.header["Content-Type"] + assert_equal "text/plain; charset=us-ascii; name=\"test.html\"", a.header["Content-Type"] assert_equal "attachment; filename=\"test.html\"", a.header["Content-Disposition"] assert_equal "quoted-printable", a.header["Content-Transfer-Encoding"] qp_encoded = ("a" * 73 + "=\n") * 14 + "a=\n" assert_equal qp_encoded, a.body end + + def test_make_file_attachment_mimetype_guessing + [ + ["git-format.patch", 'application/mbox; charset=us-ascii; name="git-format.patch"', "7bit"], + ["traditional.patch", 'text/x-diff; charset=us-ascii; name="traditional.patch"', "7bit"], + ["configure", 'text/x-shellscript; charset=us-ascii; name="configure"', "7bit"], + ["hello.c", 'text/x-c; charset=utf-8; name="hello.c"', "8bit"], + ["Makefile", 'text/plain; charset=us-ascii; name="Makefile"', "7bit"], + ["Dockerfile", 'text/plain; charset=us-ascii; name="Dockerfile"', "7bit"], + ["Cargo.toml", 'text/plain; charset=us-ascii; name="Cargo.toml"', "7bit"], + ["firmware.hex", 'text/x-hex; charset=us-ascii; name="firmware.hex"', "7bit"], + ["go.mod", 'text/plain; charset=us-ascii; name="go.mod"', "7bit"], + ["Hello.pm", 'text/plain; charset=us-ascii; name="Hello.pm"', "7bit"], + ["hello.py", 'text/x-script.python; charset=us-ascii; name="hello.py"', "7bit"], + ["main.rs", 'text/x-c; charset=us-ascii; name="main.rs"', "7bit"], + ["main.ts", 'text/plain; charset=us-ascii; name="main.ts"', "7bit"], + ["koi8.txt", 'text/plain; charset=iso-8859-1; name="koi8.txt"', "8bit"], + ["cp1251.txt", 'text/plain; charset=iso-8859-1; name="cp1251.txt"', "8bit"], + ["minimal.pdf", 'application/pdf; charset=us-ascii; name="minimal.pdf"', "base64"], + ["board.dtb", 'application/octet-stream; name="board.dtb"', "base64"], + ["hello", 'application/x-executable; name="hello"', "base64"], + ["module.wasm", 'application/wasm; name="module.wasm"', "base64"], + ["component.wasm", 'application/wasm; name="component.wasm"', "base64"], + ].each do |fn, expected_content_type, expected_encoding| + a = RMail::Message.make_file_attachment fixture_path("attachments/#{fn}") + expected_content_type.sub!(/text\/x-hex/, "text/plain") unless Magic.version >= 546 + expected_content_type.sub!(/application\/mbox/, "text/x-diff") unless Magic.version >= 546 + expected_content_type.sub!(/application\/wasm/, "application/octet-stream") unless Magic.version >= 543 + expected_content_type.sub!(/application\/wasm/, "application/octet-stream") if fn == "module.wasm" and Magic.version >= 547 + expected_content_type.sub!(/application\/wasm/, "application/octet-stream") if fn == "component.wasm" and Magic.version < 547 + assert_equal expected_content_type, a.header["Content-Type"] + assert_equal expected_encoding, a.header["Content-Transfer-Encoding"] + assert_equal File.binread(fixture_path("attachments/#{fn}")), a.decode + end + end + + def test_make_file_attachment_nested_emails + [ + ["simple-message.eml", 'message/rfc822; charset=us-ascii; name="simple-message.eml"', "7bit"], + ["utf8-header.eml", 'message/rfc822; charset=utf-8; name="utf8-header.eml"', "8bit"], + ].each do |fn, expected_content_type, expected_encoding| + a = RMail::Message.make_file_attachment fixture_path(fn) + assert_equal expected_content_type, a.header["Content-Type"] + assert_equal expected_encoding, a.header["Content-Transfer-Encoding"] + assert_equal File.binread(fixture_path(fn)), a.decode + end + end end