diff -uNr mutt-1.5.21.orig/PATCHES mutt-1.5.21/PATCHES
--- mutt-1.5.21.orig/PATCHES	2010-11-08 09:44:31.692997710 +0900
+++ mutt-1.5.21/PATCHES	2010-11-11 19:01:03.212940788 +0900
@@ -0,0 +1 @@
+patch-1.5.21.tt.sanitize_ja.1
diff -uNr mutt-1.5.21.orig/charset.c mutt-1.5.21/charset.c
--- mutt-1.5.21.orig/charset.c	2010-11-08 09:44:31.552997939 +0900
+++ mutt-1.5.21/charset.c	2010-11-11 19:01:03.222943016 +0900
@@ -481,6 +481,9 @@
   if (!s || !*s)
     return 0;
 
+  if (option (OPTSANITIZEJACHARS) && !ascii_strncasecmp (from, "iso-2022-jp", 11))
+    mutt_sanitize_ja_chars (s, mutt_strlen(s), 0);
+
   if (to && from && (cd = mutt_iconv_open (to, from, flags)) != (iconv_t)-1)
   {
     int len;
@@ -677,3 +680,188 @@
 
   return -1;
 }
+
+/*
+ * mutt_sanitize_ja_chars() 
+ *   Adapted by TAKIZAWA Takashi <taki@cyber.email.ne.jp>
+ *
+ * - It replaces undefined KANJI characters to GETA mark.
+ * - It replaces character of 'JIS X 0201 kana' to '?'.
+ * - If $charset is EUC-JP, it replaces third character 'J' of 
+ *   escape sequence switching to 'JIS X 0201 latin' to 'B' indicating
+ *   'US-ASCII'.
+ * - If $charset is Shift_JIS, it replaces third character 'B' of 
+ *   escape sequence switching to 'US-ASCII' to 'J' indicating
+ *   'JIS X 0201 latin'.
+ */
+
+#define ASCII 0
+#define JISX0201LATIN 1
+#define JISX0201KANA 2
+#define JISX0208 3
+#define OTHER_CS 4
+
+void mutt_sanitize_ja_chars(char *s, size_t len, int keep_state)
+{
+  static int cs = ASCII;
+  static int kanji_cont = 0;
+  static int illegal_kanji = 0;
+  static int es = 0;
+  static char pes = '\0';
+  static char ascii_3rd_char = 'B';
+  static char jisx0201_3rd_char = 'J';
+
+  char *p = s;
+  char *p1 = NULL;
+  unsigned char c;
+
+  if (!keep_state || *p == 0x1b) /* consideration about mbstate's buffer */
+  {
+    if (!ascii_strcasecmp (Charset, "euc-jp"))
+      jisx0201_3rd_char = 'B';
+    else if (!ascii_strcasecmp (Charset, "shift_jis"))
+      ascii_3rd_char = 'J';
+    cs = ASCII;
+    kanji_cont = 0;
+    illegal_kanji = 0;
+    es = 0;
+    pes = '\0';
+  }
+  
+  for (;p - s < len;p++)
+  {
+    if (es == 0)
+    {
+      if (*p == 0x1b)
+	es++;
+      else
+      {
+	switch (cs)
+	{
+	case ASCII:
+	case JISX0201LATIN:
+	  break;
+	case JISX0201KANA:
+	  *p = '?';
+	  break;
+	case JISX0208:
+	  /* replace ku-ten code from 9 to 15 and 85 or more to "GETA MARK" */
+	  c = (unsigned char)*p;
+	  if (! kanji_cont)
+	  {
+	    if ((size_t)(p - s + 1) == len)
+	      return; /* the last character is a primary byte of KANJI */
+	    if (c <= 0x20 || (c >= 0x29 && c <= 0x2f) 
+		|| (c >= 0x75 && c <= 0xa0))
+	      illegal_kanji = 1;
+	    kanji_cont = 1;
+	    p1 = p;
+	  }
+	  else
+	  {
+	    if (c <= 0x20 || c >= 0x7f)
+	      illegal_kanji = 1;
+	    if (illegal_kanji && p1)
+	      *p1 = 0x22, *p = 0x2e;
+	    kanji_cont = 0;
+	    illegal_kanji = 0;
+	  }
+	  break;
+	}
+      }
+    }
+    else if (es == 1)
+    {
+      if (*p == '$' || (*p >= '(' && *p <= '/' && *p != ','))
+      {
+	es++;
+	pes = *p;
+      }
+      else
+      {
+	es = 0;
+	return; /* broken */
+      }
+    }
+    else if (es == 2)
+    {
+      if (pes == '(')
+      {
+	switch (*p)
+	{
+	case 'B':
+	  cs = ASCII, *p = ascii_3rd_char;
+	  break;
+	case 'J':
+	  cs = JISX0201LATIN, *p = jisx0201_3rd_char;
+	  break;
+	case 'I':
+	  /* ready to replace character to '?' */
+	  cs = JISX0201KANA, *p = ascii_3rd_char;
+	  break;
+	default:
+	  cs = OTHER_CS;
+	}
+	es = 0;
+      }
+      else if (pes == '$')
+      {
+	switch (*p)
+	{
+	case '@': /* JIS X 0208-1978 */
+	case 'B': /* JIS X 0208-1983 */
+	  cs = JISX0208;
+	  es = 0;
+	  break;
+	case 'A':
+	  cs = OTHER_CS; /* GB 2312 */
+	  es = 0;
+	  break;
+	case '(':
+	case ')':
+	case '*':
+	case '+':
+	case '-':
+	case '.':
+	case '/':
+	  es++;
+	  break;
+	default:
+	  es = 0;
+	  return; /* broken */
+	}
+      }
+      else
+      {
+	cs = OTHER_CS;
+	es = 0;
+      }
+    }
+    else /* es == 3 */
+    {
+      cs = OTHER_CS;
+      es = 0;
+    }
+  }
+}
+
+int mutt_copy_bytes_sanitize_ja (FILE *in, FILE *out, size_t size)
+{
+  char buf[2048];
+  size_t chunk;
+
+  mutt_sanitize_ja_chars (NULL, 0, 0);
+  while (size > 0)
+  {
+    chunk = (size > sizeof (buf)) ? sizeof (buf) : size;
+    if ((chunk = fread (buf, 1, chunk, in)) < 1)
+      break;
+    mutt_sanitize_ja_chars (buf, chunk, 1);
+    if (fwrite (buf, 1, chunk, out) != chunk)
+      return (-1);
+    size -= chunk;
+  }
+
+  return 0;
+}
+
diff -uNr mutt-1.5.21.orig/charset.h mutt-1.5.21/charset.h
--- mutt-1.5.21.orig/charset.h	2010-11-08 09:44:31.552997939 +0900
+++ mutt-1.5.21/charset.h	2010-11-11 19:01:03.222943016 +0900
@@ -36,6 +36,9 @@
 
 int mutt_convert_string (char **, const char *, const char *, int);
 
