Re: Javamail - MBOX problem

Countach <[email protected]>
Newsgroups gmane.comp.java.classpath.extensions.javamail
Message-ID <[email protected]>
Chris Burdess wrote:

> Countach,
>
> Please do send in a patch and I'll try to correlate it with Chris' 
> submission.


Sorry for the confusion, I am Chris (using two email addresses).  Diff 
attached.

_______________________________________________
Classpathx-javamail mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/classpathx-javamail
mboxdiff.txt (text/plain, 7.6 KB)
--- classpathx/mail/source/gnu/mail/providers/mbox/MboxFolder.java	2005-01-06 06:05:43.000000000 +1100
+++ mailnaa/mail/source/gnu/mail/providers/mbox/MboxFolder.java	2005-02-15 09:37:59.671875000 +1100
@@ -79,6 +79,7 @@
   static final String FROM = "From ";
 	
   File file;
+  String name;
   MboxMessage[] messages;
   boolean open;
   boolean readOnly;
@@ -90,11 +91,15 @@
   /**
    * Constructor.
    */
-  protected MboxFolder(Store store, String filename, boolean inbox) 
+  protected MboxFolder(MboxStore store, String name, boolean inbox)
   {
     super(store);
-    
-    file = new File(filename);
+    this.name = name;
+    if (0 < name.length() && name.charAt(0) == '/') {
+      file = new File(canonicalNameToLocal(name));
+    } else {
+      file = new File(store.getMailRootDir(), canonicalNameToLocal(name));
+    }
     if (file.exists() && file.isDirectory())
       {
         type = HOLDS_FOLDERS;
@@ -112,9 +117,9 @@
   /**
    * Constructor.
    */
-  protected MboxFolder(Store store, String filename) 
+  protected MboxFolder(MboxStore store, String name)
   {
-    this(store, filename, false);
+    this(store, name, false);
   }
 
   /**
@@ -138,7 +143,7 @@
       {
         return "INBOX";
       }
-    return file.getPath();
+    return name;
   }
 
   /**
@@ -642,9 +647,36 @@
   public Folder getParent() 
     throws MessagingException 
   {
-    return store.getFolder(file.getParent());
+    StringBuffer newName = new StringBuffer(name);
+    while (newName.charAt(newName.length()-1) == '/') {
+      newName.setLength(newName.length()-1);
+    }
+    if (newName.equals("")) {
+      return store.getDefaultFolder();
+    }
+    while (newName.charAt(newName.length()-1) != '/') {
+      newName.setLength(newName.length()-1);
+    }
+    return store.getFolder(newName.toString());
+  }
+
+static String canonicalNameToLocal(String name) {
+  // convert into a valid filename for this platform
+  StringBuffer buf = new StringBuffer();
+  if ('/' != File.separatorChar) {
+    String rtn = name.replace('/', File.separatorChar);
+    // This allows you to use "//c:/foo/bar" as a hack for "c:/foo/bar".
+    // This is because the MboxStore specification specifies that only
+    // names starting with the separator ( / ) are treated absolute.
+    if (2 <= name.length() && name.charAt(0) == '/' && name.charAt(1) == '/') {
+      rtn = rtn.substring(2);
   }
-
+    return rtn;
+  }
+  else {
+   return name;
+  }
+}
   /**
    * Returns the subfolders of this folder.
    */
@@ -661,9 +693,7 @@
         Folder[] folders = new Folder[files.length];
         for (int i = 0; i < files.length; i++)
           {
-            folders[i] = store.getFolder(file.getAbsolutePath() +
-                                         File.separator +
-                                         files[i]);
+            folders[i] = store.getFolder(makeName(name, files[i]));
           }
         return folders;
       }
@@ -689,9 +719,7 @@
         Folder[] folders = new Folder[files.length];
         for (int i = 0; i < files.length; i++)
           {
-            folders[i] = store.getFolder(file.getAbsolutePath() +
-                                         File.separator +
-                                         files[i]);
+            folders[i] = store.getFolder(makeName(name, files[i]));
           }
         return folders;
       } 
