#!/usr/bin/perl

use strict;

use MIME::QuotedPrint ();
use MIME::Base64 ();

#my(@header) = <>;
#print(decode_str(join('', @header)));

while (<>) {
	print(decode_str($_));
}

sub decode_str() {
	my($str) = @_;
	my($charset, $encode_method, $encoded, $decoded, $before, $after);
	($before, $charset, $encode_method, $encoded, $after) = ($str =~ /^(.*)=\?([\w\-\d]+)\?(Q|B)\?(.*)\?=(.*)$/io);
	while ($encode_method ne '') {
		if ($encode_method =~ /^Q$/i) {
			$encoded =~ s/_/ /g;
			$decoded = MIME::QuotedPrint::decode($encoded);
			$str = "$before$decoded$after\n";
		} elsif ($encode_method =~ /^B$/i) {
			$decoded = MIME::Base64::decode($encoded);
			$str = "$before$decoded$after\n";
		}
		($before, $charset, $encode_method, $encoded, $after) = ($str =~ /^(.*)=\?([\w\-\d]+)\?(Q|B)\?(.*)\?=(.*)$/io);
	}
	return($str);
}