+void mutt_sanitize_ja_chars (char *, size_t, int);
+int mutt_copy_bytes_sanitize_ja (FILE *, FILE *, size_t);
+
 iconv_t mutt_iconv_open (const char *, const char *, int);
 size_t mutt_iconv (iconv_t, ICONV_CONST char **, size_t *, char **, size_t *, ICONV_CONST char **, const char *);
 
diff -uNr mutt-1.5.21.orig/handler.c mutt-1.5.21/handler.c
--- mutt-1.5.21.orig/handler.c	2010-11-08 09:44:31.652997836 +0900
+++ mutt-1.5.21/handler.c	2010-11-11 19:01:03.222943016 +0900
@@ -100,6 +100,9 @@
     return;
   }
 
+  if (option (OPTSANITIZEJACHARS) && strchr (bufi, 0x1b))
+    mutt_sanitize_ja_chars (bufi, *l, 1);
+
   ib = bufi, ibl = *l;
   for (;;)
   {
@@ -1234,6 +1237,7 @@
   int piped = FALSE;
   pid_t thepid;
   int rc = 0;
+  char *charset;
 
   snprintf (type, sizeof (type), "%s/%s", TYPE (a), a->subtype);
   rfc1524_mailcap_lookup (a, type, entry, M_AUTOVIEW);
@@ -1264,6 +1268,10 @@
       return -1;
     }
     
