From 2546a725a2196bf6ad4e3db9da337d5b5b4a8fe8 Mon Sep 17 00:00:00 2001 From: Dean Pemberton Date: Thu, 31 Jul 2008 12:31:24 +1200 Subject: [PATCH] Initial changes to support HTML format emails --- bin/mythtv-status | 110 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 99 insertions(+), 11 deletions(-) diff --git a/bin/mythtv-status b/bin/mythtv-status index 36e6cee..3ea7cd6 100755 --- a/bin/mythtv-status +++ b/bin/mythtv-status @@ -103,6 +103,15 @@ if (defined $colour && scalar(@email) == 0) { $normal = "\033[0m"; } +my $safe_html = ''; +my $warning_html = ''; +my $normal_html = ''; +if (defined $colour && scalar(@email) == 1) { + $safe_html = ''; + $warning_html = ''; + $normal_html = ''; +} + # Is a warning present? my $warn_present = 0; @@ -144,6 +153,7 @@ my @blocks = ( 'name' => 'One Liners', 'type' => 'sub', 'template' => '', + 'template_html' => '', 'sub' => sub { return 'Place holder' }, }, @@ -154,6 +164,7 @@ my @blocks = ( 'xpath' => "//Status", 'attrs' => [ qw/time date/ ], 'template' => "__date__, __time__", + 'template_html' => "__date__, __time__
", 'format' => 'one line' }, @@ -164,6 +175,7 @@ my @blocks = ( 'xpath' => "//Status/Encoders/Encoder", 'attrs' => [ qw/hostname id state connected/ ], 'template' => "__hostname__ (__id__) - __state____connected__", + 'template_html' => "__hostname__ (__id__) - __state____connected__
", 'rewrite' => { '/connected/' => { '1' => '', '0' => ' (Disconnected)' }, '/state/' => { @@ -183,6 +195,7 @@ my @blocks = ( 'hide' => 'after', 'attrs' => [ qw/title endTime channelName:.\/Channel[@channelName]/ ], 'template' => "__title__ (__channelName__) Ends: __endTime__", + 'template_html' => "__title__ (__channelName__) Ends: __endTime__
", 'rewrite' => { '/endTime/' => { '.*T' => '' }, }, @@ -197,6 +210,7 @@ my @blocks = ( 'type' => 'xpath', 'xpath' => '//Status/Scheduled/Program', 'defaults' => 'schedule', + 'template_html' =>'', 'hide' => 'after', 'subs' => { 'find_next' => sub { @@ -238,6 +252,7 @@ my @blocks = ( 'attrs' => [ qw/_total_total _total_used/ ], 'commify' => [ qw/_total_total _total_used/ ], 'template' => "Total space is ___total_total__ MB, with ___total_used__ MB used (__percent__)", + 'template_html' => "Total space is ___total_total__ MB, with ___total_used__ MB used (__percent__)
", 'format' => 'one line', 'optional' => 1, 'subs' => { @@ -257,6 +272,7 @@ my @blocks = ( 'attrs' => [ qw/drive_total_total drive_total_used/ ], 'commify' => [ qw/drive_total_total drive_total_used/ ], 'template' => "Total space is __drive_total_total__ MB, with __drive_total_used__ MB used (__percent__)", + 'template_html' => "Total space is __drive_total_total__ MB, with __drive_total_used__ MB used (__percent__)
", 'format' => 'one line', 'optional' => 1, 'subs' => { @@ -275,6 +291,7 @@ my @blocks = ( 'attrs' => [ qw/total used/ ], 'commify' => [ qw/total used/ ], 'template' => "Total space is __total__ MB, with __used__ MB used (__percent__)", + 'template_html' => "Total space is __total__ MB, with __used__ MB used (__percent__)
", 'format' => 'one line', 'optional' => 1, 'subs' => { @@ -293,6 +310,7 @@ my @blocks = ( 'attrs' => [ qw/id total used/ ], 'commify' => [ qw/total used/ ], 'template' => "Total space for group __id__ is __total__ MB, with __used__ MB used (__percent__)", + 'template_html' => "Total space for group __id__ is __total__ MB, with __used__ MB used (__percent__)
", 'filter' => { 'id' => sub { return $_[0] eq 'total' }, 'used' => sub { @@ -314,6 +332,7 @@ my @blocks = ( 'type' => 'sub', 'format' => 'one line', 'template' => '__next_time__', + 'template_html' => '__next_time__
', 'rewrite' => { '&next_time' => sub { return $next_time @@ -339,7 +358,8 @@ my @blocks = ( 'next_time' => sub { return $_[0] eq 'now' } }, 'sub' => sub { - return substitute_vars($_[0], { 'next_time' => $next_time }); + my ($str, $str_html) = substitute_vars($_[0], { 'next_time' => $next_time }); + return $str; } }, @@ -416,8 +436,11 @@ my %version; my $exit_value = 0; my $title = "MythTV status for $host"; +my $title_html = "

MythTV status for $host

