Support modified UTF-7 in folder names. From Edmund Grimley Evans,
authorThomas Roessler <roessler@does-not-exist.org>
Sun, 16 Jul 2000 15:35:14 +0000 (15:35 +0000)
committerThomas Roessler <roessler@does-not-exist.org>
Sun, 16 Jul 2000 15:35:14 +0000 (15:35 +0000)
modified by Brendan Cully.

imap/Makefile.am
imap/browse.c
imap/imap.c
imap/imap_private.h
imap/message.c
imap/utf7.c [new file with mode: 0644]
imap/util.c

index 27bc38ad10bd5b08b1834c2c74928fe7baa7e275..124893b2ebf798378856545d6926d35382bd3f71 100644 (file)
@@ -19,4 +19,4 @@ noinst_LIBRARIES = libimap.a
 noinst_HEADERS = imap_private.h imap_socket.h md5.h message.h $(SSLHEADERS)
 
 libimap_a_SOURCES = auth.c browse.c command.c imap.c imap.h md5c.c message.c \
-       socket.c util.c $(GSSSOURCES) $(SSLSOURCES)
+       socket.c utf7.c util.c $(GSSSOURCES) $(SSLSOURCES)
index f08414b8c72b094a421f275c89c43e4b7d39d375..a8676b37fbe5263833ec0be184c07be52f775519 100644 (file)
@@ -123,6 +123,10 @@ int imap_init_browse (char *path, struct browser_state *state)
   if (mx.mbox && mx.mbox[0] != '\0')
   {
     imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
+    imap_munge_mbox_name (buf, sizeof (buf), mbox);
+    imap_unquote_string(buf); /* As kludgy as it gets */
+    mbox[sizeof (mbox) - 1] = '\0';
+    strncpy (mbox, buf, sizeof (mbox) - 1);
     n = mutt_strlen (mbox);
 
     dprint (3, (debugfile, "imap_init_browse: mbox: %s\n", mbox));
@@ -143,7 +147,7 @@ int imap_init_browse (char *path, struct browser_state *state)
 
         if (cur_folder)
         {
-          imap_unquote_string (cur_folder);
+          imap_unmunge_mbox_name (cur_folder);
 
           if (!noinferiors && cur_folder[0] &&
             (n = strlen (mbox)) < LONG_STRING-1)
@@ -295,7 +299,6 @@ static int add_list_result (CONNECTION *conn, const char *seq, const char *cmd,
 
     if (name)
     {
-      imap_unquote_string (name);
       /* Let a parent folder never be selectable for navigation */
       if (isparent)
         noselect = 1;
@@ -321,7 +324,7 @@ static void imap_add_folder (char delim, char *folder, int noselect,
   if (imap_parse_path (state->folder, &mx))
     return;
 
-  imap_unquote_string (folder);
+  imap_unmunge_mbox_name (folder);
 
   if (state->entrylen + 1 == state->entrymax)
   {
index 051a32e9858714c1cdf8d62a42b0a649943ea247..754c92f1345ae338eb69c7927817e4799f604385 100644 (file)
@@ -46,7 +46,7 @@ int imap_create_mailbox (CONTEXT* ctx, char* mailbox)
 {
   char buf[LONG_STRING], mbox[LONG_STRING];
 
-  imap_quote_string (mbox, sizeof (mbox), mailbox);
+  imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
   snprintf (buf, sizeof (buf), "CREATE %s", mbox);
       
   if (imap_exec (buf, sizeof (buf), CTX_DATA, buf, 0) != 0)
@@ -206,7 +206,7 @@ int imap_reopen_mailbox (CONTEXT *ctx, int *index_hint)
   ctx->subj_hash = hash_create (1031);
 
   mutt_message (_("Reopening mailbox... %s"), CTX_DATA->selected_mailbox);
-  imap_quote_string (buf, sizeof (buf), CTX_DATA->selected_mailbox);
+  imap_munge_mbox_name (buf, sizeof (buf), CTX_DATA->selected_mailbox);
   imap_make_sequence (seq, sizeof (seq));
   snprintf (bufout, sizeof (bufout), "%s STATUS %s (MESSAGES)\r\n", seq, buf);
   mutt_socket_write (CTX_DATA->conn, bufout);
@@ -389,7 +389,7 @@ static int imap_check_acl (IMAP_DATA *idata)
   char buf[LONG_STRING];
   char mbox[LONG_STRING];
 
-  imap_quote_string (mbox, sizeof(mbox), idata->selected_mailbox);
+  imap_munge_mbox_name (mbox, sizeof(mbox), idata->selected_mailbox);
   snprintf (buf, sizeof (buf), "MYRIGHTS %s", mbox);
   if (imap_exec (buf, sizeof (buf), idata, buf, 0) != 0)
   {
@@ -579,7 +579,7 @@ int imap_open_mailbox (CONTEXT *ctx)
   memset (idata->rights, 0, (RIGHTSMAX+7)/8);
 
   mutt_message (_("Selecting %s..."), idata->selected_mailbox);
-  imap_quote_string (buf, sizeof(buf), idata->selected_mailbox);
+  imap_munge_mbox_name (buf, sizeof(buf), idata->selected_mailbox);
   imap_make_sequence (seq, sizeof (seq));
   snprintf (bufout, sizeof (bufout), "%s %s %s\r\n", seq,
     ctx->readonly ? "EXAMINE" : "SELECT", buf);
@@ -778,7 +778,7 @@ int imap_open_mailbox_append (CONTEXT *ctx)
 
   imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
 
-  imap_quote_string (mbox, sizeof (mbox), mailbox);
+  imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
                                
   if (mutt_bit_isset(idata->capabilities,IMAP4REV1))
   {
@@ -1223,7 +1223,7 @@ int imap_mailbox_check (char *path, int new)
       strcpy (mx.mbox, buf);
 
   imap_make_sequence (seq, sizeof (seq));              
-  imap_quote_string (mbox, sizeof(mbox), buf);
+  imap_munge_mbox_name (mbox, sizeof(mbox), buf);
   strfcpy (mbox_unquoted, buf, sizeof (mbox_unquoted));
 
   /* The draft IMAP implementor's guide warns againts using the STATUS
@@ -1398,7 +1398,7 @@ int imap_subscribe (char *path, int subscribe)
     mutt_message (_("Subscribing to %s..."), buf);
   else
     mutt_message (_("Unsubscribing to %s..."), buf);
-  imap_quote_string (mbox, sizeof(mbox), buf);
+  imap_munge_mbox_name (mbox, sizeof(mbox), buf);
 
   snprintf (buf, sizeof (buf), "%s %s", subscribe ? "SUBSCRIBE" :
     "UNSUBSCRIBE", mbox);
@@ -1468,7 +1468,7 @@ int imap_complete(char* dest, size_t dlen, char* path) {
     if (list_word)
     {
       /* store unquoted */
-      imap_unquote_string (list_word);
+      imap_unmunge_mbox_name (list_word);
 
       /* if the folder isn't selectable, append delimiter to force browse
        * to enter it on second tab. */
index 1d5cc24ed5e37d9365634b2dd3f144c87700212d..0999deb941733c4dff87d9950dcc2cdb776a7f6a 100644 (file)
@@ -207,5 +207,12 @@ char* imap_get_qualifier (char* buf);
 char* imap_next_word (char* s);
 void imap_quote_string (char* dest, size_t slen, const char* src);
 void imap_unquote_string (char* s);
+void imap_munge_mbox_name (char *dest, size_t dlen, const char *src);
+void imap_unmunge_mbox_name (char *s);
 int imap_wordcasecmp(const char *a, const char *b);
+
+/* utf8.c */
+void mutt_utf7_encode (char **s);
+void mutt_utf7_decode (char **s);
+
 #endif
index 41916cb59619989ab5ce42264e38ada9c161899d..e608fc2ae254fd43e7655fda4943b684183a5c5c 100644 (file)
@@ -399,7 +399,7 @@ int imap_append_message (CONTEXT *ctx, MESSAGE *msg)
   
   mutt_message _("Sending APPEND command ...");
 
-  imap_quote_string (mbox, sizeof (mbox), mailbox);
+  imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
   imap_make_sequence (seq, sizeof (seq));
   snprintf (buf, sizeof (buf), "%s APPEND %s {%d}\r\n", seq, mbox, len);
 
@@ -492,6 +492,7 @@ int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
   char buf[HUGE_STRING];
   char cmd[LONG_STRING];
   char mbox[LONG_STRING];
+  char mmbox[LONG_STRING];
   int rc;
   int n;
   IMAP_MBOX mx;
@@ -531,7 +532,8 @@ int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
 
   /* let's get it on */
   strncpy (mbox, cmd, sizeof (mbox));
-  snprintf (cmd, sizeof (cmd), "COPY %s \"%s\"", buf, mbox);
+  imap_munge_mbox_name (mmbox, sizeof (mmbox), cmd);
+  snprintf (cmd, sizeof (cmd), "COPY %s %s", buf, mmbox);
   rc = imap_exec (buf, sizeof (buf), CTX_DATA, cmd, IMAP_CMD_FAIL_OK);
   if (rc == -2)
   {
diff --git a/imap/utf7.c b/imap/utf7.c
new file mode 100644 (file)
index 0000000..3c56086
--- /dev/null
@@ -0,0 +1,268 @@
+/*
+ * Copyright (C) 2000 Edmund Grimley Evans <edmundo@rano.org>
+ * 
+ *     This program is free software; you can redistribute it and/or modify
+ *     it under the terms of the GNU General Public License as published by
+ *     the Free Software Foundation; either version 2 of the License, or
+ *     (at your option) any later version.
+ * 
+ *     This program is distributed in the hope that it will be useful,
+ *     but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *     GNU General Public License for more details.
+ * 
+ *     You should have received a copy of the GNU General Public License
+ *     along with this program; if not, write to the Free Software
+ *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ */ 
+
+#include "mutt.h"
+#include "charset.h"
+
+static int Index_64[128] = {
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
+    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, 63,-1,-1,-1,
+    52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
+    -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
+    15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
+    -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
+    41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
+};
+
+static char B64Chars[64] = {
+  'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
+  'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
+  'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
+  't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
+  '8', '9', '+', ','
+};
+
+/*
+ * Convert the data (u7,u7len) from RFC 2060's UTF-7 to UTF-8.
+ * The result is null-terminated and returned, and also stored
+ * in (*u8,*u8len) if u8 or u8len is non-zero.
+ * If input data is invalid, return 0 and don't store anything.
+ * RFC 2060 obviously intends the encoding to be unique (see
+ * point 5 in section 5.1.3), so we reject any non-canonical
+ * form, such as &ACY- (instead of &-) or &AMA-&AMA- (instead
+ * of &AMAAwA-).
+ */
+char *utf7_to_utf8 (const char *u7, size_t u7len, char **u8, size_t *u8len)
+{
+  char *buf, *p;
+  int b, ch, k;
+
+  p = buf = safe_malloc (u7len + u7len / 8 + 1);
+
+  for (; u7len; u7++, u7len--)
+  {
+    if (*u7 == '&')
+    {
+      u7++, u7len--;
+
+      if (u7len && *u7 == '-')
+      {
+       *p++ = '&';
+       continue;
+      }
+
+      ch = 0;
+      k = 10;
+      for (; u7len; u7++, u7len--)
+      {
+       if ((*u7 & 0x80) || (b = Index_64[(int)*u7]) == -1)
+         break;
+       if (k > 0)
+       {
+         ch |= b << k;
+         k -= 6;
+       }
+       else
+       {
+         ch |= b >> (-k);
+         if (ch < 0x80)
+         {
+           if (0x20 <= ch && ch < 0x7f)
+             /* Printable US-ASCII */
+             goto bail;
+           *p++ = ch;
+         }
+         else if (ch < 0x800)
+         {
+           *p++ = 0xc0 | (ch >> 6);
+           *p++ = 0x80 | (ch & 0x3f);
+         }
+         else
+         {
+           *p++ = 0xe0 | (ch >> 12);
+           *p++ = 0x80 | ((ch >> 6) & 0x3f);
+           *p++ = 0x80 | (ch & 0x3f);
+         }
+         ch = (b << (16 + k)) & 0xffff;
+         k += 10;
+       }
+      }
+      if (ch || k < 6)
+       /* Non-zero or too many extra bits */
+       goto bail;
+      if (!u7len || *u7 != '-')
+       /* BASE64 not properly terminated */
+       goto bail;
+      if (u7len > 2 && u7[1] == '&' && u7[2] != '-')
+       /* Adjacent BASE64 sections */
+       goto bail;
+    }
+    else if (*u7 < 0x20 || *u7 >= 0x7f)
+      /* Not printable US-ASCII */
+      goto bail;
+    else
+      *p++ = *u7;
+  }
+  *p++ = '\0';
+  if (u8len)
+    *u8len = p - buf;
+  p = realloc (buf, p - buf);
+  p = p ? p : buf;
+  if (u8)
+    *u8 = p;
+  return p;
+
+ bail:
+  free (buf);
+  return 0;
+}
+
+/*
+ * Convert the data (u8,u8len) from UTF-8 to RFC 2060's UTF-7.
+ * The result is null-terminated and returned, and also stored
+ * in (*u7,*u7len) if u7 or u7len is non-zero.
+ * Unicode characters above U+FFFF are replaced by U+FFFE.
+ * If input data is invalid, return 0 and don't store anything.
+ */
+char *utf8_to_utf7 (const char *u8, size_t u8len, char **u7, size_t *u7len)
+{
+  char *buf, *p;
+  int ch;
+  int n, i, b = 0, k = 0;
+  int base64 = 0;
+
+  p = buf = safe_malloc (u8len * 2 + 1);
+
+  while (u8len)
+  {
+    unsigned char c = *u8;
+
+    if (c < 0x80)
+      ch = c, n = 0;
+    else if (c < 0xc2)
+      goto bail;
+    else if (c < 0xe0)
+      ch = c & 0x1f, n = 1;
+    else if (c < 0xf0)
+      ch = c & 0x0f, n = 2;
+    else if (c < 0xf8)
+      ch = c & 0x07, n = 3;
+    else if (c < 0xfc)
+      ch = c & 0x03, n = 4;
+    else if (c < 0xfe)
+      ch = c & 0x01, n = 5;
+    else
+      goto bail;
+
+    u8++, u8len--;
+    if (n > u8len)
+      goto bail;
+    for (i = 0; i < n; i++)
+    {
+      if ((u8[i] & 0xc0) != 0x80)
+       goto bail;
+      ch = (ch << 6) | (u8[i] & 0x3f);
+    }
+    if (n > 1 && !(ch >> (n * 5 + 1)))
+      goto bail;
+    u8 += n, u8len -= n;
+
+    if (ch < 0x20 || ch >= 0x7f)
+    {
+      if (!base64)
+      {
+       *p++ = '&';
+       base64 = 1;
+       b = 0;
+       k = 10;
+      }
+      if (ch & ~0xffff)
+       ch = 0xfffe;
+      *p++ = B64Chars[b | ch >> k];
+      k -= 6;
+      for (; k >= 0; k -= 6)
+       *p++ = B64Chars[(ch >> k) & 0x3f];
+      b = (ch << (-k)) & 0x3f;
+      k += 16;
+    }
+    else
+    {
+      if (base64)
+      {
+       if (k > 10)
+         *p++ = B64Chars[b];
+       *p++ = '-';
+       base64 = 0;
+      }
+      *p++ = ch;
+      if (ch == '&')
+       *p++ = '-';
+    }
+  }
+
+  if (u8len)
+  {
+    free (buf);
+    return 0;
+  }
+
+  if (base64)
+  {
+    if (k > 10)
+      *p++ = B64Chars[b];
+    *p++ = '-';
+  }
+
+  *p++ = '\0';
+  if (u7len)
+    *u7len = p - buf;
+  p = realloc (buf, p - buf);
+  p = p ? p : buf;
+  if (u7)
+    *u7 = p;
+  return p;
+
+ bail:
+  free (buf);
+  return 0;
+}
+
+void mutt_utf7_encode (char **s)
+{
+  if (Charset)
+  {
+    char *t = safe_strdup (*s);
+    if (!mutt_convert_string (&t, Charset, "UTF-8"))
+      utf8_to_utf7 (t, strlen (t), s, 0);
+    safe_free ((void **) &t);
+  }
+}
+
+void mutt_utf7_decode (char **s)
+{
+  if (Charset)
+  {
+    char *t = utf7_to_utf8 (*s, strlen (*s), 0, 0);
+    if (t && !mutt_convert_string (&t, "UTF-8", Charset))
+    {
+      free (*s);
+      *s = t;
+    }
+  }
+}
index e36d91d27e21454a5b24e56fbf3335fb87b9a391..3a6d1540e39423dfbac5a39215c2f94e01f7a319 100644 (file)
@@ -309,6 +309,38 @@ void imap_unquote_string (char *s)
   *d = '\0';
 }
 
+/*
+ * Quoting and UTF-7 conversion
+ */
+
+void imap_munge_mbox_name (char *dest, size_t dlen, const char *src)
+{
+  char *buf;
+
+  buf = safe_strdup (src);
+  mutt_utf7_encode (&buf);
+
+  imap_quote_string (dest, dlen, buf);
+
+  free (buf);
+}
+
+void imap_unmunge_mbox_name (char *s)
+{
+  char *buf;
+
+  imap_unquote_string(s);
+
+  buf = safe_strdup (s);
+  if (buf)
+  {
+    mutt_utf7_decode (&buf);
+    strncpy (s, buf, strlen (s));
+  }
+  
+  free (buf);
+}
+
 /* imap_wordcasecmp: find word a in word list b */
 int imap_wordcasecmp(const char *a, const char *b)
 {