#!/usr/bin/perl # Examines an e-mail reply template from Mutt for quoted text in HTML format. # If HTML is detected, the user is prompted for conversion to plain text. # # Designed for Mutt options "edit_headers"==yes and "indent_string"=="> ". # Usage: processreply FILE # Copyright 2015-2024 by Gero Treuner # This program is free software: you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation, either version 3 of the License, or (at your option) any later # version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program. If not, see . use strict; use warnings; # Methods # ------- sub queryUser() { print "\nMessage appears to be HTML formatted - convert to plain text? [n|Y] "; my $r = ; $r =~ /^[^Nn]/; } # Initialization # -------------- # HTML conversion command: Must take filename from last argument and write to stdout. my $htmlConversionCommand = '/usr/bin/w3m -dump -cols 72 -F -T text/html'; # Quoting command: Must read from stdin and write to stdout. my $quoteConversionCommand = "sed -e 's/^/> /'"; if (!$ARGV[0]) { die('No input file given'); } my $inputFile = $ARGV[0]; if (! open(FILE, $inputFile)) { die('Can\'t open file "' . $inputFile . '"'); } my $state = 'getBody'; while ($state ne 'done') { my $line = ; if ($state eq 'getBody' && $line =~ /^$/) { $state = 'getQuotedText'; } if ($state eq 'getQuotedText' && $line =~ /^> (.+)/) { # at first quoted line with at least 1 more character: # check for doctype=html declaration or html/head|font/p tag if ($1 =~ /(?:]+)*<(?:(?:!DOCTYPE\s+)?HTML|HEAD|FONT|P)(?:\s[^>]+)?>/i) { # found HTML # ---------- my $filePosition = tell(FILE) - length($line); if (&queryUser()) { $state = "convert"; require File::Temp; use File::Temp (); my $path = $inputFile =~ /^(.+)\/[^\/]+$/ ? $1 : '.'; my $tempFile = File::Temp->new(DIR => $path, TEMPLATE => 'fromhtml.XXXXXXXXXX', UNLINK => 0); print $tempFile substr($line, 2); do { $line = ; print $tempFile substr($line, 2); } while (! eof(FILE)); close(FILE); close($tempFile); truncate($inputFile, $filePosition); $line = "$htmlConversionCommand '" . $tempFile->filename . "' | $quoteConversionCommand >> '$inputFile'"; system($line); unlink($tempFile->filename); } } $state = 'done'; } if (eof(FILE)) { $state = 'done'; } } close(FILE);