From 9aee03308b838cda11b2fdc9dc7bd0605e5545ec Mon Sep 17 00:00:00 2001
From: "Jannis M. Hoffmann" <jannis@fehcom.de>
Date: Sat, 4 Mar 2023 18:11:23 +0100
Subject: better handling of mime_maintype in readmail

---
 lib/JWebmail/Plugin/Helper.pm | 53 ++++++++++++++++++++++++-------------------
 1 file changed, 30 insertions(+), 23 deletions(-)

(limited to 'lib/JWebmail')

diff --git a/lib/JWebmail/Plugin/Helper.pm b/lib/JWebmail/Plugin/Helper.pm
index ed2c31b..a98f245 100644
--- a/lib/JWebmail/Plugin/Helper.pm
+++ b/lib/JWebmail/Plugin/Helper.pm
@@ -2,7 +2,7 @@ package JWebmail::Plugin::Helper;
 
 use Mojo::Base Mojolicious::Plugin;
 
-use List::Util qw(all min max);
+use List::Util qw(all any min max);
 use Carp 'carp';
 use POSIX qw(floor round log ceil);
 
@@ -36,13 +36,13 @@ sub mail_line {
     /xn;
 }
 
-
 sub filter_empty_upload {
     my ($v, $name, $value) = @_;
 
     return $value->filename ? $value : undef;
 }
 
+
 ### template formatting functions
 
 sub print_sizes10 {
@@ -62,7 +62,6 @@ sub print_sizes10 {
     return sprintf('%.0f %s', $var / (10**$expo), $PREFIX[$i]);
 }
 
-
 sub print_sizes2 {
     my $var = shift || return '0 Byte';
 
@@ -80,7 +79,6 @@ sub print_sizes2 {
     return round($var / (2**$expo)) . " $pref";
 }
 
-
 my sub dgt { "([[:digit:]]{$_[0]})" }
 
 sub parse_iso_date {
@@ -100,6 +98,14 @@ sub parse_iso_date {
     };
 }
 
+sub to_mime_type {
+    my $c = shift;
+    my ($mime_head) = @_;
+
+    return "$mime_head->{content_maintype}/$mime_head->{content_subtype}";
+}
+
+
 ### mime type html render functions
 
 my $render_text_plain = sub {
@@ -111,29 +117,27 @@ my $render_text_plain = sub {
     return $content;
 };
 
-
 my $render_text_html = sub {
     my $c_ = shift;
 
     return '<iframe src="' . $c_->url_for('read', id => $c_->stash('id'))->query(body => 'html') . '" class=html-mail></iframe>';
 };
 
-
 our %MIME_Render_Subs = (
     'text/plain' => $render_text_plain,
     'text/html'  => $render_text_html,
 );
 
-
 sub mime_render {
     my ($c, $enc, $cont) = @_;
 
     ($enc) = $enc =~ m<^(\w+/\w+);?>;
 
-    my $renderer = $MIME_Render_Subs{$enc} // return '';
+    my $renderer = $MIME_Render_Subs{$enc} // return;
     return $renderer->($c, $cont);
 };
 
+
 ### session password handling
 
 use constant { S_PASSWD => 'pw', S_OTP_S3D_PW => 'otp_s3d_pw' };
@@ -212,6 +216,7 @@ sub warn_crypt {
     }
 }
 
+
 ### pagination
 
 sub _clamp {
@@ -268,7 +273,7 @@ sub _paginate {
 
 sub paginate {
     my $c = shift;
-    my $count = shift;
+    my ($count) = @_;
 
     my $v = $c->validation;
     my $start = $v->optional('start')->num(0, undef)->param // 0;
@@ -286,6 +291,7 @@ sub paginate {
     return $start, $end;
 }
 
+
 ### registering
 
 sub register {
@@ -293,29 +299,32 @@ sub register {
     $conf //= {};
 
     if (ref $conf->{import} eq 'ARRAY' and my @import = @{ $conf->{import} }) {
-        no warnings 'experimental::smartmatch';
+        my sub contains { any { $_[0] eq $_ } @import }
 
         # selective import
-        $app->helper(print_sizes10  => sub { shift; print_sizes10(@_) })
-            if 'print_sizes10' ~~ @import;
+        $app->helper(print_sizes10 => sub { shift; print_sizes10(@_) })
+            if contains 'print_sizes10';
         $app->helper(parse_iso_date => sub { shift; parse_iso_date(@_) })
-            if 'parse_iso_date' ~~ @import;
-        $app->helper(print_sizes2  => sub { shift; print_sizes2(@_) })
-            if 'print_sizes2' ~~ @import;
-        $app->helper(mime_render    => \&mime_render)
-            if 'mime_render' ~~ @import;
+            if contains 'parse_iso_date';
+        $app->helper(print_sizes2 => sub { shift; print_sizes2(@_) })
+            if contains 'print_sizes2';
+        $app->helper(to_mime_type => \&to_mime_type)
+            if contains 'to_mime_type';
+        $app->helper(mime_render => \&mime_render)
+            if contains 'mime_render';
         $app->helper(session_passwd => \&session_passwd)
-            if 'session_passwd' ~~ @import;
+            if contains 'session_passwd';
         $app->helper(paginate => \&paginate)
-            if 'paginate' ~~ @import;
+            if contains 'paginate';
         $app->validator->add_check(mail_line => \&mail_line)
-            if 'mail_line' ~~ @import;
+            if contains 'mail_line';
         $app->validator->add_filter(non_empty_ul => \&filter_empty_upload)
-            if 'non_empty_ul' ~~ @import;
+            if contains 'non_empty_ul';
     }
     elsif (!$conf->{import}) { # default imports
         $app->helper(print_sizes10  => sub { shift; print_sizes10(@_) });
         $app->helper(parse_iso_date => sub { shift; parse_iso_date(@_) });
+        $app->helper(to_mime_type   => \&to_mime_type);
         $app->helper(mime_render    => \&mime_render);
         $app->helper(session_passwd => \&session_passwd);
         $app->helper(paginate       => \&paginate);
@@ -469,5 +478,3 @@ L<JWebmail>, L<JWebmail::Controller::All>, L<Mojolicious>, L<Mojolicious::Contro
 =head1 NOTICE
 
 This package is part of JWebmail.
-
-=cut
-- 
cgit v1.2.3