+    charset = mutt_get_parameter ("charset", a->parameter);
+    if (charset && option (OPTSANITIZEJACHARS) && !ascii_strncasecmp (charset,"iso-2022-jp", 11))
+      mutt_copy_bytes_sanitize_ja (s->fpin, fpin, a->length);
+    else
     mutt_copy_bytes (s->fpin, fpin, a->length);
 
     if(!piped)
diff -uNr mutt-1.5.21.orig/init.h mutt-1.5.21/init.h
--- mutt-1.5.21.orig/init.h	2010-11-08 09:44:31.552997939 +0900
+++ mutt-1.5.21/init.h	2010-11-11 19:01:03.222943016 +0900
@@ -176,7 +175,7 @@
   ** If \fIset\fP, Mutt will prompt you for carbon-copy (Cc) recipients before
   ** editing the body of an outgoing message.
   */
-  { "assumed_charset", DT_STR, R_NONE, UL &AssumedCharset, UL 0},
+  { "assumed_charset", DT_STR, R_NONE, UL &AssumedCharset, UL  "iso-2022-jp:utf-8:euc-jp:shift_jis"},
   /*
   ** .pp
   ** This variable is a colon-separated list of character encoding
@@ -2378,6 +2378,28 @@
   ** that mutt \fIgenerates\fP this kind of encoding.  Instead, mutt will
   ** unconditionally use the encoding specified in RFC2231.
   */
+  { "sanitize_ja_chars",   DT_BOOL, R_NONE, OPTSANITIZEJACHARS, 1 },
+  /*
+  ** .pp
+  ** When set, Japanese "platform dependent characters" (illegal
+  ** characters for iso-2022-jp charset; mainly used by MS-Windows
+  ** mailers) are substituted to special character, GETA mark ('ESC $$ B " .
+  ** ESC ( B' in iso-2022-jp), and JIS X 0201 kana characters
+  ** (only for "ESC ) I" cases) are also substituted to "?" to
+  ** prevent garbage characters. JIS X 0201 kana characters are
+  ** not substituted if they appear in 8bit form.
+  ** .pp
+  ** This fixes another Japanese encoding issue. In case $$charset
+  ** is set to "EUC-JP", which does not contain JIS X 0201 roman
+  ** character set, the JIS X 0201 roman part of received messages
+  ** encoded in iso-2022-jp can not be converted to EUC-JP.
+  ** On the other hand, the ASCII part can not be converted to
+  ** Shift_JIS, which does not contain ASCII character set. Thus,
+  ** the converted characters are garbled in these cases. When this
+  ** option is set, the JIS X 0201 roman escape sequence and the
+  ** ASCII escape sequence are replaced appropriately to prevent
+  ** the output from being garbled.
+  */
   { "save_address",	DT_BOOL, R_NONE, OPTSAVEADDRESS, 0 },
   /*
   ** .pp
diff -uNr mutt-1.5.21.orig/mutt.h mutt-1.5.21/mutt.h
--- mutt-1.5.21.orig/mutt.h	2010-11-08 09:44:31.532998225 +0900
+++ mutt-1.5.21/mutt.h	2010-11-11 19:01:03.232941539 +0900
@@ -415,6 +415,7 @@
   OPTREVNAME,
   OPTREVREAL,
   OPTRFC2047PARAMS,
+  OPTSANITIZEJACHARS,
   OPTSAVEADDRESS,
   OPTSAVEEMPTY,
   OPTSAVENAME,
diff -uNr mutt-1.5.21.orig/rfc2047.c mutt-1.5.21/rfc2047.c
--- mutt-1.5.21.orig/rfc2047.c	2010-11-08 09:44:31.652997836 +0900
+++ mutt-1.5.21/rfc2047.c	2010-11-11 19:01:03.232941539 +0900
@@ -62,6 +62,9 @@
   size_t obl, n;
   int e;
 
+  if (option (OPTSANITIZEJACHARS) && !ascii_strncasecmp (from, "iso-2022-jp",
+11))
+    mutt_sanitize_ja_chars ((char *) f, flen, 0);
   cd = mutt_iconv_open (to, from, 0);
   if (cd == (iconv_t)(-1))
     return (size_t)(-1);
