[pmg-devel] [PATCH pmg-api v2 1/8] utils: return perl string from decode_rfc1522

Stoiko Ivanov s.ivanov at proxmox.com
Thu Nov 17 16:06:04 CET 2022


decode_rfc1522 is a more robust version of decode_mimewords (in
scalar context) - adapt it to return a perlstring, under the
assumption that data is utf-8 encoded (holds true for ascii and
smtputf8 mails)

Signed-off-by: Stoiko Ivanov <s.ivanov at proxmox.com>
---
 src/PMG/Utils.pm | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/src/PMG/Utils.pm b/src/PMG/Utils.pm
index cef232b..77abde4 100644
--- a/src/PMG/Utils.pm
+++ b/src/PMG/Utils.pm
@@ -1088,6 +1088,7 @@ sub decode_to_html {
     return $res;
 }
 
+# assume enc contains utf-8 and mime-encoded data a perl-string (with wide characters)
 sub decode_rfc1522 {
     my ($enc) = @_;
 
@@ -1100,7 +1101,7 @@ sub decode_rfc1522 {
 	    my ($d, $cs) = @$r;
 	    if ($d) {
 		if ($cs) {
-		    $res .= decode($cs, $d);
+		    $res .= encode('UTF-8', decode($cs, $d));
 		} else {
 		    $res .= $d;
 		}
@@ -1108,8 +1109,11 @@ sub decode_rfc1522 {
 	}
     };
 
-    $res = $enc if $@;
-
+    if ($@) {
+	$res = $enc;
+    } else {
+	$res = decode('UTF-8', $res);
+    }
     return $res;
 }
 
-- 
2.30.2





More information about the pmg-devel mailing list