Forth demime script

  • Follow


Sometimes, brain-dead services send mime-encoded mail without alternative
text to my ASCII mail and news reader. In really festering cases the mime
encoding is wrapped around HTLM in such a way that a browser won't 
understand the raw text. 

Complaining doesn't help as these services simply don't understand the 
issue. 

Ignoring e.g. KLM ticket reservations etc. is not really a option.

This Forth script (that doubtlessly can be compressed to a single line 
of lury) converts the message to something that can be pasted or piped 
into an external browser.

What it does:  

'=' <hexdigit1> <hexdigit2>  
                Becomes the ASCII character with value  
		val(hexdigit1)*16 + val(hexdigit2). 

'=' <CR> <LF>  
'=' <LF>        are ignored (thrown away).

The resulting text may have an excess of empty lines. I don't know how
to fix that (other than replace n empty lines with a single empty line).

You will need the Forth streams library.

-marcel

-- ------------------------------------------------------------------------
NEEDS -streams

STREAM mime

0 VALUE hi
0 VALUE lo
0 VALUE hx

: -hex? ( u -- flag ) 
	#16 DIGIT? IF  hx 4 LSHIFT OR TO hx FALSE EXIT  ENDIF DROP TRUE ;

: process-byte? ( -- eof? )
	SGETCH mime DUP EOF  = IF  DROP TRUE EXIT  ENDIF
		    DUP '=' <> IF  SPUTCH mime FALSE EXIT  ENDIF DROP
	0 TO hx
	SGETCH mime TO hi 
	hi EOF = IF  '=' SPUTCH mime  TRUE EXIT  ENDIF
	hi ^J  = IF  FALSE EXIT ENDIF
	hi ^M  = IF  0 TO hx  
	       ELSE  hi -hex? IF  '=' SPUTCH mime  hi SPUTCH mime FALSE EXIT  ENDIF 
	      ENDIF
	SGETCH mime TO lo 
	lo EOF = IF  '=' SPUTCH mime  hi SPUTCH mime  TRUE EXIT  ENDIF  
	lo ^J  = hi ^M = AND IF  FALSE EXIT  ENDIF 
	lo -hex? IF  '=' SPUTCH mime  hi SPUTCH mime  lo SPUTCH mime FALSE EXIT  ENDIF
	hx SPUTCH mime  FALSE ;

: DEMIME ( basename -- )
	2DUP S" .txt"  $+ $OPEN-INPUT  mime
	     S" .html" $+ $OPEN-OUTPUT mime 

	BEGIN  process-byte? UNTIL

	SCLOSE-INPUT  mime
	SCLOSE-OUTPUT mime ;

: .ABOUT CR .~ Try: <basename> DEMIME -- S" aap" unmime  converts aap.txt to aap.html~ ;

	CR .ABOUT 
-- ------------------------------------------------------------------------

0
Reply mhx (2133) 12/17/2009 7:47:38 PM

On Dec 17, 1:47=A0pm, m...@iae.nl (Marcel Hendrix) wrote:
> Sometimes, brain-dead services send mime-encoded mail without alternative
> text to my ASCII mail and news reader. In really festering cases the mime
> encoding is wrapped around HTLM in such a way that a browser won't
> understand the raw text.
>
> Complaining doesn't help as these services simply don't understand the
> issue.
>
> Ignoring e.g. KLM ticket reservations etc. is not really a option.
>
> This Forth script (that doubtlessly can be compressed to a single line
> of lury) converts the message to something that can be pasted or piped
> into an external browser.
>
> What it does: =A0
>
> '=3D' <hexdigit1> <hexdigit2> =A0
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Becomes the ASCII character with value =
=A0
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 val(hexdigit1)*16 + val(hexdigit2).
>
> '=3D' <CR> <LF> =A0
> '=3D' <LF> =A0 =A0 =A0 =A0are ignored (thrown away).
>
> The resulting text may have an excess of empty lines. I don't know how
> to fix that (other than replace n empty lines with a single empty line).
>
> You will need the Forth streams library.
>
> -marcel
>
> -- ----------------------------------------------------------------------=
--
> NEEDS -streams
>
> STREAM mime
>
> 0 VALUE hi
> 0 VALUE lo
> 0 VALUE hx
>
> : -hex? ( u -- flag )
> =A0 =A0 =A0 =A0 #16 DIGIT? IF =A0hx 4 LSHIFT OR TO hx FALSE EXIT =A0ENDIF=
 DROP TRUE ;
