#!/usr/bin/ruby1.8
require "rmail"

class RMail::Message
  def every_part(&block)
    self.each_part{|prt|
      if prt.multipart?
	prt.every_part(&block)
      else
	block.call(prt)
      end}
  end
end

msg=RMail::Parser.read(STDIN.read)

if msg.body and msg.body.length <= 1 and msg.header.subject.nil?
  print "FUCK"; exit (0)
end

out=msg.header.to_s
if msg.multipart?
  msg.every_part{|prt|
    out+=prt.header.to_s
    if type=prt.header.content_type
      case type
      when /text/i
	if prt.decode
	  out+=prt.decode
	else
	  out+=prt.to_s
	end
      when /image|octet/i
      end
    else
      out+=prt.body.to_s
    end}
else
  if (msg.header.subject.nil? or msg.header.subject == "") and msg.body == ""
    exit 0
  end
  if not msg.to_s[/base64/i]
    out+=msg.decode if msg.decode
  end
end
print out
