| 1 | ##---------------------------------------------------------------------------## | ||
| 2 | ## File: | ||
| 3 | ## $Id: mhtxtenrich.pl,v 2.11 2010/12/31 20:34:00 ehood Exp $ | ||
| 4 | ## Author: | ||
| 5 | ## Earl Hood mhonarc@mhonarc.org | ||
| 6 | ## Description: | ||
| 7 | ## Library defines a routine for MHonArc to filter text/enriched | ||
| 8 | ## data. | ||
| 9 | ## | ||
| 10 | ## Filter routine can be registered with the following: | ||
| 11 | ## | ||
| 12 | ## <MIMEFILTERS> | ||
| 13 | ## text/enriched;m2h_text_enriched::filter;mhtxtenrich.pl | ||
| 14 | ## text/richtext;m2h_text_enriched::filter;mhtxtenrich.pl | ||
| 15 | ## </MIMEFILTERS> | ||
| 16 | ## | ||
| 17 | ##---------------------------------------------------------------------------## | ||
| 18 | ## MHonArc -- Internet mail-to-HTML converter | ||
| 19 | ## Copyright (C) 1997-2002 Earl Hood, mhonarc@mhonarc.org | ||
| 20 | ## | ||
| 21 | ## This program is free software; you can redistribute it and/or modify | ||
| 22 | ## it under the terms of the GNU General Public License as published by | ||
| 23 | ## the Free Software Foundation; either version 2 of the License, or | ||
| 24 | ## (at your option) any later version. | ||
| 25 | ## | ||
| 26 | ## This program is distributed in the hope that it will be useful, | ||
| 27 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 28 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 29 | ## GNU General Public License for more details. | ||
| 30 | ## | ||
| 31 | ## You should have received a copy of the GNU General Public License | ||
| 32 | ## along with this program; if not, write to the Free Software | ||
| 33 | ## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | ||
| 34 | ## 02111-1307, USA | ||
| 35 | ##---------------------------------------------------------------------------## | ||
| 36 | |||
| 37 | package m2h_text_enriched; | ||
| 38 | |||
| 39 | my %enriched_tags = ( | ||
| 40 | 'bigger' => 1, | ||
| 41 | 'bold' => 1, | ||
| 42 | 'center' => 1, | ||
| 43 | 'color' => 1, | ||
| 44 | 'comment' => 1, | ||
| 45 | 'excerpt' => 1, | ||
| 46 | 'fixed' => 1, | ||
| 47 | 'flushboth' => 1, | ||
| 48 | 'flushleft' => 1, | ||
| 49 | 'flushright' => 1, | ||
| 50 | 'fontfamily' => 1, | ||
| 51 | 'indent' => 1, | ||
| 52 | 'indentright' => 1, | ||
| 53 | 'italic' => 1, | ||
| 54 | 'lang' => 1, | ||
| 55 | 'lt' => 1, | ||
| 56 | 'nl' => 1, | ||
| 57 | 'nofill' => 1, | ||
| 58 | 'paraindent' => 1, | ||
| 59 | 'param' => 1, | ||
| 60 | 'samepage' => 1, | ||
| 61 | 'signature' => 1, | ||
| 62 | 'smaller' => 1, | ||
| 63 | 'subscript' => 1, | ||
| 64 | 'superscript' => 1, | ||
| 65 | 'underline' => 1, | ||
| 66 | ); | ||
| 67 | |||
| 68 | my %special_to_char = ( | ||
| 69 | 'lt' => '<', | ||
| 70 | 'gt' => '>', | ||
| 71 | ); | ||
| 72 | |||
| 73 | ##--------------------------------------------------------------------------- | ||
| 74 | ## Filter routine. | ||
| 75 | ## XXX: Need to update this filter. However, does anyone still use | ||
| 76 | ## text/enriched anymore. | ||
| 77 | ## | ||
| 78 | sub filter { | ||
| 79 | my ($fields, $data, $isdecode, $args) = @_; | ||
| 80 | my ($innofill, $chunk); | ||
| 81 | my $charset = $fields->{'x-mha-charset'}; | ||
| 82 | my ($charcnv, $real_charset_name) = | ||
| 83 | readmail::MAILload_charset_converter($charset); | ||
| 84 | my $ret = ""; | ||
| 85 | $args = "" unless defined($args); | ||
| 86 | |||
| 87 | ## Get content-type | ||
| 88 | my ($ctype) = $fields->{'content-type'}[0] =~ m%^\s*([\w\-\./]+)%; | ||
| 89 | my $richtext = $ctype =~ /\btext\/richtext\b/i; | ||
| 90 | |||
| 91 | if (defined($charcnv) && defined(&$charcnv)) { | ||
| 92 | $$data = &$charcnv($$data, $real_charset_name); | ||
| 93 | } else { | ||
| 94 | mhonarc::htmlize($data); | ||
| 95 | warn qq/\n/, | ||
| 96 | qq/Warning: Unrecognized character set: $charset\n/, | ||
| 97 | qq/ Message-Id: <$mhonarc::MHAmsgid>\n/, | ||
| 98 | qq/ Message Subject: /, $fields->{'x-mha-subject'}, | ||
| 99 | qq/\n/, qq/ Message Number: $mhonarc::MHAmsgnum\n/ | ||
| 100 | unless ($charcnv eq '-decode-'); | ||
| 101 | } | ||
| 102 | ## Fixup any EOL mess | ||
| 103 | $$data =~ s/\r?\n/\n/g; | ||
| 104 | $$data =~ s/\r/\n/g; | ||
| 105 | |||
| 106 | # translate back <>'s for tag processing | ||
| 107 | $$data =~ s/&([lg]t);/$special_to_char{$1}/g; | ||
| 108 | |||
| 109 | ## Convert specials | ||
| 110 | if (!$richtext) { | ||
| 111 | $$data =~ s/<</\</g; | ||
| 112 | } | ||
| 113 | |||
| 114 | ## Make sure only non-enriched tags are escaped | ||
| 115 | $$data =~ s{<(/?)([^>]*)>} | ||
| 116 | { | ||
| 117 | my $eot = $1; | ||
| 118 | my $tag = lc $2; | ||
| 119 | $tag =~ s/\s+//g; | ||
| 120 | ($enriched_tags{$tag}) ? '<'.$eot.$tag.'>' : '<'.$eot.$tag.'>'; | ||
| 121 | }gexs; | ||
| 122 | |||
| 123 | $innofill = 0; | ||
| 124 | foreach $chunk (split(m|(</?nofill>)|i, $$data)) { | ||
| 125 | if ($chunk =~ m|<nofill>|i) { | ||
| 126 | $ret .= '<pre>'; | ||
| 127 | $innofill = 1; | ||
| 128 | next; | ||
| 129 | } | ||
| 130 | if ($chunk =~ m|</nofill>|i) { | ||
| 131 | $ret .= '</pre>'; | ||
| 132 | $innofill = 0; | ||
| 133 | next; | ||
| 134 | } | ||
| 135 | convert_tags(\$chunk, $richtext); | ||
| 136 | if (!$richtext && !$innofill) { | ||
| 137 | $chunk =~ s/(\n\s*)/&nl_seq_to_brs($1)/ge; | ||
| 138 | } | ||
| 139 | $ret .= $chunk; | ||
| 140 | } | ||
| 141 | $ret; | ||
| 142 | } | ||
| 143 | |||
| 144 | ##--------------------------------------------------------------------------- | ||
| 145 | ## convert_tags translates text/enriched commands to HTML tags. | ||
| 146 | ## | ||
| 147 | sub convert_tags { | ||
| 148 | my $str = shift; | ||
| 149 | my $richtext = shift; | ||
| 150 | |||
| 151 | $$str =~ s{<comment\s*>.*?</comment\s*>}{}gis; | ||
| 152 | |||
| 153 | $$str =~ s{<(/?)bold\s*>}{<$1b>}gi; | ||
| 154 | $$str =~ s{<(/?)italic\s*>}{<$1i>}gi; | ||
| 155 | $$str =~ s{<(/?)underline\s*>}{<$1u>}gi; | ||
| 156 | $$str =~ s{<(/?)fixed\s*>}{<$1tt>}gi; | ||
| 157 | $$str =~ s{<(/?)smaller\s*>}{<$1small>}gi; | ||
| 158 | $$str =~ s{<(/?)bigger\s*>}{<$1big>}gi; | ||
| 159 | $$str =~ s{<(/?)signature\s*>}{<$1pre>}gi; | ||
| 160 | |||
| 161 | $$str =~ s{<fontfamily\s*>\s*<param\s*>([^<]+)</param\s*>} | ||
| 162 | {<font face="$1">}gix; | ||
| 163 | $$str =~ s|</fontfamily\s*>|</font>|gi; | ||
| 164 | $$str =~ s{<color\s*>\s*<param\s*>\s*(\S+)\s*</param\s*>} | ||
| 165 | {<font color="$1">}gix; | ||
| 166 | $$str =~ s|</color\s*>|</font>|gi; | ||
| 167 | $$str =~ s|<center\s*>|<p align="center">|gi; | ||
| 168 | $$str =~ s|</center\s*>|</p>|gi; | ||
| 169 | $$str =~ s|<flushleft\s*>|<p align="left">|gi; | ||
| 170 | $$str =~ s|</flushleft\s*>|</p>|gi; | ||
| 171 | $$str =~ s|<flushright\s*>|<p align="right">|gi; | ||
| 172 | $$str =~ s|</flushright\s*>|</p>|gi; | ||
| 173 | $$str =~ s|<flushboth\s*>|<p align="justify">|gi; | ||
| 174 | $$str =~ s|</flushboth\s*>|</p>|gi; | ||
| 175 | $$str =~ s|<paraindent\s*>\s*<param\s*>([^<]*)</param\s*>|<blockquote>|gi; | ||
| 176 | $$str =~ s|</paraindent\s*>|</blockquote>|gi; | ||
| 177 | |||
| 178 | $$str =~ s|<excerpt\s*>\s*(<param\s*>([^<]*)</param\s*>)?|<blockquote>|gi; | ||
| 179 | $$str =~ s|</excerpt\s*>|</blockquote>|gi; | ||
| 180 | |||
| 181 | $$str =~ s|<lang\s*>\s*<param\s*>([^<]*)</param\s*>|<div lang="$1">|gi; | ||
| 182 | $$str =~ s|</lang\s*>|</div>|gi; | ||
| 183 | |||
| 184 | # richtext commands | ||
| 185 | $$str =~ s{</?samepage\s*>}{}gi; | ||
| 186 | $$str =~ s{<(/?)subscript\s*>}{<$1sub>}gi; | ||
| 187 | $$str =~ s{<(/?)superscript\s*>}{<$1sup>}gi; | ||
| 188 | $$str =~ s{<lt\s*>}{<}gi; | ||
| 189 | $$str =~ s{<np\s*>}{\f}gi; | ||
| 190 | $$str =~ s{<paragraph\s*>}{<p>}gi; | ||
| 191 | $$str =~ s{</paragraph\s*>\n?}{</p>}gis; | ||
| 192 | $$str =~ s{<indent\s*>}{<p style="margin-left: 1em;">}gi; | ||
| 193 | $$str =~ s{</indent\s*>}{</p>}gi; | ||
| 194 | $$str =~ s{<indentright\s*>}{<p style="margin-right: 1em;">}gi; | ||
| 195 | $$str =~ s{</indentright\s*>}{</p>}gi; | ||
| 196 | |||
| 197 | if ($richtext) { | ||
| 198 | $$str =~ s{<nl\s*>\n?}{<br>}gis; | ||
| 199 | } else { | ||
| 200 | $$str =~ s{<nl\s*>}{}gis; | ||
| 201 | } | ||
| 202 | |||
| 203 | # Cleanup bad tags | ||
| 204 | $$str =~ s{</?(?:para(?:m|indent)|excerpt|lang|color|fontfamily)\s*>}{}g; | ||
| 205 | } | ||
| 206 | |||
| 207 | ##--------------------------------------------------------------------------- | ||
| 208 | ## nl_seq_to_brs returns a "<BR>" string based on the number | ||
| 209 | ## of eols in a string. | ||
| 210 | ## | ||
| 211 | sub nl_seq_to_brs { | ||
| 212 | my ($str) = shift; | ||
| 213 | my ($n); | ||
| 214 | $n = $str =~ tr/\n/\n/; | ||
| 215 | --$n; | ||
| 216 | if ($n <= 0) { | ||
| 217 | return " "; | ||
| 218 | } else { | ||
| 219 | return "<br>\n" x $n; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | ##--------------------------------------------------------------------------- | ||
| 224 | ## preserve_space returns a string with all spaces and tabs | ||
| 225 | ## converted to nbsps. | ||
| 226 | ## | ||
| 227 | sub preserve_space { | ||
| 228 | my ($str) = shift; | ||
| 229 | 1 while $str =~ | ||
| 230 | s/^([^\t]*)(\t+)/$1 . ' ' x (length($2) * 8 - length($1) % 8)/e; | ||
| 231 | $str =~ s/ /\ /g; | ||
| 232 | $str; | ||
| 233 | } | ||
| 234 | |||
| 235 | ##--------------------------------------------------------------------------- | ||
| 236 | 1; |