| 1 | # This code is part of Perl distribution HTML-FromMail version 4.00. | ||||||||||||||||||||||||||||
| 2 | # The POD got stripped from this file by OODoc version 3.05. | ||||||||||||||||||||||||||||
| 3 | # For contributors see file ChangeLog. | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # This software is copyright (c) 2003-2025 by Mark Overmeer. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | # This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 8 | # the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 9 | # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | package HTML::FromMail::Default::Previewers;{ | ||||||||||||||||||||||||||||
| 13 | 1 | our $VERSION = '4.00'; | |||||||||||||||||||||||||||
| 14 | } | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use base 'HTML::FromMail::Object'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Log::Report 'html-frommail'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 1 | ▶ | use File::Basename qw/basename dirname/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #-------------------- | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | our @previewers = ( | |||||||||||||||||||||||||||
| 28 | 'text/plain' => \&previewText, | ||||||||||||||||||||||||||||
| 29 | 'text/html' => \&previewHtml, | ||||||||||||||||||||||||||||
| 30 | 'image' => \&previewImage, # added when Image::Magick is installed | ||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub previewText($$$$$) | ||||||||||||||||||||||||||||
| 35 | 0 | ▶ | { my ($page, $message, $part, $attach, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
previewText: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 0 | my $decoded = $attach->{decoded}->string; | |||||||||||||||||||||||||||
| 38 | 0 | for($decoded) | |||||||||||||||||||||||||||
| 39 | 0 | { s/^\s+//; | |||||||||||||||||||||||||||
| 40 | 0 | s/\s+/ /gs; # lists of blanks | |||||||||||||||||||||||||||
| 41 | 0 | s/([!@#$%^&*<>?|:;+=\s-]{5,})/substr($1, 0, 3)/ge; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | my $max = $args->{text_max_chars} || 250; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'text_max_chars'} || 250
MC/DC0%0/1
$args->{'text_max_chars'} || 250
$args->{'text_max_chars'}
Truth table0%0/2
A
A= $args->{'text_max_chars'}
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | substr($decoded, $max) = '' if length $decoded > $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | +{ %$attach, | |||||||||||||||||||||||||||
| 48 | image => '', # this is not an image | ||||||||||||||||||||||||||||
| 49 | html => { text => $decoded }, | ||||||||||||||||||||||||||||
| 50 | }; | ||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub previewHtml($$$$$) | ||||||||||||||||||||||||||||
| 55 | 0 | ▶ | { my ($page, $message, $part, $attach, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
previewHtml: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my $decoded = $attach->{decoded}->string; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $title = $decoded =~ s!\<title\b[^>]*\>(.*?)\</title\>!!i ? $1 : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | for($title) | |||||||||||||||||||||||||||
| 60 | 0 | { s/\<[^>]*\>//g; | |||||||||||||||||||||||||||
| 61 | 0 | s/^\s+//; | |||||||||||||||||||||||||||
| 62 | 0 | s/\s+/ /gs; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | for($decoded) | |||||||||||||||||||||||||||
| 66 | 0 | { s!\<\!\-\-.*?\>!!g; # remove comment | |||||||||||||||||||||||||||
| 67 | 0 | s!\<script.*?script\>!!gsi; # remove script blocks | |||||||||||||||||||||||||||
| 68 | 0 | s!\<style.*?style\>!!gsi; # remove style-sheets | |||||||||||||||||||||||||||
| 69 | 0 | s!^.*\<body!<!gi; # remove all before body | |||||||||||||||||||||||||||
| 70 | 0 | s!\<[^>]*\>!!gs; # remove all tags | |||||||||||||||||||||||||||
| 71 | 0 | s!\s+! !gs; # unfold lines | |||||||||||||||||||||||||||
| 72 | 0 | s/([!@#$%^&*<>?|:;+=\s-]{5,})/substr($1, 0, 3)/ge; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | my $max = $args->{text_max_chars} || 250; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'text_max_chars'} || 250
MC/DC0%0/1
$args->{'text_max_chars'} || 250
$args->{'text_max_chars'}
Truth table0%0/2
A
A= $args->{'text_max_chars'}
| |||||||||||||||||||||||||||||
| 76 | 0 | ▶ | if(length $title) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 0 | { $decoded = "<b>$title</b>, $decoded"; | |||||||||||||||||||||||||||
| 78 | 0 | $max += 7; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | 0 | ▶ | substr($decoded, $max) = '' if length $decoded > $max; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | +{ %$attach, | |||||||||||||||||||||||||||
| 83 | image => '', # this is not an image | ||||||||||||||||||||||||||||
| 84 | html => { text => $decoded }, | ||||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | BEGIN | ||||||||||||||||||||||||||||
| 90 | 1 | ▶ | { eval { require Image::Magick }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.4
| |||||||||||||||||||||||||||||
| 91 | 1 | ▶ | if($@) { warning __x"Image::Magick not installed." } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | else { push @previewers, image => \&previewImage } | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub previewImage($$$$$) | ||||||||||||||||||||||||||||
| 96 | 0 | ▶ | { my ($page, $message, $part, $attach, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
previewImage: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 0 | my $filename = $attach->{filename}; | |||||||||||||||||||||||||||
| 99 | 0 | my $magick = Image::Magick->new; | |||||||||||||||||||||||||||
| 100 | 0 | my $error = $magick->Read($filename); | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | length $error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | and error __x"cannot read image from {fn}: {error}", fn => $filename, error => $error; | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my %image; | |||||||||||||||||||||||||||
| 105 | 0 | my ($srcw, $srch) = @image{ qw/width height/ } = $magick->Get( qw/width height/ ); | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | my $base = basename $filename; | |||||||||||||||||||||||||||
| 108 | 0 | $base =~ s/\.[^.]+$//; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my $dirname = dirname $filename; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | ▶ | my $reqw = $args->{img_max_width} || 250; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'img_max_width'} || 250
MC/DC0%0/1
$args->{'img_max_width'} || 250
$args->{'img_max_width'}
Truth table0%0/2
A
A= $args->{'img_max_width'}
| |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | my $reqh = $args->{img_max_height} || 250; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'img_max_height'} || 250
MC/DC0%0/1
$args->{'img_max_height'} || 250
$args->{'img_max_height'}
Truth table0%0/2
A
A= $args->{'img_max_height'}
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if($reqw < $srcw || $reqh < $srch) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$reqw < $srcw or $reqh < $srch
MC/DC0%0/2
$reqw < $srcw or $reqh < $srch
$reqw < $srcw
$reqh < $srch
Truth table0%0/3
A or B
A= $reqw < $srcwB= $reqh < $srch
| |||||||||||||||||||||||||||||
| 116 | { # Size reduction is needed. | ||||||||||||||||||||||||||||
| 117 | 0 | $error = $magick->Resize(width => $reqw, height => $reqh); | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | length $error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | and error __x"cannot resize image from {fn}: {error}", fn => $filename, error => $error; | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | my ($resw, $resh) = @image{ qw/smallwidth smallheight/ } = $magick->Get( qw/width height/ ); | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | my $outfile = File::Spec->catfile($dirname,"$base-${resw}x${resh}.jpg"); | |||||||||||||||||||||||||||
| 124 | 0 | @image{ qw/smallfile smallurl/ } = ($outfile, basename($outfile)); | |||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | $error = $magick->Write($outfile); | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | length $error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | and error __x"cannot write smaller image from {fn} to {out}: {error}", in => $filename, out => $outfile, error => $error; | ||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | else | ||||||||||||||||||||||||||||
| 131 | 0 | { @image{ qw/smallfile smallurl smallwidth smallheight/ } = ($filename, $attach->{url}, $srcw, $srch); | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | +{ %$attach, | |||||||||||||||||||||||||||
| 135 | image => \%image, | ||||||||||||||||||||||||||||
| 136 | html => '', # this is not text | ||||||||||||||||||||||||||||
| 137 | }; | ||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 1 | 1; | |||||||||||||||||||||||||||