"; my $output = "$title\n"; +my $html = "$title_html\n"; $output .= '=' x length($title) . "\n"; +$html .= '=' x length($title_html) . "\n"; for my $block (@blocks) { $block->{'format'} ||= 'multi line'; @@ -432,6 +455,7 @@ for my $block (@blocks) { if (defined $block->{'hide'} && lc($block->{'hide'}) eq 'after') { $hide = 1; } else { + warn "Nexting display = $display{ $block->{'name'}} \n" if $verbose; next; } } @@ -447,16 +471,18 @@ for my $block (@blocks) { } my $result = undef; + my $result_html = undef; $warn_present = 0; if ($block->{'type'} eq 'xpath') { $xml ||= load_xml(); - $result = process_xml($block, $xml); + ($result,$result_html) = process_xml($block, $xml); } elsif ($block->{'type'} eq 'sub') { - - $result = &{ $block->{'sub'} }($block) - if defined $block->{'sub'}; + if (defined $block->{'sub'}) { + $result = &{ $block->{'sub'} }($block); + $result_html = &{ $block->{'sub'} }($block) if (defined $block->{'template_html'}); + } } if (defined $result && $result ne '' && ! defined $hide) { @@ -469,6 +495,16 @@ for my $block (@blocks) { $output .= $result . "\n\n"; } } + + if (defined $result_html && $result_html ne '' && ! defined $hide) { + $exit_value ||= $warn_present; + if ($block->{'format'} eq 'one line') { + push @oneliners_html, [ $block->{'name'}, $result_html ]; + } else { + $html .= "$block->{'name'}:\n"; + $html .= $result_html . "\n\n"; + } + } } # Deal with the one liners. @@ -493,6 +529,28 @@ if (scalar(@oneliners) > 0) { $output =~ s/^One Liners:\nPlace holder\n/$oneliners/m; } +# Deal with the one liners for html - this should probably be a sub now +if (scalar(@oneliners_html) > 0) { + + # Find the longest header + $length = 0; + for $line (@oneliners_html) { + if (length($line->[0]) > $length) { + $length = length($line->[0]); + } + } + + # Put the one liners together, with leading dots to the colon. + my $oneliners_html = ""; + for $line (@oneliners_html) { + $oneliners_html .= "$line->[0]" + . ('.' x ($length - length($line->[0]))) . ": $line->[1]\n"; + } + + # What a hacky way of putting the one liners where I want them... + $html =~ s/^One Liners:\nPlace holder\n/$oneliners_html/m; +} + # Either print the status out, or email it. if ($return_code_only) { exit $exit_value; @@ -508,13 +566,33 @@ if ($return_code_only) { $suffix = "MULTIPLE WARNINGS"; } + # my $html = '\n'; + # $html .= $output; + # $html .= "\n"; + my $mail = MIME::Entity->build( To => \@email, Subject => $title . (defined $suffix ? " - $suffix" : ''), - Data => $output + Type => 'multipart/alternative' + ); + + my $alt = MIME::Entity->build(Type => 'multipart/alternative'); + $mail->attach( + Data => $output, + Type => 'text/plain', + Encoding => '7bit' + ); + $mail->attach( + Data => $html, + Type => 'text/html', + Encoding => '7bit' ); $mail->send('sendmail'); + + # $mail->dump_skeleton(); + # print $mail->as_string; + } } @@ -649,6 +727,7 @@ sub process_xml { if (scalar(@$items) == 0); my @lines; + my @lines_html; for my $item (@{ $items }) { my %vars; for my $key (@{ $block->{'attrs'} }) { @@ -661,12 +740,17 @@ sub process_xml { } } - my $str = substitute_vars($block, \%vars); + my ($str,$str_html) = substitute_vars($block, \%vars); + push @lines, $str if defined $str; + push @lines_html, $str_html + if defined $str_html; + } - return join("\n", @lines); + return ( join("\n", @lines) , join("\n", @lines_html) ); + } sub process_conflicts { @@ -704,7 +788,7 @@ sub process_conflicts { 'chanNum' => $show->{'channum'}, ); - my $str = substitute_vars($block, \%vars); + my ($str, $str_html) = substitute_vars($block, \%vars); push @lines, $str if defined $str; } @@ -756,7 +840,7 @@ sub process_auto_expire { 'chanNum' => $show->{'chanid'}, ); - my $str = substitute_vars($block, \%vars); + my ($str, $str_html) = substitute_vars($block, \%vars); push @lines, $str if defined $str; @@ -775,6 +859,7 @@ sub substitute_vars { if defined $block->{'commify'}; my $template = $block->{'template'}; + my $template_html = defined $block->{'template_html'} ? $block->{'template_html'} : undef ; my $skip = undef; my ($key, $value); while (($key, $value) = (each %{ $vars })) { @@ -815,6 +900,9 @@ sub substitute_vars { if defined $commify{$key}; $template =~ s/__${key}__/$value/g; + if (defined $template_html) { + $template_html =~ s/__${key}__/$value/g; + } } my ($name, $sub); @@ -825,7 +913,7 @@ sub substitute_vars { if defined $value; } - return defined $skip ? undef : $template; + return (defined $skip ? undef : $template, $template_html ); } # Work out the disk space percentage, possibly setting a flag that we should -- 2.30.2