@@ -832,10 +860,10 @@
   {
     try 
       {
-        String filename = folder.getFullName();
-        if (filename != null) 
+        if (folder.getFullName() != null)
           {
-            if (!file.renameTo(new File(filename)))
+        File newfile = new File(((MboxStore)getStore()).getMailRootDir(), canonicalNameToLocal(folder.getFullName()));
+            if (!file.renameTo(newfile))
               {
                 return false;
               }
@@ -856,11 +884,11 @@
   /**
    * Returns the subfolder of this folder with the specified name.
    */
-  public Folder getFolder(String filename) 
+  public Folder getFolder(String fname)
     throws MessagingException 
   {
-    String INBOX = "INBOX";
-    if (INBOX.equalsIgnoreCase(filename))
+/*    String INBOX = "/INBOX";
+    if (INBOX.equalsIgnoreCase(fname))
       {
         try
           {
@@ -870,10 +898,29 @@
           {
             // fall back to standard behaviour
           }
-      }
-    return store.getFolder(file.getAbsolutePath() + File.separator + filename);
+      } */
+
+if (0 < fname.length() && fname.charAt(0) == '/') {
+  return store.getFolder(fname);
+} else {
+  return store.getFolder(makeName(name, fname));
+}
   }
 
+  static String makeName(String parent, String child) {
+      StringBuffer rtn = new StringBuffer();
+	  if (parent.length() == 0) {
+		  rtn.append(child);
+	  } else if (parent.charAt(parent.length() - 1) == '/') {
+		  rtn.append(parent);
+		  rtn.append(child);
+	  } else {
+		  rtn.append(parent);
+		  rtn.append('/');
+		  rtn.append(child);
+      }
+      return rtn.toString();
+  }
   /**
    * Checks if the current file is or is supposed to be
    * compressed. Uses the filename to figure it out.
--- classpathx/mail/source/gnu/mail/providers/mbox/MboxStore.java	2005-01-06 06:05:44.000000000 +1100
+++ mailnaa/mail/source/gnu/mail/providers/mbox/MboxStore.java	2005-02-15 09:37:59.718750000 +1100
@@ -41,6 +41,7 @@
 import gnu.mail.treeutil.StatusSource;
 
 import gnu.inet.util.Logger;
+import java.io.IOException;
 
 /**
  * The storage class implementing the Mbox mailbox file format.
@@ -83,12 +84,16 @@
   {
     return true;
   }
+  public Folder getDefaultFolder()
+    throws MessagingException
+  {
+    return getFolder("");
+  }
 
   /**
    * Returns the default folder.
    */
-  public Folder getDefaultFolder() 
-    throws MessagingException 
+  public File getMailRootDir()
   {
     // If the url used to contruct the store references a file directly,
     // return this file.
@@ -97,7 +102,7 @@
         String file = url.getFile();
         if (file != null && file.length() > 0) 
           {
-            return getFolder(file);
+            return new File(file);
           }
       }
     // Otherwise attempt to return a sensible root folder.
@@ -123,21 +128,24 @@
             mailhome = "/";
           }
       }
-    return getFolder(mailhome);
+    return new File(mailhome);
   }
 
+
+
+
   /**
    * Returns the folder with the specified filename.
    */
-  public Folder getFolder(String filename) 
+  public Folder getFolder(String name)
     throws MessagingException
   {
-    if (filename == null)
+    if (name == null)
       {
-        filename = "";
+        name = "";
       }
     boolean inbox = false;
-    if (INBOX.equalsIgnoreCase(filename)) 
+    if (INBOX.equalsIgnoreCase(name) && !new File(getMailRootDir(), name).exists())
       {
         // First try the session property mail.mbox.inbox.
         String inboxname = session.getProperty("mail.mbox.inbox");
@@ -176,29 +184,13 @@
           }
         if (inboxname!=null)
           {
-            filename = inboxname;
+            name = inboxname;
             inbox = true;
           }
         // otherwise we assume it is actually a file called "inbox"
       }
-    
-    // convert into a valid filename for this platform
-    StringBuffer buf = new StringBuffer();
-    if (filename.length() < 1 || filename.charAt(0) != separatorChar)
-      {
-        buf.append(File.separator);
-      }
-    if (separatorChar!=File.separatorChar)
-      {
-        buf.append(filename.replace(separatorChar, File.separatorChar));
-      }
-    else
-      {
-        buf.append(filename);
-      }
-    filename = buf.toString();
-    
-    return new MboxFolder(this, filename, inbox);
+    return new MboxFolder(this, name, inbox);
+//return new MboxFolder(this, name, false);
   }
 
   /*
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.