>
> : process-byte? ( -- eof? )
> =A0 =A0 =A0 =A0 SGETCH mime DUP EOF =A0=3D IF =A0DROP TRUE EXIT =A0ENDIF
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 DUP '=3D' <> IF =A0SPUTCH mime FA=
LSE EXIT =A0ENDIF DROP
> =A0 =A0 =A0 =A0 0 TO hx
> =A0 =A0 =A0 =A0 SGETCH mime TO hi
> =A0 =A0 =A0 =A0 hi EOF =3D IF =A0'=3D' SPUTCH mime =A0TRUE EXIT =A0ENDIF
> =A0 =A0 =A0 =A0 hi ^J =A0=3D IF =A0FALSE EXIT ENDIF
> =A0 =A0 =A0 =A0 hi ^M =A0=3D IF =A00 TO hx =A0
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ELSE =A0hi -hex? IF =A0'=3D' SPUTCH mime =
=A0hi SPUTCH mime FALSE EXIT =A0ENDIF
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 ENDIF
> =A0 =A0 =A0 =A0 SGETCH mime TO lo
> =A0 =A0 =A0 =A0 lo EOF =3D IF =A0'=3D' SPUTCH mime =A0hi SPUTCH mime =A0T=
RUE EXIT =A0ENDIF =A0
> =A0 =A0 =A0 =A0 lo ^J =A0=3D hi ^M =3D AND IF =A0FALSE EXIT =A0ENDIF
> =A0 =A0 =A0 =A0 lo -hex? IF =A0'=3D' SPUTCH mime =A0hi SPUTCH mime =A0lo =
SPUTCH mime FALSE EXIT =A0ENDIF
> =A0 =A0 =A0 =A0 hx SPUTCH mime =A0FALSE ;
>
> : DEMIME ( basename -- )
> =A0 =A0 =A0 =A0 2DUP S" .txt" =A0$+ $OPEN-INPUT =A0mime
> =A0 =A0 =A0 =A0 =A0 =A0 =A0S" .html" $+ $OPEN-OUTPUT mime
>
> =A0 =A0 =A0 =A0 BEGIN =A0process-byte? UNTIL
>
> =A0 =A0 =A0 =A0 SCLOSE-INPUT =A0mime
> =A0 =A0 =A0 =A0 SCLOSE-OUTPUT mime ;
>
> : .ABOUT CR .~ Try: <basename> DEMIME -- S" aap" unmime =A0converts aap.t=
xt to aap.html~ ;
>
> =A0 =A0 =A0 =A0 CR .ABOUT
> -- ----------------------------------------------------------------------=
--

Care to give us some sample input and output?
0
Reply w_a_x_man 12/17/2009 9:15:05 PM


Marcel Hendrix wrote:

> Sometimes, brain-dead services send mime-encoded mail without
> alternative text to my ASCII mail and news reader. In really
> festering cases the mime encoding is wrapped around HTLM in such a
> way that a browser won't understand the raw text. 
> 
> Complaining doesn't help as these services simply don't understand
> the issue. 
> 
> Ignoring e.g. KLM ticket reservations etc. is not really a option.
> 
> This Forth script (that doubtlessly can be compressed to a single
> line of lury) converts the message to something that can be pasted or
> piped into an external browser.
> 
> What it does:  
> 
> '=' <hexdigit1> <hexdigit2>  
>                 Becomes the ASCII character with value  
> 		val(hexdigit1)*16 + val(hexdigit2). 
> 
> '=' <CR> <LF>  
> '=' <LF>        are ignored (thrown away).
> 
> The resulting text may have an excess of empty lines. I don't know how
> to fix that (other than replace n empty lines with a single empty
> line).
> 
> You will need the Forth streams library.
> 
> -marcel

Ruby:

x = DATA.read.gsub( /=\r?\n/, "" ).gsub( /=[\dA-F]{2}/ ){|s|
  s[1,2].to_i(16).chr }

p x
puts x

__END__
This is a example of a quoted-printable text file.  This might contain =
some special characters such as:=20
equal sign =3D, dollar sign $, or even extended characters such as the =
cent sign =A2 or foreign characters =C0=C6=DF


=== output ===
"This is a example of a quoted-printable text file.  This might contain
some special characters such as: \nequal sign =, dollar sign $, or even
extended characters such as the cent sign \242 or foreign characters
\300\306\337\n"
This is a example of a quoted-printable text file.  This might contain
some special characters such as:
equal sign =, dollar sign $, or even extended characters such as the
cent sign \xA2 or foreign characters \xC0\xC6\xDF

-- 

0
Reply William 12/17/2009 9:45:28 PM

mhx@iae.nl (Marcel Hendrix) writes:
>Sometimes, brain-dead services send mime-encoded mail without alternative
>text to my ASCII mail and news reader.

You mean the quoted-printable encoding.  MIME can get worse, much
worse.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2009: http://www.euroforth.org/ef09/
0
Reply anton 12/19/2009 9:09:08 PM

w_a_x_man <w_a_x_man@yahoo.com> writes:
>On Dec 17, 1:47=A0pm, m...@iae.nl (Marcel Hendrix) wrote:
>> What it does: =A0
>>
>> '=3D' <hexdigit1> <hexdigit2> =A0
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 Becomes the ASCII character with value =
>=A0
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 val(hexdigit1)*16 + val(hexdigit2).
>>
>> '=3D' <CR> <LF> =A0
>> '=3D' <LF> =A0 =A0 =A0 =A0are ignored (thrown away).
>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0ELSE =A0hi -hex? IF =A0'=3D' SPUTCH mime =
>=A0hi SPUTCH mime FALSE EXIT =A0ENDIF
....
>Care to give us some sample input and output?

Is that irony?

If not, the output is what you intended to post, the input is what you
posted.  Please switch your news reader to use 8-bit encoding instead
of quoted-printable.  Or turn off MIME completely.

- anton
-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2009: http://www.euroforth.org/ef09/
0
Reply anton 12/19/2009 9:11:09 PM

4 Replies
76 Views

(page loaded in 0.339 seconds)

9/8/2012 11:08:04 PM


Reply: